Skip to content

Commit 9221bed

Browse files
committed
Revert "Implement CWG2631"
This reverts commit 26fa17e. This reverts commit 4403c4f. There is still an ODR issue causing linker errors, investigating.
1 parent 1bd0ff7 commit 9221bed

25 files changed

+156
-840
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -618,11 +618,6 @@ C++ Language Changes in Clang
618618
- Implemented DR2358 allowing init captures in lambdas in default arguments.
619619
- implemented `DR2654 <https://wg21.link/cwg2654>`_ which undeprecates
620620
all compound assignements operations on volatile qualified variables.
621-
- Implemented DR2631. Invalid ``consteval`` calls in default arguments and default
622-
member initializers are diagnosed when and if the default is used.
623-
This Fixes `Issue 56379 <https://github.com/llvm/llvm-project/issues/56379>`_
624-
and changes the value of ``std::source_location::current()``
625-
used in default parameters calls compared to previous versions of Clang.
626621

627622
C++20 Feature Support
628623
^^^^^^^^^^^^^^^^^^^^^

clang/include/clang/AST/ExprCXX.h

Lines changed: 24 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,12 +1245,8 @@ class CXXThrowExpr : public Expr {
12451245
/// This wraps up a function call argument that was created from the
12461246
/// corresponding parameter's default argument, when the call did not
12471247
/// explicitly supply arguments for all of the parameters.
1248-
class CXXDefaultArgExpr final
1249-
: public Expr,
1250-
private llvm::TrailingObjects<CXXDefaultArgExpr, Expr *> {
1248+
class CXXDefaultArgExpr final : public Expr {
12511249
friend class ASTStmtReader;
1252-
friend class ASTReader;
1253-
friend TrailingObjects;
12541250

12551251
/// The parameter whose default is being used.
12561252
ParmVarDecl *Param;
@@ -1259,7 +1255,7 @@ class CXXDefaultArgExpr final
12591255
DeclContext *UsedContext;
12601256

12611257
CXXDefaultArgExpr(StmtClass SC, SourceLocation Loc, ParmVarDecl *Param,
1262-
Expr *RewrittenExpr, DeclContext *UsedContext)
1258+
DeclContext *UsedContext)
12631259
: Expr(SC,
12641260
Param->hasUnparsedDefaultArg()
12651261
? Param->getType().getNonReferenceType()
@@ -1268,58 +1264,28 @@ class CXXDefaultArgExpr final
12681264
Param->getDefaultArg()->getObjectKind()),
12691265
Param(Param), UsedContext(UsedContext) {
12701266
CXXDefaultArgExprBits.Loc = Loc;
1271-
CXXDefaultArgExprBits.HasRewrittenInit = RewrittenExpr != nullptr;
1272-
if (RewrittenExpr)
1273-
*getTrailingObjects<Expr *>() = RewrittenExpr;
12741267
setDependence(computeDependence(this));
12751268
}
12761269

1277-
CXXDefaultArgExpr(EmptyShell Empty, bool HasRewrittenInit)
1278-
: Expr(CXXDefaultArgExprClass, Empty) {
1279-
CXXDefaultArgExprBits.HasRewrittenInit = HasRewrittenInit;
1280-
}
1281-
1282-
size_t numTrailingObjects() const {
1283-
return CXXDefaultArgExprBits.HasRewrittenInit;
1284-
}
1285-
12861270
public:
1287-
static CXXDefaultArgExpr *CreateEmpty(const ASTContext &C,
1288-
bool HasRewrittenInit);
1271+
CXXDefaultArgExpr(EmptyShell Empty) : Expr(CXXDefaultArgExprClass, Empty) {}
12891272

12901273
// \p Param is the parameter whose default argument is used by this
12911274
// expression.
12921275
static CXXDefaultArgExpr *Create(const ASTContext &C, SourceLocation Loc,
1293-
ParmVarDecl *Param, Expr *RewrittenExpr,
1294-
DeclContext *UsedContext);
1276+
ParmVarDecl *Param,
1277+
DeclContext *UsedContext) {
1278+
return new (C)
1279+
CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param, UsedContext);
1280+
}
1281+
12951282
// Retrieve the parameter that the argument was created from.
12961283
const ParmVarDecl *getParam() const { return Param; }
12971284
ParmVarDecl *getParam() { return Param; }
12981285

