r185820 - Fix use of invalidated iterator bug in AST match finder.

Manuel Klimek klimek at google.com
Mon Jul 8 07:16:30 PDT 2013


Author: klimek
Date: Mon Jul  8 09:16:30 2013
New Revision: 185820

URL: http://llvm.org/viewvc/llvm-project?rev=185820&view=rev
Log:
Fix use of invalidated iterator bug in AST match finder.

Pulled out the cache clearing in the case of descendant matching, too,
for consistency, also it is not technically needed there.

FIXME: Make cache size configurable and add unit test.

Modified:
    cfe/trunk/lib/ASTMatchers/ASTMatchFinder.cpp

Modified: cfe/trunk/lib/ASTMatchers/ASTMatchFinder.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ASTMatchers/ASTMatchFinder.cpp?rev=185820&r1=185819&r2=185820&view=diff
==============================================================================
--- cfe/trunk/lib/ASTMatchers/ASTMatchFinder.cpp (original)
+++ cfe/trunk/lib/ASTMatchers/ASTMatchFinder.cpp Mon Jul  8 09:16:30 2013
@@ -384,8 +384,6 @@ public:
       return matchesRecursively(Node, Matcher, Builder, MaxDepth, Traversal,
                                 Bind);
 
-    if (ResultCache.size() > MaxMemoizationEntries)
-      ResultCache.clear();
     std::pair<MemoizationMap::iterator, bool> InsertResult =
         ResultCache.insert(std::make_pair(Key, MemoizedMatchResult()));
     if (InsertResult.second) {
@@ -426,6 +424,8 @@ public:
                                    const DynTypedMatcher &Matcher,
                                    BoundNodesTreeBuilder *Builder,
                                    BindKind Bind) {
+    if (ResultCache.size() > MaxMemoizationEntries)
+      ResultCache.clear();
     return memoizedMatchesRecursively(Node, Matcher, Builder, INT_MAX,
                                       TK_AsIs, Bind);
   }
@@ -434,6 +434,10 @@ public:
                                  const DynTypedMatcher &Matcher,
                                  BoundNodesTreeBuilder *Builder,
                                  AncestorMatchMode MatchMode) {
+    // Reset the cache outside of the recursive call to make sure we
+    // don't invalidate any iterators.
+    if (ResultCache.size() > MaxMemoizationEntries)
+      ResultCache.clear();
     return memoizedMatchesAncestorOfRecursively(Node, Matcher, Builder,
                                                 MatchMode);
   }
@@ -498,8 +502,6 @@ private:
     Key.MatcherID = Matcher.getID();
     Key.Node = Node;
     Key.BoundNodes = *Builder;
-    if (ResultCache.size() > MaxMemoizationEntries)
-      ResultCache.clear();
     std::pair<MemoizationMap::iterator, bool> InsertResult =
         ResultCache.insert(std::make_pair(Key, MemoizedMatchResult()));
     if (InsertResult.second) {





More information about the cfe-commits mailing list