clang  10.0.0svn
DependencyGraph.cpp
Go to the documentation of this file.
1 //===--- DependencyGraph.cpp - Generate dependency file -------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This code generates a header dependency graph in DOT format, for use
10 // with, e.g., GraphViz.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/Frontend/Utils.h"
18 #include "clang/Lex/PPCallbacks.h"
19 #include "clang/Lex/Preprocessor.h"
20 #include "llvm/ADT/SetVector.h"
21 #include "llvm/Support/GraphWriter.h"
22 #include "llvm/Support/raw_ostream.h"
23 
24 using namespace clang;
25 namespace DOT = llvm::DOT;
26 
27 namespace {
28 class DependencyGraphCallback : public PPCallbacks {
29  const Preprocessor *PP;
30  std::string OutputFile;
31  std::string SysRoot;
32  llvm::SetVector<const FileEntry *> AllFiles;
33  typedef llvm::DenseMap<const FileEntry *,
34  SmallVector<const FileEntry *, 2> > DependencyMap;
35 
36  DependencyMap Dependencies;
37 
38 private:
39  raw_ostream &writeNodeReference(raw_ostream &OS,
40  const FileEntry *Node);
41  void OutputGraphFile();
42 
43 public:
44  DependencyGraphCallback(const Preprocessor *_PP, StringRef OutputFile,
45  StringRef SysRoot)
46  : PP(_PP), OutputFile(OutputFile.str()), SysRoot(SysRoot.str()) { }
47 
48  void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
49  StringRef FileName, bool IsAngled,
50  CharSourceRange FilenameRange, const FileEntry *File,
51  StringRef SearchPath, StringRef RelativePath,
52  const Module *Imported,
53  SrcMgr::CharacteristicKind FileType) override;
54 
55  void EndOfMainFile() override {
56  OutputGraphFile();
57  }
58 
59 };
60 }
61 
62 void clang::AttachDependencyGraphGen(Preprocessor &PP, StringRef OutputFile,
63  StringRef SysRoot) {
64  PP.addPPCallbacks(std::make_unique<DependencyGraphCallback>(&PP, OutputFile,
65  SysRoot));
66 }
67 
68 void DependencyGraphCallback::InclusionDirective(
69  SourceLocation HashLoc,
70  const Token &IncludeTok,
71  StringRef FileName,
72  bool IsAngled,
73  CharSourceRange FilenameRange,
74  const FileEntry *File,
75  StringRef SearchPath,
76  StringRef RelativePath,
77  const Module *Imported,
78  SrcMgr::CharacteristicKind FileType) {
79  if (!File)
80  return;
81 
82  SourceManager &SM = PP->getSourceManager();
83  const FileEntry *FromFile
84  = SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(HashLoc)));
85  if (!FromFile)
86  return;
87 
88  Dependencies[FromFile].push_back(File);
89 
90  AllFiles.insert(File);
91  AllFiles.insert(FromFile);
92 }
93 
94 raw_ostream &
95 DependencyGraphCallback::writeNodeReference(raw_ostream &OS,
96  const FileEntry *Node) {
97  OS << "header_" << Node->getUID();
98  return OS;
99 }
100 
101 void DependencyGraphCallback::OutputGraphFile() {
102  std::error_code EC;
103  llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::OF_Text);
104  if (EC) {
105  PP->getDiagnostics().Report(diag::err_fe_error_opening) << OutputFile
106  << EC.message();
107  return;
108  }
109 
110  OS << "digraph \"dependencies\" {\n";
111 
112  // Write the nodes
113  for (unsigned I = 0, N = AllFiles.size(); I != N; ++I) {
114  // Write the node itself.
115  OS.indent(2);
116  writeNodeReference(OS, AllFiles[I]);
117  OS << " [ shape=\"box\", label=\"";
118  StringRef FileName = AllFiles[I]->getName();
119  if (FileName.startswith(SysRoot))
120  FileName = FileName.substr(SysRoot.size());
121 
122  OS << DOT::EscapeString(FileName)
123  << "\"];\n";
124  }
125 
126  // Write the edges
127  for (DependencyMap::iterator F = Dependencies.begin(),
128  FEnd = Dependencies.end();
129  F != FEnd; ++F) {
130  for (unsigned I = 0, N = F->second.size(); I != N; ++I) {
131  OS.indent(2);
132  writeNodeReference(OS, F->first);
133  OS << " -> ";
134  writeNodeReference(OS, F->second[I]);
135  OS << ";\n";
136  }
137  }
138  OS << "}\n";
139 }
140 
Defines the clang::FileManager interface and associated types.
Defines the SourceManager interface.
CharacteristicKind
Indicates whether a file or directory holds normal user code, system code, or system code which is im...
Definition: SourceManager.h:77
This interface provides a way to observe the actions of the preprocessor as it does its thing...
Definition: PPCallbacks.h:35
unsigned getUID() const
Definition: FileManager.h:106
Token - This structure provides full information about a lexed token.
Definition: Token.h:34
Describes a module or submodule.
Definition: Module.h:64
SourceLocation getExpansionLoc(SourceLocation Loc) const
Given a SourceLocation object Loc, return the expansion location referenced by the ID...
Represents a character-granular source range.
const FileEntry * getFileEntryForID(FileID FID) const
Returns the FileEntry record for the provided FileID.
Defines the clang::Preprocessor interface.
Record the location of an inclusion directive, such as an #include or #import statement.
const SourceManager & SM
Definition: Format.cpp:1667
Encodes a location in the source.
Cached information about one file (either on disk or in the virtual file system). ...
Definition: FileManager.h:78
void AttachDependencyGraphGen(Preprocessor &PP, StringRef OutputFile, StringRef SysRoot)
AttachDependencyGraphGen - Create a dependency graph generator, and attach it to the given preprocess...
ast_type_traits::DynTypedNode Node
Dataflow Directional Tag Classes.
FileID getFileID(SourceLocation SpellingLoc) const
Return the FileID for a SourceLocation.
Defines the PPCallbacks interface.
void addPPCallbacks(std::unique_ptr< PPCallbacks > C)
Definition: Preprocessor.h:996
This class handles loading and caching of source files into memory.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:125