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

via cfe-commits cfe-commits at lists.llvm.org
Mon May 13 18:49:01 PDT 2024


Author: Congcong Cai
Date: 2024-05-14T09:48:57+08:00
New Revision: f12018eba11f8d4b74cf67dbc416c429c870a5f4

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

LOG: [clang-tidy] support expect no diagnosis test (#91293)

When someone wants to declare a test case without any diagnosis.
check-clang-tidy will failed with error message

```
CHECK-FIXES, CHECK-MESSAGES or CHECK-NOTES not found in the input
```

This PR want to check there are no diagnosis from clang-tidy when
CHECK-FIXES, CHECK-MESSAGES or CHECK-NOTES are not found.

It also changes the extension of a test case. `hxx` is not a valid test
case extension and won't be tested.

---------

Co-authored-by: Danny Mösch <danny.moesch at icloud.com>

Added: 
    clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.hpp

Modified: 
    clang-tools-extra/test/clang-tidy/check_clang_tidy.py

Removed: 
    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..e92179ac82c6a 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 = False
         self.export_fixes = args.export_fixes
         self.fixes = MessagePrefix("CHECK-FIXES")
         self.messages = MessagePrefix("CHECK-MESSAGES")
@@ -172,12 +173,21 @@ 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
+        expect_diagnosis = (
+            self.has_check_fixes or self.has_check_messages or self.has_check_notes
+        )
+        if self.expect_no_diagnosis and expect_diagnosis:
+            sys.exit(
+                "%s, %s or %s not found in the input"
+                % (
+                    self.fixes.prefix,
+                    self.messages.prefix,
+                    self.notes.prefix,
+                )
+            )
+        assert expect_diagnosis or self.expect_no_diagnosis
 
     def prepare_test_inputs(self):
         # Remove the contents of the CHECK lines to avoid CHECKs matching on
@@ -226,6 +236,10 @@ def run_clang_tidy(self):
         print("------------------------------------------------------------------")
         return clang_tidy_output
 
+    def check_no_diagnosis(self, clang_tidy_output):
+        if clang_tidy_output != "":
+            sys.exit("No diagnostics were expected, but found the ones above")
+
     def check_fixes(self):
         if self.has_check_fixes:
             try_run(
@@ -277,7 +291,9 @@ def run(self):
             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)

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..4918aae16cb94
--- /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 %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;


        


More information about the cfe-commits mailing list