llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang Author: Timm Baeder (tbaederr) <details> <summary>Changes</summary> We need to emit the 'initializer of X is not a constant expression' note for local constexpr variables as well. --- Full diff: https://github.com/llvm/llvm-project/pull/123588.diff 3 Files Affected: - (modified) clang/lib/AST/ByteCode/Interp.cpp (+6-4) - (modified) clang/test/AST/ByteCode/c23.c (+8) - (modified) clang/test/AST/ByteCode/literals.cpp (+9) ``````````diff diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp index 4b26cc66cd09ad..c765ebf5d618ee 100644 --- a/clang/lib/AST/ByteCode/Interp.cpp +++ b/clang/lib/AST/ByteCode/Interp.cpp @@ -416,9 +416,11 @@ bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK) { if (!Ptr.isOnePastEnd()) return true; - const SourceInfo &Loc = S.Current->getSource(OpPC); - S.FFDiag(Loc, diag::note_constexpr_access_past_end) - << AK << S.Current->getRange(OpPC); + if (S.getLangOpts().CPlusPlus) { + const SourceInfo &Loc = S.Current->getSource(OpPC); + S.FFDiag(Loc, diag::note_constexpr_access_past_end) + << AK << S.Current->getRange(OpPC); + } return false; } @@ -538,7 +540,7 @@ bool CheckInitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr, return true; if (const auto *VD = Ptr.getDeclDesc()->asVarDecl(); - VD && VD->hasGlobalStorage()) { + VD && (VD->isConstexpr() || VD->hasGlobalStorage())) { const SourceInfo &Loc = S.Current->getSource(OpPC); if (VD->getAnyInitializer()) { S.FFDiag(Loc, diag::note_constexpr_var_init_non_constant, 1) << VD; diff --git a/clang/test/AST/ByteCode/c23.c b/clang/test/AST/ByteCode/c23.c index 5154d57f6cb9e7..0e9851aa2ad3a5 100644 --- a/clang/test/AST/ByteCode/c23.c +++ b/clang/test/AST/ByteCode/c23.c @@ -49,3 +49,11 @@ static_assert(arg1[1] == 254); static_assert(arg1[2] == 186); static_assert(arg1[3] == 190); #endif + +void ghissue109095() { + constexpr char c[] = { 'a' }; + constexpr int i = c[1]; // both-error {{constexpr variable 'i' must be initialized by a constant expression}}\ + // both-note {{declared here}} + _Static_assert(i == c[0]); // both-error {{static assertion expression is not an integral constant expression}}\ + // both-note {{initializer of 'i' is not a constant expression}} +} diff --git a/clang/test/AST/ByteCode/literals.cpp b/clang/test/AST/ByteCode/literals.cpp index fdf1a6820e4466..b75ca2b19a969a 100644 --- a/clang/test/AST/ByteCode/literals.cpp +++ b/clang/test/AST/ByteCode/literals.cpp @@ -1315,3 +1315,12 @@ namespace { } } #endif + +void localConstexpr() { + constexpr int a = 1/0; // both-error {{must be initialized by a constant expression}} \ + // both-note {{division by zero}} \ + // both-warning {{division by zero is undefined}} \ + // both-note {{declared here}} + static_assert(a == 0, ""); // both-error {{not an integral constant expression}} \ + // both-note {{initializer of 'a' is not a constant expression}} +} `````````` </details> https://github.com/llvm/llvm-project/pull/123588 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits