Skip to content

Commit

Permalink
[clang][Interp] Emit correct diagnostic for uninitialized reads
Browse files Browse the repository at this point in the history
Differential Revision: https://reviews.llvm.org/D154758
  • Loading branch information
tbaederr committed Jul 9, 2023
1 parent 254ad7d commit c54ff51
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
3 changes: 2 additions & 1 deletion clang/lib/AST/Interp/Interp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ bool CheckInitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr,

if (!S.checkingPotentialConstantExpression()) {
const SourceInfo &Loc = S.Current->getSource(OpPC);
S.FFDiag(Loc, diag::note_constexpr_access_uninit) << AK << false;
S.FFDiag(Loc, diag::note_constexpr_access_uninit)
<< AK << /*uninitialized=*/true;
}
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion clang/test/AST/Interp/constexpr-nqueens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct Board {
Failed(Failed) {}
constexpr Board addQueen(int Row, int Col) const {
return Board(State | ((uint64_t)Row << (Col * 4))); // ref-note {{read of uninitialized object}} \
// expected-note {{read of object outside its lifetime}}
// expected-note {{read of uninitialized object}}
}
constexpr int getQueenRow(int Col) const {
return (State >> (Col * 4)) & 0xf;
Expand Down
13 changes: 6 additions & 7 deletions clang/test/AST/Interp/cxx20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ static_assert(pointerAssign2() == 12, "");
constexpr int unInitLocal() {
int a;
return a; // ref-note {{read of uninitialized object}} \
// expected-note {{read of object outside its lifetime}}
// FIXME: ^^^ Wrong diagnostic.
// expected-note {{read of uninitialized object}}
}
static_assert(unInitLocal() == 0, ""); // ref-error {{not an integral constant expression}} \
// ref-note {{in call to 'unInitLocal()'}} \
Expand All @@ -76,7 +75,7 @@ static_assert(initializedLocal() == 20);

constexpr int initializedLocal2() {
int a[2];
return *a; // expected-note {{read of object outside its lifetime}} \
return *a; // expected-note {{read of uninitialized object is not allowed in a constant expression}} \
// ref-note {{read of uninitialized object is not allowed in a constant expression}}
}
static_assert(initializedLocal2() == 20); // expected-error {{not an integral constant expression}} \
Expand All @@ -89,7 +88,7 @@ struct Int { int a; };
constexpr int initializedLocal3() {
Int i;
return i.a; // ref-note {{read of uninitialized object is not allowed in a constant expression}} \
// expected-note {{read of object outside its lifetime}}
// expected-note {{read of uninitialized object}}
}
static_assert(initializedLocal3() == 20); // expected-error {{not an integral constant expression}} \
// expected-note {{in call to}} \
Expand Down Expand Up @@ -315,7 +314,7 @@ namespace BaseInit {

static_assert(Final{1, 2, 3}.c == 3, ""); // OK
static_assert(Final{1, 2, 3}.a == 0, ""); // expected-error {{not an integral constant expression}} \
// expected-note {{read of object outside its lifetime}} \
// expected-note {{read of uninitialized object}} \
// ref-error {{not an integral constant expression}} \
// ref-note {{read of uninitialized object}}

Expand All @@ -337,7 +336,7 @@ namespace BaseInit {
static_assert(Final2{1, 2, 3}.c == 3, ""); // OK
static_assert(Final2{1, 2, 3}.b == 2, ""); // OK
static_assert(Final2{1, 2, 3}.a == 0, ""); // expected-error {{not an integral constant expression}} \
// expected-note {{read of object outside its lifetime}} \
// expected-note {{read of uninitialized object}} \
// ref-error {{not an integral constant expression}} \
// ref-note {{read of uninitialized object}}

Expand All @@ -356,7 +355,7 @@ namespace BaseInit {
static_assert(Final3{1, 2, 3}.c == 3, ""); // OK
static_assert(Final3{1, 2, 3}.b == 2, ""); // OK
static_assert(Final3{1, 2, 3}.a == 0, ""); // expected-error {{not an integral constant expression}} \
// expected-note {{read of object outside its lifetime}} \
// expected-note {{read of uninitialized object}} \
// ref-error {{not an integral constant expression}} \
// ref-note {{read of uninitialized object}}
};
Expand Down
4 changes: 2 additions & 2 deletions clang/test/AST/Interp/literals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,10 +467,10 @@ namespace IncDec {
T a;
if constexpr (Inc)
++a; // ref-note 2{{increment of uninitialized}} \
// expected-note 2{{increment of object outside its lifetime}}
// expected-note 2{{increment of uninitialized}}
else
--a; // ref-note 2{{decrement of uninitialized}} \
// expected-note 2{{decrement of object outside its lifetime}}
// expected-note 2{{decrement of uninitialized}}
return 1;
}
static_assert(uninit<int, true>(), ""); // ref-error {{not an integral constant expression}} \
Expand Down
4 changes: 2 additions & 2 deletions clang/test/AST/Interp/records.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ namespace DeriveFailures {
static_assert(D.Val == 0, ""); // ref-error {{not an integral constant expression}} \
// ref-note {{initializer of 'D' is not a constant expression}} \
// expected-error {{not an integral constant expression}} \
// expected-note {{read of object outside its lifetime}}
// expected-note {{read of uninitialized object}}
#endif

struct AnotherBase {
Expand Down Expand Up @@ -467,7 +467,7 @@ namespace DeclRefs {
constexpr A a{10}; // expected-error {{must be initialized by a constant expression}}
static_assert(a.m == 10, "");
static_assert(a.f == 10, ""); // expected-error {{not an integral constant expression}} \
// expected-note {{read of object outside its lifetime}}
// expected-note {{read of uninitialized object}}

class Foo {
public:
Expand Down

0 comments on commit c54ff51

Please sign in to comment.