Skip to content

Commit 892df30

Browse files
committed
Fix interaction of constinit and weak.
We previously took a shortcut and said that weak variables never have constant initializers (because those initializers are never correct to use outside the variable). We now say that weak variables can have constant initializers, but are never usable in constant expressions.
1 parent 700e632 commit 892df30

File tree

4 files changed

+10
-6
lines changed

4 files changed

+10
-6
lines changed

clang/lib/AST/Decl.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2287,6 +2287,10 @@ bool VarDecl::mightBeUsableInConstantExpressions(ASTContext &C) const {
22872287
if (isa<ParmVarDecl>(this))
22882288
return false;
22892289

2290+
// The values of weak variables are never usable in constant expressions.
2291+
if (isWeak())
2292+
return false;
2293+
22902294
// In C++11, any variable of reference type can be used in a constant
22912295
// expression if it is initialized by a constant expression.
22922296
if (Lang.CPlusPlus11 && getType()->isReferenceType())
@@ -2414,10 +2418,6 @@ bool VarDecl::isInitICE() const {
24142418
}
24152419

24162420
bool VarDecl::checkInitIsICE() const {
2417-
// Initializers of weak variables are never ICEs.
2418-
if (isWeak())
2419-
return false;
2420-
24212421
EvaluatedStmt *Eval = ensureEvaluatedStmt();
24222422
if (Eval->CheckedICE)
24232423
// We have already checked whether this subexpression is an

clang/lib/AST/ExprConstant.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14816,7 +14816,7 @@ static ICEDiag CheckICE(const Expr* E, const ASTContext &Ctx) {
1481614816
const VarDecl *VD;
1481714817
// Look for a declaration of this variable that has an initializer, and
1481814818
// check whether it is an ICE.
14819-
if (Dcl->getAnyInitializer(VD) && VD->checkInitIsICE())
14819+
if (Dcl->getAnyInitializer(VD) && !VD->isWeak() && VD->checkInitIsICE())
1482014820
return NoDiag();
1482114821
else
1482214822
return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation());

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11112,7 +11112,7 @@ QualType Sema::CheckComparisonCategoryType(ComparisonCategoryType Kind,
1111211112
// might be foobar, including it failing to be a constant expression.
1111311113
// TODO Handle more ways the lookup or result can be invalid.
1111411114
if (!VD->isStaticDataMember() || !VD->isConstexpr() || !VD->hasInit() ||
11115-
!VD->checkInitIsICE())
11115+
VD->isWeak() || !VD->checkInitIsICE())
1111611116
return UnsupportedSTLError(USS_InvalidMember, MemName, VD);
1111711117

1111811118
// Attempt to evaluate the var decl as a constant expression and extract
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// RUN: %clang_cc1 %s -std=c++20 -verify
2+
// expected-no-diagnostics
3+
4+
constinit int a __attribute__((weak)) = 0;

0 commit comments

Comments
 (0)