Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The SourceLocation of the decl node related to macro extension is invalid. How to obtain the file and line number information according to the decl node? #122268

Open
yaqianp opened this issue Jan 9, 2025 · 2 comments
Labels
clang:as-a-library libclang and C++ API question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

Comments

@yaqianp
Copy link

yaqianp commented Jan 9, 2025

example:
Image

bool ClangASTVisitor::TraverseDecl(clang::Decl* decl)
{
    if (decl == nullptr) {
        return true;
    }
    if (auto* cxxRecordDecl = llvm::dyn_cast<clang::CXXRecordDecl>(decl);
        IsTemplateCXXRecordUninstantiated(cxxRecordDecl)) {
        return true;
    }
    const std::string& currFilename =
        m_astContext->getSourceManager().getFilename(decl->getEndLoc()).str();

    if (decl->getBeginLoc().isInvalid()) {
     // How to obtain the file and line number information according to the decl node?
   }

}
@tbaederr tbaederr added question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead! clang:as-a-library libclang and C++ API and removed new issue labels Jan 9, 2025
@DeinAlptraum
Copy link
Contributor

DeinAlptraum commented Jan 9, 2025

Hi, I'm not entirely sure I understood your question correctly, but when using libclang, you're processing an AST representation of the code, which has already passed through the preprocessor, i.e. all macros have already been expanded. So you'll never see e.g. AAA, just struct M {int a;};

@yaqianp
Copy link
Author

yaqianp commented Jan 10, 2025

Hi, I'm not entirely sure I understood your question correctly, but when using libclang, you're processing an AST representation of the code, which has already passed through the preprocessor, i.e. all macros have already been expanded. So you'll never see e.g. AAA, just struct M {int a;};

Appreciate your reply, It is worth noting that first I use libtooling, and then after the Clang AST is generated, you can view the source code information through dump. It's an oolong incident. I've found a solution. First, SourceLocation is valid. Then, use the isMacroID interface to determine whether SourceLocation is a macro ID. If yes, i can use the getExpansionLoc interface to obtain the source code information.

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:as-a-library libclang and C++ API question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!
Projects
None yet
Development

No branches or pull requests

4 participants