[clang-tools-extra] [clang-tidy] support expect no diagnosis test (PR #91293)

Congcong Cai via cfe-commits cfe-commits at lists.llvm.org
Thu May 9 08:59:56 PDT 2024


https://github.com/HerrCai0907 updated https://github.com/llvm/llvm-project/pull/91293

>From 55aecbedf3e10207eaef1d4c7913086a32e16b1e Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Tue, 7 May 2024 10:55:45 +0800
Subject: [PATCH 1/4] [clang-tidy] support expect no diagnosis test

---
 .../test/clang-tidy/check_clang_tidy.py             | 13 +++++++++++--
 .../clang-tidy/checkers/misc/unused-using-decls.hpp |  6 ++++++
 .../clang-tidy/checkers/misc/unused-using-decls.hxx |  6 ------
 3 files changed, 17 insertions(+), 8 deletions(-)
 create mode 100644 clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.hpp
 delete mode 100644 clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.hxx

diff --git a/clang-tools-extra/test/clang-tidy/check_clang_tidy.py b/clang-tools-extra/test/clang-tidy/check_clang_tidy.py
index 6d4b466afa691..d1cfe086fc968 100755
--- a/clang-tools-extra/test/clang-tidy/check_clang_tidy.py
+++ b/clang-tools-extra/test/clang-tidy/check_clang_tidy.py
@@ -99,6 +99,7 @@ def __init__(self, args, extra_args):
         self.has_check_fixes = False
         self.has_check_messages = False
         self.has_check_notes = False
+        self.expect_no_diagnosis = args.expect_no_diagnosis
         self.export_fixes = args.export_fixes
         self.fixes = MessagePrefix("CHECK-FIXES")
         self.messages = MessagePrefix("CHECK-MESSAGES")
@@ -225,6 +226,11 @@ def run_clang_tidy(self):
         print(diff_output)
         print("------------------------------------------------------------------")
         return clang_tidy_output
+    
+    def check_no_diagnosis(self, clang_tidy_output):
+        print(clang_tidy_output)
+        if clang_tidy_output != "":
+            sys.exit('expect no diagnosis')
 
     def check_fixes(self):
         if self.has_check_fixes:
@@ -273,11 +279,13 @@ def check_notes(self, clang_tidy_output):
 
     def run(self):
         self.read_input()
-        if self.export_fixes is None:
+        if self.export_fixes is None and not self.expect_no_diagnosis:
             self.get_prefixes()
         self.prepare_test_inputs()
         clang_tidy_output = self.run_clang_tidy()
-        if self.export_fixes is None:
+        if self.expect_no_diagnosis:
+            self.check_no_diagnosis(clang_tidy_output)
+        elif self.export_fixes is None:
             self.check_fixes()
             self.check_messages(clang_tidy_output)
             self.check_notes(clang_tidy_output)
@@ -310,6 +318,7 @@ def parse_arguments():
         formatter_class=argparse.RawDescriptionHelpFormatter,
     )
     parser.add_argument("-expect-clang-tidy-error", action="store_true")
+    parser.add_argument("-expect-no-diagnosis", action="store_true")
     parser.add_argument("-resource-dir")
     parser.add_argument("-assume-filename")
     parser.add_argument("input_file_name")
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.hpp b/clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.hpp
new file mode 100644
index 0000000000000..ce37877a22eca
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.hpp
@@ -0,0 +1,6 @@
+// RUN: %check_clang_tidy %s misc-unused-using-decls -expect-no-diagnosis %t
+
+// Verify that we don't generate the warnings on header files.
+namespace foo { class Foo {}; }
+
+using foo::Foo;
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.hxx b/clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.hxx
deleted file mode 100644
index f15e4fae80c0b..0000000000000
--- a/clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.hxx
+++ /dev/null
@@ -1,6 +0,0 @@
-// RUN: %check_clang_tidy %s misc-unused-using-decls %t -- --fix-notes -- -fno-delayed-template-parsing -isystem %S/Inputs
-
-// Verify that we don't generate the warnings on header files.
-namespace foo { class Foo {}; }
-
-using foo::Foo;

>From 2a5d18a739e56cbefbff40d49cc1546fcd4b82d4 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Tue, 7 May 2024 13:32:29 +0800
Subject: [PATCH 2/4] format

---
 clang-tools-extra/test/clang-tidy/check_clang_tidy.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang-tools-extra/test/clang-tidy/check_clang_tidy.py b/clang-tools-extra/test/clang-tidy/check_clang_tidy.py
index d1cfe086fc968..e87a53df29969 100755
--- a/clang-tools-extra/test/clang-tidy/check_clang_tidy.py
+++ b/clang-tools-extra/test/clang-tidy/check_clang_tidy.py
@@ -226,11 +226,11 @@ def run_clang_tidy(self):
         print(diff_output)
         print("------------------------------------------------------------------")
         return clang_tidy_output
