[cfe-commits] r124750 - in /cfe/trunk: include/clang/Driver/CC1Options.td include/clang/Frontend/DependencyOutputOptions.h lib/Frontend/CompilerInstance.cpp lib/Frontend/CompilerInvocation.cpp lib/Frontend/HeaderIncludeGen.cpp

Daniel Dunbar daniel at zuster.org
Wed Feb 2 13:11:31 PST 2011


Author: ddunbar
Date: Wed Feb  2 15:11:31 2011
New Revision: 124750

URL: http://llvm.org/viewvc/llvm-project?rev=124750&view=rev
Log:
Frontend: Add -header-include-file option, for allowing saving header include
information to a file.

Modified:
    cfe/trunk/include/clang/Driver/CC1Options.td
    cfe/trunk/include/clang/Frontend/DependencyOutputOptions.h
    cfe/trunk/lib/Frontend/CompilerInstance.cpp
    cfe/trunk/lib/Frontend/CompilerInvocation.cpp
    cfe/trunk/lib/Frontend/HeaderIncludeGen.cpp

Modified: cfe/trunk/include/clang/Driver/CC1Options.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Driver/CC1Options.td?rev=124750&r1=124749&r2=124750&view=diff
==============================================================================
--- cfe/trunk/include/clang/Driver/CC1Options.td (original)
+++ cfe/trunk/include/clang/Driver/CC1Options.td Wed Feb  2 15:11:31 2011
@@ -196,6 +196,10 @@
   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">,
@@ -600,8 +604,6 @@
   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

Modified: cfe/trunk/include/clang/Frontend/DependencyOutputOptions.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/DependencyOutputOptions.h?rev=124750&r1=124749&r2=124750&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/DependencyOutputOptions.h (original)
+++ cfe/trunk/include/clang/Frontend/DependencyOutputOptions.h Wed Feb  2 15:11:31 2011
@@ -20,7 +20,7 @@
 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.
@@ -28,6 +28,12 @@
   /// 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;

Modified: cfe/trunk/lib/Frontend/CompilerInstance.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInstance.cpp?rev=124750&r1=124749&r2=124750&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/CompilerInstance.cpp (original)
+++ cfe/trunk/lib/Frontend/CompilerInstance.cpp Wed Feb  2 15:11:31 2011
@@ -210,6 +210,12 @@
   // 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;
 }

Modified: cfe/trunk/lib/Frontend/CompilerInvocation.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInvocation.cpp?rev=124750&r1=124749&r2=124750&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/CompilerInvocation.cpp (original)
+++ cfe/trunk/lib/Frontend/CompilerInvocation.cpp Wed Feb  2 15:11:31 2011
@@ -222,6 +222,10 @@
     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 @@
   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,

Modified: cfe/trunk/lib/Frontend/HeaderIncludeGen.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/HeaderIncludeGen.cpp?rev=124750&r1=124749&r2=124750&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/HeaderIncludeGen.cpp (original)
+++ cfe/trunk/lib/Frontend/HeaderIncludeGen.cpp Wed Feb  2 15:11:31 2011
@@ -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 @@
 
 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 @@
   PresumedLoc UserLoc = SM.getPresumedLoc(Loc);
   if (UserLoc.isInvalid())
     return;
-  
+
   // Adjust the current include depth.
   if (Reason == PPCallbacks::EnterFile) {
     ++CurrentIncludeDepth;
@@ -76,7 +99,7 @@
     Msg += Filename;
     Msg += '\n';
 
-    llvm::errs() << Msg;
+    fwrite(Msg.data(), Msg.size(), 1, OutputFile);
   }
 }
 





More information about the cfe-commits mailing list