Index: clang/lib/AST/Interp/Interp.h =================================================================== --- clang/lib/AST/Interp/Interp.h +++ clang/lib/AST/Interp/Interp.h @@ -76,6 +76,9 @@ /// Checks if a value can be loaded from a block. bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr); +bool CheckInitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr, + AccessKinds AK); + /// Checks if a value can be stored in a block. bool CheckStore(InterpState &S, CodePtr OpPC, const Pointer &Ptr); @@ -500,9 +503,11 @@ /// 4) Pushes the original (pre-inc) value on the stack. template ::T> bool Inc(InterpState &S, CodePtr OpPC) { - // FIXME: Check initialization of Ptr const Pointer &Ptr = S.Stk.pop(); + if (!CheckInitialized(S, OpPC, Ptr, AK_Increment)) + return false; + return IncDecHelper(S, OpPC, Ptr); } @@ -511,9 +516,11 @@ /// 3) Writes the value increased by one back to the pointer template ::T> bool IncPop(InterpState &S, CodePtr OpPC) { - // FIXME: Check initialization of Ptr const Pointer &Ptr = S.Stk.pop(); + if (!CheckInitialized(S, OpPC, Ptr, AK_Increment)) + return false; + return IncDecHelper(S, OpPC, Ptr); } @@ -523,9 +530,11 @@ /// 4) Pushes the original (pre-dec) value on the stack. template ::T> bool Dec(InterpState &S, CodePtr OpPC) { - // FIXME: Check initialization of Ptr const Pointer &Ptr = S.Stk.pop(); + if (!CheckInitialized(S, OpPC, Ptr, AK_Decrement)) + return false; + return IncDecHelper(S, OpPC, Ptr); } @@ -534,9 +543,11 @@ /// 3) Writes the value decreased by one back to the pointer template ::T> bool DecPop(InterpState &S, CodePtr OpPC) { - // FIXME: Check initialization of Ptr const Pointer &Ptr = S.Stk.pop(); + if (!CheckInitialized(S, OpPC, Ptr, AK_Decrement)) + return false; + return IncDecHelper(S, OpPC, Ptr); } @@ -561,26 +572,38 @@ } inline bool Incf(InterpState &S, CodePtr OpPC, llvm::RoundingMode RM) { - // FIXME: Check initialization of Ptr const Pointer &Ptr = S.Stk.pop(); + + if (!CheckInitialized(S, OpPC, Ptr, AK_Increment)) + return false; + return IncDecFloatHelper(S, OpPC, Ptr, RM); } inline bool IncfPop(InterpState &S, CodePtr OpPC, llvm::RoundingMode RM) { - // FIXME: Check initialization of Ptr const Pointer &Ptr = S.Stk.pop(); + + if (!CheckInitialized(S, OpPC, Ptr, AK_Increment)) + return false; + return IncDecFloatHelper(S, OpPC, Ptr, RM); } inline bool Decf(InterpState &S, CodePtr OpPC, llvm::RoundingMode RM) { - // FIXME: Check initialization of Ptr const Pointer &Ptr = S.Stk.pop(); + + if (!CheckInitialized(S, OpPC, Ptr, AK_Decrement)) + return false; + return IncDecFloatHelper(S, OpPC, Ptr, RM); } inline bool DecfPop(InterpState &S, CodePtr OpPC, llvm::RoundingMode RM) { - // FIXME: Check initialization of Ptr const Pointer &Ptr = S.Stk.pop(); + + if (!CheckInitialized(S, OpPC, Ptr, AK_Decrement)) + return false; + return IncDecFloatHelper(S, OpPC, Ptr, RM); } Index: clang/lib/AST/Interp/Interp.cpp =================================================================== --- clang/lib/AST/Interp/Interp.cpp +++ clang/lib/AST/Interp/Interp.cpp @@ -53,17 +53,6 @@ return true; } -static bool CheckInitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr, - AccessKinds AK) { - if (Ptr.isInitialized()) - return true; - if (!S.checkingPotentialConstantExpression()) { - const SourceInfo &Loc = S.Current->getSource(OpPC); - S.FFDiag(Loc, diag::note_constexpr_access_uninit) << AK << false; - } - return false; -} - static bool CheckActive(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK) { if (Ptr.isActive()) @@ -243,6 +232,18 @@ return false; } +bool CheckInitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr, + AccessKinds AK) { + if (Ptr.isInitialized()) + return true; + + if (!S.checkingPotentialConstantExpression()) { + const SourceInfo &Loc = S.Current->getSource(OpPC); + S.FFDiag(Loc, diag::note_constexpr_access_uninit) << AK << false; + } + return false; +} + bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr) { if (!CheckLive(S, OpPC, Ptr, AK_Read)) return false; Index: clang/test/AST/Interp/literals.cpp =================================================================== --- clang/test/AST/Interp/literals.cpp +++ clang/test/AST/Interp/literals.cpp @@ -462,14 +462,36 @@ } static_assert(incBool(), ""); + template constexpr int uninit() { - int a; - ++a; // ref-note {{increment of uninitialized}} \ - // FIXME: Should also be rejected by new interpreter + T a; + if constexpr (Inc) + ++a; // ref-note 2{{increment of uninitialized}} \ + // expected-note 2{{increment of object outside its lifetime}} + else + --a; // ref-note 2{{decrement of uninitialized}} \ + // expected-note 2{{decrement of object outside its lifetime}} return 1; } - static_assert(uninit(), ""); // ref-error {{not an integral constant expression}} \ - // ref-note {{in call to 'uninit()'}} + static_assert(uninit(), ""); // ref-error {{not an integral constant expression}} \ + // ref-note {{in call to 'uninit()'}} \ + // expected-error {{not an integral constant expression}} \ + // expected-note {{in call to 'uninit()'}} + + static_assert(uninit(), ""); // ref-error {{not an integral constant expression}} \ + // ref-note {{in call to 'uninit()'}} \ + // expected-error {{not an integral constant expression}} \ + // expected-note {{in call to 'uninit()'}} + + static_assert(uninit(), ""); // ref-error {{not an integral constant expression}} \ + // ref-note {{in call to 'uninit()'}} \ + // expected-error {{not an integral constant expression}} \ + // expected-note {{in call to 'uninit()'}} + + static_assert(uninit(), ""); // ref-error {{not an integral constant expression}} \ + // ref-note {{in call to 'uninit()'}} \ + // expected-error {{not an integral constant expression}} \ + // expected-note {{in call to 'uninit()'}} constexpr int OverFlow() { // ref-error {{never produces a constant expression}} \ // expected-error {{never produces a constant expression}}