r220694 - Have TypoCorrectionConsumer remember the TypoCorrections it returned.

Kaelyn Takata rikka at google.com
Mon Oct 27 11:07:34 PDT 2014


Author: rikka
Date: Mon Oct 27 13:07:34 2014
New Revision: 220694

URL: http://llvm.org/viewvc/llvm-project?rev=220694&view=rev
Log:
Have TypoCorrectionConsumer remember the TypoCorrections it returned.

Two additional methods are provided: one to return the current
correction (the last correction returned by getNextCorrection), and one
to "reset" the state so that getNextCorrection will return the previous
corrections before returning any new corrections.

Also ensure that all TypoCorrections have valid source ranges.

Modified:
    cfe/trunk/include/clang/Sema/SemaInternal.h
    cfe/trunk/lib/Sema/SemaLookup.cpp

Modified: cfe/trunk/include/clang/Sema/SemaInternal.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/SemaInternal.h?rev=220694&r1=220693&r2=220694&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/SemaInternal.h (original)
+++ cfe/trunk/include/clang/Sema/SemaInternal.h Mon Oct 27 13:07:34 2014
@@ -100,12 +100,14 @@ public:
                          std::unique_ptr<CorrectionCandidateCallback> CCC,
                          DeclContext *MemberContext,
                          bool EnteringContext)
-      : Typo(TypoName.getName().getAsIdentifierInfo()), SemaRef(SemaRef), S(S),
-        SS(SS), CorrectionValidator(std::move(CCC)), MemberContext(MemberContext),
-        Result(SemaRef, TypoName, LookupKind),
+      : Typo(TypoName.getName().getAsIdentifierInfo()), CurrentTCIndex(0),
+        SemaRef(SemaRef), S(S), SS(SS), CorrectionValidator(std::move(CCC)),
+        MemberContext(MemberContext), Result(SemaRef, TypoName, LookupKind),
         Namespaces(SemaRef.Context, SemaRef.CurContext, SS),
         EnteringContext(EnteringContext), SearchNamespaces(false) {
     Result.suppressDiagnostics();
+    // Arrange for ValidatedCorrections[0] to always be an empty correction.
+    ValidatedCorrections.push_back(TypoCorrection());
   }
 
   bool includeHiddenDecls() const override { return true; }
@@ -117,7 +119,9 @@ public:
   void addKeywordResult(StringRef Keyword);
   void addCorrection(TypoCorrection Correction);
 
-  bool empty() const { return CorrectionResults.empty(); }
+  bool empty() const {
+    return CorrectionResults.empty() && ValidatedCorrections.size() == 1;
+  }
 
   /// \brief Return the list of TypoCorrections for the given identifier from
   /// the set of corrections that have the closest edit distance, if any.
@@ -147,7 +151,21 @@ public:
   /// starting with the corrections that have the closest edit distance. An
   /// empty TypoCorrection is returned once no more viable corrections remain
   /// in the consumer.
-  TypoCorrection getNextCorrection();
+  const TypoCorrection &getNextCorrection();
+
+  /// \brief Get the last correction returned by getNextCorrection().
+  const TypoCorrection &getCurrentCorrection() {
+    return CurrentTCIndex < ValidatedCorrections.size()
+               ? ValidatedCorrections[CurrentTCIndex]
+               : ValidatedCorrections[0];  // The empty correction.
+  }
+
+  /// \brief Reset the consumer's position in the stream of viable corrections
+  /// (i.e. getNextCorrection() will return each of the previously returned
+  /// corrections in order before returning any new corrections).
+  void resetCorrectionStream() {
+    CurrentTCIndex = 0;
+  }
 
   ASTContext &getContext() const { return SemaRef.Context; }
   const LookupResult &getLookupResult() const { return Result; }
@@ -223,6 +241,9 @@ private:
   /// whether there is a keyword with this name.
   TypoEditDistanceMap CorrectionResults;
 
+  SmallVector<TypoCorrection, 4> ValidatedCorrections;
+  size_t CurrentTCIndex;
+
   Sema &SemaRef;
   Scope *S;
   CXXScopeSpec *SS;

Modified: cfe/trunk/lib/Sema/SemaLookup.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaLookup.cpp?rev=220694&r1=220693&r2=220694&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaLookup.cpp (original)
+++ cfe/trunk/lib/Sema/SemaLookup.cpp Mon Oct 27 13:07:34 2014
@@ -3444,7 +3444,11 @@ void TypoCorrectionConsumer::addNamespac
   }
 }
 
-TypoCorrection TypoCorrectionConsumer::getNextCorrection() {
+const TypoCorrection &TypoCorrectionConsumer::getNextCorrection() {
+  if (++CurrentTCIndex < ValidatedCorrections.size())
+    return ValidatedCorrections[CurrentTCIndex];
+
+  CurrentTCIndex = ValidatedCorrections.size();
   while (!CorrectionResults.empty()) {
     auto DI = CorrectionResults.begin();
     if (DI->second.empty()) {
@@ -3460,16 +3464,20 @@ TypoCorrection TypoCorrectionConsumer::g
     }
 
     TypoCorrection TC = RI->second.pop_back_val();
-    if (TC.isResolved() || resolveCorrection(TC))
-      return TC;
+    if (TC.isResolved() || resolveCorrection(TC)) {
+      ValidatedCorrections.push_back(TC);
+      return ValidatedCorrections[CurrentTCIndex];
+    }
   }
-  return TypoCorrection();
+  return ValidatedCorrections[0];  // The empty correction.
 }
 
 bool TypoCorrectionConsumer::resolveCorrection(TypoCorrection &Candidate) {
   IdentifierInfo *Name = Candidate.getCorrectionAsIdentifierInfo();
   DeclContext *TempMemberContext = MemberContext;
   CXXScopeSpec *TempSS = SS;
+  if (Candidate.getCorrectionRange().isInvalid())
+    Candidate.setCorrectionRange(TempSS, Result.getLookupNameInfo());
 retry_lookup:
   LookupPotentialTypoResult(SemaRef, Result, Name, S, TempSS, TempMemberContext,
                             EnteringContext,





More information about the cfe-commits mailing list