CLang 3.5 LibTooling: getting variable filename in clang :: VarDecl

I have an object clang::VarDecl. I want to get the file name / location of the variable (at least if they are global). I also looked at the question: -

How to get location of variable name in clang :: VarDecl

But I think that this is not about the name of the file in which the variables are declared. I also mentioned

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

There is no function that can return a file name. Can someone tell me how to get it?

+1
source share
2 answers
+3

SourceManager. MatchFinder::MatchResult::Context ASTContext*, getSourceManager, SourceManager. - , .

class VarDeclPrinter : public MatchFinder::MatchCallback {
  public:

  virtual void run(const MatchFinder::MatchResult &Result) {

    SourceManager &srcMgr = Result.Context->getSourceManager();

    if(const VarDecl* var = Result.Nodes.getNodeAs<VarDecl>("var")) {
      if(var->isFunctionOrMethodVarDecl()) {
        cout << setw(20) << left << "Local Variable: " << var->getName().str() << "\t\t";
        cout << ((CXXMethodDecl*)(var->getParentFunctionOrMethod()))->getQualifiedNameAsString() << "\t";
        cout << "--" << srcMgr.getFilename(var->getLocation()).str();
        cout << "\n";
      }
      if(var->hasExternalStorage()) {
        cout << setw(20) << left << "External Variable: " << var->getName().str() << "\t\t";
        cout << "--" << srcMgr.getFilename(var->getLocation()).str();
        cout << "\n";
      }
      else if(var->hasGlobalStorage()) {
        cout << setw(20) << left << "Global Variable: " << var->getName().str() << "\t\t";
        cout << "--" << srcMgr.getFilename(var->getLocation()).str();
        cout << "\n";
      }
    }
  }
};

, @Oak.

+3

All Articles