[clang] [clang][bytecode] Revisit global variables separately (PR #123358)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Jan 17 07:37:16 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
Call `EvaluateAsInitializer()` explicitly here, so we don't abort the evaluation of the `DeflRefExpr` just because the initializer of that global variable failed.
---
Full diff: https://github.com/llvm/llvm-project/pull/123358.diff
2 Files Affected:
- (modified) clang/lib/AST/ByteCode/Compiler.cpp (+13-1)
- (modified) clang/test/AST/ByteCode/cxx98.cpp (+5)
``````````diff
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index fca8518575594f..3ef2b0858e667b 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -6210,8 +6210,20 @@ bool Compiler<Emitter>::visitDeclRef(const ValueDecl *D, const Expr *E) {
return revisit(VD);
if ((VD->hasGlobalStorage() || VD->isStaticDataMember()) &&
- typeShouldBeVisited(VD->getType()))
+ typeShouldBeVisited(VD->getType())) {
+ if (const Expr *Init = VD->getAnyInitializer();
+ Init && !Init->isValueDependent()) {
+ // Whether or not the evaluation is successul doesn't really matter
+ // here -- we will create a global variable in any case, and that
+ // will have the state of initializer evaluation attached.
+ APValue V;
+ SmallVector<PartialDiagnosticAt> Notes;
+ (void)Init->EvaluateAsInitializer(V, Ctx.getASTContext(), VD, Notes,
+ true);
+ return this->visitDeclRef(D, E);
+ }
return revisit(VD);
+ }
// FIXME: The evaluateValue() check here is a little ridiculous, since
// it will ultimately call into Context::evaluateAsInitializer(). In
diff --git a/clang/test/AST/ByteCode/cxx98.cpp b/clang/test/AST/ByteCode/cxx98.cpp
index 20f98d33c31c4f..c17049b01c1daf 100644
--- a/clang/test/AST/ByteCode/cxx98.cpp
+++ b/clang/test/AST/ByteCode/cxx98.cpp
@@ -59,3 +59,8 @@ struct PR65784s{
int *ptr;
} const PR65784[] = {(int *)""};
PR65784s PR65784f() { return *PR65784; }
+
+const int b = 1 / 0; // both-warning {{division by zero is undefined}} \
+ // both-note {{declared here}}
+_Static_assert(b, ""); // both-error {{not an integral constant expression}} \
+ // both-note {{initializer of 'b' is not a constant expression}}
``````````
</details>
https://github.com/llvm/llvm-project/pull/123358
More information about the cfe-commits
mailing list