https://github.com/daandemeyer updated https://github.com/llvm/llvm-project/pull/237623
>From e8e64dd9a31d1a10b9cdaaef1331185559090203 Mon Sep 17 00:00:00 2001 From: Daan De Meyer <[email protected]> Date: Fri, 30 Oct 2026 16:00:32 +0100 Subject: [PATCH 1/4] [clang-tidy] Check macro argument calls in argument-comment bugprone-argument-comment never adds a missing comment to an argument that comes from a macro expansion. That also skips calls that the user wrote in a macro argument, such as ASSERT(f(0)). Many C projects check the results of their function calls that way. Let's add an IgnoreMacros option. When it's false, we add the comment when the argument and the closing parenthesis of the call map to one range of the file, starting at the argument. That's the case when the user wrote the call in a macro argument. A call in a macro body fails the test, because a fix-it would edit the macro definition that every expansion shares. An argument that is passed through another macro, such as f(ID(0)), fails it too, because the fix-it would insert the comment inside the argument of ID. IgnoreMacros defaults to true, so existing configurations don't get new warnings. The comparison of existing comments with parameter names already runs for calls in macro arguments, so the option doesn't affect it. Signed-off-by: Daan De Meyer <[email protected]> --- .../bugprone/ArgumentCommentCheck.cpp | 52 +++++++++++--- .../bugprone/ArgumentCommentCheck.h | 5 +- clang-tools-extra/docs/ReleaseNotes.md | 4 ++ .../checks/bugprone/argument-comment.md | 8 +++ .../bugprone/argument-comment-macros.cpp | 72 +++++++++++++++++++ 5 files changed, 130 insertions(+), 11 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/bugprone/argument-comment-macros.cpp diff --git a/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp index 783944cd1a5a17..3d808144c57a89 100644 --- a/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp @@ -35,6 +35,7 @@ ArgumentCommentCheck::ArgumentCommentCheck(StringRef Name, ClangTidyContext *Context) : ClangTidyCheck(Name, Context), StrictMode(Options.get("StrictMode", false)), + IgnoreMacros(Options.get("IgnoreMacros", true)), IgnoreSingleArgument(Options.get("IgnoreSingleArgument", false)), CommentAnonymousInitLists( Options.get("CommentAnonymousInitLists", false)), @@ -53,6 +54,7 @@ ArgumentCommentCheck::ArgumentCommentCheck(StringRef Name, void ArgumentCommentCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { Options.store(Opts, "StrictMode", StrictMode); + Options.store(Opts, "IgnoreMacros", IgnoreMacros); Options.store(Opts, "IgnoreSingleArgument", IgnoreSingleArgument); Options.store(Opts, "CommentAnonymousInitLists", CommentAnonymousInitLists); Options.store(Opts, "CommentBoolLiterals", CommentBoolLiterals); @@ -251,6 +253,24 @@ static InitListKind getInitListKind(const Expr *Arg) { return InitListKind::None; } +// Returns the file location of Arg. Returns an invalid location when Arg and +// the closing parenthesis of the call at CloseLoc don't map to one range of the +// file that starts at Arg. That happens when a macro body contains Arg or the +// closing parenthesis, or when Arg is in another macro argument than the +// closing parenthesis, such as the `0` in `f(ID(0))`. +static SourceLocation getWrittenArgLoc(const Expr *Arg, SourceLocation CloseLoc, + const SourceManager &SM, + const LangOptions &LangOpts) { + const CharSourceRange Range = Lexer::makeFileCharRange( + CharSourceRange::getTokenRange(Arg->getBeginLoc(), CloseLoc), SM, + LangOpts); + const SourceLocation ArgLoc = + SM.getExpansionLoc(SM.getTopMacroCallerLoc(Arg->getBeginLoc())); + if (Range.isInvalid() || Range.getBegin() != ArgLoc) + return {}; + return ArgLoc; +} + static bool isParenthesizedTemporary(const Expr *Arg) { Arg = Arg->IgnoreUnlessSpelledInSource(); if (const auto *TempObject = dyn_cast<CXXTemporaryObjectExpr>(Arg)) @@ -265,7 +285,8 @@ static bool isParenthesizedTemporary(const Expr *Arg) { // Given the argument type and the options determine if we should be adding an // argument comment and which diagnostic wording to use. ArgumentCommentCheck::CommentKind -ArgumentCommentCheck::shouldAddComment(const Expr *Arg) const { +ArgumentCommentCheck::shouldAddComment(const Expr *Arg, + const SourceManager &SM) const { const InitListKind Kind = getInitListKind(Arg); const bool IsParenthesizedTemporary = isParenthesizedTemporary(Arg); @@ -274,7 +295,7 @@ ArgumentCommentCheck::shouldAddComment(const Expr *Arg) const { Arg = Arg->IgnoreImplicit(); if (const auto *UO = dyn_cast<UnaryOperator>(Arg)) Arg = UO->getSubExpr()->IgnoreImplicit(); - if (Arg->getExprLoc().isMacroID()) + if (SM.getTopMacroCallerLoc(Arg->getExprLoc()).isMacroID()) return CommentKind::None; if ((CommentAnonymousInitLists && Kind == InitListKind::Anonymous) || @@ -299,6 +320,7 @@ ArgumentCommentCheck::shouldAddComment(const Expr *Arg) const { void ArgumentCommentCheck::checkCallArgs(ASTContext *Ctx, const FunctionDecl *OriginalCallee, SourceLocation ArgBeginLoc, + SourceLocation CloseLoc, llvm::ArrayRef<const Expr *> Args) { const FunctionDecl *Callee = resolveMocks(OriginalCallee); if (!Callee) @@ -315,6 +337,12 @@ void ArgumentCommentCheck::checkCallArgs(ASTContext *Ctx, if ((NumArgs == 0) || (IgnoreSingleArgument && NumArgs == 1)) return; + const SourceManager &SM = Ctx->getSourceManager(); + // A call whose closing parenthesis is a macro location is in a macro + // argument, such as `f(0)` in `ASSERT(f(0))`. getWrittenArgLoc() rejects + // calls in a macro body. + const bool AddMissingComments = !IgnoreMacros || CloseLoc.isFileID(); + const auto MakeFileCharRange = [Ctx](SourceLocation Begin, SourceLocation End) { return Lexer::makeFileCharRange(CharSourceRange::getCharRange(Begin, End), @@ -377,17 +405,22 @@ void ArgumentCommentCheck::checkCallArgs(ASTContext *Ctx, // If the argument comments are missing for configured argument kinds, add // them. - const CommentKind Kind = shouldAddComment(Args[I]); - if (Comments.empty() && Kind != CommentKind::None) { + if (!AddMissingComments || !Comments.empty()) + continue; + const SourceLocation ArgLoc = + getWrittenArgLoc(Args[I], CloseLoc, SM, Ctx->getLangOpts()); + if (ArgLoc.isInvalid()) + continue; + const CommentKind Kind = shouldAddComment(Args[I], SM); + if (Kind != CommentKind::None) { SmallString<32> ArgComment; llvm::Twine(llvm::Twine("/*") + II->getName() + "=*/") .toStringRef(ArgComment); const DiagnosticBuilder Diag = - diag(Args[I]->getBeginLoc(), - "argument comment missing for %select{literal argument|" - "argument}0 %1") + diag(ArgLoc, "argument comment missing for %select{literal argument|" + "argument}0 %1") << (Kind == CommentKind::Literal ? 0 : 1) << II - << FixItHint::CreateInsertion(Args[I]->getBeginLoc(), ArgComment); + << FixItHint::CreateInsertion(ArgLoc, ArgComment); } } } @@ -400,6 +433,7 @@ void ArgumentCommentCheck::check(const MatchFinder::MatchResult &Result) { return; checkCallArgs(Result.Context, Callee, Call->getCallee()->getEndLoc(), + Call->getEndLoc(), llvm::ArrayRef(Call->getArgs(), Call->getNumArgs())); } else { const auto *Construct = cast<CXXConstructExpr>(E); @@ -410,7 +444,7 @@ void ArgumentCommentCheck::check(const MatchFinder::MatchResult &Result) { } checkCallArgs( Result.Context, Construct->getConstructor(), - Construct->getParenOrBraceRange().getBegin(), + Construct->getParenOrBraceRange().getBegin(), Construct->getEndLoc(), llvm::ArrayRef(Construct->getArgs(), Construct->getNumArgs())); } } diff --git a/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.h b/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.h index 3bed1ad4247e5b..037f1317d3574d 100644 --- a/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.h +++ b/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.h @@ -47,6 +47,7 @@ class ArgumentCommentCheck : public ClangTidyCheck { }; const unsigned StrictMode : 1; + const unsigned IgnoreMacros : 1; const unsigned IgnoreSingleArgument : 1; const unsigned CommentAnonymousInitLists : 1; const unsigned CommentBoolLiterals : 1; @@ -61,10 +62,10 @@ class ArgumentCommentCheck : public ClangTidyCheck { llvm::Regex IdentRE; void checkCallArgs(ASTContext *Ctx, const FunctionDecl *Callee, - SourceLocation ArgBeginLoc, + SourceLocation ArgBeginLoc, SourceLocation CloseLoc, llvm::ArrayRef<const Expr *> Args); - CommentKind shouldAddComment(const Expr *Arg) const; + CommentKind shouldAddComment(const Expr *Arg, const SourceManager &SM) const; }; } // namespace clang::tidy::bugprone diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md index 679ca9ecbb4a39..3a66ae9ee96747 100644 --- a/clang-tools-extra/docs/ReleaseNotes.md +++ b/clang-tools-extra/docs/ReleaseNotes.md @@ -184,6 +184,10 @@ infrastructure are described first, followed by tool-specific sections. #### Changes in existing checks +- Improved {doc}`bugprone-argument-comment + <clang-tidy/checks/bugprone/argument-comment>` check by adding the + {option}`IgnoreMacros` option. + - Improved {doc}`bugprone-assignment-in-if-condition <clang-tidy/checks/bugprone/assignment-in-if-condition>` check by ignoring assignments inside GNU statement expressions unless they form the value of diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/argument-comment.md b/clang-tools-extra/docs/clang-tidy/checks/bugprone/argument-comment.md index 68f7a6eb7a260f..0c95e768d9cb53 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/argument-comment.md +++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/argument-comment.md @@ -29,6 +29,14 @@ underscores and case when comparing names -- otherwise they are taken into account. Default is `false`. ``` +```{option} IgnoreMacros +When `false`, the check also adds missing comments to calls in a macro +argument, such as `ASSERT(f(0))`. It never adds them to calls in a macro body, +or to arguments that you pass through another macro, such as `f(ID(0))`. The +check compares argument comments with parameter names regardless of this +option. Default is `true`. +``` + ```{option} IgnoreSingleArgument When `true`, the check will ignore the single argument. Default is `false`. ``` diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/argument-comment-macros.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/argument-comment-macros.cpp new file mode 100644 index 00000000000000..f93162fa360ff0 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/argument-comment-macros.cpp @@ -0,0 +1,72 @@ +// RUN: %check_clang_tidy -check-suffixes=,MACROS %s bugprone-argument-comment %t -- \ +// RUN: -config="{CheckOptions: { \ +// RUN: bugprone-argument-comment.CommentBoolLiterals: true, \ +// RUN: bugprone-argument-comment.CommentIntegerLiterals: true, \ +// RUN: bugprone-argument-comment.IgnoreMacros: false}}" -- +// RUN: %check_clang_tidy %s bugprone-argument-comment %t -- \ +// RUN: -config="{CheckOptions: { \ +// RUN: bugprone-argument-comment.CommentBoolLiterals: true, \ +// RUN: bugprone-argument-comment.CommentIntegerLiterals: true}}" -- + +struct A { + void foo(bool abc); +}; + +void g(int a); +void j(int a, int b, int c); +void k(int a, int b); +void real_g(int a, bool b); + +#define CHECK(e) do { (void)(e); } while (0) +#define CHECK_TWICE(e) do { (void)(e); (void)(e); } while (0) +#define ID(x) x +#define X(x) (x) +#define VOID(e) ((void)(e)) +#define CALL_G(x) g(x) +#define CALL_G_1 g(1) +#define CALL(fn, a) fn(a) +#define CALL2(fn, a) fn(a, a) +#define my_g real_g + +void test() { + A a; + + my_g(0, true); + // CHECK-MESSAGES: [[@LINE-1]]:8: warning: argument comment missing for literal argument 'a' [bugprone-argument-comment] + // CHECK-MESSAGES: [[@LINE-2]]:11: warning: argument comment missing for literal argument 'b' [bugprone-argument-comment] + // CHECK-FIXES: my_g(/*a=*/0, /*b=*/true); + + ID(real_g)(0, true); + // CHECK-MESSAGES: [[@LINE-1]]:14: warning: argument comment missing for literal argument 'a' [bugprone-argument-comment] + // CHECK-MESSAGES: [[@LINE-2]]:17: warning: argument comment missing for literal argument 'b' [bugprone-argument-comment] + // CHECK-FIXES: ID(real_g)(/*a=*/0, /*b=*/true); + + CHECK(a.foo(true)); + // CHECK-MESSAGES-MACROS: [[@LINE-1]]:15: warning: argument comment missing for literal argument 'abc' [bugprone-argument-comment] + // CHECK-FIXES-MACROS: CHECK(a.foo(/*abc=*/true)); + CHECK(a.foo(/*abc=*/true)); + + CHECK(j(1, X(1), -1)); + // CHECK-MESSAGES-MACROS: [[@LINE-1]]:11: warning: argument comment missing for literal argument 'a' [bugprone-argument-comment] + // CHECK-MESSAGES-MACROS: [[@LINE-2]]:20: warning: argument comment missing for literal argument 'c' [bugprone-argument-comment] + // CHECK-FIXES-MACROS: CHECK(j(/*a=*/1, X(1), /*c=*/-1)); + + CHECK_TWICE(g(1)); + // CHECK-MESSAGES-MACROS: [[@LINE-1]]:17: warning: argument comment missing for literal argument 'a' [bugprone-argument-comment] + // CHECK-FIXES-MACROS: CHECK_TWICE(g(/*a=*/1)); + + CHECK(VOID(g(1))); + // CHECK-MESSAGES-MACROS: [[@LINE-1]]:16: warning: argument comment missing for literal argument 'a' [bugprone-argument-comment] + // CHECK-FIXES-MACROS: CHECK(VOID(g(/*a=*/1))); + + // The check adds no comment when the literal and the closing parenthesis of + // the call are in different macro arguments, or when the call is in a macro + // body. + g(ID(1)); + CHECK(g(ID(1))); + ID(g)(ID(1)); + CALL_G(1); + CALL_G_1; + CALL(g, 1); + CALL2(k, 1); +} >From 85d13a7e6cbfc50f6a3fca2bde47340ed3c3990a Mon Sep 17 00:00:00 2001 From: Daan De Meyer <[email protected]> Date: Fri, 30 Oct 2026 16:02:51 +0100 Subject: [PATCH 2/4] [clang-tidy] Support NULL/true/false in argument-comment Before C23, C has no nullptr, true and false keywords. C code spells them with the NULL macro of <stddef.h> and the true and false macros of <stdbool.h> instead. bugprone-argument-comment skips literals that come from a macro, so CommentNullPtrs and CommentBoolLiterals don't apply to C code that uses the macros. The same holds for C++: NULL is a macro there too, and GNU C++98 defines true and false as macros in <stdbool.h>. Let's treat an argument that is exactly one of the macros in the new NullPtrMacros and BoolLiteralMacros options like the literal it stands for. The options default to NULL and to true and false. Other macros already name their value, so they don't need a comment by default. A project can add its own macros to the options, such as TRUE and FALSE. The comment before an argument that follows a multi-token macro such as NULL wasn't found either. In f(NULL, /*b=*/NULL), the range from the end of the first NULL has no file range, and neither has the range of the second argument. Let's fall back to an empty range at the start of the argument, which maps to the location of the macro name. With that range, the check also reports comments there that don't match the parameter name. Signed-off-by: Daan De Meyer <[email protected]> --- .../bugprone/ArgumentCommentCheck.cpp | 38 ++++++++++++-- .../bugprone/ArgumentCommentCheck.h | 4 +- clang-tools-extra/docs/ReleaseNotes.md | 6 ++- .../checks/bugprone/argument-comment.md | 21 ++++++-- .../checkers/bugprone/argument-comment-c.c | 52 +++++++++++++++++++ .../bugprone/argument-comment-gnu98.cpp | 18 +++++++ .../bugprone/argument-comment-macro-names.c | 31 +++++++++++ 7 files changed, 158 insertions(+), 12 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/bugprone/argument-comment-c.c create mode 100644 clang-tools-extra/test/clang-tidy/checkers/bugprone/argument-comment-gnu98.cpp create mode 100644 clang-tools-extra/test/clang-tidy/checkers/bugprone/argument-comment-macro-names.c diff --git a/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp index 3d808144c57a89..bb588e2821d122 100644 --- a/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp @@ -13,6 +13,7 @@ #include "clang/Lex/Token.h" #include "../utils/LexerUtils.h" +#include "../utils/OptionsUtils.h" using namespace clang::ast_matchers; @@ -50,6 +51,10 @@ ArgumentCommentCheck::ArgumentCommentCheck(StringRef Name, CommentTypedInitLists(Options.get("CommentTypedInitLists", false)), CommentUserDefinedLiterals( Options.get("CommentUserDefinedLiterals", false)), + NullPtrMacros(utils::options::parseStringList( + Options.get("NullPtrMacros", "NULL"))), + BoolLiteralMacros(utils::options::parseStringList( + Options.get("BoolLiteralMacros", "true;false"))), IdentRE("^(/\\* *)([_A-Za-z][_A-Za-z0-9]*)( *= *\\*/)$") {} void ArgumentCommentCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { @@ -67,6 +72,10 @@ void ArgumentCommentCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { Options.store(Opts, "CommentStringLiterals", CommentStringLiterals); Options.store(Opts, "CommentTypedInitLists", CommentTypedInitLists); Options.store(Opts, "CommentUserDefinedLiterals", CommentUserDefinedLiterals); + Options.store(Opts, "NullPtrMacros", + utils::options::serializeStringList(NullPtrMacros)); + Options.store(Opts, "BoolLiteralMacros", + utils::options::serializeStringList(BoolLiteralMacros)); } void ArgumentCommentCheck::registerMatchers(MatchFinder *Finder) { @@ -286,7 +295,8 @@ static bool isParenthesizedTemporary(const Expr *Arg) { // argument comment and which diagnostic wording to use. ArgumentCommentCheck::CommentKind ArgumentCommentCheck::shouldAddComment(const Expr *Arg, - const SourceManager &SM) const { + const ASTContext &Ctx) const { + const SourceManager &SM = Ctx.getSourceManager(); const InitListKind Kind = getInitListKind(Arg); const bool IsParenthesizedTemporary = isParenthesizedTemporary(Arg); @@ -295,8 +305,24 @@ ArgumentCommentCheck::shouldAddComment(const Expr *Arg, Arg = Arg->IgnoreImplicit(); if (const auto *UO = dyn_cast<UnaryOperator>(Arg)) Arg = UO->getSubExpr()->IgnoreImplicit(); - if (SM.getTopMacroCallerLoc(Arg->getExprLoc()).isMacroID()) + + // C spells the null pointer constant with the macro `NULL`, and the boolean + // constants before C23 with the macros `true` and `false`. The options + // NullPtrMacros and BoolLiteralMacros contain `NULL`, `true` and `false` by + // default. Other macros, such as `MAX_SIZE`, already name their value and + // need no comment. + if (SM.getTopMacroCallerLoc(Arg->getExprLoc()).isMacroID()) { + const StringRef MacroName = + Lexer::getSourceText(CharSourceRange::getTokenRange( + SM.getTopMacroCallerLoc(Arg->getBeginLoc()), + SM.getTopMacroCallerLoc(Arg->getEndLoc())), + SM, Ctx.getLangOpts()); + if ((CommentNullPtrs && llvm::is_contained(NullPtrMacros, MacroName)) || + (CommentBoolLiterals && + llvm::is_contained(BoolLiteralMacros, MacroName))) + return CommentKind::Literal; return CommentKind::None; + } if ((CommentAnonymousInitLists && Kind == InitListKind::Anonymous) || (CommentTypedInitLists && Kind == InitListKind::Typed) || @@ -373,9 +399,11 @@ void ArgumentCommentCheck::checkCallArgs(ASTContext *Ctx, Comments = utils::lexer::getTrailingCommentsInRange( BeforeArgument, Ctx->getSourceManager(), Ctx->getLangOpts()); } else { - // Fall back to parsing back from the start of the argument. + // Fall back to parsing back from the start of the argument. The range + // is empty because a range that ends inside a macro expansion, such as + // the `)` of `NULL`, has no file range. const CharSourceRange ArgsRange = - MakeFileCharRange(Args[I]->getBeginLoc(), Args[I]->getEndLoc()); + MakeFileCharRange(Args[I]->getBeginLoc(), Args[I]->getBeginLoc()); Comments = getCommentsBeforeLoc(Ctx, ArgsRange.getBegin()); } @@ -411,7 +439,7 @@ void ArgumentCommentCheck::checkCallArgs(ASTContext *Ctx, getWrittenArgLoc(Args[I], CloseLoc, SM, Ctx->getLangOpts()); if (ArgLoc.isInvalid()) continue; - const CommentKind Kind = shouldAddComment(Args[I], SM); + const CommentKind Kind = shouldAddComment(Args[I], *Ctx); if (Kind != CommentKind::None) { SmallString<32> ArgComment; llvm::Twine(llvm::Twine("/*") + II->getName() + "=*/") diff --git a/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.h b/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.h index 037f1317d3574d..9e1fa5b6d811b7 100644 --- a/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.h +++ b/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.h @@ -59,13 +59,15 @@ class ArgumentCommentCheck : public ClangTidyCheck { const unsigned CommentStringLiterals : 1; const unsigned CommentTypedInitLists : 1; const unsigned CommentUserDefinedLiterals : 1; + const std::vector<StringRef> NullPtrMacros; + const std::vector<StringRef> BoolLiteralMacros; llvm::Regex IdentRE; void checkCallArgs(ASTContext *Ctx, const FunctionDecl *Callee, SourceLocation ArgBeginLoc, SourceLocation CloseLoc, llvm::ArrayRef<const Expr *> Args); - CommentKind shouldAddComment(const Expr *Arg, const SourceManager &SM) const; + CommentKind shouldAddComment(const Expr *Arg, const ASTContext &Ctx) const; }; } // namespace clang::tidy::bugprone diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md index 3a66ae9ee96747..d8881f9c84a360 100644 --- a/clang-tools-extra/docs/ReleaseNotes.md +++ b/clang-tools-extra/docs/ReleaseNotes.md @@ -185,8 +185,10 @@ infrastructure are described first, followed by tool-specific sections. #### Changes in existing checks - Improved {doc}`bugprone-argument-comment - <clang-tidy/checks/bugprone/argument-comment>` check by adding the - {option}`IgnoreMacros` option. + <clang-tidy/checks/bugprone/argument-comment>` check by treating the `NULL`, + `true` and `false` macros as literals and by adding the + {option}`IgnoreMacros`, {option}`NullPtrMacros` and + {option}`BoolLiteralMacros` options. - Improved {doc}`bugprone-assignment-in-if-condition <clang-tidy/checks/bugprone/assignment-in-if-condition>` check by ignoring diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/argument-comment.md b/clang-tools-extra/docs/clang-tidy/checks/bugprone/argument-comment.md index 0c95e768d9cb53..a56a10876b7124 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/argument-comment.md +++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/argument-comment.md @@ -65,8 +65,8 @@ foo(/*Dims=*/{}); ```{option} CommentBoolLiterals When `true`, the check will add argument comments in the format -`/*ParameterName=*/` right before the boolean literal argument. -Default is `false`. +`/*ParameterName=*/` right before the boolean literal argument and the macros +in {option}`BoolLiteralMacros`. Default is `false`. ``` Before: @@ -153,8 +153,8 @@ foo(/*MeaningOfLife=*/42); ```{option} CommentNullPtrs When `true`, the check will add argument comments in the format -`/*ParameterName=*/` right before the nullptr literal argument. -Default is `false`. +`/*ParameterName=*/` right before the nullptr literal argument and the macros +in {option}`NullPtrMacros`. Default is `false`. ``` Before: @@ -280,3 +280,16 @@ double operator"" _km(long double); foo(/*Distance=*/402.0_km); ``` + +```{option} NullPtrMacros +A semicolon-separated list of macros that {option}`CommentNullPtrs` treats like +`nullptr`. The check only matches an argument that is exactly one of these +macros. Default is `NULL`. +``` + +```{option} BoolLiteralMacros +A semicolon-separated list of macros that {option}`CommentBoolLiterals` treats +like boolean literals. The check only matches an argument that is exactly one +of these macros. Default is `true;false`, which covers `<stdbool.h>` in C +before C23 and in GNU C++98. +``` diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/argument-comment-c.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone/argument-comment-c.c new file mode 100644 index 00000000000000..63119c2259518e --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/argument-comment-c.c @@ -0,0 +1,52 @@ +// RUN: %check_clang_tidy %s bugprone-argument-comment %t -- \ +// RUN: -config="{CheckOptions: { \ +// RUN: bugprone-argument-comment.CommentBoolLiterals: true, \ +// RUN: bugprone-argument-comment.CommentNullPtrs: true, \ +// RUN: bugprone-argument-comment.IgnoreMacros: false}}" -- + +#include <stdbool.h> +#include <stddef.h> + +#define CHECK(e) do { (void)(e); } while (0) +#define ID(x) x +#define MY_NULL NULL +#define MY_TRUE true +#define CALL_F f(NULL, true) + +int f(const char *path, bool force); +int h(const char *path, const char *root); + +void test(void) { + f(NULL, false); + // CHECK-MESSAGES: [[@LINE-1]]:5: warning: argument comment missing for literal argument 'path' [bugprone-argument-comment] + // CHECK-MESSAGES: [[@LINE-2]]:11: warning: argument comment missing for literal argument 'force' [bugprone-argument-comment] + // CHECK-FIXES: f(/*path=*/NULL, /*force=*/false); + f(/*path=*/NULL, /*force=*/false); + f(/* path= */ NULL, /* force= */ true); + + CHECK(f(NULL, true)); + // CHECK-MESSAGES: [[@LINE-1]]:11: warning: argument comment missing for literal argument 'path' [bugprone-argument-comment] + // CHECK-MESSAGES: [[@LINE-2]]:17: warning: argument comment missing for literal argument 'force' [bugprone-argument-comment] + // CHECK-FIXES: CHECK(f(/*path=*/NULL, /*force=*/true)); + CHECK(f(/*path=*/NULL, /*force=*/true)); + + h(NULL, NULL); + // CHECK-MESSAGES: [[@LINE-1]]:5: warning: argument comment missing for literal argument 'path' [bugprone-argument-comment] + // CHECK-MESSAGES: [[@LINE-2]]:11: warning: argument comment missing for literal argument 'root' [bugprone-argument-comment] + // CHECK-FIXES: h(/*path=*/NULL, /*root=*/NULL); + h(/*path=*/NULL, /*root=*/NULL); + CHECK(h(/* path= */ NULL, /* root= */ NULL)); + + h(NULL, /*rot=*/NULL); + // CHECK-MESSAGES: [[@LINE-1]]:5: warning: argument comment missing for literal argument 'path' [bugprone-argument-comment] + // CHECK-MESSAGES: [[@LINE-2]]:11: warning: argument name 'rot' in comment does not match parameter name 'root' [bugprone-argument-comment] + // CHECK-MESSAGES: :[[@LINE-26]]:37: note: 'root' declared here + // CHECK-FIXES: h(/*path=*/NULL, /*root=*/NULL); + + // The check adds no comment to other macros, to integers, to macros in + // another macro argument, and to calls in a macro body. + f(MY_NULL, MY_TRUE); + f(0, 1); + f(ID(NULL), ID(true)); + CALL_F; +} diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/argument-comment-gnu98.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/argument-comment-gnu98.cpp new file mode 100644 index 00000000000000..abb6e150e9d81d --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/argument-comment-gnu98.cpp @@ -0,0 +1,18 @@ +// RUN: %check_clang_tidy -std=gnu++98 %s bugprone-argument-comment %t -- \ +// RUN: -config="{CheckOptions: { \ +// RUN: bugprone-argument-comment.CommentBoolLiterals: true, \ +// RUN: bugprone-argument-comment.CommentNullPtrs: true}}" -- + +// In GNU C++98, <stdbool.h> defines `true` and `false` as macros, and +// <stddef.h> defines `NULL` as `__null`. +#include <stdbool.h> +#include <stddef.h> + +void f(const char *path, bool force); + +void test() { + f(NULL, true); + // CHECK-MESSAGES: [[@LINE-1]]:5: warning: argument comment missing for literal argument 'path' [bugprone-argument-comment] + // CHECK-MESSAGES: [[@LINE-2]]:11: warning: argument comment missing for literal argument 'force' [bugprone-argument-comment] + // CHECK-FIXES: f(/*path=*/NULL, /*force=*/true); +} diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/argument-comment-macro-names.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone/argument-comment-macro-names.c new file mode 100644 index 00000000000000..b2d8c98eb85b19 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/argument-comment-macro-names.c @@ -0,0 +1,31 @@ +// RUN: %check_clang_tidy %s bugprone-argument-comment %t -- \ +// RUN: -config="{CheckOptions: { \ +// RUN: bugprone-argument-comment.CommentBoolLiterals: true, \ +// RUN: bugprone-argument-comment.CommentNullPtrs: true, \ +// RUN: bugprone-argument-comment.NullPtrMacros: 'NULL;MY_NULL', \ +// RUN: bugprone-argument-comment.BoolLiteralMacros: 'TRUE;FALSE'}}" -- + +#include <stddef.h> + +#define MY_NULL ((void *)0) +#define TRUE (1 == 1) +#define FALSE 0 +#define OTHER 0 + +int f(const char *path, int force); + +void test(void) { + f(MY_NULL, TRUE); + // CHECK-MESSAGES: [[@LINE-1]]:5: warning: argument comment missing for literal argument 'path' [bugprone-argument-comment] + // CHECK-MESSAGES: [[@LINE-2]]:14: warning: argument comment missing for literal argument 'force' [bugprone-argument-comment] + // CHECK-FIXES: f(/*path=*/MY_NULL, /*force=*/TRUE); + + f(NULL, FALSE); + // CHECK-MESSAGES: [[@LINE-1]]:5: warning: argument comment missing for literal argument 'path' [bugprone-argument-comment] + // CHECK-MESSAGES: [[@LINE-2]]:11: warning: argument comment missing for literal argument 'force' [bugprone-argument-comment] + // CHECK-FIXES: f(/*path=*/NULL, /*force=*/FALSE); + + f(NULL, OTHER); + // CHECK-MESSAGES: [[@LINE-1]]:5: warning: argument comment missing for literal argument 'path' [bugprone-argument-comment] + // CHECK-FIXES: f(/*path=*/NULL, OTHER); +} >From 437c3d8d487cc869fea83ead6a438a7a518fafa8 Mon Sep 17 00:00:00 2001 From: Daan De Meyer <[email protected]> Date: Fri, 30 Oct 2026 16:03:24 +0100 Subject: [PATCH 3/4] [clang-tidy] Add zero literal option to argument-comment Many C APIs take 0 to mean "no flags" or "the default", and a bare 0 doesn't tell the reader which parameter it sets. CommentIntegerLiterals asks for a comment on every integer literal though, including sizes, modes and counts whose meaning is clear from their value. Let's add a CommentZeroIntegerLiterals option to bugprone-argument-comment that only asks for a comment on integer literals with the value zero. We check for zero before stripping a unary operator, because ~0 and !0 aren't zero. Signed-off-by: Daan De Meyer <[email protected]> --- .../bugprone/ArgumentCommentCheck.cpp | 9 +++++++ .../bugprone/ArgumentCommentCheck.h | 1 + clang-tools-extra/docs/ReleaseNotes.md | 4 +-- .../checks/bugprone/argument-comment.md | 25 +++++++++++++++++++ ...argument-comment-zero-integer-literals.cpp | 23 +++++++++++++++++ 5 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/bugprone/argument-comment-zero-integer-literals.cpp diff --git a/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp index bb588e2821d122..bbf1e10dee54b1 100644 --- a/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp @@ -51,6 +51,8 @@ ArgumentCommentCheck::ArgumentCommentCheck(StringRef Name, CommentTypedInitLists(Options.get("CommentTypedInitLists", false)), CommentUserDefinedLiterals( Options.get("CommentUserDefinedLiterals", false)), + CommentZeroIntegerLiterals( + Options.get("CommentZeroIntegerLiterals", false)), NullPtrMacros(utils::options::parseStringList( Options.get("NullPtrMacros", "NULL"))), BoolLiteralMacros(utils::options::parseStringList( @@ -72,6 +74,7 @@ void ArgumentCommentCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { Options.store(Opts, "CommentStringLiterals", CommentStringLiterals); Options.store(Opts, "CommentTypedInitLists", CommentTypedInitLists); Options.store(Opts, "CommentUserDefinedLiterals", CommentUserDefinedLiterals); + Options.store(Opts, "CommentZeroIntegerLiterals", CommentZeroIntegerLiterals); Options.store(Opts, "NullPtrMacros", utils::options::serializeStringList(NullPtrMacros)); Options.store(Opts, "BoolLiteralMacros", @@ -303,6 +306,11 @@ ArgumentCommentCheck::shouldAddComment(const Expr *Arg, // Strip implicit wrappers so brace-init arguments bound to references still // look like list-initialization at this point. Arg = Arg->IgnoreImplicit(); + // Check for zero before stripping the unary operator, because `~0` and `!0` + // are not zero. + const auto *ZeroLiteral = dyn_cast<IntegerLiteral>(Arg); + if (ZeroLiteral && !ZeroLiteral->getValue().isZero()) + ZeroLiteral = nullptr; if (const auto *UO = dyn_cast<UnaryOperator>(Arg)) Arg = UO->getSubExpr()->IgnoreImplicit(); @@ -332,6 +340,7 @@ ArgumentCommentCheck::shouldAddComment(const Expr *Arg, if ((CommentBoolLiterals && isa<CXXBoolLiteralExpr>(Arg)) || (CommentIntegerLiterals && isa<IntegerLiteral>(Arg)) || + (CommentZeroIntegerLiterals && ZeroLiteral) || (CommentFloatLiterals && isa<FloatingLiteral>(Arg)) || (CommentUserDefinedLiterals && isa<UserDefinedLiteral>(Arg)) || (CommentCharacterLiterals && isa<CharacterLiteral>(Arg)) || diff --git a/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.h b/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.h index 9e1fa5b6d811b7..86add03d0d8b58 100644 --- a/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.h +++ b/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.h @@ -59,6 +59,7 @@ class ArgumentCommentCheck : public ClangTidyCheck { const unsigned CommentStringLiterals : 1; const unsigned CommentTypedInitLists : 1; const unsigned CommentUserDefinedLiterals : 1; + const unsigned CommentZeroIntegerLiterals : 1; const std::vector<StringRef> NullPtrMacros; const std::vector<StringRef> BoolLiteralMacros; llvm::Regex IdentRE; diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md index d8881f9c84a360..06c7c9656f2c93 100644 --- a/clang-tools-extra/docs/ReleaseNotes.md +++ b/clang-tools-extra/docs/ReleaseNotes.md @@ -187,8 +187,8 @@ infrastructure are described first, followed by tool-specific sections. - Improved {doc}`bugprone-argument-comment <clang-tidy/checks/bugprone/argument-comment>` check by treating the `NULL`, `true` and `false` macros as literals and by adding the - {option}`IgnoreMacros`, {option}`NullPtrMacros` and - {option}`BoolLiteralMacros` options. + {option}`IgnoreMacros`, {option}`NullPtrMacros`, {option}`BoolLiteralMacros` + and {option}`CommentZeroIntegerLiterals` options. - Improved {doc}`bugprone-assignment-in-if-condition <clang-tidy/checks/bugprone/assignment-in-if-condition>` check by ignoring diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/argument-comment.md b/clang-tools-extra/docs/clang-tidy/checks/bugprone/argument-comment.md index a56a10876b7124..7391b797fbc0eb 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/argument-comment.md +++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/argument-comment.md @@ -281,6 +281,31 @@ double operator"" _km(long double); foo(/*Distance=*/402.0_km); ``` +```{option} CommentZeroIntegerLiterals +When `true`, the check will add argument comments in the format +`/*ParameterName=*/` right before integer literal arguments with the value +zero, which often stands for "none" or "default". This option has no effect +when {option}`CommentIntegerLiterals` is `true`. Default is `false`. +``` + +Before: + +```c +int open_file(const char *Path, int Flags); + +open_file(Path, 0); +open_file(Path, 1); +``` + +After: + +```c +int open_file(const char *Path, int Flags); + +open_file(Path, /*Flags=*/0); +open_file(Path, 1); +``` + ```{option} NullPtrMacros A semicolon-separated list of macros that {option}`CommentNullPtrs` treats like `nullptr`. The check only matches an argument that is exactly one of these diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/argument-comment-zero-integer-literals.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/argument-comment-zero-integer-literals.cpp new file mode 100644 index 00000000000000..74063cfe7c9e1c --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/argument-comment-zero-integer-literals.cpp @@ -0,0 +1,23 @@ +// RUN: %check_clang_tidy %s bugprone-argument-comment %t -- \ +// RUN: -config="{CheckOptions: { \ +// RUN: bugprone-argument-comment.CommentZeroIntegerLiterals: true}}" -- + +void f(int flags, unsigned long size, double ratio, bool force); + +#define ZERO 0 + +void test() { + f(0, 0UL, 0.0, false); + // CHECK-MESSAGES: [[@LINE-1]]:5: warning: argument comment missing for literal argument 'flags' [bugprone-argument-comment] + // CHECK-MESSAGES: [[@LINE-2]]:8: warning: argument comment missing for literal argument 'size' [bugprone-argument-comment] + // CHECK-FIXES: f(/*flags=*/0, /*size=*/0UL, 0.0, false); + + f(0x0, 4096, 1.0, true); + // CHECK-MESSAGES: [[@LINE-1]]:5: warning: argument comment missing for literal argument 'flags' [bugprone-argument-comment] + // CHECK-FIXES: f(/*flags=*/0x0, 4096, 1.0, true); + + f(/*flags=*/0, /*size=*/0, 0.0, false); + f(1, 4096, 0.0, true); + f(ZERO, ZERO, 0.0, false); + f(~0, -0, 0.0, !0); +} >From 005be693d380238f4d32faeb003b6fff7be08480 Mon Sep 17 00:00:00 2001 From: Daan De Meyer <[email protected]> Date: Fri, 30 Oct 2026 16:04:28 +0100 Subject: [PATCH 4/4] [clang-tidy] Add IgnoredParameterNames to argument-comment C code often names parameters with a single letter or a generic name such as arg1. A comment like /*p=*/NULL tells the reader nothing, but the Comment* options of bugprone-argument-comment still ask for one. When the caller writes /*path=*/NULL for a parameter named p instead, the check reports a mismatch. Let's add an option with a semicolon-separated list of regular expressions for the parameter names that the check skips. Most clang-tidy options with regular expressions use that format. We report a regular expression that doesn't compile as a configuration error, like misc-include-cleaner does, instead of silently matching nothing. Signed-off-by: Daan De Meyer <[email protected]> --- .../bugprone/ArgumentCommentCheck.cpp | 22 +++++++++++++++-- .../bugprone/ArgumentCommentCheck.h | 2 ++ clang-tools-extra/docs/ReleaseNotes.md | 5 ++-- .../checks/bugprone/argument-comment.md | 9 +++++++ ...gument-comment-ignored-parameter-names.cpp | 24 +++++++++++++++++++ 5 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/bugprone/argument-comment-ignored-parameter-names.cpp diff --git a/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp index bbf1e10dee54b1..b8f90a3b89513c 100644 --- a/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp @@ -57,7 +57,20 @@ ArgumentCommentCheck::ArgumentCommentCheck(StringRef Name, Options.get("NullPtrMacros", "NULL"))), BoolLiteralMacros(utils::options::parseStringList( Options.get("BoolLiteralMacros", "true;false"))), - IdentRE("^(/\\* *)([_A-Za-z][_A-Za-z0-9]*)( *= *\\*/)$") {} + IgnoredParameterNames(utils::options::parseStringList( + Options.get("IgnoredParameterNames", ""))), + IdentRE("^(/\\* *)([_A-Za-z][_A-Za-z0-9]*)( *= *\\*/)$") { + for (const StringRef Name : IgnoredParameterNames) { + llvm::Regex Regex(Name); + if (!Regex.isValid()) { + configurationDiag("Invalid regular expression '%0' in " + "IgnoredParameterNames") + << Name; + continue; + } + IgnoredParameterNameRegexes.push_back(std::move(Regex)); + } +} void ArgumentCommentCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { Options.store(Opts, "StrictMode", StrictMode); @@ -79,6 +92,8 @@ void ArgumentCommentCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { utils::options::serializeStringList(NullPtrMacros)); Options.store(Opts, "BoolLiteralMacros", utils::options::serializeStringList(BoolLiteralMacros)); + Options.store(Opts, "IgnoredParameterNames", + utils::options::serializeStringList(IgnoredParameterNames)); } void ArgumentCommentCheck::registerMatchers(MatchFinder *Finder) { @@ -388,7 +403,10 @@ void ArgumentCommentCheck::checkCallArgs(ASTContext *Ctx, for (unsigned I = 0; I < NumArgs; ++I) { const ParmVarDecl *PVD = Callee->getParamDecl(I); const IdentifierInfo *II = PVD->getIdentifier(); - if (!II) + if (!II || llvm::any_of(IgnoredParameterNameRegexes, + [II](const llvm::Regex &Regex) { + return Regex.match(II->getName()); + })) continue; // Don't warn on arguments for parameters instantiated from template // parameter packs. If we find more arguments than the template diff --git a/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.h b/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.h index 86add03d0d8b58..143db4f6fd1c96 100644 --- a/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.h +++ b/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.h @@ -62,6 +62,8 @@ class ArgumentCommentCheck : public ClangTidyCheck { const unsigned CommentZeroIntegerLiterals : 1; const std::vector<StringRef> NullPtrMacros; const std::vector<StringRef> BoolLiteralMacros; + const std::vector<StringRef> IgnoredParameterNames; + std::vector<llvm::Regex> IgnoredParameterNameRegexes; llvm::Regex IdentRE; void checkCallArgs(ASTContext *Ctx, const FunctionDecl *Callee, diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md index 06c7c9656f2c93..d6c578ad49f8ce 100644 --- a/clang-tools-extra/docs/ReleaseNotes.md +++ b/clang-tools-extra/docs/ReleaseNotes.md @@ -187,8 +187,9 @@ infrastructure are described first, followed by tool-specific sections. - Improved {doc}`bugprone-argument-comment <clang-tidy/checks/bugprone/argument-comment>` check by treating the `NULL`, `true` and `false` macros as literals and by adding the - {option}`IgnoreMacros`, {option}`NullPtrMacros`, {option}`BoolLiteralMacros` - and {option}`CommentZeroIntegerLiterals` options. + {option}`IgnoreMacros`, {option}`NullPtrMacros`, {option}`BoolLiteralMacros`, + {option}`CommentZeroIntegerLiterals` and {option}`IgnoredParameterNames` + options. - Improved {doc}`bugprone-assignment-in-if-condition <clang-tidy/checks/bugprone/assignment-in-if-condition>` check by ignoring diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/argument-comment.md b/clang-tools-extra/docs/clang-tidy/checks/bugprone/argument-comment.md index 7391b797fbc0eb..4668fb94ef51fe 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/argument-comment.md +++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/argument-comment.md @@ -41,6 +41,15 @@ option. Default is `true`. When `true`, the check will ignore the single argument. Default is `false`. ``` +```{option} IgnoredParameterNames +A semicolon-separated list of regular expressions for the parameter names that +the check skips. The check neither compares the argument comment of a matching +parameter nor adds a missing comment for it. A regular expression matches +anywhere in the name unless you anchor it. Use the option for names that say +nothing about the parameter, such as `^[a-z]$;^arg[0-9]*$`. Default is an +empty string. +``` + ```{option} CommentAnonymousInitLists When `true`, the check will add argument comments in the format `/*ParameterName=*/` right before anonymous braced-init list arguments diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/argument-comment-ignored-parameter-names.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/argument-comment-ignored-parameter-names.cpp new file mode 100644 index 00000000000000..2d24371ab3b7c8 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/argument-comment-ignored-parameter-names.cpp @@ -0,0 +1,24 @@ +// RUN: %check_clang_tidy %s bugprone-argument-comment %t -- \ +// RUN: -config="{CheckOptions: { \ +// RUN: bugprone-argument-comment.CommentBoolLiterals: true, \ +// RUN: bugprone-argument-comment.IgnoredParameterNames: '^[a-z]$;^arg[0-9]*$'}}" -- +// RUN: %check_clang_tidy -check-suffix=INVALID %s bugprone-argument-comment %t -- \ +// RUN: -config="{CheckOptions: { \ +// RUN: bugprone-argument-comment.CommentBoolLiterals: true, \ +// RUN: bugprone-argument-comment.IgnoredParameterNames: '[;^arg[0-9]*$'}}" -- + +// CHECK-MESSAGES-INVALID: warning: Invalid regular expression '[' in IgnoredParameterNames [clang-tidy-config] + +void f(bool b, bool arg1, bool force); + +void test() { + f(true, false, true); + // CHECK-MESSAGES: [[@LINE-1]]:18: warning: argument comment missing for literal argument 'force' [bugprone-argument-comment] + // CHECK-MESSAGES-INVALID: [[@LINE-2]]:5: warning: argument comment missing for literal argument 'b' [bugprone-argument-comment] + // CHECK-MESSAGES-INVALID: [[@LINE-3]]:18: warning: argument comment missing for literal argument 'force' [bugprone-argument-comment] + // CHECK-FIXES: f(true, false, /*force=*/true); + // CHECK-FIXES-INVALID: f(/*b=*/true, false, /*force=*/true); + + f(/*enable=*/true, /*verbose=*/false, /*force=*/true); + // CHECK-MESSAGES-INVALID: [[@LINE-1]]:5: warning: argument name 'enable' in comment does not match parameter name 'b' [bugprone-argument-comment] +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
