Make the lifetime code not crash on non-type template parameters
PiperOrigin-RevId: 470818155
diff --git a/lifetime_annotations/lifetime_annotations_test.cc b/lifetime_annotations/lifetime_annotations_test.cc
index 1b467db..d634197 100644
--- a/lifetime_annotations/lifetime_annotations_test.cc
+++ b/lifetime_annotations/lifetime_annotations_test.cc
@@ -220,6 +220,16 @@
IsOkAndHolds(LifetimesAre({{"f", "a -> a"}, {"g", "a -> a"}})));
}
+TEST_F(LifetimeAnnotationsTest, LifetimeElision_TemplatesWithConstant) {
+ EXPECT_THAT(GetNamedLifetimeAnnotations(R"(
+ #pragma clang lifetime_elision
+ template <class T, bool B> class vector {};
+ int* f(vector<int *, true>);
+ vector<int*, false> g(int *);
+ )"),
+ IsOkAndHolds(LifetimesAre({{"f", "a -> a"}, {"g", "a -> a"}})));
+}
+
TEST_F(LifetimeAnnotationsTest, LifetimeElision_NestedTemplates) {
EXPECT_THAT(GetNamedLifetimeAnnotations(R"(
#pragma clang lifetime_elision
diff --git a/lifetime_annotations/type_lifetimes.cc b/lifetime_annotations/type_lifetimes.cc
index fcb1b01..3d83ae9 100644
--- a/lifetime_annotations/type_lifetimes.cc
+++ b/lifetime_annotations/type_lifetimes.cc
@@ -674,9 +674,12 @@
args.push_back({});
for (unsigned i = 0; i < template_specialization_type_loc.getNumArgs();
++i) {
- args.back().push_back(template_specialization_type_loc.getArgLoc(i)
- .getTypeSourceInfo()
- ->getTypeLoc());
+ if (auto* source_info = template_specialization_type_loc.getArgLoc(i)
+ .getTypeSourceInfo()) {
+ args.back().push_back(source_info->getTypeLoc());
+ } else {
+ args.back().push_back(clang::TypeLoc());
+ }
}
} else if (auto record_type_loc = type_loc.getAs<clang::RecordTypeLoc>()) {
if (auto specialization_decl =