1 // RUN: %check_clang_tidy %s bugprone-argument-comment %t -- \
2 // RUN:   -config="{CheckOptions: [ \
3 // RUN:     {key: bugprone-argument-comment.CommentBoolLiterals, value: true}, \
4 // RUN:     {key: bugprone-argument-comment.CommentIntegerLiterals, value: true}, \
5 // RUN:     {key: bugprone-argument-comment.CommentFloatLiterals, value: true}, \
6 // RUN:     {key: bugprone-argument-comment.CommentUserDefinedLiterals, value: true}, \
7 // RUN:     {key: bugprone-argument-comment.CommentStringLiterals, value: true}, \
8 // RUN:     {key: bugprone-argument-comment.CommentNullPtrs, value: true}, \
9 // RUN:     {key: bugprone-argument-comment.CommentCharacterLiterals, value: true}]}" --
10 
11 struct A {
12   void foo(bool abc);
13   void foo(bool abc, bool cde);
14   void foo(const char *, bool abc);
15   void foo(int iabc);
16   void foo(float fabc);
17   void foo(double dabc);
18   void foo(const char *strabc);
19   void fooW(const wchar_t *wstrabc);
20   void fooPtr(A *ptrabc);
21   void foo(char chabc);
22 };
23 
24 #define FOO 1
25 #define X(x) (x)
26 
27 void g(int a);
28 void h(double b);
29 void i(const char *c);
30 void j(int a, int b, int c);
31 
32 double operator"" _km(long double);
33 
test()34 void test() {
35   A a;
36 
37   a.foo(true);
38   // CHECK-MESSAGES: [[@LINE-1]]:9: warning: argument comment missing for literal argument 'abc' [bugprone-argument-comment]
39   // CHECK-FIXES: a.foo(/*abc=*/true);
40 
41   a.foo(false);
42   // CHECK-MESSAGES: [[@LINE-1]]:9: warning: argument comment missing for literal argument 'abc' [bugprone-argument-comment]
43   // CHECK-FIXES: a.foo(/*abc=*/false);
44 
45   a.foo(true, false);
46   // CHECK-MESSAGES: [[@LINE-1]]:9: warning: argument comment missing for literal argument 'abc' [bugprone-argument-comment]
47   // CHECK-MESSAGES: [[@LINE-2]]:15: warning: argument comment missing for literal argument 'cde' [bugprone-argument-comment]
48   // CHECK-FIXES: a.foo(/*abc=*/true, /*cde=*/false);
49 
50   a.foo(false, true);
51   // CHECK-MESSAGES: [[@LINE-1]]:9: warning: argument comment missing for literal argument 'abc' [bugprone-argument-comment]
52   // CHECK-MESSAGES: [[@LINE-2]]:16: warning: argument comment missing for literal argument 'cde' [bugprone-argument-comment]
53   // CHECK-FIXES: a.foo(/*abc=*/false, /*cde=*/true);
54 
55   a.foo(/*abc=*/false, true);
56   // CHECK-MESSAGES: [[@LINE-1]]:24: warning: argument comment missing for literal argument 'cde' [bugprone-argument-comment]
57   // CHECK-FIXES: a.foo(/*abc=*/false, /*cde=*/true);
58 
59   a.foo(false, /*cde=*/true);
60   // CHECK-MESSAGES: [[@LINE-1]]:9: warning: argument comment missing for literal argument 'abc' [bugprone-argument-comment]
61   // CHECK-FIXES: a.foo(/*abc=*/false, /*cde=*/true);
62 
63   bool val1 = true;
64   bool val2 = false;
65   a.foo(val1, val2);
66 
67   a.foo("", true);
68   // CHECK-MESSAGES: [[@LINE-1]]:13: warning: argument comment missing for literal argument 'abc' [bugprone-argument-comment]
69   // CHECK-FIXES: a.foo("", /*abc=*/true);
70 
71   a.foo(0);
72   // CHECK-MESSAGES: [[@LINE-1]]:9: warning: argument comment missing for literal argument 'iabc' [bugprone-argument-comment]
73   // CHECK-FIXES: a.foo(/*iabc=*/0);
74 
75   a.foo(1.0f);
76   // CHECK-MESSAGES: [[@LINE-1]]:9: warning: argument comment missing for literal argument 'fabc' [bugprone-argument-comment]
77   // CHECK-FIXES: a.foo(/*fabc=*/1.0f);
78 
79   a.foo(-1.0f);
80   // CHECK-MESSAGES: [[@LINE-1]]:9: warning: argument comment missing for literal argument 'fabc' [bugprone-argument-comment]
81   // CHECK-FIXES: a.foo(/*fabc=*/-1.0f);
82 
83   a.foo(1.0);
84   // CHECK-MESSAGES: [[@LINE-1]]:9: warning: argument comment missing for literal argument 'dabc' [bugprone-argument-comment]
85   // CHECK-FIXES: a.foo(/*dabc=*/1.0);
86 
87   a.foo(-1.0);
88   // CHECK-MESSAGES: [[@LINE-1]]:9: warning: argument comment missing for literal argument 'dabc' [bugprone-argument-comment]
89   // CHECK-FIXES: a.foo(/*dabc=*/-1.0);
90 
91   int val3 = 10;
92   a.foo(val3);
93   a.foo(-val3);
94 
95   float val4 = 10.0;
96   a.foo(val4);
97   a.foo(-val4);
98 
99   double val5 = 10.0;
100   a.foo(val5);
101   a.foo(-val5);
102 
103   a.foo("Hello World");
104   // CHECK-MESSAGES: [[@LINE-1]]:9: warning: argument comment missing for literal argument 'strabc' [bugprone-argument-comment]
105   // CHECK-FIXES: a.foo(/*strabc=*/"Hello World");
106   //
107   a.fooW(L"Hello World");
108   // CHECK-MESSAGES: [[@LINE-1]]:10: warning: argument comment missing for literal argument 'wstrabc' [bugprone-argument-comment]
109   // CHECK-FIXES: a.fooW(/*wstrabc=*/L"Hello World");
110 
111   a.fooPtr(nullptr);
112   // CHECK-MESSAGES: [[@LINE-1]]:12: warning: argument comment missing for literal argument 'ptrabc' [bugprone-argument-comment]
113   // CHECK-FIXES: a.fooPtr(/*ptrabc=*/nullptr);
114 
115   a.foo(402.0_km);
116   // CHECK-MESSAGES: [[@LINE-1]]:9: warning: argument comment missing for literal argument 'dabc' [bugprone-argument-comment]
117   // CHECK-FIXES: a.foo(/*dabc=*/402.0_km);
118 
119   a.foo(-402.0_km);
120   // CHECK-MESSAGES: [[@LINE-1]]:9: warning: argument comment missing for literal argument 'dabc' [bugprone-argument-comment]
121   // CHECK-FIXES: a.foo(/*dabc=*/-402.0_km);
122 
123   a.foo('A');
124   // CHECK-MESSAGES: [[@LINE-1]]:9: warning: argument comment missing for literal argument 'chabc' [bugprone-argument-comment]
125   // CHECK-FIXES: a.foo(/*chabc=*/'A');
126 
127   g(FOO);
128   g(-FOO);
129   h(1.0f);
130   // CHECK-MESSAGES: [[@LINE-1]]:5: warning: argument comment missing for literal argument 'b' [bugprone-argument-comment]
131   // CHECK-FIXES: h(/*b=*/1.0f);
132   h(-1.0f);
133   // CHECK-MESSAGES: [[@LINE-1]]:5: warning: argument comment missing for literal argument 'b' [bugprone-argument-comment]
134   // CHECK-FIXES: h(/*b=*/-1.0f);
135   i(__FILE__);
136 
137   j(1, X(1), X(1));
138   // CHECK-MESSAGES: [[@LINE-1]]:5: warning: argument comment missing for literal argument 'a' [bugprone-argument-comment]
139   // CHECK-FIXES: j(/*a=*/1, X(1), X(1));
140   j(/*a=*/1, X(1), X(1));
141 
142   j(X(1), 1, X(1));
143   // CHECK-MESSAGES: [[@LINE-1]]:11: warning: argument comment missing for literal argument 'b' [bugprone-argument-comment]
144   // CHECK-FIXES: j(X(1), /*b=*/1, X(1));
145   j(X(1), /*b=*/1, X(1));
146 
147   j(X(1), X(1), 1);
148   // CHECK-MESSAGES: [[@LINE-1]]:17: warning: argument comment missing for literal argument 'c' [bugprone-argument-comment]
149   // CHECK-FIXES: j(X(1), X(1), /*c=*/1);
150   j(X(1), X(1), /*c=*/1);
151 
152   j(X(1), 1, 1);
153   // CHECK-MESSAGES: [[@LINE-1]]:11: warning: argument comment missing for literal argument 'b' [bugprone-argument-comment]
154   // CHECK-MESSAGES: [[@LINE-2]]:14: warning: argument comment missing for literal argument 'c' [bugprone-argument-comment]
155   // CHECK-FIXES: j(X(1), /*b=*/1, /*c=*/1);
156   j(X(1), /*b=*/1, /*c=*/1);
157 
158   j(1, X(1), 1);
159   // CHECK-MESSAGES: [[@LINE-1]]:5: warning: argument comment missing for literal argument 'a' [bugprone-argument-comment]
160   // CHECK-MESSAGES: [[@LINE-2]]:14: warning: argument comment missing for literal argument 'c' [bugprone-argument-comment]
161   // CHECK-FIXES: j(/*a=*/1, X(1), /*c=*/1);
162   j(/*a=*/1, X(1), /*c=*/1);
163 
164   j(1, 1, X(1));
165   // CHECK-MESSAGES: [[@LINE-1]]:5: warning: argument comment missing for literal argument 'a' [bugprone-argument-comment]
166   // CHECK-MESSAGES: [[@LINE-2]]:8: warning: argument comment missing for literal argument 'b' [bugprone-argument-comment]
167   // CHECK-FIXES: j(/*a=*/1, /*b=*/1, X(1));
168   j(/*a=*/1, /*b=*/1, X(1));
169 
170   // FIXME Would like the below to add argument comments.
171   g((1));
172   // FIXME But we should not add argument comments here.
173   g(_Generic(0, int : 0));
174 }
175 
176 void f(bool _with_underscores_);
ignores_underscores()177 void ignores_underscores() {
178   f(false);
179   // CHECK-MESSAGES: [[@LINE-1]]:5: warning: argument comment missing for literal argument '_with_underscores_' [bugprone-argument-comment]
180   // CHECK-FIXES: f(/*_with_underscores_=*/false);
181 
182   f(true);
183   // CHECK-MESSAGES: [[@LINE-1]]:5: warning: argument comment missing for literal argument
184   // CHECK-FIXES: f(/*_with_underscores_=*/true);
185 }
186