-
Notifications
You must be signed in to change notification settings - Fork 13.7k
[Clang] Check for uninitialized use in lambda within CXXOperatorCallExpr #129198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,7 @@ | |
#include "llvm/ADT/STLForwardCompat.h" | ||
#include "llvm/ADT/SmallString.h" | ||
#include "llvm/ADT/StringExtras.h" | ||
#include "llvm/Support/SaveAndRestore.h" | ||
#include "llvm/TargetParser/Triple.h" | ||
#include <algorithm> | ||
#include <cstring> | ||
|
@@ -12597,6 +12598,7 @@ namespace { | |
bool isRecordType; | ||
bool isPODType; | ||
bool isReferenceType; | ||
bool isInCXXOperatorCall; | ||
|
||
bool isInitList; | ||
llvm::SmallVector<unsigned, 4> InitFieldIndex; | ||
|
@@ -12609,6 +12611,7 @@ namespace { | |
isPODType = false; | ||
isRecordType = false; | ||
isReferenceType = false; | ||
isInCXXOperatorCall = false; | ||
isInitList = false; | ||
if (ValueDecl *VD = dyn_cast<ValueDecl>(OrigDecl)) { | ||
isPODType = VD->getType().isPODType(S.Context); | ||
|
@@ -12796,6 +12799,7 @@ namespace { | |
} | ||
|
||
void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) { | ||
llvm::SaveAndRestore CxxOpCallScope(isInCXXOperatorCall, true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should be using bugprone-argument-comment style for all literal arguments like this. |
||
Expr *Callee = E->getCallee(); | ||
|
||
if (isa<UnresolvedLookupExpr>(Callee)) | ||
|
@@ -12806,6 +12810,19 @@ namespace { | |
HandleValue(Arg->IgnoreParenImpCasts()); | ||
} | ||
|
||
void VisitLambdaExpr(LambdaExpr *E) { | ||
if (!isInCXXOperatorCall) { | ||
Inherited::VisitLambdaExpr(E); | ||
return; | ||
} | ||
|
||
for (Expr *Init : E->capture_inits()) | ||
if (DeclRefExpr *DRE = dyn_cast_if_present<DeclRefExpr>(Init)) | ||
HandleDeclRefExpr(DRE); | ||
else if (Init) | ||
Visit(Init); | ||
} | ||
|
||
void VisitUnaryOperator(UnaryOperator *E) { | ||
// For POD record types, addresses of its own members are well-defined. | ||
if (E->getOpcode() == UO_AddrOf && isRecordType && | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -892,6 +892,11 @@ namespace lambdas { | |
return a1.x; | ||
}); | ||
A a2([&] { return a2.x; }); // ok | ||
A a3([=] { return a3.x; }()); // expected-warning{{variable 'a3' is uninitialized when used within its own initialization}} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we now also catch this case: https://godbolt.org/z/bnYEqhqY9 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think not, |
||
A a4([&] { return a4.x; }()); // expected-warning{{variable 'a4' is uninitialized when used within its own initialization}} | ||
A a5([&] { return a5; }()); // expected-warning{{variable 'a5' is uninitialized when used within its own initialization}} | ||
A a6([&] { return a5.x; }()); // ok | ||
A a7 = [&a7] { return a7; }(); // expected-warning{{variable 'a7' is uninitialized when used within its own initialization}} | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels like changing this to use in class member initializers would have been a big win.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I need to change all the other fields to this form as well, like
isPODType
,isRecordType
... ?