How do I find the SourceLocation of the type definition of a VarDecl using the Clang AST?

srujzs

I'm trying to determine if a type is defined within the file or is included in another file (either system header or otherwise), and I can't seem to find an easy way to find the location of a type of a variable. The closest I can get is using TypeLoc's methods, but those provide only the location of the variable declaration (weirdly enough). ASTContext and SourceManager classes don't seem to provide much help either. Any ideas on how this can be done (I'm sure there has to be a way)?

srujzs

This can be accomplished by typecasting or using the getAs() method to cast a Type class to something that has references to a type declaration.

For example,

// Get Type of variable declaration
const clang::Type* type = var_decl->getTypeSourceInfo()->getTypeLoc().getTypePtr();

// Cast it as its appropriate subtype and then obtain its declaration
// For example, if the type was a record, you can cast it as a 
// RecordType and then obtain its definition.
const clang::RecordType* record_type = type->getAs<clang::RecordType>();
if (record_type != nullptr) {
  const clang::RecordDecl* type_decl = record_type->getDecl();

  clang::SourceLocation end_of_type_decl = type_decl->getLocEnd();
}

Hope this helps!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I find the SourceLocation of a character after a SourceLocation in a Clang AST?

How do I find the SourceLocation of the commas between function arguments using libtooling?

How do I get clang to dump the AST without color?

How to find SourceLocation of the outer template parameter list of a class template member function definition?

Using Jest I get: Cannot find type definition file for 'jasmine'

How do you match a C++ template function call using Clang AST matchers?

How do I view the definition of a type using Visual Studio Code's plugin for Elm?

How do I get AST as string using libclang?

How can I find the definition of a built-in type like `RegExpExecArray`?

How to view Clang AST?

Clang AST Matchers: how to find function body from a function declaration?

How do I find the definition of a Power BI Data Selected Event

How do I find the definition point of an operand in LLVM?

How do I write the type definition for a module with a default export

How do I get the typeparam names from a generic type definition?

How do I create a TypeScript type definition for a stateless React component?

How do I use a type definition that is not exposed by the library but is implemented in the typing?

How to find scriptmethod using AST in powershell?

How do I find implementations of a function type?

How do I link Core frameworks on macOS using Clang?

how do I jump to a function definition in emacs when using slime?

How do I build a Java type object at runtime from a generic type definition and runtime type parameters?

How do I specify a type variable used in an inline type, is the same as a type variable used in a function definition?

How to parse AST openMP with clang

How do I get to the bottom of an AST Expression

How do I cache hash codes for an AST?

clang python bindings: how to find type of a variable

Any way to find the function of a VarDecl?

How do I find all xml elements of a specific type using xpath?