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

Sam McCall via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 11 15:07:05 PDT 2020


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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D85789.284908.patch
Type: text/x-patch
Size: 1572 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200811/537ff225/attachment.bin>


More information about the cfe-commits mailing list