From 94a56856d24e5f8c5c30c4bec2dbca15bd2d6076 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Fri, 16 Nov 2012 18:39:22 +0000 Subject: [PATCH] Fix partial-match-bind-behavior with forEachDescendant() matchers. The problem is that a partial match of an (explicit or implicit) allOf matcher binds results, i.e. recordDecl(decl().bind("x"), hasName("A")) can very well bind a record that is not named "A". With this fix, the common cases of stumbling over this bug are fixed by the BoundNodesMap overwriting the results of a partial match. An error can still be created with a weird combination of anyOf and allOf (see inactive test). We need to decide whether this is worth fixing, as the fix will have performance impact. Review: http://llvm-reviews.chandlerc.com/D124 llvm-svn: 168177 --- clang/lib/ASTMatchers/ASTMatchersInternal.cpp | 7 +++++-- clang/unittests/ASTMatchers/ASTMatchersTest.cpp | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/clang/lib/ASTMatchers/ASTMatchersInternal.cpp b/clang/lib/ASTMatchers/ASTMatchersInternal.cpp index 408195d..f1a9ff2 100644 --- a/clang/lib/ASTMatchers/ASTMatchersInternal.cpp +++ b/clang/lib/ASTMatchers/ASTMatchersInternal.cpp @@ -27,8 +27,11 @@ void BoundNodesMap::copyTo(BoundNodesTreeBuilder *Builder) const { } void BoundNodesMap::copyTo(BoundNodesMap *Other) const { - copy(NodeMap.begin(), NodeMap.end(), - inserter(Other->NodeMap, Other->NodeMap.begin())); + for (IDToNodeMap::const_iterator I = NodeMap.begin(), + E = NodeMap.end(); + I != E; ++I) { + Other->NodeMap[I->first] = I->second; + } } BoundNodesTree::BoundNodesTree() {} diff --git a/clang/unittests/ASTMatchers/ASTMatchersTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersTest.cpp index 6d8c000..3eddccc 100644 --- a/clang/unittests/ASTMatchers/ASTMatchersTest.cpp +++ b/clang/unittests/ASTMatchers/ASTMatchersTest.cpp @@ -2777,6 +2777,22 @@ TEST(ForEachDescendant, BindsOneNode) { new VerifyIdIsBoundTo("x", 1))); } +TEST(ForEachDescendant, NestedForEachDescendant) { + DeclarationMatcher m = recordDecl( + isDefinition(), decl().bind("x"), hasName("C")); + EXPECT_TRUE(matchAndVerifyResultTrue( + "class A { class B { class C {}; }; };", + recordDecl(hasName("A"), anyOf(m, forEachDescendant(m))), + new VerifyIdIsBoundTo("x", "C"))); + + // FIXME: This is not really a useful matcher, but the result is still + // surprising (currently binds "A"). + //EXPECT_TRUE(matchAndVerifyResultTrue( + // "class A { class B { class C {}; }; };", + // recordDecl(hasName("A"), allOf(hasDescendant(m), anyOf(m, anything()))), + // new VerifyIdIsBoundTo("x", "C"))); +} + TEST(ForEachDescendant, BindsMultipleNodes) { EXPECT_TRUE(matchAndVerifyResultTrue( "class C { class D { int x; int y; }; " -- 2.7.4