]> granicus.if.org Git - clang/commitdiff
Suppress constant initializer checking when the declaration isn't valid.
authorEli Friedman <eli.friedman@gmail.com>
Fri, 20 Feb 2009 01:34:21 +0000 (01:34 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Fri, 20 Feb 2009 01:34:21 +0000 (01:34 +0000)
This prevents emitting diagnostics which are almost certainly useless.

(Note that the test is checking that we emit only one diagnostic.)

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@65101 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaDecl.cpp
test/Sema/invalid-init-diag.c [new file with mode: 0644]

index 7b54b8bc7c84bfd104de232162f6a61c35191c13..5a18aeb0accaa0c6e8506ee91885e2b612fc6df4 100644 (file)
@@ -2457,7 +2457,8 @@ void Sema::AddInitializerToDecl(DeclTy *dcl, ExprArg init, bool DirectInit) {
         VDecl->setInvalidDecl();
       
       // C++ 3.6.2p2, allow dynamic initialization of static initializers.
-      if (!getLangOptions().CPlusPlus) {
+      // Don't check invalid declarations to avoid emitting useless diagnostics.
+      if (!getLangOptions().CPlusPlus && !VDecl->isInvalidDecl()) {
         if (SC == VarDecl::Static) // C99 6.7.8p4.
           CheckForConstantInitializer(Init, DclT);
       }
@@ -2471,7 +2472,8 @@ void Sema::AddInitializerToDecl(DeclTy *dcl, ExprArg init, bool DirectInit) {
         VDecl->setInvalidDecl();
     
     // C++ 3.6.2p2, allow dynamic initialization of static initializers.
-    if (!getLangOptions().CPlusPlus) {
+    // Don't check invalid declarations to avoid emitting useless diagnostics.
+    if (!getLangOptions().CPlusPlus && !VDecl->isInvalidDecl()) {
       // C99 6.7.8p4. All file scoped initializers need to be constant.
       CheckForConstantInitializer(Init, DclT);
     }
diff --git a/test/Sema/invalid-init-diag.c b/test/Sema/invalid-init-diag.c
new file mode 100644 (file)
index 0000000..8eaefa6
--- /dev/null
@@ -0,0 +1,4 @@
+// RUN: clang %s -verify -fsyntax-only
+
+int a;
+struct {int x;} x = a; // expected-error {{incompatible type initializing 'int', expected 'struct <anonymous>'}}