[clang] [clang][Interp] Diagnose reads from non-const global variables (PR #71919)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 10 02:36:28 PST 2023
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
This fixes a long-standing FIXME item.
Unfortunately it changes the diagnostic output of the tests added in `cxx23.cpp`, but they were wrong before and are wrong after, so no big deal.
---
Full diff: https://github.com/llvm/llvm-project/pull/71919.diff
7 Files Affected:
- (modified) clang/lib/AST/Interp/ByteCodeExprGen.cpp (+1-1)
- (modified) clang/lib/AST/Interp/Interp.cpp (+34-10)
- (modified) clang/lib/AST/Interp/Interp.h (+14)
- (modified) clang/lib/AST/Interp/Opcodes.td (+1)
- (modified) clang/test/AST/Interp/arrays.cpp (+32)
- (modified) clang/test/AST/Interp/cxx23.cpp (+16-9)
- (modified) clang/test/AST/Interp/literals.cpp (+24-3)
``````````diff
diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index 15a717089660337..eca31efc85272bf 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -2125,7 +2125,7 @@ bool ByteCodeExprGen<Emitter>::visitDecl(const VarDecl *VD) {
auto GlobalIndex = P.getGlobal(VD);
assert(GlobalIndex); // visitVarDecl() didn't return false.
if (VarT) {
- if (!this->emitGetGlobal(*VarT, *GlobalIndex, VD))
+ if (!this->emitGetGlobalUnchecked(*VarT, *GlobalIndex, VD))
return false;
} else {
if (!this->emitGetPtrGlobal(*GlobalIndex, VD))
diff --git a/clang/lib/AST/Interp/Interp.cpp b/clang/lib/AST/Interp/Interp.cpp
index 144b674451e353c..d1d812a591591bf 100644
--- a/clang/lib/AST/Interp/Interp.cpp
+++ b/clang/lib/AST/Interp/Interp.cpp
@@ -53,6 +53,18 @@ static bool Jf(InterpState &S, CodePtr &PC, int32_t Offset) {
return true;
}
+static void diagnoseNonConstVariable(InterpState &S, CodePtr OpPC,
+ const ValueDecl *VD) {
+ const SourceInfo &Loc = S.Current->getSource(OpPC);
+ S.FFDiag(Loc,
+ VD->getType()->isIntegralOrEnumerationType()
+ ? diag::note_constexpr_ltor_non_const_int
+ : diag::note_constexpr_ltor_non_constexpr,
+ 1)
+ << VD;
+ S.Note(VD->getLocation(), diag::note_declared_at);
+}
+
static bool CheckActive(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
AccessKinds AK) {
if (Ptr.isActive())
@@ -170,9 +182,7 @@ bool CheckExtern(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
if (!S.checkingPotentialConstantExpression() && S.getLangOpts().CPlusPlus) {
const auto *VD = Ptr.getDeclDesc()->asValueDecl();
- const SourceInfo &Loc = S.Current->getSource(OpPC);
- S.FFDiag(Loc, diag::note_constexpr_ltor_non_constexpr, 1) << VD;
- S.Note(VD->getLocation(), diag::note_declared_at);
+ diagnoseNonConstVariable(S, OpPC, VD);
}
return false;
}
@@ -215,6 +225,24 @@ bool CheckLive(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
return true;
}
+bool CheckConstant(InterpState &S, CodePtr OpPC, const Descriptor *Desc) {
+ assert(Desc);
+ if (const auto *D = Desc->asValueDecl()) {
+ if (const auto *VD = dyn_cast<VarDecl>(D);
+ VD && VD->hasGlobalStorage() &&
+ !(VD->isConstexpr() || VD->getType().isConstQualified())) {
+ diagnoseNonConstVariable(S, OpPC, VD);
+ return false;
+ }
+ }
+
+ return true;
+}
+
+static bool CheckConstant(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
+ return CheckConstant(S, OpPC, Ptr.getDeclDesc());
+}
+
bool CheckDummy(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
return !Ptr.isZero() && !Ptr.isDummy();
}
@@ -303,6 +331,8 @@ bool CheckInitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
if (!CheckDummy(S, OpPC, Ptr))
return false;
+ if (!CheckConstant(S, OpPC, Ptr))
+ return false;
if (!CheckLive(S, OpPC, Ptr, AK_Read))
return false;
if (!CheckExtern(S, OpPC, Ptr))
@@ -601,13 +631,7 @@ bool CheckDeclRef(InterpState &S, CodePtr OpPC, const DeclRefExpr *DR) {
}
} else if (const auto *VD = dyn_cast<VarDecl>(D)) {
if (!VD->getType().isConstQualified()) {
- S.FFDiag(E,
- VD->getType()->isIntegralOrEnumerationType()
- ? diag::note_constexpr_ltor_non_const_int
- : diag::note_constexpr_ltor_non_constexpr,
- 1)
- << VD;
- S.Note(VD->getLocation(), diag::note_declared_at) << VD->getSourceRange();
+ diagnoseNonConstVariable(S, OpPC, VD);
return false;
}
diff --git a/clang/lib/AST/Interp/Interp.h b/clang/lib/AST/Interp/Interp.h
index 7dd415d6e460536..5b7bd9916c02b57 100644
--- a/clang/lib/AST/Interp/Interp.h
+++ b/clang/lib/AST/Interp/Interp.h
@@ -78,6 +78,9 @@ bool CheckSubobject(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
/// Checks if a pointer points to const storage.
bool CheckConst(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
+/// Checks if the Descriptor is of a constexpr or const global variable.
+bool CheckConstant(InterpState &S, CodePtr OpPC, const Descriptor *Desc);
+
/// Checks if a pointer points to a mutable field.
bool CheckMutable(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
@@ -1005,12 +1008,23 @@ bool SetThisField(InterpState &S, CodePtr OpPC, uint32_t I) {
template <PrimType Name, class T = typename PrimConv<Name>::T>
bool GetGlobal(InterpState &S, CodePtr OpPC, uint32_t I) {
auto *B = S.P.getGlobal(I);
+
+ if (!CheckConstant(S, OpPC, B->getDescriptor()))
+ return false;
if (B->isExtern())
return false;
S.Stk.push<T>(B->deref<T>());
return true;
}
+/// Same as GetGlobal, but without the checks.
+template <PrimType Name, class T = typename PrimConv<Name>::T>
+bool GetGlobalUnchecked(InterpState &S, CodePtr OpPC, uint32_t I) {
+ auto *B = S.P.getGlobal(I);
+ S.Stk.push<T>(B->deref<T>());
+ return true;
+}
+
template <PrimType Name, class T = typename PrimConv<Name>::T>
bool SetGlobal(InterpState &S, CodePtr OpPC, uint32_t I) {
// TODO: emit warning.
diff --git a/clang/lib/AST/Interp/Opcodes.td b/clang/lib/AST/Interp/Opcodes.td
index 69068e87d5720ab..e01b6b9eea7dbb4 100644
--- a/clang/lib/AST/Interp/Opcodes.td
+++ b/clang/lib/AST/Interp/Opcodes.td
@@ -379,6 +379,7 @@ def CheckGlobalCtor : Opcode {}
// [] -> [Value]
def GetGlobal : AccessOpcode;
+def GetGlobalUnchecked : AccessOpcode;
// [Value] -> []
def InitGlobal : AccessOpcode;
// [Value] -> []
diff --git a/clang/test/AST/Interp/arrays.cpp b/clang/test/AST/Interp/arrays.cpp
index 34e0086fb9ee8ca..91d64a9bd5e7d55 100644
--- a/clang/test/AST/Interp/arrays.cpp
+++ b/clang/test/AST/Interp/arrays.cpp
@@ -554,3 +554,35 @@ namespace GH69115 {
static_assert(foo2() == 0, "");
#endif
}
+
+namespace NonConstReads {
+#if __cplusplus >= 202002L
+ void *p = nullptr; // ref-note {{declared here}} \
+ // expected-note {{declared here}}
+
+ int arr[!p]; // ref-error {{not allowed at file scope}} \
+ // expected-error {{not allowed at file scope}} \
+ // ref-warning {{variable length arrays}} \
+ // ref-note {{read of non-constexpr variable 'p'}} \
+ // expected-warning {{variable length arrays}} \
+ // expected-note {{read of non-constexpr variable 'p'}}
+ int z; // ref-note {{declared here}} \
+ // expected-note {{declared here}}
+ int a[z]; // ref-error {{not allowed at file scope}} \
+ // expected-error {{not allowed at file scope}} \
+ // ref-warning {{variable length arrays}} \
+ // ref-note {{read of non-const variable 'z'}} \
+ // expected-warning {{variable length arrays}} \
+ // expected-note {{read of non-const variable 'z'}}
+#else
+ void *p = nullptr;
+ int arr[!p]; // ref-error {{not allowed at file scope}} \
+ // expected-error {{not allowed at file scope}}
+ int z;
+ int a[z]; // ref-error {{not allowed at file scope}} \
+ // expected-error {{not allowed at file scope}}
+#endif
+
+ const int y = 0;
+ int yy[y];
+}
diff --git a/clang/test/AST/Interp/cxx23.cpp b/clang/test/AST/Interp/cxx23.cpp
index e284a66626fb331..4cd511f853571c4 100644
--- a/clang/test/AST/Interp/cxx23.cpp
+++ b/clang/test/AST/Interp/cxx23.cpp
@@ -4,9 +4,6 @@
// RUN: %clang_cc1 -std=c++23 -fsyntax-only -fcxx-exceptions -verify=expected23 %s -fexperimental-new-constant-interpreter
-// expected23-no-diagnostics
-
-
/// FIXME: The new interpreter is missing all the 'control flows through...' diagnostics.
constexpr int f(int n) { // ref20-error {{constexpr function never produces a constant expression}} \
@@ -28,22 +25,32 @@ constexpr int g(int n) { // ref20-error {{constexpr function never produc
}
constexpr int c_thread_local(int n) { // ref20-error {{constexpr function never produces a constant expression}} \
- // ref23-error {{constexpr function never produces a constant expression}}
+ // ref23-error {{constexpr function never produces a constant expression}} \
+ // expected20-error {{constexpr function never produces a constant expression}} \
+ // expected23-error {{constexpr function never produces a constant expression}}
static _Thread_local int m = 0; // ref20-note {{control flows through the definition of a thread_local variable}} \
// ref20-warning {{is a C++23 extension}} \
// ref23-note {{control flows through the definition of a thread_local variable}} \
- // expected20-warning {{is a C++23 extension}}
- return m;
+ // expected20-warning {{is a C++23 extension}} \
+ // expected20-note {{declared here}} \
+ // expected23-note {{declared here}}
+ return m; // expected20-note {{read of non-const variable}} \
+ // expected23-note {{read of non-const variable}}
}
constexpr int gnu_thread_local(int n) { // ref20-error {{constexpr function never produces a constant expression}} \
- // ref23-error {{constexpr function never produces a constant expression}}
+ // ref23-error {{constexpr function never produces a constant expression}} \
+ // expected20-error {{constexpr function never produces a constant expression}} \
+ // expected23-error {{constexpr function never produces a constant expression}}
static __thread int m = 0; // ref20-note {{control flows through the definition of a thread_local variable}} \
// ref20-warning {{is a C++23 extension}} \
// ref23-note {{control flows through the definition of a thread_local variable}} \
- // expected20-warning {{is a C++23 extension}}
- return m;
+ // expected20-warning {{is a C++23 extension}} \
+ // expected20-note {{declared here}} \
+ // expected23-note {{declared here}}
+ return m; // expected20-note {{read of non-const variable}} \
+ // expected23-note {{read of non-const variable}}
}
constexpr int h(int n) { // ref20-error {{constexpr function never produces a constant expression}} \
diff --git a/clang/test/AST/Interp/literals.cpp b/clang/test/AST/Interp/literals.cpp
index ba24955d14503be..0e472445b2d0e15 100644
--- a/clang/test/AST/Interp/literals.cpp
+++ b/clang/test/AST/Interp/literals.cpp
@@ -1156,8 +1156,29 @@ namespace InvalidDeclRefs {
// expected-error {{not an integral constant expression}} \
// expected-note {{initializer of 'b02' is unknown}}
- /// FIXME: This should also be diagnosed in the new interpreter.
- int b03 = 3; // ref-note {{declared here}}
+ int b03 = 3; // ref-note {{declared here}} \
+ // expected-note {{declared here}}
static_assert(b03, ""); // ref-error {{not an integral constant expression}} \
- // ref-note {{read of non-const variable}}
+ // ref-note {{read of non-const variable}} \
+ // expected-error {{not an integral constant expression}} \
+ // expected-note {{read of non-const variable}}
+}
+
+namespace NonConstReads {
+ void *p = nullptr; // ref-note {{declared here}} \
+ // expected-note {{declared here}}
+ static_assert(!p, ""); // ref-error {{not an integral constant expression}} \
+ // ref-note {{read of non-constexpr variable 'p'}} \
+ // expected-error {{not an integral constant expression}} \
+ // expected-note {{read of non-constexpr variable 'p'}}
+
+ int arr[!p]; // ref-error {{variable length array}} \
+ // expected-error {{variable length array}}
+
+ int z; // ref-note {{declared here}} \
+ // expected-note {{declared here}}
+ static_assert(z == 0, ""); // ref-error {{not an integral constant expression}} \
+ // ref-note {{read of non-const variable 'z'}} \
+ // expected-error {{not an integral constant expression}} \
+ // expected-note {{read of non-const variable 'z'}}
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/71919
More information about the cfe-commits
mailing list