Skip to content

Commit 1aa31a8

Browse files
committed
Add bugprone-argument-comment option: IgnoreSingleArgument.
When true, the check will ignore the single argument. Sometimes, it's not necessary to add comment to single argument. For example: std::string name("Yubo Xie"); pScreen->SetWidth(1920); pScreen->SetHeight(1080); This option can ignore such single argument in bugprone-argument-comment check.
1 parent b458819 commit 1aa31a8

File tree

4 files changed

+105
-1
lines changed

4 files changed

+105
-1
lines changed

clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ ArgumentCommentCheck::ArgumentCommentCheck(StringRef Name,
2424
ClangTidyContext *Context)
2525
: ClangTidyCheck(Name, Context),
2626
StrictMode(Options.getLocalOrGlobal("StrictMode", 0) != 0),
27+
IgnoreSingleArgument(Options.get("IgnoreSingleArgument", 0) != 0),
2728
CommentBoolLiterals(Options.getLocalOrGlobal("CommentBoolLiterals", 0) !=
2829
0),
2930
CommentIntegerLiterals(
@@ -41,6 +42,7 @@ ArgumentCommentCheck::ArgumentCommentCheck(StringRef Name,
4142

4243
void ArgumentCommentCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
4344
Options.store(Opts, "StrictMode", StrictMode);
45+
Options.store(Opts, "IgnoreSingleArgument", IgnoreSingleArgument);
4446
Options.store(Opts, "CommentBoolLiterals", CommentBoolLiterals);
4547
Options.store(Opts, "CommentIntegerLiterals", CommentIntegerLiterals);
4648
Options.store(Opts, "CommentFloatLiterals", CommentFloatLiterals);
@@ -254,7 +256,7 @@ void ArgumentCommentCheck::checkCallArgs(ASTContext *Ctx,
254256

255257
Callee = Callee->getFirstDecl();
256258
unsigned NumArgs = std::min<unsigned>(Args.size(), Callee->getNumParams());
257-
if (NumArgs == 0)
259+
if ((NumArgs == 0) || (IgnoreSingleArgument && NumArgs == 1))
258260
return;
259261

260262
auto MakeFileCharRange = [Ctx](SourceLocation Begin, SourceLocation End) {

clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class ArgumentCommentCheck : public ClangTidyCheck {
4141

4242
private:
4343
const unsigned StrictMode : 1;
44+
const unsigned IgnoreSingleArgument : 1;
4445
const unsigned CommentBoolLiterals : 1;
4546
const unsigned CommentIntegerLiterals : 1;
4647
const unsigned CommentFloatLiterals : 1;

clang-tools-extra/docs/clang-tidy/checks/bugprone-argument-comment.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ Options
2828
underscores and case when comparing names -- otherwise they are taken into
2929
account.
3030

31+
.. option:: IgnoreSingleArgument
32+
33+
When true, the check will ignore the single argument.
34+
3135
.. option:: CommentBoolLiterals
3236

3337
When true, the check will add argument comments in the format
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// RUN: %check_clang_tidy %s bugprone-argument-comment %t -- \
2+
// RUN: -config="{CheckOptions: [{key: bugprone-argument-comment.IgnoreSingleArgument, value: 1}, {key: CommentBoolLiterals, value: 1},{key: CommentIntegerLiterals, value: 1}, {key: CommentFloatLiterals, value: 1}, {key: CommentUserDefinedLiterals, value: 1}, {key: CommentStringLiterals, value: 1}, {key: CommentNullPtrs, value: 1}, {key: CommentCharacterLiterals, value: 1}]}" --
3+
4+
struct A {
5+
void foo(bool abc);
6+
void foo(bool abc, bool cde);
7+
void foo(const char *, bool abc);
8+
void foo(int iabc);
9+
void foo(float fabc);
10+
void foo(double dabc);
11+
void foo(const char *strabc);
12+
void fooW(const wchar_t *wstrabc);
13+
void fooPtr(A *ptrabc);
14+
void foo(char chabc);
15+
};
16+
17+
#define FOO 1
18+
19+
void g(int a);
20+
void h(double b);
21+
void i(const char *c);
22+
23+
double operator"" _km(long double);
24+
25+
void test() {
26+
A a;
27+
28+
a.foo(true);
29+
30+
a.foo(false);
31+
32+
a.foo(true, false);
33+
// CHECK-MESSAGES: [[@LINE-1]]:9: warning: argument comment missing for literal argument 'abc' [bugprone-argument-comment]
34+
// CHECK-MESSAGES: [[@LINE-2]]:15: warning: argument comment missing for literal argument 'cde' [bugprone-argument-comment]
35+
// CHECK-FIXES: a.foo(/*abc=*/true, /*cde=*/false);
36+
37+
a.foo(false, true);
38+
// CHECK-MESSAGES: [[@LINE-1]]:9: warning: argument comment missing for literal argument 'abc' [bugprone-argument-comment]
39+
// CHECK-MESSAGES: [[@LINE-2]]:16: warning: argument comment missing for literal argument 'cde' [bugprone-argument-comment]
40+
// CHECK-FIXES: a.foo(/*abc=*/false, /*cde=*/true);
41+
42+
a.foo(/*abc=*/false, true);
43+
// CHECK-MESSAGES: [[@LINE-1]]:24: warning: argument comment missing for literal argument 'cde' [bugprone-argument-comment]
44+
// CHECK-FIXES: a.foo(/*abc=*/false, /*cde=*/true);
45+
46+
a.foo(false, /*cde=*/true);
47+
// CHECK-MESSAGES: [[@LINE-1]]:9: warning: argument comment missing for literal argument 'abc' [bugprone-argument-comment]
48+
// CHECK-FIXES: a.foo(/*abc=*/false, /*cde=*/true);
49+
50+
bool val1 = true;
51+
bool val2 = false;
52+
a.foo(val1, val2);
53+
54+
a.foo("", true);
55+
// CHECK-MESSAGES: [[@LINE-1]]:13: warning: argument comment missing for literal argument 'abc' [bugprone-argument-comment]
56+
// CHECK-FIXES: a.foo("", /*abc=*/true);
57+
58+
a.foo(0);
59+
60+
a.foo(1.0f);
61+
62+
a.foo(1.0);
63+
64+
int val3 = 10;
65+
a.foo(val3);
66+
67+
float val4 = 10.0;
68+
a.foo(val4);
69+
70+
double val5 = 10.0;
71+
a.foo(val5);
72+
73+
a.foo("Hello World");
74+
75+
a.fooW(L"Hello World");
76+
77+
a.fooPtr(nullptr);
78+
79+
a.foo(402.0_km);
80+
81+
a.foo('A');
82+
83+
g(FOO);
84+
85+
h(1.0f);
86+
87+
i(__FILE__);
88+
89+
g((1));
90+
}
91+
92+
void f(bool _with_underscores_);
93+
void ignores_underscores() {
94+
f(false);
95+
96+
f(true);
97+
}

0 commit comments

Comments
 (0)