@@ -108,10 +108,19 @@ bool IsParentADefinition(const clang::Decl* decl) {
108108
109109const clang::ClassTemplateDecl* GetClassTemplateDefinition (
110110 const clang::ClassTemplateDecl* class_template_decl) {
111- if (class_template_decl->getTemplatedDecl ()->getDefinition ()) {
112- class_template_decl = class_template_decl->getTemplatedDecl ()
113- ->getDefinition ()
114- ->getDescribedClassTemplate ();
111+ if (!class_template_decl) {
112+ return nullptr ;
113+ }
114+ if (class_template_decl->getTemplatedDecl () &&
115+ class_template_decl->getTemplatedDecl ()->getDefinition ()) {
116+ const auto * definition =
117+ class_template_decl->getTemplatedDecl ()->getDefinition ();
118+ if (definition) {
119+ const auto * described = definition->getDescribedClassTemplate ();
120+ if (described) {
121+ return described;
122+ }
123+ }
115124 }
116125 return class_template_decl;
117126}
@@ -141,7 +150,13 @@ const clang::Decl* GetSpecializationDecl(
141150 const clang::ClassTemplateDecl* class_template_decl,
142151 const llvm::ArrayRef<clang::TemplateArgument> template_arguments,
143152 const clang::ASTContext& context) {
153+ if (!class_template_decl) {
154+ return nullptr ;
155+ }
144156 class_template_decl = GetClassTemplateDefinition (class_template_decl);
157+ if (!class_template_decl) {
158+ return nullptr ;
159+ }
145160 const clang::Decl* decl = class_template_decl;
146161 const auto * specialization_decl =
147162 FindSpecialization (class_template_decl, template_arguments, context);
@@ -280,8 +295,7 @@ const clang::NamedDecl* GetTemplatePrototypeNamedDecl(
280295 named_decl->getDeclContext ())) {
281296 if (const clang::FunctionDecl* instantiation_pattern =
282297 function_decl->getTemplateInstantiationPattern ()) {
283- template_context = instantiation_pattern;
284- template_decl = instantiation_pattern->getDescribedFunctionTemplate ();
298+ template_context = instantiation_pattern->getDefinition ();
285299 } else if (function_decl->getDescribedFunctionTemplate () &&
286300 function_decl->getDescribedFunctionTemplate ()
287301 ->getInstantiatedFromMemberTemplate ()) {
@@ -1046,13 +1060,21 @@ LocationId AstVisitor::GetLocationId(clang::SourceLocation start,
10461060}
10471061
10481062LocationId AstVisitor::GetLocationId (const clang::Decl* decl) {
1063+ if (!decl) {
1064+ return kInvalidLocationId ;
1065+ }
1066+
10491067 // If we have a template specialization or instantiation, we should make
10501068 // sure we use the source location that matches the closest explicit
10511069 // specialization instead of the base template.
10521070 if (llvm::isa<clang::ClassTemplateSpecializationDecl>(decl)) {
10531071 const auto * specialization_decl =
10541072 llvm::cast<clang::ClassTemplateSpecializationDecl>(decl);
1055- decl = GetSpecializationDecl (specialization_decl, context_);
1073+ const auto * resolved_decl =
1074+ GetSpecializationDecl (specialization_decl, context_);
1075+ if (resolved_decl) {
1076+ decl = resolved_decl;
1077+ }
10561078 }
10571079
10581080 // For class template definitions, the AST has two nodes:
@@ -1070,7 +1092,11 @@ LocationId AstVisitor::GetLocationId(const clang::Decl* decl) {
10701092 const auto * class_template_decl =
10711093 cxx_record_decl->getDescribedClassTemplate ();
10721094 if (class_template_decl) {
1073- decl = GetClassTemplateDefinition (class_template_decl);
1095+ const auto * resolved_template =
1096+ GetClassTemplateDefinition (class_template_decl);
1097+ if (resolved_template) {
1098+ decl = resolved_template;
1099+ }
10741100 }
10751101 }
10761102
@@ -1083,16 +1109,26 @@ LocationId AstVisitor::GetLocationId(const clang::Decl* decl) {
10831109 if (llvm::isa<clang::FunctionDecl>(decl)) {
10841110 const auto * function_decl = llvm::cast<clang::FunctionDecl>(decl);
10851111 if (function_decl->isTemplateInstantiation ()) {
1086- function_decl = function_decl->getTemplateInstantiationPattern ();
1112+ const auto * pattern = function_decl->getTemplateInstantiationPattern ();
1113+ if (pattern) {
1114+ function_decl = pattern;
1115+ if (function_decl->getDefinition ()) {
1116+ function_decl = function_decl->getDefinition ();
1117+ }
1118+ }
10871119 } else if (function_decl->getTemplateSpecializationInfo ()) {
10881120 const auto * tmp_info = function_decl->getTemplateSpecializationInfo ();
1089- function_decl = tmp_info->getFunction ();
1121+ if (tmp_info && tmp_info->getFunction ()) {
1122+ function_decl = tmp_info->getFunction ();
1123+ }
10901124 }
10911125
10921126 decl = function_decl;
1093- const auto * func_template = function_decl->getDescribedFunctionTemplate ();
1094- if (func_template) {
1095- decl = func_template;
1127+ if (function_decl) {
1128+ const auto * func_template = function_decl->getDescribedFunctionTemplate ();
1129+ if (func_template) {
1130+ decl = func_template;
1131+ }
10961132 }
10971133 }
10981134
@@ -1420,8 +1456,9 @@ std::optional<Entity> AstVisitor::GetEntityForDecl(const clang::Decl* decl,
14201456
14211457 // Check for template instantiation.
14221458 const clang::Decl* function_template = nullptr ;
1423- if (function_decl->getTemplateInstantiationPattern ()) {
1424- function_template = function_decl->getTemplateInstantiationPattern ();
1459+ if (const auto * instantiation_pattern =
1460+ function_decl->getTemplateInstantiationPattern ()) {
1461+ function_template = instantiation_pattern->getDefinition ();
14251462 } else if (function_decl->getDescribedFunctionTemplate () &&
14261463 function_decl->getDescribedFunctionTemplate ()
14271464 ->getInstantiatedFromMemberTemplate ()) {
0 commit comments