[cfe-commits] r111983 - in /cfe/trunk: include/clang/Driver/CC1Options.td include/clang/Frontend/PreprocessorOutputOptions.h lib/Driver/Tools.cpp lib/Frontend/CompilerInvocation.cpp lib/Frontend/PrintPreprocessedOutput.cpp test/Frontend/Inputs/ test/Frontend/Inputs/lit.local.cfg test/Frontend/Inputs/test.h test/Frontend/Inputs/test2.h test/Frontend/Inputs/test3.h test/Frontend/print-header-includes.c

Daniel Dunbar daniel at zuster.org
Tue Aug 24 15:44:13 PDT 2010


Author: ddunbar
Date: Tue Aug 24 17:44:13 2010
New Revision: 111983

URL: http://llvm.org/viewvc/llvm-project?rev=111983&view=rev
Log:
Frontend: Add basic -H support.
 - I didn't implement the GCC "multiple include guard" detection parts, because
   it doesn't seem useful or obvious.

Added:
    cfe/trunk/test/Frontend/Inputs/
    cfe/trunk/test/Frontend/Inputs/lit.local.cfg
    cfe/trunk/test/Frontend/Inputs/test.h
    cfe/trunk/test/Frontend/Inputs/test2.h
    cfe/trunk/test/Frontend/Inputs/test3.h
    cfe/trunk/test/Frontend/print-header-includes.c
Modified:
    cfe/trunk/include/clang/Driver/CC1Options.td
    cfe/trunk/include/clang/Frontend/PreprocessorOutputOptions.h
    cfe/trunk/lib/Driver/Tools.cpp
    cfe/trunk/lib/Frontend/CompilerInvocation.cpp
    cfe/trunk/lib/Frontend/PrintPreprocessedOutput.cpp

Modified: cfe/trunk/include/clang/Driver/CC1Options.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Driver/CC1Options.td?rev=111983&r1=111982&r2=111983&view=diff
==============================================================================
--- cfe/trunk/include/clang/Driver/CC1Options.td (original)
+++ cfe/trunk/include/clang/Driver/CC1Options.td Tue Aug 24 17:44:13 2010
@@ -556,3 +556,5 @@
   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">;

Modified: cfe/trunk/include/clang/Frontend/PreprocessorOutputOptions.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/PreprocessorOutputOptions.h?rev=111983&r1=111982&r2=111983&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/PreprocessorOutputOptions.h (original)
+++ cfe/trunk/include/clang/Frontend/PreprocessorOutputOptions.h Tue Aug 24 17:44:13 2010
@@ -16,19 +16,21 @@
 /// output (e.g., -E).
 class PreprocessorOutputOptions {
 public:
-  unsigned ShowCPP : 1;           ///< Print normal preprocessed output.
-  unsigned ShowMacros : 1;        ///< Print macro definitions.
-  unsigned ShowLineMarkers : 1;   ///< Show #line markers.
-  unsigned ShowComments : 1;      ///< Show comments.
-  unsigned ShowMacroComments : 1; ///< Show comments, even in macros.
+  unsigned ShowCPP : 1;            ///< Print normal preprocessed output.
+  unsigned ShowComments : 1;       ///< Show comments.
+  unsigned ShowHeaderIncludes : 1; ///< Show header inclusions (-H).
+  unsigned ShowLineMarkers : 1;    ///< Show #line markers.
+  unsigned ShowMacroComments : 1;  ///< Show comments, even in macros.
+  unsigned ShowMacros : 1;         ///< Print macro definitions.
 
 public:
   PreprocessorOutputOptions() {
     ShowCPP = 1;
-    ShowMacros = 0;
-    ShowLineMarkers = 1;
     ShowComments = 0;
+    ShowHeaderIncludes = 0;
+    ShowLineMarkers = 1;
     ShowMacroComments = 0;
+    ShowMacros = 0;
   }
 };
 

Modified: cfe/trunk/lib/Driver/Tools.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/Tools.cpp?rev=111983&r1=111982&r2=111983&view=diff
==============================================================================
--- cfe/trunk/lib/Driver/Tools.cpp (original)
+++ cfe/trunk/lib/Driver/Tools.cpp Tue Aug 24 17:44:13 2010
@@ -962,6 +962,7 @@
   }
 
   Args.AddAllArgs(CmdArgs, options::OPT_v);
