Skip to content

Commit 230b9b2

Browse files
authored
[clang-tidy] Add -std argument in check_clang_tidy.py for C files (#150791)
Before, the `-std` argument in C tests was simply ignored, and they were run with whatever Clang defaults to when you don't specify a standard. The new default is `-std=c99-or-later`. This means a bunch of tests are suddenly being run in C23 mode, and so need to be adapted to account for various things: - `typeof` is changed to `__typeof__`; the non-`__ugly__` spelling is only available with GNU extensions or C23. - In C23, `bool` is a keyword, so `typedef foo bool;` is invalid. - In C23, `f()` means `f(void)`, not "takes arbitrary parameters". - In C23, K&R-style function definitions are removed.
1 parent 699f154 commit 230b9b2

26 files changed

+104
-54
lines changed

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ Improvements to clang-query
104104
Improvements to clang-tidy
105105
--------------------------
106106

107+
- The :program:`check_clang_tidy.py` tool now recognizes the ``-std`` argument
108+
when run over C files. If ``-std`` is not specified, it defaults to
109+
``c99-or-later``.
110+
107111
- :program:`clang-tidy` no longer attemps to analyze code from system headers
108112
by default, greatly improving performance. This behavior is disabled if the
109113
`SystemHeaders` option is enabled.

clang-tools-extra/test/clang-tidy/check_clang_tidy.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ def __init__(self, args: argparse.Namespace, extra_args: List[str]) -> None:
135135
"-fblocks",
136136
] + self.clang_extra_args
137137

138-
if extension in [".cpp", ".hpp", ".mm"]:
139-
self.clang_extra_args.append("-std=" + self.std)
138+
self.clang_extra_args.append("-std=" + self.std)
140139

141140
# Tests should not rely on STL being available, and instead provide mock
142141
# implementations of relevant APIs.
@@ -374,15 +373,23 @@ def parse_arguments() -> Tuple[argparse.Namespace, List[str]]:
374373
parser.add_argument(
375374
"-std",
376375
type=csv,
377-
default=["c++11-or-later"],
376+
default=None,
378377
help="Passed to clang. Special -or-later values are expanded.",
379378
)
380379
parser.add_argument(
381380
"--match-partial-fixes",
382381
action="store_true",
383382
help="allow partial line matches for fixes",
384383
)
385-
return parser.parse_known_args()
384+
385+
args, extra_args = parser.parse_known_args()
386+
if args.std is None:
387+
_, extension = os.path.splitext(args.assume_filename or args.input_file_name)
388+
args.std = [
389+
"c++11-or-later" if extension in [".cpp", ".hpp", ".mm"] else "c99-or-later"
390+
]
391+
392+
return (args, extra_args)
386393

387394

388395
def main() -> None:

clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/system-other.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
// Special system calls.
1313

14+
#if __STDC_VERSION__ < 202311L
1415
void other_call();
16+
#endif
1517

1618
#endif // _SYSTEM_OTHER_H_

