Skip to content

Commit 20eb165

Browse files
committed
[clang-tidy] Stop ignoring -std argument in check_clang_tidy.py for C files
1 parent f65b329 commit 20eb165

25 files changed

+90
-55
lines changed

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

Lines changed: 13 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,25 @@ 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"]
390+
if extension in [".cpp", ".hpp", ".mm"]
391+
else ["c99-or-later"]
392+
)
393+
394+
return (args, extra_args)
386395

387396

388397
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) {

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// RUN: bugprone-easily-swappable-parameters.NamePrefixSuffixSilenceDissimilarityTreshold: 0 \
1010
// RUN: }}' -- -Wno-strict-prototypes -x c
1111

12-
int myprint();
1312
int add(int X, int Y);
1413

1514
void notRelated(int A, int B) {}
@@ -19,13 +18,16 @@ void notRelated(int A, int B) {}
1918

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

21+
// FIXME: This triggers a false positive: the "passed to same function" heuristic
22+
// can't map the parameter index 1 to A and B because myprint() has no
23+
// parameters.
24+
// warning: 2 adjacent parameters of 'passedToSameKNRFunction' of similar type ('int')
25+
// note: the first parameter in the range is 'A'
26+
// note: the last parameter in the range is 'B'
27+
#if 0
28+
int myprint();
2229
void passedToSameKNRFunction(int A, int B) {
2330
myprint("foo", A);
2431
myprint("bar", B);
2532
}
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
30-
// can't map the parameter index 1 to A and B because myprint() has no
31-
// parameters.
33+
#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 %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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %check_clang_tidy %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

0 commit comments

Comments
 (0)