Skip to content

Commit 15b7806

Browse files
committed
[clang] Fix the post-filtering heuristics for GSLPointer case.
1 parent c047a5b commit 15b7806

File tree

3 files changed

+139
-24
lines changed

3 files changed

+139
-24
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,8 @@ Improvements to Clang's diagnostics
614614

615615
- Clang now diagnoses ``[[deprecated]]`` attribute usage on local variables (#GH90073).
616616

617+
- Fix false positives when `[[gsl::Owner/Pointer]]` and `[[clang::lifetimebound]]` are used together.
618+
617619
- Improved diagnostic message for ``__builtin_bit_cast`` size mismatch (#GH115870).
618620

619621
- Clang now omits shadow warnings for enum constants in separate class scopes (#GH62588).

clang/lib/Sema/CheckExprLifetime.cpp

Lines changed: 92 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,87 @@ static bool pathOnlyHandlesGslPointer(const IndirectLocalPath &Path) {
11521152
}
11531153
return false;
11541154
}
1155+
// Result of analyzing the Path for GSLPointer.
1156+
enum AnalysisResult {
1157+
// Path does not correspond to a GSLPointer.
1158+
NotGSLPointer,
1159+
1160+
// A relevant case was identified.
1161+
Report,
1162+
// Stop the entire traversal.
1163+
Abandon,
1164+
// Skip this step and continue traversing inner AST nodes.
1165+
Skip,
1166+
};
1167+
// Analyze cases where a GSLPointer is initialized or assigned from a
1168+
// temporary owner object.
1169+
static AnalysisResult analyzePathForGSLPointer(const IndirectLocalPath &Path,
1170+
Local L) {
1171+
if (!pathOnlyHandlesGslPointer(Path))
1172+
return NotGSLPointer;
1173+
1174+
// At this point, Path represents a series of operations involving a
1175+
// GSLPointer, either in the process of initialization or assignment.
1176+
1177+
// Note: A LifetimeBoundCall can appear interleaved in this sequence.
1178+
// For example:
1179+
// const std::string& Ref(const std::string& a [[clang::lifetimebound]]);
1180+
// string_view abc = Ref(std::string());
1181+
// The "Path" is [GSLPointerInit, LifetimeboundCall], where "L" is the
1182+
// temporary "std::string()" object. We need to check if the function with the
1183+
// lifetimebound attribute returns a "owner" type.
1184+
if (Path.back().Kind == IndirectLocalPathEntry::LifetimeBoundCall) {
1185+
// The lifetimebound applies to the implicit object parameter of a method.
1186+
if (const auto *Method = llvm::dyn_cast<CXXMethodDecl>(Path.back().D)) {
1187+
if (Method->getReturnType()->isReferenceType() &&
1188+
isRecordWithAttr<OwnerAttr>(
1189+
Method->getReturnType()->getPointeeType()))
1190+
return Report;
1191+
return Abandon;
1192+
}
1193+
// The lifetimebound applies to a function parameter.
1194+
const auto *PD = llvm::dyn_cast<ParmVarDecl>(Path.back().D);
1195+
if (const auto *FD = llvm::dyn_cast<FunctionDecl>(PD->getDeclContext())) {
1196+
if (isa<CXXConstructorDecl>(FD)) {
1197+
// Constructor case: the parameter is annotated with lifetimebound
1198+
// e.g., GSLPointer(const S& s [[clang::lifetimebound]])
1199+
// We still respect this case even the type S is not an owner.
1200+
return Report;
1201+
}
1202+
// For regular functions, check if the return type has an Owner attribute.
1203+
// e.g., const GSLOwner& func(const Foo& foo [[clang::lifetimebound]])
1204+
if (FD->getReturnType()->isReferenceType() &&
1205+
isRecordWithAttr<OwnerAttr>(FD->getReturnType()->getPointeeType()))
1206+
return Report;
1207+
}
1208+
return Abandon;
1209+
}
1210+
1211+
if (isa<DeclRefExpr>(L)) {
1212+
// We do not want to follow the references when returning a pointer
1213+
// originating from a local owner to avoid the following false positive:
1214+
// int &p = *localUniquePtr;
1215+
// someContainer.add(std::move(localUniquePtr));
1216+
// return p;
1217+
if (!pathContainsInit(Path) && isRecordWithAttr<OwnerAttr>(L->getType()))
1218+
return Report;
1219+
return Abandon;
1220+
}
1221+
1222+
// The GSLPointer is from a temporary object.
1223+
auto *MTE = dyn_cast<MaterializeTemporaryExpr>(L);
1224+
1225+
bool IsGslPtrValueFromGslTempOwner =
1226+
MTE && !MTE->getExtendingDecl() &&
1227+
isRecordWithAttr<OwnerAttr>(MTE->getType());
1228+
// Skipping a chain of initializing gsl::Pointer annotated objects.
1229+
// We are looking only for the final source to find out if it was
1230+
// a local or temporary owner or the address of a local
1231+
// variable/param.
1232+
if (!IsGslPtrValueFromGslTempOwner)
1233+
return Skip;
1234+
return Report;
1235+
}
11551236

11561237
static bool isAssignmentOperatorLifetimeBound(CXXMethodDecl *CMD) {
11571238
return CMD && isNormalAssignmentOperator(CMD) && CMD->param_size() == 1 &&
@@ -1189,27 +1270,17 @@ checkExprLifetimeImpl(Sema &SemaRef, const InitializedEntity *InitEntity,
11891270

11901271
auto *MTE = dyn_cast<MaterializeTemporaryExpr>(L);
11911272

1192-
bool IsGslPtrValueFromGslTempOwner = false;
1193-
if (pathOnlyHandlesGslPointer(Path)) {
1194-
if (isa<DeclRefExpr>(L)) {
1195-
// We do not want to follow the references when returning a pointer
1196-
// originating from a local owner to avoid the following false positive:
1197-
// int &p = *localUniquePtr;
1198-
// someContainer.add(std::move(localUniquePtr));
1199-
// return p;
1200-
if (pathContainsInit(Path) ||
1201-
!isRecordWithAttr<OwnerAttr>(L->getType()))
1202-
return false;
1203-
} else {
1204-
IsGslPtrValueFromGslTempOwner =
1205-
MTE && !MTE->getExtendingDecl() &&
1206-
isRecordWithAttr<OwnerAttr>(MTE->getType());
1207-
// Skipping a chain of initializing gsl::Pointer annotated objects.
1208-
// We are looking only for the final source to find out if it was
1209-
// a local or temporary owner or the address of a local variable/param.
1210-
if (!IsGslPtrValueFromGslTempOwner)
1211-
return true;
1212-
}
1273+
bool IsGslPtrValueFromGslTempOwner = true;
1274+
switch (analyzePathForGSLPointer(Path, L)) {
1275+
case Abandon:
1276+
return false;
1277+
case Skip:
1278+
return true;
1279+
case NotGSLPointer:
1280+
IsGslPtrValueFromGslTempOwner = false;
1281+
LLVM_FALLTHROUGH;
1282+
case Report:
1283+
break;
12131284
}
12141285

12151286
switch (LK) {

clang/test/Sema/warn-lifetime-analysis-nocfg.cpp

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -604,8 +604,9 @@ struct [[gsl::Pointer]] Span {
604604

605605
// Pointer from Owner<Pointer>
606606
std::string_view test5() {
607-
std::string_view a = StatusOr<std::string_view>().valueLB(); // expected-warning {{object backing the pointer will be dest}}
608-
return StatusOr<std::string_view>().valueLB(); // expected-warning {{returning address of local temporary}}
607+
// The Owner<Pointer> doesn't own the object which its inner pointer points to.
608+
std::string_view a = StatusOr<std::string_view>().valueLB(); // OK
609+
return StatusOr<std::string_view>().valueLB(); // OK
609610

610611
// No dangling diagnostics on non-lifetimebound methods.
611612
std::string_view b = StatusOr<std::string_view>().valueNoLB();
@@ -652,7 +653,7 @@ Span<std::string> test10(StatusOr<std::vector<std::string>> aa) {
652653

653654
// Pointer<Owner>> from Owner<Pointer<Owner>>
654655
Span<std::string> test11(StatusOr<Span<std::string>> aa) {
655-
return aa.valueLB(); // expected-warning {{address of stack memory}}
656+
return aa.valueLB(); // OK
656657
return aa.valueNoLB(); // OK.
657658
}
658659

@@ -693,3 +694,44 @@ void test() {
693694
auto y = std::set<int>{}.begin(); // expected-warning {{object backing the pointer}}
694695
}
695696
} // namespace GH118064
697+
698+
namespace LifetimeboundInterleave {
699+
700+
const std::string& Ref(const std::string& abc [[clang::lifetimebound]]);
701+
std::string_view test1() {
702+
std::string_view t1 = Ref(std::string()); // expected-warning {{object backing}}
703+
t1 = Ref(std::string()); // expected-warning {{object backing}}
704+
return Ref(std::string()); // expected-warning {{returning address}}
705+
}
706+
707+
template <typename T>
708+
struct Foo {
709+
const T& get() const [[clang::lifetimebound]];
710+
const T& getNoLB() const;
711+
};
712+
std::string_view test2(Foo<std::string> r1, Foo<std::string_view> r2) {
713+
std::string_view t1 = Foo<std::string>().get(); // expected-warning {{object backing}}
714+
t1 = Foo<std::string>().get(); // expected-warning {{object backing}}
715+
return r1.get(); // expected-warning {{address of stack}}
716+
717+
std::string_view t2 = Foo<std::string_view>().get();
718+
t2 = Foo<std::string_view>().get();
719+
return r2.get();
720+
721+
// no warning on no-LB-annotated method.
722+
std::string_view t3 = Foo<std::string>().getNoLB();
723+
t3 = Foo<std::string>().getNoLB();
724+
return r1.getNoLB();
725+
}
726+
727+
struct Bar {};
728+
struct [[gsl::Pointer]] Pointer {
729+
Pointer(const Bar & bar [[clang::lifetimebound]]);
730+
};
731+
Pointer test3(Bar bar) {
732+
Pointer p = Pointer(Bar()); // expected-warning {{temporary}}
733+
p = Pointer(Bar()); // expected-warning {{object backing}}
734+
return bar; // expected-warning {{address of stack}}
735+
}
736+
737+
} // namespace LifetimeboundInterleave

0 commit comments

Comments
 (0)