[cfe-dev] Patch: reporting the full include graph for a C++ compilation unit

Douglas Gregor dgregor at apple.com
Mon May 9 16:39:39 PDT 2011


On May 9, 2011, at 4:25 PM, Lukács T. Berki wrote:

> Hello happy people,
> 
> I created a small patch to Clang to allow the compiler to report the full inclusion graph (i.e. a list of including file - included file) pairs. Note that this is quite similar to the functionality of -H, with the difference that this one reports also inclusions that are skipped because it would have no effect due to include guards and that the output is written to a file instead of stderr.
> 
> The patch is in the attachment. What does it take to get this submitted to Clang?

Index: include/clang/Driver/CC1Options.td
===================================================================
--- include/clang/Driver/CC1Options.td	(revision 131087)
+++ include/clang/Driver/CC1Options.td	(working copy)
@@ -200,6 +200,8 @@
   HelpText<"Filename (or -) to write header include output to">;
 def H : Flag<"-H">,
   HelpText<"Show header includes and nesting depth">;
+def inclusion_graph_file : Separate<"-inclusion-graph-file">,
+  HelpText<"Print the complete header inclusion graph to this file">;
 def MQ : Separate<"-MQ">, HelpText<"Specify target to quote for dependency">;
 def MT : Separate<"-MT">, HelpText<"Specify target for dependency">;
 def MP : Flag<"-MP">,


This is a -cc1-level option. Did you also mean to have it work through the driver?

+void InclusionGraphCallback::InclusionDirective(SourceLocation HashLoc,
+                                                const Token &IncludeTok,
+                                                llvm::StringRef FileName,
+                                                bool IsAngled,
+                                                const FileEntry *File,
+                                                SourceLocation EndLoc,
+                                                llvm::StringRef SearchPath,
+                                                llvm::StringRef RelativePath) {
+  PresumedLoc UserLoc = SM.getPresumedLoc(HashLoc);
+  if (UserLoc.isInvalid())
+    return;
+
+  *OutputFile << UserLoc.getFilename();
+  *OutputFile << " ";
+  *OutputFile << File->getName();
+  *OutputFile << "\n";
+}
+}

If a path has spaces in it, you won't be able to tell where the first path ends and the second path begins. You'll need to perform some escaping here.

Also, the FileManager doesn't necessarily use full paths. Did you want the relative paths or full paths?

	- Doug



More information about the cfe-dev mailing list