sammccall created this revision. sammccall added a reviewer: kadircet. Herald added subscribers: cfe-commits, usaxena95. Herald added a project: clang. sammccall requested review of this revision. Herald added a subscriber: ilya-biryukov.
This is motivated by tooling (clangd, libclang etc) - headers without declarations are legitimate even if they're not valid TUs. The other use -x c-header cases (PCH/modules) are nonstandard anyway and this warning doesn't seem necessary there either. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D85789 Files: clang/lib/Parse/Parser.cpp clang/test/Parser/empty-translation-unit.c Index: clang/test/Parser/empty-translation-unit.c =================================================================== --- clang/test/Parser/empty-translation-unit.c +++ 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}} Index: clang/lib/Parse/Parser.cpp =================================================================== --- clang/lib/Parse/Parser.cpp +++ 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;
Index: clang/test/Parser/empty-translation-unit.c =================================================================== --- clang/test/Parser/empty-translation-unit.c +++ 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}} Index: clang/lib/Parse/Parser.cpp =================================================================== --- clang/lib/Parse/Parser.cpp +++ 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;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits