From c5eb7311445bb14b6a26eb2ad667fe7a1ca20887 Mon Sep 17 00:00:00 2001 From: Anders Carlsson Date: Fri, 22 Aug 2008 05:00:02 +0000 Subject: [PATCH] Initial sema support for C++ static initializers. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@55166 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaDecl.cpp | 15 +++++++++++---- test/SemaCXX/static-initializers.cpp | 12 ++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 test/SemaCXX/static-initializers.cpp diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 021673bf07..d8d77b0d54 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -1325,8 +1325,12 @@ void Sema::AddInitializerToDecl(DeclTy *dcl, ExprTy *init) { } else if (!VDecl->isInvalidDecl()) { if (CheckInitializerTypes(Init, DclT)) VDecl->setInvalidDecl(); - if (SC == VarDecl::Static) // C99 6.7.8p4. - CheckForConstantInitializer(Init, DclT); + + // C++ 3.6.2p2, allow dynamic initialization of static initializers. + if (!getLangOptions().CPlusPlus) { + if (SC == VarDecl::Static) // C99 6.7.8p4. + CheckForConstantInitializer(Init, DclT); + } } } else if (VDecl->isFileVarDecl()) { if (VDecl->getStorageClass() == VarDecl::Extern) @@ -1335,8 +1339,11 @@ void Sema::AddInitializerToDecl(DeclTy *dcl, ExprTy *init) { if (CheckInitializerTypes(Init, DclT)) VDecl->setInvalidDecl(); - // C99 6.7.8p4. All file scoped initializers need to be constant. - CheckForConstantInitializer(Init, DclT); + // C++ 3.6.2p2, allow dynamic initialization of static initializers. + if (!getLangOptions().CPlusPlus) { + // C99 6.7.8p4. All file scoped initializers need to be constant. + CheckForConstantInitializer(Init, DclT); + } } // If the type changed, it means we had an incomplete type that was // completed by the initializer. For example: diff --git a/test/SemaCXX/static-initializers.cpp b/test/SemaCXX/static-initializers.cpp new file mode 100644 index 0000000000..0da412ab29 --- /dev/null +++ b/test/SemaCXX/static-initializers.cpp @@ -0,0 +1,12 @@ +// RUN: clang -fsyntax-only -verify %s +int f() +{ + return 10; +} + +void g() +{ + static int a = f(); +} + +static int b = f(); -- 2.40.0