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 *Imported,
55 void EndOfMainFile()
override {
64 PP.
addPPCallbacks(std::make_unique<DependencyGraphCallback>(&PP, OutputFile,
68void DependencyGraphCallback::InclusionDirective(
71 StringRef SearchPath, StringRef RelativePath,
const Module *Imported,
78 SM.getFileEntryRefForID(
SM.getFileID(
SM.getExpansionLoc(HashLoc)));
82 Dependencies[*FromFile].push_back(*
File);
84 AllFiles.insert(*
File);
85 AllFiles.insert(*FromFile);
89DependencyGraphCallback::writeNodeReference(raw_ostream &OS,
91 OS <<
"header_" <<
Node->getUID();
95void DependencyGraphCallback::OutputGraphFile() {
97 llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::OF_TextWithCRLF);
99 PP->getDiagnostics().Report(diag::err_fe_error_opening) << OutputFile
104 OS <<
"digraph \"dependencies\" {\n";
107 for (
unsigned I = 0, N = AllFiles.size(); I != N; ++I) {
110 writeNodeReference(OS, AllFiles[I]);
111 OS <<
" [ shape=\"box\", label=\"";
112 StringRef
FileName = AllFiles[I].getName();
116 OS << DOT::EscapeString(std::string(
FileName)) <<
"\"];\n";
120 for (DependencyMap::iterator F = Dependencies.begin(),
121 FEnd = Dependencies.end();
123 for (
unsigned I = 0, N = F->second.size(); I != N; ++I) {
125 writeNodeReference(OS, F->first);
127 writeNodeReference(OS, F->second[I]);
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).
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...
void AttachDependencyGraphGen(Preprocessor &PP, StringRef OutputFile, StringRef SysRoot)
AttachDependencyGraphGen - Create a dependency graph generator, and attach it to the given preprocess...