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)?
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.
Comments