diff options
Diffstat (limited to 'clang/lib/Frontend')
-rw-r--r-- | clang/lib/Frontend/CompilerInstance.cpp | 6 | ||||
-rw-r--r-- | clang/lib/Frontend/CompilerInvocation.cpp | 5 | ||||
-rw-r--r-- | clang/lib/Frontend/HeaderIncludeGen.cpp | 37 |
3 files changed, 41 insertions, 7 deletions
diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp index dea698d5931..92aed39b5c3 100644 --- a/clang/lib/Frontend/CompilerInstance.cpp +++ b/clang/lib/Frontend/CompilerInstance.cpp @@ -210,6 +210,12 @@ CompilerInstance::createPreprocessor(Diagnostic &Diags, // Handle generating header include information, if requested. if (DepOpts.ShowHeaderIncludes) AttachHeaderIncludeGen(*PP); + if (!DepOpts.HeaderIncludeOutputFile.empty()) { + llvm::StringRef OutputPath = DepOpts.HeaderIncludeOutputFile; + if (OutputPath == "-") + OutputPath = ""; + AttachHeaderIncludeGen(*PP, /*ShowAllHeaders=*/true, OutputPath); + } return PP; } diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 1869049cbc8..c2fe7d3f4df 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -222,6 +222,10 @@ static void DependencyOutputOptsToArgs(const DependencyOutputOptions &Opts, Res.push_back("-sys-header-deps"); if (Opts.ShowHeaderIncludes) Res.push_back("-H"); + if (!Opts.HeaderIncludeOutputFile.empty()) { + Res.push_back("-header-include-file"); + Res.push_back(Opts.HeaderIncludeOutputFile); + } if (Opts.UsePhonyTargets) Res.push_back("-MP"); if (!Opts.OutputFile.empty()) { @@ -961,6 +965,7 @@ static void ParseDependencyOutputArgs(DependencyOutputOptions &Opts, Opts.IncludeSystemHeaders = Args.hasArg(OPT_sys_header_deps); Opts.UsePhonyTargets = Args.hasArg(OPT_MP); Opts.ShowHeaderIncludes = Args.hasArg(OPT_H); + Opts.HeaderIncludeOutputFile = Args.getLastArgValue(OPT_header_include_file); } static void ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args, diff --git a/clang/lib/Frontend/HeaderIncludeGen.cpp b/clang/lib/Frontend/HeaderIncludeGen.cpp index 480a97dd101..0d478d7065b 100644 --- a/clang/lib/Frontend/HeaderIncludeGen.cpp +++ b/clang/lib/Frontend/HeaderIncludeGen.cpp @@ -10,19 +10,29 @@ #include "clang/Frontend/Utils.h" #include "clang/Basic/SourceManager.h" #include "clang/Lex/Preprocessor.h" +#include <cstdio> using namespace clang; namespace { class HeaderIncludesCallback : public PPCallbacks { SourceManager &SM; + FILE *OutputFile; unsigned CurrentIncludeDepth; - bool ShowAllHeaders; bool HasProcessedPredefines; + bool OwnsOutputFile; + bool ShowAllHeaders; public: - HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_) - : SM(PP->getSourceManager()), CurrentIncludeDepth(0), - ShowAllHeaders(ShowAllHeaders_), HasProcessedPredefines(false) {} + HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_, + FILE *OutputFile_, bool OwnsOutputFile_) + : SM(PP->getSourceManager()), OutputFile(OutputFile_), + CurrentIncludeDepth(0), HasProcessedPredefines(false), + OwnsOutputFile(OwnsOutputFile_), ShowAllHeaders(ShowAllHeaders_) {} + + ~HeaderIncludesCallback() { + if (OwnsOutputFile) + fclose(OutputFile); + } virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, SrcMgr::CharacteristicKind FileType); @@ -31,7 +41,20 @@ public: void clang::AttachHeaderIncludeGen(Preprocessor &PP, bool ShowAllHeaders, llvm::StringRef OutputPath) { - PP.addPPCallbacks(new HeaderIncludesCallback(&PP, ShowAllHeaders)); + FILE *OutputFile; + bool OwnsOutputFile; + + // Open the output file, if used. + if (OutputPath.empty()) { + OutputFile = stderr; + OwnsOutputFile = false; + } else { + OutputFile = fopen(OutputPath.str().c_str(), "a"); + OwnsOutputFile = true; + } + + PP.addPPCallbacks(new HeaderIncludesCallback(&PP, ShowAllHeaders, + OutputFile, OwnsOutputFile)); } void HeaderIncludesCallback::FileChanged(SourceLocation Loc, @@ -42,7 +65,7 @@ void HeaderIncludesCallback::FileChanged(SourceLocation Loc, PresumedLoc UserLoc = SM.getPresumedLoc(Loc); if (UserLoc.isInvalid()) return; - + // Adjust the current include depth. if (Reason == PPCallbacks::EnterFile) { ++CurrentIncludeDepth; @@ -76,7 +99,7 @@ void HeaderIncludesCallback::FileChanged(SourceLocation Loc, Msg += Filename; Msg += '\n'; - llvm::errs() << Msg; + fwrite(Msg.data(), Msg.size(), 1, OutputFile); } } |