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 PR12951 {
    206 // If there are two corrections that have the same identifier and edit distance
    207 // and only differ by their namespaces, don't suggest either as a correction
    208 // since both are equally likely corrections.
    209 namespace foobar { struct Thing {}; }
    210 namespace bazquux { struct Thing {}; }
    211 void f() { Thing t; } // expected-error{{unknown type name 'Thing'}}
    212 }
    213 
    214 namespace PR13051 {
    215   template<typename T> struct S {
    216     template<typename U> void f();
    217     operator bool() const;
    218   };
    219 
    220   void f() {
    221     f(&S<int>::tempalte f<int>); // expected-error{{did you mean 'template'?}}
    222     f(&S<int>::opeartor bool); // expected-error{{did you mean 'operator'?}}
    223     f(&S<int>::foo); // expected-error-re{{no member named 'foo' in 'PR13051::S<int>'$}}
    224   }
    225 }
    226 
    227 inf f(doulbe); // expected-error{{'int'}} expected-error{{'double'}}
    228 
    229 namespace PR6325 {
    230 class foo { }; // expected-note{{'foo' declared here}}
    231 // Note that for this example (pulled from the PR), if keywords are not excluded
    232 // as correction candidates then no suggestion would be given; correcting
    233 // 'boo' to 'bool' is the same edit distance as correcting 'boo' to 'foo'.
    234 class bar : boo { }; // expected-error{{unknown class name 'boo'; did you mean 'foo'?}}
    235 }
    236 
    237 namespace outer {
    238   void somefunc();  // expected-note{{'::outer::somefunc' declared here}}
    239   void somefunc(int, int);  // expected-note{{'::outer::somefunc' declared here}}
    240 
    241   namespace inner {
    242     void somefunc(int) {
    243       someFunc();  // expected-error{{use of undeclared identifier 'someFunc'; did you mean '::outer::somefunc'?}}
    244       someFunc(1, 2);  // expected-error{{use of undeclared identifier 'someFunc'; did you mean '::outer::somefunc'?}}
    245     }
    246   }
    247 }
    248 
    249 namespace bogus_keyword_suggestion {
    250 void test() {
    251    status = "OK"; // expected-error-re{{use of undeclared identifier 'status'$}}
    252    return status; // expected-error-re{{use of undeclared identifier 'status'$}}
    253  }
    254 }
    255 
    256 namespace PR13387 {
    257 struct A {
    258   void CreateFoo(float, float);
    259   void CreateBar(float, float);
    260 };
    261 struct B : A {
    262   using A::CreateFoo;
    263   void CreateFoo(int, int);
    264 };
    265 void f(B &x) {
    266   x.Createfoo(0,0); // expected-error {{no member named 'Createfoo' in 'PR13387::B'; did you mean 'CreateFoo'?}}
    267 }
    268 }
    269 
    270 struct DataStruct {void foo();};
    271 struct T {
    272  DataStruct data_struct;
    273  void f();
    274 };
    275 // should be void T::f();
    276 void f() {
    277  data_struct->foo(); // expected-error-re{{use of undeclared identifier 'data_struct'$}}
    278 }
    279 
    280 namespace b6956809_test1 {
    281   struct A {};
    282   struct B {};
    283 
    284   struct S1 {
    285     void method(A*);  // no note here
    286     void method(B*);
    287   };
    288 
    289   void test1() {
    290     B b;
    291     S1 s;
    292     s.methodd(&b);  // expected-error{{no member named 'methodd' in 'b6956809_test1::S1'; did you mean 'method'}}
    293   }
    294 
    295   struct S2 {
    296     S2();
    297     void method(A*) const;  // expected-note{{candidate function not viable}}
    298    private:
    299     void method(B*);  // expected-note{{candidate function not viable}}
    300   };
    301 
    302   void test2() {
    303     B b;
    304     const S2 s;
    305     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'}}
    306   }
    307 }
    308 
    309 namespace b6956809_test2 {
    310   template<typename T> struct Err { typename T::error n; };  // expected-error{{type 'void *' cannot be used prior to '::' because it has no members}}
    311   struct S {
    312     template<typename T> typename Err<T>::type method(T);  // expected-note{{in instantiation of template class 'b6956809_test2::Err<void *>' requested here}}  expected-note{{while substituting deduced template arguments into function template 'method' [with T = void *]}}
    313     template<typename T> int method(T *);
    314   };
    315 
    316   void test() {
    317     S s;
    318     int k = s.methodd((void*)0);  // expected-error{{no member named 'methodd' in 'b6956809_test2::S'; did you mean 'method'?}}
    319   }
    320 }
    321 
    322 namespace CorrectTypo_has_reached_its_limit {
    323 int flibberdy();  // no note here
    324 int no_correction() {
    325   return gibberdy();  // expected-error-re{{use of undeclared identifier 'gibberdy'$}}
    326 };
    327 }
    328