forked from mongodb/mongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclang_tidy_vscode.py
executable file
·85 lines (67 loc) · 3.19 KB
/
clang_tidy_vscode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/opt/mongodbtoolchain/v4/bin/python3
"""
Wraps clang tidy to include our custom checks.
This script acts as a wrapper for the `clang-tidy` tool, allowing it to include custom checks
defined in a shared object file (`libmongo_tidy_checks.so`). Additionally, it filters the files
to be checked, ensuring that only files within the `src/mongo` directory are processed, excluding
those within `src/mongo/db/modules/enterprise/src/streams/third_party`.
Input:
- The script expects command-line arguments that are passed to `clang-tidy`.
- These arguments can include file paths, options, and other parameters supported by `clang-tidy`.
Output:
- The script runs `clang-tidy` on the specified files and outputs the results.
- If no valid `.cpp` files are found, or if all `.cpp` files are located in the excluded directories,
the script skips running `clang-tidy`.
- Standard output and error from the `clang-tidy` process are captured and printed.
Expected Format:
- command line example: buildscripts/clang_tidy_vscode.py /path/to/file/filename1 --export-fixes=-
- buildscripts/clang_tidy_vscode.py /path/to/file/filename1 /path/to/file/filename2 --export-fixes=-
"""
# TODO: if https://github.com/notskm/vscode-clang-tidy/pull/77#issuecomment-1422910143 is resolved then this script can be removed
import os
import subprocess
import sys
# Get relative imports to work when the package is not installed on the PYTHONPATH.
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from mongo_toolchain import get_mongo_toolchain
CHECKS_SO = [
"build/install/lib/libmongo_tidy_checks.so",
]
if os.path.exists(".mongo_checks_module_path"):
with open(".mongo_checks_module_path") as f:
CHECKS_SO = [f.read().strip()] + CHECKS_SO
def main():
toolchain = get_mongo_toolchain()
clang_tidy_args = [toolchain.get_tool_path("clang-tidy")]
for check_lib in CHECKS_SO:
if os.path.isfile(check_lib):
clang_tidy_args += [f"-load={check_lib}"]
break
# Filter out non src/mongo files for clang tidy checks
files_to_check = []
other_args = []
for arg in sys.argv[1:]:
if os.path.isfile(arg):
source_relative_path = os.path.relpath(arg, os.path.dirname(os.path.dirname(__file__)))
if (
(arg.endswith(".cpp") or arg.endswith(".h"))
and source_relative_path.startswith("src/mongo")
# TODO: SERVER-79076 remove this condition when resolved
and not source_relative_path.startswith(
"src/mongo/db/modules/enterprise/src/streams/third_party"
)
):
files_to_check.append(arg)
else:
other_args.append(arg)
# No files to check in src/mongo. Skipping clang-tidy
if not files_to_check:
return 0
clang_tidy_args += files_to_check + other_args
proc = subprocess.run(clang_tidy_args, capture_output=True)
# Write to output buffer here because that is how to copy directly from stdin to stdout without making assumptions about encoding
sys.stdout.buffer.write(proc.stdout)
sys.stderr.buffer.write(proc.stderr)
return proc.returncode
if __name__ == "__main__":
sys.exit(main())