1e5dd7070Spatrick //===--- DependencyGraph.cpp - Generate dependency file -------------------===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick //
9e5dd7070Spatrick // This code generates a header dependency graph in DOT format, for use
10e5dd7070Spatrick // with, e.g., GraphViz.
11e5dd7070Spatrick //
12e5dd7070Spatrick //===----------------------------------------------------------------------===//
13e5dd7070Spatrick
14e5dd7070Spatrick #include "clang/Frontend/Utils.h"
15e5dd7070Spatrick #include "clang/Basic/FileManager.h"
16e5dd7070Spatrick #include "clang/Basic/SourceManager.h"
17e5dd7070Spatrick #include "clang/Frontend/FrontendDiagnostic.h"
18e5dd7070Spatrick #include "clang/Lex/PPCallbacks.h"
19e5dd7070Spatrick #include "clang/Lex/Preprocessor.h"
20e5dd7070Spatrick #include "llvm/ADT/SetVector.h"
21e5dd7070Spatrick #include "llvm/Support/GraphWriter.h"
22e5dd7070Spatrick #include "llvm/Support/raw_ostream.h"
23e5dd7070Spatrick
24e5dd7070Spatrick using namespace clang;
25e5dd7070Spatrick namespace DOT = llvm::DOT;
26e5dd7070Spatrick
27e5dd7070Spatrick namespace {
28e5dd7070Spatrick class DependencyGraphCallback : public PPCallbacks {
29e5dd7070Spatrick const Preprocessor *PP;
30e5dd7070Spatrick std::string OutputFile;
31e5dd7070Spatrick std::string SysRoot;
32*12c85518Srobert llvm::SetVector<FileEntryRef> AllFiles;
33*12c85518Srobert using DependencyMap =
34*12c85518Srobert llvm::DenseMap<FileEntryRef, SmallVector<FileEntryRef, 2>>;
35e5dd7070Spatrick
36e5dd7070Spatrick DependencyMap Dependencies;
37e5dd7070Spatrick
38e5dd7070Spatrick private:
39e5dd7070Spatrick raw_ostream &writeNodeReference(raw_ostream &OS,
40e5dd7070Spatrick const FileEntry *Node);
41e5dd7070Spatrick void OutputGraphFile();
42e5dd7070Spatrick
43e5dd7070Spatrick public:
DependencyGraphCallback(const Preprocessor * _PP,StringRef OutputFile,StringRef SysRoot)44e5dd7070Spatrick DependencyGraphCallback(const Preprocessor *_PP, StringRef OutputFile,
45e5dd7070Spatrick StringRef SysRoot)
46e5dd7070Spatrick : PP(_PP), OutputFile(OutputFile.str()), SysRoot(SysRoot.str()) { }
47e5dd7070Spatrick
48e5dd7070Spatrick void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
49e5dd7070Spatrick StringRef FileName, bool IsAngled,
50*12c85518Srobert CharSourceRange FilenameRange,
51*12c85518Srobert OptionalFileEntryRef File, StringRef SearchPath,
52*12c85518Srobert StringRef RelativePath, const Module *Imported,
53e5dd7070Spatrick SrcMgr::CharacteristicKind FileType) override;
54e5dd7070Spatrick
EndOfMainFile()55e5dd7070Spatrick void EndOfMainFile() override {
56e5dd7070Spatrick OutputGraphFile();
57e5dd7070Spatrick }
58e5dd7070Spatrick
59e5dd7070Spatrick };
60e5dd7070Spatrick }
61e5dd7070Spatrick
AttachDependencyGraphGen(Preprocessor & PP,StringRef OutputFile,StringRef SysRoot)62e5dd7070Spatrick void clang::AttachDependencyGraphGen(Preprocessor &PP, StringRef OutputFile,
63e5dd7070Spatrick StringRef SysRoot) {
64e5dd7070Spatrick PP.addPPCallbacks(std::make_unique<DependencyGraphCallback>(&PP, OutputFile,
65e5dd7070Spatrick SysRoot));
66e5dd7070Spatrick }
67e5dd7070Spatrick
InclusionDirective(SourceLocation HashLoc,const Token & IncludeTok,StringRef FileName,bool IsAngled,CharSourceRange FilenameRange,OptionalFileEntryRef File,StringRef SearchPath,StringRef RelativePath,const Module * Imported,SrcMgr::CharacteristicKind FileType)68e5dd7070Spatrick void DependencyGraphCallback::InclusionDirective(
69*12c85518Srobert SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
70*12c85518Srobert bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File,
71*12c85518Srobert StringRef SearchPath, StringRef RelativePath, const Module *Imported,
72e5dd7070Spatrick SrcMgr::CharacteristicKind FileType) {
73e5dd7070Spatrick if (!File)
74e5dd7070Spatrick return;
75e5dd7070Spatrick
76e5dd7070Spatrick SourceManager &SM = PP->getSourceManager();
77*12c85518Srobert OptionalFileEntryRef FromFile =
78*12c85518Srobert SM.getFileEntryRefForID(SM.getFileID(SM.getExpansionLoc(HashLoc)));
79e5dd7070Spatrick if (!FromFile)
80e5dd7070Spatrick return;
81e5dd7070Spatrick
82*12c85518Srobert Dependencies[*FromFile].push_back(*File);
83e5dd7070Spatrick
84*12c85518Srobert AllFiles.insert(*File);
85*12c85518Srobert AllFiles.insert(*FromFile);
86e5dd7070Spatrick }
87e5dd7070Spatrick
88e5dd7070Spatrick raw_ostream &
writeNodeReference(raw_ostream & OS,const FileEntry * Node)89e5dd7070Spatrick DependencyGraphCallback::writeNodeReference(raw_ostream &OS,
90e5dd7070Spatrick const FileEntry *Node) {
91e5dd7070Spatrick OS << "header_" << Node->getUID();
92e5dd7070Spatrick return OS;
93e5dd7070Spatrick }
94e5dd7070Spatrick
OutputGraphFile()95e5dd7070Spatrick void DependencyGraphCallback::OutputGraphFile() {
96e5dd7070Spatrick std::error_code EC;
97a9ac8606Spatrick llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::OF_TextWithCRLF);
98e5dd7070Spatrick if (EC) {
99e5dd7070Spatrick PP->getDiagnostics().Report(diag::err_fe_error_opening) << OutputFile
100e5dd7070Spatrick << EC.message();
101e5dd7070Spatrick return;
102e5dd7070Spatrick }
103e5dd7070Spatrick
104e5dd7070Spatrick OS << "digraph \"dependencies\" {\n";
105e5dd7070Spatrick
106e5dd7070Spatrick // Write the nodes
107e5dd7070Spatrick for (unsigned I = 0, N = AllFiles.size(); I != N; ++I) {
108e5dd7070Spatrick // Write the node itself.
109e5dd7070Spatrick OS.indent(2);
110e5dd7070Spatrick writeNodeReference(OS, AllFiles[I]);
111e5dd7070Spatrick OS << " [ shape=\"box\", label=\"";
112*12c85518Srobert StringRef FileName = AllFiles[I].getName();
113e5dd7070Spatrick if (FileName.startswith(SysRoot))
114e5dd7070Spatrick FileName = FileName.substr(SysRoot.size());
115e5dd7070Spatrick
116ec727ea7Spatrick OS << DOT::EscapeString(std::string(FileName)) << "\"];\n";
117e5dd7070Spatrick }
118e5dd7070Spatrick
119e5dd7070Spatrick // Write the edges
120e5dd7070Spatrick for (DependencyMap::iterator F = Dependencies.begin(),
121e5dd7070Spatrick FEnd = Dependencies.end();
122e5dd7070Spatrick F != FEnd; ++F) {
123e5dd7070Spatrick for (unsigned I = 0, N = F->second.size(); I != N; ++I) {
124e5dd7070Spatrick OS.indent(2);
125e5dd7070Spatrick writeNodeReference(OS, F->first);
126e5dd7070Spatrick OS << " -> ";
127e5dd7070Spatrick writeNodeReference(OS, F->second[I]);
128e5dd7070Spatrick OS << ";\n";
129e5dd7070Spatrick }
130e5dd7070Spatrick }
131e5dd7070Spatrick OS << "}\n";
132e5dd7070Spatrick }
133e5dd7070Spatrick
134