[clang] 528e6eb - [clang] Improve diagnostic for reopened inline namespace

Fabian Wolff via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 23 14:32:10 PDT 2022


Author: Fabian Wolff
Date: 2022-03-23T22:30:45+01:00
New Revision: 528e6eba2f7906d974594b1fc50362dfad239609

URL: https://github.com/llvm/llvm-project/commit/528e6eba2f7906d974594b1fc50362dfad239609
DIFF: https://github.com/llvm/llvm-project/commit/528e6eba2f7906d974594b1fc50362dfad239609.diff

LOG: [clang] Improve diagnostic for reopened inline namespace

Reviewed By: cor3ntin, aaron.ballman

Differential Revision: https://reviews.llvm.org/D122278

Added: 
    clang/test/SemaCXX/warn-inline-namespace-reopened-twice.cpp

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaDeclCXX.cpp
    clang/test/Parser/cxx2a-inline-nested-namespace-definition.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 83d5dbdda2607..373d4156cd797 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -83,6 +83,11 @@ Improvements to Clang's diagnostics
 - ``-Wliteral-range`` will warn on floating-point equality comparisons with
   constants that are not representable in a casted value. For example,
   ``(float) f == 0.1`` is always false.
+- ``-Winline-namespace-reopened-noninline`` now takes into account that the
+  ``inline`` keyword must appear on the original but not necessarily all
+  extension definitions of an inline namespace and therefore points its note
+  at the original definition. This fixes `Issue 50794 (PR51452)
+  <https://github.com/llvm/llvm-project/issues/50794>`_.
 
 Non-comprehensive list of changes in this release
 -------------------------------------------------

diff  --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 7992e4a349611..0b5440c227d1c 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -11060,6 +11060,11 @@ static void DiagnoseNamespaceInlineMismatch(Sema &S, SourceLocation KeywordLoc,
                                             NamespaceDecl *PrevNS) {
   assert(*IsInline != PrevNS->isInline());
 
+  // 'inline' must appear on the original definition, but not necessarily
+  // on all extension definitions, so the note should point to the first
+  // definition to avoid confusion.
+  PrevNS = PrevNS->getFirstDecl();
+
   if (PrevNS->isInline())
     // The user probably just forgot the 'inline', so suggest that it
     // be added back.

diff  --git a/clang/test/Parser/cxx2a-inline-nested-namespace-definition.cpp b/clang/test/Parser/cxx2a-inline-nested-namespace-definition.cpp
index f37dc8c033cec..72a0cbf8da8f1 100644
--- a/clang/test/Parser/cxx2a-inline-nested-namespace-definition.cpp
+++ b/clang/test/Parser/cxx2a-inline-nested-namespace-definition.cpp
@@ -17,7 +17,7 @@ inline namespace foo4::foo5::foo6 { // expected-error {{nested namespace definit
 // expected-warning at +2 {{inline nested namespace definition is incompatible with C++ standards before C++20}}
 #endif
 namespace valid1::valid2::inline valid3::inline valid4::valid5 {}
-// expected-note at -1 2 {{previous definition is here}}
+// expected-note at -1 4 {{previous definition is here}}
 
 #if __cplusplus <= 201402L
 // expected-warning at +3 {{nested namespace definition is a C++17 extension; define each namespace separately}}
@@ -34,7 +34,6 @@ namespace valid1::valid2::valid3::valid4::valid5 {}
 // expected-warning at +2 {{inline nested namespace definition is incompatible with C++ standards before C++20}}
 #endif
 namespace valid1::valid2::inline valid3::inline valid4::valid5 {}
-// expected-note at -1 2 {{previous definition is here}}
 
 namespace valid1 {
 namespace valid2 {

diff  --git a/clang/test/SemaCXX/warn-inline-namespace-reopened-twice.cpp b/clang/test/SemaCXX/warn-inline-namespace-reopened-twice.cpp
new file mode 100644
index 0000000000000..ac22280fdf800
--- /dev/null
+++ b/clang/test/SemaCXX/warn-inline-namespace-reopened-twice.cpp
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -fsyntax-only -Wall -verify -std=c++11 %s
+
+// Regression test for #50794.
+
+// expected-note at +1 2 {{previous definition is here}}
+inline namespace X {}
+
+namespace X {} // expected-warning {{inline namespace reopened as a non-inline namespace}}
+namespace X {} // expected-warning {{inline namespace reopened as a non-inline namespace}}


        


More information about the cfe-commits mailing list