HelpText<"Filename (or -) to write dependency output to">;
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 H : Flag<"-H">,
+ HelpText<"Show header includes and nesting depth">;
def MQ : Separate<"-MQ">, HelpText<"Specify target to quote for dependency">;
def MT : Separate<"-MT">, HelpText<"Specify target for dependency">;
def MP : Flag<"-MP">,
HelpText<"Print macro definitions in -E mode instead of normal output">;
def dD : Flag<"-dD">,
HelpText<"Print macro definitions in -E mode in addition to normal output">;
-def H : Flag<"-H">,
- HelpText<"Show header includes and nesting depth">;
//===----------------------------------------------------------------------===//
// OpenCL Options
class DependencyOutputOptions {
public:
unsigned IncludeSystemHeaders : 1; ///< Include system header dependencies.
- unsigned ShowHeaderIncludes : 1; ///< Show header inclusions (-H).
+ unsigned ShowHeaderIncludes : 1; ///< Show header inclusions (-H).
unsigned UsePhonyTargets : 1; ///< Include phony targets for each
/// dependency, which can avoid some 'make'
/// problems.
/// The file to write dependency output to.
std::string OutputFile;
+ /// The file to write header include output to. This is orthogonal to
+ /// ShowHeaderIncludes (-H) and will include headers mentioned in the
+ /// predefines buffer. If the output file is "-", output will be sent to
+ /// stderr.
+ std::string HeaderIncludeOutputFile;
+
/// A list of names to use as the targets in the dependency file; this list
/// must contain at least one entry.
std::vector<std::string> Targets;
// 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;
}
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()) {
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,
#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);
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,
PresumedLoc UserLoc = SM.getPresumedLoc(Loc);
if (UserLoc.isInvalid())
return;
-
+
// Adjust the current include depth.
if (Reason == PPCallbacks::EnterFile) {
++CurrentIncludeDepth;
Msg += Filename;
Msg += '\n';
- llvm::errs() << Msg;
+ fwrite(Msg.data(), Msg.size(), 1, OutputFile);
}
}