+  Args.AddLastArg(CmdArgs, options::OPT_H);
   Args.AddLastArg(CmdArgs, options::OPT_P);
   Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
 

Modified: cfe/trunk/lib/Frontend/CompilerInvocation.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInvocation.cpp?rev=111983&r1=111982&r2=111983&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/CompilerInvocation.cpp (original)
+++ cfe/trunk/lib/Frontend/CompilerInvocation.cpp Tue Aug 24 17:44:13 2010
@@ -689,6 +689,8 @@
   else if (!Opts.ShowCPP && Opts.ShowMacros)
     Res.push_back("-dM");
 
+  if (Opts.ShowHeaderIncludes)
+    Res.push_back("-H");
   if (!Opts.ShowLineMarkers)
     Res.push_back("-P");
   if (Opts.ShowComments)
@@ -1456,10 +1458,11 @@
                                         ArgList &Args) {
   using namespace cc1options;
   Opts.ShowCPP = !Args.hasArg(OPT_dM);
-  Opts.ShowMacros = Args.hasArg(OPT_dM) || Args.hasArg(OPT_dD);
-  Opts.ShowLineMarkers = !Args.hasArg(OPT_P);
   Opts.ShowComments = Args.hasArg(OPT_C);
+  Opts.ShowHeaderIncludes = Args.hasArg(OPT_H);
+  Opts.ShowLineMarkers = !Args.hasArg(OPT_P);
   Opts.ShowMacroComments = Args.hasArg(OPT_CC);
+  Opts.ShowMacros = Args.hasArg(OPT_dM) || Args.hasArg(OPT_dD);
 }
 
 static void ParseTargetArgs(TargetOptions &Opts, ArgList &Args) {

Modified: cfe/trunk/lib/Frontend/PrintPreprocessedOutput.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PrintPreprocessedOutput.cpp?rev=111983&r1=111982&r2=111983&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/PrintPreprocessedOutput.cpp (original)
+++ cfe/trunk/lib/Frontend/PrintPreprocessedOutput.cpp Tue Aug 24 17:44:13 2010
@@ -85,6 +85,10 @@
   llvm::raw_ostream &OS;
 private:
   unsigned CurLine;
+
+  /// The current include nesting level, used by header include dumping (-H).
+  unsigned CurrentIncludeDepth;
+
   bool EmittedTokensOnThisLine;
   bool EmittedMacroOnThisLine;
   SrcMgr::CharacteristicKind FileType;
@@ -92,19 +96,22 @@
   bool Initialized;
   bool DisableLineMarkers;
   bool DumpDefines;
+  bool DumpHeaderIncludes;
   bool UseLineDirective;
+  bool HasProcessedPredefines;
 public:
   PrintPPOutputPPCallbacks(Preprocessor &pp, llvm::raw_ostream &os,
-                           bool lineMarkers, bool defines)
+                           bool lineMarkers, bool defines, bool headers)
      : PP(pp), SM(PP.getSourceManager()),
        ConcatInfo(PP), OS(os), DisableLineMarkers(lineMarkers),
