[cfe-commits] r154736 - /cfe/trunk/lib/Sema/SemaLookup.cpp
Kaelyn Uhrain
rikka at google.com
Mon Apr 16 11:01:03 PDT 2012
Funny you should be making this change, as it is how I originally had the
TypoEditDistanceMap. In r134009 last June, Doug changed TypoEditDistanceMap
to "[d]ynamically allocate the StringMaps used in typo correction so that
we don't rely on the existence of a copy constructor."
For the record, I prefer to not dynamically allocate the StringMaps so that
manual deletion does not have to be done, making the code simpler to
understand and reducing the potential for memory leaks. ;)
Cheers,
Kaelyn
On Sat, Apr 14, 2012 at 1:26 AM, Benjamin Kramer
<benny.kra at googlemail.com>wrote:
> Author: d0k
> Date: Sat Apr 14 03:26:28 2012
> New Revision: 154736
>
> URL: http://llvm.org/viewvc/llvm-project?rev=154736&view=rev
> Log:
> Directly store TypoResultsMaps in the TypoEditDistanceMap, getting rid of
> manual deletion.
>
> Modified:
> cfe/trunk/lib/Sema/SemaLookup.cpp
>
> Modified: cfe/trunk/lib/Sema/SemaLookup.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaLookup.cpp?rev=154736&r1=154735&r2=154736&view=diff
>
> ==============================================================================
> --- cfe/trunk/lib/Sema/SemaLookup.cpp (original)
> +++ cfe/trunk/lib/Sema/SemaLookup.cpp Sat Apr 14 03:26:28 2012
> @@ -3165,7 +3165,7 @@
> namespace {
>
> typedef llvm::StringMap<TypoCorrection, llvm::BumpPtrAllocator>
> TypoResultsMap;
> -typedef std::map<unsigned, TypoResultsMap *> TypoEditDistanceMap;
> +typedef std::map<unsigned, TypoResultsMap> TypoEditDistanceMap;
>
> static const unsigned MaxTypoDistanceResultSets = 5;
>
> @@ -3187,14 +3187,6 @@
> : Typo(Typo->getName()),
> SemaRef(SemaRef) { }
>
> - ~TypoCorrectionConsumer() {
> - for (TypoEditDistanceMap::iterator I = BestResults.begin(),
> - IEnd = BestResults.end();
> - I != IEnd;
> - ++I)
> - delete I->second;
> - }
> -
> virtual void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext
> *Ctx,
> bool InBaseClass);
> void FoundName(StringRef Name);
> @@ -3207,15 +3199,12 @@
> typedef TypoEditDistanceMap::iterator distance_iterator;
> distance_iterator begin() { return BestResults.begin(); }
> distance_iterator end() { return BestResults.end(); }
> - void erase(distance_iterator I) {
> - delete I->second;
> - BestResults.erase(I);
> - }
> + void erase(distance_iterator I) { BestResults.erase(I); }
> unsigned size() const { return BestResults.size(); }
> bool empty() const { return BestResults.empty(); }
>
> TypoCorrection &operator[](StringRef Name) {
> - return (*BestResults.begin()->second)[Name];
> + return BestResults.begin()->second[Name];
> }
>
> unsigned getBestEditDistance(bool Normalized) {
> @@ -3279,11 +3268,9 @@
>
> void TypoCorrectionConsumer::addCorrection(TypoCorrection Correction) {
> StringRef Name = Correction.getCorrectionAsIdentifierInfo()->getName();
> - TypoResultsMap *& Map = BestResults[Correction.getEditDistance(false)];
> - if (!Map)
> - Map = new TypoResultsMap;
> + TypoResultsMap &Map = BestResults[Correction.getEditDistance(false)];
>
> - TypoCorrection &CurrentCorrection = (*Map)[Name];
> + TypoCorrection &CurrentCorrection = Map[Name];
> if (!CurrentCorrection ||
> // FIXME: The following should be rolled up into an operator< on
> // TypoCorrection with a more principled definition.
> @@ -3881,8 +3868,8 @@
> while (!Consumer.empty()) {
> TypoCorrectionConsumer::distance_iterator DI = Consumer.begin();
> unsigned ED = DI->first;
> - for (TypoCorrectionConsumer::result_iterator I = DI->second->begin(),
> - IEnd = DI->second->end();
> + for (TypoCorrectionConsumer::result_iterator I = DI->second.begin(),
> + IEnd = DI->second.end();
> I != IEnd; /* Increment in loop. */) {
> // If the item already has been looked up or is a keyword, keep it.
> // If a validator callback object was given, drop the correction
> @@ -3891,7 +3878,7 @@
> TypoCorrectionConsumer::result_iterator Prev = I;
> ++I;
> if (!isCandidateViable(CCC, Prev->second))
> - DI->second->erase(Prev);
> + DI->second.erase(Prev);
> continue;
> }
>
> @@ -3910,7 +3897,7 @@
> {
> TypoCorrectionConsumer::result_iterator Next = I;
> ++Next;
> - DI->second->erase(I);
> + DI->second.erase(I);
> I = Next;
> }
> break;
> @@ -3928,7 +3915,7 @@
> I->second.addCorrectionDecl(*TRD);
> ++I;
> if (!isCandidateViable(CCC, Prev->second))
> - DI->second->erase(Prev);
> + DI->second.erase(Prev);
> break;
> }
>
> @@ -3937,14 +3924,14 @@
> I->second.setCorrectionDecl(TmpRes.getAsSingle<NamedDecl>());
> ++I;
> if (!isCandidateViable(CCC, Prev->second))
> - DI->second->erase(Prev);
> + DI->second.erase(Prev);
> break;
> }
>
> }
> }
>
> - if (DI->second->empty())
> + if (DI->second.empty())
> Consumer.erase(DI);
> else if (!getLangOpts().CPlusPlus || QualifiedResults.empty() || !ED)
> // If there are results in the closest possible bucket, stop
> @@ -4008,7 +3995,7 @@
> // No corrections remain...
> if (Consumer.empty()) return TypoCorrection();
>
> - TypoResultsMap &BestResults = *Consumer.begin()->second;
> + TypoResultsMap &BestResults = Consumer.begin()->second;
> ED = TypoCorrection::NormalizeEditDistance(Consumer.begin()->first);
>
> if (ED > 0 && Typo->getName().size() / ED < 3) {
> @@ -4082,7 +4069,8 @@
> std::string tmpBuffer;
> llvm::raw_string_ostream PrefixOStream(tmpBuffer);
> CorrectionNameSpec->print(PrefixOStream, PrintingPolicy(LO));
> - return PrefixOStream.str() + CorrectionName.getAsString();
> + CorrectionName.printName(PrefixOStream);
> + return PrefixOStream.str();
> }
>
> return CorrectionName.getAsString();
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120416/7a20e6ff/attachment.html>
More information about the cfe-commits
mailing list