[clang] c286d6f - [Parser] Suppress -Wempty-translation-unit if this is a header file

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 13 00:44:07 PDT 2020


Author: Sam McCall
Date: 2020-08-13T09:43:27+02:00
New Revision: c286d6fdeeb2b103777d3ae8c9de84937cb51efe

URL: https://github.com/llvm/llvm-project/commit/c286d6fdeeb2b103777d3ae8c9de84937cb51efe
DIFF: https://github.com/llvm/llvm-project/commit/c286d6fdeeb2b103777d3ae8c9de84937cb51efe.diff

LOG: [Parser] Suppress -Wempty-translation-unit if this is a header file

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.

Differential Revision: https://reviews.llvm.org/D85789

Added: 
    

Modified: 
    clang/lib/Parse/Parser.cpp
    clang/test/Parser/empty-translation-unit.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/Parse/Parser.cpp b/clang/lib/Parse/Parser.cpp
index 45cf855cf8c9..5e485eda831c 100644
--- a/clang/lib/Parse/Parser.cpp
+++ b/clang/lib/Parse/Parser.cpp
@@ -552,9 +552,10 @@ bool Parser::ParseFirstTopLevelDecl(DeclGroupPtrTy &Result) {
   // 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
index 04e57f69ce87..991d98eb52fa 100644
--- 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}}


        


More information about the cfe-commits mailing list