[cfe-commits] r158109 - in /cfe/trunk: lib/Sema/SemaLookup.cpp test/CXX/temp/temp.spec/temp.expl.spec/p3.cpp test/Parser/cxx-using-directive.cpp test/SemaCXX/elaborated-type-specifier.cpp test/SemaCXX/nested-name-spec.cpp test/SemaCXX/qualified-id-lookup.cpp

Kaelyn Uhrain rikka at google.com
Wed Jun 6 13:54:52 PDT 2012


Author: rikka
Date: Wed Jun  6 15:54:51 2012
New Revision: 158109

URL: http://llvm.org/viewvc/llvm-project?rev=158109&view=rev
Log:
Allow CorrectTypo to add/modify nested name qualifiers to typos that
are otherwise too short to try to correct.

The TODOs added to two of the tests are for existing deficiencies in the
typo correction code that could be exposed by using longer identifiers.

Modified:
    cfe/trunk/lib/Sema/SemaLookup.cpp
    cfe/trunk/test/CXX/temp/temp.spec/temp.expl.spec/p3.cpp
    cfe/trunk/test/Parser/cxx-using-directive.cpp
    cfe/trunk/test/SemaCXX/elaborated-type-specifier.cpp
    cfe/trunk/test/SemaCXX/nested-name-spec.cpp
    cfe/trunk/test/SemaCXX/qualified-id-lookup.cpp

Modified: cfe/trunk/lib/Sema/SemaLookup.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaLookup.cpp?rev=158109&r1=158108&r2=158109&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaLookup.cpp (original)
+++ cfe/trunk/lib/Sema/SemaLookup.cpp Wed Jun  6 15:54:51 2012
@@ -3795,6 +3795,9 @@
   bool SearchNamespaces
     = getLangOpts().CPlusPlus &&
       (IsUnqualifiedLookup || (QualifiedDC && QualifiedDC->isNamespace()));
+  // In a few cases we *only* want to search for corrections bases on just
+  // adding or changing the nested name specifier.
+  bool AllowOnlyNNSChanges = Typo->getName().size() < 3;
   
   if (IsUnqualifiedLookup || SearchNamespaces) {
     // For unqualified lookup, look through all of the names that we have
@@ -3831,8 +3834,8 @@
     return TypoCorrection();
   }
 
