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/FrontendAction.h"
237 #include "clang/Frontend/FrontendActions.h"
238 #include "clang/Lex/Preprocessor.h"
239 #include "clang/Tooling/CompilationDatabase.h"
240 #include "clang/Tooling/Tooling.h"
241 #include "llvm/Option/Arg.h"
242 #include "llvm/Option/ArgList.h"
243 #include "llvm/Option/OptTable.h"
244 #include "llvm/Option/Option.h"
245 #include "llvm/Support/CommandLine.h"
246 #include "llvm/Support/FileSystem.h"
247 #include "llvm/Support/MemoryBuffer.h"
248 #include "llvm/Support/Path.h"
254 using namespace clang;
255 using namespace clang::driver;
256 using namespace clang::driver::options;
258 using namespace llvm;
259 using namespace llvm::opt;
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::value_desc(
"path"));
318 cl::desc(
"Don't do the coverage check."));
323 cl::desc(
"Only do the coverage check."));
328 cl::desc(
"Display lists of good files (no compile errors), problem files,"
329 " and a combined list with problem files preceded by a '#'."));
338 const unsigned IncludedFlagsBitmask = options::CC1Option;
339 unsigned MissingArgIndex, MissingArgCount;
340 SmallVector<const char *, 256> Argv;
341 for (
auto I = CLArgs.begin(),
E = CLArgs.end(); I !=
E; ++I)
342 Argv.push_back(I->c_str());
343 InputArgList
Args = getDriverOptTable().ParseArgs(
344 Argv, MissingArgIndex, MissingArgCount, IncludedFlagsBitmask);
345 std::vector<std::string>
Inputs =
Args.getAllArgValues(OPT_INPUT);
352 static ArgumentsAdjuster
354 return [&Dependencies](
const CommandLineArguments &
Args,
358 CommandLineArguments NewArgs(
Args);
359 if (
int Count = FileDependents.size()) {
361 NewArgs.push_back(
"-include");
362 std::string File(std::string(
"\"") + FileDependents[
Index] +
364 NewArgs.push_back(FileDependents[
Index]);
368 NewArgs.insert(NewArgs.begin() + 1,
"-w");
370 if (!llvm::is_contained(NewArgs,
"-x")) {
371 NewArgs.insert(NewArgs.begin() + 2,
"-x");
372 NewArgs.insert(NewArgs.begin() + 3,
"c++");
388 Loc = SM.getExpansionLoc(
Loc);
392 std::pair<FileID, unsigned> Decomposed = SM.getDecomposedLoc(
Loc);
393 File = SM.getFileEntryForID(Decomposed.first);
397 Line = SM.getLineNumber(Decomposed.first, Decomposed.second);
398 Column = SM.getColumnNumber(Decomposed.first, Decomposed.second);
401 operator bool()
const {
return File !=
nullptr; }
412 if (
X.File != Y.
File)
413 return X.File < Y.
File;
414 if (
X.Line != Y.
Line)
415 return X.Line < Y.
Line;
439 static StringRef getKindName(EntryKind kind);
451 case EK_NumberOfKinds:
454 llvm_unreachable(
"invalid Entry kind");
462 return X.Loc == Y.
Loc &&
X.Name == Y.
Name;
490 CurHeaderContents[
Loc.File].push_back(HE);
493 SmallVector<Entry, 2> &Entries = (*this)[
Name];
494 for (
unsigned I = 0, N = Entries.size(); I != N; ++I) {
501 Entries.push_back(
E);
505 for (DenseMap<const FileEntry *, HeaderContents>::iterator
506 H = CurHeaderContents.begin(),
507 HEnd = CurHeaderContents.end();
510 llvm::sort(H->second);
513 DenseMap<const FileEntry *, HeaderContents>::iterator KnownH =
514 AllHeaderContents.find(H->first);
515 if (KnownH == AllHeaderContents.end()) {
517 AllHeaderContents.insert(*H);
522 if (H->second == KnownH->second)
526 std::set_symmetric_difference(
527 H->second.begin(), H->second.end(), KnownH->second.begin(),
528 KnownH->second.end(),
529 std::back_inserter(HeaderContentMismatches[H->first]));
532 CurHeaderContents.clear();
536 DenseMap<const FileEntry *, HeaderContents> CurHeaderContents;
537 DenseMap<const FileEntry *, HeaderContents> AllHeaderContents;
541 :
public RecursiveASTVisitor<CollectEntitiesVisitor> {
546 : SM(SM), Entities(Entities),
PP(
PP), PPTracker(PPTracker),
547 HadErrors(HadErrors) {}
579 SourceRange BlockRange =
D->getSourceRange();
580 const char *LinkageLabel;
581 switch (
D->getLanguage()) {
582 case LinkageSpecDecl::lang_c:
583 LinkageLabel =
"extern \"C\" {}";
585 case LinkageSpecDecl::lang_cxx:
586 LinkageLabel =
"extern \"C++\" {}";
589 if (!PPTracker.checkForIncludesInBlock(
PP, BlockRange, LinkageLabel,
597 SourceRange BlockRange =
D->getSourceRange();
598 std::string
Label(
"namespace ");
601 if (!PPTracker.checkForIncludesInBlock(
PP, BlockRange,
Label.c_str(),
610 if (!ND->getDeclContext()->isFileContext())
614 if (isa<NamespaceDecl>(ND) || isa<UsingDirectiveDecl>(ND) ||
615 isa<NamespaceAliasDecl>(ND) ||
616 isa<ClassTemplateSpecializationDecl>(ND) || isa<UsingDecl>(ND) ||
617 isa<ClassTemplateDecl>(ND) || isa<TemplateTypeParmDecl>(ND) ||
618 isa<TypeAliasTemplateDecl>(ND) || isa<UsingShadowDecl>(ND) ||
619 isa<FunctionDecl>(ND) || isa<FunctionTemplateDecl>(ND) ||
621 !cast<TagDecl>(ND)->isThisDeclarationADefinition()))
625 if (!ND->getDeclName())
630 llvm::raw_string_ostream
OS(
Name);
631 ND->printQualifiedName(
OS);
656 Preprocessor &
PP, StringRef InFile,
int &HadErrors)
657 : Entities(Entities), PPTracker(preprocessorTracker),
PP(
PP),
658 HadErrors(HadErrors) {
659 PPTracker.handlePreprocessorEntry(
PP, InFile);
665 SourceManager &SM =
Ctx.getSourceManager();
669 .TraverseDecl(
Ctx.getTranslationUnitDecl());
672 for (Preprocessor::macro_iterator
M =
PP.macro_begin(),
673 MEnd =
PP.macro_end();
683 Entities.mergeCurHeaderContents();
698 : Entities(Entities), PPTracker(preprocessorTracker),
699 HadErrors(HadErrors) {}
702 std::unique_ptr<clang::ASTConsumer>
704 return std::make_unique<CollectEntitiesConsumer>(
705 Entities, PPTracker,
CI.getPreprocessor(), InFile, HadErrors);
719 : Entities(Entities), PPTracker(preprocessorTracker),
720 HadErrors(HadErrors) {}
722 std::unique_ptr<FrontendAction>
create()
override {
723 return std::make_unique<CollectEntitiesAction>(Entities, PPTracker,
734 :
public RecursiveASTVisitor<CompileCheckVisitor> {
793 std::unique_ptr<clang::ASTConsumer>
795 return std::make_unique<CompileCheckConsumer>();
803 std::unique_ptr<FrontendAction>
create()
override {
804 return std::make_unique<CompileCheckAction>();
808 int main(
int Argc,
const char **Argv) {
815 for (
int ArgIndex = 1; ArgIndex < Argc; ArgIndex++) {
821 cl::ParseCommandLineOptions(Argc, Argv,
"modularize.\n");
825 cl::PrintHelpMessage();
829 std::unique_ptr<ModularizeUtilities> ModUtil;
833 ModularizeUtilities::createModularizeUtilities(
837 if (ModUtil->loadAllHeaderListsAndDependencies())
843 ModUtil->ProblemFileNames,
861 SmallString<256> PathBuf;
862 sys::fs::current_path(PathBuf);
863 std::unique_ptr<CompilationDatabase> Compilations;
865 new FixedCompilationDatabase(Twine(PathBuf),
CC1Arguments));
868 std::unique_ptr<PreprocessorTracker> PPTracker(
869 PreprocessorTracker::create(ModUtil->HeaderFileNames,
881 for (
auto &CompileCheckFile : ModUtil->HeaderFileNames) {
882 llvm::SmallVector<std::string, 32> CompileCheckFileArray;
883 CompileCheckFileArray.push_back(CompileCheckFile);
884 ClangTool CompileCheckTool(*Compilations, CompileCheckFileArray);
885 CompileCheckTool.appendArgumentsAdjuster(
887 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(std::string(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(std::string(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();