clang-tools-extra/test/clang-tidy/checkers/android/comparison-in-temp-failure-retry-custom-macro.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#define MY_TEMP_FAILURE_RETRY(x) \
44
({ \
5-
typeof(x) __z; \
5+
__typeof__(x) __z; \
66
do \
77
__z = (x); \
88
while (__z == -1); \
@@ -11,7 +11,7 @@
1111

1212
#define MY_OTHER_TEMP_FAILURE_RETRY(x) \
1313
({ \
14-
typeof(x) __z; \
14+
__typeof__(x) __z; \
1515
do \
1616
__z = (x); \
1717
while (__z == -1); \

clang-tools-extra/test/clang-tidy/checkers/android/comparison-in-temp-failure-retry.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#define TEMP_FAILURE_RETRY(x) \
44
({ \
5-
typeof(x) __z; \
5+
__typeof__(x) __z; \
66
do \
77
__z = (x); \
88
while (__z == -1); \
@@ -130,7 +130,7 @@ void obscured_temp_failure_retry(void) {
130130
#undef TEMP_FAILURE_RETRY
131131
#define IMPL(x) \
132132
({ \
133-
typeof(x) __z; \
133+
__typeof__(x) __z; \
134134
do \
135135
__z = (x); \
136136
while (__z == -1); \

clang-tools-extra/test/clang-tidy/checkers/bugprone/branch-clone-macro-crash.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// RUN: %check_clang_tidy %s bugprone-branch-clone %t
22
int x = 0;
33
int y = 1;
4-
#define a(b, c) \
5-
typeof(b) d; \
6-
if (b) \
7-
d = b; \
8-
else if (c) \
4+
#define a(b, c) \
5+
__typeof__(b) d; \
6+
if (b) \
7+
d = b; \
8+
else if (c) \
99
d = b;
1010

1111
void f(void) {
Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
// RUN: %check_clang_tidy %s bugprone-easily-swappable-parameters %t \
1+
// RUN: %check_clang_tidy -std=c99,c11,c17 -check-suffixes=,BEFORE-C23 %s bugprone-easily-swappable-parameters %t \
2+
// RUN: -config='{CheckOptions: { \
3+
// RUN: bugprone-easily-swappable-parameters.MinimumLength: 2, \
4+
// RUN: bugprone-easily-swappable-parameters.IgnoredParameterNames: "", \
5+
// RUN: bugprone-easily-swappable-parameters.IgnoredParameterTypeSuffixes: "", \
6+
// RUN: bugprone-easily-swappable-parameters.QualifiersMix: 0, \
7+
// RUN: bugprone-easily-swappable-parameters.ModelImplicitConversions: 0, \
8+
// RUN: bugprone-easily-swappable-parameters.SuppressParametersUsedTogether: 1, \
9+
// RUN: bugprone-easily-swappable-parameters.NamePrefixSuffixSilenceDissimilarityTreshold: 0 \
10+
// RUN: }}' -- -Wno-strict-prototypes -x c
11+
//
12+
// RUN: %check_clang_tidy -std=c23-or-later %s bugprone-easily-swappable-parameters %t \
213
// RUN: -config='{CheckOptions: { \
314
// RUN: bugprone-easily-swappable-parameters.MinimumLength: 2, \
415
// RUN: bugprone-easily-swappable-parameters.IgnoredParameterNames: "", \
@@ -9,7 +20,6 @@
920
// RUN: bugprone-easily-swappable-parameters.NamePrefixSuffixSilenceDissimilarityTreshold: 0 \
1021
// RUN: }}' -- -Wno-strict-prototypes -x c
1122

12-
int myprint();
1323
int add(int X, int Y);
1424

1525
void notRelated(int A, int B) {}
@@ -19,13 +29,18 @@ void notRelated(int A, int B) {}
1929

2030
int addedTogether(int A, int B) { return add(A, B); } // NO-WARN: Passed to same function.
2131

32+
#if __STDC_VERSION__ < 202311L
33+
int myprint();
2234
void passedToSameKNRFunction(int A, int B) {
2335
myprint("foo", A);
2436
myprint("bar", B);
2537
}
26-
// CHECK-MESSAGES: :[[@LINE-4]]:30: warning: 2 adjacent parameters of 'passedToSameKNRFunction' of similar type ('int')
27-
// CHECK-MESSAGES: :[[@LINE-5]]:34: note: the first parameter in the range is 'A'
28-
// CHECK-MESSAGES: :[[@LINE-6]]:41: note: the last parameter in the range is 'B'
29-
// This is actually a false positive: the "passed to same function" heuristic
38+
// CHECK-MESSAGES-BEFORE-C23: :[[@LINE-4]]:30: warning: 2 adjacent parameters of 'passedToSameKNRFunction' of similar type ('int')
39+
// CHECK-MESSAGES-BEFORE-C23: :[[@LINE-5]]:34: note: the first parameter in the range is 'A'
40+
// CHECK-MESSAGES-BEFORE-C23: :[[@LINE-6]]:41: note: the last parameter in the range is 'B'
41+
// FIXME: This is actually a false positive: the "passed to same function" heuristic
3042
// can't map the parameter index 1 to A and B because myprint() has no
3143
// parameters.
44+
// If you fix this, you should be able to combine the `%check_clang_tidy` invocations
45+
// in this file into one and remove the `-std` and `-check-suffixes` arguments.
46+
#endif

clang-tools-extra/test/clang-tidy/checkers/bugprone/easily-swappable-parameters.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %check_clang_tidy %s bugprone-easily-swappable-parameters %t \
1+
// RUN: %check_clang_tidy -std=c99,c11,c17 -check-suffixes=,BEFORE-23 %s bugprone-easily-swappable-parameters %t \
22
// RUN: -config='{CheckOptions: { \
33
// RUN: bugprone-easily-swappable-parameters.MinimumLength: 2, \
44
// RUN: bugprone-easily-swappable-parameters.IgnoredParameterNames: "", \
@@ -7,7 +7,18 @@
77
// RUN: bugprone-easily-swappable-parameters.ModelImplicitConversions: 0, \
88
// RUN: bugprone-easily-swappable-parameters.SuppressParametersUsedTogether: 0, \
99
// RUN: bugprone-easily-swappable-parameters.NamePrefixSuffixSilenceDissimilarityTreshold: 0 \
10-
// RUN: }}' -- -Wno-strict-prototypes -x c
10+
// RUN: }}' -- -Wno-strict-prototypes
11+
//
12+
// RUN: %check_clang_tidy -std=c23-or-later %s bugprone-easily-swappable-parameters %t \
13+
// RUN: -config='{CheckOptions: { \
14+
// RUN: bugprone-easily-swappable-parameters.MinimumLength: 2, \
15+
// RUN: bugprone-easily-swappable-parameters.IgnoredParameterNames: "", \
16+
// RUN: bugprone-easily-swappable-parameters.IgnoredParameterTypeSuffixes: "bool;MyBool;struct U;MAKE_LOGICAL_TYPE(int)", \
17+
// RUN: bugprone-easily-swappable-parameters.QualifiersMix: 0, \
18+
// RUN: bugprone-easily-swappable-parameters.ModelImplicitConversions: 0, \
19+
// RUN: bugprone-easily-swappable-parameters.SuppressParametersUsedTogether: 0, \
20+
// RUN: bugprone-easily-swappable-parameters.NamePrefixSuffixSilenceDissimilarityTreshold: 0 \
21+
// RUN: }}' -- -Wno-strict-prototypes
1122

1223
#define bool _Bool
1324
#define true 1
@@ -45,8 +56,11 @@ void pointerConversion(int *IP, long *LP) {}
4556

4657
void testVariadicsCall() {
4758
int IVal = 1;
59+
60+
#if __STDC_VERSION__ < 202311L
4861
decl(IVal); // NO-WARN: Particular calls to "variadics" are like template
4962
// instantiations, and we do not model them.
63+
#endif
5064

5165
variadic(IVal); // NO-WARN.
5266
variadic(IVal, 2, 3, 4); // NO-WARN.
@@ -64,13 +78,15 @@ void taggedTypes2(struct S SVar1, struct S SVar2) {}
6478

6579
void wrappers(struct { int I; } I1, struct { int I; } I2) {} // NO-WARN: Distinct anonymous types.
6680

81+
#if __STDC_VERSION__ < 202311L
6782
void knr(I, J)
6883
int I;
6984
int J;
7085
{}
71-
// CHECK-MESSAGES: :[[@LINE-3]]:3: warning: 2 adjacent parameters of 'knr' of similar type ('int')
72-
// CHECK-MESSAGES: :[[@LINE-4]]:7: note: the first parameter in the range is 'I'
73-
// CHECK-MESSAGES: :[[@LINE-4]]:7: note: the last parameter in the range is 'J'
86+
// CHECK-MESSAGES-BEFORE-23: :[[@LINE-3]]:3: warning: 2 adjacent parameters of 'knr' of similar type ('int')
87+
// CHECK-MESSAGES-BEFORE-23: :[[@LINE-4]]:7: note: the first parameter in the range is 'I'
88+
// CHECK-MESSAGES-BEFORE-23: :[[@LINE-4]]:7: note: the last parameter in the range is 'J'
89+
#endif
7490

7591
void boolAsWritten(bool B1, bool B2) {} // NO-WARN: The type name is ignored.
7692
// Note that "bool" is a macro that expands to "_Bool" internally, but it is
@@ -145,7 +161,7 @@ void thisIsGettingRidiculous(MAKE_PRIMITIVE_WRAPPER(int) I1,
145161
void macroMagic3(MAKE_LOGICAL_TYPE(char) B1, MAKE_LOGICAL_TYPE(long) B2) {}
146162
// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 2 adjacent parameters of 'macroMagic3' of similar type ('bool')
147163
// CHECK-MESSAGES: :[[@LINE-4]]:30: note: expanded from macro 'MAKE_LOGICAL_TYPE'
148-
// CHECK-MESSAGES: :[[@LINE-136]]:14: note: expanded from macro 'bool'
164+
// CHECK-MESSAGES: :[[@LINE-141]]:14: note: expanded from macro 'bool'
149165
// CHECK-MESSAGES: :[[@LINE-4]]:42: note: the first parameter in the range is 'B1'
150166
// CHECK-MESSAGES: :[[@LINE-5]]:70: note: the last parameter in the range is 'B2'
151167

clang-tools-extra/test/clang-tidy/checkers/bugprone/not-null-terminated-result-in-initialization-strlen.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %check_clang_tidy --match-partial-fixes %s bugprone-not-null-terminated-result %t -- \
2-
// RUN: -- -std=c11 -I %S/Inputs/not-null-terminated-result
2+
// RUN: -- -I %S/Inputs/not-null-terminated-result
33

44
#include "not-null-terminated-result-c.h"
55

clang-tools-extra/test/clang-tidy/checkers/bugprone/not-null-terminated-result-memcpy-before-safe.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %check_clang_tidy %s bugprone-not-null-terminated-result %t -- \
22
// RUN: -config="{CheckOptions: \
33
// RUN: {bugprone-not-null-terminated-result.WantToUseSafeFunction: true}}" \
4-
// RUN: -- -std=c11 -I %S/Inputs/not-null-terminated-result
4+
// RUN: -- -I %S/Inputs/not-null-terminated-result
55

66
#include "not-null-terminated-result-c.h"
77

0 commit comments

Comments
 (0)