llvm-project
154 строки · 5.0 Кб
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 {
29const Preprocessor *PP;
30std::string OutputFile;
31std::string SysRoot;
32llvm::SetVector<FileEntryRef> AllFiles;
33using DependencyMap =
34llvm::DenseMap<FileEntryRef, SmallVector<FileEntryRef, 2>>;
35
36DependencyMap Dependencies;
37
38private:
39raw_ostream &writeNodeReference(raw_ostream &OS,
40const FileEntry *Node);
41void OutputGraphFile();
42
43public:
44DependencyGraphCallback(const Preprocessor *_PP, StringRef OutputFile,
45StringRef SysRoot)
46: PP(_PP), OutputFile(OutputFile.str()), SysRoot(SysRoot.str()) {}
47
48void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
49StringRef FileName, bool IsAngled,
50CharSourceRange FilenameRange,
51OptionalFileEntryRef File, StringRef SearchPath,
52StringRef RelativePath, const Module *SuggestedModule,
53bool ModuleImported,
54SrcMgr::CharacteristicKind FileType) override;
55
56void EmbedDirective(SourceLocation HashLoc, StringRef FileName, bool IsAngled,
57OptionalFileEntryRef File,
58const LexEmbedParametersResult &Params) override;
59
60void EndOfMainFile() override {
61OutputGraphFile();
62}
63
64};
65}
66
67void clang::AttachDependencyGraphGen(Preprocessor &PP, StringRef OutputFile,
68StringRef SysRoot) {
69PP.addPPCallbacks(std::make_unique<DependencyGraphCallback>(&PP, OutputFile,
70SysRoot));
71}
72
73void DependencyGraphCallback::InclusionDirective(
74SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
75bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File,
76StringRef SearchPath, StringRef RelativePath, const Module *SuggestedModule,
77bool ModuleImported, SrcMgr::CharacteristicKind FileType) {
78if (!File)
79return;
80
81SourceManager &SM = PP->getSourceManager();
82OptionalFileEntryRef FromFile =
83SM.getFileEntryRefForID(SM.getFileID(SM.getExpansionLoc(HashLoc)));
84if (!FromFile)
85return;
86
87Dependencies[*FromFile].push_back(*File);
88
89AllFiles.insert(*File);
90AllFiles.insert(*FromFile);
91}
92
93void DependencyGraphCallback::EmbedDirective(SourceLocation HashLoc, StringRef,
94bool, OptionalFileEntryRef File,
95const LexEmbedParametersResult &) {
96if (!File)
97return;
98
99SourceManager &SM = PP->getSourceManager();
100OptionalFileEntryRef FromFile =
101SM.getFileEntryRefForID(SM.getFileID(SM.getExpansionLoc(HashLoc)));
102if (!FromFile)
103return;
104
105Dependencies[*FromFile].push_back(*File);
106
107AllFiles.insert(*File);
108AllFiles.insert(*FromFile);
109}
110
111raw_ostream &
112DependencyGraphCallback::writeNodeReference(raw_ostream &OS,
113const FileEntry *Node) {
114OS << "header_" << Node->getUID();
115return OS;
116}
117
118void DependencyGraphCallback::OutputGraphFile() {
119std::error_code EC;
120llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::OF_TextWithCRLF);
121if (EC) {
122PP->getDiagnostics().Report(diag::err_fe_error_opening) << OutputFile
123<< EC.message();
124return;
125}
126
127OS << "digraph \"dependencies\" {\n";
128
129// Write the nodes
130for (unsigned I = 0, N = AllFiles.size(); I != N; ++I) {
131// Write the node itself.
132OS.indent(2);
133writeNodeReference(OS, AllFiles[I]);
134OS << " [ shape=\"box\", label=\"";
135StringRef FileName = AllFiles[I].getName();
136FileName.consume_front(SysRoot);
137
138OS << DOT::EscapeString(std::string(FileName)) << "\"];\n";
139}
140
141// Write the edges
142for (DependencyMap::iterator F = Dependencies.begin(),
143FEnd = Dependencies.end();
144F != FEnd; ++F) {
145for (unsigned I = 0, N = F->second.size(); I != N; ++I) {
146OS.indent(2);
147writeNodeReference(OS, F->first);
148OS << " -> ";
149writeNodeReference(OS, F->second[I]);
150OS << ";\n";
151}
152}
153OS << "}\n";
154}
155
156