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;
546 : SM(SM), Entities(Entities),
PP(
PP), PPTracker(PPTracker),
547 HadErrors(HadErrors) {}
576 SourceRange BlockRange =
D->getSourceRange();
577 const char *LinkageLabel;
578 switch (
D->getLanguage()) {
579 case LinkageSpecDecl::lang_c:
580 LinkageLabel =
"extern \"C\" {}";
582 case LinkageSpecDecl::lang_cxx:
583 LinkageLabel =
"extern \"C++\" {}";
586 if (!PPTracker.checkForIncludesInBlock(
PP, BlockRange, LinkageLabel,
594 SourceRange BlockRange =
D->getSourceRange();
595 std::string
Label(
"namespace ");
598 if (!PPTracker.checkForIncludesInBlock(
PP, BlockRange,
Label.c_str(),
607 if (!ND->getDeclContext()->isFileContext())
611 if (isa<NamespaceDecl>(ND) || isa<UsingDirectiveDecl>(ND) ||
612 isa<NamespaceAliasDecl>(ND) ||
613 isa<ClassTemplateSpecializationDecl>(ND) || isa<UsingDecl>(ND) ||
614 isa<ClassTemplateDecl>(ND) || isa<TemplateTypeParmDecl>(ND) ||
615 isa<TypeAliasTemplateDecl>(ND) || isa<UsingShadowDecl>(ND) ||
616 isa<FunctionDecl>(ND) || isa<FunctionTemplateDecl>(ND) ||
618 !cast<TagDecl>(ND)->isThisDeclarationADefinition()))
622 if (!ND->getDeclName())
627 llvm::raw_string_ostream
OS(
Name);
628 ND->printQualifiedName(
OS);
653 Preprocessor &
PP, StringRef InFile,
int &HadErrors)
654 : Entities(Entities), PPTracker(preprocessorTracker),
PP(
PP),
655 HadErrors(HadErrors) {
656 PPTracker.handlePreprocessorEntry(
PP, InFile);
662 SourceManager &SM =
Ctx.getSourceManager();
666 .TraverseDecl(
Ctx.getTranslationUnitDecl());
669 for (Preprocessor::macro_iterator
M =
PP.macro_begin(),
670 MEnd =
PP.macro_end();
680 Entities.mergeCurHeaderContents();
695 : Entities(Entities), PPTracker(preprocessorTracker),
696 HadErrors(HadErrors) {}
699 std::unique_ptr<clang::ASTConsumer>
701 return std::make_unique<CollectEntitiesConsumer>(
702 Entities, PPTracker,
CI.getPreprocessor(), InFile, HadErrors);
716 : Entities(Entities), PPTracker(preprocessorTracker),
717 HadErrors(HadErrors) {}
719 std::unique_ptr<FrontendAction>
create()
override {
720 return std::make_unique<CollectEntitiesAction>(Entities, PPTracker,
731 :
public RecursiveASTVisitor<CompileCheckVisitor> {
787 std::unique_ptr<clang::ASTConsumer>
789 return std::make_unique<CompileCheckConsumer>();
797 std::unique_ptr<FrontendAction>
create()
override {
798 return std::make_unique<CompileCheckAction>();
802 int main(
int Argc,
const char **Argv) {
809 for (
int ArgIndex = 1; ArgIndex < Argc; ArgIndex++) {
815 cl::ParseCommandLineOptions(Argc, Argv,
"modularize.\n");
819 cl::PrintHelpMessage();
823 std::unique_ptr<ModularizeUtilities> ModUtil;
827 ModularizeUtilities::createModularizeUtilities(
831 if (ModUtil->loadAllHeaderListsAndDependencies())
837 ModUtil->ProblemFileNames,
855 SmallString<256> PathBuf;
856 sys::fs::current_path(PathBuf);
857 std::unique_ptr<CompilationDatabase> Compilations;
859 new FixedCompilationDatabase(Twine(PathBuf),
CC1Arguments));
862 std::unique_ptr<PreprocessorTracker> PPTracker(
863 PreprocessorTracker::create(ModUtil->HeaderFileNames,
875 for (
auto &CompileCheckFile : ModUtil->HeaderFileNames) {
876 llvm::SmallVector<std::string, 32> CompileCheckFileArray;
877 CompileCheckFileArray.push_back(CompileCheckFile);
878 ClangTool CompileCheckTool(*Compilations, CompileCheckFileArray);
879 CompileCheckTool.appendArgumentsAdjuster(
881 int CompileCheckFileErrors = 0;
884 CompileCheckFileErrors |= CompileCheckTool.run(&CompileCheckFactory);
885 if (CompileCheckFileErrors != 0) {
886 ModUtil->addUniqueProblemFile(CompileCheckFile);
890 ModUtil->addNoCompileErrorsFile(CompileCheckFile);
895 ClangTool Tool(*Compilations,
897 Tool.appendArgumentsAdjuster(
900 HadErrors |= Tool.run(&Factory);
903 typedef SmallVector<Location, 8> LocationArray;
904 typedef SmallVector<LocationArray, Entry::EK_NumberOfKinds> EntryBinArray;
905 EntryBinArray EntryBins;
909 EntryBins.push_back(Array);
913 for (EntityMap::iterator
E = Entities.begin(), EEnd = Entities.end();
916 if (
E->second.size() == 1)
919 for (EntryBinArray::iterator
CI = EntryBins.begin(),
CE = EntryBins.end();
925 for (
unsigned I = 0, N =
E->second.size(); I != N; ++I) {
926 EntryBins[
E->second[I].Kind].push_back(
E->second[I].Loc);
930 for (EntryBinArray::iterator DI = EntryBins.begin(), DE = EntryBins.end();
931 DI != DE; ++DI, ++KindIndex) {
932 int ECount = DI->size();
936 LocationArray::iterator FI = DI->begin();
938 errs() <<
"error: " << kindName <<
" '" <<
E->first()
939 <<
"' defined at multiple locations:\n";
940 for (LocationArray::iterator FE = DI->end(); FI != FE; ++FI) {
941 errs() <<
" " << FI->File->getName() <<
":" << FI->Line <<
":"
942 << FI->Column <<
"\n";
943 ModUtil->addUniqueProblemFile(std::string(FI->File->getName()));
951 if (PPTracker->reportInconsistentMacros(errs()))
956 if (PPTracker->reportInconsistentConditionals(errs()))
963 for (DenseMap<const FileEntry *, HeaderContents>::iterator
967 if (H->second.empty()) {
968 errs() <<
"internal error: phantom header content mismatch\n";
973 ModUtil->addUniqueProblemFile(std::string(H->first->getName()));
974 errs() <<
"error: header '" << H->first->getName()
975 <<
"' has different contents depending on how it was included.\n";
976 for (
unsigned I = 0, N = H->second.size(); I != N; ++I) {
977 errs() <<
"note: '" << H->second[I].Name <<
"' in "
978 << H->second[I].Loc.File->getName() <<
" at "
979 << H->second[I].Loc.Line <<
":" << H->second[I].Loc.Column
980 <<
" not always provided\n";
985 ModUtil->displayProblemFiles();
986 ModUtil->displayGoodFiles();
987 ModUtil->displayCombinedFiles();