diff --git a/clang/lib/Parse/Parser.cpp b/clang/lib/Parse/Parser.cpp --- a/clang/lib/Parse/Parser.cpp +++ b/clang/lib/Parse/Parser.cpp @@ -552,9 +552,10 @@ // declaration. C++ doesn't have this restriction. We also don't want to // complain if we have a precompiled header, although technically if the PCH // is empty we should still emit the (pedantic) diagnostic. + // If the main file is a header, we're only pretending it's a TU; don't warn. bool NoTopLevelDecls = ParseTopLevelDecl(Result, true); if (NoTopLevelDecls && !Actions.getASTContext().getExternalSource() && - !getLangOpts().CPlusPlus) + !getLangOpts().CPlusPlus && !getLangOpts().IsHeaderFile) Diag(diag::ext_empty_translation_unit); return NoTopLevelDecls; diff --git a/clang/test/Parser/empty-translation-unit.c b/clang/test/Parser/empty-translation-unit.c --- a/clang/test/Parser/empty-translation-unit.c +++ b/clang/test/Parser/empty-translation-unit.c @@ -1,4 +1,5 @@ // RUN: %clang_cc1 -fsyntax-only -std=c99 -pedantic -W -verify %s +// RUN: %clang_cc1 -fsyntax-only -x c-header -std=c99 -pedantic-errors -W %s // RUN: %clang_cc1 -fsyntax-only -x c++ -std=c++03 -pedantic-errors -W %s #include "completely-empty-header-file.h" @@ -6,5 +7,5 @@ #define A_MACRO_IS_NOT_GOOD_ENOUGH 1 -// In C we should get this warning, but in C++ we shouldn't. +// In C we should get this warning, but in C++ (or a header) we shouldn't. // expected-warning{{ISO C requires a translation unit to contain at least one declaration}}