Skip to content

Commit 15686e1

Browse files
authored
Merge pull request #4697 from barnabasdomozi/fix_clang_diagnostic_checker
Fix clang-tidy 'clang-diagnostic-*' checkers
2 parents 7a974c9 + d0a32e1 commit 15686e1

4 files changed

Lines changed: 86 additions & 5 deletions

File tree

analyzer/codechecker_analyzer/analyzers/clangtidy/analyzer.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -418,18 +418,20 @@ def get_checker_list(self, config) -> Tuple[List[str], List[str]]:
418418
has_checker_config = \
419419
config.checker_config and config.checker_config != '{}'
420420

421+
clang_diagnostic_prefix = 'clang-diagnostic-'
422+
421423
# Config handler stores which checkers are enabled or disabled.
422424
for checker_name, value in config.checks().items():
423425
state, _ = value
424426

425-
if checker_name.startswith('clang-diagnostic-'):
427+
if checker_name.startswith(clang_diagnostic_prefix):
426428
# If a clang-diagnostic-... is enabled add it as a compiler
427429
# warning as -W..., if it is disabled, tidy can suppress when
428430
# specified in the -checks parameter list, so we add it there
429431
# as -clang-diagnostic-... .
430432

431433
# TODO: str.removeprefix() available in Python 3.9
432-
warning_name = checker_name[len('clang-diagnostic-'):]
434+
warning_name = checker_name[len(clang_diagnostic_prefix):]
433435

434436
if state == CheckerState.ENABLED:
435437
if checker_name == 'clang-diagnostic-error':
@@ -444,10 +446,18 @@ def get_checker_list(self, config) -> Tuple[List[str], List[str]]:
444446
if state == CheckerState.ENABLED:
445447
enabled_checkers.append(checker_name)
446448

447-
# By default all checkers are disabled and the enabled ones are added
448-
# explicitly.
449+
# By default all checkers are disabled and the enabled ones
450+
# are added explicitly.
449451
checkers = ['-*']
450452

453+
# If only clang-diagnostic-* checkers are enabled,
454+
# we need to add a dummy checker otherwise clang-tidy
455+
# will fail with the following error: "no checks enabled"
456+
if all(c.startswith(clang_diagnostic_prefix) for c
457+
in enabled_checkers):
458+
dummy_checker_name = "darwin-dispatch-once-nonstatic"
459+
checkers.append(dummy_checker_name)
460+
451461
checkers += _add_asterisk_for_group(
452462
enabled_checkers,
453463
set(x[0] for x in ClangTidy.get_analyzer_checkers()))

analyzer/tests/functional/analyze_and_parse/test_files/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,6 @@ cppcheck_undef_include:
5454
gcc_simple:
5555
$(CXX) -w gcc_simple.cpp -o /dev/null -c
5656
infer_simple:
57-
$(CXX) -w infer_simple.cpp -o /dev/null -c
57+
$(CXX) -w infer_simple.cpp -o /dev/null -c
58+
all_checkers_off_except_return_stack_address:
59+
$(CC) -w all_checkers_off_except_return_stack_address.c -o /dev/null
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
int* f() {
2+
// We only enabled clang-diagnostic-return-stack-address checker,
3+
// so there should be no finding for division by zero.
4+
int a = 5/0;
5+
6+
// The line below should raise clang-diagnostic-return-stack-address error
7+
return &a;
8+
}
9+
10+
int main (void) {
11+
return *f();
12+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
NORMAL#CodeChecker log --output $LOGFILE$ --build "make all_checkers_off_except_return_stack_address" --quiet
2+
NORMAL#CodeChecker analyze $LOGFILE$ --analyzers clang-tidy -d default -e clang-diagnostic-return-stack-address --output $OUTPUT$
3+
NORMAL#CodeChecker parse $OUTPUT$
4+
CHECK#CodeChecker check --build "make all_checkers_off_except_return_stack_address" --output $OUTPUT$ --quiet --analyzers clang-tidy -d default -e clang-diagnostic-return-stack-address
5+
--------------------------------------------------------------------------------
6+
[] - Starting build...
7+
[] - Using CodeChecker ld-logger.
8+
[] - Build finished successfully.
9+
[] - Starting static analysis ...
10+
[] - [1/1] clang-tidy analyzed all_checkers_off_except_return_stack_address.c successfully.
11+
[] - ----==== Summary ====----
12+
[] - Successfully analyzed
13+
[] - clang-tidy: 1
14+
[] - Total analyzed compilation commands: 1
15+
[] - ----=================----
16+
[] - Analysis finished.
17+
[] - To view results in the terminal use the "CodeChecker parse" command.
18+
[] - To store results use the "CodeChecker store" command.
19+
[] - See --help and the user guide for further options about parsing and storing the reports.
20+
[] - ----=================----
21+
[MEDIUM] all_checkers_off_except_return_stack_address.c:7:11: address of stack memory associated with local variable 'a' returned [clang-diagnostic-return-stack-address]
22+
return &a;
23+
^
24+
25+
Found 1 defect(s) in all_checkers_off_except_return_stack_address.c
26+
27+
28+
----==== Severity Statistics ====----
29+
----------------------------
30+
Severity | Number of reports
31+
----------------------------
32+
MEDIUM | 1
33+
----------------------------
34+
----=================----
35+
36+
----==== Checker Statistics ====----
37+
--------------------------------------------------------------------
38+
Checker name | Severity | Number of reports
39+
--------------------------------------------------------------------
40+
clang-diagnostic-return-stack-address | MEDIUM | 1
41+
--------------------------------------------------------------------
42+
----=================----
43+
44+
----==== File Statistics ====----
45+
------------------------------------------------------------------
46+
File name | Number of reports
47+
------------------------------------------------------------------
48+
all_checkers_off_except_return_stack_address.c | 1
49+
------------------------------------------------------------------
50+
----=================----
51+
52+
----======== Summary ========----
53+
---------------------------------------------
54+
Number of processed analyzer result files | 1
55+
Number of analyzer reports | 1
56+
---------------------------------------------
57+
----=================----

0 commit comments

Comments
 (0)