Get a VarDecl Associated with a SourceLocation

Hi,

Is it possible to get the VarDecl associated with a specific SourceLocation?
For example, VarDecl is “int a;”. However, I have the SourceLocation for a. I want to get the SourceLocation that indicates the beginning (and end) of this VarDecl.

Hi Pardis,

You can use some of these as a basis for your feature.

  1. Use libTooling - clang::tooling::getNamedDeclAt().
  2. Use libclang (C interface) - clang_getCursor().
  3. Use clangd via LSP - textDocument/SymbolInfo method.
  4. Use clangd - clang::clangd::getSymbolInfo().
  5. Take a look at implementation of these and write your own AST visitor.

Not knowing anything about your specific use-case I’d recommend to use libTooling.

Hope this helps!

Jan