]> granicus.if.org Git - clang/commitdiff
Initial sema support for C++ static initializers.
authorAnders Carlsson <andersca@mac.com>
Fri, 22 Aug 2008 05:00:02 +0000 (05:00 +0000)
committerAnders Carlsson <andersca@mac.com>
Fri, 22 Aug 2008 05:00:02 +0000 (05:00 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@55166 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaDecl.cpp
test/SemaCXX/static-initializers.cpp [new file with mode: 0644]

index 021673bf071276c014ba006425bd5c9f567a6f61..d8d77b0d5427350da0324686c3e9272a061e6e0b 100644 (file)
@@ -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 (file)
index 0000000..0da412a
--- /dev/null
@@ -0,0 +1,12 @@
+// RUN: clang -fsyntax-only -verify %s 
+int f()
+{
+       return 10;
+}
+
+void g()
+{
+       static int a = f();
+}
+
+static int b = f();