-    
+
     def check_no_diagnosis(self, clang_tidy_output):
         print(clang_tidy_output)
         if clang_tidy_output != "":
-            sys.exit('expect no diagnosis')
+            sys.exit("expect no diagnosis")
 
     def check_fixes(self):
         if self.has_check_fixes:

>From 753e3b3c88e53f24026012555651aa91f8f2582c Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Thu, 9 May 2024 17:53:09 +0800
Subject: [PATCH 3/4] avoid use cmd option

---
 .../test/clang-tidy/check_clang_tidy.py            | 14 ++++++--------
 .../checkers/misc/unused-using-decls.hpp           |  2 +-
 2 files changed, 7 insertions(+), 9 deletions(-)

diff --git a/clang-tools-extra/test/clang-tidy/check_clang_tidy.py b/clang-tools-extra/test/clang-tidy/check_clang_tidy.py
index e87a53df29969..d7d9e2c0f51e9 100755
--- a/clang-tools-extra/test/clang-tidy/check_clang_tidy.py
+++ b/clang-tools-extra/test/clang-tidy/check_clang_tidy.py
@@ -99,7 +99,7 @@ def __init__(self, args, extra_args):
         self.has_check_fixes = False
         self.has_check_messages = False
         self.has_check_notes = False
-        self.expect_no_diagnosis = args.expect_no_diagnosis
+        self.expect_no_diagnosis = False
         self.export_fixes = args.export_fixes
         self.fixes = MessagePrefix("CHECK-FIXES")
         self.messages = MessagePrefix("CHECK-MESSAGES")
@@ -173,12 +173,11 @@ def get_prefixes(self):
                 )
 
             if not has_check_fix and not has_check_message and not has_check_note:
-                sys.exit(
-                    "%s, %s or %s not found in the input"
-                    % (self.fixes.prefix, self.messages.prefix, self.notes.prefix)
-                )
+                self.expect_no_diagnosis = True
 
-        assert self.has_check_fixes or self.has_check_messages or self.has_check_notes
+        assert self.expect_no_diagnosis != (
+            self.has_check_fixes or self.has_check_messages or self.has_check_notes
+        )
 
     def prepare_test_inputs(self):
         # Remove the contents of the CHECK lines to avoid CHECKs matching on
@@ -279,7 +278,7 @@ def check_notes(self, clang_tidy_output):
 
     def run(self):
         self.read_input()
-        if self.export_fixes is None and not self.expect_no_diagnosis:
+        if self.export_fixes is None:
             self.get_prefixes()
         self.prepare_test_inputs()
         clang_tidy_output = self.run_clang_tidy()
@@ -318,7 +317,6 @@ def parse_arguments():
         formatter_class=argparse.RawDescriptionHelpFormatter,
     )
     parser.add_argument("-expect-clang-tidy-error", action="store_true")
-    parser.add_argument("-expect-no-diagnosis", action="store_true")
     parser.add_argument("-resource-dir")
     parser.add_argument("-assume-filename")
     parser.add_argument("input_file_name")
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.hpp b/clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.hpp
index ce37877a22eca..4918aae16cb94 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.hpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.hpp
@@ -1,4 +1,4 @@
-// RUN: %check_clang_tidy %s misc-unused-using-decls -expect-no-diagnosis %t
+// RUN: %check_clang_tidy %s misc-unused-using-decls %t
 
 // Verify that we don't generate the warnings on header files.
 namespace foo { class Foo {}; }

>From bf618b7800c26e8004cd218ba0389a5da2f4998c Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Thu, 9 May 2024 23:59:47 +0800
Subject: [PATCH 4/4] Update
 clang-tools-extra/test/clang-tidy/check_clang_tidy.py
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Co-authored-by: Danny Mösch <danny.moesch at icloud.com>
---
 clang-tools-extra/test/clang-tidy/check_clang_tidy.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang-tools-extra/test/clang-tidy/check_clang_tidy.py b/clang-tools-extra/test/clang-tidy/check_clang_tidy.py
index d7d9e2c0f51e9..180e6e4e64a05 100755
--- a/clang-tools-extra/test/clang-tidy/check_clang_tidy.py
+++ b/clang-tools-extra/test/clang-tidy/check_clang_tidy.py
@@ -229,7 +229,7 @@ def run_clang_tidy(self):
     def check_no_diagnosis(self, clang_tidy_output):
         print(clang_tidy_output)
         if clang_tidy_output != "":
-            sys.exit("expect no diagnosis")
+            sys.exit("No diagnostics were expected, but found the ones above")
 
     def check_fixes(self):
         if self.has_check_fixes:



More information about the cfe-commits mailing list