Skip to content

Commit b3e6ff3

Browse files
authored
[clang-cl] Add support for [[msvc::constexpr]] C++11 attribute (#71300)
This commit introduces support for the MSVC-specific C++11-style attribute `[[msvc::constexpr]]`, which was introduced in MSVC 14.33. The semantics of this attribute are enabled only under MSVC compatibility (`-fms-compatibility-version`) 14.33 and higher. Additionally, the default value of `_MSC_VER` has been raised to 1433. The current implementation lacks support for: - `[[msvc::constexpr]]` constructors (see #72149); at the time of this implementation, such support would have required an unreasonable number of changes in Clang. - `[[msvc::constexpr]] return ::new` (constexpr placement new) from non-std namespaces (see #74924). Relevant to: #57696
1 parent b3000ec commit b3e6ff3

18 files changed

+239
-13
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ Non-comprehensive list of changes in this release
238238
except that it returns the size of a type ignoring tail padding.
239239
* ``__builtin_classify_type()`` now classifies ``_BitInt`` values as the return value ``18``
240240
and vector types as return value ``19``, to match GCC 14's behavior.
241+
* The default value of `_MSC_VER` was raised from 1920 to 1933.
242+
* Since MSVC 19.33 added undocumented attribute ``[[msvc::constexpr]]``, this release adds the attribute as well.
241243

242244
* Added ``#pragma clang fp reciprocal``.
243245

clang/docs/UsersManual.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3359,8 +3359,8 @@ default for Windows targets.
33593359

33603360
For compatibility with existing code that compiles with MSVC, clang defines the
33613361
``_MSC_VER`` and ``_MSC_FULL_VER`` macros. When on Windows, these default to
3362-
either the same value as the currently installed version of cl.exe, or ``1920``
3363-
and ``192000000`` (respectively). The ``-fms-compatibility-version=`` flag
3362+
either the same value as the currently installed version of cl.exe, or ``1933``
3363+
and ``193300000`` (respectively). The ``-fms-compatibility-version=`` flag
33643364
overrides these values. It accepts a dotted version tuple, such as 19.00.23506.
33653365
Changing the MSVC compatibility version makes clang behave more like that
33663366
version of MSVC. For example, ``-fms-compatibility-version=19`` will enable

clang/include/clang/Basic/Attr.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3646,6 +3646,14 @@ def : MutualExclusions<[Owner, Pointer]>;
36463646

36473647
// Microsoft-related attributes
36483648

3649+
def MSConstexpr : InheritableAttr {
3650+
let LangOpts = [MicrosoftExt];
3651+
let Spellings = [CXX11<"msvc", "constexpr">];
3652+
let Subjects = SubjectList<[Function, ReturnStmt], ErrorDiag,
3653+
"functions and return statements">;
3654+
let Documentation = [MSConstexprDocs];
3655+
}
3656+
36493657
def MSNoVTable : InheritableAttr, TargetSpecificAttr<TargetMicrosoftCXXABI> {
36503658
let Spellings = [Declspec<"novtable">];
36513659
let Subjects = SubjectList<[CXXRecord]>;

clang/include/clang/Basic/AttrDocs.td

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3657,6 +3657,21 @@ an error:
36573657
}];
36583658
}
36593659

