21 #include "llvm/ADT/SetVector.h" 22 #include "llvm/Support/GraphWriter.h" 23 #include "llvm/Support/raw_ostream.h" 25 using namespace clang;
26 namespace DOT = llvm::DOT;
29 class DependencyGraphCallback :
public PPCallbacks {
31 std::string OutputFile;
33 llvm::SetVector<const FileEntry *> AllFiles;
37 DependencyMap Dependencies;
40 raw_ostream &writeNodeReference(raw_ostream &OS,
41 const FileEntry *
Node);
42 void OutputGraphFile();
45 DependencyGraphCallback(
const Preprocessor *_PP, StringRef OutputFile,
47 : PP(_PP), OutputFile(OutputFile.str()), SysRoot(SysRoot.str()) { }
50 StringRef FileName,
bool IsAngled,
52 StringRef SearchPath, StringRef RelativePath,
56 void EndOfMainFile()
override {
65 PP.
addPPCallbacks(llvm::make_unique<DependencyGraphCallback>(&PP, OutputFile,
69 void DependencyGraphCallback::InclusionDirective(
71 const Token &IncludeTok,
77 StringRef RelativePath,
89 Dependencies[FromFile].push_back(File);
91 AllFiles.insert(File);
92 AllFiles.insert(FromFile);
96 DependencyGraphCallback::writeNodeReference(raw_ostream &OS,
98 OS <<
"header_" << Node->
getUID();
102 void DependencyGraphCallback::OutputGraphFile() {
104 llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::F_Text);
106 PP->getDiagnostics().Report(diag::err_fe_error_opening) << OutputFile
111 OS <<
"digraph \"dependencies\" {\n";
114 for (
unsigned I = 0, N = AllFiles.size(); I != N; ++I) {
117 writeNodeReference(OS, AllFiles[I]);
118 OS <<
" [ shape=\"box\", label=\"";
119 StringRef FileName = AllFiles[I]->getName();
120 if (FileName.startswith(SysRoot))
121 FileName = FileName.substr(SysRoot.size());
123 OS << DOT::EscapeString(FileName)
128 for (DependencyMap::iterator F = Dependencies.begin(),
129 FEnd = Dependencies.end();
131 for (
unsigned I = 0, N = F->second.size(); I != N; ++I) {
133 writeNodeReference(OS, F->first);
135 writeNodeReference(OS, F->second[I]);
Defines the clang::FileManager interface and associated types.
Defines the SourceManager interface.
CharacteristicKind
Indicates whether a file or directory holds normal user code, system code, or system code which is im...
This interface provides a way to observe the actions of the preprocessor as it does its thing...
Token - This structure provides full information about a lexed token.
Describes a module or submodule.
SourceLocation getExpansionLoc(SourceLocation Loc) const
Given a SourceLocation object Loc, return the expansion location referenced by the ID...
Represents a character-granular source range.
const FileEntry * getFileEntryForID(FileID FID) const
Returns the FileEntry record for the provided FileID.
Defines the clang::Preprocessor interface.
Record the location of an inclusion directive, such as an #include or #import statement.
Encodes a location in the source.
Cached information about one file (either on disk or in the virtual file system). ...
void AttachDependencyGraphGen(Preprocessor &PP, StringRef OutputFile, StringRef SysRoot)
AttachDependencyGraphGen - Create a dependency graph generator, and attach it to the given preprocess...
ast_type_traits::DynTypedNode Node
Dataflow Directional Tag Classes.
FileID getFileID(SourceLocation SpellingLoc) const
Return the FileID for a SourceLocation.
Defines the PPCallbacks interface.
void addPPCallbacks(std::unique_ptr< PPCallbacks > C)
This class handles loading and caching of source files into memory.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.