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/raw_ostream.h"
23 #include "llvm/Support/GraphWriter.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 llvm::SmallVector<const FileEntry *, 2> >
38 DependencyMap Dependencies;
41 llvm::raw_ostream &writeNodeReference(llvm::raw_ostream &OS,
42 const FileEntry *Node);
43 void OutputGraphFile();
46 DependencyGraphCallback(const Preprocessor *_PP, StringRef OutputFile,
48 : PP(_PP), OutputFile(OutputFile.str()), SysRoot(SysRoot.str()) { }
50 virtual void InclusionDirective(SourceLocation HashLoc,
51 const Token &IncludeTok,
54 CharSourceRange FilenameRange,
55 const FileEntry *File,
57 StringRef RelativePath,
58 const Module *Imported);
60 virtual void EndOfMainFile() {
67 void clang::AttachDependencyGraphGen(Preprocessor &PP, StringRef OutputFile,
69 PP.addPPCallbacks(new DependencyGraphCallback(&PP, OutputFile, SysRoot));
72 void DependencyGraphCallback::InclusionDirective(SourceLocation HashLoc,
73 const Token &IncludeTok,
76 CharSourceRange FilenameRange,
77 const FileEntry *File,
79 StringRef RelativePath,
80 const Module *Imported) {
84 SourceManager &SM = PP->getSourceManager();
85 const FileEntry *FromFile
86 = SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(HashLoc)));
90 Dependencies[FromFile].push_back(File);
92 AllFiles.insert(File);
93 AllFiles.insert(FromFile);
97 DependencyGraphCallback::writeNodeReference(llvm::raw_ostream &OS,
98 const FileEntry *Node) {
99 OS << "header_" << Node->getUID();
103 void DependencyGraphCallback::OutputGraphFile() {
105 llvm::raw_fd_ostream OS(OutputFile.c_str(), Err);
107 PP->getDiagnostics().Report(diag::err_fe_error_opening)
108 << OutputFile << Err;
112 OS << "digraph \"dependencies\" {\n";
115 for (unsigned I = 0, N = AllFiles.size(); I != N; ++I) {
116 // Write the node itself.
118 writeNodeReference(OS, AllFiles[I]);
119 OS << " [ shape=\"box\", label=\"";
120 StringRef FileName = AllFiles[I]->getName();
121 if (FileName.startswith(SysRoot))
122 FileName = FileName.substr(SysRoot.size());
124 OS << DOT::EscapeString(FileName)
129 for (DependencyMap::iterator F = Dependencies.begin(),
130 FEnd = Dependencies.end();
132 for (unsigned I = 0, N = F->second.size(); I != N; ++I) {
134 writeNodeReference(OS, F->first);
136 writeNodeReference(OS, F->second[I]);