Skip to content

[clang-tidy] Add -std argument in check_clang_tidy.py for C files #150791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ Improvements to clang-query
Improvements to clang-tidy
--------------------------

- The :program:`check_clang_tidy.py` tool now recognizes the ``-std`` argument
when run over C files. If ``-std`` is not specified, it defaults to
``c99-or-later``.

- The :program:`run-clang-tidy.py` and :program:`clang-tidy-diff.py` scripts
now run checks in parallel by default using all available hardware threads.
Both scripts display the number of threads being used in their output.
Expand Down
17 changes: 13 additions & 4 deletions clang-tools-extra/test/clang-tidy/check_clang_tidy.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ def __init__(self, args: argparse.Namespace, extra_args: List[str]) -> None:
"-fblocks",
] + self.clang_extra_args

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

# Tests should not rely on STL being available, and instead provide mock
# implementations of relevant APIs.
Expand Down Expand Up @@ -374,15 +373,25 @@ def parse_arguments() -> Tuple[argparse.Namespace, List[str]]:
parser.add_argument(
"-std",
type=csv,
default=["c++11-or-later"],
default=None,
help="Passed to clang. Special -or-later values are expanded.",
)
parser.add_argument(
"--match-partial-fixes",
action="store_true",
help="allow partial line matches for fixes",
)
return parser.parse_known_args()

args, extra_args = parser.parse_known_args()
if args.std is None:
_, extension = os.path.splitext(args.assume_filename or args.input_file_name)
args.std = (
["c++11-or-later"]
if extension in [".cpp", ".hpp", ".mm"]
else ["c99-or-later"]
)

return (args, extra_args)


def main() -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

// Special system calls.

#if __STDC_VERSION__ < 202311L
void other_call();
#endif

