Skip to content

Commit ab30f17

Browse files
committed
address comments
1 parent f349aa8 commit ab30f17

File tree

4 files changed

+70
-65
lines changed

4 files changed

+70
-65
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10135,7 +10135,8 @@ def err_lifetimebound_parameter_void_return_type : Error<
1013510135
"that returns void; did you mean 'lifetime_capture_by(X)'">;
1013610136
def err_lifetimebound_implicit_object_parameter_void_return_type : Error<
1013710137
"'lifetimebound' attribute cannot be applied to an implicit object "
10138-
"parameter of a function that returns void; did you mean 'lifetime_capture_by(X)'">;
10138+
"parameter of a function that returns void; "
10139+
"did you mean 'lifetime_capture_by(X)'">;
1013910140

1014010141
// CHECK: returning address/reference of stack memory
1014110142
def warn_ret_stack_addr_ref : Warning<
@@ -10231,8 +10232,11 @@ def warn_dangling_pointer_assignment : Warning<
1023110232
"will be destroyed at the end of the full-expression">,
1023210233
InGroup<DanglingAssignment>;
1023310234
def warn_dangling_reference_captured : Warning<
10234-
"object whose reference is captured%select{| by '%1'}0 will be destroyed at the end of the full-expression">,
10235-
InGroup<DanglingCapture>;
10235+
"object whose reference is captured by '%0' will be destroyed at the end of "
10236+
"the full-expression">, InGroup<DanglingCapture>;
10237+
def warn_dangling_reference_captured_by_unknown : Warning<
10238+
"object whose reference is captured will be destroyed at the end of "
10239+
"the full-expression">, InGroup<DanglingCapture>;
1023610240

1023710241
// For non-floating point, expressions of the form x == x or x != x
1023810242
// should result in a warning, since these always evaluate to a constant.

clang/lib/Sema/CheckExprLifetime.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,17 +1208,19 @@ checkExprLifetimeImpl(Sema &SemaRef, const InitializedEntity *InitEntity,
12081208
}
12091209

12101210
case LK_LifetimeCapture: {
1211+
// The captured entity has lifetime beyond the full-expression,
1212+
// and the capturing entity does too, so don't warn.
12111213
if (!MTE)
12121214
return false;
12131215
assert(shouldLifetimeExtendThroughPath(Path) ==
12141216
PathLifetimeKind::NoExtend &&
12151217
"No lifetime extension in function calls");
1216-
if (CapEntity->Entity != nullptr)
1218+
if (CapEntity->Entity)
12171219
SemaRef.Diag(DiagLoc, diag::warn_dangling_reference_captured)
1218-
<< true << CapEntity->Entity << DiagRange;
1220+
<< CapEntity->Entity << DiagRange;
12191221
else
1220-
SemaRef.Diag(DiagLoc, diag::warn_dangling_reference_captured)
1221-
<< false << "<ignore>" << DiagRange;
1222+
SemaRef.Diag(DiagLoc, diag::warn_dangling_reference_captured_by_unknown)
1223+
<< DiagRange;
12221224
return false;
12231225
}
12241226

@@ -1471,13 +1473,13 @@ void checkExprLifetime(Sema &SemaRef, const InitializedEntity &Entity,
14711473
LifetimeKind LK = LTResult.getInt();
14721474
const InitializedEntity *ExtendingEntity = LTResult.getPointer();
14731475
checkExprLifetimeImpl(SemaRef, &Entity, ExtendingEntity, LK,
1474-
/*AEntity*/ nullptr, /*CapEntity=*/nullptr, Init);
1476+
/*AEntity=*/nullptr, /*CapEntity=*/nullptr, Init);
14751477
}
14761478

14771479
void checkExprLifetimeMustTailArg(Sema &SemaRef,
14781480
const InitializedEntity &Entity, Expr *Init) {
14791481
checkExprLifetimeImpl(SemaRef, &Entity, nullptr, LK_MustTail,
1480-
/*AEntity*/ nullptr, /*CapEntity=*/nullptr, Init);
1482+
/*AEntity=*/nullptr, /*CapEntity=*/nullptr, Init);
14811483
}
14821484

