Skip to content

Commit 41e0881

Browse files
author
Anders Carlsson
committed
Initial sema support for C++ static initializers.
llvm-svn: 55166
1 parent dec5170 commit 41e0881

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

clang/lib/Sema/SemaDecl.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,8 +1325,12 @@ void Sema::AddInitializerToDecl(DeclTy *dcl, ExprTy *init) {
13251325
} else if (!VDecl->isInvalidDecl()) {
13261326
if (CheckInitializerTypes(Init, DclT))
13271327
VDecl->setInvalidDecl();
1328-
if (SC == VarDecl::Static) // C99 6.7.8p4.
1329-
CheckForConstantInitializer(Init, DclT);
1328+
1329+
// C++ 3.6.2p2, allow dynamic initialization of static initializers.
1330+
if (!getLangOptions().CPlusPlus) {
1331+
if (SC == VarDecl::Static) // C99 6.7.8p4.
1332+
CheckForConstantInitializer(Init, DclT);
1333+
}
13301334
}
13311335
} else if (VDecl->isFileVarDecl()) {
13321336
if (VDecl->getStorageClass() == VarDecl::Extern)
@@ -1335,8 +1339,11 @@ void Sema::AddInitializerToDecl(DeclTy *dcl, ExprTy *init) {
13351339
if (CheckInitializerTypes(Init, DclT))
13361340
VDecl->setInvalidDecl();
13371341

1338-
// C99 6.7.8p4. All file scoped initializers need to be constant.
1339-
CheckForConstantInitializer(Init, DclT);
1342+
// C++ 3.6.2p2, allow dynamic initialization of static initializers.
1343+
if (!getLangOptions().CPlusPlus) {
1344+
// C99 6.7.8p4. All file scoped initializers need to be constant.
1345+
CheckForConstantInitializer(Init, DclT);
1346+
}
13401347
}
13411348
// If the type changed, it means we had an incomplete type that was
13421349
// completed by the initializer. For example:
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: clang -fsyntax-only -verify %s
2+
int f()
3+
{
4+
return 10;
5+
}
6+
7+
void g()
8+
{
9+
static int a = f();
10+
}
11+
12+
static int b = f();

0 commit comments

Comments
 (0)