clang 18.0.0git
DependencyGraph.cpp
Go to the documentation of this file.
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
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 *Imported,
53 SrcMgr::CharacteristicKind FileType) override;
54
55 void EndOfMainFile() override {
56 OutputGraphFile();
57 }
58
59};
60}
61
62void clang::AttachDependencyGraphGen(Preprocessor &PP, StringRef OutputFile,
63 StringRef SysRoot) {
64 PP.addPPCallbacks(std::make_unique<DependencyGraphCallback>(&PP, OutputFile,
65 SysRoot));
66}
67
68void DependencyGraphCallback::InclusionDirective(
69 SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
70 bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File,
71 StringRef SearchPath, StringRef RelativePath, const Module *Imported,
73 if (!File)
74 return;
75
76 SourceManager &SM = PP->getSourceManager();
77 OptionalFileEntryRef FromFile =
78 SM.getFileEntryRefForID(SM.getFileID(SM.getExpansionLoc(HashLoc)));
79 if (!FromFile)
80 return;
81
82 Dependencies[*FromFile].push_back(*File);
83
84 AllFiles.insert(*File);
85 AllFiles.insert(*FromFile);
86}
87
88raw_ostream &
89DependencyGraphCallback::writeNodeReference(raw_ostream &OS,
90 const FileEntry *Node) {
91 OS << "header_" << Node->getUID();
92 return OS;
93}
94
95void DependencyGraphCallback::OutputGraphFile() {
96 std::error_code EC;
97 llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::OF_TextWithCRLF);
98 if (EC) {
99 PP->getDiagnostics().Report(diag::err_fe_error_opening) << OutputFile
100 << EC.message();
101 return;
102 }
103
104 OS << "digraph \"dependencies\" {\n";
105
106 // Write the nodes
107 for (unsigned I = 0, N = AllFiles.size(); I != N; ++I) {
108 // Write the node itself.
109 OS.indent(2);
110 writeNodeReference(OS, AllFiles[I]);
111 OS << " [ shape=\"box\", label=\"";
112 StringRef FileName = AllFiles[I].getName();
113 if (FileName.startswith(SysRoot))
114 FileName = FileName.substr(SysRoot.size());
115
116 OS << DOT::EscapeString(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(2);
125 writeNodeReference(OS, F->first);
126 OS << " -> ";
127 writeNodeReference(OS, F->second[I]);
128 OS << ";\n";
129 }
130 }
131 OS << "}\n";
132}
133
DynTypedNode Node
#define SM(sm)
Definition: Cuda.cpp:80
Defines the clang::FileManager interface and associated types.
Defines the PPCallbacks interface.
Defines the clang::Preprocessor interface.
Defines the SourceManager interface.
Represents a character-granular source range.
Cached information about one file (either on disk or in the virtual file system).
Definition: FileEntry.h:366
Record the location of an inclusion directive, such as an #include or #import statement.
Describes a module or submodule.
Definition: Module.h:105
This interface provides a way to observe the actions of the preprocessor as it does its thing.
Definition: PPCallbacks.h:35
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:128
void addPPCallbacks(std::unique_ptr< PPCallbacks > C)
Encodes a location in the source.
This class handles loading and caching of source files into memory.
Token - This structure provides full information about a lexed token.
Definition: Token.h:35
CharacteristicKind
Indicates whether a file or directory holds normal user code, system code, or system code which is im...
Definition: SourceManager.h:81
void AttachDependencyGraphGen(Preprocessor &PP, StringRef OutputFile, StringRef SysRoot)
AttachDependencyGraphGen - Create a dependency graph generator, and attach it to the given preprocess...