-       DumpDefines(defines) {
-    CurLine = 0;
+       DumpDefines(defines), DumpHeaderIncludes(headers) {
+    CurLine = CurrentIncludeDepth = 0;
     CurFilename += "<uninit>";
     EmittedTokensOnThisLine = false;
     EmittedMacroOnThisLine = false;
     FileType = SrcMgr::C_User;
     Initialized = false;
+    HasProcessedPredefines = false;
 
     // If we're in microsoft mode, use normal #line instead of line markers.
     UseLineDirective = PP.getLangOptions().Microsoft;
@@ -219,7 +226,7 @@
   
   PresumedLoc UserLoc = SourceMgr.getPresumedLoc(Loc);
   unsigned NewLine = UserLoc.getLine();
-  
+
   if (Reason == PPCallbacks::EnterFile) {
     SourceLocation IncludeLoc = SourceMgr.getPresumedLoc(Loc).getIncludeLoc();
     if (IncludeLoc.isValid())
@@ -231,16 +238,41 @@
     // directive and emits a bunch of spaces that aren't needed.  Emulate this
     // strange behavior.
   }
+
+  // Adjust the current include depth.
+  if (Reason == PPCallbacks::EnterFile) {
+    ++CurrentIncludeDepth;
+  } else {
+    if (CurrentIncludeDepth)
+      --CurrentIncludeDepth;
+
+    // We track when we are done with the predefines by watching for the first
+    // place where we drop back to a nesting depth of 0.
+    if (CurrentIncludeDepth == 0 && !HasProcessedPredefines)
+      HasProcessedPredefines = true;
+  }
   
   CurLine = NewLine;
 
-  if (DisableLineMarkers) return;
-
   CurFilename.clear();
   CurFilename += UserLoc.getFilename();
   Lexer::Stringify(CurFilename);
   FileType = NewFileType;
 
+  // Dump the header include information, if enabled and we are past the
+  // predefines buffer.
+  if (DumpHeaderIncludes && HasProcessedPredefines &&
+      Reason == PPCallbacks::EnterFile) {
+    llvm::SmallString<256> Msg;
+    llvm::raw_svector_ostream OS(Msg);
+    for (unsigned i = 0; i != CurrentIncludeDepth; ++i)
+      OS << '.';
+    OS << ' ' << CurFilename << '\n';
+    llvm::errs() << OS.str();
+  }
+
+  if (DisableLineMarkers) return;
+  
   if (!Initialized) {
     WriteLineInfo(CurLine);
     Initialized = true;
@@ -529,7 +561,7 @@
 
   PrintPPOutputPPCallbacks *Callbacks =
       new PrintPPOutputPPCallbacks(PP, *OS, !Opts.ShowLineMarkers,
-                                   Opts.ShowMacros);
+                                   Opts.ShowMacros, Opts.ShowHeaderIncludes);
   PP.AddPragmaHandler(new UnknownPragmaHandler("#pragma", Callbacks));
   PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",
                                                       Callbacks));

Added: cfe/trunk/test/Frontend/Inputs/lit.local.cfg
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Frontend/Inputs/lit.local.cfg?rev=111983&view=auto
==============================================================================
--- cfe/trunk/test/Frontend/Inputs/lit.local.cfg (added)
+++ cfe/trunk/test/Frontend/Inputs/lit.local.cfg Tue Aug 24 17:44:13 2010
@@ -0,0 +1 @@
+config.suffixes = []

Added: cfe/trunk/test/Frontend/Inputs/test.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Frontend/Inputs/test.h?rev=111983&view=auto
==============================================================================
--- cfe/trunk/test/Frontend/Inputs/test.h (added)
+++ cfe/trunk/test/Frontend/Inputs/test.h Tue Aug 24 17:44:13 2010
@@ -0,0 +1 @@
+#include "test2.h"

Added: cfe/trunk/test/Frontend/Inputs/test2.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Frontend/Inputs/test2.h?rev=111983&view=auto
==============================================================================
--- cfe/trunk/test/Frontend/Inputs/test2.h (added)
+++ cfe/trunk/test/Frontend/Inputs/test2.h Tue Aug 24 17:44:13 2010
@@ -0,0 +1 @@
+int x;

Added: cfe/trunk/test/Frontend/Inputs/test3.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Frontend/Inputs/test3.h?rev=111983&view=auto
==============================================================================
--- cfe/trunk/test/Frontend/Inputs/test3.h (added)
+++ cfe/trunk/test/Frontend/Inputs/test3.h Tue Aug 24 17:44:13 2010
@@ -0,0 +1 @@
+int y;

Added: cfe/trunk/test/Frontend/print-header-includes.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Frontend/print-header-includes.c?rev=111983&view=auto
==============================================================================
--- cfe/trunk/test/Frontend/print-header-includes.c (added)
+++ cfe/trunk/test/Frontend/print-header-includes.c Tue Aug 24 17:44:13 2010
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -include Inputs/test3.h -E -H -o %t.out %s 2> %t.err
+// RUN: FileCheck < %t.err %s
+
+// CHECK-NOT: test3.h
+// CHECK: . {{.*test.h}}
+// CHECK: .. {{.*test2.h}}
+
+#include "Inputs/test.h"





More information about the cfe-commits mailing list