1299-
bool hasRewrittenInit() const {
1300-
return CXXDefaultArgExprBits.HasRewrittenInit;
1301-
}
1302-
1303-
// Retrieve the argument to the function call.
1304-
Expr *getExpr();
1305-
const Expr *getExpr() const {
1306-
return const_cast<CXXDefaultArgExpr *>(this)->getExpr();
1307-
}
1308-
1309-
Expr *getRewrittenExpr() {
1310-
return hasRewrittenInit() ? *getTrailingObjects<Expr *>() : nullptr;
1311-
}
1312-
1313-
const Expr *getRewrittenExpr() const {
1314-
return const_cast<CXXDefaultArgExpr *>(this)->getRewrittenExpr();
1315-
}
1316-
1317-
// Retrieve the rewritten init expression (for an init expression containing
1318-
// immediate calls) with the top level FullExpr and ConstantExpr stripped off.
1319-
Expr *getAdjustedRewrittenExpr();
1320-
const Expr *getAdjustedRewrittenExpr() const {
1321-
return const_cast<CXXDefaultArgExpr *>(this)->getAdjustedRewrittenExpr();
1322-
}
1286+
// Retrieve the actual argument to the function call.
1287+
const Expr *getExpr() const { return getParam()->getDefaultArg(); }
1288+
Expr *getExpr() { return getParam()->getDefaultArg(); }
13231289

