From bb708cb4156a6237569ac57636aebcfccdf0242c Mon Sep 17 00:00:00 2001 From: Philipp Wullstein-Kammler <111539239+phiwuu@users.noreply.github.com> Date: Wed, 4 Dec 2024 10:46:43 +0100 Subject: [PATCH] Add option to pass parameters to clang tidy (#138) Add new command line argument `--compile-commands` to `lobster-cpp`. Its value will be forwarded to the call of clang tidy. --- CHANGELOG.md | 5 ++++ lobster/tools/cpp/cpp.py | 50 +++++++++++++++++++++++++++++----------- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 570987c8..b3bca2f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ ### 0.9.20-dev +* Add `--compile-commands` flag to `lobster-cpp`. This allows to specify a path to the + compile command database and is effectively a wrapper around the `-p` argument of + `clang tidy`. See the official documentation of `clang tidy` for more details on this + parameter. + * `lobster-cpptest` writes absolute paths into its `*.lobster` output files instead of paths relative to the current working directory. diff --git a/lobster/tools/cpp/cpp.py b/lobster/tools/cpp/cpp.py index b09aa546..93e22f6f 100755 --- a/lobster/tools/cpp/cpp.py +++ b/lobster/tools/cpp/cpp.py @@ -59,6 +59,14 @@ def main(): metavar="FILE", help=("use the specified clang-tidy; by default we" " pick the one on PATH")) + ap.add_argument("--compile-commands", + metavar="FILE", + default=None, + help=("Path to the compile command database for all targets for " + "'clang tidy', or none to use the default behavior of " + "'clang tidy'. This is equal to calling 'clang tidy' " + "directly with its '-p' option. Refer to its official " + "documentation for more details.")) ap.add_argument("--out", default=None, help=("write output to this file; otherwise output to" @@ -83,13 +91,17 @@ def main(): # Test if the clang-tidy can be used - rv = subprocess.run([os.path.expanduser(options.clang_tidy), - "-checks=-*,lobster-tracing", - "--list-checks"], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - encoding="UTF-8", - check=False) + rv = subprocess.run( + [ + os.path.expanduser(options.clang_tidy), + "-checks=-*,lobster-tracing", + "--list-checks", + ], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + encoding="UTF-8", + check=False, + ) if "No checks enabled." in rv.stderr: print("The provided clang-tidy does include the lobster-tracing check") @@ -99,13 +111,23 @@ def main(): "correct binary using the --clang-tidy flag") return 1 - rv = subprocess.run([os.path.expanduser(options.clang_tidy), - "-checks=-*,lobster-tracing"] + - file_list, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - encoding="UTF-8", - check=False) + subprocess_args = [ + os.path.expanduser(options.clang_tidy), + "-checks=-*,lobster-tracing", + ] + if options.compile_commands: + subprocess_args.append("-p") + subprocess_args.append(options.compile_commands) + + subprocess_args += file_list + + rv = subprocess.run( + subprocess_args, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + encoding="UTF-8", + check=False, + ) if rv.returncode != 0: found_reason = False