[clang] [Modules] Record whether VarDecl initializers contain side effects (PR #143739)
Chuanqi Xu via cfe-commits
cfe-commits at lists.llvm.org
Sun Jun 22 19:00:02 PDT 2025
================
@@ -2434,6 +2434,31 @@ VarDecl *VarDecl::getInitializingDeclaration() {
return Def;
}
+bool VarDecl::hasInitWithSideEffects() const {
+ if (!hasInit())
+ return false;
+
+ // Check if we can get the initializer without deserializing
+ const Expr *E = nullptr;
+ if (auto *S = dyn_cast<Stmt *>(Init)) {
+ E = cast<Expr>(S);
+ } else {
+ auto *Eval = getEvaluatedStmt();
+ if (!Eval->Value.isOffset())
+ E = cast<Expr>(Eval->Value.get(nullptr));
+ }
+
+ if (E)
+ return E->HasSideEffects(getASTContext()) &&
+ // We can get a value-dependent initializer during error recovery.
+ (E->isValueDependent() || !evaluateValue());
+
+ assert(getEvaluatedStmt()->Value.isOffset());
+ // ASTReader tracks this without having to deserialize the initializer
+ return getASTContext().getExternalSource()->hasInitializerWithSideEffects(
----------------
ChuanqiXu9 wrote:
Currently I don't know but I prefer to guard `getASTContext().getExternalSource()` generally, it is consistent and easier to maintain. I don't think the additional branch is critical.
https://github.com/llvm/llvm-project/pull/143739
More information about the cfe-commits
mailing list