13241290
const DeclContext *getUsedContext() const { return UsedContext; }
13251291
DeclContext *getUsedContext() { return UsedContext; }
@@ -1356,67 +1322,41 @@ class CXXDefaultArgExpr final
13561322
/// is implicitly used in a mem-initializer-list in a constructor
13571323
/// (C++11 [class.base.init]p8) or in aggregate initialization
13581324
/// (C++1y [dcl.init.aggr]p7).
1359-
class CXXDefaultInitExpr final
1360-
: public Expr,
1361-
private llvm::TrailingObjects<CXXDefaultArgExpr, Expr *> {
1362-
1363-
friend class ASTStmtReader;
1325+
class CXXDefaultInitExpr : public Expr {
13641326
friend class ASTReader;
1365-
friend TrailingObjects;
1327+
friend class ASTStmtReader;
1328+
13661329
/// The field whose default is being used.
13671330
FieldDecl *Field;
13681331

13691332
/// The context where the default initializer expression was used.
13701333
DeclContext *UsedContext;
13711334

13721335
CXXDefaultInitExpr(const ASTContext &Ctx, SourceLocation Loc,
1373-
FieldDecl *Field, QualType Ty, DeclContext *UsedContext,
1374-
Expr *RewrittenInitExpr);
1375-
1376-
CXXDefaultInitExpr(EmptyShell Empty, bool HasRewrittenInit)
1377-
: Expr(CXXDefaultInitExprClass, Empty) {
1378-
CXXDefaultInitExprBits.HasRewrittenInit = HasRewrittenInit;
1379-
}
1336+
FieldDecl *Field, QualType Ty, DeclContext *UsedContext);
13801337

1381-
size_t numTrailingObjects() const {
1382-
return CXXDefaultInitExprBits.HasRewrittenInit;
1383-
}
1338+
CXXDefaultInitExpr(EmptyShell Empty) : Expr(CXXDefaultInitExprClass, Empty) {}
13841339

13851340
public:
1386-
static CXXDefaultInitExpr *CreateEmpty(const ASTContext &C,
1387-
bool HasRewrittenInit);
13881341
/// \p Field is the non-static data member whose default initializer is used
13891342
/// by this expression.
13901343
static CXXDefaultInitExpr *Create(const ASTContext &Ctx, SourceLocation Loc,
1391-
FieldDecl *Field, DeclContext *UsedContext,
1392-
Expr *RewrittenInitExpr);
1393-
1394-
bool hasRewrittenInit() const {
1395-
return CXXDefaultInitExprBits.HasRewrittenInit;
1344+
FieldDecl *Field, DeclContext *UsedContext) {
1345+
return new (Ctx) CXXDefaultInitExpr(Ctx, Loc, Field, Field->getType(), UsedContext);
13961346
}
13971347

13981348
/// Get the field whose initializer will be used.
13991349
FieldDecl *getField() { return Field; }
14001350
const FieldDecl *getField() const { return Field; }
14011351

14021352
/// Get the initialization expression that will be used.
1403-
Expr *getExpr();
14041353
const Expr *getExpr() const {
1405-
return const_cast<CXXDefaultInitExpr *>(this)->getExpr();
1406-
}
1407-
1408-
/// Retrieve the initializing expression with evaluated immediate calls, if
1409-
/// any.
1410-
const Expr *getRewrittenExpr() const {
1411-
assert(hasRewrittenInit() && "expected a rewritten init expression");
1412-
return *getTrailingObjects<Expr *>();
1354+
assert(Field->getInClassInitializer() && "initializer hasn't been parsed");
1355+
return Field->getInClassInitializer();
14131356
}
1414-
1415-
/// Retrieve the initializing expression with evaluated immediate calls, if
1416-
/// any.
1417-
Expr *getRewrittenExpr() {
1418-
assert(hasRewrittenInit() && "expected a rewritten init expression");
1419-
return *getTrailingObjects<Expr *>();
1357+
Expr *getExpr() {
1358+
assert(Field->getInClassInitializer() && "initializer hasn't been parsed");
1359+
return Field->getInClassInitializer();
14201360
}
14211361

14221362
const DeclContext *getUsedContext() const { return UsedContext; }

clang/include/clang/AST/Stmt.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -690,9 +690,6 @@ class alignas(void *) Stmt {
690690

691691
unsigned : NumExprBits;
692692

693-
/// Whether this CXXDefaultArgExpr rewrote its argument and stores a copy.
694-
unsigned HasRewrittenInit : 1;
695-
696693
/// The location where the default argument expression was used.
697694
SourceLocation Loc;
698695
};
@@ -703,10 +700,6 @@ class alignas(void *) Stmt {
703700

704701
unsigned : NumExprBits;
705702

706-
/// Whether this CXXDefaultInitExprBitfields rewrote its argument and stores
707-
/// a copy.
708-
unsigned HasRewrittenInit : 1;
709-
710703
/// The location where the default initializer expression was used.
711704
SourceLocation Loc;
712705
};

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2646,10 +2646,6 @@ def err_invalid_consteval_take_address : Error<
26462646
" of an immediate invocation">;
26472647
def err_invalid_consteval_call : Error<
26482648
"call to consteval function %q0 is not a constant expression">;
2649-
def note_invalid_consteval_initializer : Note<
2650-
"in the default initalizer of %0">;
2651-
def note_invalid_consteval_initializer_here : Note<
2652-
"initialized here %0">;
26532649
def err_invalid_consteval_decl_kind : Error<
26542650
"%0 cannot be declared consteval">;
26552651
def err_invalid_constexpr : Error<

clang/include/clang/Sema/Sema.h

Lines changed: 4 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,25 +1330,6 @@ class Sema final {
13301330
bool InDiscardedStatement;
13311331
bool InImmediateFunctionContext;
13321332

1333-
bool IsCurrentlyCheckingDefaultArgumentOrInitializer = false;
1334-
1335-
// When evaluating immediate functions in the initializer of a default
1336-
// argument or default member initializer, this is the declaration whose
1337-
// default initializer is being evaluated and the location of the call
1338-
// or constructor definition.
1339-
struct InitializationContext {
1340-
InitializationContext(SourceLocation Loc, ValueDecl *Decl,
1341-
DeclContext *Context)
1342-
: Loc(Loc), Decl(Decl), Context(Context) {
1343-
assert(Decl && Context && "invalid initialization context");
1344-
}
1345-
1346-
SourceLocation Loc;
1347-
ValueDecl *Decl = nullptr;
1348-
DeclContext *Context = nullptr;
1349-
};
1350-
llvm::Optional<InitializationContext> DelayedDefaultInitializationContext;
1351-
13521333
ExpressionEvaluationContextRecord(ExpressionEvaluationContext Context,
13531334
unsigned NumCleanupObjects,
13541335
CleanupInfo ParentCleanup,
@@ -6229,22 +6210,19 @@ class Sema final {
62296210
bool IsStdInitListInitialization, bool RequiresZeroInit,
62306211
unsigned ConstructKind, SourceRange ParenRange);
62316212

6232-
ExprResult ConvertMemberDefaultInitExpression(FieldDecl *FD, Expr *InitExpr,
6233-
SourceLocation InitLoc);
6234-
62356213
ExprResult BuildCXXDefaultInitExpr(SourceLocation Loc, FieldDecl *Field);
62366214

62376215

62386216
/// Instantiate or parse a C++ default argument expression as necessary.
62396217
/// Return true on error.
62406218
bool CheckCXXDefaultArgExpr(SourceLocation CallLoc, FunctionDecl *FD,
6241-
ParmVarDecl *Param, Expr *Init = nullptr,
6242-
bool SkipImmediateInvocations = true);
6219+
ParmVarDecl *Param);
62436220

62446221
/// BuildCXXDefaultArgExpr - Creates a CXXDefaultArgExpr, instantiating
62456222
/// the default expr if needed.
6246-
ExprResult BuildCXXDefaultArgExpr(SourceLocation CallLoc, FunctionDecl *FD,
6247-
ParmVarDecl *Param, Expr *Init = nullptr);
6223+
ExprResult BuildCXXDefaultArgExpr(SourceLocation CallLoc,
6224+
FunctionDecl *FD,
6225+
ParmVarDecl *Param);
62486226

62496227
/// FinalizeVarWithDestructor - Prepare for calling destructor on the
62506228
/// constructed variable.
@@ -9648,63 +9626,6 @@ class Sema final {
96489626
return ExprEvalContexts.back().isImmediateFunctionContext();
96499627
}
96509628

9651-
bool isCheckingDefaultArgumentOrInitializer() const {
9652-
assert(!ExprEvalContexts.empty() &&
9653-
"Must be in an expression evaluation context");
9654-
const ExpressionEvaluationContextRecord &Ctx = ExprEvalContexts.back();
9655-
return (Ctx.Context ==
9656-
ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed) ||
9657-
Ctx.IsCurrentlyCheckingDefaultArgumentOrInitializer;
9658-
}
9659-
9660-
bool isCheckingDefaultArgumentOrInitializerOfOuterEntity() const {
9661-
assert(!ExprEvalContexts.empty() &&
9662-
"Must be in an expression evaluation context");
9663-
for (const auto &Ctx : llvm::reverse(ExprEvalContexts)) {
9664-
if ((Ctx.Context ==
9665-
ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed) ||
9666-
Ctx.IsCurrentlyCheckingDefaultArgumentOrInitializer)
9667-
return true;
9668-
if (Ctx.isConstantEvaluated() || Ctx.isImmediateFunctionContext() ||
9669-
Ctx.isUnevaluated())
9670-
return false;
9671-
}
9672-
return false;
9673-
}
9674-
9675-
llvm::Optional<ExpressionEvaluationContextRecord::InitializationContext>
9676-
InnermostDeclarationWithDelayedImmediateInvocations() const {
9677-
assert(!ExprEvalContexts.empty() &&
9678-
"Must be in an expression evaluation context");
9679-
for (const auto &Ctx : llvm::reverse(ExprEvalContexts)) {
9680-
if (Ctx.Context == ExpressionEvaluationContext::PotentiallyEvaluated &&
9681-
Ctx.DelayedDefaultInitializationContext)
9682-
return Ctx.DelayedDefaultInitializationContext;
9683-
if (Ctx.isConstantEvaluated() || Ctx.isImmediateFunctionContext() ||
9684-
Ctx.isUnevaluated())
9685-
break;
9686-
}
9687-
return llvm::None;
9688-
}
9689-
9690-
llvm::Optional<ExpressionEvaluationContextRecord::InitializationContext>
9691-
OutermostDeclarationWithDelayedImmediateInvocations() const {
9692-
assert(!ExprEvalContexts.empty() &&
9693-
"Must be in an expression evaluation context");
9694-
llvm::Optional<ExpressionEvaluationContextRecord::InitializationContext>
9695-
Res;
9696-
for (auto &Ctx : llvm::reverse(ExprEvalContexts)) {
9697-
if (Ctx.Context == ExpressionEvaluationContext::PotentiallyEvaluated &&
9698-
!Ctx.DelayedDefaultInitializationContext && Res)
9699-
break;
9700-
if (Ctx.isConstantEvaluated() || Ctx.isImmediateFunctionContext() ||
9701-
Ctx.isUnevaluated())
9702-
break;
9703-
Res = Ctx.DelayedDefaultInitializationContext;
9704-
}
9705-
return Res;
9706-
}
9707-
97089629
/// RAII class used to determine whether SFINAE has
97099630
/// trapped any errors that occur during template argument
97109631
/// deduction.

clang/lib/AST/ASTImporter.cpp

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7687,16 +7687,9 @@ ExpectedStmt ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
76877687
if (Error Err = ImportDefaultArgOfParmVarDecl(*FromParam, ToParam))
76887688
return std::move(Err);
76897689
}
7690-
Expr *RewrittenInit = nullptr;
7691-
if (E->hasRewrittenInit()) {
7692-
ExpectedExpr ExprOrErr = import(E->getExpr());
7693-
if (!ExprOrErr)
7694-
return ExprOrErr.takeError();
7695-
RewrittenInit = ExprOrErr.get();
7696-
}
7690+
76977691
return CXXDefaultArgExpr::Create(Importer.getToContext(), *ToUsedLocOrErr,
7698-
*ToParamOrErr, RewrittenInit,
7699-
*UsedContextOrErr);
7692+
*ToParamOrErr, *UsedContextOrErr);
77007693
}
77017694

