From 708002ede3938256a6223d153a0c040f7a40f5cc Mon Sep 17 00:00:00 2001 From: Hans Wennborg Date: Fri, 9 Aug 2013 00:32:23 +0000 Subject: [PATCH] clang-cl: Support /showIncludes This option prints information about #included files to stderr. Clang could already do it, this patch just teaches the existing code about the /showIncludes style and adds the flag. Differential Revision: http://llvm-reviews.chandlerc.com/D1333 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@188037 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Driver/CC1Options.td | 2 ++ include/clang/Driver/CLCompatOptions.td | 4 +++- include/clang/Driver/Options.td | 4 ++-- .../clang/Frontend/DependencyOutputOptions.h | 2 ++ include/clang/Frontend/Utils.h | 4 +++- lib/Driver/Tools.cpp | 3 +++ lib/Frontend/CompilerInstance.cpp | 7 +++++- lib/Frontend/CompilerInvocation.cpp | 1 + lib/Frontend/HeaderIncludeGen.cpp | 22 +++++++++++++------ test/Driver/cl-options.c | 5 ++++- test/Frontend/print-header-includes.c | 7 ++++++ 11 files changed, 48 insertions(+), 13 deletions(-) diff --git a/include/clang/Driver/CC1Options.td b/include/clang/Driver/CC1Options.td index 6ac40f5d20..2774dc69f2 100644 --- a/include/clang/Driver/CC1Options.td +++ b/include/clang/Driver/CC1Options.td @@ -221,6 +221,8 @@ def sys_header_deps : Flag<["-"], "sys-header-deps">, HelpText<"Include system headers in dependency output">; def header_include_file : Separate<["-"], "header-include-file">, HelpText<"Filename (or -) to write header include output to">; +def show_includes : Flag<["--"], "show-includes">, + HelpText<"Print cl.exe style /showIncludes to stderr">; //===----------------------------------------------------------------------===// // Diagnostic Options diff --git a/include/clang/Driver/CLCompatOptions.td b/include/clang/Driver/CLCompatOptions.td index d5251f6df2..0fdd6f343a 100644 --- a/include/clang/Driver/CLCompatOptions.td +++ b/include/clang/Driver/CLCompatOptions.td @@ -68,6 +68,9 @@ def _SLASH_Oy_ : CLFlag<"Oy-">, HelpText<"Disable frame pointer omission">, def _SLASH_P : CLFlag<"P">, HelpText<"Only run the preprocessor">, Alias; def _SLASH_QUESTION : CLFlag<"?">, Alias, HelpText<"Display available options">; +def _SLASH_showIncludes : CLFlag<"showIncludes">, + HelpText<"Print info about included files to stderr">, + Alias; def _SLASH_U : CLJoinedOrSeparate<"U">, HelpText<"Undefine macro">, MetaVarName<"">, Alias; def _SLASH_W0 : CLFlag<"W0">, HelpText<"Disable all warnings">, Alias; @@ -130,7 +133,6 @@ def _SLASH_Gy : CLFlag<"Gy">; def _SLASH_Gy_ : CLFlag<"Gy-">; def _SLASH_GZ : CLFlag<"GZ">; def _SLASH_RTC : CLJoined<"RTC">; -def _SLASH_showIncludes : CLJoined<"showIncludes">; def _SLASH_w : CLJoined<"w">; def _SLASH_Za : CLFlag<"Za">; def _SLASH_Zc : CLJoined<"Zc:">; diff --git a/include/clang/Driver/Options.td b/include/clang/Driver/Options.td index 4598265aeb..ff48369d08 100644 --- a/include/clang/Driver/Options.td +++ b/include/clang/Driver/Options.td @@ -1333,6 +1333,6 @@ def Z_reserved_lib_stdcxx : Flag<["-"], "Z-reserved-lib-stdc++">, def Z_reserved_lib_cckext : Flag<["-"], "Z-reserved-lib-cckext">, Flags<[LinkerInput, NoArgumentUnused, Unsupported]>, Group; -include "CLCompatOptions.td" - include "CC1Options.td" + +include "CLCompatOptions.td" diff --git a/include/clang/Frontend/DependencyOutputOptions.h b/include/clang/Frontend/DependencyOutputOptions.h index 83976c3604..fefb6f3eda 100644 --- a/include/clang/Frontend/DependencyOutputOptions.h +++ b/include/clang/Frontend/DependencyOutputOptions.h @@ -25,6 +25,7 @@ public: /// dependency, which can avoid some 'make' /// problems. unsigned AddMissingHeaderDeps : 1; ///< Add missing headers to dependency list + unsigned PrintShowIncludes : 1; ///< Print cl.exe style /showIncludes info. /// The file to write dependency output to. std::string OutputFile; @@ -48,6 +49,7 @@ public: ShowHeaderIncludes = 0; UsePhonyTargets = 0; AddMissingHeaderDeps = 0; + PrintShowIncludes = 0; } }; diff --git a/include/clang/Frontend/Utils.h b/include/clang/Frontend/Utils.h index d6ab037837..dff56c3a8a 100644 --- a/include/clang/Frontend/Utils.h +++ b/include/clang/Frontend/Utils.h @@ -91,9 +91,11 @@ void AttachDependencyFileGen(Preprocessor &PP, /// the default behavior used by -H. /// \param OutputPath - If non-empty, a path to write the header include /// information to, instead of writing to stderr. +/// \param ShowDepth - Whether to indent to show the nesting of the includes. +/// \param MSStyle - Whether to print in cl.exe /showIncludes style. void AttachHeaderIncludeGen(Preprocessor &PP, bool ShowAllHeaders = false, StringRef OutputPath = "", - bool ShowDepth = true); + bool ShowDepth = true, bool MSStyle = false); /// CacheTokens - Cache tokens for use with PCH. Note that this requires /// a seekable stream. diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp index 7724a1e681..1d41b94b7f 100644 --- a/lib/Driver/Tools.cpp +++ b/lib/Driver/Tools.cpp @@ -3727,6 +3727,9 @@ void Clang::AddClangCLArgs(const ArgList &Args, ArgStringList &CmdArgs) const { // FIXME: Make this default for the win32 triple. CmdArgs.push_back("-cxx-abi"); CmdArgs.push_back("microsoft"); + + if (Arg *A = Args.getLastArg(options::OPT_show_includes)) + A->render(Args, CmdArgs); } void ClangAs::ConstructJob(Compilation &C, const JobAction &JA, diff --git a/lib/Frontend/CompilerInstance.cpp b/lib/Frontend/CompilerInstance.cpp index f06eaf8736..d2e9368506 100644 --- a/lib/Frontend/CompilerInstance.cpp +++ b/lib/Frontend/CompilerInstance.cpp @@ -259,7 +259,7 @@ void CompilerInstance::createPreprocessor() { AttachDependencyGraphGen(*PP, DepOpts.DOTOutputFile, getHeaderSearchOpts().Sysroot); - + // Handle generating header include information, if requested. if (DepOpts.ShowHeaderIncludes) AttachHeaderIncludeGen(*PP); @@ -270,6 +270,11 @@ void CompilerInstance::createPreprocessor() { AttachHeaderIncludeGen(*PP, /*ShowAllHeaders=*/true, OutputPath, /*ShowDepth=*/false); } + + if (DepOpts.PrintShowIncludes) { + AttachHeaderIncludeGen(*PP, /*ShowAllHeaders=*/false, /*OutputPath=*/"", + /*ShowDepth=*/true, /*MSStyle=*/true); + } } // ASTContext diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp index f88c124f08..a4a84127c0 100644 --- a/lib/Frontend/CompilerInvocation.cpp +++ b/lib/Frontend/CompilerInvocation.cpp @@ -518,6 +518,7 @@ static void ParseDependencyOutputArgs(DependencyOutputOptions &Opts, Opts.ShowHeaderIncludes = Args.hasArg(OPT_H); Opts.HeaderIncludeOutputFile = Args.getLastArgValue(OPT_header_include_file); Opts.AddMissingHeaderDeps = Args.hasArg(OPT_MG); + Opts.PrintShowIncludes = Args.hasArg(OPT_show_includes); Opts.DOTOutputFile = Args.getLastArgValue(OPT_dependency_dot); } diff --git a/lib/Frontend/HeaderIncludeGen.cpp b/lib/Frontend/HeaderIncludeGen.cpp index 4d8a05cfbc..237e5b1ac0 100644 --- a/lib/Frontend/HeaderIncludeGen.cpp +++ b/lib/Frontend/HeaderIncludeGen.cpp @@ -24,15 +24,16 @@ class HeaderIncludesCallback : public PPCallbacks { bool OwnsOutputFile; bool ShowAllHeaders; bool ShowDepth; + bool MSStyle; public: HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_, raw_ostream *OutputFile_, bool OwnsOutputFile_, - bool ShowDepth_) + bool ShowDepth_, bool MSStyle_) : SM(PP->getSourceManager()), OutputFile(OutputFile_), CurrentIncludeDepth(0), HasProcessedPredefines(false), OwnsOutputFile(OwnsOutputFile_), ShowAllHeaders(ShowAllHeaders_), - ShowDepth(ShowDepth_) {} + ShowDepth(ShowDepth_), MSStyle(MSStyle_) {} ~HeaderIncludesCallback() { if (OwnsOutputFile) @@ -46,7 +47,8 @@ public: } void clang::AttachHeaderIncludeGen(Preprocessor &PP, bool ShowAllHeaders, - StringRef OutputPath, bool ShowDepth) { + StringRef OutputPath, bool ShowDepth, + bool MSStyle) { raw_ostream *OutputFile = &llvm::errs(); bool OwnsOutputFile = false; @@ -69,7 +71,7 @@ void clang::AttachHeaderIncludeGen(Preprocessor &PP, bool ShowAllHeaders, PP.addPPCallbacks(new HeaderIncludesCallback(&PP, ShowAllHeaders, OutputFile, OwnsOutputFile, - ShowDepth)); + ShowDepth, MSStyle)); } void HeaderIncludesCallback::FileChanged(SourceLocation Loc, @@ -109,14 +111,20 @@ void HeaderIncludesCallback::FileChanged(SourceLocation Loc, if (ShowHeader && Reason == PPCallbacks::EnterFile) { // Write to a temporary string to avoid unnecessary flushing on errs(). SmallString<512> Filename(UserLoc.getFilename()); - Lexer::Stringify(Filename); + if (!MSStyle) + Lexer::Stringify(Filename); SmallString<256> Msg; + if (MSStyle) + Msg += "Note: including file:"; + if (ShowDepth) { // The main source file is at depth 1, so skip one dot. for (unsigned i = 1; i != CurrentIncludeDepth; ++i) - Msg += '.'; - Msg += ' '; + Msg += MSStyle ? ' ' : '.'; + + if (!MSStyle) + Msg += ' '; } Msg += Filename; Msg += '\n'; diff --git a/test/Driver/cl-options.c b/test/Driver/cl-options.c index f9f5c37136..f10141b824 100644 --- a/test/Driver/cl-options.c +++ b/test/Driver/cl-options.c @@ -59,6 +59,9 @@ // RUN: %clang_cl /P -### -- %s 2>&1 | FileCheck -check-prefix=P %s // P: -E +// RUN: %clang_cl /showIncludes -### -- %s 2>&1 | FileCheck -check-prefix=showIncludes %s +// showIncludes: --show-includes + // RUN: %clang_cl /Umymacro -### -- %s 2>&1 | FileCheck -check-prefix=U %s // RUN: %clang_cl /U mymacro -### -- %s 2>&1 | FileCheck -check-prefix=U %s // U: "-U" "mymacro" @@ -97,4 +100,4 @@ // RUN: %clang_cl /Zs /EHsc /Fdfoo /fp:precise /Gd /GL /GL- -- %s 2>&1 // RUN: %clang_cl /Zs /Gm /Gm- /GS /Gy /Gy- /GZ -- %s 2>&1 // RUN: %clang_cl /Zs /RTC1 /wfoo /Zc:wchar_t- -- %s 2>&1 -// RUN: %clang_cl /Zs /ZI /Zi /showIncludes -- %s 2>&1 +// RUN: %clang_cl /Zs /ZI /Zi -- %s 2>&1 diff --git a/test/Frontend/print-header-includes.c b/test/Frontend/print-header-includes.c index 7773d2069d..aa3e3971fd 100644 --- a/test/Frontend/print-header-includes.c +++ b/test/Frontend/print-header-includes.c @@ -5,4 +5,11 @@ // CHECK: . {{.*test.h}} // CHECK: .. {{.*test2.h}} +// RUN: %clang_cc1 -include Inputs/test3.h -E --show-includes -o %t.out %s 2> %t.err +// RUN: FileCheck --check-prefix=MS < %t.err %s +// MS-NOT: test3.h +// MS: Note: including file: {{.*test.h}} +// MS: Note: including file: {{.*test2.h}} +// MS-NOT: Note + #include "Inputs/test.h" -- 2.40.0