2013-02-28 21:21:39 +08:00
|
|
|
//===- unittest/AST/ASTContextParentMapTest.cpp - AST parent map test -----===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-02-28 21:21:39 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Tests for the getParents(...) methods of ASTContext.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/ASTContext.h"
|
2014-01-07 19:51:46 +08:00
|
|
|
#include "MatchVerifier.h"
|
2013-02-28 21:21:39 +08:00
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchers.h"
|
|
|
|
#include "clang/Tooling/Tooling.h"
|
|
|
|
#include "gtest/gtest.h"
|
[AST] Allow limiting the scope of common AST traversals (getParents, RAV).
Summary:
The goal is to allow analyses such as clang-tidy checks to run on a
subset of the AST, e.g. "only on main-file decls" for interactive tools.
Today, these become "problematically global" by running RecursiveASTVisitors
rooted at the TUDecl, or by navigating up via ASTContext::getParent().
The scope is restricted using a set of top-level-decls that RecursiveASTVisitors
should be rooted at. This also applies to the visitor that populates the
parent map, and so the top-level-decls are considered to have no parents.
This patch makes the traversal scope a mutable property of ASTContext.
The more obvious way to do this is to pass the top-level decls to
relevant functions directly, but this has some problems:
- it's error-prone: accidentally mixing restricted and unrestricted
scopes is a performance trap. Interleaving multiple analyses is
common (many clang-tidy checks run matchers or RAVs from matcher callbacks)
- it doesn't map well to the actual use cases, where we really do want
*all* traversals to be restricted.
- it involves a lot of plumbing in parts of the code that don't care
about traversals.
This approach was tried out in D54259 and D54261, I wanted to like it
but it feels pretty awful in practice.
Caveats: to get scope-limiting behavior of RecursiveASTVisitors, callers
have to call the new TraverseAST(Ctx) function instead of TraverseDecl(TU).
I think this is an improvement to the API regardless.
Reviewers: klimek, ioeric
Subscribers: mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D54309
llvm-svn: 346847
2018-11-14 18:33:30 +08:00
|
|
|
#include "gmock/gmock.h"
|
|
|
|
|
|
|
|
using testing::ElementsAre;
|
2013-02-28 21:21:39 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace ast_matchers {
|
|
|
|
|
|
|
|
TEST(GetParents, ReturnsParentForDecl) {
|
|
|
|
MatchVerifier<Decl> Verifier;
|
2015-09-17 21:30:52 +08:00
|
|
|
EXPECT_TRUE(
|
|
|
|
Verifier.match("class C { void f(); };",
|
|
|
|
cxxMethodDecl(hasParent(recordDecl(hasName("C"))))));
|
2013-02-28 21:21:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(GetParents, ReturnsParentForStmt) {
|
|
|
|
MatchVerifier<Stmt> Verifier;
|
|
|
|
EXPECT_TRUE(Verifier.match("class C { void f() { if (true) {} } };",
|
|
|
|
ifStmt(hasParent(compoundStmt()))));
|
|
|
|
}
|
|
|
|
|
2015-10-23 17:04:55 +08:00
|
|
|
TEST(GetParents, ReturnsParentForTypeLoc) {
|
|
|
|
MatchVerifier<TypeLoc> Verifier;
|
|
|
|
EXPECT_TRUE(
|
|
|
|
Verifier.match("namespace a { class b {}; } void f(a::b) {}",
|
|
|
|
typeLoc(hasParent(typeLoc(hasParent(functionDecl()))))));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(GetParents, ReturnsParentForNestedNameSpecifierLoc) {
|
|
|
|
MatchVerifier<NestedNameSpecifierLoc> Verifier;
|
|
|
|
EXPECT_TRUE(Verifier.match("namespace a { class b {}; } void f(a::b) {}",
|
|
|
|
nestedNameSpecifierLoc(hasParent(typeLoc()))));
|
|
|
|
}
|
|
|
|
|
2013-02-28 21:21:39 +08:00
|
|
|
TEST(GetParents, ReturnsParentInsideTemplateInstantiations) {
|
|
|
|
MatchVerifier<Decl> DeclVerifier;
|
|
|
|
EXPECT_TRUE(DeclVerifier.match(
|
|
|
|
"template<typename T> struct C { void f() {} };"
|
|
|
|
"void g() { C<int> c; c.f(); }",
|
2015-09-17 21:30:52 +08:00
|
|
|
cxxMethodDecl(hasName("f"),
|
|
|
|
hasParent(cxxRecordDecl(isTemplateInstantiation())))));
|
2013-02-28 21:21:39 +08:00
|
|
|
EXPECT_TRUE(DeclVerifier.match(
|
|
|
|
"template<typename T> struct C { void f() {} };"
|
|
|
|
"void g() { C<int> c; c.f(); }",
|
2015-09-17 21:30:52 +08:00
|
|
|
cxxMethodDecl(hasName("f"),
|
|
|
|
hasParent(cxxRecordDecl(unless(isTemplateInstantiation()))))));
|
2013-02-28 21:21:39 +08:00
|
|
|
EXPECT_FALSE(DeclVerifier.match(
|
|
|
|
"template<typename T> struct C { void f() {} };"
|
|
|
|
"void g() { C<int> c; c.f(); }",
|
2015-09-17 21:30:52 +08:00
|
|
|
cxxMethodDecl(
|
|
|
|
hasName("f"),
|
|
|
|
allOf(hasParent(cxxRecordDecl(unless(isTemplateInstantiation()))),
|
|
|
|
hasParent(cxxRecordDecl(isTemplateInstantiation()))))));
|
2013-02-28 21:21:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(GetParents, ReturnsMultipleParentsInTemplateInstantiations) {
|
|
|
|
MatchVerifier<Stmt> TemplateVerifier;
|
|
|
|
EXPECT_TRUE(TemplateVerifier.match(
|
|
|
|
"template<typename T> struct C { void f() {} };"
|
|
|
|
"void g() { C<int> c; c.f(); }",
|
2015-09-17 21:30:52 +08:00
|
|
|
compoundStmt(allOf(
|
|
|
|
hasAncestor(cxxRecordDecl(isTemplateInstantiation())),
|
|
|
|
hasAncestor(cxxRecordDecl(unless(isTemplateInstantiation())))))));
|
2013-02-28 21:21:39 +08:00
|
|
|
}
|
|
|
|
|
[AST] Allow limiting the scope of common AST traversals (getParents, RAV).
Summary:
The goal is to allow analyses such as clang-tidy checks to run on a
subset of the AST, e.g. "only on main-file decls" for interactive tools.
Today, these become "problematically global" by running RecursiveASTVisitors
rooted at the TUDecl, or by navigating up via ASTContext::getParent().
The scope is restricted using a set of top-level-decls that RecursiveASTVisitors
should be rooted at. This also applies to the visitor that populates the
parent map, and so the top-level-decls are considered to have no parents.
This patch makes the traversal scope a mutable property of ASTContext.
The more obvious way to do this is to pass the top-level decls to
relevant functions directly, but this has some problems:
- it's error-prone: accidentally mixing restricted and unrestricted
scopes is a performance trap. Interleaving multiple analyses is
common (many clang-tidy checks run matchers or RAVs from matcher callbacks)
- it doesn't map well to the actual use cases, where we really do want
*all* traversals to be restricted.
- it involves a lot of plumbing in parts of the code that don't care
about traversals.
This approach was tried out in D54259 and D54261, I wanted to like it
but it feels pretty awful in practice.
Caveats: to get scope-limiting behavior of RecursiveASTVisitors, callers
have to call the new TraverseAST(Ctx) function instead of TraverseDecl(TU).
I think this is an improvement to the API regardless.
Reviewers: klimek, ioeric
Subscribers: mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D54309
llvm-svn: 346847
2018-11-14 18:33:30 +08:00
|
|
|
TEST(GetParents, RespectsTraversalScope) {
|
|
|
|
auto AST =
|
|
|
|
tooling::buildASTFromCode("struct foo { int bar; };", "foo.cpp",
|
|
|
|
std::make_shared<PCHContainerOperations>());
|
|
|
|
auto &Ctx = AST->getASTContext();
|
|
|
|
auto &TU = *Ctx.getTranslationUnitDecl();
|
|
|
|
auto &Foo = *TU.lookup(&Ctx.Idents.get("foo")).front();
|
|
|
|
auto &Bar = *cast<DeclContext>(Foo).lookup(&Ctx.Idents.get("bar")).front();
|
|
|
|
|
|
|
|
// Initially, scope is the whole TU.
|
|
|
|
EXPECT_THAT(Ctx.getParents(Bar), ElementsAre(DynTypedNode::create(Foo)));
|
|
|
|
EXPECT_THAT(Ctx.getParents(Foo), ElementsAre(DynTypedNode::create(TU)));
|
|
|
|
|
|
|
|
// Restrict the scope, now some parents are gone.
|
|
|
|
Ctx.setTraversalScope({&Foo});
|
|
|
|
EXPECT_THAT(Ctx.getParents(Bar), ElementsAre(DynTypedNode::create(Foo)));
|
|
|
|
EXPECT_THAT(Ctx.getParents(Foo), ElementsAre());
|
|
|
|
|
|
|
|
// Reset the scope, we get back the original results.
|
|
|
|
Ctx.setTraversalScope({&TU});
|
|
|
|
EXPECT_THAT(Ctx.getParents(Bar), ElementsAre(DynTypedNode::create(Foo)));
|
|
|
|
EXPECT_THAT(Ctx.getParents(Foo), ElementsAre(DynTypedNode::create(TU)));
|
|
|
|
}
|
|
|
|
|
2019-01-14 18:31:42 +08:00
|
|
|
TEST(GetParents, ImplicitLambdaNodes) {
|
|
|
|
MatchVerifier<Decl> LambdaVerifier;
|
|
|
|
EXPECT_TRUE(LambdaVerifier.match(
|
|
|
|
"auto x = []{int y;};",
|
|
|
|
varDecl(hasName("y"), hasAncestor(functionDecl(
|
|
|
|
hasOverloadedOperatorName("()"),
|
|
|
|
hasParent(cxxRecordDecl(
|
|
|
|
isImplicit(), hasParent(lambdaExpr())))))),
|
|
|
|
Lang_CXX11));
|
|
|
|
}
|
|
|
|
|
2013-02-28 21:21:39 +08:00
|
|
|
} // end namespace ast_matchers
|
|
|
|
} // end namespace clang
|