From da153239bb6e69722fdd82914e729bb39f5821c5 Mon Sep 17 00:00:00 2001 From: Eli Friedman Date: Fri, 20 Feb 2009 01:34:21 +0000 Subject: [PATCH] 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 --- lib/Sema/SemaDecl.cpp | 6 ++++-- test/Sema/invalid-init-diag.c | 4 ++++ 2 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 test/Sema/invalid-init-diag.c 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 '}} -- 2.40.0