Home | History | Annotate | Download | only in SemaCXX
      1 // RUN: %clang_cc1 -fsyntax-only -verify -Wno-c++11-extensions %s
      2 //
      3 // WARNING: Do not add more typo correction test cases to this file lest you run
      4 // afoul the hard-coded limit (escape hatch) of 20 different typos whose
      5 // correction was attempted by Sema::CorrectTypo
      6 
      7 struct errc {
      8   int v_;
      9   operator int() const {return v_;}
     10 };
     11 
     12 class error_condition
     13 {
     14   int _val_;
     15 public:
     16   error_condition() : _val_(0) {}
     17 
     18   error_condition(int _val)
     19     : _val_(_val) {}
     20 
     21   template <class E>
     22   error_condition(E _e) {
     23     // make_error_condition must not be typo corrected to error_condition
     24     // even though the first declaration of make_error_condition has not
     25     // yet been encountered. This was a bug in the first version of the type
     26     // name typo correction patch that wasn't noticed until building LLVM with
     27     // Clang failed.
     28     *this = make_error_condition(_e);
     29   }
     30 
     31 };
     32 
     33 inline error_condition make_error_condition(errc _e) {
     34   return error_condition(static_cast<int>(_e));
     35 }
     36 
     37 
     38 // Prior to the introduction of a callback object to further filter possible
     39 // typo corrections, this example would not trigger a suggestion as "base_type"
     40 // is a closer match to "basetype" than is "BaseType" but "base_type" does not
     41 // refer to a base class or non-static data member.
     42 struct BaseType { };
     43 struct Derived : public BaseType { // expected-note {{base class 'BaseType' specified here}}
     44   static int base_type; // expected-note {{'base_type' declared here}}
     45   Derived() : basetype() {} // expected-error{{initializer 'basetype' does not name a non-static data member or base class; did you mean the base class 'BaseType'?}}
     46 };
     47 
     48 // Test the improvement from passing a callback object to CorrectTypo in
     49 // the helper function LookupMemberExprInRecord.
     50 int get_type(struct Derived *st) {
     51   return st->Base_Type; // expected-error{{no member named 'Base_Type' in 'Derived'; did you mean 'base_type'?}}
     52 }
     53 
     54 // In this example, somename should not be corrected to the cached correction
     55 // "some_name" since "some_name" is a class and a namespace name is needed.
     56 class some_name {}; // expected-note {{'some_name' declared here}}
     57 somename Foo; // expected-error {{unknown type name 'somename'; did you mean 'some_name'?}}
     58 namespace SomeName {} // expected-note {{namespace 'SomeName' defined here}}
     59 using namespace somename; // expected-error {{no namespace named 'somename'; did you mean 'SomeName'?}}
     60 
     61 
     62 // Without the callback object, CorrectTypo would choose "field1" as the
     63 // correction for "fielda" as it is closer than "FieldA", but that correction
     64 // would be later discarded by the caller and no suggestion would be given.
     65 struct st {
     66   struct {
     67     int field1;
     68   };
     69   double FieldA; // expected-note{{'FieldA' declared here}}
     70 };
     71 st var = { .fielda = 0.0 }; // expected-error{{field designator 'fielda' does not refer to any field in type 'st'; did you mean 'FieldA'?}}
     72 
     73 // Test the improvement from passing a callback object to CorrectTypo in
     74 // Sema::BuildCXXNestedNameSpecifier. And also for the improvement by doing
     75 // so in Sema::getTypeName.
     76 typedef char* another_str; // expected-note{{'another_str' declared here}}
     77 namespace AnotherStd { // expected-note{{'AnotherStd' declared here}}
     78   class string {};
     79 }
     80 another_std::string str; // expected-error{{use of undeclared identifier 'another_std'; did you mean 'AnotherStd'?}}
     81 another_str *cstr = new AnotherStr; // expected-error{{unknown type name 'AnotherStr'; did you mean 'another_str'?}}
     82 
     83 // Test the improvement from passing a callback object to CorrectTypo in
     84 // Sema::ActOnSizeofParameterPackExpr.
     85 char* TireNames;
     86 template<typename ...TypeNames> struct count { // expected-note{{parameter pack 'TypeNames' declared here}}
     87   static const unsigned value = sizeof...(TyreNames); // expected-error{{'TyreNames' does not refer to the name of a parameter pack; did you mean 'TypeNames'?}}
     88 };
     89 
     90 // Test the typo-correction callback in Sema::DiagnoseUnknownTypeName.
     91 namespace unknown_type_test {
     92   class StreamOut {}; // expected-note 2 {{'StreamOut' declared here}}
     93   long stream_count; // expected-note 2 {{'stream_count' declared here}}
     94 };
     95 unknown_type_test::stream_out out; // expected-error{{no type named 'stream_out' in namespace 'unknown_type_test'; did you mean 'StreamOut'?}}
     96 
     97 // Demonstrate a case where using only the cached value returns the wrong thing
     98 // when the cached value was the result of a previous callback object that only
     99 // accepts a subset of the current callback object.
    100 namespace {
    101 using namespace unknown_type_test;
    102 void bar(long i);
    103 void before_caching_classname() {
    104   bar((stream_out)); // expected-error{{use of undeclared identifier 'stream_out'; did you mean 'stream_count'?}}
    105 }
    106 stream_out out; // expected-error{{unknown type name 'stream_out'; did you mean 'StreamOut'?}}
    107 void after_caching_classname() {
    108   bar((stream_out)); // expected-error{{use of undeclared identifier 'stream_out'; did you mean 'stream_count'?}}
    109 }
    110 }
    111 
    112 // Test the typo-correction callback in Sema::DiagnoseInvalidRedeclaration.
    113 struct BaseDecl {
    114   void add_in(int i);
    115 };
    116 struct TestRedecl : public BaseDecl {
    117   void add_it(int i); // expected-note{{'add_it' declared here}}
    118 };
    119 void TestRedecl::add_in(int i) {} // expected-error{{out-of-line definition of 'add_in' does not match any declaration in 'TestRedecl'; did you mean 'add_it'?}}
    120 
    121 // Test the improved typo correction for the Parser::ParseCastExpr =>
    122 // Sema::ActOnIdExpression => Sema::DiagnoseEmptyLookup call path.
    123 class SomeNetMessage; // expected-note 2{{'SomeNetMessage'}}
    124 class Message {};
    125 void foo(Message&);
    126 void foo(SomeNetMessage&);
    127 void doit(void *data) {
    128   Message somenetmsg; // expected-note{{'somenetmsg' declared here}}
    129   foo(somenetmessage); // expected-error{{use of undeclared identifier 'somenetmessage'; did you mean 'somenetmsg'?}}
    130   foo((somenetmessage)data); // expected-error{{unknown type name 'somenetmessage'; did you mean 'SomeNetMessage'?}} expected-error{{incomplete type}}
    131 }
    132 
    133 // Test the typo-correction callback in BuildRecoveryCallExpr.
    134 // Solves the main issue in PR 9320 of suggesting corrections that take the
    135 // wrong number of arguments.
    136 void revoke(const char*); // expected-note 2{{'revoke' declared here}}
    137 void Test() {
    138   Invoke(); // expected-error{{use of undeclared identifier 'Invoke'}}
    139   Invoke("foo"); // expected-error{{use of undeclared identifier 'Invoke'; did you mean 'revoke'?}}
    140   Invoke("foo", "bar"); // expected-error{{use of undeclared identifier 'Invoke'}}
    141 }
    142 void Test2(void (*invoke)(const char *, int)) { // expected-note{{'invoke' declared here}}
    143   Invoke(); // expected-error{{use of undeclared identifier 'Invoke'}}
    144   Invoke("foo"); // expected-error{{use of undeclared identifier 'Invoke'; did you mean 'revoke'?}}
    145   Invoke("foo", 7); // expected-error{{use of undeclared identifier 'Invoke'; did you mean 'invoke'?}}
    146   Invoke("foo", 7, 22); // expected-error{{use of undeclared identifier 'Invoke'}}
    147 }
    148 
    149 void provoke(const char *x, bool y=false) {} // expected-note 2{{'provoke' declared here}}
    150 void Test3() {
    151   Provoke(); // expected-error{{use of undeclared identifier 'Provoke'}}
    152   Provoke("foo"); // expected-error{{use of undeclared identifier 'Provoke'; did you mean 'provoke'?}}
    153   Provoke("foo", true); // expected-error{{use of undeclared identifier 'Provoke'; did you mean 'provoke'?}}
    154   Provoke("foo", 7, 22); // expected-error{{use of undeclared identifier 'Provoke'}}
    155 }
    156 
    157 // PR 11737 - Don't try to typo-correct the implicit 'begin' and 'end' in a
    158 // C++11 for-range statement.
    159 struct R {};
    160 bool begun(R);
    161 void RangeTest() {
    162   for (auto b : R()) {} // expected-error {{invalid range expression of type 'R'}}
    163 }
    164 
    165 // PR 12019 - Avoid infinite mutual recursion in DiagnoseInvalidRedeclaration
    166 // by not trying to typo-correct a method redeclaration to declarations not
    167 // in the current record.
    168 class Parent {
    169  void set_types(int index, int value);
    170  void add_types(int value);
    171 };
    172 class Child: public Parent {};
    173 void Child::add_types(int value) {} // expected-error{{out-of-line definition of 'add_types' does not match any declaration in 'Child'}}
    174 
    175 // Fix the callback based filtering of typo corrections within
    176 // Sema::ActOnIdExpression by Parser::ParseCastExpression to allow type names as
    177 // potential corrections for template arguments.
    178 namespace clash {
    179 class ConstructExpr {}; // expected-note 2{{'clash::ConstructExpr' declared here}}
    180 }
    181 class ClashTool {
    182   bool HaveConstructExpr();
    183   template <class T> T* getExprAs();
    184 
    185   void test() {
    186     ConstructExpr *expr = // expected-error{{unknown type name 'ConstructExpr'; did you mean 'clash::ConstructExpr'?}}
    187         getExprAs<ConstructExpr>(); // expected-error{{unknown type name 'ConstructExpr'; did you mean 'clash::ConstructExpr'?}}
    188   }
    189 };
    190 
    191 namespace test1 {
    192   struct S {
    193     struct Foobar *f;  // expected-note{{'Foobar' declared here}}
    194   };
    195   test1::FooBar *b;  // expected-error{{no type named 'FooBar' in namespace 'test1'; did you mean 'Foobar'?}}
    196 }
    197 
    198 namespace ImplicitInt {
    199   void f(int, unsinged); // expected-error{{did you mean 'unsigned'}}
    200   struct S {
    201     unsinged : 4; // expected-error{{did you mean 'unsigned'}}
    202   };
    203 }
    204 
    205 namespace PR13051 {
    206   template<typename T> struct S {
    207     template<typename U> void f();
    208     operator bool() const;
    209   };
    210 
    211   void foo(); // expected-note{{'foo' declared here}}
    212   void g(void(*)());
    213   void g(bool(S<int>::*)() const);
    214 
    215   void test() {
    216     g(&S<int>::tempalte f<int>); // expected-error{{did you mean 'template'?}}
    217     g(&S<int>::opeartor bool); // expected-error{{did you mean 'operator'?}}
    218     g(&S<int>::foo); // expected-error{{no member named 'foo' in 'PR13051::S<int>'; did you mean simply 'foo'?}}
    219   }
    220 }
    221 
    222 inf f(doulbe); // expected-error{{'int'}} expected-error{{'double'}}
    223 
    224 namespace PR6325 {
    225 class foo { }; // expected-note{{'foo' declared here}}
    226 // Note that for this example (pulled from the PR), if keywords are not excluded
    227 // as correction candidates then no suggestion would be given; correcting
    228 // 'boo' to 'bool' is the same edit distance as correcting 'boo' to 'foo'.
    229 class bar : boo { }; // expected-error{{unknown class name 'boo'; did you mean 'foo'?}}
    230 }
    231 
    232 namespace outer {
    233   void somefunc();  // expected-note{{'::outer::somefunc' declared here}}
    234   void somefunc(int, int);  // expected-note{{'::outer::somefunc' declared here}}
    235 
    236   namespace inner {
    237     void somefunc(int) {
    238       someFunc();  // expected-error{{use of undeclared identifier 'someFunc'; did you mean '::outer::somefunc'?}}
    239       someFunc(1, 2);  // expected-error{{use of undeclared identifier 'someFunc'; did you mean '::outer::somefunc'?}}
    240     }
    241   }
    242 }
    243 
    244 namespace b6956809_test1 {
    245   struct A {};
    246   struct B {};
    247 
    248   struct S1 {
    249     void method(A*);  // no note here
    250     void method(B*);
    251   };
    252 
    253   void test1() {
    254     B b;
    255     S1 s;
    256     s.methodd(&b);  // expected-error{{no member named 'methodd' in 'b6956809_test1::S1'; did you mean 'method'}}
    257   }
    258 
    259   struct S2 {
    260     S2();
    261     void method(A*) const;  // expected-note{{candidate function not viable}}
    262    private:
    263     void method(B*);  // expected-note{{candidate function not viable}}
    264   };
    265 
    266   void test2() {
    267     B b;
    268     const S2 s;
    269     s.methodd(&b);  // expected-error{{no member named 'methodd' in 'b6956809_test1::S2'; did you mean 'method'}}  expected-error{{no matching member function for call to 'method'}}
    270   }
    271 }
    272 
    273 namespace b6956809_test2 {
    274   template<typename T> struct Err { typename T::error n; };  // expected-error{{type 'void *' cannot be used prior to '::' because it has no members}}
    275   struct S {
    276     template<typename T> typename Err<T>::type method(T);  // expected-note{{in instantiation of template class 'b6956809_test2::Err<void *>' requested here}}
    277     template<typename T> int method(T *);
    278   };
    279 
    280   void test() {
    281     S s;
    282     int k = s.methodd((void*)0);  // expected-error{{no member named 'methodd' in 'b6956809_test2::S'; did you mean 'method'?}} expected-note{{while substituting deduced template arguments into function template 'method' [with T = void *]}}
    283   }
    284 }
    285 
    286 // This test should have one correction, followed by an error without a
    287 // suggestion due to exceeding the maximum number of typos for which correction
    288 // is attempted.
    289 namespace CorrectTypo_has_reached_its_limit {
    290 int flibberdy();  // expected-note{{'flibberdy' declared here}}
    291 int no_correction() {
    292   return hibberdy() +  // expected-error{{use of undeclared identifier 'hibberdy'; did you mean 'flibberdy'?}}
    293          gibberdy();  // expected-error-re{{use of undeclared identifier 'gibberdy'{{$}}}}
    294 };
    295 }
    296