Home | History | Annotate | Download | only in SemaCXX
      1 // RUN: %clang_cc1 -fsyntax-only -verify -Wno-c++11-extensions %s
      2 
      3 struct A {};
      4 struct B {};
      5 struct D {
      6   A fizbin;  // expected-note 2 {{declared here}}
      7   A foobar;  // expected-note 2 {{declared here}}
      8   B roxbin;  // expected-note 2 {{declared here}}
      9   B toobad;  // expected-note 2 {{declared here}}
     10   void BooHoo();
     11   void FoxBox();
     12 };
     13 
     14 void something(A, B);
     15 void test() {
     16   D obj;
     17   something(obj.fixbin,   // expected-error {{did you mean 'fizbin'?}}
     18             obj.toobat);  // expected-error {{did you mean 'toobad'?}}
     19   something(obj.toobat,   // expected-error {{did you mean 'foobar'?}}
     20             obj.fixbin);  // expected-error {{did you mean 'roxbin'?}}
     21   something(obj.fixbin,   // expected-error {{did you mean 'fizbin'?}}
     22             obj.fixbin);  // expected-error {{did you mean 'roxbin'?}}
     23   something(obj.toobat,   // expected-error {{did you mean 'foobar'?}}
     24             obj.toobat);  // expected-error {{did you mean 'toobad'?}}
     25   // Both members could be corrected to methods, but that isn't valid.
     26   something(obj.boohoo,   // expected-error-re {{no member named 'boohoo' in 'D'{{$}}}}
     27             obj.foxbox);  // expected-error-re {{no member named 'foxbox' in 'D'{{$}}}}
     28   // The first argument has a usable correction but the second doesn't.
     29   something(obj.boobar,   // expected-error-re {{no member named 'boobar' in 'D'{{$}}}}
     30             obj.foxbox);  // expected-error-re {{no member named 'foxbox' in 'D'{{$}}}}
     31 }
     32 
     33 // Ensure the delayed typo correction does the right thing when trying to
     34 // recover using a seemingly-valid correction for which a valid expression to
     35 // replace the TypoExpr cannot be created (but which does have a second
     36 // correction candidate that would be a valid and usable correction).
     37 class Foo {
     38 public:
     39   template <> void testIt();  // expected-error {{no function template matches}}
     40   void textIt();  // expected-note {{'textIt' declared here}}
     41 };
     42 void testMemberExpr(Foo *f) {
     43   f->TestIt();  // expected-error {{no member named 'TestIt' in 'Foo'; did you mean 'textIt'?}}
     44 }
     45 
     46 void callee(double, double);
     47 void testNoCandidates() {
     48   callee(xxxxxx,   // expected-error-re {{use of undeclared identifier 'xxxxxx'{{$}}}}
     49          zzzzzz);  // expected-error-re {{use of undeclared identifier 'zzzzzz'{{$}}}}
     50 }
     51 
     52 class string {};
     53 struct Item {
     54   void Nest();
     55   string text();
     56   Item* next();  // expected-note {{'next' declared here}}
     57 };
     58 void testExprFilter(Item *i) {
     59   Item *j;
     60   j = i->Next();  // expected-error {{no member named 'Next' in 'Item'; did you mean 'next'?}}
     61 }
     62 
     63 // Test that initializer expressions are handled correctly and that the type
     64 // being initialized is taken into account when choosing a correction.
     65 namespace initializerCorrections {
     66 struct Node {
     67   string text() const;
     68   // Node* Next() is not implemented yet
     69 };
     70 void f(Node *node) {
     71   // text is only an edit distance of 1 from Next, but would trigger type
     72   // conversion errors if used in this initialization expression.
     73   Node *next = node->Next();  // expected-error-re {{no member named 'Next' in 'initializerCorrections::Node'{{$}}}}
     74 }
     75 
     76 struct LinkedNode {
     77   LinkedNode* next();  // expected-note {{'next' declared here}}
     78   string text() const;
     79 };
     80 void f(LinkedNode *node) {
     81   // text and next are equidistant from Next, but only one results in a valid
     82   // initialization expression.
     83   LinkedNode *next = node->Next();  // expected-error {{no member named 'Next' in 'initializerCorrections::LinkedNode'; did you mean 'next'?}}
     84 }
     85 
     86 struct NestedNode {
     87   NestedNode* Nest();
     88   NestedNode* next();
     89   string text() const;
     90 };
     91 void f(NestedNode *node) {
     92   // There are two equidistant, usable corrections for Next: next and Nest
     93   NestedNode *next = node->Next();  // expected-error-re {{no member named 'Next' in 'initializerCorrections::NestedNode'{{$}}}}
     94 }
     95 }
     96 
     97 namespace PR21669 {
     98 void f(int *i) {
     99   // Check that arguments to a builtin with custom type checking are corrected
    100   // properly, since calls to such builtins bypass much of the normal code path
    101   // for building and checking the call.
    102   __atomic_load(i, i, something_something);  // expected-error-re {{use of undeclared identifier 'something_something'{{$}}}}
    103 }
    104 }
    105 
    106 const int DefaultArg = 9;  // expected-note {{'DefaultArg' declared here}}
    107 template <int I = defaultArg> struct S {};  // expected-error {{use of undeclared identifier 'defaultArg'; did you mean 'DefaultArg'?}}
    108 S<1> s;
    109 
    110 namespace foo {}
    111 void test_paren_suffix() {
    112   foo::bar({5, 6});  // expected-error-re {{no member named 'bar' in namespace 'foo'{{$}}}} \
    113                      // expected-error {{expected expression}}
    114 }
    115 
    116 const int kNum = 10;  // expected-note {{'kNum' declared here}}
    117 class SomeClass {
    118   int Kind;
    119 public:
    120   explicit SomeClass() : Kind(kSum) {}  // expected-error {{use of undeclared identifier 'kSum'; did you mean 'kNum'?}}
    121 };
    122 
    123 // There used to be an issue with typo resolution inside overloads.
    124 struct AssertionResult { ~AssertionResult(); };
    125 AssertionResult Overload(const char *a);
    126 AssertionResult Overload(int a);
    127 void UseOverload() {
    128   // expected-note@+1 {{'result' declared here}}
    129   const char *result;
    130   // expected-error@+1 {{use of undeclared identifier 'resulta'; did you mean 'result'?}}
    131   Overload(resulta);
    132 }
    133 
    134 namespace PR21925 {
    135 struct X {
    136   int get() { return 7; }  // expected-note {{'get' declared here}}
    137 };
    138 void test() {
    139   X variable;  // expected-note {{'variable' declared here}}
    140 
    141   // expected-error@+2 {{use of undeclared identifier 'variableX'; did you mean 'variable'?}}
    142   // expected-error@+1 {{no member named 'getX' in 'PR21925::X'; did you mean 'get'?}}
    143   int x = variableX.getX();
    144 }
    145 }
    146 
    147 namespace PR21905 {
    148 int (*a) () = (void)Z;  // expected-error-re {{use of undeclared identifier 'Z'{{$}}}}
    149 }
    150 
    151 namespace PR21947 {
    152 int blue;  // expected-note {{'blue' declared here}}
    153 __typeof blur y;  // expected-error {{use of undeclared identifier 'blur'; did you mean 'blue'?}}
    154 }
    155 
    156 namespace PR22092 {
    157 a = b ? : 0;  // expected-error {{C++ requires a type specifier for all declarations}} \
    158               // expected-error-re {{use of undeclared identifier 'b'{{$}}}}
    159 }
    160 
    161 extern long clock (void);
    162 struct Pointer {
    163   void set_xpos(int);
    164   void set_ypos(int);
    165 };
    166 void MovePointer(Pointer &Click, int x, int y) {  // expected-note 2 {{'Click' declared here}}
    167   click.set_xpos(x);  // expected-error {{use of undeclared identifier 'click'; did you mean 'Click'?}}
    168   click.set_ypos(x);  // expected-error {{use of undeclared identifier 'click'; did you mean 'Click'?}}
    169 }
    170 
    171 namespace PR22250 {
    172 // expected-error@+4 {{use of undeclared identifier 'size_t'; did you mean 'sizeof'?}}
    173 // expected-error-re@+3 {{use of undeclared identifier 'y'{{$}}}}
    174 // expected-error-re@+2 {{use of undeclared identifier 'z'{{$}}}}
    175 // expected-error@+1 {{expected ';' after top level declarator}}
    176 int getenv_s(size_t *y, char(&z)) {}
    177 }
    178 
    179 namespace PR22291 {
    180 template <unsigned I> void f() {
    181   unsigned *prio_bits_array;  // expected-note {{'prio_bits_array' declared here}}
    182   // expected-error@+1 {{use of undeclared identifier 'prio_op_array'; did you mean 'prio_bits_array'?}}
    183   __atomic_store_n(prio_op_array + I, false, __ATOMIC_RELAXED);
    184 }
    185 }
    186 
    187 namespace PR22297 {
    188 double pow(double x, double y);
    189 struct TimeTicks {
    190   static void Now();  // expected-note {{'Now' declared here}}
    191 };
    192 void f() {
    193   TimeTicks::now();  // expected-error {{no member named 'now' in 'PR22297::TimeTicks'; did you mean 'Now'?}}
    194 }
    195 }
    196 
    197 namespace PR23005 {
    198 void f() { int a = Unknown::b(c); }  // expected-error {{use of undeclared identifier 'Unknown'}}
    199 // expected-error@-1 {{use of undeclared identifier 'c'}}
    200 }
    201