-  // Make sure that the user typed at least 3 characters for each correction
-  // made. Otherwise, we don't even both looking at the results.
+  // Make sure the best edit distance (prior to adding any namespace qualifiers)
+  // is not more that about a third of the length of the typo's identifier.
   unsigned ED = Consumer.getBestEditDistance(true);
   if (ED > 0 && Typo->getName().size() / ED < 3) {
     // If this was an unqualified lookup, note that no correction was found.
@@ -3872,6 +3875,16 @@
     for (TypoCorrectionConsumer::result_iterator I = DI->second.begin(),
                                               IEnd = DI->second.end();
          I != IEnd; /* Increment in loop. */) {
+      // If we only want nested name specifier corrections, ignore potential
+      // corrections that have a different base identifier from the typo.
+      if (AllowOnlyNNSChanges &&
+          I->second.front().getCorrectionAsIdentifierInfo() != Typo) {
+        TypoCorrectionConsumer::result_iterator Prev = I;
+        ++I;
+        DI->second.erase(Prev);
+        continue;
+      }
+
       // If the item already has been looked up or is a keyword, keep it.
       // If a validator callback object was given, drop the correction
       // unless it passes validation.
@@ -4013,7 +4026,7 @@
   TypoResultsMap &BestResults = Consumer.getBestResults();
   ED = Consumer.getBestEditDistance(true);
 
-  if (ED > 0 && Typo->getName().size() / ED < 3) {
+  if (!AllowOnlyNNSChanges && ED > 0 && Typo->getName().size() / ED < 3) {
     // If this was an unqualified lookup and we believe the callback
     // object wouldn't have filtered out possible corrections, note
     // that no correction was found.

Modified: cfe/trunk/test/CXX/temp/temp.spec/temp.expl.spec/p3.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/temp/temp.spec/temp.expl.spec/p3.cpp?rev=158109&r1=158108&r2=158109&view=diff
==============================================================================
--- cfe/trunk/test/CXX/temp/temp.spec/temp.expl.spec/p3.cpp (original)
+++ cfe/trunk/test/CXX/temp/temp.spec/temp.expl.spec/p3.cpp Wed Jun  6 15:54:51 2012
@@ -1,10 +1,14 @@
 // RUN: %clang_cc1 -fsyntax-only -verify %s
 
 namespace N {
-  template<class T> class X;
+  template<class T> class X; // expected-note {{'N::X' declared here}} \
+                             // expected-note {{explicitly specialized declaration is here}}
 }
 
-template<> class X<int> { /* ... */ };	// expected-error {{non-template class 'X'}}
+// TODO: Don't add a namespace qualifier to the template if it would trigger
+// the warning about the specialization being outside of the namespace.
+template<> class X<int> { /* ... */ };	// expected-error {{no template named 'X'; did you mean 'N::X'?}} \
+                                        // expected-warning {{first declaration of class template specialization of 'X' outside namespace 'N' is a C++11 extension}}
 
 namespace N {
   

Modified: cfe/trunk/test/Parser/cxx-using-directive.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Parser/cxx-using-directive.cpp?rev=158109&r1=158108&r2=158109&view=diff
==============================================================================
--- cfe/trunk/test/Parser/cxx-using-directive.cpp (original)
+++ cfe/trunk/test/Parser/cxx-using-directive.cpp Wed Jun  6 15:54:51 2012
@@ -3,7 +3,8 @@
 class A {};
 
 namespace B {
-  namespace A {} // expected-note{{namespace '::B::A' defined here}}
+  namespace A {} // expected-note{{namespace '::B::A' defined here}} \
+                 // expected-note{{namespace 'B::A' defined here}}
   using namespace A ;
 }
 
@@ -25,7 +26,7 @@
 }
 
 using namespace ! ; // expected-error{{expected namespace name}}
-using namespace A ; // expected-error{{expected namespace name}}
+using namespace A ; // expected-error{{no namespace named 'A'; did you mean 'B::A'?}}
 using namespace ::A // expected-error{{expected namespace name}} \
                     // expected-error{{expected ';' after namespace name}}
                     B ; 

Modified: cfe/trunk/test/SemaCXX/elaborated-type-specifier.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/elaborated-type-specifier.cpp?rev=158109&r1=158108&r2=158109&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/elaborated-type-specifier.cpp (original)
+++ cfe/trunk/test/SemaCXX/elaborated-type-specifier.cpp Wed Jun  6 15:54:51 2012
@@ -19,7 +19,7 @@
 namespace NS {
   class X {
   public:
-    void test_elab2(struct S4 *s4);
+    void test_elab2(struct S4 *s4); // expected-note{{'NS::S4' declared here}}
   };
 
   void X::test_elab2(S4 *s4) { } // expected-note{{passing argument to parameter 's4' here}}
@@ -35,8 +35,7 @@
 }
 
 void test_S5_scope() {
-  S4 *s4; // expected-error{{use of undeclared identifier 'S4'}} \
-  // expected-error{{use of undeclared identifier 's4'}}
+  S4 *s4; // expected-error{{unknown type name 'S4'; did you mean 'NS::S4'?}}
 }
 
 int test_funcparam_scope(struct S5 * s5) {
@@ -44,5 +43,3 @@
   if (s5 == s5_2) return 1; // expected-error {{comparison of distinct pointer types ('struct S5 *' and 'struct S5 *')}}
   return 0;
 }
-
-

Modified: cfe/trunk/test/SemaCXX/nested-name-spec.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/nested-name-spec.cpp?rev=158109&r1=158108&r2=158109&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/nested-name-spec.cpp (original)
+++ cfe/trunk/test/SemaCXX/nested-name-spec.cpp Wed Jun  6 15:54:51 2012
@@ -113,7 +113,8 @@
       X = 0
     };
 
-    void f() {
+    void f() { // expected-note{{'E::Nested::f' declared here}} \
+               // expected-note{{previous definition is here}}
       return E::X; // expected-error{{expected a class or namespace}}
     }
   }
@@ -143,7 +144,10 @@
   void g(int&); // expected-note{{type of 1st parameter of member declaration does not match definition ('int &' vs 'const int &')}}
 } 
 
-void A::f() {} // expected-error{{out-of-line definition of 'f' does not match any declaration in namespace 'A'}}
+// TODO: Suppress the typo correction for an invalid redeclaration if the chosen
+// correction is a function that already has a body.
+void A::f() {} // expected-error{{out-of-line definition of 'f' does not match any declaration in namespace 'A'; did you mean 'E::Nested::f'?}} \
+               // expected-error{{redefinition of 'f'}}
 
 void A::g(const int&) { } // expected-error{{out-of-line definition of 'g' does not match any declaration in namespace 'A'}}
 
@@ -286,3 +290,15 @@
 template <typename T>
 struct A2<T>::B::C; // expected-error {{no struct named 'C'}}
 }
+
+namespace PR13033 {
+namespace NS {
+ int a; // expected-note {{'NS::a' declared here}}
+ int longer_b; //expected-note {{'NS::longer_b' declared here}}
+}
+
+// Suggest adding a namespace qualifier to both variable names even though one
+// is only a single character long.
+int foobar = a + longer_b; // expected-error {{use of undeclared identifier 'a'; did you mean 'NS::a'?}} \
+                           // expected-error {{use of undeclared identifier 'longer_b'; did you mean 'NS::longer_b'?}}
+}

Modified: cfe/trunk/test/SemaCXX/qualified-id-lookup.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/qualified-id-lookup.cpp?rev=158109&r1=158108&r2=158109&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/qualified-id-lookup.cpp (original)
+++ cfe/trunk/test/SemaCXX/qualified-id-lookup.cpp Wed Jun  6 15:54:51 2012
@@ -86,14 +86,15 @@
 namespace a {  
   namespace a {   // A1
     namespace a { // A2
-      int i;
+      int i; // expected-note{{'::a::a::a::i' declared here}}
     }
   }
 }
 
 void test_a() {
-  a::a::i = 3; // expected-error{{no member named 'i'}}
+  a::a::i = 3; // expected-error{{no member named 'i' in namespace 'a::a'; did you mean '::a::a::a::i'?}}
   a::a::a::i = 4;
+  a::a::j = 3; // expected-error-re{{no member named 'j' in namespace 'a::a'$}}
 }
   
 struct Undef { // expected-note{{definition of 'Undef' is not complete until the closing '}'}}





More information about the cfe-commits mailing list