1 //===--- DependencyGraph.cpp - Generate dependency file -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This code generates a header dependency graph in DOT format, for use
11 // with, e.g., GraphViz.
13 //===----------------------------------------------------------------------===//
15 #include "clang/Frontend/Utils.h"
16 #include "clang/Basic/FileManager.h"
17 #include "clang/Basic/SourceManager.h"
18 #include "clang/Frontend/FrontendDiagnostic.h"
19 #include "clang/Lex/PPCallbacks.h"
20 #include "clang/Lex/Preprocessor.h"
21 #include "llvm/ADT/SetVector.h"
22 #include "llvm/Support/GraphWriter.h"
23 #include "llvm/Support/raw_ostream.h"
25 using namespace clang;
26 namespace DOT = llvm::DOT;
29 class DependencyGraphCallback : public PPCallbacks {
30 const Preprocessor *PP;
31 std::string OutputFile;
33 llvm::SetVector<const FileEntry *> AllFiles;
34 typedef llvm::DenseMap<const FileEntry *,
35 SmallVector<const FileEntry *, 2> > DependencyMap;
37 DependencyMap Dependencies;
40 raw_ostream &writeNodeReference(raw_ostream &OS,
41 const FileEntry *Node);
42 void OutputGraphFile();
45 DependencyGraphCallback(const Preprocessor *_PP, StringRef OutputFile,
47 : PP(_PP), OutputFile(OutputFile.str()), SysRoot(SysRoot.str()) { }
49 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
50 StringRef FileName, bool IsAngled,
51 CharSourceRange FilenameRange, const FileEntry *File,
52 StringRef SearchPath, StringRef RelativePath,
53 const Module *Imported) override;
55 void EndOfMainFile() override {
62 void clang::AttachDependencyGraphGen(Preprocessor &PP, StringRef OutputFile,
64 PP.addPPCallbacks(llvm::make_unique<DependencyGraphCallback>(&PP, OutputFile,
68 void DependencyGraphCallback::InclusionDirective(SourceLocation HashLoc,
69 const Token &IncludeTok,
72 CharSourceRange FilenameRange,
73 const FileEntry *File,
75 StringRef RelativePath,
76 const Module *Imported) {
80 SourceManager &SM = PP->getSourceManager();
81 const FileEntry *FromFile
82 = SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(HashLoc)));
86 Dependencies[FromFile].push_back(File);
88 AllFiles.insert(File);
89 AllFiles.insert(FromFile);
93 DependencyGraphCallback::writeNodeReference(raw_ostream &OS,
94 const FileEntry *Node) {
95 OS << "header_" << Node->getUID();
99 void DependencyGraphCallback::OutputGraphFile() {
101 llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::F_Text);
103 PP->getDiagnostics().Report(diag::err_fe_error_opening) << OutputFile
108 OS << "digraph \"dependencies\" {\n";
111 for (unsigned I = 0, N = AllFiles.size(); I != N; ++I) {
112 // Write the node itself.
114 writeNodeReference(OS, AllFiles[I]);
115 OS << " [ shape=\"box\", label=\"";
116 StringRef FileName = AllFiles[I]->getName();
117 if (FileName.startswith(SysRoot))
118 FileName = FileName.substr(SysRoot.size());
120 OS << DOT::EscapeString(FileName)
125 for (DependencyMap::iterator F = Dependencies.begin(),
126 FEnd = Dependencies.end();
128 for (unsigned I = 0, N = F->second.size(); I != N; ++I) {
130 writeNodeReference(OS, F->first);
132 writeNodeReference(OS, F->second[I]);