From: Eli Friedman Date: Fri, 20 Feb 2009 01:34:21 +0000 (+0000) Subject: Suppress constant initializer checking when the declaration isn't valid. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=da153239bb6e69722fdd82914e729bb39f5821c5;p=clang Suppress constant initializer checking when the declaration isn't valid. 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 --- diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 7b54b8bc7c..5a18aeb0ac 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -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 index 0000000000..8eaefa6cd1 --- /dev/null +++ b/test/Sema/invalid-init-diag.c @@ -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 '}}