[llvm-bugs] [Bug 39784] New: [ast_matchers] Assert matching on usages of lambda trying to see if lambda is in global scope

via llvm-bugs llvm-bugs at lists.llvm.org
Mon Nov 26 02:04:53 PST 2018


https://bugs.llvm.org/show_bug.cgi?id=39784

            Bug ID: 39784
           Summary: [ast_matchers] Assert matching on usages of lambda
                    trying to see if lambda is in global scope
           Product: clang
           Version: 7.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Frontend
          Assignee: unassignedclangbugs at nondot.org
          Reporter: whisperity at gmail.com
                CC: llvm-bugs at lists.llvm.org, neeilans at live.com,
                    richard-llvm at metafoo.co.uk

I have a matcher that tries to find functions and types and variables that are
in a namespace, or in the top level. I also try to find usages of such calls.


Consider the following minimal examples:

int main()
{
  auto X = [](auto&& i) { return i + 1; };
  return X(-1);
}

Of course, in this case, X is *not* in some global-esque scope, so the call
shouldn't be matched.


static auto X = [](auto&& i) { return i + 1; };
int main() { return X(-1); }


However, the following matcher expression trying to usages to names that are in
the global-ish scope:

declRefExpr(
  to(
    functionDecl(
      hasAncestor(translationUnitDecl()
    )
  )
)

This second case works well and the call to X() is matched.

(The inner matcher could be a more complex expression like
namespaceDecl(isAnonymous()) or hasParent() instead of hasAncestor())

The following assert snaps:

clang/lib/ASTMatchers/ASTMatchFinder.cpp:676: bool
clang::ast_matchers::internal::{anonymous}::MatchASTVisitor::matchesAncestorOfRecursively(const
clang::ast_type_traits::DynTypedNode&, const
clang::ast_matchers::internal::DynTypedMatcher&,
clang::ast_matchers::internal::BoundNodesTreeBuilder*,
clang::ast_matchers::internal::ASTMatchFinder::AncestorMatchMode): Assertion
`!Parents.empty() && "Found node that is not in the parent map."' failed.
#9 0x00007f765f9c282a clang::ast_matchers::internal::(anonymous
namespace)::MatchASTVisitor::matchesAncestorOfRecursively(clang::ast_type_traits::DynTypedNode
const&, clang::ast_matchers::internal::DynTypedMatcher const&,
clang::ast_matchers::internal::BoundNodesTreeBuilder*,
clang::ast_matchers::internal::ASTMatchFinder::AncestorMatchMode)
clang/lib/ASTMatchers/ASTMatchFinder.cpp:677:0
#10 0x00007f765f9c26cf clang::ast_matchers::internal::(anonymous
namespace)::MatchASTVisitor::memoizedMatchesAncestorOfRecursively(clang::ast_type_traits::DynTypedNode
const&, clang::ast_matchers::internal::DynTypedMatcher const&,
clang::ast_matchers::internal::BoundNodesTreeBuilder*,
clang::ast_matchers::internal::ASTMatchFinder::AncestorMatchMode)
clang/lib/ASTMatchers/ASTMatchFinder.cpp:662:0
#11 0x00007f765f9c1bc9 clang::ast_matchers::internal::(anonymous
namespace)::MatchASTVisitor::matchesAncestorOf(clang::ast_type_traits::DynTypedNode
const&, clang::ast_matchers::internal::DynTypedMatcher const&,
clang::ast_matchers::internal::BoundNodesTreeBuilder*,
clang::ast_matchers::internal::ASTMatchFinder::AncestorMatchMode)
clang/lib/ASTMatchers/ASTMatchFinder.cpp:473:0
#12 0x00007f765f28ca80 bool
clang::ast_matchers::internal::ASTMatchFinder::matchesAncestorOf<clang::Decl>(clang::Decl
const&, clang::ast_matchers::internal::DynTypedMatcher const&,
clang::ast_matchers::internal::BoundNodesTreeBuilder*,
clang::ast_matchers::internal::ASTMatchFinder::AncestorMatchMode)
clang/include/clang/ASTMatchers/ASTMatchersInternal.h:1038:0
#13 0x00007f765f28a6ba
clang::ast_matchers::internal::HasAncestorMatcher<clang::Decl,
clang::Decl>::matches(clang::Decl const&,
clang::ast_matchers::internal::ASTMatchFinder*,
clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
clang/include/clang/ASTMatchers/ASTMatchersInternal.h:1471:0
#14 0x00007f765f0c85a9
clang::ast_matchers::internal::MatcherInterface<clang::Decl>::dynMatches(clang::ast_type_traits::DynTypedNode
const&, clang::ast_matchers::internal::ASTMatchFinder*,
clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
clang/include/clang/ASTMatchers/ASTMatchersInternal.h:310:0
#15 0x00007f765fad765e
clang::ast_matchers::internal::DynTypedMatcher::matches(clang::ast_type_traits::DynTypedNode
const&, clang::ast_matchers::internal::ASTMatchFinder*,
clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
clang/lib/ASTMatchers/ASTMatchersInternal.cpp:215:0
#16 0x0000000000440b60 (clang-query+0x440b60)
#17 0x000000000043b8cd (clang-query+0x43b8cd)
#18 0x00000000004587ef (clang-query+0x4587ef)
#19 0x00007f765fad7017 clang::ast_matchers::internal::(anonymous
namespace)::IdDynMatcher::dynMatches(clang::ast_type_traits::DynTypedNode
const&, clang::ast_matchers::internal::ASTMatchFinder*,
clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
clang/lib/ASTMatchers/ASTMatchersInternal.cpp:111:0
#20 0x00007f765fad7757
clang::ast_matchers::internal::DynTypedMatcher::matchesNoKindCheck(clang::ast_type_traits::DynTypedNode
const&, clang::ast_matchers::internal::ASTMatchFinder*,
clang::ast_matchers::internal::BoundNodesTreeBuilder*) const
clang/lib/ASTMatchers/ASTMatchersInternal.cpp:230:0
#21 0x00007f765f9c20b2 clang::ast_matchers::internal::(anonymous
namespace)::MatchASTVisitor::matchWithFilter(clang::ast_type_traits::DynTypedNode
const&) clang/lib/ASTMatchers/ASTMatchFinder.cpp:572:0
#22 0x00007f765f9c22f1 clang::ast_matchers::internal::(anonymous
namespace)::MatchASTVisitor::matchDispatch(clang::Stmt const*)
clang/lib/ASTMatchers/ASTMatchFinder.cpp:599:0
#23 0x00007f765f9c7883 void clang::ast_matchers::internal::(anonymous
namespace)::MatchASTVisitor::match<clang::Stmt>(clang::Stmt const&)
clang/lib/ASTMatchers/ASTMatchFinder.cpp:500:0
#24 0x00007f765f9c32ed clang::ast_matchers::internal::(anonymous
namespace)::MatchASTVisitor::TraverseStmt(clang::Stmt*,
llvm::SmallVectorImpl<llvm::PointerIntPair<clang::Stmt*, 1u, bool,
llvm::PointerLikeTypeTraits<clang::Stmt*>,
llvm::PointerIntPairInfo<clang::Stmt*, 1u,
llvm::PointerLikeTypeTraits<clang::Stmt*> > > >*)
clang/lib/ASTMatchers/ASTMatchFinder.cpp:854:0
#25 0x00007f765fa3cddf
clang::RecursiveASTVisitor<clang::ast_matchers::internal::(anonymous
namespace)::MatchASTVisitor>::TraverseImplicitCastExpr(clang::ImplicitCastExpr*,
llvm::SmallVectorImpl<llvm::PointerIntPair<clang::Stmt*, 1u, bool,
llvm::PointerLikeTypeTraits<clang::Stmt*>,
llvm::PointerIntPairInfo<clang::Stmt*, 1u,
llvm::PointerLikeTypeTraits<clang::Stmt*> > > >*)
clang/include/clang/AST/RecursiveASTVisitor.h:2226:0
#26 0x00007f765f9ec0cf
clang::RecursiveASTVisitor<clang::ast_matchers::internal::(anonymous
namespace)::MatchASTVisitor>::dataTraverseNode(clang::Stmt*,
llvm::SmallVectorImpl<llvm::PointerIntPair<clang::Stmt*, 1u, bool,
llvm::PointerLikeTypeTraits<clang::Stmt*>,
llvm::PointerIntPairInfo<clang::Stmt*, 1u,
llvm::PointerLikeTypeTraits<clang::Stmt*> > > >*)
/home/ericsza/Work/LLVM/Normal/Build/tools/clang/include/clang/AST/StmtNodes.inc:423:0
#27 0x00007f765f9c96c8
clang::RecursiveASTVisitor<clang::ast_matchers::internal::(anonymous
namespace)::MatchASTVisitor>::TraverseStmt(clang::Stmt*,
llvm::SmallVectorImpl<llvm::PointerIntPair<clang::Stmt*, 1u, bool,
llvm::PointerLikeTypeTraits<clang::Stmt*>,
llvm::PointerIntPairInfo<clang::Stmt*, 1u,
llvm::PointerLikeTypeTraits<clang::Stmt*> > > >*)
clang/include/clang/AST/RecursiveASTVisitor.h:654:0
#28 0x00007f765f9c3304 clang::ast_matchers::internal::(anonymous
namespace)::MatchASTVisitor::TraverseStmt(clang::Stmt*,
llvm::SmallVectorImpl<llvm::PointerIntPair<clang::Stmt*, 1u, bool,
llvm::PointerLikeTypeTraits<clang::Stmt*>,
llvm::PointerIntPairInfo<clang::Stmt*, 1u,
llvm::PointerLikeTypeTraits<clang::Stmt*> > > >*)
clang/lib/ASTMatchers/ASTMatchFinder.cpp:855:0
#29 0x00007f765fa25581
clang::RecursiveASTVisitor<clang::ast_matchers::internal::(anonymous
namespace)::MatchASTVisitor>::TraverseFunctionHelper(clang::FunctionDecl*)
clang/include/clang/AST/RecursiveASTVisitor.h:2001:0
#30 0x00007f765f9e3e58
clang::RecursiveASTVisitor<clang::ast_matchers::internal::(anonymous
namespace)::MatchASTVisitor>::TraverseFunctionDecl(clang::FunctionDecl*)
clang/include/clang/AST/RecursiveASTVisitor.h:2006:0
#31 0x00007f765f9c8dd9
clang::RecursiveASTVisitor<clang::ast_matchers::internal::(anonymous
namespace)::MatchASTVisitor>::TraverseDecl(clang::Decl*)
/home/ericsza/Work/LLVM/Normal/Build/tools/clang/include/clang/AST/DeclNodes.inc:389:0
#32 0x00007f765f9c32b6 clang::ast_matchers::internal::(anonymous
namespace)::MatchASTVisitor::TraverseDecl(clang::Decl*)
clang/lib/ASTMatchers/ASTMatchFinder.cpp:847:0
#33 0x00007f765fa22ec2
clang::RecursiveASTVisitor<clang::ast_matchers::internal::(anonymous
namespace)::MatchASTVisitor>::TraverseDeclContextHelper(clang::DeclContext*)
clang/include/clang/AST/RecursiveASTVisitor.h:1388:0
#34 0x00007f765f9e5f73
clang::RecursiveASTVisitor<clang::ast_matchers::internal::(anonymous
namespace)::MatchASTVisitor>::TraverseTranslationUnitDecl(clang::TranslationUnitDecl*)
clang/include/clang/AST/RecursiveASTVisitor.h:1480:0
#35 0x00007f765f9c92a1
clang::RecursiveASTVisitor<clang::ast_matchers::internal::(anonymous
namespace)::MatchASTVisitor>::TraverseDecl(clang::Decl*)
/home/ericsza/Work/LLVM/Normal/Build/tools/clang/include/clang/AST/DeclNodes.inc:553:0
#36 0x00007f765f9c32b6 clang::ast_matchers::internal::(anonymous
namespace)::MatchASTVisitor::TraverseDecl(clang::Decl*)
clang/lib/ASTMatchers/ASTMatchFinder.cpp:847:0
#37 0x00007f765f9c3e59
clang::ast_matchers::MatchFinder::matchAST(clang::ASTContext&)
clang/lib/ASTMatchers/ASTMatchFinder.cpp:1023:0
#38 0x00007f7660c75198 clang::query::MatchQuery::run(llvm::raw_ostream&,
clang::query::QuerySession&) const clang/tools/extra/clang-query/Query.cpp:89:0

It's seemingly only lambda expressions (no matter if they are generic lambdae
or not) that trip this.

In the topmost few stack frames, the "Node" argument that is passed through the
stack is a 'CXXMethodDecl' of the lambda's 'operator()' function.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20181126/c9f778fc/attachment-0001.html>


More information about the llvm-bugs mailing list