20#include "llvm/ADT/SetVector.h"
21#include "llvm/Support/GraphWriter.h"
22#include "llvm/Support/raw_ostream.h"
25namespace DOT = llvm::DOT;
30 std::string OutputFile;
32 llvm::SetVector<FileEntryRef> AllFiles;
34 llvm::DenseMap<FileEntryRef, SmallVector<FileEntryRef, 2>>;
36 DependencyMap Dependencies;
39 raw_ostream &writeNodeReference(raw_ostream &OS,
41 void OutputGraphFile();
44 DependencyGraphCallback(
const Preprocessor *_PP, StringRef OutputFile,
46 : PP(_PP), OutputFile(OutputFile.str()), SysRoot(SysRoot.str()) {}
52 StringRef RelativePath,
const Module *SuggestedModule,
60 void EndOfMainFile()
override {
69 PP.
addPPCallbacks(std::make_unique<DependencyGraphCallback>(&PP, OutputFile,
73void DependencyGraphCallback::InclusionDirective(
76 StringRef SearchPath, StringRef RelativePath,
const Module *SuggestedModule,
83 SM.getFileEntryRefForID(
SM.getFileID(
SM.getExpansionLoc(HashLoc)));
87 Dependencies[*FromFile].push_back(*
File);
89 AllFiles.insert(*
File);
90 AllFiles.insert(*FromFile);
93void DependencyGraphCallback::EmbedDirective(
SourceLocation HashLoc, StringRef,
101 SM.getFileEntryRefForID(
SM.getFileID(
SM.getExpansionLoc(HashLoc)));
105 Dependencies[*FromFile].push_back(*
File);
107 AllFiles.insert(*
File);
108 AllFiles.insert(*FromFile);
112DependencyGraphCallback::writeNodeReference(raw_ostream &OS,
114 OS <<
"header_" <<
Node->getUID();
118void DependencyGraphCallback::OutputGraphFile() {
120 llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::OF_TextWithCRLF);
122 PP->getDiagnostics().Report(diag::err_fe_error_opening) << OutputFile
127 OS <<
"digraph \"dependencies\" {\n";
130 for (
unsigned I = 0, N = AllFiles.size(); I != N; ++I) {
133 writeNodeReference(OS, AllFiles[I]);
134 OS <<
" [ shape=\"box\", label=\"";
135 StringRef
FileName = AllFiles[I].getName();
138 OS << DOT::EscapeString(std::string(
FileName)) <<
"\"];\n";
142 for (DependencyMap::iterator F = Dependencies.begin(),
143 FEnd = Dependencies.end();
145 for (
unsigned I = 0, N = F->second.size(); I != N; ++I) {
147 writeNodeReference(OS, F->first);
149 writeNodeReference(OS, F->second[I]);
Defines the clang::FileManager interface and associated types.
llvm::MachO::FileType FileType
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).
Record the location of an inclusion directive, such as an #include or #import statement.
Describes a module or submodule.
This interface provides a way to observe the actions of the preprocessor as it does its thing.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
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.
CharacteristicKind
Indicates whether a file or directory holds normal user code, system code, or system code which is im...
The JSON file list parser is used to communicate input to InstallAPI.
void AttachDependencyGraphGen(Preprocessor &PP, StringRef OutputFile, StringRef SysRoot)
AttachDependencyGraphGen - Create a dependency graph generator, and attach it to the given preprocess...