r285295 - Do not print include_next/pragma once warnings when input is a header.

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 27 09:33:05 PDT 2016


On 27 Oct 2016 7:26 am, "Erik Verbruggen via cfe-commits" <
cfe-commits at lists.llvm.org> wrote:

Author: erikjv
Date: Thu Oct 27 09:17:10 2016
New Revision: 285295

URL: http://llvm.org/viewvc/llvm-project?rev=285295&view=rev
Log:
Do not print include_next/pragma once warnings when input is a header.

r276653 suppressed the pragma once warning when generating a PCH file.
This patch extends that to any main file for which clang is told (with
the -x option) that it's a header file. It will also suppress the
warning "#include_next in primary source file".

Differential Revision: http://reviews.llvm.org/D25989

Added:
    cfe/trunk/test/Preprocessor/header_is_main_file.c
Modified:
    cfe/trunk/include/clang/Basic/LangOptions.h
    cfe/trunk/lib/Basic/LangOptions.cpp
    cfe/trunk/lib/Frontend/CompilerInvocation.cpp
    cfe/trunk/lib/Lex/PPDirectives.cpp
    cfe/trunk/lib/Lex/PPMacroExpansion.cpp
    cfe/trunk/lib/Lex/Pragma.cpp

Modified: cfe/trunk/include/clang/Basic/LangOptions.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/
clang/Basic/LangOptions.h?rev=285295&r1=285294&r2=285295&view=diff
============================================================
==================
--- cfe/trunk/include/clang/Basic/LangOptions.h (original)
+++ cfe/trunk/include/clang/Basic/LangOptions.h Thu Oct 27 09:17:10 2016
@@ -132,6 +132,10 @@ public:
   /// host code generation.
   std::string OMPHostIRFile;

+  /// \brief Indicates whether the front-end is explicitly told that the
+  /// input is a header file (i.e. -x c-header).
+  bool IsHeaderFile;
+
   LangOptions();

   // Define accessors/mutators for language options of enumeration type.

Modified: cfe/trunk/lib/Basic/LangOptions.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/
LangOptions.cpp?rev=285295&r1=285294&r2=285295&view=diff
============================================================
==================
--- cfe/trunk/lib/Basic/LangOptions.cpp (original)
+++ cfe/trunk/lib/Basic/LangOptions.cpp Thu Oct 27 09:17:10 2016
@@ -15,7 +15,8 @@

 using namespace clang;

-LangOptions::LangOptions() {
+LangOptions::LangOptions()
+  : IsHeaderFile(false) {
 #define LANGOPT(Name, Bits, Default, Description) Name = Default;
 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description)
set##Name(Default);
 #include "clang/Basic/LangOptions.def"
@@ -34,6 +35,7 @@ void LangOptions::resetNonModularOptions
   SanitizerBlacklistFiles.clear();

   CurrentModule.clear();
+  IsHeaderFile = false;
 }

 bool LangOptions::isNoBuiltinFunc(StringRef FuncName) const {

Modified: cfe/trunk/lib/Frontend/CompilerInvocation.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/
Frontend/CompilerInvocation.cpp?rev=285295&r1=285294&r2=285295&view=diff
============================================================
==================
--- cfe/trunk/lib/Frontend/CompilerInvocation.cpp (original)
+++ cfe/trunk/lib/Frontend/CompilerInvocation.cpp Thu Oct 27 09:17:10 2016
@@ -1111,7 +1111,8 @@ static bool parseTestModuleFileExtension
 }

 static InputKind ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args,
