diff options
author | Richard Trieu <rtrieu@google.com> | 2014-06-24 23:14:24 +0000 |
---|---|---|
committer | Richard Trieu <rtrieu@google.com> | 2014-06-24 23:14:24 +0000 |
commit | 3e11835412bb95edf3526dc16082ff61586100a5 (patch) | |
tree | 7a00fa5091c31442407561c18a65ebb5b9ea78aa /test | |
parent | 93d4222c228925c5c97d0e0190d7e614db1778f3 (diff) | |
download | clang-3e11835412bb95edf3526dc16082ff61586100a5.tar.gz clang-3e11835412bb95edf3526dc16082ff61586100a5.tar.bz2 clang-3e11835412bb95edf3526dc16082ff61586100a5.tar.xz |
Provide a better diagnostic when braces are put before the identifier.
When a user types:
int [4] foo;
assume that the user means:
int foo[4];
Update the information for 'foo' to prevent additional errors, and provide
a fix-it hint to move the brackets to the correct location.
Additionally, suggest parens for types that require it, such as:
int [4] *foo;
to:
int (*foo)[4];
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@211641 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test')
-rw-r--r-- | test/Parser/brackets.c | 79 | ||||
-rw-r--r-- | test/Parser/brackets.cpp | 153 |
2 files changed, 232 insertions, 0 deletions
diff --git a/test/Parser/brackets.c b/test/Parser/brackets.c new file mode 100644 index 0000000000..2750d0e42e --- /dev/null +++ b/test/Parser/brackets.c @@ -0,0 +1,79 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s +// RUN: cp %s %t +// RUN: not %clang_cc1 -fixit %t -x c -DFIXIT +// RUN: %clang_cc1 -fsyntax-only %t -x c -DFIXIT +// RUN: not %clang_cc1 -fsyntax-only -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s -strict-whitespace + +void test1() { + int a[] = {0,1,1,2,3}; + int []b = {0,1,4,9,16}; + // expected-error@-1{{brackets go after the identifier}} + // CHECK: {{^}} int []b = {0,1,4,9,16}; + // CHECK: {{^}} ~~ ^ + // CHECK: {{^}} [] + // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:7-[[@LINE-5]]:9}:"" + // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:10-[[@LINE-6]]:10}:"[]" + + int c = a[0]; + int d = b[0]; // No undeclared identifer error here. + + int *e = a; + int *f = b; // No undeclared identifer error here. +} + +struct S { + int [1][1]x; + // expected-error@-1{{brackets go after the identifier}} + // CHECK: {{^}} int [1][1]x; + // CHECK: {{^}} ~~~~~~ ^ + // CHECK: {{^}} [1][1] + // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:7-[[@LINE-5]]:13}:"" + // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:14-[[@LINE-6]]:14}:"[1][1]" +} s; + +#ifndef FIXIT +void test2() { + int [][][]; + // expected-error@-1{{expected identifier or '('}} + // CHECK: {{^}} int [][][]; + // CHECK: {{^}} ^ + // CHECK-NOT: fix-it + struct T { + int []; + // expected-error@-1{{expected member name or ';' after declaration specifiers}} + // CHECK: {{^}} int []; + // CHECK: {{^}} ~~~ ^ + // CHECK-NOT: fix-it + }; +} + +void test3() { + int [5] *; + // expected-error@-1{{expected identifier or '('}} + // CHECK: {{^}} int [5] *; + // CHECK: {{^}} ^ + // CHECK-NOT: fix-it + // expected-error@-5{{brackets go after the identifier}} + // CHECK: {{^}} int [5] *; + // CHECK: {{^}} ~~~~ ^ + // CHECK: {{^}} ()[5] + // CHECK: fix-it:{{.*}}:{[[@LINE-9]]:7-[[@LINE-9]]:11}:"" + // CHECK: fix-it:{{.*}}:{[[@LINE-10]]:11-[[@LINE-10]]:11}:"(" + // CHECK: fix-it:{{.*}}:{[[@LINE-11]]:12-[[@LINE-11]]:12}:")[5]" + + int [5] * a; + // expected-error@-1{{brackets go after the identifier}} + // CHECK: {{^}} int [5] * a; + // CHECK: {{^}} ~~~~ ^ + // CHECK: {{^}} ( )[5] + // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:7-[[@LINE-5]]:11}:"" + // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:11-[[@LINE-6]]:11}:"(" + // CHECK: fix-it:{{.*}}:{[[@LINE-7]]:14-[[@LINE-7]]:14}:")[5]" + + int *b[5] = a; // expected-error{{}} a should not be corrected to type b + + int (*c)[5] = a; // a should be the same type as c +} +#endif + +// CHECK: 8 errors generated. diff --git a/test/Parser/brackets.cpp b/test/Parser/brackets.cpp new file mode 100644 index 0000000000..f418c11d3a --- /dev/null +++ b/test/Parser/brackets.cpp @@ -0,0 +1,153 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s +// RUN: cp %s %t +// RUN: not %clang_cc1 -fixit %t -x c++ -DFIXIT +// RUN: %clang_cc1 -fsyntax-only %t -x c++ -DFIXIT +// RUN: not %clang_cc1 -fsyntax-only -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s -strict-whitespace + +void test1() { + int a[] = {0,1,1,2,3}; + int []b = {0,1,4,9,16}; + // expected-error@-1{{brackets go after the unqualified-id}} + // CHECK: {{^}} int []b = {0,1,4,9,16}; + // CHECK: {{^}} ~~ ^ + // CHECK: {{^}} [] + // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:7-[[@LINE-5]]:9}:"" + // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:10-[[@LINE-6]]:10}:"[]" + + int c = a[0]; + int d = b[0]; // No undeclared identifer error here. + + int *e = a; + int *f = b; // No undeclared identifer error here. + + int[1] g[2]; + // expected-error@-1{{brackets go after the unqualified-id}} + // CHECK: {{^}} int[1] g[2]; + // CHECK: {{^}} ~~~ ^ + // CHECK: {{^}} [1] + // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:6-[[@LINE-5]]:9}:"" + // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:14-[[@LINE-6]]:14}:"[1]" +} + +void test2() { + int [3] (*a) = 0; + // expected-error@-1{{brackets go after the unqualified-id}} + // CHECK: {{^}} int [3] (*a) = 0; + // CHECK: {{^}} ~~~~ ^ + // CHECK: {{^}} [3] + // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:7-[[@LINE-5]]:11}:"" + // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:15-[[@LINE-6]]:15}:"[3]" + +#ifndef FIXIT + // Make sure a is corrected to be like type y, instead of like type z. + int (*b)[3] = a; + int (*c[3]) = a; // expected-error{{}} +#endif +} + +struct A { + static int [1][1]x; + // expected-error@-1{{brackets go after the unqualified-id}} + // CHECK: {{^}} static int [1][1]x; + // CHECK: {{^}} ~~~~~~ ^ + // CHECK: {{^}} [1][1] + // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:14-[[@LINE-5]]:20}:"" + // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:21-[[@LINE-6]]:21}:"[1][1]" +}; + +int [1][1]A::x = { {42} }; +// expected-error@-1{{brackets go after the unqualified-id}} +// CHECK: {{^}}int [1][1]A::x = { {42} }; +// CHECK: {{^}} ~~~~~~ ^ +// CHECK: {{^}} [1][1] +// CHECK: fix-it:{{.*}}:{[[@LINE-5]]:5-[[@LINE-5]]:11}:"" +// CHECK: fix-it:{{.*}}:{[[@LINE-6]]:15-[[@LINE-6]]:15}:"[1][1]" + +struct B { static int (*x)[5]; }; +int [5] *B::x = 0; +// expected-error@-1{{brackets go after the unqualified-id}} +// CHECK: {{^}}int [5] *B::x = 0; +// CHECK: {{^}} ~~~~ ^ +// CHECK: {{^}} ( )[5] +// CHECK: fix-it:{{.*}}:{[[@LINE-5]]:5-[[@LINE-5]]:9}:"" +// CHECK: fix-it:{{.*}}:{[[@LINE-6]]:9-[[@LINE-6]]:9}:"(" +// CHECK: fix-it:{{.*}}:{[[@LINE-7]]:14-[[@LINE-7]]:14}:")[5]" + +void test3() { + int [3] *a; + // expected-error@-1{{brackets go after the unqualified-id}} + // CHECK: {{^}} int [3] *a; + // CHECK: {{^}} ~~~~ ^ + // CHECK: {{^}} ( )[3] + // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:7-[[@LINE-5]]:11}:"" + // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:11-[[@LINE-6]]:11}:"(" + // CHECK: fix-it:{{.*}}:{[[@LINE-7]]:13-[[@LINE-7]]:13}:")[3]" + + int (*b)[3] = a; // no error +} + +void test4() { + int [2] a; + // expected-error@-1{{brackets go after the unqualified-id}} + // CHECK: {{^}} int [2] a; + // CHECK: {{^}} ~~~~ ^ + // CHECK: {{^}} [2] + // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:7-[[@LINE-5]]:11}:"" + // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:12-[[@LINE-6]]:12}:"[2]" + + int [2] &b = a; + // expected-error@-1{{brackets go after the unqualified-id}} + // CHECK: {{^}} int [2] &b = a; + // CHECK: {{^}} ~~~~ ^ + // CHECK: {{^}} ( )[2] + // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:7-[[@LINE-5]]:11}:"" + // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:11-[[@LINE-6]]:11}:"(" + // CHECK: fix-it:{{.*}}:{[[@LINE-7]]:13-[[@LINE-7]]:13}:")[2]" + +} + +namespace test5 { +#ifndef FIXIT +int [][][]; +// expected-error@-1{{expected unqualified-id}} +// CHECK: {{^}}int [][][]; +// CHECK: {{^}} ^ + +struct C { + int []; + // expected-error@-1{{expected member name or ';' after declaration specifiers}} + // CHECK: {{^}} int []; + // CHECK: {{^}} ~~~ ^ +}; + +#endif +} + +namespace test6 { +struct A { + static int arr[3]; +}; +int [3] ::test6::A::arr = {1,2,3}; +// expected-error@-1{{brackets go after the unqualified-id}} +// CHECK: {{^}}int [3] ::test6::A::arr = {1,2,3}; +// CHECK: {{^}} ~~~~ ^ +// CHECK: {{^}} [3] +// CHECK: fix-it:{{.*}}:{[[@LINE-5]]:5-[[@LINE-5]]:9}:"" +// CHECK: fix-it:{{.*}}:{[[@LINE-6]]:24-[[@LINE-6]]:24}:"[3]" + +} + +namespace test7 { +class A{}; +void test() { + int [3] A::*a; + // expected-error@-1{{brackets go after the unqualified-id}} + // CHECK: {{^}} int [3] A::*a; + // CHECK: {{^}} ~~~~ ^ + // CHECK: {{^}} ( )[3] + // CHECK: fix-it:{{.*}}:{[[@LINE-5]]:7-[[@LINE-5]]:11}:"" + // CHECK: fix-it:{{.*}}:{[[@LINE-6]]:11-[[@LINE-6]]:11}:"(" + // CHECK: fix-it:{{.*}}:{[[@LINE-7]]:16-[[@LINE-7]]:16}:")[3]" +} +} +// CHECK: 14 errors generated. |