Question

While I'm visiting, let's say, a declaration (Decl in Clang library), how can I get the name of the file where this Decl has been written ?

There is a FileData class, but I can't find any other class allowing me to get this FileData

Was it helpful?

Solution

You can ask the SourceManager for the FileEntry of the current file.

For example in a matcher callback:

void MyMatcher::run(const MatchFinder::MatchResult& Result) {
    ASTContext* Context = Result.Context;
    if (const Decl* D = Result.Nodes.getNodeAs<Decl>("MyDecl")) {
        SourceManager& SrcMgr = Context->getSourceManager();
        const FileEntry* Entry = SrcMgr.getFileEntryForID(SrcMgr.getFileID(D.getCaretLocation()));
        const char* FileName = Entry->getName();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top