1c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor//===--- DependencyGraph.cpp - Generate dependency file -------------------===// 2c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor// 3c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor// The LLVM Compiler Infrastructure 4c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor// 5c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor// This file is distributed under the University of Illinois Open Source 6c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor// License. See LICENSE.TXT for details. 7c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor// 8c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor//===----------------------------------------------------------------------===// 9c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor// 10773303a505c23d73da624f1e6abcf41a393dad2bDouglas Gregor// This code generates a header dependency graph in DOT format, for use 11773303a505c23d73da624f1e6abcf41a393dad2bDouglas Gregor// with, e.g., GraphViz. 12c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor// 13c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor//===----------------------------------------------------------------------===// 14c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor 15c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor#include "clang/Frontend/Utils.h" 16c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor#include "clang/Basic/FileManager.h" 17c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor#include "clang/Basic/SourceManager.h" 18c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor#include "clang/Frontend/FrontendDiagnostic.h" 19c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor#include "clang/Lex/PPCallbacks.h" 20c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor#include "clang/Lex/Preprocessor.h" 21c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor#include "llvm/ADT/SetVector.h" 22c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor#include "llvm/Support/GraphWriter.h" 2355fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/Support/raw_ostream.h" 24c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor 25c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregorusing namespace clang; 26c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregornamespace DOT = llvm::DOT; 27c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor 28c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregornamespace { 29c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregorclass DependencyGraphCallback : public PPCallbacks { 30c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor const Preprocessor *PP; 31c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor std::string OutputFile; 32c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor std::string SysRoot; 33c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor llvm::SetVector<const FileEntry *> AllFiles; 34cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko typedef llvm::DenseMap<const FileEntry *, 35cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko SmallVector<const FileEntry *, 2> > DependencyMap; 36c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor 37c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor DependencyMap Dependencies; 38c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor 39c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregorprivate: 40cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko raw_ostream &writeNodeReference(raw_ostream &OS, 41cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko const FileEntry *Node); 42c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor void OutputGraphFile(); 43c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor 44c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregorpublic: 45c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor DependencyGraphCallback(const Preprocessor *_PP, StringRef OutputFile, 46c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor StringRef SysRoot) 47c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor : PP(_PP), OutputFile(OutputFile.str()), SysRoot(SysRoot.str()) { } 48c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor 49651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, 50651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines StringRef FileName, bool IsAngled, 51651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines CharSourceRange FilenameRange, const FileEntry *File, 52651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines StringRef SearchPath, StringRef RelativePath, 53651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines const Module *Imported) override; 54c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor 55651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines void EndOfMainFile() override { 56c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor OutputGraphFile(); 57c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor } 58c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor 59c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor}; 60c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor} 61c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor 62c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregorvoid clang::AttachDependencyGraphGen(Preprocessor &PP, StringRef OutputFile, 63c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor StringRef SysRoot) { 64176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines PP.addPPCallbacks(llvm::make_unique<DependencyGraphCallback>(&PP, OutputFile, 65176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines SysRoot)); 66c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor} 67c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor 68c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregorvoid DependencyGraphCallback::InclusionDirective(SourceLocation HashLoc, 69c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor const Token &IncludeTok, 70c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor StringRef FileName, 71c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor bool IsAngled, 72da313592441db36cf4b06be97c4bcc238ee6fa9cArgyrios Kyrtzidis CharSourceRange FilenameRange, 73c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor const FileEntry *File, 74c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor StringRef SearchPath, 75f8afcffe6a0213760b64c211812b1750e1e1e967Argyrios Kyrtzidis StringRef RelativePath, 76f8afcffe6a0213760b64c211812b1750e1e1e967Argyrios Kyrtzidis const Module *Imported) { 77c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor if (!File) 78c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor return; 79c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor 80c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor SourceManager &SM = PP->getSourceManager(); 81c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor const FileEntry *FromFile 82c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor = SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(HashLoc))); 836bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines if (!FromFile) 84c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor return; 85c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor 86c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor Dependencies[FromFile].push_back(File); 87c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor 88c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor AllFiles.insert(File); 89c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor AllFiles.insert(FromFile); 90c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor} 91c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor 92cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenkoraw_ostream & 93cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri GribenkoDependencyGraphCallback::writeNodeReference(raw_ostream &OS, 94c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor const FileEntry *Node) { 95c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor OS << "header_" << Node->getUID(); 96c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor return OS; 97c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor} 98c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor 99c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregorvoid DependencyGraphCallback::OutputGraphFile() { 100176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines std::error_code EC; 101176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::F_Text); 102176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines if (EC) { 103176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines PP->getDiagnostics().Report(diag::err_fe_error_opening) << OutputFile 104176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines << EC.message(); 105c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor return; 106c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor } 107c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor 108c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor OS << "digraph \"dependencies\" {\n"; 109c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor 110c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor // Write the nodes 111c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor for (unsigned I = 0, N = AllFiles.size(); I != N; ++I) { 112c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor // Write the node itself. 113c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor OS.indent(2); 114c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor writeNodeReference(OS, AllFiles[I]); 115c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor OS << " [ shape=\"box\", label=\""; 116c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor StringRef FileName = AllFiles[I]->getName(); 117c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor if (FileName.startswith(SysRoot)) 118c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor FileName = FileName.substr(SysRoot.size()); 119c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor 120c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor OS << DOT::EscapeString(FileName) 121c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor << "\"];\n"; 122c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor } 123c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor 124c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor // Write the edges 125c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor for (DependencyMap::iterator F = Dependencies.begin(), 126c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor FEnd = Dependencies.end(); 127c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor F != FEnd; ++F) { 128c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor for (unsigned I = 0, N = F->second.size(); I != N; ++I) { 129c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor OS.indent(2); 130c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor writeNodeReference(OS, F->first); 131c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor OS << " -> "; 132c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor writeNodeReference(OS, F->second[I]); 133c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor OS << ";\n"; 134c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor } 135c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor } 136c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor OS << "}\n"; 137c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor} 138c69a181049ab52da29f8f69316a34c90c3ea3b8eDouglas Gregor 139