183d46be3SDouglas Gregor //===--- DependencyGraph.cpp - Generate dependency file -------------------===//
283d46be3SDouglas Gregor //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
683d46be3SDouglas Gregor //
783d46be3SDouglas Gregor //===----------------------------------------------------------------------===//
883d46be3SDouglas Gregor //
92e129659SDouglas Gregor // This code generates a header dependency graph in DOT format, for use
102e129659SDouglas Gregor // with, e.g., GraphViz.
1183d46be3SDouglas Gregor //
1283d46be3SDouglas Gregor //===----------------------------------------------------------------------===//
1383d46be3SDouglas Gregor 
1483d46be3SDouglas Gregor #include "clang/Frontend/Utils.h"
1583d46be3SDouglas Gregor #include "clang/Basic/FileManager.h"
1683d46be3SDouglas Gregor #include "clang/Basic/SourceManager.h"
1783d46be3SDouglas Gregor #include "clang/Frontend/FrontendDiagnostic.h"
1883d46be3SDouglas Gregor #include "clang/Lex/PPCallbacks.h"
1983d46be3SDouglas Gregor #include "clang/Lex/Preprocessor.h"
2083d46be3SDouglas Gregor #include "llvm/ADT/SetVector.h"
2183d46be3SDouglas Gregor #include "llvm/Support/GraphWriter.h"
223a02247dSChandler Carruth #include "llvm/Support/raw_ostream.h"
2383d46be3SDouglas Gregor 
2483d46be3SDouglas Gregor using namespace clang;
2583d46be3SDouglas Gregor namespace DOT = llvm::DOT;
2683d46be3SDouglas Gregor 
2783d46be3SDouglas Gregor namespace {
2883d46be3SDouglas Gregor class DependencyGraphCallback : public PPCallbacks {
2983d46be3SDouglas Gregor   const Preprocessor *PP;
3083d46be3SDouglas Gregor   std::string OutputFile;
3183d46be3SDouglas Gregor   std::string SysRoot;
32*d79ad2f1SJan Svoboda   llvm::SetVector<FileEntryRef> AllFiles;
33*d79ad2f1SJan Svoboda   using DependencyMap =
34*d79ad2f1SJan Svoboda       llvm::DenseMap<FileEntryRef, SmallVector<FileEntryRef, 2>>;
3583d46be3SDouglas Gregor 
3683d46be3SDouglas Gregor   DependencyMap Dependencies;
3783d46be3SDouglas Gregor 
3883d46be3SDouglas Gregor private:
39f857950dSDmitri Gribenko   raw_ostream &writeNodeReference(raw_ostream &OS,
4083d46be3SDouglas Gregor                                   const FileEntry *Node);
4183d46be3SDouglas Gregor   void OutputGraphFile();
4283d46be3SDouglas Gregor 
4383d46be3SDouglas Gregor public:
DependencyGraphCallback(const Preprocessor * _PP,StringRef OutputFile,StringRef SysRoot)4483d46be3SDouglas Gregor   DependencyGraphCallback(const Preprocessor *_PP, StringRef OutputFile,
4583d46be3SDouglas Gregor                           StringRef SysRoot)
4683d46be3SDouglas Gregor     : PP(_PP), OutputFile(OutputFile.str()), SysRoot(SysRoot.str()) { }
4783d46be3SDouglas Gregor 
48afa7cb3aSCraig Topper   void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
49afa7cb3aSCraig Topper                           StringRef FileName, bool IsAngled,
50*d79ad2f1SJan Svoboda                           CharSourceRange FilenameRange,
51*d79ad2f1SJan Svoboda                           Optional<FileEntryRef> File, StringRef SearchPath,
52*d79ad2f1SJan Svoboda                           StringRef RelativePath, const Module *Imported,
5396fbe58bSJulie Hockett                           SrcMgr::CharacteristicKind FileType) override;
5483d46be3SDouglas Gregor 
EndOfMainFile()55afa7cb3aSCraig Topper   void EndOfMainFile() override {
5683d46be3SDouglas Gregor     OutputGraphFile();
5783d46be3SDouglas Gregor   }
5883d46be3SDouglas Gregor 
5983d46be3SDouglas Gregor };
60ab9db510SAlexander Kornienko }
6183d46be3SDouglas Gregor 
AttachDependencyGraphGen(Preprocessor & PP,StringRef OutputFile,StringRef SysRoot)6283d46be3SDouglas Gregor void clang::AttachDependencyGraphGen(Preprocessor &PP, StringRef OutputFile,
6383d46be3SDouglas Gregor                                      StringRef SysRoot) {
642b3d49b6SJonas Devlieghere   PP.addPPCallbacks(std::make_unique<DependencyGraphCallback>(&PP, OutputFile,
65b8a70530SCraig Topper                                                                SysRoot));
6683d46be3SDouglas Gregor }
6783d46be3SDouglas Gregor 
InclusionDirective(SourceLocation HashLoc,const Token & IncludeTok,StringRef FileName,bool IsAngled,CharSourceRange FilenameRange,Optional<FileEntryRef> File,StringRef SearchPath,StringRef RelativePath,const Module * Imported,SrcMgr::CharacteristicKind FileType)6896fbe58bSJulie Hockett void DependencyGraphCallback::InclusionDirective(
6996fbe58bSJulie Hockett     SourceLocation HashLoc,
7083d46be3SDouglas Gregor     const Token &IncludeTok,
7183d46be3SDouglas Gregor     StringRef FileName,
7283d46be3SDouglas Gregor     bool IsAngled,
734fcd2885SArgyrios Kyrtzidis     CharSourceRange FilenameRange,
74*d79ad2f1SJan Svoboda     Optional<FileEntryRef> File,
7583d46be3SDouglas Gregor     StringRef SearchPath,
7619d78b74SArgyrios Kyrtzidis     StringRef RelativePath,
7796fbe58bSJulie Hockett     const Module *Imported,
7896fbe58bSJulie Hockett     SrcMgr::CharacteristicKind FileType) {
7983d46be3SDouglas Gregor   if (!File)
8083d46be3SDouglas Gregor     return;
8183d46be3SDouglas Gregor 
8283d46be3SDouglas Gregor   SourceManager &SM = PP->getSourceManager();
83*d79ad2f1SJan Svoboda   Optional<FileEntryRef> FromFile =
84*d79ad2f1SJan Svoboda       SM.getFileEntryRefForID(SM.getFileID(SM.getExpansionLoc(HashLoc)));
8549a2790fSCraig Topper   if (!FromFile)
8683d46be3SDouglas Gregor     return;
8783d46be3SDouglas Gregor 
88*d79ad2f1SJan Svoboda   Dependencies[*FromFile].push_back(*File);
8983d46be3SDouglas Gregor 
90*d79ad2f1SJan Svoboda   AllFiles.insert(*File);
91*d79ad2f1SJan Svoboda   AllFiles.insert(*FromFile);
9283d46be3SDouglas Gregor }
9383d46be3SDouglas Gregor 
94f857950dSDmitri Gribenko raw_ostream &
writeNodeReference(raw_ostream & OS,const FileEntry * Node)95f857950dSDmitri Gribenko DependencyGraphCallback::writeNodeReference(raw_ostream &OS,
9683d46be3SDouglas Gregor                                             const FileEntry *Node) {
9783d46be3SDouglas Gregor   OS << "header_" << Node->getUID();
9883d46be3SDouglas Gregor   return OS;
9983d46be3SDouglas Gregor }
10083d46be3SDouglas Gregor 
OutputGraphFile()10183d46be3SDouglas Gregor void DependencyGraphCallback::OutputGraphFile() {
102dae941a6SRafael Espindola   std::error_code EC;
10382b3e28eSAbhina Sreeskantharajan   llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::OF_TextWithCRLF);
104dae941a6SRafael Espindola   if (EC) {
105dae941a6SRafael Espindola     PP->getDiagnostics().Report(diag::err_fe_error_opening) << OutputFile
106dae941a6SRafael Espindola                                                             << EC.message();
10783d46be3SDouglas Gregor     return;
10883d46be3SDouglas Gregor   }
10983d46be3SDouglas Gregor 
11083d46be3SDouglas Gregor   OS << "digraph \"dependencies\" {\n";
11183d46be3SDouglas Gregor 
11283d46be3SDouglas Gregor   // Write the nodes
11383d46be3SDouglas Gregor   for (unsigned I = 0, N = AllFiles.size(); I != N; ++I) {
11483d46be3SDouglas Gregor     // Write the node itself.
11583d46be3SDouglas Gregor     OS.indent(2);
11683d46be3SDouglas Gregor     writeNodeReference(OS, AllFiles[I]);
11783d46be3SDouglas Gregor     OS << " [ shape=\"box\", label=\"";
118*d79ad2f1SJan Svoboda     StringRef FileName = AllFiles[I].getName();
11983d46be3SDouglas Gregor     if (FileName.startswith(SysRoot))
12083d46be3SDouglas Gregor       FileName = FileName.substr(SysRoot.size());
12183d46be3SDouglas Gregor 
122adcd0268SBenjamin Kramer     OS << DOT::EscapeString(std::string(FileName)) << "\"];\n";
12383d46be3SDouglas Gregor   }
12483d46be3SDouglas Gregor 
12583d46be3SDouglas Gregor   // Write the edges
12683d46be3SDouglas Gregor   for (DependencyMap::iterator F = Dependencies.begin(),
12783d46be3SDouglas Gregor                             FEnd = Dependencies.end();
12883d46be3SDouglas Gregor        F != FEnd; ++F) {
12983d46be3SDouglas Gregor     for (unsigned I = 0, N = F->second.size(); I != N; ++I) {
13083d46be3SDouglas Gregor       OS.indent(2);
13183d46be3SDouglas Gregor       writeNodeReference(OS, F->first);
13283d46be3SDouglas Gregor       OS << " -> ";
13383d46be3SDouglas Gregor       writeNodeReference(OS, F->second[I]);
13483d46be3SDouglas Gregor       OS << ";\n";
13583d46be3SDouglas Gregor     }
13683d46be3SDouglas Gregor   }
13783d46be3SDouglas Gregor   OS << "}\n";
13883d46be3SDouglas Gregor }
13983d46be3SDouglas Gregor 
140