Skip to content

Commit 2c047e6

Browse files
authored
[clang] Add fixit for using declaration with a (qualified) namespace (#94762)
For `using std::literals`, we now output: error: using declaration cannot refer to a namespace 4 | using std::literals; | ~~~~~^ note: did you mean 'using namespace'? 4 | using std::literals; | ^ | namespace Previously, we didn't have the note. This only fires for qualified namespaces. Just `using std;` doesn't trigger this, since using declarations without cxx scope specifier are rejected earlier. Making that work is an exercise for future selves :)
1 parent 66df614 commit 2c047e6

File tree

4 files changed

+11
-1
lines changed

4 files changed

+11
-1
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

+2
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,8 @@ def note_using_decl_class_member_workaround : Note<
607607
"a const variable|a constexpr variable}0 instead">;
608608
def err_using_decl_can_not_refer_to_namespace : Error<
609609
"using declaration cannot refer to a namespace">;
610+
def note_namespace_using_decl : Note<
611+
"did you mean 'using namespace'?">;
610612
def warn_cxx17_compat_using_decl_scoped_enumerator: Warning<
611613
"using declaration naming a scoped enumerator is incompatible with "
612614
"C++ standards before C++20">, InGroup<CXXPre20Compat>, DefaultIgnore;

clang/lib/Sema/SemaDeclCXX.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -13079,7 +13079,10 @@ NamedDecl *Sema::BuildUsingDeclaration(
1307913079
// A using-declaration shall not name a namespace.
1308013080
if (R.getAsSingle<NamespaceDecl>()) {
1308113081
Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_namespace)
13082-
<< SS.getRange();
13082+
<< SS.getRange();
13083+
// Suggest using 'using namespace ...' instead.
13084+
Diag(SS.getBeginLoc(), diag::note_namespace_using_decl)
13085+
<< FixItHint::CreateInsertion(SS.getBeginLoc(), "namespace ");
1308313086
return BuildInvalid();
1308413087
}
1308513088

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

34
namespace A {
45
namespace B { }
56
}
67

8+
// CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:7-[[@LINE+1]]:7}:"namespace "
79
using A::B; // expected-error{{using declaration cannot refer to a namespace}}
10+
// expected-note@-1 {{did you mean 'using namespace'?}}

clang/test/CXX/drs/cwg4xx.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -941,8 +941,10 @@ namespace cwg460 { // cwg460: yes
941941
// expected-error@-1 {{using declaration requires a qualified name}}
942942
using cwg460::X;
943943
// expected-error@-1 {{using declaration cannot refer to a namespace}}
944+
// expected-note@-2 {{did you mean 'using namespace'?}}
944945
using X::Q;
945946
// expected-error@-1 {{using declaration cannot refer to a namespace}}
947+
// expected-note@-2 {{did you mean 'using namespace'?}}
946948
}
947949
}
948950

0 commit comments

Comments
 (0)