3660+
def MSConstexprDocs : Documentation {
3661+
let Category = DocCatStmt;
3662+
let Content = [{
3663+
The ``[[msvc::constexpr]]`` attribute can be applied only to a function
3664+
definition or a ``return`` statement. It does not impact function declarations.
3665+
A ``[[msvc::constexpr]]`` function cannot be ``constexpr`` or ``consteval``.
3666+
A ``[[msvc::constexpr]]`` function is treated as if it were a ``constexpr`` function
3667+
when it is evaluated in a constant context of ``[[msvc::constexpr]] return`` statement.
3668+
Otherwise, it is treated as a regular function.
3669+
3670+
Semantics of this attribute are enabled only under MSVC compatibility
3671+
(``-fms-compatibility-version``) 19.33 and later.
3672+
}];
3673+
}
3674+
36603675
def MSNoVTableDocs : Documentation {
36613676
let Category = DocCatDecl;
36623677
let Content = [{

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2884,6 +2884,8 @@ def warn_cxx11_compat_constexpr_body_multiple_return : Warning<
28842884
InGroup<CXXPre14Compat>, DefaultIgnore;
28852885
def note_constexpr_body_previous_return : Note<
28862886
"previous return statement is here">;
2887+
def err_ms_constexpr_cannot_be_applied : Error<
2888+
"attribute 'msvc::constexpr' cannot be applied to the %select{constexpr|consteval|virtual}0 function %1">;
28872889

28882890
// C++20 function try blocks in constexpr
28892891
def ext_constexpr_function_try_block_cxx20 : ExtWarn<

clang/include/clang/Basic/LangOptions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ class LangOptions : public LangOptionsBase {
152152
MSVC2019 = 1920,
153153
MSVC2019_5 = 1925,
154154
MSVC2019_8 = 1928,
155+
MSVC2022_3 = 1933,
155156
};
156157

157158
enum SYCLMajorVersion {

clang/lib/AST/ExprConstant.cpp

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,10 @@ namespace {
641641
return false;
642642
}
643643

644+
/// Whether we're in a context where [[msvc::constexpr]] evaluation is
645+
/// permitted. See MSConstexprDocs for description of permitted contexts.
646+
bool CanEvalMSConstexpr = false;
647+
644648
private:
645649
APValue &createLocal(APValue::LValueBase Base, const void *Key, QualType T,
646650
ScopeKind Scope);
@@ -674,6 +678,19 @@ namespace {
674678
private:
675679
llvm::TimeTraceScope TimeScope;
676680
};
681+
682+
/// RAII object used to change the current ability of
683+
/// [[msvc::constexpr]] evaulation.
684+
struct MSConstexprContextRAII {
685+
CallStackFrame &Frame;
686+
bool OldValue;
687+
explicit MSConstexprContextRAII(CallStackFrame &Frame, bool Value)
688+
: Frame(Frame), OldValue(Frame.CanEvalMSConstexpr) {
689+
Frame.CanEvalMSConstexpr = Value;
690+
}
691+
692+
~MSConstexprContextRAII() { Frame.CanEvalMSConstexpr = OldValue; }
693+
};
677694
}
678695

679696
static bool HandleDestruction(EvalInfo &Info, const Expr *E,
@@ -5546,11 +5563,14 @@ static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info,
55465563
case Stmt::LabelStmtClass:
55475564
return EvaluateStmt(Result, Info, cast<LabelStmt>(S)->getSubStmt(), Case);
55485565

5549-
case Stmt::AttributedStmtClass:
5550-
// As a general principle, C++11 attributes can be ignored without
5551-
// any semantic impact.
5552-
return EvaluateStmt(Result, Info, cast<AttributedStmt>(S)->getSubStmt(),
5553-
Case);
5566+
case Stmt::AttributedStmtClass: {
5567+
const auto *AS = cast<AttributedStmt>(S);
5568+
const auto *SS = AS->getSubStmt();
5569+
MSConstexprContextRAII ConstexprContext(
5570+
*Info.CurrentCall, hasSpecificAttr<MSConstexprAttr>(AS->getAttrs()) &&
5571+
isa<ReturnStmt>(SS));
5572+
return EvaluateStmt(Result, Info, SS, Case);
5573+
}
55545574

55555575
case Stmt::CaseStmtClass:
55565576
case Stmt::DefaultStmtClass:
@@ -5621,7 +5641,9 @@ static bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc,
56215641
}
56225642

56235643
// Can we evaluate this function call?
5624-
if (Definition && Definition->isConstexpr() && Body)
5644+
if (Definition && Body &&
5645+
(Definition->isConstexpr() || Info.CurrentCall->CanEvalMSConstexpr &&
5646+
Definition->hasAttr<MSConstexprAttr>()))
56255647
return true;
56265648

56275649
if (Info.getLangOpts().CPlusPlus11) {

clang/lib/Basic/Targets/OSTargets.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ static void addVisualCDefines(const LangOptions &Opts, MacroBuilder &Builder) {
224224
else if (Opts.CPlusPlus14)
225225
Builder.defineMacro("_MSVC_LANG", "201402L");
226226
}
227+
228+
if (Opts.isCompatibleWithMSVC(LangOptions::MSVC2022_3))
229+
Builder.defineMacro("_MSVC_CONSTEXPR_ATTRIBUTE");
227230
}
228231

229232
if (Opts.MicrosoftExt) {

clang/lib/Driver/ToolChains/MSVC.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -787,11 +787,11 @@ VersionTuple MSVCToolChain::computeMSVCVersion(const Driver *D,
787787
if (MSVT.empty() &&
788788
Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
789789
IsWindowsMSVC)) {
790-
// -fms-compatibility-version=19.20 is default, aka 2019, 16.x
790+
// -fms-compatibility-version=19.33 is default, aka 2022, 17.3
791791
// NOTE: when changing this value, also update
792792
// clang/docs/CommandGuide/clang.rst and clang/docs/UsersManual.rst
793793
// accordingly.
794-
MSVT = VersionTuple(19, 20);
794+
MSVT = VersionTuple(19, 33);
795795
}
796796
return MSVT;
797797
}

clang/lib/Sema/SemaDecl.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16221,7 +16221,9 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt *Body,
1622116221
ActivePolicy = &WP;
1622216222
}
1622316223

16224-
if (!IsInstantiation && FD && FD->isConstexpr() && !FD->isInvalidDecl() &&
16224+
if (!IsInstantiation && FD &&
16225+
(FD->isConstexpr() || FD->hasAttr<MSConstexprAttr>()) &&
16226+
!FD->isInvalidDecl() &&
1622516227
!CheckConstexprFunctionDefinition(FD, CheckConstexprKind::Diagnose))
1622616228
FD->setInvalidDecl();
1622716229

0 commit comments

Comments
 (0)