Path: blob/main/contrib/llvm-project/clang/lib/Frontend/DependencyGraph.cpp
35232 views
//===--- DependencyGraph.cpp - Generate dependency file -------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This code generates a header dependency graph in DOT format, for use9// with, e.g., GraphViz.10//11//===----------------------------------------------------------------------===//1213#include "clang/Frontend/Utils.h"14#include "clang/Basic/FileManager.h"15#include "clang/Basic/SourceManager.h"16#include "clang/Frontend/FrontendDiagnostic.h"17#include "clang/Lex/PPCallbacks.h"18#include "clang/Lex/Preprocessor.h"19#include "llvm/ADT/SetVector.h"20#include "llvm/Support/GraphWriter.h"21#include "llvm/Support/raw_ostream.h"2223using namespace clang;24namespace DOT = llvm::DOT;2526namespace {27class DependencyGraphCallback : public PPCallbacks {28const Preprocessor *PP;29std::string OutputFile;30std::string SysRoot;31llvm::SetVector<FileEntryRef> AllFiles;32using DependencyMap =33llvm::DenseMap<FileEntryRef, SmallVector<FileEntryRef, 2>>;3435DependencyMap Dependencies;3637private:38raw_ostream &writeNodeReference(raw_ostream &OS,39const FileEntry *Node);40void OutputGraphFile();4142public:43DependencyGraphCallback(const Preprocessor *_PP, StringRef OutputFile,44StringRef SysRoot)45: PP(_PP), OutputFile(OutputFile.str()), SysRoot(SysRoot.str()) {}4647void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,48StringRef FileName, bool IsAngled,49CharSourceRange FilenameRange,50OptionalFileEntryRef File, StringRef SearchPath,51StringRef RelativePath, const Module *SuggestedModule,52bool ModuleImported,53SrcMgr::CharacteristicKind FileType) override;5455void EmbedDirective(SourceLocation HashLoc, StringRef FileName, bool IsAngled,56OptionalFileEntryRef File,57const LexEmbedParametersResult &Params) override;5859void EndOfMainFile() override {60OutputGraphFile();61}6263};64}6566void clang::AttachDependencyGraphGen(Preprocessor &PP, StringRef OutputFile,67StringRef SysRoot) {68PP.addPPCallbacks(std::make_unique<DependencyGraphCallback>(&PP, OutputFile,69SysRoot));70}7172void DependencyGraphCallback::InclusionDirective(73SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,74bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File,75StringRef SearchPath, StringRef RelativePath, const Module *SuggestedModule,76bool ModuleImported, SrcMgr::CharacteristicKind FileType) {77if (!File)78return;7980SourceManager &SM = PP->getSourceManager();81OptionalFileEntryRef FromFile =82SM.getFileEntryRefForID(SM.getFileID(SM.getExpansionLoc(HashLoc)));83if (!FromFile)84return;8586Dependencies[*FromFile].push_back(*File);8788AllFiles.insert(*File);89AllFiles.insert(*FromFile);90}9192void DependencyGraphCallback::EmbedDirective(SourceLocation HashLoc, StringRef,93bool, OptionalFileEntryRef File,94const LexEmbedParametersResult &) {95if (!File)96return;9798SourceManager &SM = PP->getSourceManager();99OptionalFileEntryRef FromFile =100SM.getFileEntryRefForID(SM.getFileID(SM.getExpansionLoc(HashLoc)));101if (!FromFile)102return;103104Dependencies[*FromFile].push_back(*File);105106AllFiles.insert(*File);107AllFiles.insert(*FromFile);108}109110raw_ostream &111DependencyGraphCallback::writeNodeReference(raw_ostream &OS,112const FileEntry *Node) {113OS << "header_" << Node->getUID();114return OS;115}116117void DependencyGraphCallback::OutputGraphFile() {118std::error_code EC;119llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::OF_TextWithCRLF);120if (EC) {121PP->getDiagnostics().Report(diag::err_fe_error_opening) << OutputFile122<< EC.message();123return;124}125126OS << "digraph \"dependencies\" {\n";127128// Write the nodes129for (unsigned I = 0, N = AllFiles.size(); I != N; ++I) {130// Write the node itself.131OS.indent(2);132writeNodeReference(OS, AllFiles[I]);133OS << " [ shape=\"box\", label=\"";134StringRef FileName = AllFiles[I].getName();135FileName.consume_front(SysRoot);136137OS << DOT::EscapeString(std::string(FileName)) << "\"];\n";138}139140// Write the edges141for (DependencyMap::iterator F = Dependencies.begin(),142FEnd = Dependencies.end();143F != FEnd; ++F) {144for (unsigned I = 0, N = F->second.size(); I != N; ++I) {145OS.indent(2);146writeNodeReference(OS, F->first);147OS << " -> ";148writeNodeReference(OS, F->second[I]);149OS << ";\n";150}151}152OS << "}\n";153}154155156157