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

via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 7 08:22:22 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Nico Weber (nico)

<details>
<summary>Changes</summary>

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 :)

---
Full diff: https://github.com/llvm/llvm-project/pull/94762.diff


4 Files Affected:

- (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+2) 
- (modified) clang/lib/Sema/SemaDeclCXX.cpp (+3) 
- (modified) clang/test/CXX/dcl.dcl/basic.namespace/namespace.udecl/p6-cxx11.cpp (+3) 
- (modified) clang/test/CXX/drs/cwg4xx.cpp (+2) 


``````````diff
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 9f0b6f5a36389..13a73b39135b4 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -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;
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 3c28da1b077cd..b5ffa75f04ffa 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -13080,6 +13080,9 @@ NamedDecl *Sema::BuildUsingDeclaration(
   if (R.getAsSingle<NamespaceDecl>()) {
     Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_namespace)
       << SS.getRange();
+    // Suggest using 'using namespace ...' instead.
+    Diag(SS.getBeginLoc(), diag::note_namespace_using_decl)
+      << FixItHint::CreateInsertion(SS.getBeginLoc(), "namespace ");
     return BuildInvalid();
   }
 
diff --git a/clang/test/CXX/dcl.dcl/basic.namespace/namespace.udecl/p6-cxx11.cpp b/clang/test/CXX/dcl.dcl/basic.namespace/namespace.udecl/p6-cxx11.cpp
index 97b2953b90312..473290dd1c191 100644
--- a/clang/test/CXX/dcl.dcl/basic.namespace/namespace.udecl/p6-cxx11.cpp
+++ b/clang/test/CXX/dcl.dcl/basic.namespace/namespace.udecl/p6-cxx11.cpp
@@ -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 at -1 {{did you mean 'using namespace'?}}
diff --git a/clang/test/CXX/drs/cwg4xx.cpp b/clang/test/CXX/drs/cwg4xx.cpp
index 07162cc28f6b6..40ad5fb9b7900 100644
--- a/clang/test/CXX/drs/cwg4xx.cpp
+++ b/clang/test/CXX/drs/cwg4xx.cpp
@@ -941,8 +941,10 @@ namespace cwg460 { // cwg460: yes
     // expected-error at -1 {{using declaration requires a qualified name}}
     using cwg460::X;
     // expected-error at -1 {{using declaration cannot refer to a namespace}}
+    // expected-note at -2 {{did you mean 'using namespace'?}}
     using X::Q;
     // expected-error at -1 {{using declaration cannot refer to a namespace}}
+    // expected-note at -2 {{did you mean 'using namespace'?}}
   }
 }
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/94762


More information about the cfe-commits mailing list