-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[clang][bytecode] Check for global decls in destructors #137525
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
Conversation
|
@llvm/pr-subscribers-clang Author: Timm Baeder (tbaederr) ChangesDestructors can't be called on global variables. Full diff: https://github.com/llvm/llvm-project/pull/137525.diff 3 Files Affected:
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 37111343178dd..080f694e27da2 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -1387,9 +1387,14 @@ static bool checkConstructor(InterpState &S, CodePtr OpPC, const Function *Func,
return false;
}
-static bool checkDestructor(InterpState &S, CodePtr OpPC, const Function *Func,
- const Pointer &ThisPtr) {
- return CheckActive(S, OpPC, ThisPtr, AK_Destroy);
+bool CheckDestructor(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
+ // Can't call a dtor on a global variable.
+ if (Ptr.block()->isStatic()) {
+ const SourceInfo &E = S.Current->getSource(OpPC);
+ S.FFDiag(E, diag::note_constexpr_modify_global);
+ return false;
+ }
+ return CheckActive(S, OpPC, Ptr, AK_Destroy);
}
static void compileFunction(InterpState &S, const Function *Func) {
@@ -1486,7 +1491,7 @@ bool Call(InterpState &S, CodePtr OpPC, const Function *Func,
if (Func->isConstructor() && !checkConstructor(S, OpPC, Func, ThisPtr))
return false;
- if (Func->isDestructor() && !checkDestructor(S, OpPC, Func, ThisPtr))
+ if (Func->isDestructor() && !CheckDestructor(S, OpPC, ThisPtr))
return false;
}
diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 63a1f3e8033b7..27e73bb72fecb 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -162,6 +162,7 @@ bool InvalidShuffleVectorIndex(InterpState &S, CodePtr OpPC, uint32_t Index);
bool CheckBitCast(InterpState &S, CodePtr OpPC, bool HasIndeterminateBits,
bool TargetIsUCharOrByte);
bool CheckBCPResult(InterpState &S, const Pointer &Ptr);
+bool CheckDestructor(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
template <typename T>
static bool handleOverflow(InterpState &S, CodePtr OpPC, const T &SrcValue) {
@@ -3242,7 +3243,7 @@ bool DiagTypeid(InterpState &S, CodePtr OpPC);
inline bool CheckDestruction(InterpState &S, CodePtr OpPC) {
const auto &Ptr = S.Stk.peek<Pointer>();
- return CheckActive(S, OpPC, Ptr, AK_Destroy);
+ return CheckDestructor(S, OpPC, Ptr);
}
//===----------------------------------------------------------------------===//
diff --git a/clang/test/AST/ByteCode/records.cpp b/clang/test/AST/ByteCode/records.cpp
index b4059f009b887..9abfe6b8a15e7 100644
--- a/clang/test/AST/ByteCode/records.cpp
+++ b/clang/test/AST/ByteCode/records.cpp
@@ -1810,3 +1810,12 @@ namespace AccessMismatch {
static_assert(B().cmp(), ""); // both-error {{constant expression}} \
// both-note {{in call}}
}
+
+namespace GlobalDtor {
+ struct A {
+ };
+ constexpr A a = {};
+ constexpr void destroy1() { // both-error {{constexpr}}
+ a.~A(); // both-note {{cannot modify an object that is visible outside}}
+ }
+}
|
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/10/builds/4235 Here is the relevant piece of the build log for the reference |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/88/builds/10975 Here is the relevant piece of the build log for the reference |
Destructors can't be called on global variables.
Destructors can't be called on global variables.