[clang-tools-extra] bdf7fd8 - [clang-tidy] fix false positve for namespace with attrs in modernize-concat-nested-namespaces

Congcong Cai via cfe-commits cfe-commits at lists.llvm.org
Sun Apr 9 07:23:42 PDT 2023


Author: Congcong Cai
Date: 2023-04-09T16:23:29+02:00
New Revision: bdf7fd8297bcbcddc9c184a40c954c1f1b0b8340

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

LOG: [clang-tidy] fix false positve for namespace with attrs in modernize-concat-nested-namespaces

Fixed https://github.com/llvm/llvm-project/issues/57530
Add pre check to avoid false positive for namespace with attributes like
```
namespace [[deprecated]] ns {}
```

Reviewed By: PiotrZSL

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

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/modernize/ConcatNestedNamespacesCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/test/clang-tidy/checkers/modernize/concat-nested-namespaces.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/modernize/ConcatNestedNamespacesCheck.cpp b/clang-tools-extra/clang-tidy/modernize/ConcatNestedNamespacesCheck.cpp
index e18dcc2b3e591..a0b004f851ee0 100644
--- a/clang-tools-extra/clang-tidy/modernize/ConcatNestedNamespacesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/ConcatNestedNamespacesCheck.cpp
@@ -31,8 +31,9 @@ static StringRef getRawStringRef(const SourceRange &Range,
   return Lexer::getSourceText(TextRange, Sources, LangOpts);
 }
 
-static bool anonymousOrInlineNamespace(const NamespaceDecl &ND) {
-  return ND.isAnonymousNamespace() || ND.isInlineNamespace();
+static bool unsupportedNamespace(const NamespaceDecl &ND) {
+  return ND.isAnonymousNamespace() || ND.isInlineNamespace() ||
+         !ND.attrs().empty();
 }
 
 static bool singleNamedNamespaceChild(const NamespaceDecl &ND) {
@@ -41,7 +42,7 @@ static bool singleNamedNamespaceChild(const NamespaceDecl &ND) {
     return false;
 
   const auto *ChildNamespace = dyn_cast<const NamespaceDecl>(*Decls.begin());
-  return ChildNamespace && !anonymousOrInlineNamespace(*ChildNamespace);
+  return ChildNamespace && !unsupportedNamespace(*ChildNamespace);
 }
 
 static bool alreadyConcatenated(std::size_t NumCandidates,
@@ -166,7 +167,7 @@ void ConcatNestedNamespacesCheck::check(
   if (!locationsInSameFile(Sources, ND.getBeginLoc(), ND.getRBraceLoc()))
     return;
 
-  if (anonymousOrInlineNamespace(ND))
+  if (unsupportedNamespace(ND))
     return;
 
   Namespaces.push_back(&ND);

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 1ab7f4c79c7df..35cdcf7b207f2 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -301,9 +301,10 @@ Changes in existing checks
   <clang-tidy/checks/google/readability-avoid-underscore-in-googletest-name>` when using
   ``DISABLED_`` in the test suite name.
 
-- Fixed an issue in :doc:`modernize-concat-nested-namespaces
-  <clang-tidy/checks/modernize/concat-nested-namespaces>` when using macro between
-  namespace declarations could result incorrect fix.
+- Improved :doc:`modernize-concat-nested-namespaces
+  <clang-tidy/checks/modernize/concat-nested-namespaces>` to fix incorrect fixes when 
+  using macro between namespace declarations and false positive when using namespace 
+  with attributes.
 
 - Fixed a false positive in :doc:`performance-no-automatic-move
   <clang-tidy/checks/performance/no-automatic-move>` when warning would be

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/concat-nested-namespaces.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/concat-nested-namespaces.cpp
index 7728ada6a18db..13d1f2c37d983 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/concat-nested-namespaces.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/concat-nested-namespaces.cpp
@@ -21,11 +21,17 @@ void t();
 } // namespace n2
 
 namespace n5 {
-inline namespace n6 {
+inline namespace inline_ns {
 void t();
-}
+} // namespace inline_ns
 } // namespace n5
 
+namespace n6 {
+namespace [[deprecated]] attr_ns {
+void t();
+} // namespace attr_ns
+} // namespace n6
+
 namespace n7 {
 void t();
 


        


More information about the cfe-commits mailing list