xref: /llvm-project/clang/test/FixIt/fixit-vexing-parse.cpp (revision ac63d63543ca824434236ffd788c46eec9339657)
1542f04ccSCharles Li // RUN: %clang_cc1 -verify -x c++ -std=c++98 %s
2542f04ccSCharles Li // RUN: not %clang_cc1 -fdiagnostics-parseable-fixits -x c++ -std=c++98 %s 2>&1 | FileCheck %s
38d06f424SRichard Smith 
48d06f424SRichard Smith struct S {
58d06f424SRichard Smith   int n;
68d06f424SRichard Smith };
78d06f424SRichard Smith 
88d06f424SRichard Smith struct T {
98d06f424SRichard Smith   T();
10943c4404SRichard Smith   T(S, S);
118d06f424SRichard Smith   int n;
128d06f424SRichard Smith };
138d06f424SRichard Smith 
148d06f424SRichard Smith struct U {
158d06f424SRichard Smith   ~U();
168d06f424SRichard Smith   int n;
178d06f424SRichard Smith };
188d06f424SRichard Smith 
198d06f424SRichard Smith struct V {
208d06f424SRichard Smith   ~V();
218d06f424SRichard Smith };
228d06f424SRichard Smith 
238d06f424SRichard Smith struct W : V {
248d06f424SRichard Smith };
258d06f424SRichard Smith 
268d06f424SRichard Smith struct X : U {
278d06f424SRichard Smith };
288d06f424SRichard Smith 
298d06f424SRichard Smith int F1();
308d06f424SRichard Smith S F2();
318d06f424SRichard Smith 
328d06f424SRichard Smith namespace N {
test()338d06f424SRichard Smith   void test() {
34943c4404SRichard Smith     // CHECK: fix-it:"{{.*}}":{35:9-35:11}:" = {}"
358d06f424SRichard Smith     S s1(); // expected-warning {{function declaration}} expected-note {{replace parentheses with an initializer}}
368d06f424SRichard Smith 
37943c4404SRichard Smith     // CHECK: fix-it:"{{.*}}":{39:9-39:10}:";"
38943c4404SRichard Smith     // CHECK: fix-it:"{{.*}}":{40:7-40:9}:" = {}"
398d06f424SRichard Smith     S s2, // expected-note {{change this ',' to a ';' to call 'F2'}}
408d06f424SRichard Smith     F2(); // expected-warning {{function declaration}} expected-note {{replace parentheses with an initializer}}
418d06f424SRichard Smith 
428d06f424SRichard Smith     // CHECK: fix-it:"{{.*}}":{44:9-44:11}:""
43943c4404SRichard Smith     // CHECK: fix-it:"{{.*}}":{45:9-45:11}:""
448d06f424SRichard Smith     T t1(), // expected-warning {{function declaration}} expected-note {{remove parentheses}}
458d06f424SRichard Smith       t2(); // expected-warning {{function declaration}} expected-note {{remove parentheses}}
468d06f424SRichard Smith 
47943c4404SRichard Smith     // Suggest parentheses only around the first argument.
48943c4404SRichard Smith     // CHECK: fix-it:"{{.*}}":{50:10-50:10}:"("
49943c4404SRichard Smith     // CHECK: fix-it:"{{.*}}":{50:13-50:13}:")"
50943c4404SRichard Smith     T t3(S(), S()); // expected-warning {{disambiguated as a function declaration}} expected-note {{add a pair of parentheses}}
51943c4404SRichard Smith 
52943c4404SRichard Smith     // Check fixit position for pathological case
53943c4404SRichard Smith     // CHECK: fix-it:"{{.*}}":{56:11-56:11}:"("
54943c4404SRichard Smith     // CHECK: fix-it:"{{.*}}":{56:20-56:20}:")"
55943c4404SRichard Smith     float k[1];
56943c4404SRichard Smith     int l(int(k[0])); // expected-warning {{disambiguated as a function declaration}} expected-note {{add a pair of parentheses}}
57943c4404SRichard Smith 
58943c4404SRichard Smith     // Don't emit warning and fixit because this must be a function declaration due to void return type.
59943c4404SRichard Smith     typedef void VO;
60943c4404SRichard Smith     VO m(int (*p)[4]);
61943c4404SRichard Smith 
62943c4404SRichard Smith     // Don't emit warning and fixit because direct initializer is not permitted here.
6303a4aa3dSRichard Smith     if (int n(int())){} // expected-error {{function type is not allowed here}}
64943c4404SRichard Smith 
65943c4404SRichard Smith     // CHECK: fix-it:"{{.*}}":{66:8-66:10}:" = {}"
668d06f424SRichard Smith     U u(); // expected-warning {{function declaration}} expected-note {{replace parentheses with an initializer}}
678d06f424SRichard Smith 
68943c4404SRichard Smith     // CHECK: fix-it:"{{.*}}":{69:8-69:10}:""
698d06f424SRichard Smith     V v(); // expected-warning {{function declaration}} expected-note {{remove parentheses}}
708d06f424SRichard Smith 
71943c4404SRichard Smith     // CHECK: fix-it:"{{.*}}":{72:8-72:10}:""
728d06f424SRichard Smith     W w(); // expected-warning {{function declaration}} expected-note {{remove parentheses}}
738d06f424SRichard Smith 
748d06f424SRichard Smith     // TODO: Removing the parens here would not initialize U::n.
758d06f424SRichard Smith     // Maybe suggest an " = X()" initializer for this case?
768d06f424SRichard Smith     // Maybe suggest removing the parens anyway?
778d06f424SRichard Smith     X x(); // expected-warning {{function declaration}}
788d06f424SRichard Smith 
79943c4404SRichard Smith     // CHECK: fix-it:"{{.*}}":{80:11-80:13}:" = 0"
808d06f424SRichard Smith     int n1(); // expected-warning {{function declaration}} expected-note {{replace parentheses with an initializer}}
818d06f424SRichard Smith 
82943c4404SRichard Smith     // CHECK: fix-it:"{{.*}}":{84:11-84:12}:";"
83943c4404SRichard Smith     // CHECK: fix-it:"{{.*}}":{85:7-85:9}:" = 0"
848d06f424SRichard Smith     int n2, // expected-note {{change this ',' to a ';' to call 'F1'}}
858d06f424SRichard Smith     F1(); // expected-warning {{function declaration}} expected-note {{replace parentheses with an initializer}}
868d06f424SRichard Smith 
87943c4404SRichard Smith     // CHECK: fix-it:"{{.*}}":{88:13-88:15}:" = 0.0"
888d06f424SRichard Smith     double d(); // expected-warning {{function declaration}} expected-note {{replace parentheses with an initializer}}
898d06f424SRichard Smith 
908d06f424SRichard Smith     typedef void *Ptr;
918d06f424SRichard Smith 
92943c4404SRichard Smith     // CHECK: fix-it:"{{.*}}":{93:10-93:12}:" = 0"
938d06f424SRichard Smith     Ptr p(); // expected-warning {{function declaration}} expected-note {{replace parentheses with an initializer}}
948d06f424SRichard Smith 
958d06f424SRichard Smith #define NULL 0
96943c4404SRichard Smith     // CHECK: fix-it:"{{.*}}":{97:10-97:12}:" = NULL"
978d06f424SRichard Smith     Ptr p(); // expected-warning {{function declaration}} expected-note {{replace parentheses with an initializer}}
98a90e86aaSDavid Blaikie 
99943c4404SRichard Smith     // CHECK: fix-it:"{{.*}}":{100:11-100:13}:" = false"
100a90e86aaSDavid Blaikie     bool b(); // expected-warning {{function declaration}} expected-note {{replace parentheses with an initializer}}
101a90e86aaSDavid Blaikie 
102943c4404SRichard Smith     // CHECK: fix-it:"{{.*}}":{103:11-103:13}:" = '\\0'"
103a90e86aaSDavid Blaikie     char c(); // expected-warning {{function declaration}} expected-note {{replace parentheses with an initializer}}
104a90e86aaSDavid Blaikie 
105943c4404SRichard Smith     // CHECK: fix-it:"{{.*}}":{106:15-106:17}:" = L'\\0'"
106a90e86aaSDavid Blaikie     wchar_t wc(); // expected-warning {{function declaration}} expected-note {{replace parentheses with an initializer}}
1078d06f424SRichard Smith   }
1088d06f424SRichard Smith }
109*ac63d635SRichard Smith 
110*ac63d635SRichard Smith namespace RedundantParens {
111*ac63d635SRichard Smith struct Y {
112*ac63d635SRichard Smith   Y();
113*ac63d635SRichard Smith   Y(int);
114*ac63d635SRichard Smith   ~Y();
115*ac63d635SRichard Smith };
116*ac63d635SRichard Smith int n;
117*ac63d635SRichard Smith 
test()118*ac63d635SRichard Smith void test() {
119*ac63d635SRichard Smith   // CHECK: add a variable name
120*ac63d635SRichard Smith   // CHECK: fix-it:"{{.*}}":{[[@LINE+7]]:4-[[@LINE+7]]:4}:" varname"
121*ac63d635SRichard Smith   // CHECK: add enclosing parentheses
122*ac63d635SRichard Smith   // CHECK: fix-it:"{{.*}}":{[[@LINE+5]]:3-[[@LINE+5]]:3}:"("
123*ac63d635SRichard Smith   // CHECK: fix-it:"{{.*}}":{[[@LINE+4]]:7-[[@LINE+4]]:7}:")"
124*ac63d635SRichard Smith   // CHECK: remove parentheses
125*ac63d635SRichard Smith   // CHECK: fix-it:"{{.*}}":{[[@LINE+2]]:4-[[@LINE+2]]:5}:" "
126*ac63d635SRichard Smith   // CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:6-[[@LINE+1]]:7}:""
127*ac63d635SRichard Smith   Y(n); // expected-warning {{declaration of variable named 'n'}} expected-note 3{{}}
128*ac63d635SRichard Smith }
129*ac63d635SRichard Smith }
130