Skip to content

Commit 192b7e4

Browse files
authored
addresses #7 (#9)
* addresses #7 * extra-arg accumulates as a list * admonish nuances in docs
1 parent cc3cc3a commit 192b7e4

File tree

5 files changed

+76
-3
lines changed

5 files changed

+76
-3
lines changed

cpp_linter/run.py

+52-2
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124
installed (if using a custom install location). All paths specified
125125
here are converted to absolute.
126126
127-
Default is """
127+
Default is """,
128128
)
129129
assert arg.help is not None
130130
arg.help += "a blank string." if not arg.default else f"``{arg.default}``."
@@ -237,6 +237,23 @@
237237
238238
Defaults to ``%(default)s``.""",
239239
)
240+
cli_arg_parser.add_argument(
241+
"-x",
242+
"--extra-arg",
243+
default=[],
244+
action="append",
245+
help="""A string of extra arguments passed to clang-tidy for use as
246+
compiler arguments. This can be specified more than once for each
247+
additional argument. Recommend using quotes around the value and
248+
avoid using spaces between name and value (use ``=`` instead):
249+
250+
.. code-block:: shell
251+
252+
cpp-linter --extra-arg="-std=c++17" --extra-arg="-Wall"
253+
254+
Defaults to ``'%(default)s'``.
255+
""",
256+
)
240257

241258

242259
def set_exit_code(override: int = None) -> int:
@@ -477,6 +494,7 @@ def run_clang_tidy(
477494
lines_changed_only: int,
478495
database: str,
479496
repo_root: str,
497+
extra_args: List[str],
480498
) -> None:
481499
"""Run clang-tidy on a certain file.
482500
@@ -489,6 +507,23 @@ def run_clang_tidy(
489507
diff info.
490508
:param database: The path to the compilation database.
491509
:param repo_root: The path to the repository root folder.
510+
:param extra_args: A list of extra arguments used by clang-tidy as compiler
511+
arguments.
512+
513+
.. note::
514+
If the list is only 1 item long and there is a space in the first item,
515+
then the list is reformed from splitting the first item by whitespace
516+
characters.
517+
518+
.. code-block:: shell
519+
520+
cpp-linter -extra-arg="-std=c++14 -Wall"
521+
522+
is equivalent to
523+
524+
.. code-block:: shell
525+
526+
cpp-linter -extra-arg=-std=c++14 --extra-arg=-Wall
492527
"""
493528
if checks == "-*": # if all checks are disabled, then clang-tidy is skipped
494529
# clear the clang-tidy output file and exit function
@@ -511,6 +546,10 @@ def run_clang_tidy(
511546
line_ranges = dict(name=filename, lines=file_obj["line_filter"][ranges])
512547
logger.info("line_filter = %s", json.dumps([line_ranges]))
513548
cmds.append(f"--line-filter={json.dumps([line_ranges])}")
549+
if len(extra_args) == 1 and " " in extra_args[0]:
550+
extra_args = extra_args[0].split()
551+
for extra_arg in extra_args:
552+
cmds.append(f"--extra-arg={extra_arg}")
514553
cmds.append(filename)
515554
# clear yml file's content before running clang-tidy
516555
Path("clang_tidy_output.yml").write_bytes(b"")
@@ -623,6 +662,7 @@ def capture_clang_tools_output(
623662
lines_changed_only: int,
624663
database: str,
625664
repo_root: str,
665+
extra_args: List[str],
626666
):
627667
"""Execute and capture all output from clang-tidy and clang-format. This aggregates
628668
results in the :attr:`~cpp_linter.Globals.OUTPUT`.
@@ -636,14 +676,23 @@ def capture_clang_tools_output(
636676
diff info.
637677
:param database: The path to the compilation database.
638678
:param repo_root: The path to the repository root folder.
679+
:param extra_args: A list of extra arguments used by clang-tidy as compiler
680+
arguments.
639681
"""
640682
# temporary cache of parsed notifications for use in log commands
641683
tidy_notes: List[TidyNotification] = []
642684
for file in Globals.FILES:
643685
filename = cast(str, file["filename"])
644686
start_log_group(f"Performing checkup on {filename}")
645687
run_clang_tidy(
646-
filename, file, version, checks, lines_changed_only, database, repo_root
688+
filename,
689+
file,
690+
version,
691+
checks,
692+
lines_changed_only,
693+
database,
694+
repo_root,
695+
extra_args,
647696
)
648697
run_clang_format(filename, file, version, style, lines_changed_only)
649698
end_log_group()
@@ -962,6 +1011,7 @@ def main():
9621011
args.lines_changed_only,
9631012
args.database,
9641013
args.repo_root,
1014+
args.extra_arg,
9651015
)
9661016

9671017
start_log_group("Posting comment(s)")

pyproject.toml

+17-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,23 @@ confidence = ["HIGH", "CONTROL_FLOW", "INFERENCE", "INFERENCE_FAILURE", "UNDEFIN
440440
# --enable=similarities". If you want to run only the classes checker, but have
441441
# no Warning level messages displayed, use "--disable=all --enable=classes
442442
# --disable=W".
443-
disable = ["raw-checker-failed", "bad-inline-option", "locally-disabled", "file-ignored", "suppressed-message", "useless-suppression", "deprecated-pragma", "use-symbolic-message-instead", "invalid-sequence-index", "anomalous-backslash-in-string", "too-few-public-methods", "consider-using-f-string", "subprocess-run-check"]
443+
disable = [
444+
"raw-checker-failed",
445+
"bad-inline-option",
446+
"locally-disabled",
447+
"file-ignored",
448+
"suppressed-message",
449+
"useless-suppression",
450+
"deprecated-pragma",
451+
"use-symbolic-message-instead",
452+
"invalid-sequence-index",
453+
"anomalous-backslash-in-string",
454+
"too-few-public-methods",
455+
"consider-using-f-string",
456+
"subprocess-run-check",
457+
"missing-timeout",
458+
"too-many-lines"
459+
]
444460

445461
# Enable the message, report, category or checker with the given id(s). You can
446462
# either give multiple identifier separated by comma (,) or put this option

tests/capture_tools_output/test_database_path.py

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def test_db_detection(
4848
lines_changed_only=0, # analyze complete file
4949
database=database,
5050
repo_root=rel_root,
51+
extra_args=[],
5152
)
5253
matched_args = []
5354
for record in caplog.records:

tests/capture_tools_output/test_tools_output.py

+3
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ def test_format_annotations(
113113
lines_changed_only=lines_changed_only,
114114
database="",
115115
repo_root="",
116+
extra_args=[],
116117
)
117118
assert "Output from `clang-tidy`" not in cpp_linter.Globals.OUTPUT
118119
caplog.set_level(logging.INFO, logger=log_commander.name)
@@ -162,6 +163,7 @@ def test_tidy_annotations(
162163
lines_changed_only=lines_changed_only,
163164
database="",
164165
repo_root="",
166+
extra_args=[],
165167
)
166168
assert "Run `clang-format` on the following files" not in cpp_linter.Globals.OUTPUT
167169
caplog.set_level(logging.INFO, logger=log_commander.name)
@@ -195,6 +197,7 @@ def test_diff_comment(lines_changed_only: int):
195197
lines_changed_only=lines_changed_only,
196198
database="",
197199
repo_root="",
200+
extra_args=[],
198201
)
199202
diff_comments = list_diff_comments(lines_changed_only)
200203
# output = Path(__file__).parent / "diff_comments.json"

tests/test_cli_args.py

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class Args:
3636
files_changed_only: bool = False
3737
thread_comments: bool = False
3838
file_annotations: bool = True
39+
extra_arg: List[str] = []
3940

4041

4142
def test_defaults():
@@ -62,6 +63,8 @@ def test_defaults():
6263
("files-changed-only", "True", "files_changed_only", True),
6364
("thread-comments", "True", "thread_comments", True),
6465
("file-annotations", "False", "file_annotations", False),
66+
("extra-arg", "-std=c++17", "extra_arg", ["-std=c++17"]),
67+
("extra-arg", '"-std=c++17 -Wall"', "extra_arg", ['"-std=c++17 -Wall"']),
6568
],
6669
)
6770
def test_arg_parser(

0 commit comments

Comments
 (0)