From e0bde7554ce04ef7af99c69d526882a56f54a928 Mon Sep 17 00:00:00 2001 From: Erik Verbruggen Date: Thu, 27 Oct 2016 14:17:10 +0000 Subject: [PATCH] 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 llvm-svn: 285295 --- clang/include/clang/Basic/LangOptions.h | 4 ++++ clang/lib/Basic/LangOptions.cpp | 4 +++- clang/lib/Frontend/CompilerInvocation.cpp | 13 +++++++++++-- clang/lib/Lex/PPDirectives.cpp | 6 +++++- clang/lib/Lex/PPMacroExpansion.cpp | 6 +++++- clang/lib/Lex/Pragma.cpp | 6 ++++-- clang/test/Preprocessor/header_is_main_file.c | 8 ++++++++ 7 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 clang/test/Preprocessor/header_is_main_file.c diff --git a/clang/include/clang/Basic/LangOptions.h b/clang/include/clang/Basic/LangOptions.h index d4ecd1f7656b..10635b11225e 100644 --- a/clang/include/clang/Basic/LangOptions.h +++ b/clang/include/clang/Basic/LangOptions.h @@ -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. diff --git a/clang/lib/Basic/LangOptions.cpp b/clang/lib/Basic/LangOptions.cpp index 59cee6c806e2..ff10a773a97c 100644 --- a/clang/lib/Basic/LangOptions.cpp +++ b/clang/lib/Basic/LangOptions.cpp @@ -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 { diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 3f2e303b5c39..8588fa37810e 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -1111,7 +1111,8 @@ static bool parseTestModuleFileExtensionArg(StringRef Arg, } 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(FrontendOptions &Opts, ArgList &Args, if (DashX == IK_None) Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << A->getValue(); + IsHeaderFile = llvm::StringSwitch(A->getValue()) + .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(CompilerInvocation &Res, 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()); diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp index ff20492e9aa0..7fc008274bdb 100644 --- a/clang/lib/Lex/PPDirectives.cpp +++ b/clang/lib/Lex/PPDirectives.cpp @@ -2058,7 +2058,11 @@ void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc, // 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) { diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp index fde053b7c74b..a9e7dcbe5483 100644 --- a/clang/lib/Lex/PPMacroExpansion.cpp +++ b/clang/lib/Lex/PPMacroExpansion.cpp @@ -1421,7 +1421,11 @@ static bool EvaluateHasIncludeNext(Token &Tok, // 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()) { diff --git a/clang/lib/Lex/Pragma.cpp b/clang/lib/Lex/Pragma.cpp index 2c04a2d57c01..100da514144a 100644 --- a/clang/lib/Lex/Pragma.cpp +++ b/clang/lib/Lex/Pragma.cpp @@ -372,8 +372,10 @@ void Preprocessor::HandleMicrosoft__pragma(Token &Tok) { /// 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; } diff --git a/clang/test/Preprocessor/header_is_main_file.c b/clang/test/Preprocessor/header_is_main_file.c new file mode 100644 index 000000000000..03ade13ff333 --- /dev/null +++ b/clang/test/Preprocessor/header_is_main_file.c @@ -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