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"
15#include "clang/Basic/FileManager.h"
16#include "clang/Basic/SourceManager.h"
17#include "clang/Frontend/FrontendDiagnostic.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
24using namespace clang;
25namespace DOT = llvm::DOT;
26
27namespace {
28class DependencyGraphCallback : public PPCallbacks {
29 const Preprocessor *PP;
30 std::string OutputFile;
31 std::string SysRoot;
32 llvm::SetVector<FileEntryRef> AllFiles;
33 using DependencyMap =
34 llvm::DenseMap<FileEntryRef, SmallVector<FileEntryRef, 2>>;
35
36 DependencyMap Dependencies;
37
38private:
39 raw_ostream &writeNodeReference(raw_ostream &OS,
40 const FileEntry *Node);
41 void OutputGraphFile();
42
43public:
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,
51 OptionalFileEntryRef File, StringRef SearchPath,
52 StringRef RelativePath, const Module *SuggestedModule,
53 bool ModuleImported,
54 SrcMgr::CharacteristicKind FileType) override;
55
56 void EndOfMainFile() override {
57 OutputGraphFile();
58 }
59
60};
61}
62
63void clang::AttachDependencyGraphGen(Preprocessor &PP, StringRef OutputFile,
64 StringRef SysRoot) {
65 PP.addPPCallbacks(C: std::make_unique<DependencyGraphCallback>(args: &PP, args&: OutputFile,
66 args&: SysRoot));
67}
68
69void DependencyGraphCallback::InclusionDirective(
70 SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
71 bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File,
72 StringRef SearchPath, StringRef RelativePath, const Module *SuggestedModule,
73 bool ModuleImported, SrcMgr::CharacteristicKind FileType) {
74 if (!File)
75 return;
76
77 SourceManager &SM = PP->getSourceManager();
78 OptionalFileEntryRef FromFile =
79 SM.getFileEntryRefForID(FID: SM.getFileID(SpellingLoc: SM.getExpansionLoc(Loc: HashLoc)));
80 if (!FromFile)
81 return;
82
83 Dependencies[*FromFile].push_back(Elt: *File);
84
85 AllFiles.insert(X: *File);
86 AllFiles.insert(X: *FromFile);
87}
88
89raw_ostream &
90DependencyGraphCallback::writeNodeReference(raw_ostream &OS,
91 const FileEntry *Node) {
92 OS << "header_" << Node->getUID();
93 return OS;
94}
95
96void DependencyGraphCallback::OutputGraphFile() {
97 std::error_code EC;
98 llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::OF_TextWithCRLF);
99 if (EC) {
100 PP->getDiagnostics().Report(diag::err_fe_error_opening) << OutputFile
101 << EC.message();
102 return;
103 }
104
105 OS << "digraph \"dependencies\" {\n";
106
107 // Write the nodes
108 for (unsigned I = 0, N = AllFiles.size(); I != N; ++I) {
109 // Write the node itself.
110 OS.indent(NumSpaces: 2);
111 writeNodeReference(OS, Node: AllFiles[I]);
112 OS << " [ shape=\"box\", label=\"";
113 StringRef FileName = AllFiles[I].getName();
114 FileName.consume_front(Prefix: SysRoot);
115
116 OS << DOT::EscapeString(Label: std::string(FileName)) << "\"];\n";
117 }
118
119 // Write the edges
120 for (DependencyMap::iterator F = Dependencies.begin(),
121 FEnd = Dependencies.end();
122 F != FEnd; ++F) {
123 for (unsigned I = 0, N = F->second.size(); I != N; ++I) {
124 OS.indent(NumSpaces: 2);
125 writeNodeReference(OS, Node: F->first);
126 OS << " -> ";
127 writeNodeReference(OS, Node: F->second[I]);
128 OS << ";\n";
129 }
130 }
131 OS << "}\n";
132}
133
134

source code of clang/lib/Frontend/DependencyGraph.cpp