-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang][dataflow] Correctly treat empty initializer lists for unions. #82986
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
cd64b0e
607c26e
5c3c1da
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 |
---|---|---|
|
@@ -2392,14 +2392,92 @@ TEST(TransferTest, InitListExprAsUnion) { | |
} F; | ||
|
||
public: | ||
constexpr target() : F{nullptr} {} | ||
constexpr target() : F{nullptr} { | ||
int *null = nullptr; | ||
F.b; // Make sure we reference 'b' so it is modeled. | ||
// [[p]] | ||
} | ||
}; | ||
)cc"; | ||
runDataflow( | ||
Code, | ||
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results, | ||
ASTContext &ASTCtx) { | ||
// Just verify that it doesn't crash. | ||
const Environment &Env = getEnvironmentAtAnnotation(Results, "p"); | ||
|
||
auto &FLoc = getFieldLoc<RecordStorageLocation>( | ||
*Env.getThisPointeeStorageLocation(), "F", ASTCtx); | ||
auto *AVal = cast<PointerValue>(getFieldValue(&FLoc, "a", ASTCtx, Env)); | ||
EXPECT_EQ(AVal, &getValueForDecl<PointerValue>(ASTCtx, Env, "null")); | ||
EXPECT_EQ(getFieldValue(&FLoc, "b", ASTCtx, Env), nullptr); | ||
}); | ||
} | ||
|
||
TEST(TransferTest, EmptyInitListExprForUnion) { | ||
// This is a crash repro. | ||
std::string Code = R"cc( | ||
class target { | ||
union { | ||
int *a; | ||
bool *b; | ||
} F; | ||
|
||
public: | ||
// Empty initializer list means that `F` is aggregate-initialized. | ||
// For a union, this has the effect that the first member of the union | ||
// is copy-initialized from an empty initializer list; in this specific | ||
// case, this has the effect of initializing `a` with null. | ||
constexpr target() : F{} { | ||
int *null = nullptr; | ||
F.b; // Make sure we reference 'b' so it is modeled. | ||
// [[p]] | ||
} | ||
}; | ||
)cc"; | ||
runDataflow( | ||
Code, | ||
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results, | ||
ASTContext &ASTCtx) { | ||
const Environment &Env = getEnvironmentAtAnnotation(Results, "p"); | ||
|
||
auto &FLoc = getFieldLoc<RecordStorageLocation>( | ||
*Env.getThisPointeeStorageLocation(), "F", ASTCtx); | ||
auto *AVal = cast<PointerValue>(getFieldValue(&FLoc, "a", ASTCtx, Env)); | ||
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. Why is 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've added a comment explaining what the language rules are that govern the behavior we expect here. |
||
EXPECT_EQ(AVal, &getValueForDecl<PointerValue>(ASTCtx, Env, "null")); | ||
EXPECT_EQ(getFieldValue(&FLoc, "b", ASTCtx, Env), nullptr); | ||
}); | ||
} | ||
|
||
TEST(TransferTest, EmptyInitListExprForStruct) { | ||
std::string Code = R"cc( | ||
class target { | ||
struct { | ||
int *a; | ||
bool *b; | ||
} F; | ||
|
||
public: | ||
constexpr target() : F{} { | ||
int *NullIntPtr = nullptr; | ||
bool *NullBoolPtr = nullptr; | ||
// [[p]] | ||
} | ||
}; | ||
)cc"; | ||
runDataflow( | ||
Code, | ||
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results, | ||
ASTContext &ASTCtx) { | ||
const Environment &Env = getEnvironmentAtAnnotation(Results, "p"); | ||
|
||
auto &FLoc = getFieldLoc<RecordStorageLocation>( | ||
*Env.getThisPointeeStorageLocation(), "F", ASTCtx); | ||
auto *AVal = cast<PointerValue>(getFieldValue(&FLoc, "a", ASTCtx, Env)); | ||
EXPECT_EQ(AVal, | ||
&getValueForDecl<PointerValue>(ASTCtx, Env, "NullIntPtr")); | ||
auto *BVal = cast<PointerValue>(getFieldValue(&FLoc, "b", ASTCtx, Env)); | ||
EXPECT_EQ(BVal, | ||
&getValueForDecl<PointerValue>(ASTCtx, Env, "NullBoolPtr")); | ||
}); | ||
} | ||
|
||
|
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.
I am wondering how does CodeGen deal with this? If it happens to have similar extra logic, we could simplify both by changing the AST.
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 appears that CodeGen also appears to special-case this like we do:
llvm-project/clang/lib/CodeGen/CGExprAgg.cpp
Line 1760 in e08fe57
And if we changed the AST so that we had an
ImplicitValueInitExpr
here, it looks as if the general case in CodeGen would do the right thing.I'll make a note of this for myself.