77027695
ExpectedStmt
@@ -8388,16 +8381,8 @@ ExpectedStmt ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) {
83888381
ToField->setInClassInitializer(*ToInClassInitializerOrErr);
83898382
}
83908383

8391-
Expr *RewrittenInit = nullptr;
8392-
if (E->hasRewrittenInit()) {
8393-
ExpectedExpr ExprOrErr = import(E->getExpr());
8394-
if (!ExprOrErr)
8395-
return ExprOrErr.takeError();
8396-
RewrittenInit = ExprOrErr.get();
8397-
}
8398-
83998384
return CXXDefaultInitExpr::Create(Importer.getToContext(), *ToBeginLocOrErr,
8400-
ToField, *UsedContextOrErr, RewrittenInit);
8385+
ToField, *UsedContextOrErr);
84018386
}
84028387

84038388
ExpectedStmt ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {

clang/lib/AST/Decl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2888,7 +2888,8 @@ Expr *ParmVarDecl::getDefaultArg() {
28882888

28892889
Expr *Arg = getInit();
28902890
if (auto *E = dyn_cast_or_null<FullExpr>(Arg))
2891-
return E->getSubExpr();
2891+
if (!isa<ConstantExpr>(E))
2892+
return E->getSubExpr();
28922893

28932894
return Arg;
28942895
}

0 commit comments

Comments
 (0)