]> granicus.if.org Git - clang/commitdiff
Fix use of invalidated iterator bug in AST match finder.
authorManuel Klimek <klimek@google.com>
Mon, 8 Jul 2013 14:16:30 +0000 (14:16 +0000)
committerManuel Klimek <klimek@google.com>
Mon, 8 Jul 2013 14:16:30 +0000 (14:16 +0000)
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.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@185820 91177308-0d34-0410-b5e6-96231b3b80d8

lib/ASTMatchers/ASTMatchFinder.cpp

index a68c7fdffe1b65eaeb27e88bbe05ff437fe7ae88..e1e5f44c07b1754bc2fd097bc7b47eb7981427b6 100644 (file)
@@ -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) {