Skip to content

[clang] Add fixit for using declaration with a (qualified) namespace #94762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,8 @@ def note_using_decl_class_member_workaround : Note<
"a const variable|a constexpr variable}0 instead">;
def err_using_decl_can_not_refer_to_namespace : Error<
"using declaration cannot refer to a namespace">;
def note_namespace_using_decl : Note<
"did you mean 'using namespace'?">;
def warn_cxx17_compat_using_decl_scoped_enumerator: Warning<
"using declaration naming a scoped enumerator is incompatible with "
"C++ standards before C++20">, InGroup<CXXPre20Compat>, DefaultIgnore;
Expand Down
5 changes: 4 additions & 1 deletion clang/lib/Sema/SemaDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13079,7 +13079,10 @@ NamedDecl *Sema::BuildUsingDeclaration(
// A using-declaration shall not name a namespace.
if (R.getAsSingle<NamespaceDecl>()) {
Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_namespace)
<< SS.getRange();
<< SS.getRange();
// Suggest using 'using namespace ...' instead.
Diag(SS.getBeginLoc(), diag::note_namespace_using_decl)
<< FixItHint::CreateInsertion(SS.getBeginLoc(), "namespace ");
return BuildInvalid();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose another exercise for our future selves could be to recover as if the user typed "using namespace"

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// RUN: %clang_cc1 -fsyntax-only -verify %s
// RUN: not %clang_cc1 -fsyntax-only -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s

namespace A {
namespace B { }
}

// CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:7-[[@LINE+1]]:7}:"namespace "
using A::B; // expected-error{{using declaration cannot refer to a namespace}}
// expected-note@-1 {{did you mean 'using namespace'?}}
2 changes: 2 additions & 0 deletions clang/test/CXX/drs/cwg4xx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -941,8 +941,10 @@ namespace cwg460 { // cwg460: yes
// expected-error@-1 {{using declaration requires a qualified name}}
using cwg460::X;
// expected-error@-1 {{using declaration cannot refer to a namespace}}
// expected-note@-2 {{did you mean 'using namespace'?}}
using X::Q;
// expected-error@-1 {{using declaration cannot refer to a namespace}}
// expected-note@-2 {{did you mean 'using namespace'?}}
}
}

Expand Down
Loading