[clang-tools-extra] [run-clang-tidy.py] Add option to ignore source files from compilation database (PR #82416)

Alexander Scholz via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 21 10:18:25 PST 2024


https://github.com/duddel updated https://github.com/llvm/llvm-project/pull/82416

>From a3596bf357ef991abcaef04f8811958c0984d9f6 Mon Sep 17 00:00:00 2001
From: duddel <duddel at users.noreply.github.com>
Date: Tue, 20 Feb 2024 21:11:26 +0100
Subject: [PATCH 1/4] add -source-ignore option to run-clang-tidy.py

---
 .../clang-tidy/tool/run-clang-tidy.py             | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py b/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
index 70f8cbcdcb2f11..ba4314dfb50aa1 100755
--- a/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
+++ b/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
@@ -300,6 +300,12 @@ def main():
         "the main file of each translation unit are always "
         "displayed.",
     )
+    parser.add_argument(
+        "-source-ignore",
+        default=None,
+        help="Regular expression matching the names of the "
+        "source files from compilation database to ignore.",
+    )
     parser.add_argument(
         "-line-filter",
         default=None,
@@ -462,6 +468,15 @@ def main():
         [make_absolute(entry["file"], entry["directory"]) for entry in database]
     )
 
+    # Remove source file to be ignored from database.
+    if args.source_ignore:
+        try:
+            source_ignore_re = re.compile(args.source_ignore)
+        except:
+            print("Error: unable to compile regex from arg -source-ignore.", file=sys.stderr)
+            sys.exit(1)
+        files = {f for f in files if not source_ignore_re.match(f)}
+
     max_task = args.j
     if max_task == 0:
         max_task = multiprocessing.cpu_count()

>From 6c8cf48bd8ad36ec0e5740eb105742df3f2b60a6 Mon Sep 17 00:00:00 2001
From: duddel <duddel at users.noreply.github.com>
Date: Tue, 20 Feb 2024 21:42:03 +0100
Subject: [PATCH 2/4] Apply suggested python code formatting

---
 clang-tools-extra/clang-tidy/tool/run-clang-tidy.py | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py b/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
index ba4314dfb50aa1..66465ba78289d9 100755
--- a/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
+++ b/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
@@ -473,7 +473,10 @@ def main():
         try:
             source_ignore_re = re.compile(args.source_ignore)
         except:
-            print("Error: unable to compile regex from arg -source-ignore.", file=sys.stderr)
+            print(
+                "Error: unable to compile regex from arg -source-ignore.",
+                file=sys.stderr,
+            )
             sys.exit(1)
         files = {f for f in files if not source_ignore_re.match(f)}
 

>From 7e53d55063aedfb3507f45a13cd8f44a7d339e5f Mon Sep 17 00:00:00 2001
From: duddel <duddel at users.noreply.github.com>
Date: Wed, 21 Feb 2024 17:27:53 +0100
Subject: [PATCH 3/4] change -source-ignore to -source-filter option in
 run-clang-tidy.py

---
 .../clang-tidy/tool/run-clang-tidy.py             | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py b/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
index 66465ba78289d9..584ef41f19c4b9 100755
--- a/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
+++ b/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
@@ -301,10 +301,11 @@ def main():
         "displayed.",
     )
     parser.add_argument(
-        "-source-ignore",
+        "-source-filter",
         default=None,
         help="Regular expression matching the names of the "
-        "source files from compilation database to ignore.",
+        "source files from compilation database to output "
+        "diagnostics from.",
     )
     parser.add_argument(
         "-line-filter",
@@ -468,17 +469,17 @@ def main():
         [make_absolute(entry["file"], entry["directory"]) for entry in database]
     )
 
-    # Remove source file to be ignored from database.
-    if args.source_ignore:
+    # Filter source files from compilation database.
+    if args.source_filter:
         try:
-            source_ignore_re = re.compile(args.source_ignore)
+            source_filter_re = re.compile(args.source_filter)
         except:
             print(
-                "Error: unable to compile regex from arg -source-ignore.",
+                "Error: unable to compile regex from arg -source-filter.",
                 file=sys.stderr,
             )
             sys.exit(1)
-        files = {f for f in files if not source_ignore_re.match(f)}
+        files = {f for f in files if source_filter_re.match(f)}
 
     max_task = args.j
     if max_task == 0:

>From 13510698c238ee380b28fe0847332a4b4db49b5a Mon Sep 17 00:00:00 2001
From: duddel <duddel at users.noreply.github.com>
Date: Wed, 21 Feb 2024 19:17:53 +0100
Subject: [PATCH 4/4] run-clang-tidy.py: print exception if regex compile
 fails, add Release Note

---
 clang-tools-extra/clang-tidy/tool/run-clang-tidy.py | 3 ++-
 clang-tools-extra/docs/ReleaseNotes.rst             | 3 +++
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py b/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
index 584ef41f19c4b9..1bd4a5b283091c 100755
--- a/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
+++ b/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
@@ -475,9 +475,10 @@ def main():
             source_filter_re = re.compile(args.source_filter)
         except:
             print(
-                "Error: unable to compile regex from arg -source-filter.",
+                "Error: unable to compile regex from arg -source-filter:",
                 file=sys.stderr,
             )
+            traceback.print_exc()
             sys.exit(1)
         files = {f for f in files if source_filter_re.match(f)}
 
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index a0b9fcfe0d7774..7db9d7a92eaf2b 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -97,6 +97,9 @@ The improvements are...
 Improvements to clang-tidy
 --------------------------
 
+- Improved :program:`run-clang-tidy.py` script. Added argument `-source-filter`
+  to filter out source files from the compilation database.
+
 New checks
 ^^^^^^^^^^
 



More information about the cfe-commits mailing list