hans created this revision.
hans added reviewers: thakis, rnk.
Herald added subscribers: jansvoboda11, dang.
hans requested review of this revision.
Herald added a project: clang.

Consider the following set of files:

  a.cc:
  #include "a.h"
  
  a.h:
  #ifndef A_H
  #define A_H
  
  #include "b.h"
  #include "c.h"  // This gets "skipped".
  
  #endif
  
  b.h:
  #ifndef B_H
  #define B_H
  
  #include "c.h"
  
  #endif
  
  c.h:
  #ifndef C_H
  #define C_H
  
  void c();
  
  #endif

And the output of the -H option:

  $ clang -c -H a.cc
  . ./a.h
  .. ./b.h
  ... ./c.h

Note that the include of `c.h` in `a.h` is not shown in the output. (GCC does 
the same.) This is because of the include guard optimization: clang knows `c.h` 
is covered by an include guard which is already defined, so when it sees the 
include in `a.h`, it skips it. The same would have happened if `#pragma once` 
were used instead of include guards.

However, `a.h` *does* include `c.h`, and it may be useful to show that in the 
`-H` output. This patch adds a flag for doing that.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D100480

Files:
  clang/include/clang/Driver/Options.td
  clang/include/clang/Frontend/DependencyOutputOptions.h
  clang/lib/Frontend/HeaderIncludeGen.cpp
  clang/test/Frontend/Inputs/test.h
  clang/test/Frontend/Inputs/test2.h
  clang/test/Frontend/print-header-includes.c

Index: clang/test/Frontend/print-header-includes.c
===================================================================
--- clang/test/Frontend/print-header-includes.c
+++ clang/test/Frontend/print-header-includes.c
@@ -6,6 +6,16 @@
 // CHECK-NOT: . {{.*noline.h}}
 // CHECK: . {{.*test.h}}
 // CHECK: .. {{.*test2.h}}
+// CHECK-NOT: .. {{.*test2.h}}
+
+// RUN: %clang_cc1 -I%S -isystem %S/Inputs/SystemHeaderPrefix \
+// RUN:     -E -H -show-skipped-includes -o /dev/null %s 2> %t.stderr
+// RUN: FileCheck --check-prefix=SKIPPED < %t.stderr %s
+
+// SKIPPED-NOT: . {{.*noline.h}}
+// SKIPPED: . {{.*test.h}}
+// SKIPPED: .. {{.*test2.h}}
+// SKIPPED: .. {{.*test2.h}}
 
 // RUN: %clang_cc1 -I%S -include Inputs/test3.h -isystem %S/Inputs/SystemHeaderPrefix \
 // RUN:     -E -H -sys-header-deps -o /dev/null %s 2> %t.stderr
Index: clang/test/Frontend/Inputs/test2.h
===================================================================
--- clang/test/Frontend/Inputs/test2.h
+++ clang/test/Frontend/Inputs/test2.h
@@ -1 +1,6 @@
+#ifndef TEST2_H
+#define TEST2_H
+
 int x;
+
+#endif
Index: clang/test/Frontend/Inputs/test.h
===================================================================
--- clang/test/Frontend/Inputs/test.h
+++ clang/test/Frontend/Inputs/test.h
@@ -1 +1,7 @@
+#ifndef TEST_H
+#define TEST_H
+
 #include "test2.h"
+#include "test2.h"
+
+#endif
Index: clang/lib/Frontend/HeaderIncludeGen.cpp
===================================================================
--- clang/lib/Frontend/HeaderIncludeGen.cpp
+++ clang/lib/Frontend/HeaderIncludeGen.cpp
@@ -45,6 +45,9 @@
   void FileChanged(SourceLocation Loc, FileChangeReason Reason,
                    SrcMgr::CharacteristicKind FileType,
                    FileID PrevFID) override;
+
+  void FileSkipped(const FileEntryRef &SkippedFile, const Token &FilenameTok,
+                   SrcMgr::CharacteristicKind FileType) override;
 };
 }
 
@@ -181,3 +184,16 @@
                     MSStyle);
   }
 }
+
+void HeaderIncludesCallback::FileSkipped(const FileEntryRef &SkippedFile, const
+                                         Token &FilenameTok,
+                                         SrcMgr::CharacteristicKind FileType) {
+  if (!DepOpts.ShowSkippedHeaderIncludes)
+    return;
+
+  if (!DepOpts.IncludeSystemHeaders && isSystem(FileType))
+    return;
+
+  PrintHeaderInfo(OutputFile, SkippedFile.getName(), ShowDepth,
+                  CurrentIncludeDepth + 1, MSStyle);
+}
Index: clang/include/clang/Frontend/DependencyOutputOptions.h
===================================================================
--- clang/include/clang/Frontend/DependencyOutputOptions.h
+++ clang/include/clang/Frontend/DependencyOutputOptions.h
@@ -39,6 +39,10 @@
                                      /// problems.
   unsigned AddMissingHeaderDeps : 1; ///< Add missing headers to dependency list
   unsigned IncludeModuleFiles : 1; ///< Include module file dependencies.
+  unsigned ShowSkippedHeaderIncludes : 1; ///< With ShowHeaderIncludes, show
+                                          /// also includes that were skipped
+                                          /// due to the "include guard
+                                          /// optimization" or #pragma once.
 
   /// Destination of cl.exe style /showIncludes info.
   ShowIncludesDestination ShowIncludesDest = ShowIncludesDestination::None;
@@ -75,7 +79,8 @@
 public:
   DependencyOutputOptions()
       : IncludeSystemHeaders(0), ShowHeaderIncludes(0), UsePhonyTargets(0),
-        AddMissingHeaderDeps(0), IncludeModuleFiles(0) {}
+        AddMissingHeaderDeps(0), IncludeModuleFiles(0),
+        ShowSkippedHeaderIncludes(0) {}
 };
 
 }  // end namespace clang
Index: clang/include/clang/Driver/Options.td
===================================================================
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -4945,6 +4945,11 @@
 def sys_header_deps : Flag<["-"], "sys-header-deps">,
   HelpText<"Include system headers in dependency output">,
   MarshallingInfoFlag<DependencyOutputOpts<"IncludeSystemHeaders">>;
+def show_skipped_includes : Flag<["-"], "show-skipped-includes">,
+  HelpText<"Show skipped includes in -H output.">,
+  DocBrief<[{#include files may be "skipped" due to include guard optimization
+             or #pragma once. This flag makes -H show also such includes.}]>,
+  MarshallingInfoFlag<DependencyOutputOpts<"ShowSkippedHeaderIncludes">>;
 def module_file_deps : Flag<["-"], "module-file-deps">,
   HelpText<"Include module files in dependency output">,
   MarshallingInfoFlag<DependencyOutputOpts<"IncludeModuleFiles">>;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D100480: Add flag fo... Hans Wennborg via Phabricator via cfe-commits

Reply via email to