@@ -149,6 +149,8 @@ class SyntaxTree::Impl {
149149
150150 std::string getNodeValue (NodeId Id) const ;
151151 std::string getNodeValue (const Node &Node) const ;
152+ std::string getDeclValue (const Decl *D) const ;
153+ std::string getStmtValue (const Stmt *S) const ;
152154
153155private:
154156 // / Nodes in preorder.
@@ -268,9 +270,6 @@ struct PreorderVisitor : public RecursiveASTVisitor<PreorderVisitor> {
268270};
269271} // end anonymous namespace
270272
271- SyntaxTree::Impl::Impl (SyntaxTree *Parent, const ASTContext &AST)
272- : Impl(Parent, AST.getTranslationUnitDecl(), AST) {}
273-
274273SyntaxTree::Impl::Impl (SyntaxTree *Parent, Decl *N, const ASTContext &AST)
275274 : Parent(Parent), AST(AST) {
276275 NodeCountVisitor NodeCounter (*this );
@@ -372,49 +371,82 @@ std::string SyntaxTree::Impl::getNodeValue(NodeId Id) const {
372371
373372std::string SyntaxTree::Impl::getNodeValue (const Node &N) const {
374373 const DynTypedNode &DTN = N.ASTNode ;
375- if (auto *X = DTN.get <BinaryOperator>())
376- return X->getOpcodeStr ();
377- if (auto *X = DTN.get <AccessSpecDecl>()) {
378- CharSourceRange Range (X->getSourceRange (), false );
374+ if (auto *S = DTN.get <Stmt>())
375+ return getStmtValue (S);
376+ if (auto *D = DTN.get <Decl>())
377+ return getDeclValue (D);
378+ llvm_unreachable (" Fatal: unhandled AST node.\n " );
379+ }
380+
381+ std::string SyntaxTree::Impl::getDeclValue (const Decl *D) const {
382+ std::string Value;
383+ PrintingPolicy TypePP (AST.getLangOpts ());
384+ TypePP.AnonymousTagLocations = false ;
385+
386+ if (auto *V = dyn_cast<ValueDecl>(D)) {
387+ Value += V->getQualifiedNameAsString () + " (" +
388+ V->getType ().getAsString (TypePP) + " )" ;
389+ if (auto *C = dyn_cast<CXXConstructorDecl>(D)) {
390+ for (auto *Init : C->inits ()) {
391+ if (!Init->isWritten ())
392+ continue ;
393+ if (Init->isBaseInitializer ()) {
394+ Value += Init->getBaseClass ()->getCanonicalTypeInternal ().getAsString (
395+ TypePP);
396+ } else if (Init->isDelegatingInitializer ()) {
397+ Value += C->getNameAsString ();
398+ } else {
399+ assert (Init->isAnyMemberInitializer ());
400+ Value += Init->getMember ()->getQualifiedNameAsString ();
401+ }
402+ Value += " ," ;
403+ }
404+ }
405+ return Value;
406+ }
407+ if (auto *N = dyn_cast<NamedDecl>(D))
408+ Value += N->getQualifiedNameAsString () + " ;" ;
409+ if (auto *T = dyn_cast<TypedefNameDecl>(D))
410+ return Value + T->getUnderlyingType ().getAsString (TypePP) + " ;" ;
411+ if (auto *T = dyn_cast<TypeDecl>(D))
412+ if (T->getTypeForDecl ())
413+ Value +=
414+ T->getTypeForDecl ()->getCanonicalTypeInternal ().getAsString (TypePP) +
415+ " ;" ;
416+ if (auto *U = dyn_cast<UsingDirectiveDecl>(D))
417+ return U->getNominatedNamespace ()->getName ();
418+ if (auto *A = dyn_cast<AccessSpecDecl>(D)) {
419+ CharSourceRange Range (A->getSourceRange (), false );
379420 return Lexer::getSourceText (Range, AST.getSourceManager (),
380421 AST.getLangOpts ());
381422 }
382- if (auto *X = DTN.get <IntegerLiteral>()) {
423+ return Value;
424+ }
425+
426+ std::string SyntaxTree::Impl::getStmtValue (const Stmt *S) const {
427+ if (auto *U = dyn_cast<UnaryOperator>(S))
428+ return UnaryOperator::getOpcodeStr (U->getOpcode ());
429+ if (auto *B = dyn_cast<BinaryOperator>(S))
430+ return B->getOpcodeStr ();
431+ if (auto *M = dyn_cast<MemberExpr>(S))
432+ return M->getMemberDecl ()->getQualifiedNameAsString ();
433+ if (auto *I = dyn_cast<IntegerLiteral>(S)) {
383434 SmallString<256 > Str;
384- X ->getValue ().toString (Str, /* Radix=*/ 10 , /* Signed=*/ false );
435+ I ->getValue ().toString (Str, /* Radix=*/ 10 , /* Signed=*/ false );
385436 return Str.str ();
386437 }
387- if (auto *X = DTN.get <StringLiteral>())
388- return X->getString ();
389- if (auto *X = DTN.get <ValueDecl>())
390- return X->getNameAsString () + " (" + X->getType ().getAsString () + " )" ;
391- if (DTN.get <DeclStmt>() || DTN.get <TranslationUnitDecl>())
392- return " " ;
393- std::string Value;
394- if (auto *X = DTN.get <DeclRefExpr>()) {
395- if (X->hasQualifier ()) {
396- llvm::raw_string_ostream OS (Value);
397- PrintingPolicy PP (AST.getLangOpts ());
398- X->getQualifier ()->print (OS, PP);
399- }
400- Value += X->getDecl ()->getNameAsString ();
401- return Value;
438+ if (auto *F = dyn_cast<FloatingLiteral>(S)) {
439+ SmallString<256 > Str;
440+ F->getValue ().toString (Str);
441+ return Str.str ();
402442 }
403- if (auto *X = DTN.get <NamedDecl>())
404- Value += X->getNameAsString () + " ;" ;
405- if (auto *X = DTN.get <TypedefNameDecl>())
406- return Value + X->getUnderlyingType ().getAsString () + " ;" ;
407- if (DTN.get <NamespaceDecl>())
408- return Value;
409- if (auto *X = DTN.get <TypeDecl>())
410- if (X->getTypeForDecl ())
411- Value +=
412- X->getTypeForDecl ()->getCanonicalTypeInternal ().getAsString () + " ;" ;
413- if (DTN.get <Decl>())
414- return Value;
415- if (DTN.get <Stmt>())
416- return " " ;
417- llvm_unreachable (" Fatal: unhandled AST node.\n " );
443+ if (auto *D = dyn_cast<DeclRefExpr>(S))
444+ return D->getDecl ()->getQualifiedNameAsString ();
445+ if (auto *String = dyn_cast<StringLiteral>(S))
446+ return String->getString ();
447+ if (auto *B = dyn_cast<CXXBoolLiteralExpr>(S))
448+ return B->getValue () ? " true" : " false" ;
449+ return " " ;
418450}
419451
420452// / Identifies a node in a subtree by its postorder offset, starting at 1.
@@ -637,6 +669,22 @@ ast_type_traits::ASTNodeKind Node::getType() const {
637669
638670StringRef Node::getTypeLabel () const { return getType ().asStringRef (); }
639671
672+ llvm::Optional<std::string> Node::getQualifiedIdentifier () const {
673+ if (auto *ND = ASTNode.get <NamedDecl>()) {
674+ if (ND->getDeclName ().isIdentifier ())
675+ return ND->getQualifiedNameAsString ();
676+ }
677+ return llvm::None;
678+ }
679+
680+ llvm::Optional<StringRef> Node::getIdentifier () const {
681+ if (auto *ND = ASTNode.get <NamedDecl>()) {
682+ if (ND->getDeclName ().isIdentifier ())
683+ return ND->getName ();
684+ }
685+ return llvm::None;
686+ }
687+
640688namespace {
641689// Compares nodes by their depth.
642690struct HeightLess {
0 commit comments