54#include "clang/AST/ASTConsumer.h"
56#include "clang/AST/ASTContext.h"
57#include "clang/AST/RecursiveASTVisitor.h"
58#include "clang/Basic/SourceManager.h"
59#include "clang/Driver/Options.h"
60#include "clang/Frontend/CompilerInstance.h"
61#include "clang/Frontend/FrontendAction.h"
62#include "clang/Frontend/FrontendActions.h"
63#include "clang/Lex/PPCallbacks.h"
64#include "clang/Lex/Preprocessor.h"
65#include "clang/Tooling/CompilationDatabase.h"
66#include "clang/Tooling/Tooling.h"
67#include "llvm/Option/Option.h"
68#include "llvm/Support/CommandLine.h"
69#include "llvm/Support/FileSystem.h"
70#include "llvm/Support/Path.h"
71#include "llvm/Support/raw_ostream.h"
75using namespace clang::driver;
76using namespace clang::driver::options;
78namespace cl = llvm::cl;
79namespace sys = llvm::sys;
91 CharSourceRange FilenameRange,
92 OptionalFileEntryRef File, StringRef SearchPath,
93 StringRef RelativePath,
const Module *SuggestedModule,
95 SrcMgr::CharacteristicKind FileType)
override {
110 PP.addPPCallbacks(std::make_unique<CoverageCheckerCallbacks>(Checker));
120 StringRef InFile)
override {
121 return std::make_unique<CoverageCheckerConsumer>(Checker,
122 CI.getPreprocessor());
132 : Checker(Checker) {}
134 std::unique_ptr<FrontendAction>
create()
override {
135 return std::make_unique<CoverageCheckerAction>(Checker);
148 clang::ModuleMap *ModuleMap)
157 ArrayRef<std::string>
CommandLine, clang::ModuleMap *ModuleMap) {
159 return std::make_unique<CoverageChecker>(ModuleMapPath, IncludePaths,
173 std::error_code returnValue;
180 return std::error_code(2, std::generic_category());
186 if (!UnaccountedForHeaders.empty())
187 returnValue = std::error_code(1, std::generic_category());
198 for (ModuleMap::module_iterator I = ModMap->module_begin(),
199 E = ModMap->module_end();
211 if (std::optional<Module::Header> UmbrellaHeader =
212 Mod.getUmbrellaHeaderAsWritten()) {
214 ModuleMapHeadersSet.insert(
219 }
else if (std::optional<Module::DirectoryName> UmbrellaDir =
220 Mod.getUmbrellaDirAsWritten()) {
226 for (
auto &HeaderKind : Mod.Headers)
227 for (
auto &Header : HeaderKind)
228 ModuleMapHeadersSet.insert(
231 for (
auto *Submodule : Mod.submodules())
240 SmallString<256>
Directory(ModuleMapDirectory);
241 if (UmbrellaDirName.size())
242 sys::path::append(
Directory, UmbrellaDirName);
247 for (sys::fs::directory_iterator I(
Directory.str(), EC),
E; I !=
E;
251 std::string File(I->path());
252 llvm::ErrorOr<sys::fs::basic_file_status> Status = I->status();
255 sys::fs::file_type
Type = Status->type();
257 if (
Type == sys::fs::file_type::directory_file) {
275 SmallString<256> PathBuf(ModuleMapDirectory);
278 if (ModuleMapDirectory.length() == 0)
279 sys::fs::current_path(PathBuf);
282 std::unique_ptr<CompilationDatabase> Compilations;
283 Compilations.reset(
new FixedCompilationDatabase(Twine(PathBuf), CommandLine));
285 std::vector<std::string> HeaderPath;
286 HeaderPath.push_back(std::string(UmbrellaHeaderName));
289 ClangTool Tool(*Compilations, HeaderPath);
300 SmallString<256> PathBuf(ModuleMapDirectory);
302 if (ModuleMapDirectory.length() == 0)
303 sys::fs::current_path(PathBuf);
306 if (HeaderName.starts_with(PathBuf))
307 HeaderName = HeaderName.substr(PathBuf.size() + 1);
326 if (IncludePaths.size() == 0) {
333 for (std::vector<std::string>::const_iterator I = IncludePaths.begin(),
334 E = IncludePaths.end();
342 llvm::sort(FileSystemHeaders);
355 SmallString<256>
Directory(ModuleMapDirectory);
356 if (IncludePath.size())
357 sys::path::append(
Directory, IncludePath);
360 if (IncludePath.starts_with(
"/") || IncludePath.starts_with(
"\\") ||
361 ((IncludePath.size() >= 2) && (IncludePath[1] ==
':'))) {
362 llvm::errs() <<
"error: Include path \"" << IncludePath
363 <<
"\" is not relative to the module map file.\n";
370 for (sys::fs::recursive_directory_iterator I(
Directory.str(), EC),
E; I !=
E;
375 StringRef file(I->path());
376 llvm::ErrorOr<sys::fs::basic_file_status> Status = I->status();
379 sys::fs::file_type type = Status->type();
381 if (type == sys::fs::file_type::directory_file)
385 if (file.contains(
"\\.") || file.contains(
"/."))
395 llvm::errs() <<
"warning: No headers found in include path: \""
396 << IncludePath <<
"\"\n";
413 for (std::vector<std::string>::const_iterator I = FileSystemHeaders.begin(),
414 E = FileSystemHeaders.end();
417 if (ModuleMapHeadersSet.insert(*I).second) {
418 UnaccountedForHeaders.push_back(*I);
419 llvm::errs() <<
"warning: " << ModuleMapPath
420 <<
" does not account for file: " << *I <<
"\n";
Definitions for CoverageChecker.
bool IsAngled
true if this was an include with angle brackets
ModularizeUtilities class definition.
static cl::list< std::string > IncludePaths("I", cl::desc("Include path for coverage check."), cl::value_desc("path"))
static cl::opt< std::string > ModuleMapPath("module-map-path", cl::init(""), cl::desc("Turn on module map output and specify output path or file name." " If no path is specified and if prefix option is specified," " use prefix for file path."))
std::vector< llvm::StringRef > CommandLine
llvm::StringRef Directory
std::unique_ptr< CompilerInvocation > CI
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
CoverageCheckerAction(CoverageChecker &Checker)
CoverageCheckerCallbacks(CoverageChecker &Checker)
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File, StringRef SearchPath, StringRef RelativePath, const Module *SuggestedModule, bool ModuleImported, SrcMgr::CharacteristicKind FileType) override
~CoverageCheckerCallbacks() override
CoverageCheckerConsumer(CoverageChecker &Checker, Preprocessor &PP)
CoverageCheckerFrontendActionFactory(CoverageChecker &Checker)
std::unique_ptr< FrontendAction > create() override
Module map checker class.
void findUnaccountedForHeaders()
Find headers unaccounted-for in module map.
bool collectUmbrellaHeaderHeaders(llvm::StringRef UmbrellaHeaderName)
Collect headers referenced from an umbrella file.
void collectModuleHeaders()
Collect module headers.
CoverageChecker(llvm::StringRef ModuleMapPath, std::vector< std::string > &IncludePaths, llvm::ArrayRef< std::string > CommandLine, clang::ModuleMap *ModuleMap)
Constructor.
std::error_code doChecks()
Do checks.
bool collectUmbrellaHeaders(llvm::StringRef UmbrellaDirName)
Collect headers from an umbrella directory.
bool collectFileSystemHeaders()
Collect file system header files.
void collectUmbrellaHeaderHeader(llvm::StringRef HeaderName)
Called from CoverageCheckerCallbacks to track a header included from an umbrella header.
static std::unique_ptr< CoverageChecker > createCoverageChecker(llvm::StringRef ModuleMapPath, std::vector< std::string > &IncludePaths, llvm::ArrayRef< std::string > CommandLine, clang::ModuleMap *ModuleMap)
Create instance of CoverageChecker.
static std::string getCanonicalPath(llvm::StringRef FilePath)
Convert header path to canonical form.
static bool isHeader(llvm::StringRef FileName)
Check for header file extension.
static std::string getDirectoryFromPath(llvm::StringRef Path)
Get directory path component from file path.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//