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

Nico Weber via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 7 08:54:17 PDT 2024


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

>From 2cbc9f7e066066ffb04480be6bd7e19855086b80 Mon Sep 17 00:00:00 2001
From: Nico Weber <thakis at chromium.org>
Date: Fri, 7 Jun 2024 11:17:29 -0400
Subject: [PATCH 1/2] [clang] Add fixit for using declaration with a
 (qualified) namespace

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 :)
---
 clang/include/clang/Basic/DiagnosticSemaKinds.td               | 2 ++
 clang/lib/Sema/SemaDeclCXX.cpp                                 | 3 +++
 .../CXX/dcl.dcl/basic.namespace/namespace.udecl/p6-cxx11.cpp   | 3 +++
 clang/test/CXX/drs/cwg4xx.cpp                                  | 2 ++
 4 files changed, 10 insertions(+)

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'?}}
   }
 }
 

>From d3c074bce2d59546f9cdbf3b4ab4fb2420d5b527 Mon Sep 17 00:00:00 2001
From: Nico Weber <thakis at chromium.org>
Date: Fri, 7 Jun 2024 11:54:06 -0400
Subject: [PATCH 2/2] clang-format

---
 clang/lib/Sema/SemaDeclCXX.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index b5ffa75f04ffa..e224e79fdb8ea 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -13079,10 +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 ");
+        << FixItHint::CreateInsertion(SS.getBeginLoc(), "namespace ");
     return BuildInvalid();
   }
 



More information about the cfe-commits mailing list