2

I am working on CLang 3.5. I am trying to fetch info about variables declared in a C++ project.

How can I fetch datatype or qualified class name of a variable in a clang::VarDecl, clang::FieldDecl or clang::ParmVarDecl object? I tried to find a function which can return datatype or class name of the variable in doc for clang::VarDecl provided here.

http://clang.llvm.org/doxygen/classclang_1_1VarDecl.html

I also tried to look into the code of $LLVM/tools/clang/tools/clang-check/ClangCheck.cpp because on passing cmd arg --ast-dump it shows all of the information about every AST node including all of the variables declared. I wonder how to access all of that information.

I am using ASTMatchers to find my variable declarations, those are:

fieldDecl().bind("field")
parmVarDecl().bind("param")
varDecl().bind("var")

Can anybody please tell me how can I get datatype of all of the variables delcared?

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
Prakhar Mishra
  • 1,586
  • 4
  • 28
  • 52
  • Should I add more details to it? If I am asking something very obvious, please provide me some links to relevant tutorials. Or at least tell me from which class to start with if I want to understand parsing and accessing AST in CLang source code. – Prakhar Mishra Dec 25 '13 at 13:56
  • Try looking at llvm/tools/clang/unittests/AST and take a look at the code dealing with traversing an AST. I personally found this link very helpful: http://clang.llvm.org/docs/LibASTMatchersTutorial.html – Konrad Kleine Mar 06 '14 at 12:38

1 Answers1

2

Recently, I'm learning Clang and I've wrote some codes after reading this question. It might help you.

Full source code is available in github.com (see ex04.cc)

DeclarationMatcher FieldDeclMatcher =
    clang::ast_matchers::fieldDecl().bind("field_decl");

class LoopPrinter : public MatchFinder::MatchCallback
{
public :
    virtual void run(const MatchFinder::MatchResult& result)
    {
        if (const clang::FieldDecl* fd
            = result.Nodes.getNodeAs<clang::FieldDecl>("field_decl"))
        {
            std::cout << "======== FieldDecl found ======" << std::endl;

            const clang::RecordDecl* rd = fd->getParent();
            const clang::QualType qt = fd->getType();
            const clang::Type* t = qt.getTypePtr();

            std::cout << "FieldDecl found '"
                      << fd->getQualifiedNameAsString() << " "
                      << fd->getName().str() << "' in '"
                      << rd->getName().str() << "'. "
                      << "is Builtintype = " << t->isBuiltinType() << " "
                      << std::endl << std::endl;
        }

    } // end of run()
};
Jason Heo
  • 9,956
  • 2
  • 36
  • 64
  • could you let me know what is the best method to retrieve the init value for a `ParmVarDecl`? `PVD->getInit()` or `PVD->evaluateValue()` returns NULL and thus a seg fault for me. Full question here: https://stackoverflow.com/questions/64050621/matching-the-argument-passed-to-a-function-in-a-forstmts-cond-using-clang-ast-v – Amir Oct 01 '20 at 15:07