/// Represents the AST of a TranslationUnit.
class SyntaxTree::Impl {
public:
- /// Constructs a tree from the entire translation unit.
- Impl(SyntaxTree *Parent, const ASTContext &AST);
/// Constructs a tree from an AST node.
- Impl(SyntaxTree *Parent, Decl *N, const ASTContext &AST);
- Impl(SyntaxTree *Parent, Stmt *N, const ASTContext &AST);
+ Impl(SyntaxTree *Parent, Decl *N, ASTContext &AST);
+ Impl(SyntaxTree *Parent, Stmt *N, ASTContext &AST);
template <class T>
Impl(SyntaxTree *Parent,
typename std::enable_if<std::is_base_of<Stmt, T>::value, T>::type *Node,
- const ASTContext &AST)
+ ASTContext &AST)
: Impl(Parent, dyn_cast<Stmt>(Node), AST) {}
template <class T>
Impl(SyntaxTree *Parent,
typename std::enable_if<std::is_base_of<Decl, T>::value, T>::type *Node,
- const ASTContext &AST)
+ ASTContext &AST)
: Impl(Parent, dyn_cast<Decl>(Node), AST) {}
SyntaxTree *Parent;
- const ASTContext &AST;
+ ASTContext &AST;
std::vector<NodeId> Leaves;
// Maps preorder indices to postorder ones.
std::vector<int> PostorderIds;
bool isInSubtree(NodeId Id, NodeId SubtreeRoot) const;
int findPositionInParent(NodeId Id, bool Shifted = false) const;
+ std::string getRelativeName(const NamedDecl *ND,
+ const DeclContext *Context) const;
+ std::string getRelativeName(const NamedDecl *ND) const;
+
std::string getNodeValue(NodeId Id) const;
std::string getNodeValue(const Node &Node) const;
std::string getDeclValue(const Decl *D) const;
};
} // end anonymous namespace
-SyntaxTree::Impl::Impl(SyntaxTree *Parent, Decl *N, const ASTContext &AST)
+SyntaxTree::Impl::Impl(SyntaxTree *Parent, Decl *N, ASTContext &AST)
: Parent(Parent), AST(AST) {
NodeCountVisitor NodeCounter(*this);
NodeCounter.TraverseDecl(N);
initTree();
}
-SyntaxTree::Impl::Impl(SyntaxTree *Parent, Stmt *N, const ASTContext &AST)
+SyntaxTree::Impl::Impl(SyntaxTree *Parent, Stmt *N, ASTContext &AST)
: Parent(Parent), AST(AST) {
NodeCountVisitor NodeCounter(*this);
NodeCounter.TraverseStmt(N);
llvm_unreachable("Node not found in parent's children.");
}
+// Returns the qualified name of ND. If it is subordinate to Context,
+// then the prefix of the latter is removed from the returned value.
+std::string
+SyntaxTree::Impl::getRelativeName(const NamedDecl *ND,
+ const DeclContext *Context) const {
+ std::string ContextPrefix;
+ if (auto *Namespace = dyn_cast<NamespaceDecl>(Context))
+ ContextPrefix = Namespace->getQualifiedNameAsString();
+ else if (auto *Record = dyn_cast<RecordDecl>(Context))
+ ContextPrefix = Record->getQualifiedNameAsString();
+ else if (AST.getLangOpts().CPlusPlus11)
+ if (auto *Tag = dyn_cast<TagDecl>(Context))
+ ContextPrefix = Tag->getQualifiedNameAsString();
+ std::string Val = ND->getQualifiedNameAsString();
+ // Strip the qualifier, if Val refers to somthing in the current scope.
+ // But leave one leading ':' in place, so that we know that this is a
+ // relative path.
+ if (!ContextPrefix.empty() && StringRef(Val).startswith(ContextPrefix))
+ Val = Val.substr(ContextPrefix.size() + 1);
+ return Val;
+}
+
+std::string SyntaxTree::Impl::getRelativeName(const NamedDecl *ND) const {
+ return getRelativeName(ND, ND->getDeclContext());
+}
+
+static const DeclContext *getEnclosingDeclContext(ASTContext &AST,
+ const Stmt *S) {
+ while (S) {
+ const auto &Parents = AST.getParents(*S);
+ if (Parents.empty())
+ return nullptr;
+ const auto &P = Parents[0];
+ if (const auto *D = P.get<Decl>())
+ return D->getDeclContext();
+ S = P.get<Stmt>();
+ }
+ llvm_unreachable("Could not find Decl ancestor.");
+}
+
std::string SyntaxTree::Impl::getNodeValue(NodeId Id) const {
return getNodeValue(getNode(Id));
}
TypePP.AnonymousTagLocations = false;
if (auto *V = dyn_cast<ValueDecl>(D)) {
- Value += V->getQualifiedNameAsString() + "(" +
- V->getType().getAsString(TypePP) + ")";
+ Value += getRelativeName(V) + "(" + V->getType().getAsString(TypePP) + ")";
if (auto *C = dyn_cast<CXXConstructorDecl>(D)) {
for (auto *Init : C->inits()) {
if (!Init->isWritten())
Value += C->getNameAsString();
} else {
assert(Init->isAnyMemberInitializer());
- Value += Init->getMember()->getQualifiedNameAsString();
+ Value += getRelativeName(Init->getMember());
}
Value += ",";
}
return Value;
}
if (auto *N = dyn_cast<NamedDecl>(D))
- Value += N->getQualifiedNameAsString() + ";";
+ Value += getRelativeName(N) + ";";
if (auto *T = dyn_cast<TypedefNameDecl>(D))
return Value + T->getUnderlyingType().getAsString(TypePP) + ";";
if (auto *T = dyn_cast<TypeDecl>(D))
if (auto *B = dyn_cast<BinaryOperator>(S))
return B->getOpcodeStr();
if (auto *M = dyn_cast<MemberExpr>(S))
- return M->getMemberDecl()->getQualifiedNameAsString();
+ return getRelativeName(M->getMemberDecl());
if (auto *I = dyn_cast<IntegerLiteral>(S)) {
SmallString<256> Str;
I->getValue().toString(Str, /*Radix=*/10, /*Signed=*/false);
return Str.str();
}
if (auto *D = dyn_cast<DeclRefExpr>(S))
- return D->getDecl()->getQualifiedNameAsString();
+ return getRelativeName(D->getDecl(), getEnclosingDeclContext(AST, S));
if (auto *String = dyn_cast<StringLiteral>(S))
return String->getString();
if (auto *B = dyn_cast<CXXBoolLiteralExpr>(S))
return DiffImpl->getMapped(SourceTree.TreeImpl, Id);
}
-SyntaxTree::SyntaxTree(const ASTContext &AST)
+SyntaxTree::SyntaxTree(ASTContext &AST)
: TreeImpl(llvm::make_unique<SyntaxTree::Impl>(
this, AST.getTranslationUnitDecl(), AST)) {}
// CHECK: {{^}} NamespaceDecl: test;(
namespace test {
-// CHECK: {{^}} FunctionDecl: test::f(
+// CHECK: {{^}} FunctionDecl: :f(
// CHECK: CompoundStmt(
void f() {
// CHECK: VarDecl: i(int)(
auto b = true;
// CHECK: CallExpr(
// CHECK-NOT: ImplicitCastExpr
- // CHECK: DeclRefExpr: test::f(
+ // CHECK: DeclRefExpr: :f(
f();
// CHECK: UnaryOperator: ++(
++i;
// CHECK: CXXRecordDecl: X;X;(
class X : Base {
int m;
- // CHECK: CXXMethodDecl: X::foo(const char *(int)
+ // CHECK: CXXMethodDecl: :foo(const char *(int)
// CHECK: ParmVarDecl: i(int)(
const char *foo(int i) {
if (i == 0)
int not_initialized;
// All initialized members, bases and delegating initializers are are
// appended, separated by commas.
- // CHECK: CXXConstructorDecl: X::X(void (char, int){{( __attribute__\(\(thiscall\)\))?}})Base,X::m,(
+ // CHECK: CXXConstructorDecl: :X(void (char, int){{( __attribute__\(\(thiscall\)\))?}})Base,:m,(
X(char c, int) : Base(), m(0) {
- // CHECK: MemberExpr: X::m(
+ // CHECK: MemberExpr: :m(
int x = m;
}
- // CHECK: CXXConstructorDecl: X::X(void (char){{( __attribute__\(\(thiscall\)\))?}})X,(
+ // CHECK: CXXConstructorDecl: :X(void (char){{( __attribute__\(\(thiscall\)\))?}})X,(
X(char s) : X(s, 4) {}
};