[cfe-commits] r101259 - in /cfe/trunk/lib/Sema: Sema.h SemaLookup.cpp

Douglas Gregor dgregor at apple.com
Wed Apr 14 10:09:22 PDT 2010


Author: dgregor
Date: Wed Apr 14 12:09:22 2010
New Revision: 101259

URL: http://llvm.org/viewvc/llvm-project?rev=101259&view=rev
Log:
Return the corrected DeclarationName from Sema::CorrectTypo rather
than just a bool indicating that correction occurred. No actual
functionality change (it's still always used like a bool), but this
refactoring will be used to support typo correction to keywords.

Modified:
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaLookup.cpp

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=101259&r1=101258&r2=101259&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Wed Apr 14 12:09:22 2010
@@ -1431,10 +1431,10 @@
   void LookupVisibleDecls(DeclContext *Ctx, LookupNameKind Kind,
                           VisibleDeclConsumer &Consumer);
 
-  bool CorrectTypo(LookupResult &R, Scope *S, CXXScopeSpec *SS,
-                   DeclContext *MemberContext = 0,
-                   bool EnteringContext = false,
-                   const ObjCObjectPointerType *OPT = 0);
+  DeclarationName CorrectTypo(LookupResult &R, Scope *S, CXXScopeSpec *SS,
+                              DeclContext *MemberContext = 0,
+                              bool EnteringContext = false,
+                              const ObjCObjectPointerType *OPT = 0);
 
   void FindAssociatedClassesAndNamespaces(Expr **Args, unsigned NumArgs,
                                    AssociatedNamespaceSet &AssociatedNamespaces,

Modified: cfe/trunk/lib/Sema/SemaLookup.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaLookup.cpp?rev=101259&r1=101258&r2=101259&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaLookup.cpp (original)
+++ cfe/trunk/lib/Sema/SemaLookup.cpp Wed Apr 14 12:09:22 2010
@@ -2497,37 +2497,38 @@
 /// \param OPT when non-NULL, the search for visible declarations will
 /// also walk the protocols in the qualified interfaces of \p OPT.
 ///
-/// \returns true if the typo was corrected, in which case the \p Res
-/// structure will contain the results of name lookup for the
-/// corrected name. Otherwise, returns false.
-bool Sema::CorrectTypo(LookupResult &Res, Scope *S, CXXScopeSpec *SS,
+/// \returns the corrected name if the typo was corrected, otherwise returns an
+/// empty \c DeclarationName. When a typo was corrected, the result structure
+/// may contain the results of name lookup for the correct name or it may be
+/// empty.
+DeclarationName Sema::CorrectTypo(LookupResult &Res, Scope *S, CXXScopeSpec *SS,
                        DeclContext *MemberContext, bool EnteringContext,
                        const ObjCObjectPointerType *OPT) {
   if (Diags.hasFatalErrorOccurred())
-    return false;
+    return DeclarationName();
 
   // Provide a stop gap for files that are just seriously broken.  Trying
   // to correct all typos can turn into a HUGE performance penalty, causing
   // some files to take minutes to get rejected by the parser.
   // FIXME: Is this the right solution?
   if (TyposCorrected == 20)
-    return false;
+    return DeclarationName();
   ++TyposCorrected;
   
   // We only attempt to correct typos for identifiers.
   IdentifierInfo *Typo = Res.getLookupName().getAsIdentifierInfo();
   if (!Typo)
-    return false;
+    return DeclarationName();
 
   // If the scope specifier itself was invalid, don't try to correct
   // typos.
   if (SS && SS->isInvalid())
-    return false;
+    return DeclarationName();
 
   // Never try to correct typos during template deduction or
   // instantiation.
   if (!ActiveTemplateInstantiations.empty())
-    return false;
+    return DeclarationName();
 
   TypoCorrectionConsumer Consumer(Typo);
   if (MemberContext) {
@@ -2543,7 +2544,7 @@
   } else if (SS && SS->isSet()) {
     DeclContext *DC = computeDeclContext(*SS, EnteringContext);
     if (!DC)
-      return false;
+      return DeclarationName();
     
     LookupVisibleDecls(DC, Res.getLookupKind(), Consumer);
   } else {
@@ -2551,7 +2552,7 @@
   }
 
   if (Consumer.empty())
-    return false;
+    return DeclarationName();
 
   // Only allow a single, closest name in the result set (it's okay to
   // have overloads of that name, though).
@@ -2566,7 +2567,7 @@
   ++I;
   for(TypoCorrectionConsumer::iterator IEnd = Consumer.end(); I != IEnd; ++I) {
     if (BestName != (*I)->getDeclName())
-      return false;
+      return DeclarationName();
 
     // FIXME: If there are both ivars and properties of the same name,
     // don't return both because the callee can't handle two
@@ -2581,7 +2582,7 @@
   // each correction.
   unsigned ED = Consumer.getBestEditDistance();
   if (ED == 0 || (BestName.getAsIdentifierInfo()->getName().size() / ED) < 3)
-    return false;
+    return DeclarationName();
 
   // Perform name lookup again with the name we chose, and declare
   // success if we found something that was not ambiguous.
@@ -2603,8 +2604,11 @@
 
   if (Res.isAmbiguous()) {
     Res.suppressDiagnostics();
-    return false;
+    return DeclarationName();
   }
 
-  return Res.getResultKind() != LookupResult::NotFound;
+  if (Res.getResultKind() != LookupResult::NotFound)
+    return BestName;
+  
+  return DeclarationName();
 }





More information about the cfe-commits mailing list