#endif // _SYSTEM_OTHER_H_
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#define MY_TEMP_FAILURE_RETRY(x) \
({ \
typeof(x) __z; \
__typeof__(x) __z; \
do \
__z = (x); \
while (__z == -1); \
Expand All @@ -11,7 +11,7 @@

#define MY_OTHER_TEMP_FAILURE_RETRY(x) \
({ \
typeof(x) __z; \
__typeof__(x) __z; \
do \
__z = (x); \
while (__z == -1); \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#define TEMP_FAILURE_RETRY(x) \
({ \
typeof(x) __z; \
__typeof__(x) __z; \
do \
__z = (x); \
while (__z == -1); \
Expand Down Expand Up @@ -130,7 +130,7 @@ void obscured_temp_failure_retry(void) {
#undef TEMP_FAILURE_RETRY
#define IMPL(x) \
({ \
typeof(x) __z; \
__typeof__(x) __z; \
do \
__z = (x); \
while (__z == -1); \
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// RUN: %check_clang_tidy %s bugprone-branch-clone %t
int x = 0;
int y = 1;
#define a(b, c) \
typeof(b) d; \
if (b) \
d = b; \
else if (c) \
#define a(b, c) \
__typeof__(b) d; \
if (b) \
d = b; \
else if (c) \
d = b;

void f(void) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
// RUN: bugprone-easily-swappable-parameters.NamePrefixSuffixSilenceDissimilarityTreshold: 0 \
// RUN: }}' -- -Wno-strict-prototypes -x c

int myprint();
int add(int X, int Y);

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

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

// FIXME: This triggers a false positive: the "passed to same function" heuristic
// can't map the parameter index 1 to A and B because myprint() has no
// parameters.
// warning: 2 adjacent parameters of 'passedToSameKNRFunction' of similar type ('int')
// note: the first parameter in the range is 'A'
// note: the last parameter in the range is 'B'
#if 0
int myprint();
void passedToSameKNRFunction(int A, int B) {
myprint("foo", A);
myprint("bar", B);
}
// CHECK-MESSAGES: :[[@LINE-4]]:30: warning: 2 adjacent parameters of 'passedToSameKNRFunction' of similar type ('int')
// CHECK-MESSAGES: :[[@LINE-5]]:34: note: the first parameter in the range is 'A'
// CHECK-MESSAGES: :[[@LINE-6]]:41: note: the last parameter in the range is 'B'
// This is actually a false positive: the "passed to same function" heuristic
// can't map the parameter index 1 to A and B because myprint() has no
// parameters.
#endif
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %check_clang_tidy %s bugprone-easily-swappable-parameters %t \
// RUN: %check_clang_tidy -std=c99,c11,c17 -check-suffixes=,BEFORE-23 %s bugprone-easily-swappable-parameters %t \
// RUN: -config='{CheckOptions: { \
// RUN: bugprone-easily-swappable-parameters.MinimumLength: 2, \
// RUN: bugprone-easily-swappable-parameters.IgnoredParameterNames: "", \
Expand All @@ -7,7 +7,18 @@
// RUN: bugprone-easily-swappable-parameters.ModelImplicitConversions: 0, \
// RUN: bugprone-easily-swappable-parameters.SuppressParametersUsedTogether: 0, \
// RUN: bugprone-easily-swappable-parameters.NamePrefixSuffixSilenceDissimilarityTreshold: 0 \
// RUN: }}' -- -Wno-strict-prototypes -x c
// RUN: }}' -- -Wno-strict-prototypes
//
// RUN: %check_clang_tidy -std=c23-or-later %s bugprone-easily-swappable-parameters %t \
// RUN: -config='{CheckOptions: { \
// RUN: bugprone-easily-swappable-parameters.MinimumLength: 2, \
// RUN: bugprone-easily-swappable-parameters.IgnoredParameterNames: "", \
// RUN: bugprone-easily-swappable-parameters.IgnoredParameterTypeSuffixes: "bool;MyBool;struct U;MAKE_LOGICAL_TYPE(int)", \
// RUN: bugprone-easily-swappable-parameters.QualifiersMix: 0, \
// RUN: bugprone-easily-swappable-parameters.ModelImplicitConversions: 0, \
// RUN: bugprone-easily-swappable-parameters.SuppressParametersUsedTogether: 0, \
// RUN: bugprone-easily-swappable-parameters.NamePrefixSuffixSilenceDissimilarityTreshold: 0 \
// RUN: }}' -- -Wno-strict-prototypes

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

void testVariadicsCall() {
int IVal = 1;

#if __STDC_VERSION__ < 202311L
decl(IVal); // NO-WARN: Particular calls to "variadics" are like template
// instantiations, and we do not model them.
#endif

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

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

#if __STDC_VERSION__ < 202311L
void knr(I, J)
int I;
int J;
{}
// CHECK-MESSAGES: :[[@LINE-3]]:3: warning: 2 adjacent parameters of 'knr' of similar type ('int')
// CHECK-MESSAGES: :[[@LINE-4]]:7: note: the first parameter in the range is 'I'
// CHECK-MESSAGES: :[[@LINE-4]]:7: note: the last parameter in the range is 'J'
// CHECK-MESSAGES-BEFORE-23: :[[@LINE-3]]:3: warning: 2 adjacent parameters of 'knr' of similar type ('int')
// CHECK-MESSAGES-BEFORE-23: :[[@LINE-4]]:7: note: the first parameter in the range is 'I'
// CHECK-MESSAGES-BEFORE-23: :[[@LINE-4]]:7: note: the last parameter in the range is 'J'
#endif

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %check_clang_tidy --match-partial-fixes %s bugprone-not-null-terminated-result %t -- \
// RUN: -- -std=c11 -I %S/Inputs/not-null-terminated-result
// RUN: -- -I %S/Inputs/not-null-terminated-result

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// RUN: %check_clang_tidy %s bugprone-not-null-terminated-result %t -- \
// RUN: -config="{CheckOptions: \
// RUN: {bugprone-not-null-terminated-result.WantToUseSafeFunction: true}}" \
// RUN: -- -std=c11 -I %S/Inputs/not-null-terminated-result
// RUN: -- -I %S/Inputs/not-null-terminated-result

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %check_clang_tidy %s bugprone-not-null-terminated-result %t -- \
// RUN: -- -std=c11 -I %S/Inputs/not-null-terminated-result
// RUN: -- -I %S/Inputs/not-null-terminated-result

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %check_clang_tidy %s bugprone-not-null-terminated-result %t -- \
// RUN: -- -std=c11 -I %S/Inputs/not-null-terminated-result
// RUN: -- -I %S/Inputs/not-null-terminated-result

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %check_clang_tidy --match-partial-fixes %s bugprone-not-null-terminated-result %t -- \
// RUN: -- -std=c11 -I %S/Inputs/not-null-terminated-result
// RUN: -- -I %S/Inputs/not-null-terminated-result

// FIXME: Something wrong with the APInt un/signed conversion on Windows:
// in 'strncmp(str6, "string", 7);' it tries to inject '4294967302' as length.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %check_clang_tidy %s bugprone-not-null-terminated-result %t -- \
// RUN: -- -std=c11 -I %S/Inputs/not-null-terminated-result
// RUN: -- -I %S/Inputs/not-null-terminated-result

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: %check_clang_tidy %s bugprone-signal-handler %t -- -- -isystem %clang_tidy_headers
// RUN: %check_clang_tidy -std=c99,c11,c17 -check-suffixes=,BEFORE-23 %s bugprone-signal-handler %t -- -- -isystem %clang_tidy_headers
// RUN: %check_clang_tidy -std=c23-or-later %s bugprone-signal-handler %t -- -- -isystem %clang_tidy_headers

#include "signal.h"
#include "stdlib.h"
Expand Down Expand Up @@ -174,8 +175,10 @@ void test_other(void) {
signal(SIGINT, handler_signal);

signal(SIGINT, _Exit);
#if __STDC_VERSION__ < 202311L
signal(SIGINT, other_call);
// CHECK-NOTES: :[[@LINE-1]]:18: warning: standard function 'other_call' may not be asynchronous-safe; using it as a signal handler may be dangerous [bugprone-signal-handler]
// CHECK-NOTES-BEFORE-23: :[[@LINE-1]]:18: warning: standard function 'other_call' may not be asynchronous-safe; using it as a signal handler may be dangerous [bugprone-signal-handler]
#endif
signal(SIGINT, f_extern_handler);
// CHECK-NOTES: :[[@LINE-1]]:18: warning: cannot verify that external function 'f_extern_handler' is asynchronous-safe; using it as a signal handler may be dangerous [bugprone-signal-handler]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %check_clang_tidy %s bugprone-signed-char-misuse %t -- -- -std=c23
// RUN: %check_clang_tidy -std=c23-or-later %s bugprone-signed-char-misuse %t

///////////////////////////////////////////////////////////////////
/// Test cases correctly caught by the check.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %check_clang_tidy %s bugprone-sizeof-expression %t -- --
// RUN: %check_clang_tidy %s bugprone-sizeof-expression %t -- -- -x c++
// RUN: %check_clang_tidy %s bugprone-sizeof-expression %t
// RUN: %check_clang_tidy -std=c++11-or-later %s bugprone-sizeof-expression %t -- -- -x c++

#ifdef __cplusplus
#define STRKWD
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
// parsing and preprocessor state will not have that case.
// UNSUPPORTED: target={{.*-(ps4|ps5)}}
//
// RUN: %check_clang_tidy -check-suffix=WITH-ANNEX-K %s bugprone-unsafe-functions %t -- -- -D__STDC_LIB_EXT1__=1 -D__STDC_WANT_LIB_EXT1__=1
// RUN: %check_clang_tidy -check-suffix=WITHOUT-ANNEX-K %s bugprone-unsafe-functions %t -- -- -U__STDC_LIB_EXT1__ -U__STDC_WANT_LIB_EXT1__
// RUN: %check_clang_tidy -check-suffix=WITHOUT-ANNEX-K %s bugprone-unsafe-functions %t -- -- -D__STDC_LIB_EXT1__=1 -U__STDC_WANT_LIB_EXT1__
// RUN: %check_clang_tidy -check-suffix=WITHOUT-ANNEX-K %s bugprone-unsafe-functions %t -- -- -U__STDC_LIB_EXT1__ -D__STDC_WANT_LIB_EXT1__=1
// RUN: %check_clang_tidy -check-suffix=WITH-ANNEX-K-CERT-ONLY %s bugprone-unsafe-functions %t -- \
// RUN: %check_clang_tidy -std=c11-or-later -check-suffix=WITH-ANNEX-K %s bugprone-unsafe-functions %t -- -- -D__STDC_LIB_EXT1__=1 -D__STDC_WANT_LIB_EXT1__=1
// RUN: %check_clang_tidy -check-suffix=WITHOUT-ANNEX-K %s bugprone-unsafe-functions %t -- -- -U__STDC_LIB_EXT1__ -U__STDC_WANT_LIB_EXT1__
// RUN: %check_clang_tidy -check-suffix=WITHOUT-ANNEX-K %s bugprone-unsafe-functions %t -- -- -D__STDC_LIB_EXT1__=1 -U__STDC_WANT_LIB_EXT1__
// RUN: %check_clang_tidy -check-suffix=WITHOUT-ANNEX-K %s bugprone-unsafe-functions %t -- -- -U__STDC_LIB_EXT1__ -D__STDC_WANT_LIB_EXT1__=1
// RUN: %check_clang_tidy -std=c11-or-later -check-suffix=WITH-ANNEX-K-CERT-ONLY %s bugprone-unsafe-functions %t -- \
// RUN: -config="{CheckOptions: {bugprone-unsafe-functions.ReportMoreUnsafeFunctions: false}}" \
// RUN: -- -D__STDC_LIB_EXT1__=1 -D__STDC_WANT_LIB_EXT1__=1
// RUN: %check_clang_tidy -check-suffix=WITH-NONE-ENABLED %s bugprone-unsafe-functions %t --\
// RUN: %check_clang_tidy -check-suffix=WITH-NONE-ENABLED %s bugprone-unsafe-functions %t --\
// RUN: -config="{CheckOptions: {bugprone-unsafe-functions.ReportDefaultFunctions: false}}" \
// RUN: -- -D__STDC_LIB_EXT1__=1 -D__STDC_WANT_LIB_EXT1__=1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ static void TestImplicitFunctionDeclaration(int a) {
printf("%d", a);
}

typedef _Bool bool;
#define bool _Bool

static bool ispositive(int a) { return a > 0; }
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: static function named 'ispositive'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %check_clang_tidy %s misc-static-assert %t -- -- -std=c11
// RUN: %check_clang_tidy -std=c11-or-later %s misc-static-assert %t
// RUN: clang-tidy %s -checks=-*,misc-static-assert -- -std=c99 | count 0

void abort(void) {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
// RUN: %check_clang_tidy %s misc-unused-parameters %t -- -- -Wno-strict-prototypes -xc
// RUN: %check_clang_tidy %s misc-unused-parameters %t -- -- -Wno-strict-prototypes

// Basic removal
// =============
void a(int i) {;}
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: parameter 'i' is unused [misc-unused-parameters]
// CHECK-FIXES: {{^}}void a(int i) {;}{{$}}

static void b(); // In C, forward declarations can leave out parameters.
#if __STDC_VERSION__ < 202311L
static void b(); // In C before C23, forward declarations can leave out parameters.
#endif
static void b(int i) {;}
// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: parameter 'i' is unused [misc-unused-parameters]
// CHECK-FIXES: {{^}}static void b() {;}{{$}}

// Unchanged cases
// ===============
#if __STDC_VERSION__ < 202311L
void h(i, c, d) int i; char *c, *d; {} // Don't mess with K&R style
#endif

// Do not warn on naked functions.
__attribute__((naked)) void nakedFunction(int a, int b) { ; }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %check_clang_tidy %s modernize-use-nullptr %t -- -- -std=c23
// RUN: %check_clang_tidy -std=c23-or-later %s modernize-use-nullptr %t

#define NULL 0

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// RUN: %check_clang_tidy %s readability-magic-numbers %t --
// RUN: %check_clang_tidy -std=c23-or-later %s readability-magic-numbers %t

// Don't crash

_BitInt(128) A = 4533629751480627964421wb;
// CHECK-MESSAGES: warning
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ typedef void* FILE; // NOLINT
#define NULL (0) // NOLINT

#ifndef __cplusplus
typedef _Bool bool; // NOLINT
typedef __WCHAR_TYPE__ wchar_t; // NOLINT
#define bool _Bool // NOLINT
#define true 1 // NOLINT
#define false 0 // NOLINT
#endif
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// RUN: %check_clang_tidy --match-partial-fixes %s readability-implicit-bool-conversion %t -- -- -std=c23
// RUN: %check_clang_tidy -check-suffix=UPPER-CASE %s readability-implicit-bool-conversion %t -- \
// RUN: %check_clang_tidy -std=c23-or-later --match-partial-fixes %s readability-implicit-bool-conversion %t
// RUN: %check_clang_tidy -std=c23-or-later -check-suffix=UPPER-CASE %s readability-implicit-bool-conversion %t -- \
// RUN: -config='{CheckOptions: { \
// RUN: readability-implicit-bool-conversion.UseUpperCaseLiteralSuffix: true \
// RUN: }}' -- -std=c23
// RUN: }}'

#undef NULL
#define NULL 0L
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %check_clang_tidy %s readability-non-const-parameter %t
// RUN: %check_clang_tidy -std=c99,c11,c17 %s readability-non-const-parameter %t

static int f();

Expand Down
Loading