14831485
void checkExprLifetime(Sema &SemaRef, const AssignedEntity &Entity,

clang/lib/Sema/SemaChecking.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "clang/AST/ASTContext.h"
1717
#include "clang/AST/Attr.h"
1818
#include "clang/AST/AttrIterator.h"
19-
#include "clang/AST/Attrs.inc"
2019
#include "clang/AST/CharUnits.h"
2120
#include "clang/AST/Decl.h"
2221
#include "clang/AST/DeclBase.h"

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

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -808,38 +808,38 @@ void test13() {
808808
} // namespace GH100526
809809

810810
namespace lifetime_capture_by {
811-
struct S {
811+
struct X {
812812
const int *x;
813-
void captureInt(const int&x [[clang::lifetime_capture_by(this)]]) { this->x = &x; }
813+
void captureInt(const int& x [[clang::lifetime_capture_by(this)]]) { this->x = &x; }
814814
void captureSV(std::string_view sv [[clang::lifetime_capture_by(this)]]);
815815
};
816816
///////////////////////////
817817
// Detect dangling cases.
818818
///////////////////////////
819-
void captureInt(const int&x [[clang::lifetime_capture_by(s)]], S&s);
820-
void captureRValInt(int&&x [[clang::lifetime_capture_by(s)]], S&s);
821-
void noCaptureInt(int x [[clang::lifetime_capture_by(s)]], S&s);
819+
void captureInt(const int& i [[clang::lifetime_capture_by(x)]], X& x);
820+
void captureRValInt(int&& i [[clang::lifetime_capture_by(x)]], X& x);
821+
void noCaptureInt(int i [[clang::lifetime_capture_by(x)]], X& x);
822822

823823
std::string_view substr(const std::string& s [[clang::lifetimebound]]);
824824
std::string_view strcopy(const std::string& s);
825825

826-
void captureSV(std::string_view x [[clang::lifetime_capture_by(s)]], S&s);
827-
void captureRValSV(std::string_view&& x [[clang::lifetime_capture_by(s)]], S&s);
828-
void noCaptureSV(std::string_view x, S&s);
829-
void captureS(const std::string& x [[clang::lifetime_capture_by(s)]], S&s);
830-
void captureRValS(std::string&& x [[clang::lifetime_capture_by(s)]], S&s);
826+
void captureSV(std::string_view s [[clang::lifetime_capture_by(x)]], X& x);
827+
void captureRValSV(std::string_view&& sv [[clang::lifetime_capture_by(x)]], X& x);
828+
void noCaptureSV(std::string_view sv, X& x);
829+
void captureS(const std::string& s [[clang::lifetime_capture_by(x)]], X& x);
830+
void captureRValS(std::string&& s [[clang::lifetime_capture_by(x)]], X& x);
831831

832832
const std::string& getLB(const std::string& s[[clang::lifetimebound]]);
833833
const std::string& getLB(std::string_view sv[[clang::lifetimebound]]);
834834
const std::string* getPointerLB(const std::string& s[[clang::lifetimebound]]);
835835
const std::string* getPointerNoLB(const std::string& s);
836836

837-
void capturePointer(const std::string* x [[clang::lifetime_capture_by(s)]], S&s);
837+
void capturePointer(const std::string* sp [[clang::lifetime_capture_by(x)]], X& x);
838838

839839
struct ThisIsCaptured {
840-
void capture(S& s) [[clang::lifetime_capture_by(s)]];
841-
void bar(S& s) [[clang::lifetime_capture_by(abcd)]]; // expected-error {{'lifetime_capture_by' attribute argument 'abcd' is not a known function parameter}}
842-
void baz(S& s) [[clang::lifetime_capture_by(this)]]; // expected-error {{'lifetime_capture_by' argument references itself}}
840+
void capture(X& x) [[clang::lifetime_capture_by(x)]];
841+
void bar(X& x) [[clang::lifetime_capture_by(abcd)]]; // expected-error {{'lifetime_capture_by' attribute argument 'abcd' is not a known function parameter}}
842+
void baz(X& x) [[clang::lifetime_capture_by(this)]]; // expected-error {{'lifetime_capture_by' argument references itself}}
843843
};
844844

845845
void captureByGlobal(std::string_view s [[clang::lifetime_capture_by(global)]]);
@@ -848,63 +848,63 @@ void captureByUnknown(std::string_view s [[clang::lifetime_capture_by(unknown)]]
848848
void use() {
849849
std::string_view local_sv;
850850
std::string local_s;
851-
S s;
851+
X x;
852852
// Capture an 'int'.
853853
int local;
854-
captureInt(1, // expected-warning {{object whose reference is captured by 's' will be destroyed at the end of the full-expression}}
855-
s);
856-
captureRValInt(1, s); // expected-warning {{object whose reference is captured by 's'}}
857-
captureInt(local, s);
858-
noCaptureInt(1, s);
859-
noCaptureInt(local, s);
854+
captureInt(1, // expected-warning {{object whose reference is captured by 'x' will be destroyed at the end of the full-expression}}
855+
x);
856+
captureRValInt(1, x); // expected-warning {{object whose reference is captured by 'x'}}
857+
captureInt(local, x);
858+
noCaptureInt(1, x);
859+
noCaptureInt(local, x);
860860

861861
// Capture using std::string_view.
862-
captureSV(local_sv, s);
863-
captureSV(std::string(), // expected-warning {{object whose reference is captured by 's'}}
864-
s);
862+
captureSV(local_sv, x);
863+
captureSV(std::string(), // expected-warning {{object whose reference is captured by 'x'}}
864+
x);
865865
captureSV(substr(
866-
std::string() // expected-warning {{object whose reference is captured by 's'}}
867-
), s);
868-
captureSV(substr(local_s), s);
869-
captureSV(strcopy(std::string()), s);
870-
captureRValSV(std::move(local_sv), s);
871-
captureRValSV(std::string(), s); // expected-warning {{object whose reference is captured by 's'}}
872-
captureRValSV(std::string_view{"abcd"}, s);
873-
captureRValSV(substr(local_s), s);
874-
captureRValSV(substr(std::string()), s); // expected-warning {{object whose reference is captured by 's'}}
875-
captureRValSV(strcopy(std::string()), s);
876-
noCaptureSV(local_sv, s);
877-
noCaptureSV(std::string(), s);
878-
noCaptureSV(substr(std::string()), s);
866+
std::string() // expected-warning {{object whose reference is captured by 'x'}}
867+
), x);
868+
captureSV(substr(local_s), x);
869+
captureSV(strcopy(std::string()), x);
870+
captureRValSV(std::move(local_sv), x);
871+
captureRValSV(std::string(), x); // expected-warning {{object whose reference is captured by 'x'}}
872+
captureRValSV(std::string_view{"abcd"}, x);
873+
captureRValSV(substr(local_s), x);
874+
captureRValSV(substr(std::string()), x); // expected-warning {{object whose reference is captured by 'x'}}
875+
captureRValSV(strcopy(std::string()), x);
876+
noCaptureSV(local_sv, x);
877+
noCaptureSV(std::string(), x);
878+
noCaptureSV(substr(std::string()), x);
879879

880880
// Capture using std::string.
881-
captureS(std::string(), s); // expected-warning {{object whose reference is captured by 's'}}
882-
captureS(local_s, s);
883-
captureRValS(std::move(local_s), s);
884-
captureRValS(std::string(), s); // expected-warning {{object whose reference is captured by 's'}}
881+
captureS(std::string(), x); // expected-warning {{object whose reference is captured by 'x'}}
882+
captureS(local_s, x);
883+
captureRValS(std::move(local_s), x);
884+
captureRValS(std::string(), x); // expected-warning {{object whose reference is captured by 'x'}}
885885

886886
// Capture with lifetimebound.
887-
captureSV(getLB(std::string()), s); // expected-warning {{object whose reference is captured by 's'}}
888-
captureSV(getLB(substr(std::string())), s); // expected-warning {{object whose reference is captured by 's'}}
887+
captureSV(getLB(std::string()), x); // expected-warning {{object whose reference is captured by 'x'}}
888+
captureSV(getLB(substr(std::string())), x); // expected-warning {{object whose reference is captured by 'x'}}
889889
captureSV(getLB(getLB(
890-
std::string() // expected-warning {{object whose reference is captured by 's'}}
891-
)), s);
892-
capturePointer(getPointerLB(std::string()), s); // expected-warning {{object whose reference is captured by 's'}}
890+
std::string() // expected-warning {{object whose reference is captured by 'x'}}
891+
)), x);
892+
capturePointer(getPointerLB(std::string()), x); // expected-warning {{object whose reference is captured by 'x'}}
893893
capturePointer(getPointerLB(*getPointerLB(
894-
std::string() // expected-warning {{object whose reference is captured by 's'}}
895-
)), s);
896-
capturePointer(getPointerNoLB(std::string()), s);
894+
std::string() // expected-warning {{object whose reference is captured by 'x'}}
895+
)), x);
896+
capturePointer(getPointerNoLB(std::string()), x);
897897

898898
// Member functions.
899-
s.captureInt(1); // expected-warning {{object whose reference is captured by 's'}}
900-
s.captureSV(std::string()); // expected-warning {{object whose reference is captured by 's'}}
901-
s.captureSV(substr(std::string())); // expected-warning {{object whose reference is captured by 's'}}
902-
s.captureSV(strcopy(std::string()));
899+
x.captureInt(1); // expected-warning {{object whose reference is captured by 'x'}}
900+
x.captureSV(std::string()); // expected-warning {{object whose reference is captured by 'x'}}
901+
x.captureSV(substr(std::string())); // expected-warning {{object whose reference is captured by 'x'}}
902+
x.captureSV(strcopy(std::string()));
903903

904904
// 'this' is captured.
905-
ThisIsCaptured{}.capture(s); // expected-warning {{object whose reference is captured by 's'}}
905+
ThisIsCaptured{}.capture(x); // expected-warning {{object whose reference is captured by 'x'}}
906906
ThisIsCaptured TIS;
907-
TIS.capture(s);
907+
TIS.capture(x);
908908

909909
// capture by global.
910910
captureByGlobal(std::string()); // expected-warning {{object whose reference is captured will be destroyed at the end of the full-expression}}

0 commit comments

Comments
 (0)