[clang-tools-extra] da55af7 - [clang-tidy] Enable modernize-concat-nested-namespaces also on headers

Dmitry Polukhin via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 15 07:37:53 PDT 2021


Author: Dmitry Polukhin
Date: 2021-03-15T07:32:45-07:00
New Revision: da55af7f1d348c133774d8e8117d60462363fef5

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

LOG: [clang-tidy] Enable modernize-concat-nested-namespaces also on headers

For some reason the initial implementation of the check had an explicit check
for the main file to avoid being applied in headers. This diff removes this
check and add a test for the check on a header.

Similar approach was proposed in D61989 but review there got stuck.

Test Plan: added new test case

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

Added: 
    clang-tools-extra/test/clang-tidy/checkers/Inputs/modernize-concat-nested-namespaces/modernize-concat-nested-namespaces.h

Modified: 
    clang-tools-extra/clang-tidy/modernize/ConcatNestedNamespacesCheck.cpp
    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 83c3fac949ba..e55b260060ce 100644
--- a/clang-tools-extra/clang-tidy/modernize/ConcatNestedNamespacesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/ConcatNestedNamespacesCheck.cpp
@@ -82,9 +82,6 @@ void ConcatNestedNamespacesCheck::check(
   if (!locationsInSameFile(Sources, ND.getBeginLoc(), ND.getRBraceLoc()))
     return;
 
-  if (!Sources.isInMainFile(ND.getBeginLoc()))
-    return;
-
   if (anonymousOrInlineNamespace(ND))
     return;
 

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/Inputs/modernize-concat-nested-namespaces/modernize-concat-nested-namespaces.h b/clang-tools-extra/test/clang-tidy/checkers/Inputs/modernize-concat-nested-namespaces/modernize-concat-nested-namespaces.h
new file mode 100644
index 000000000000..752b33718dca
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/Inputs/modernize-concat-nested-namespaces/modernize-concat-nested-namespaces.h
@@ -0,0 +1,8 @@
+namespace nn1 {
+namespace nn2 {
+// CHECK-FIXES: namespace nn1::nn2
+void t();
+} // namespace nn2
+} // namespace nn1
+// CHECK-FIXES: void t();
+// CHECK-FIXES-NEXT: } // namespace nn1

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 dcde32d0f069..7c13c1c61cf2 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
@@ -1,4 +1,13 @@
-// RUN: %check_clang_tidy -std=c++17-or-later %s modernize-concat-nested-namespaces %t
+// RUN: cp %S/Inputs/modernize-concat-nested-namespaces/modernize-concat-nested-namespaces.h %T/modernize-concat-nested-namespaces.h
+// RUN: %check_clang_tidy -std=c++17 %s modernize-concat-nested-namespaces %t -- -header-filter=".*" -- -I %T
+// RUN: FileCheck -input-file=%T/modernize-concat-nested-namespaces.h %S/Inputs/modernize-concat-nested-namespaces/modernize-concat-nested-namespaces.h -check-prefix=CHECK-FIXES
+// Restore header file and re-run with c++20:
+// RUN: cp %S/Inputs/modernize-concat-nested-namespaces/modernize-concat-nested-namespaces.h %T/modernize-concat-nested-namespaces.h
+// RUN: %check_clang_tidy -std=c++20 %s modernize-concat-nested-namespaces %t -- -header-filter=".*" -- -I %T
+// RUN: FileCheck -input-file=%T/modernize-concat-nested-namespaces.h %S/Inputs/modernize-concat-nested-namespaces/modernize-concat-nested-namespaces.h -check-prefix=CHECK-FIXES
+
+#include "modernize-concat-nested-namespaces.h"
+// CHECK-MESSAGES-DAG: modernize-concat-nested-namespaces.h:1:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
 
 namespace n1 {}
 
@@ -27,7 +36,7 @@ void t();
 
 namespace n9 {
 namespace n10 {
-// CHECK-MESSAGES: :[[@LINE-2]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
+// CHECK-MESSAGES-DAG: :[[@LINE-2]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
 // CHECK-FIXES: namespace n9::n10
 void t();
 } // namespace n10
@@ -36,7 +45,7 @@ void t();
 
 namespace n11 {
 namespace n12 {
-// CHECK-MESSAGES: :[[@LINE-2]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
+// CHECK-MESSAGES-DAG: :[[@LINE-2]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
 // CHECK-FIXES: namespace n11::n12
 namespace n13 {
 void t();
@@ -60,7 +69,7 @@ void t();
 namespace n18 {
 namespace n19 {
 namespace n20 {
-// CHECK-MESSAGES: :[[@LINE-3]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
+// CHECK-MESSAGES-DAG: :[[@LINE-3]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
 // CHECK-FIXES: namespace n18::n19::n20
 void t();
 } // namespace n20
@@ -83,7 +92,7 @@ namespace n23 {
 namespace {
 namespace n24 {
 namespace n25 {
-// CHECK-MESSAGES: :[[@LINE-2]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
+// CHECK-MESSAGES-DAG: :[[@LINE-2]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
 // CHECK-FIXES: namespace n24::n25
 void t();
 } // namespace n25
@@ -95,7 +104,7 @@ void t();
 namespace n26::n27 {
 namespace n28 {
 namespace n29::n30 {
-// CHECK-MESSAGES: :[[@LINE-3]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
+// CHECK-MESSAGES-DAG: :[[@LINE-3]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
 // CHECK-FIXES: namespace n26::n27::n28::n29::n30
 void t() {}
 } // namespace n29::n30
@@ -105,14 +114,14 @@ void t() {}
 
 namespace n31 {
 namespace n32 {}
-// CHECK-MESSAGES: :[[@LINE-2]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
+// CHECK-MESSAGES-DAG: :[[@LINE-2]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
 } // namespace n31
 // CHECK-FIXES-EMPTY
 
 namespace n33 {
 namespace n34 {
 namespace n35 {}
-// CHECK-MESSAGES: :[[@LINE-2]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
+// CHECK-MESSAGES-DAG: :[[@LINE-2]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
 } // namespace n34
 // CHECK-FIXES-EMPTY
 namespace n36 {
@@ -127,7 +136,7 @@ void t();
 #define IEXIST
 namespace n39 {
 namespace n40 {
-// CHECK-MESSAGES: :[[@LINE-2]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
+// CHECK-MESSAGES-DAG: :[[@LINE-2]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
 // CHECK-FIXES: namespace n39::n40
 #ifdef IEXIST
 void t() {}
@@ -138,7 +147,7 @@ void t() {}
 
 namespace n41 {
 namespace n42 {
-// CHECK-MESSAGES: :[[@LINE-2]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
+// CHECK-MESSAGES-DAG: :[[@LINE-2]]:1: warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]
 // CHECK-FIXES: namespace n41::n42
 #ifdef IDONTEXIST
 void t() {}


        


More information about the cfe-commits mailing list