Follow up to d0858bffa11, add missing REQUIRES x86
[llvm-project.git] / clang-tools-extra / modularize / Modularize.cpp
blobdaa9c048279e03fed32e986c7c4869ad3d243094
1 //===- extra/modularize/Modularize.cpp - Check modularized headers --------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Introduction
11 // This file implements a tool that checks whether a set of headers provides
12 // the consistent definitions required to use modules. It can also check an
13 // existing module map for full coverage of the headers in a directory tree.
15 // For example, in examining headers, it detects whether the same entity
16 // (say, a NULL macro or size_t typedef) is defined in multiple headers
17 // or whether a header produces different definitions under
18 // different circumstances. These conditions cause modules built from the
19 // headers to behave poorly, and should be fixed before introducing a module
20 // map.
22 // Modularize takes as input either one or more module maps (by default,
23 // "module.modulemap") or one or more text files containing lists of headers
24 // to check.
26 // In the case of a module map, the module map must be well-formed in
27 // terms of syntax. Modularize will extract the header file names
28 // from the map. Only normal headers are checked, assuming headers
29 // marked "private", "textual", or "exclude" are not to be checked
30 // as a top-level include, assuming they either are included by
31 // other headers which are checked, or they are not suitable for
32 // modules.
34 // In the case of a file list, the list is a newline-separated list of headers
35 // to check with respect to each other.
36 // Lines beginning with '#' and empty lines are ignored.
37 // Header file names followed by a colon and other space-separated
38 // file names will include those extra files as dependencies.
39 // The file names can be relative or full paths, but must be on the
40 // same line.
42 // Modularize also accepts regular clang front-end arguments.
44 // Usage: modularize [(modularize options)]
45 // [(include-files_list)|(module map)]+ [(front-end-options) ...]
47 // Options:
48 // -prefix=(optional header path prefix)
49 // Note that unless a "-prefix (header path)" option is specified,
50 // non-absolute file paths in the header list file will be relative
51 // to the header list file directory. Use -prefix to specify a
52 // different directory.
53 // -module-map-path=(module map)
54 // Skip the checks, and instead act as a module.map generation
55 // assistant, generating a module map file based on the header list.
56 // An optional "-root-module=(rootName)" argument can specify a root
57 // module to be created in the generated module.map file. Note that
58 // you will likely need to edit this file to suit the needs of your
59 // headers.
60 // -problem-files-list=(problem files list file name)
61 // For use only with module map assistant. Input list of files that
62 // have problems with respect to modules. These will still be
63 // included in the generated module map, but will be marked as
64 // "excluded" headers.
65 // -root-module=(root module name)
66 // Specifies a root module to be created in the generated module.map
67 // file.
68 // -block-check-header-list-only
69 // Only warn if #include directives are inside extern or namespace
70 // blocks if the included header is in the header list.
71 // -no-coverage-check
72 // Don't do the coverage check.
73 // -coverage-check-only
74 // Only do the coverage check.
75 // -display-file-lists
76 // Display lists of good files (no compile errors), problem files,
77 // and a combined list with problem files preceded by a '#'.
78 // This can be used to quickly determine which files have problems.
79 // The latter combined list might be useful in starting to modularize
80 // a set of headers. You can start with a full list of headers,
81 // use -display-file-lists option, and then use the combined list as
82 // your intermediate list, uncommenting-out headers as you fix them.
84 // Note that by default, the modularize assumes .h files contain C++ source.
85 // If your .h files in the file list contain another language, you should
86 // append an appropriate -x option to your command line, i.e.: -x c
88 // Modularization Issue Checks
90 // In the process of checking headers for modularization issues, modularize
91 // will do normal parsing, reporting normal errors and warnings,
92 // but will also report special error messages like the following:
94 // error: '(symbol)' defined at multiple locations:
95 // (file):(row):(column)
96 // (file):(row):(column)
98 // error: header '(file)' has different contents depending on how it was
99 // included
101 // The latter might be followed by messages like the following:
103 // note: '(symbol)' in (file) at (row):(column) not always provided
105 // Checks will also be performed for macro expansions, defined(macro)
106 // expressions, and preprocessor conditional directives that evaluate
107 // inconsistently, and can produce error messages like the following:
109 // (...)/SubHeader.h:11:5:
110 // #if SYMBOL == 1
111 // ^
112 // error: Macro instance 'SYMBOL' has different values in this header,
113 // depending on how it was included.
114 // 'SYMBOL' expanded to: '1' with respect to these inclusion paths:
115 // (...)/Header1.h
116 // (...)/SubHeader.h
117 // (...)/SubHeader.h:3:9:
118 // #define SYMBOL 1
119 // ^
120 // Macro defined here.
121 // 'SYMBOL' expanded to: '2' with respect to these inclusion paths:
122 // (...)/Header2.h
123 // (...)/SubHeader.h
124 // (...)/SubHeader.h:7:9:
125 // #define SYMBOL 2
126 // ^
127 // Macro defined here.
129 // Checks will also be performed for '#include' directives that are
130 // nested inside 'extern "C/C++" {}' or 'namespace (name) {}' blocks,
131 // and can produce error message like the following:
133 // IncludeInExtern.h:2:3
134 // #include "Empty.h"
135 // ^
136 // error: Include directive within extern "C" {}.
137 // IncludeInExtern.h:1:1
138 // extern "C" {
139 // ^
140 // The "extern "C" {}" block is here.
142 // See PreprocessorTracker.cpp for additional details.
144 // Module Map Coverage Check
146 // The coverage check uses the Clang ModuleMap class to read and parse the
147 // module map file. Starting at the module map file directory, or just the
148 // include paths, if specified, it will collect the names of all the files it
149 // considers headers (no extension, .h, or .inc--if you need more, modify the
150 // isHeader function). It then compares the headers against those referenced
151 // in the module map, either explicitly named, or implicitly named via an
152 // umbrella directory or umbrella file, as parsed by the ModuleMap object.
153 // If headers are found which are not referenced or covered by an umbrella
154 // directory or file, warning messages will be produced, and this program
155 // will return an error code of 1. Other errors result in an error code of 2.
156 // If no problems are found, an error code of 0 is returned.
158 // Note that in the case of umbrella headers, this tool invokes the compiler
159 // to preprocess the file, and uses a callback to collect the header files
160 // included by the umbrella header or any of its nested includes. If any
161 // front end options are needed for these compiler invocations, these
162 // can be included on the command line after the module map file argument.
164 // Warning message have the form:
166 // warning: module.modulemap does not account for file: Level3A.h
168 // Note that for the case of the module map referencing a file that does
169 // not exist, the module map parser in Clang will (at the time of this
170 // writing) display an error message.
172 // Module Map Assistant - Module Map Generation
174 // Modularize also has an option ("-module-map-path=module.modulemap") that will
175 // skip the checks, and instead act as a module.modulemap generation assistant,
176 // generating a module map file based on the header list. An optional
177 // "-root-module=(rootName)" argument can specify a root module to be
178 // created in the generated module.modulemap file. Note that you will likely
179 // need to edit this file to suit the needs of your headers.
181 // An example command line for generating a module.modulemap file:
183 // modularize -module-map-path=module.modulemap -root-module=myroot \
184 // headerlist.txt
186 // Note that if the headers in the header list have partial paths, sub-modules
187 // will be created for the subdirectories involved, assuming that the
188 // subdirectories contain headers to be grouped into a module, but still with
189 // individual modules for the headers in the subdirectory.
191 // See the ModuleAssistant.cpp file comments for additional details about the
192 // implementation of the assistant mode.
194 // Future directions:
196 // Basically, we want to add new checks for whatever we can check with respect
197 // to checking headers for module'ability.
199 // Some ideas:
201 // 1. Omit duplicate "not always provided" messages
203 // 2. Add options to disable any of the checks, in case
204 // there is some problem with them, or the messages get too verbose.
206 // 3. Try to figure out the preprocessor conditional directives that
207 // contribute to problems and tie them to the inconsistent definitions.
209 // 4. There are some legitimate uses of preprocessor macros that
210 // modularize will flag as errors, such as repeatedly #include'ing
211 // a file and using interleaving defined/undefined macros
212 // to change declarations in the included file. Is there a way
213 // to address this? Maybe have modularize accept a list of macros
214 // to ignore. Otherwise you can just exclude the file, after checking
215 // for legitimate errors.
217 // 5. What else?
219 // General clean-up and refactoring:
221 // 1. The Location class seems to be something that we might
222 // want to design to be applicable to a wider range of tools, and stick it
223 // somewhere into Tooling/ in mainline
225 //===----------------------------------------------------------------------===//
227 #include "Modularize.h"
228 #include "ModularizeUtilities.h"
229 #include "PreprocessorTracker.h"
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"
249 #include <algorithm>
250 #include <iterator>
251 #include <map>
252 #include <string>
253 #include <vector>
255 using namespace clang;
256 using namespace clang::driver;
257 using namespace clang::driver::options;
258 using namespace clang::tooling;
259 using namespace llvm;
260 using namespace llvm::opt;
261 using namespace Modularize;
263 // Option to specify a file name for a list of header files to check.
264 static cl::list<std::string>
265 ListFileNames(cl::Positional, cl::value_desc("list"),
266 cl::desc("<list of one or more header list files>"),
267 cl::CommaSeparated);
269 // Collect all other arguments, which will be passed to the front end.
270 static cl::list<std::string>
271 CC1Arguments(cl::ConsumeAfter,
272 cl::desc("<arguments to be passed to front end>..."));
274 // Option to specify a prefix to be prepended to the header names.
275 static cl::opt<std::string> HeaderPrefix(
276 "prefix", cl::init(""),
277 cl::desc(
278 "Prepend header file paths with this prefix."
279 " If not specified,"
280 " the files are considered to be relative to the header list file."));
282 // Option for assistant mode, telling modularize to output a module map
283 // based on the headers list, and where to put it.
284 static cl::opt<std::string> ModuleMapPath(
285 "module-map-path", cl::init(""),
286 cl::desc("Turn on module map output and specify output path or file name."
287 " If no path is specified and if prefix option is specified,"
288 " use prefix for file path."));
290 // Option to specify list of problem files for assistant.
291 // This will cause assistant to exclude these files.
292 static cl::opt<std::string> ProblemFilesList(
293 "problem-files-list", cl::init(""),
294 cl::desc(
295 "List of files with compilation or modularization problems for"
296 " assistant mode. This will be excluded."));
298 // Option for assistant mode, telling modularize the name of the root module.
299 static cl::opt<std::string>
300 RootModule("root-module", cl::init(""),
301 cl::desc("Specify the name of the root module."));
303 // Option for limiting the #include-inside-extern-or-namespace-block
304 // check to only those headers explicitly listed in the header list.
305 // This is a work-around for private includes that purposefully get
306 // included inside blocks.
307 static cl::opt<bool>
308 BlockCheckHeaderListOnly("block-check-header-list-only", cl::init(false),
309 cl::desc("Only warn if #include directives are inside extern or namespace"
310 " blocks if the included header is in the header list."));
312 // Option for include paths for coverage check.
313 static cl::list<std::string>
314 IncludePaths("I", cl::desc("Include path for coverage check."),
315 cl::value_desc("path"));
317 // Option for disabling the coverage check.
318 static cl::opt<bool> NoCoverageCheck("no-coverage-check",
319 cl::desc("Don't do the coverage check."));
321 // Option for just doing the coverage check.
322 static cl::opt<bool>
323 CoverageCheckOnly("coverage-check-only", cl::init(false),
324 cl::desc("Only do the coverage check."));
326 // Option for displaying lists of good, bad, and mixed files.
327 static cl::opt<bool>
328 DisplayFileLists("display-file-lists", cl::init(false),
329 cl::desc("Display lists of good files (no compile errors), problem files,"
330 " and a combined list with problem files preceded by a '#'."));
332 // Save the program name for error messages.
333 const char *Argv0;
334 // Save the command line for comments.
335 std::string CommandLine;
337 // Helper function for finding the input file in an arguments list.
338 static std::string findInputFile(const CommandLineArguments &CLArgs) {
339 llvm::opt::Visibility VisibilityMask(options::CC1Option);
340 unsigned MissingArgIndex, MissingArgCount;
341 SmallVector<const char *, 256> Argv;
342 for (auto I = CLArgs.begin(), E = CLArgs.end(); I != E; ++I)
343 Argv.push_back(I->c_str());
344 InputArgList Args = getDriverOptTable().ParseArgs(
345 Argv, MissingArgIndex, MissingArgCount, VisibilityMask);
346 std::vector<std::string> Inputs = Args.getAllArgValues(OPT_INPUT);
347 return ModularizeUtilities::getCanonicalPath(Inputs.back());
350 // This arguments adjuster inserts "-include (file)" arguments for header
351 // dependencies. It also inserts a "-w" option and a "-x c++",
352 // if no other "-x" option is present.
353 static ArgumentsAdjuster
354 getModularizeArgumentsAdjuster(DependencyMap &Dependencies) {
355 return [&Dependencies](const CommandLineArguments &Args,
356 StringRef /*unused*/) {
357 std::string InputFile = findInputFile(Args);
358 DependentsVector &FileDependents = Dependencies[InputFile];
359 CommandLineArguments NewArgs(Args);
360 if (int Count = FileDependents.size()) {
361 for (int Index = 0; Index < Count; ++Index) {
362 NewArgs.push_back("-include");
363 std::string File(std::string("\"") + FileDependents[Index] +
364 std::string("\""));
365 NewArgs.push_back(FileDependents[Index]);
368 // Ignore warnings. (Insert after "clang_tool" at beginning.)
369 NewArgs.insert(NewArgs.begin() + 1, "-w");
370 // Since we are compiling .h files, assume C++ unless given a -x option.
371 if (!llvm::is_contained(NewArgs, "-x")) {
372 NewArgs.insert(NewArgs.begin() + 2, "-x");
373 NewArgs.insert(NewArgs.begin() + 3, "c++");
375 return NewArgs;
379 // FIXME: The Location class seems to be something that we might
380 // want to design to be applicable to a wider range of tools, and stick it
381 // somewhere into Tooling/ in mainline
382 struct Location {
383 OptionalFileEntryRef File;
384 unsigned Line, Column;
386 Location() : File(), Line(), Column() {}
388 Location(SourceManager &SM, SourceLocation Loc) : File(), Line(), Column() {
389 Loc = SM.getExpansionLoc(Loc);
390 if (Loc.isInvalid())
391 return;
393 std::pair<FileID, unsigned> Decomposed = SM.getDecomposedLoc(Loc);
394 File = SM.getFileEntryRefForID(Decomposed.first);
395 if (!File)
396 return;
398 Line = SM.getLineNumber(Decomposed.first, Decomposed.second);
399 Column = SM.getColumnNumber(Decomposed.first, Decomposed.second);
402 operator bool() const { return File != nullptr; }
404 friend bool operator==(const Location &X, const Location &Y) {
405 return X.File == Y.File && X.Line == Y.Line && X.Column == Y.Column;
408 friend bool operator!=(const Location &X, const Location &Y) {
409 return !(X == Y);
412 friend bool operator<(const Location &X, const Location &Y) {
413 if (X.File != Y.File)
414 return X.File < Y.File;
415 if (X.Line != Y.Line)
416 return X.Line < Y.Line;
417 return X.Column < Y.Column;
419 friend bool operator>(const Location &X, const Location &Y) { return Y < X; }
420 friend bool operator<=(const Location &X, const Location &Y) {
421 return !(Y < X);
423 friend bool operator>=(const Location &X, const Location &Y) {
424 return !(X < Y);
428 struct Entry {
429 enum EntryKind {
430 EK_Tag,
431 EK_Value,
432 EK_Macro,
434 EK_NumberOfKinds
435 } Kind;
437 Location Loc;
439 StringRef getKindName() { return getKindName(Kind); }
440 static StringRef getKindName(EntryKind kind);
443 // Return a string representing the given kind.
444 StringRef Entry::getKindName(Entry::EntryKind kind) {
445 switch (kind) {
446 case EK_Tag:
447 return "tag";
448 case EK_Value:
449 return "value";
450 case EK_Macro:
451 return "macro";
452 case EK_NumberOfKinds:
453 break;
455 llvm_unreachable("invalid Entry kind");
458 struct HeaderEntry {
459 std::string Name;
460 Location Loc;
462 friend bool operator==(const HeaderEntry &X, const HeaderEntry &Y) {
463 return X.Loc == Y.Loc && X.Name == Y.Name;
465 friend bool operator!=(const HeaderEntry &X, const HeaderEntry &Y) {
466 return !(X == Y);
468 friend bool operator<(const HeaderEntry &X, const HeaderEntry &Y) {
469 return X.Loc < Y.Loc || (X.Loc == Y.Loc && X.Name < Y.Name);
471 friend bool operator>(const HeaderEntry &X, const HeaderEntry &Y) {
472 return Y < X;
474 friend bool operator<=(const HeaderEntry &X, const HeaderEntry &Y) {
475 return !(Y < X);
477 friend bool operator>=(const HeaderEntry &X, const HeaderEntry &Y) {
478 return !(X < Y);
482 typedef std::vector<HeaderEntry> HeaderContents;
484 class EntityMap : public std::map<std::string, SmallVector<Entry, 2>> {
485 public:
486 DenseMap<FileEntryRef, HeaderContents> HeaderContentMismatches;
488 void add(const std::string &Name, enum Entry::EntryKind Kind, Location Loc) {
489 // Record this entity in its header.
490 HeaderEntry HE = { Name, Loc };
491 CurHeaderContents[*Loc.File].push_back(HE);
493 // Check whether we've seen this entry before.
494 SmallVector<Entry, 2> &Entries = (*this)[Name];
495 for (unsigned I = 0, N = Entries.size(); I != N; ++I) {
496 if (Entries[I].Kind == Kind && Entries[I].Loc == Loc)
497 return;
500 // We have not seen this entry before; record it.
501 Entry E = { Kind, Loc };
502 Entries.push_back(E);
505 void mergeCurHeaderContents() {
506 for (auto H = CurHeaderContents.begin(), HEnd = CurHeaderContents.end();
507 H != HEnd; ++H) {
508 // Sort contents.
509 llvm::sort(H->second);
511 // Check whether we've seen this header before.
512 auto KnownH = AllHeaderContents.find(H->first);
513 if (KnownH == AllHeaderContents.end()) {
514 // We haven't seen this header before; record its contents.
515 AllHeaderContents.insert(*H);
516 continue;
519 // If the header contents are the same, we're done.
520 if (H->second == KnownH->second)
521 continue;
523 // Determine what changed.
524 std::set_symmetric_difference(
525 H->second.begin(), H->second.end(), KnownH->second.begin(),
526 KnownH->second.end(),
527 std::back_inserter(HeaderContentMismatches[H->first]));
530 CurHeaderContents.clear();
533 private:
534 DenseMap<FileEntryRef, HeaderContents> CurHeaderContents;
535 DenseMap<FileEntryRef, HeaderContents> AllHeaderContents;
538 class CollectEntitiesVisitor
539 : public RecursiveASTVisitor<CollectEntitiesVisitor> {
540 public:
541 CollectEntitiesVisitor(SourceManager &SM, EntityMap &Entities,
542 Preprocessor &PP, PreprocessorTracker &PPTracker,
543 int &HadErrors)
544 : SM(SM), Entities(Entities), PP(PP), PPTracker(PPTracker),
545 HadErrors(HadErrors) {}
547 bool TraverseStmt(Stmt *S) { return true; }
548 bool TraverseType(QualType T) { return true; }
549 bool TraverseTypeLoc(TypeLoc TL) { return true; }
550 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) { return true; }
551 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
552 return true;
554 bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo) {
555 return true;
557 bool TraverseTemplateName(TemplateName Template) { return true; }
558 bool TraverseTemplateArgument(const TemplateArgument &Arg) { return true; }
559 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) {
560 return true;
562 bool TraverseTemplateArguments(ArrayRef<TemplateArgument>) { return true; }
563 bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { return true; }
564 bool TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C,
565 Expr *Init) {
566 return true;
569 // Check 'extern "*" {}' block for #include directives.
570 bool VisitLinkageSpecDecl(LinkageSpecDecl *D) {
571 // Bail if not a block.
572 if (!D->hasBraces())
573 return true;
574 SourceRange BlockRange = D->getSourceRange();
575 const char *LinkageLabel;
576 switch (D->getLanguage()) {
577 case LinkageSpecLanguageIDs::C:
578 LinkageLabel = "extern \"C\" {}";
579 break;
580 case LinkageSpecLanguageIDs::CXX:
581 LinkageLabel = "extern \"C++\" {}";
582 break;
584 if (!PPTracker.checkForIncludesInBlock(PP, BlockRange, LinkageLabel,
585 errs()))
586 HadErrors = 1;
587 return true;
590 // Check 'namespace (name) {}' block for #include directives.
591 bool VisitNamespaceDecl(const NamespaceDecl *D) {
592 SourceRange BlockRange = D->getSourceRange();
593 std::string Label("namespace ");
594 Label += D->getName();
595 Label += " {}";
596 if (!PPTracker.checkForIncludesInBlock(PP, BlockRange, Label.c_str(),
597 errs()))
598 HadErrors = 1;
599 return true;
602 // Collect definition entities.
603 bool VisitNamedDecl(NamedDecl *ND) {
604 // We only care about file-context variables.
605 if (!ND->getDeclContext()->isFileContext())
606 return true;
608 // Skip declarations that tend to be properly multiply-declared.
609 if (isa<NamespaceDecl>(ND) || isa<UsingDirectiveDecl>(ND) ||
610 isa<NamespaceAliasDecl>(ND) ||
611 isa<ClassTemplateSpecializationDecl>(ND) || isa<UsingDecl>(ND) ||
612 isa<ClassTemplateDecl>(ND) || isa<TemplateTypeParmDecl>(ND) ||
613 isa<TypeAliasTemplateDecl>(ND) || isa<UsingShadowDecl>(ND) ||
614 isa<FunctionDecl>(ND) || isa<FunctionTemplateDecl>(ND) ||
615 (isa<TagDecl>(ND) &&
616 !cast<TagDecl>(ND)->isThisDeclarationADefinition()))
617 return true;
619 // Skip anonymous declarations.
620 if (!ND->getDeclName())
621 return true;
623 // Get the qualified name.
624 std::string Name;
625 llvm::raw_string_ostream OS(Name);
626 ND->printQualifiedName(OS);
627 OS.flush();
628 if (Name.empty())
629 return true;
631 Location Loc(SM, ND->getLocation());
632 if (!Loc)
633 return true;
635 Entities.add(Name, isa<TagDecl>(ND) ? Entry::EK_Tag : Entry::EK_Value, Loc);
636 return true;
639 private:
640 SourceManager &SM;
641 EntityMap &Entities;
642 Preprocessor &PP;
643 PreprocessorTracker &PPTracker;
644 int &HadErrors;
647 class CollectEntitiesConsumer : public ASTConsumer {
648 public:
649 CollectEntitiesConsumer(EntityMap &Entities,
650 PreprocessorTracker &preprocessorTracker,
651 Preprocessor &PP, StringRef InFile, int &HadErrors)
652 : Entities(Entities), PPTracker(preprocessorTracker), PP(PP),
653 HadErrors(HadErrors) {
654 PPTracker.handlePreprocessorEntry(PP, InFile);
657 ~CollectEntitiesConsumer() override { PPTracker.handlePreprocessorExit(); }
659 void HandleTranslationUnit(ASTContext &Ctx) override {
660 SourceManager &SM = Ctx.getSourceManager();
662 // Collect declared entities.
663 CollectEntitiesVisitor(SM, Entities, PP, PPTracker, HadErrors)
664 .TraverseDecl(Ctx.getTranslationUnitDecl());
666 // Collect macro definitions.
667 for (Preprocessor::macro_iterator M = PP.macro_begin(),
668 MEnd = PP.macro_end();
669 M != MEnd; ++M) {
670 Location Loc(SM, M->second.getLatest()->getLocation());
671 if (!Loc)
672 continue;
674 Entities.add(M->first->getName().str(), Entry::EK_Macro, Loc);
677 // Merge header contents.
678 Entities.mergeCurHeaderContents();
681 private:
682 EntityMap &Entities;
683 PreprocessorTracker &PPTracker;
684 Preprocessor &PP;
685 int &HadErrors;
688 class CollectEntitiesAction : public SyntaxOnlyAction {
689 public:
690 CollectEntitiesAction(EntityMap &Entities,
691 PreprocessorTracker &preprocessorTracker,
692 int &HadErrors)
693 : Entities(Entities), PPTracker(preprocessorTracker),
694 HadErrors(HadErrors) {}
696 protected:
697 std::unique_ptr<clang::ASTConsumer>
698 CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override {
699 return std::make_unique<CollectEntitiesConsumer>(
700 Entities, PPTracker, CI.getPreprocessor(), InFile, HadErrors);
703 private:
704 EntityMap &Entities;
705 PreprocessorTracker &PPTracker;
706 int &HadErrors;
709 class ModularizeFrontendActionFactory : public FrontendActionFactory {
710 public:
711 ModularizeFrontendActionFactory(EntityMap &Entities,
712 PreprocessorTracker &preprocessorTracker,
713 int &HadErrors)
714 : Entities(Entities), PPTracker(preprocessorTracker),
715 HadErrors(HadErrors) {}
717 std::unique_ptr<FrontendAction> create() override {
718 return std::make_unique<CollectEntitiesAction>(Entities, PPTracker,
719 HadErrors);
722 private:
723 EntityMap &Entities;
724 PreprocessorTracker &PPTracker;
725 int &HadErrors;
728 class CompileCheckVisitor
729 : public RecursiveASTVisitor<CompileCheckVisitor> {
730 public:
731 CompileCheckVisitor() {}
733 bool TraverseStmt(Stmt *S) { return true; }
734 bool TraverseType(QualType T) { return true; }
735 bool TraverseTypeLoc(TypeLoc TL) { return true; }
736 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) { return true; }
737 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
738 return true;
740 bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo) {
741 return true;
743 bool TraverseTemplateName(TemplateName Template) { return true; }
744 bool TraverseTemplateArgument(const TemplateArgument &Arg) { return true; }
745 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) {
746 return true;
748 bool TraverseTemplateArguments(ArrayRef<TemplateArgument>) { return true; }
749 bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { return true; }
750 bool TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C,
751 Expr *Init) {
752 return true;
755 // Check 'extern "*" {}' block for #include directives.
756 bool VisitLinkageSpecDecl(LinkageSpecDecl *D) {
757 return true;
760 // Check 'namespace (name) {}' block for #include directives.
761 bool VisitNamespaceDecl(const NamespaceDecl *D) {
762 return true;
765 // Collect definition entities.
766 bool VisitNamedDecl(NamedDecl *ND) {
767 return true;
771 class CompileCheckConsumer : public ASTConsumer {
772 public:
773 CompileCheckConsumer() {}
775 void HandleTranslationUnit(ASTContext &Ctx) override {
776 CompileCheckVisitor().TraverseDecl(Ctx.getTranslationUnitDecl());
780 class CompileCheckAction : public SyntaxOnlyAction {
781 public:
782 CompileCheckAction() {}
784 protected:
785 std::unique_ptr<clang::ASTConsumer>
786 CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override {
787 return std::make_unique<CompileCheckConsumer>();
791 class CompileCheckFrontendActionFactory : public FrontendActionFactory {
792 public:
793 CompileCheckFrontendActionFactory() {}
795 std::unique_ptr<FrontendAction> create() override {
796 return std::make_unique<CompileCheckAction>();
800 int main(int Argc, const char **Argv) {
802 // Save program name for error messages.
803 Argv0 = Argv[0];
805 // Save program arguments for use in module.modulemap comment.
806 CommandLine = std::string(sys::path::stem(sys::path::filename(Argv0)));
807 for (int ArgIndex = 1; ArgIndex < Argc; ArgIndex++) {
808 CommandLine.append(" ");
809 CommandLine.append(Argv[ArgIndex]);
812 // This causes options to be parsed.
813 cl::ParseCommandLineOptions(Argc, Argv, "modularize.\n");
815 // No go if we have no header list file.
816 if (ListFileNames.size() == 0) {
817 cl::PrintHelpMessage();
818 return 1;
821 std::unique_ptr<ModularizeUtilities> ModUtil;
822 int HadErrors = 0;
824 ModUtil.reset(
825 ModularizeUtilities::createModularizeUtilities(
826 ListFileNames, HeaderPrefix, ProblemFilesList));
828 // Get header file names and dependencies.
829 if (ModUtil->loadAllHeaderListsAndDependencies())
830 HadErrors = 1;
832 // If we are in assistant mode, output the module map and quit.
833 if (ModuleMapPath.length() != 0) {
834 if (!createModuleMap(ModuleMapPath, ModUtil->HeaderFileNames,
835 ModUtil->ProblemFileNames,
836 ModUtil->Dependencies, HeaderPrefix, RootModule))
837 return 1; // Failed.
838 return 0; // Success - Skip checks in assistant mode.
841 // If we're doing module maps.
842 if (!NoCoverageCheck && ModUtil->HasModuleMap) {
843 // Do coverage check.
844 if (ModUtil->doCoverageCheck(IncludePaths, CommandLine))
845 HadErrors = 1;
848 // Bail early if only doing the coverage check.
849 if (CoverageCheckOnly)
850 return HadErrors;
852 // Create the compilation database.
853 SmallString<256> PathBuf;
854 sys::fs::current_path(PathBuf);
855 std::unique_ptr<CompilationDatabase> Compilations;
856 Compilations.reset(
857 new FixedCompilationDatabase(Twine(PathBuf), CC1Arguments));
859 // Create preprocessor tracker, to watch for macro and conditional problems.
860 std::unique_ptr<PreprocessorTracker> PPTracker(
861 PreprocessorTracker::create(ModUtil->HeaderFileNames,
862 BlockCheckHeaderListOnly));
864 // Coolect entities here.
865 EntityMap Entities;
867 // Because we can't easily determine which files failed
868 // during the tool run, if we're collecting the file lists
869 // for display, we do a first compile pass on individual
870 // files to find which ones don't compile stand-alone.
871 if (DisplayFileLists) {
872 // First, make a pass to just get compile errors.
873 for (auto &CompileCheckFile : ModUtil->HeaderFileNames) {
874 llvm::SmallVector<std::string, 32> CompileCheckFileArray;
875 CompileCheckFileArray.push_back(CompileCheckFile);
876 ClangTool CompileCheckTool(*Compilations, CompileCheckFileArray);
877 CompileCheckTool.appendArgumentsAdjuster(
878 getModularizeArgumentsAdjuster(ModUtil->Dependencies));
879 int CompileCheckFileErrors = 0;
880 // FIXME: use newFrontendActionFactory.
881 CompileCheckFrontendActionFactory CompileCheckFactory;
882 CompileCheckFileErrors |= CompileCheckTool.run(&CompileCheckFactory);
883 if (CompileCheckFileErrors != 0) {
884 ModUtil->addUniqueProblemFile(CompileCheckFile); // Save problem file.
885 HadErrors |= 1;
887 else
888 ModUtil->addNoCompileErrorsFile(CompileCheckFile); // Save good file.
892 // Then we make another pass on the good files to do the rest of the work.
893 ClangTool Tool(*Compilations,
894 (DisplayFileLists ? ModUtil->GoodFileNames : ModUtil->HeaderFileNames));
895 Tool.appendArgumentsAdjuster(
896 getModularizeArgumentsAdjuster(ModUtil->Dependencies));
897 ModularizeFrontendActionFactory Factory(Entities, *PPTracker, HadErrors);
898 HadErrors |= Tool.run(&Factory);
900 // Create a place to save duplicate entity locations, separate bins per kind.
901 typedef SmallVector<Location, 8> LocationArray;
902 typedef SmallVector<LocationArray, Entry::EK_NumberOfKinds> EntryBinArray;
903 EntryBinArray EntryBins;
904 int KindIndex;
905 for (KindIndex = 0; KindIndex < Entry::EK_NumberOfKinds; ++KindIndex) {
906 LocationArray Array;
907 EntryBins.push_back(Array);
910 // Check for the same entity being defined in multiple places.
911 for (EntityMap::iterator E = Entities.begin(), EEnd = Entities.end();
912 E != EEnd; ++E) {
913 // If only one occurrence, exit early.
914 if (E->second.size() == 1)
915 continue;
916 // Clear entity locations.
917 for (EntryBinArray::iterator CI = EntryBins.begin(), CE = EntryBins.end();
918 CI != CE; ++CI) {
919 CI->clear();
921 // Walk the entities of a single name, collecting the locations,
922 // separated into separate bins.
923 for (unsigned I = 0, N = E->second.size(); I != N; ++I) {
924 EntryBins[E->second[I].Kind].push_back(E->second[I].Loc);
926 // Report any duplicate entity definition errors.
927 int KindIndex = 0;
928 for (EntryBinArray::iterator DI = EntryBins.begin(), DE = EntryBins.end();
929 DI != DE; ++DI, ++KindIndex) {
930 int ECount = DI->size();
931 // If only 1 occurrence of this entity, skip it, we only report duplicates.
932 if (ECount <= 1)
933 continue;
934 LocationArray::iterator FI = DI->begin();
935 StringRef kindName = Entry::getKindName((Entry::EntryKind)KindIndex);
936 errs() << "error: " << kindName << " '" << E->first
937 << "' defined at multiple locations:\n";
938 for (LocationArray::iterator FE = DI->end(); FI != FE; ++FI) {
939 errs() << " " << FI->File->getName() << ":" << FI->Line << ":"
940 << FI->Column << "\n";
941 ModUtil->addUniqueProblemFile(std::string(FI->File->getName()));
943 HadErrors = 1;
947 // Complain about macro instance in header files that differ based on how
948 // they are included.
949 if (PPTracker->reportInconsistentMacros(errs()))
950 HadErrors = 1;
952 // Complain about preprocessor conditional directives in header files that
953 // differ based on how they are included.
954 if (PPTracker->reportInconsistentConditionals(errs()))
955 HadErrors = 1;
957 // Complain about any headers that have contents that differ based on how
958 // they are included.
959 // FIXME: Could we provide information about which preprocessor conditionals
960 // are involved?
961 for (auto H = Entities.HeaderContentMismatches.begin(),
962 HEnd = Entities.HeaderContentMismatches.end();
963 H != HEnd; ++H) {
964 if (H->second.empty()) {
965 errs() << "internal error: phantom header content mismatch\n";
966 continue;
969 HadErrors = 1;
970 ModUtil->addUniqueProblemFile(std::string(H->first.getName()));
971 errs() << "error: header '" << H->first.getName()
972 << "' has different contents depending on how it was included.\n";
973 for (unsigned I = 0, N = H->second.size(); I != N; ++I) {
974 errs() << "note: '" << H->second[I].Name << "' in "
975 << H->second[I].Loc.File->getName() << " at "
976 << H->second[I].Loc.Line << ":" << H->second[I].Loc.Column
977 << " not always provided\n";
981 if (DisplayFileLists) {
982 ModUtil->displayProblemFiles();
983 ModUtil->displayGoodFiles();
984 ModUtil->displayCombinedFiles();
987 return HadErrors;