-                                   DiagnosticsEngine &Diags) {
+                                   DiagnosticsEngine &Diags,
+                                   bool &IsHeaderFile) {
   using namespace options;
   Opts.ProgramAction = frontend::ParseSyntaxOnly;
   if (const Arg *A = Args.getLastArg(OPT_Action_Group)) {
@@ -1358,6 +1359,13 @@ static InputKind ParseFrontendArgs(Front
     if (DashX == IK_None)
       Diags.Report(diag::err_drv_invalid_value)
         << A->getAsString(Args) << A->getValue();
+    IsHeaderFile = llvm::StringSwitch<bool>(A->getValue())


Is there a reason to keep this separate from TUKind? TU_Prefix and this
flag appear to mean the same thing.

+      .Case("c-header", true)
+      .Case("cl-header", true)
+      .Case("objective-c-header", true)
+      .Case("c++-header", true)
+      .Case("objective-c++-header", true)
+      .Default(false);
   }

   // '-' is the default input if none is given.
@@ -2415,7 +2423,8 @@ bool CompilerInvocation::CreateFromArgs(
   ParseCommentArgs(LangOpts.CommentOpts, Args);
   ParseFileSystemArgs(Res.getFileSystemOpts(), Args);
   // FIXME: We shouldn't have to pass the DashX option around here
-  InputKind DashX = ParseFrontendArgs(Res.getFrontendOpts(), Args, Diags);
+  InputKind DashX = ParseFrontendArgs(Res.getFrontendOpts(), Args, Diags,
+                                      LangOpts.IsHeaderFile);
   ParseTargetArgs(Res.getTargetOpts(), Args, Diags);
   Success &= ParseCodeGenArgs(Res.getCodeGenOpts(), Args, DashX, Diags,
                               Res.getTargetOpts());

Modified: cfe/trunk/lib/Lex/PPDirectives.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/
PPDirectives.cpp?rev=285295&r1=285294&r2=285295&view=diff
============================================================
==================
--- cfe/trunk/lib/Lex/PPDirectives.cpp (original)
+++ cfe/trunk/lib/Lex/PPDirectives.cpp Thu Oct 27 09:17:10 2016
@@ -2058,7 +2058,11 @@ void Preprocessor::HandleIncludeNextDire
   // diagnostic.
   const DirectoryLookup *Lookup = CurDirLookup;
   const FileEntry *LookupFromFile = nullptr;
-  if (isInPrimaryFile()) {
+  if (isInPrimaryFile() && LangOpts.IsHeaderFile) {
+    // If the main file is a header, then it's either for PCH/AST
generation,
+    // or libclang opened it. Either way, handle it as a normal include
below
+    // and do not complain about include_next.
+  } else if (isInPrimaryFile()) {
     Lookup = nullptr;
     Diag(IncludeNextTok, diag::pp_include_next_in_primary);
   } else if (CurSubmodule) {

Modified: cfe/trunk/lib/Lex/PPMacroExpansion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/
PPMacroExpansion.cpp?rev=285295&r1=285294&r2=285295&view=diff
============================================================
==================
--- cfe/trunk/lib/Lex/PPMacroExpansion.cpp (original)
+++ cfe/trunk/lib/Lex/PPMacroExpansion.cpp Thu Oct 27 09:17:10 2016
@@ -1421,7 +1421,11 @@ static bool EvaluateHasIncludeNext(Token
   // Preprocessor::HandleIncludeNextDirective.
   const DirectoryLookup *Lookup = PP.GetCurDirLookup();
   const FileEntry *LookupFromFile = nullptr;
-  if (PP.isInPrimaryFile()) {
+  if (PP.isInPrimaryFile() && PP.getLangOpts().IsHeaderFile) {
+    // If the main file is a header, then it's either for PCH/AST
generation,
+    // or libclang opened it. Either way, handle it as a normal include
below
+    // and do not complain about __has_include_next.
+  } else if (PP.isInPrimaryFile()) {
     Lookup = nullptr;
     PP.Diag(Tok, diag::pp_include_next_in_primary);
   } else if (PP.getCurrentSubmodule()) {

Modified: cfe/trunk/lib/Lex/Pragma.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/
Pragma.cpp?rev=285295&r1=285294&r2=285295&view=diff
============================================================
==================
--- cfe/trunk/lib/Lex/Pragma.cpp (original)
+++ cfe/trunk/lib/Lex/Pragma.cpp Thu Oct 27 09:17:10 2016
@@ -372,8 +372,10 @@ void Preprocessor::HandleMicrosoft__prag
 ///
 void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
   // Don't honor the 'once' when handling the primary source file, unless
-  // this is a prefix to a TU, which indicates we're generating a PCH file.
-  if (isInPrimaryFile() && TUKind != TU_Prefix) {
+  // this is a prefix to a TU, which indicates we're generating a PCH
file, or
+  // when the main file is a header (e.g. when -xc-header is provided on
the
+  // commandline).
+  if (isInPrimaryFile() && TUKind != TU_Prefix &&
!getLangOpts().IsHeaderFile) {
     Diag(OnceTok, diag::pp_pragma_once_in_main_file);
     return;
   }

Added: cfe/trunk/test/Preprocessor/header_is_main_file.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/
Preprocessor/header_is_main_file.c?rev=285295&view=auto
============================================================
==================
--- cfe/trunk/test/Preprocessor/header_is_main_file.c (added)
+++ cfe/trunk/test/Preprocessor/header_is_main_file.c Thu Oct 27 09:17:10
2016
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -x c-header -ffreestanding -Eonly -verify %s
+// expected-no-diagnostics
+
+#pragma once
+#include_next "stdint.h"
+#if !__has_include_next("stdint.h")
+#error "__has_include_next failed"
+#endif


_______________________________________________
cfe-commits mailing list
cfe-commits at lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20161027/6e6244b6/attachment-0001.html>


More information about the cfe-commits mailing list