230 #include "clang/AST/ASTConsumer.h" 231 #include "clang/AST/ASTContext.h" 232 #include "clang/AST/RecursiveASTVisitor.h" 233 #include "clang/Basic/SourceManager.h" 234 #include "clang/Driver/Options.h" 235 #include "clang/Frontend/CompilerInstance.h" 236 #include "clang/Frontend/FrontendActions.h" 237 #include "clang/Lex/Preprocessor.h" 238 #include "clang/Tooling/CompilationDatabase.h" 239 #include "clang/Tooling/Tooling.h" 240 #include "llvm/Option/Arg.h" 241 #include "llvm/Option/ArgList.h" 242 #include "llvm/Option/OptTable.h" 243 #include "llvm/Option/Option.h" 244 #include "llvm/Support/CommandLine.h" 245 #include "llvm/Support/FileSystem.h" 246 #include "llvm/Support/MemoryBuffer.h" 247 #include "llvm/Support/Path.h" 254 using namespace clang;
258 using namespace llvm;
263 static cl::list<std::string>
265 cl::desc(
"<list of one or more header list files>"),
269 static cl::list<std::string>
271 cl::desc(
"<arguments to be passed to front end>..."));
275 "prefix", cl::init(
""),
277 "Prepend header file paths with this prefix." 279 " the files are considered to be relative to the header list file."));
284 "module-map-path", cl::init(
""),
285 cl::desc(
"Turn on module map output and specify output path or file name." 286 " If no path is specified and if prefix option is specified," 287 " use prefix for file path."));
292 "problem-files-list", cl::init(
""),
294 "List of files with compilation or modularization problems for" 295 " assistant mode. This will be excluded."));
298 static cl::opt<std::string>
300 cl::desc(
"Specify the name of the root module."));
308 cl::desc(
"Only warn if #include directives are inside extern or namespace" 309 " blocks if the included header is in the header list."));
312 static cl::list<std::string>
313 IncludePaths(
"I", cl::desc(
"Include path for coverage check."),
314 cl::ZeroOrMore, cl::value_desc(
"path"));
319 cl::desc(
"Don't do the coverage check."));
324 cl::desc(
"Only do the coverage check."));
329 cl::desc(
"Display lists of good files (no compile errors), problem files," 330 " and a combined list with problem files preceded by a '#'."));
339 std::unique_ptr<OptTable> Opts(createDriverOptTable());
340 const unsigned IncludedFlagsBitmask = options::CC1Option;
341 unsigned MissingArgIndex, MissingArgCount;
342 SmallVector<const char *, 256> Argv;
343 for (
auto I = CLArgs.begin(), E = CLArgs.end(); I != E; ++I)
344 Argv.push_back(I->c_str());
345 InputArgList Args = Opts->ParseArgs(Argv, MissingArgIndex, MissingArgCount,
346 IncludedFlagsBitmask);
347 std::vector<std::string> Inputs = Args.getAllArgValues(OPT_INPUT);
354 static ArgumentsAdjuster
356 return [&Dependencies](
const CommandLineArguments &Args,
360 CommandLineArguments NewArgs(Args);
361 if (
int Count = FileDependents.size()) {
363 NewArgs.push_back(
"-include");
364 std::string File(std::string(
"\"") + FileDependents[
Index] +
366 NewArgs.push_back(FileDependents[
Index]);
370 NewArgs.insert(NewArgs.begin() + 1,
"-w");
372 if (!llvm::is_contained(NewArgs,
"-x")) {
373 NewArgs.insert(NewArgs.begin() + 2,
"-x");
374 NewArgs.insert(NewArgs.begin() + 3,
"c++");
389 Location(SourceManager &SM, SourceLocation
Loc) : File(), Line(), Column() {
390 Loc = SM.getExpansionLoc(Loc);
394 std::pair<FileID, unsigned> Decomposed = SM.getDecomposedLoc(Loc);
395 File = SM.getFileEntryForID(Decomposed.first);
399 Line = SM.getLineNumber(Decomposed.first, Decomposed.second);
400 Column = SM.getColumnNumber(Decomposed.first, Decomposed.second);
403 operator bool()
const {
return File !=
nullptr; }
441 static StringRef getKindName(
EntryKind kind);
453 case EK_NumberOfKinds:
456 llvm_unreachable(
"invalid Entry kind");
492 CurHeaderContents[Loc.File].push_back(HE);
495 SmallVector<Entry, 2> &Entries = (*this)[
Name];
496 for (
unsigned I = 0, N = Entries.size(); I != N; ++I) {
497 if (Entries[I].Kind == Kind && Entries[I].Loc == Loc)
503 Entries.push_back(E);
507 for (DenseMap<const FileEntry *, HeaderContents>::iterator
508 H = CurHeaderContents.begin(),
509 HEnd = CurHeaderContents.end();
512 std::sort(H->second.begin(), H->second.end());
515 DenseMap<const FileEntry *, HeaderContents>::iterator KnownH =
516 AllHeaderContents.find(H->first);
517 if (KnownH == AllHeaderContents.end()) {
519 AllHeaderContents.insert(*H);
524 if (H->second == KnownH->second)
528 std::set_symmetric_difference(
529 H->second.begin(), H->second.end(), KnownH->second.begin(),
530 KnownH->second.end(),
531 std::back_inserter(HeaderContentMismatches[H->first]));
534 CurHeaderContents.clear();
538 DenseMap<const FileEntry *, HeaderContents> CurHeaderContents;
539 DenseMap<const FileEntry *, HeaderContents> AllHeaderContents;
543 :
public RecursiveASTVisitor<CollectEntitiesVisitor> {
548 : SM(SM), Entities(Entities), PP(PP), PPTracker(PPTracker),
549 HadErrors(HadErrors) {}
581 SourceRange BlockRange = D->getSourceRange();
582 const char *LinkageLabel;
583 switch (D->getLanguage()) {
584 case LinkageSpecDecl::lang_c:
585 LinkageLabel =
"extern \"C\" {}";
587 case LinkageSpecDecl::lang_cxx:
588 LinkageLabel =
"extern \"C++\" {}";
591 if (!PPTracker.checkForIncludesInBlock(PP, BlockRange, LinkageLabel,
599 SourceRange BlockRange = D->getSourceRange();
600 std::string Label(
"namespace ");
601 Label += D->getName();
603 if (!PPTracker.checkForIncludesInBlock(PP, BlockRange, Label.c_str(),
612 if (!ND->getDeclContext()->isFileContext())
616 if (isa<NamespaceDecl>(ND) || isa<UsingDirectiveDecl>(ND) ||
617 isa<NamespaceAliasDecl>(ND) ||
618 isa<ClassTemplateSpecializationDecl>(ND) || isa<UsingDecl>(ND) ||
619 isa<ClassTemplateDecl>(ND) || isa<TemplateTypeParmDecl>(ND) ||
620 isa<TypeAliasTemplateDecl>(ND) || isa<UsingShadowDecl>(ND) ||
621 isa<FunctionDecl>(ND) || isa<FunctionTemplateDecl>(ND) ||
623 !cast<TagDecl>(ND)->isThisDeclarationADefinition()))
627 if (!ND->getDeclName())
632 llvm::raw_string_ostream OS(Name);
633 ND->printQualifiedName(OS);
658 Preprocessor &PP, StringRef InFile,
int &HadErrors)
659 : Entities(Entities), PPTracker(preprocessorTracker), PP(PP),
660 HadErrors(HadErrors) {
661 PPTracker.handlePreprocessorEntry(PP, InFile);
667 SourceManager &SM = Ctx.getSourceManager();
671 .TraverseDecl(Ctx.getTranslationUnitDecl());
674 for (Preprocessor::macro_iterator M = PP.macro_begin(),
675 MEnd = PP.macro_end();
677 Location Loc(SM, M->second.getLatest()->getLocation());
685 Entities.mergeCurHeaderContents();
700 : Entities(Entities), PPTracker(preprocessorTracker),
701 HadErrors(HadErrors) {}
704 std::unique_ptr<clang::ASTConsumer>
706 return llvm::make_unique<CollectEntitiesConsumer>(
707 Entities, PPTracker, CI.getPreprocessor(), InFile, HadErrors);
721 : Entities(Entities), PPTracker(preprocessorTracker),
722 HadErrors(HadErrors) {}
735 :
public RecursiveASTVisitor<CompileCheckVisitor> {
794 std::unique_ptr<clang::ASTConsumer>
796 return llvm::make_unique<CompileCheckConsumer>();
809 int main(
int Argc,
const char **Argv) {
815 CommandLine = sys::path::stem(sys::path::filename(Argv0));
816 for (
int ArgIndex = 1; ArgIndex < Argc; ArgIndex++) {
817 CommandLine.append(
" ");
818 CommandLine.append(Argv[ArgIndex]);
822 cl::ParseCommandLineOptions(Argc, Argv,
"modularize.\n");
826 cl::PrintHelpMessage();
830 std::unique_ptr<ModularizeUtilities> ModUtil;
834 ModularizeUtilities::createModularizeUtilities(
838 if (ModUtil->loadAllHeaderListsAndDependencies())
844 ModUtil->ProblemFileNames,
853 if (ModUtil->doCoverageCheck(
IncludePaths, CommandLine))
862 SmallString<256> PathBuf;
863 sys::fs::current_path(PathBuf);
864 std::unique_ptr<CompilationDatabase> Compilations;
866 new FixedCompilationDatabase(Twine(PathBuf),
CC1Arguments));
869 std::unique_ptr<PreprocessorTracker> PPTracker(
870 PreprocessorTracker::create(ModUtil->HeaderFileNames,
882 for (
auto &CompileCheckFile : ModUtil->HeaderFileNames) {
883 llvm::SmallVector<std::string, 32> CompileCheckFileArray;
884 CompileCheckFileArray.push_back(CompileCheckFile);
885 ClangTool CompileCheckTool(*Compilations, CompileCheckFileArray);
886 CompileCheckTool.appendArgumentsAdjuster(
888 int CompileCheckFileErrors = 0;
890 CompileCheckFileErrors |= CompileCheckTool.run(&CompileCheckFactory);
891 if (CompileCheckFileErrors != 0) {
892 ModUtil->addUniqueProblemFile(CompileCheckFile);
896 ModUtil->addNoCompileErrorsFile(CompileCheckFile);
901 ClangTool Tool(*Compilations,
903 Tool.appendArgumentsAdjuster(
906 HadErrors |= Tool.run(&Factory);
909 typedef SmallVector<Location, 8> LocationArray;
910 typedef SmallVector<LocationArray, Entry::EK_NumberOfKinds> EntryBinArray;
911 EntryBinArray EntryBins;
915 EntryBins.push_back(Array);
919 for (EntityMap::iterator E = Entities.begin(), EEnd = Entities.end();
922 if (E->second.size() == 1)
925 for (EntryBinArray::iterator CI = EntryBins.begin(), CE = EntryBins.end();
931 for (
unsigned I = 0, N = E->second.size(); I != N; ++I) {
932 EntryBins[E->second[I].Kind].push_back(E->second[I].Loc);
936 for (EntryBinArray::iterator DI = EntryBins.begin(), DE = EntryBins.end();
937 DI != DE; ++DI, ++KindIndex) {
938 int ECount = DI->size();
942 LocationArray::iterator FI = DI->begin();
944 errs() <<
"error: " << kindName <<
" '" << E->first()
945 <<
"' defined at multiple locations:\n";
946 for (LocationArray::iterator FE = DI->end(); FI != FE; ++FI) {
947 errs() <<
" " << FI->File->getName() <<
":" << FI->Line <<
":" 948 << FI->Column <<
"\n";
949 ModUtil->addUniqueProblemFile(FI->File->getName());
957 if (PPTracker->reportInconsistentMacros(errs()))
962 if (PPTracker->reportInconsistentConditionals(errs()))
969 for (DenseMap<const FileEntry *, HeaderContents>::iterator
973 if (H->second.empty()) {
974 errs() <<
"internal error: phantom header content mismatch\n";
979 ModUtil->addUniqueProblemFile(H->first->getName());
980 errs() <<
"error: header '" << H->first->getName()
981 <<
"' has different contents depending on how it was included.\n";
982 for (
unsigned I = 0, N = H->second.size(); I != N; ++I) {
983 errs() <<
"note: '" << H->second[I].Name <<
"' in " 984 << H->second[I].Loc.File->getName() <<
" at " 985 << H->second[I].Loc.Line <<
":" << H->second[I].Loc.Column
986 <<
" not always provided\n";
991 ModUtil->displayProblemFiles();
992 ModUtil->displayGoodFiles();
993 ModUtil->displayCombinedFiles();
static cl::opt< bool > DisplayFileLists("display-file-lists", cl::init(false), cl::desc("Display lists of good files (no compile errors), problem files," " and a combined list with problem files preceded by a '#'."))
SourceLocation Loc
'#' location in the include directive
bool TraverseTemplateArguments(const TemplateArgument *Args, unsigned NumArgs)
friend bool operator>(const Location &X, const Location &Y)
void HandleTranslationUnit(ASTContext &Ctx) override
void HandleTranslationUnit(ASTContext &Ctx) override
DenseMap< const FileEntry *, HeaderContents > HeaderContentMismatches
std::unique_ptr< clang::ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Some operations such as code completion produce a set of candidates.
static cl::list< std::string > CC1Arguments(cl::ConsumeAfter, cl::desc("<arguments to be passed to front end>..."))
Common definitions for Modularize.
friend bool operator!=(const Location &X, const Location &Y)
static cl::opt< bool > CoverageCheckOnly("coverage-check-only", cl::init(false), cl::desc("Only do the coverage check."))
bool TraverseConstructorInitializer(CXXCtorInitializer *Init)
friend bool operator<=(const Location &X, const Location &Y)
llvm::SmallVector< std::string, 4 > DependentsVector
bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo)
bool TraverseTemplateArguments(const TemplateArgument *Args, unsigned NumArgs)
bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc)
bool TraverseTemplateName(TemplateName Template)
static cl::opt< std::string > HeaderPrefix("prefix", cl::init(""), cl::desc("Prepend header file paths with this prefix." " If not specified," " the files are considered to be relative to the header list file."))
CollectEntitiesAction(EntityMap &Entities, PreprocessorTracker &preprocessorTracker, int &HadErrors)
bool VisitNamedDecl(NamedDecl *ND)
static ArgumentsAdjuster getModularizeArgumentsAdjuster(DependencyMap &Dependencies)
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."))
static cl::list< std::string > ListFileNames(cl::Positional, cl::value_desc("list"), cl::desc("<list of one or more header list files>"), cl::CommaSeparated)
bool TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C, Expr *Init)
ModularizeFrontendActionFactory(EntityMap &Entities, PreprocessorTracker &preprocessorTracker, int &HadErrors)
ModularizeUtilities class definition.
void mergeCurHeaderContents()
bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS)
static ClangTidyModuleRegistry::Add< AbseilModule > X("abseil-module", "Add Abseil checks.")
static cl::opt< bool > BlockCheckHeaderListOnly("block-check-header-list-only", cl::init(false), cl::desc("Only warn if #include directives are inside extern or namespace" " blocks if the included header is in the header list."))
static std::string findInputFile(const CommandLineArguments &CLArgs)
friend bool operator<(const Location &X, const Location &Y)
void add(const std::string &Name, enum Entry::EntryKind Kind, Location Loc)
bool VisitNamespaceDecl(const NamespaceDecl *D)
bool TraverseStmt(Stmt *S)
bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo)
bool TraverseConstructorInitializer(CXXCtorInitializer *Init)
bool VisitNamespaceDecl(const NamespaceDecl *D)
std::unique_ptr< clang::ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
bool createModuleMap(llvm::StringRef ModuleMapPath, llvm::ArrayRef< std::string > HeaderFileNames, llvm::ArrayRef< std::string > ProblemFileNames, DependencyMap &Dependencies, llvm::StringRef HeaderPrefix, llvm::StringRef RootModuleName)
Create the module map file.
static constexpr llvm::StringLiteral Name
bool TraverseStmt(Stmt *S)
int main(int Argc, const char **Argv)
bool VisitLinkageSpecDecl(LinkageSpecDecl *D)
bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS)
~CollectEntitiesConsumer() override
CompileCheckAction * create() override
bool TraverseTypeLoc(TypeLoc TL)
llvm::StringMap< DependentsVector > DependencyMap
static cl::opt< bool > NoCoverageCheck("no-coverage-check", cl::init(false), cl::desc("Don't do the coverage check."))
CollectEntitiesAction * create() override
static cl::list< std::string > IncludePaths("I", cl::desc("Include path for coverage check."), cl::ZeroOrMore, cl::value_desc("path"))
bool TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C, Expr *Init)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::Optional< std::string > getCanonicalPath(const FileEntry *F, const SourceManager &SourceMgr)
Get the canonical path of F.
static cl::opt< std::string > RootModule("root-module", cl::init(""), cl::desc("Specify the name of the root module."))
Macro expansions and preprocessor conditional consistency checker.
friend bool operator>=(const Location &X, const Location &Y)
friend bool operator==(const Location &X, const Location &Y)
bool VisitLinkageSpecDecl(LinkageSpecDecl *D)
bool TraverseTemplateName(TemplateName Template)
Location(SourceManager &SM, SourceLocation Loc)
bool TraverseType(QualType T)
static cl::opt< std::string > ProblemFilesList("problem-files-list", cl::init(""), cl::desc("List of files with compilation or modularization problems for" " assistant mode. This will be excluded."))
std::vector< HeaderEntry > HeaderContents
bool VisitNamedDecl(NamedDecl *ND)
CollectEntitiesConsumer(EntityMap &Entities, PreprocessorTracker &preprocessorTracker, Preprocessor &PP, StringRef InFile, int &HadErrors)
Preprocessor tracker for modularize.
bool TraverseTypeLoc(TypeLoc TL)
bool TraverseType(QualType T)
bool TraverseTemplateArgument(const TemplateArgument &Arg)
bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS)
bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc)
CollectEntitiesVisitor(SourceManager &SM, EntityMap &Entities, Preprocessor &PP, PreprocessorTracker &PPTracker, int &HadErrors)
CompileCheckFrontendActionFactory()
bool TraverseTemplateArgument(const TemplateArgument &Arg)
const SymbolIndex * Index
bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS)