1 //===- extra/modularize/Modularize.cpp - Check modularized headers --------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements a tool that checks whether a set of headers provides
11 // the consistent definitions required to use modules. For example, it detects
12 // whether the same entity (say, a NULL macro or size_t typedef) is defined in
13 // multiple headers or whether a header produces different definitions under
14 // different circumstances. These conditions cause modules built from the
15 // headers to behave poorly, and should be fixed before introducing a module
16 // map.
17 //
18 // Modularize takes as argument a file name for a file containing the
19 // newline-separated list of headers to check with respect to each other.
20 // Lines beginning with '#' and empty lines are ignored.
21 // Header file names followed by a colon and other space-separated
22 // file names will include those extra files as dependencies.
23 // The file names can be relative or full paths, but must be on the
24 // same line.
25 //
26 // Modularize also accepts regular front-end arguments.
27 //
28 // Usage:   modularize [-prefix (optional header path prefix)]
29 //   (include-files_list) [(front-end-options) ...]
30 //
31 // Note that unless a "-prefix (header path)" option is specified,
32 // non-absolute file paths in the header list file will be relative
33 // to the header list file directory.  Use -prefix to specify a different
34 // directory.
35 //
36 // Note that by default, the underlying Clang front end assumes .h files
37 // contain C source.  If your .h files in the file list contain C++ source,
38 // you should append the following to your command lines: -x c++
39 //
40 // Modularize will do normal parsing, reporting normal errors and warnings,
41 // but will also report special error messages like the following:
42 //
43 //   error: '(symbol)' defined at multiple locations:
44 //       (file):(row):(column)
45 //       (file):(row):(column)
46 //
47 //   error: header '(file)' has different contents depending on how it was
48 //     included
49 //
50 // The latter might be followed by messages like the following:
51 //
52 //   note: '(symbol)' in (file) at (row):(column) not always provided
53 //
54 // Checks will also be performed for macro expansions, defined(macro)
55 // expressions, and preprocessor conditional directives that evaluate
56 // inconsistently, and can produce error messages like the following:
57 //
58 //   (...)/SubHeader.h:11:5:
59 //   #if SYMBOL == 1
60 //       ^
61 //   error: Macro instance 'SYMBOL' has different values in this header,
62 //          depending on how it was included.
63 //     'SYMBOL' expanded to: '1' with respect to these inclusion paths:
64 //       (...)/Header1.h
65 //         (...)/SubHeader.h
66 //   (...)/SubHeader.h:3:9:
67 //   #define SYMBOL 1
68 //             ^
69 //   Macro defined here.
70 //     'SYMBOL' expanded to: '2' with respect to these inclusion paths:
71 //       (...)/Header2.h
72 //           (...)/SubHeader.h
73 //   (...)/SubHeader.h:7:9:
74 //   #define SYMBOL 2
75 //             ^
76 //   Macro defined here.
77 //
78 // Checks will also be performed for '#include' directives that are
79 // nested inside 'extern "C/C++" {}' or 'namespace (name) {}' blocks,
80 // and can produce error message like the following:
81 //
82 // IncludeInExtern.h:2:3
83 //   #include "Empty.h"
84 //   ^
85 // error: Include directive within extern "C" {}.
86 // IncludeInExtern.h:1:1
87 // extern "C" {
88 // ^
89 // The "extern "C" {}" block is here.
90 //
91 // See PreprocessorTracker.cpp for additional details.
92 //
93 // Modularize also has an option ("-module-map-path=module.map") that will
94 // skip the checks, and instead act as a module.map generation assistant,
95 // generating a module map file based on the header list.  An optional
96 // "-root-module=(rootName)" argument can specify a root module to be
97 // created in the generated module.map file.  Note that you will likely
98 // need to edit this file to suit the needs of your headers.
99 //
100 // An example command line for generating a module.map file:
101 //
102 //   modularize -module-map-path=module.map -root-module=myroot headerlist.txt
103 //
104 // Note that if the headers in the header list have partial paths, sub-modules
105 // will be created for the subdirectires involved, assuming that the
106 // subdirectories contain headers to be grouped into a module, but still with
107 // individual modules for the headers in the subdirectory.
108 //
109 // See the ModuleAssistant.cpp file comments for additional details about the
110 // implementation of the assistant mode.
111 //
112 // Future directions:
113 //
114 // Basically, we want to add new checks for whatever we can check with respect
115 // to checking headers for module'ability.
116 //
117 // Some ideas:
118 //
119 // 1. Omit duplicate "not always provided" messages
120 //
121 // 2. Add options to disable any of the checks, in case
122 // there is some problem with them, or the messages get too verbose.
123 //
124 // 3. Try to figure out the preprocessor conditional directives that
125 // contribute to problems and tie them to the inconsistent definitions.
126 //
127 // 4. There are some legitimate uses of preprocessor macros that
128 // modularize will flag as errors, such as repeatedly #include'ing
129 // a file and using interleaving defined/undefined macros
130 // to change declarations in the included file.  Is there a way
131 // to address this?  Maybe have modularize accept a list of macros
132 // to ignore.  Otherwise you can just exclude the file, after checking
133 // for legitimate errors.
134 //
135 // 5. What else?
136 //
137 // General clean-up and refactoring:
138 //
139 // 1. The Location class seems to be something that we might
140 // want to design to be applicable to a wider range of tools, and stick it
141 // somewhere into Tooling/ in mainline
142 //
143 //===----------------------------------------------------------------------===//
144 
145 #include "Modularize.h"
146 #include "PreprocessorTracker.h"
147 #include "clang/AST/ASTConsumer.h"
148 #include "clang/AST/ASTContext.h"
149 #include "clang/AST/RecursiveASTVisitor.h"
150 #include "clang/Basic/SourceManager.h"
151 #include "clang/Driver/Options.h"
152 #include "clang/Frontend/CompilerInstance.h"
153 #include "clang/Frontend/FrontendActions.h"
154 #include "clang/Lex/Preprocessor.h"
155 #include "clang/Tooling/CompilationDatabase.h"
156 #include "clang/Tooling/Tooling.h"
157 #include "llvm/Option/Arg.h"
158 #include "llvm/Option/ArgList.h"
159 #include "llvm/Option/OptTable.h"
160 #include "llvm/Option/Option.h"
161 #include "llvm/Support/CommandLine.h"
162 #include "llvm/Support/FileSystem.h"
163 #include "llvm/Support/MemoryBuffer.h"
164 #include "llvm/Support/Path.h"
165 #include <algorithm>
166 #include <fstream>
167 #include <iterator>
168 #include <string>
169 #include <vector>
170 
171 using namespace clang;
172 using namespace clang::driver;
173 using namespace clang::driver::options;
174 using namespace clang::tooling;
175 using namespace llvm;
176 using namespace llvm::opt;
177 using namespace Modularize;
178 
179 // Option to specify a file name for a list of header files to check.
180 cl::opt<std::string>
181 ListFileName(cl::Positional,
182              cl::desc("<name of file containing list of headers to check>"));
183 
184 // Collect all other arguments, which will be passed to the front end.
185 cl::list<std::string>
186 CC1Arguments(cl::ConsumeAfter,
187              cl::desc("<arguments to be passed to front end>..."));
188 
189 // Option to specify a prefix to be prepended to the header names.
190 cl::opt<std::string> HeaderPrefix(
191     "prefix", cl::init(""),
192     cl::desc(
193         "Prepend header file paths with this prefix."
194         " If not specified,"
195         " the files are considered to be relative to the header list file."));
196 
197 // Option for assistant mode, telling modularize to output a module map
198 // based on the headers list, and where to put it.
199 cl::opt<std::string> ModuleMapPath(
200     "module-map-path", cl::init(""),
201     cl::desc("Turn on module map output and specify output path or file name."
202              " If no path is specified and if prefix option is specified,"
203              " use prefix for file path."));
204 
205 // Option for assistant mode, telling modularize to output a module map
206 // based on the headers list, and where to put it.
207 cl::opt<std::string>
208 RootModule("root-module", cl::init(""),
209            cl::desc("Specify the name of the root module."));
210 
211 // Save the program name for error messages.
212 const char *Argv0;
213 // Save the command line for comments.
214 std::string CommandLine;
215 
216 // Read the header list file and collect the header file names and
217 // optional dependencies.
218 std::error_code
219 getHeaderFileNames(SmallVectorImpl<std::string> &HeaderFileNames,
220                    DependencyMap &Dependencies, StringRef ListFileName,
221                    StringRef HeaderPrefix) {
222   // By default, use the path component of the list file name.
223   SmallString<256> HeaderDirectory(ListFileName);
224   sys::path::remove_filename(HeaderDirectory);
225   SmallString<256> CurrentDirectory;
226   sys::fs::current_path(CurrentDirectory);
227 
228   // Get the prefix if we have one.
229   if (HeaderPrefix.size() != 0)
230     HeaderDirectory = HeaderPrefix;
231 
232   // Read the header list file into a buffer.
233   ErrorOr<std::unique_ptr<MemoryBuffer>> listBuffer =
234       MemoryBuffer::getFile(ListFileName);
235   if (std::error_code EC = listBuffer.getError())
236     return EC;
237 
238   // Parse the header list into strings.
239   SmallVector<StringRef, 32> Strings;
240   listBuffer.get()->getBuffer().split(Strings, "\n", -1, false);
241 
242   // Collect the header file names from the string list.
243   for (SmallVectorImpl<StringRef>::iterator I = Strings.begin(),
244                                             E = Strings.end();
245        I != E; ++I) {
246     StringRef Line = I->trim();
247     // Ignore comments and empty lines.
248     if (Line.empty() || (Line[0] == '#'))
249       continue;
250     std::pair<StringRef, StringRef> TargetAndDependents = Line.split(':');
251     SmallString<256> HeaderFileName;
252     // Prepend header file name prefix if it's not absolute.
253     if (sys::path::is_absolute(TargetAndDependents.first))
254       llvm::sys::path::native(TargetAndDependents.first, HeaderFileName);
255     else {
256       if (HeaderDirectory.size() != 0)
257         HeaderFileName = HeaderDirectory;
258       else
259         HeaderFileName = CurrentDirectory;
260       sys::path::append(HeaderFileName, TargetAndDependents.first);
261       sys::path::native(HeaderFileName);
262     }
263     // Handle optional dependencies.
264     DependentsVector Dependents;
265     SmallVector<StringRef, 4> DependentsList;
266     TargetAndDependents.second.split(DependentsList, " ", -1, false);
267     int Count = DependentsList.size();
268     for (int Index = 0; Index < Count; ++Index) {
269       SmallString<256> Dependent;
270       if (sys::path::is_absolute(DependentsList[Index]))
271         Dependent = DependentsList[Index];
272       else {
273         if (HeaderDirectory.size() != 0)
274           Dependent = HeaderDirectory;
275         else
276           Dependent = CurrentDirectory;
277         sys::path::append(Dependent, DependentsList[Index]);
278       }
279       sys::path::native(Dependent);
280       Dependents.push_back(Dependent.str());
281     }
282     // Save the resulting header file path and dependencies.
283     HeaderFileNames.push_back(HeaderFileName.str());
284     Dependencies[HeaderFileName.str()] = Dependents;
285   }
286 
287   return std::error_code();
288 }
289 
290 // Helper function for finding the input file in an arguments list.
291 std::string findInputFile(const CommandLineArguments &CLArgs) {
292   std::unique_ptr<OptTable> Opts(createDriverOptTable());
293   const unsigned IncludedFlagsBitmask = options::CC1Option;
294   unsigned MissingArgIndex, MissingArgCount;
295   SmallVector<const char *, 256> Argv;
296   for (CommandLineArguments::const_iterator I = CLArgs.begin(),
297                                             E = CLArgs.end();
298        I != E; ++I)
299     Argv.push_back(I->c_str());
300   std::unique_ptr<InputArgList> Args(
301       Opts->ParseArgs(Argv.data(), Argv.data() + Argv.size(), MissingArgIndex,
302                       MissingArgCount, IncludedFlagsBitmask));
303   std::vector<std::string> Inputs = Args->getAllArgValues(OPT_INPUT);
304   return Inputs.back();
305 }
306 
307 // This arguments adjuster inserts "-include (file)" arguments for header
308 // dependencies.
309 ArgumentsAdjuster getAddDependenciesAdjuster(DependencyMap &Dependencies) {
310   return [&Dependencies](const CommandLineArguments &Args) {
311     std::string InputFile = findInputFile(Args);
312     DependentsVector &FileDependents = Dependencies[InputFile];
313     CommandLineArguments NewArgs(Args);
314     if (int Count = FileDependents.size()) {
315       for (int Index = 0; Index < Count; ++Index) {
316         NewArgs.push_back("-include");
317         std::string File(std::string("\"") + FileDependents[Index] +
318                          std::string("\""));
319         NewArgs.push_back(FileDependents[Index]);
320       }
321     }
322     return NewArgs;
323   };
324 }
325 
326 // FIXME: The Location class seems to be something that we might
327 // want to design to be applicable to a wider range of tools, and stick it
328 // somewhere into Tooling/ in mainline
329 struct Location {
330   const FileEntry *File;
331   unsigned Line, Column;
332 
333   Location() : File(), Line(), Column() {}
334 
335   Location(SourceManager &SM, SourceLocation Loc) : File(), Line(), Column() {
336     Loc = SM.getExpansionLoc(Loc);
337     if (Loc.isInvalid())
338       return;
339 
340     std::pair<FileID, unsigned> Decomposed = SM.getDecomposedLoc(Loc);
341     File = SM.getFileEntryForID(Decomposed.first);
342     if (!File)
343       return;
344 
345     Line = SM.getLineNumber(Decomposed.first, Decomposed.second);
346     Column = SM.getColumnNumber(Decomposed.first, Decomposed.second);
347   }
348 
349   operator bool() const { return File != nullptr; }
350 
351   friend bool operator==(const Location &X, const Location &Y) {
352     return X.File == Y.File && X.Line == Y.Line && X.Column == Y.Column;
353   }
354 
355   friend bool operator!=(const Location &X, const Location &Y) {
356     return !(X == Y);
357   }
358 
359   friend bool operator<(const Location &X, const Location &Y) {
360     if (X.File != Y.File)
361       return X.File < Y.File;
362     if (X.Line != Y.Line)
363       return X.Line < Y.Line;
364     return X.Column < Y.Column;
365   }
366   friend bool operator>(const Location &X, const Location &Y) { return Y < X; }
367   friend bool operator<=(const Location &X, const Location &Y) {
368     return !(Y < X);
369   }
370   friend bool operator>=(const Location &X, const Location &Y) {
371     return !(X < Y);
372   }
373 };
374 
375 struct Entry {
376   enum EntryKind {
377     EK_Tag,
378     EK_Value,
379     EK_Macro,
380 
381     EK_NumberOfKinds
382   } Kind;
383 
384   Location Loc;
385 
386   StringRef getKindName() { return getKindName(Kind); }
387   static StringRef getKindName(EntryKind kind);
388 };
389 
390 // Return a string representing the given kind.
391 StringRef Entry::getKindName(Entry::EntryKind kind) {
392   switch (kind) {
393   case EK_Tag:
394     return "tag";
395   case EK_Value:
396     return "value";
397   case EK_Macro:
398     return "macro";
399   case EK_NumberOfKinds:
400     break;
401   }
402   llvm_unreachable("invalid Entry kind");
403 }
404 
405 struct HeaderEntry {
406   std::string Name;
407   Location Loc;
408 
409   friend bool operator==(const HeaderEntry &X, const HeaderEntry &Y) {
410     return X.Loc == Y.Loc && X.Name == Y.Name;
411   }
412   friend bool operator!=(const HeaderEntry &X, const HeaderEntry &Y) {
413     return !(X == Y);
414   }
415   friend bool operator<(const HeaderEntry &X, const HeaderEntry &Y) {
416     return X.Loc < Y.Loc || (X.Loc == Y.Loc && X.Name < Y.Name);
417   }
418   friend bool operator>(const HeaderEntry &X, const HeaderEntry &Y) {
419     return Y < X;
420   }
421   friend bool operator<=(const HeaderEntry &X, const HeaderEntry &Y) {
422     return !(Y < X);
423   }
424   friend bool operator>=(const HeaderEntry &X, const HeaderEntry &Y) {
425     return !(X < Y);
426   }
427 };
428 
429 typedef std::vector<HeaderEntry> HeaderContents;
430 
431 class EntityMap : public StringMap<SmallVector<Entry, 2> > {
432 public:
433   DenseMap<const FileEntry *, HeaderContents> HeaderContentMismatches;
434 
435   void add(const std::string &Name, enum Entry::EntryKind Kind, Location Loc) {
436     // Record this entity in its header.
437     HeaderEntry HE = { Name, Loc };
438     CurHeaderContents[Loc.File].push_back(HE);
439 
440     // Check whether we've seen this entry before.
441     SmallVector<Entry, 2> &Entries = (*this)[Name];
442     for (unsigned I = 0, N = Entries.size(); I != N; ++I) {
443       if (Entries[I].Kind == Kind && Entries[I].Loc == Loc)
444         return;
445     }
446 
447     // We have not seen this entry before; record it.
448     Entry E = { Kind, Loc };
449     Entries.push_back(E);
450   }
451 
452   void mergeCurHeaderContents() {
453     for (DenseMap<const FileEntry *, HeaderContents>::iterator
454              H = CurHeaderContents.begin(),
455              HEnd = CurHeaderContents.end();
456          H != HEnd; ++H) {
457       // Sort contents.
458       std::sort(H->second.begin(), H->second.end());
459 
460       // Check whether we've seen this header before.
461       DenseMap<const FileEntry *, HeaderContents>::iterator KnownH =
462           AllHeaderContents.find(H->first);
463       if (KnownH == AllHeaderContents.end()) {
464         // We haven't seen this header before; record its contents.
465         AllHeaderContents.insert(*H);
466         continue;
467       }
468 
469       // If the header contents are the same, we're done.
470       if (H->second == KnownH->second)
471         continue;
472 
473       // Determine what changed.
474       std::set_symmetric_difference(
475           H->second.begin(), H->second.end(), KnownH->second.begin(),
476           KnownH->second.end(),
477           std::back_inserter(HeaderContentMismatches[H->first]));
478     }
479 
480     CurHeaderContents.clear();
481   }
482 
483 private:
484   DenseMap<const FileEntry *, HeaderContents> CurHeaderContents;
485   DenseMap<const FileEntry *, HeaderContents> AllHeaderContents;
486 };
487 
488 class CollectEntitiesVisitor
489     : public RecursiveASTVisitor<CollectEntitiesVisitor> {
490 public:
491   CollectEntitiesVisitor(SourceManager &SM, EntityMap &Entities,
492                          Preprocessor &PP, PreprocessorTracker &PPTracker,
493                          int &HadErrors)
494       : SM(SM), Entities(Entities), PP(PP), PPTracker(PPTracker),
495         HadErrors(HadErrors) {}
496 
497   bool TraverseStmt(Stmt *S) { return true; }
498   bool TraverseType(QualType T) { return true; }
499   bool TraverseTypeLoc(TypeLoc TL) { return true; }
500   bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) { return true; }
501   bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
502     return true;
503   }
504   bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo) {
505     return true;
506   }
507   bool TraverseTemplateName(TemplateName Template) { return true; }
508   bool TraverseTemplateArgument(const TemplateArgument &Arg) { return true; }
509   bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) {
510     return true;
511   }
512   bool TraverseTemplateArguments(const TemplateArgument *Args,
513                                  unsigned NumArgs) {
514     return true;
515   }
516   bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { return true; }
517   bool TraverseLambdaCapture(LambdaCapture C) { return true; }
518 
519   // Check 'extern "*" {}' block for #include directives.
520   bool VisitLinkageSpecDecl(LinkageSpecDecl *D) {
521     // Bail if not a block.
522     if (!D->hasBraces())
523       return true;
524     SourceRange BlockRange = D->getSourceRange();
525     const char *LinkageLabel;
526     switch (D->getLanguage()) {
527     case LinkageSpecDecl::lang_c:
528       LinkageLabel = "extern \"C\" {}";
529       break;
530     case LinkageSpecDecl::lang_cxx:
531       LinkageLabel = "extern \"C++\" {}";
532       break;
533     }
534     if (!PPTracker.checkForIncludesInBlock(PP, BlockRange, LinkageLabel,
535                                            errs()))
536       HadErrors = 1;
537     return true;
538   }
539 
540   // Check 'namespace (name) {}' block for #include directives.
541   bool VisitNamespaceDecl(const NamespaceDecl *D) {
542     SourceRange BlockRange = D->getSourceRange();
543     std::string Label("namespace ");
544     Label += D->getName();
545     Label += " {}";
546     if (!PPTracker.checkForIncludesInBlock(PP, BlockRange, Label.c_str(),
547                                            errs()))
548       HadErrors = 1;
549     return true;
550   }
551 
552   // Collect definition entities.
553   bool VisitNamedDecl(NamedDecl *ND) {
554     // We only care about file-context variables.
555     if (!ND->getDeclContext()->isFileContext())
556       return true;
557 
558     // Skip declarations that tend to be properly multiply-declared.
559     if (isa<NamespaceDecl>(ND) || isa<UsingDirectiveDecl>(ND) ||
560         isa<NamespaceAliasDecl>(ND) ||
561         isa<ClassTemplateSpecializationDecl>(ND) || isa<UsingDecl>(ND) ||
562         isa<ClassTemplateDecl>(ND) || isa<TemplateTypeParmDecl>(ND) ||
563         isa<TypeAliasTemplateDecl>(ND) || isa<UsingShadowDecl>(ND) ||
564         isa<FunctionDecl>(ND) || isa<FunctionTemplateDecl>(ND) ||
565         (isa<TagDecl>(ND) &&
566          !cast<TagDecl>(ND)->isThisDeclarationADefinition()))
567       return true;
568 
569     // Skip anonymous declarations.
570     if (!ND->getDeclName())
571       return true;
572 
573     // Get the qualified name.
574     std::string Name;
575     llvm::raw_string_ostream OS(Name);
576     ND->printQualifiedName(OS);
577     OS.flush();
578     if (Name.empty())
579       return true;
580 
581     Location Loc(SM, ND->getLocation());
582     if (!Loc)
583       return true;
584 
585     Entities.add(Name, isa<TagDecl>(ND) ? Entry::EK_Tag : Entry::EK_Value, Loc);
586     return true;
587   }
588 
589 private:
590   SourceManager &SM;
591   EntityMap &Entities;
592   Preprocessor &PP;
593   PreprocessorTracker &PPTracker;
594   int &HadErrors;
595 };
596 
597 class CollectEntitiesConsumer : public ASTConsumer {
598 public:
599   CollectEntitiesConsumer(EntityMap &Entities,
600                           PreprocessorTracker &preprocessorTracker,
601                           Preprocessor &PP, StringRef InFile, int &HadErrors)
602       : Entities(Entities), PPTracker(preprocessorTracker), PP(PP),
603         HadErrors(HadErrors) {
604     PPTracker.handlePreprocessorEntry(PP, InFile);
605   }
606 
607   ~CollectEntitiesConsumer() { PPTracker.handlePreprocessorExit(); }
608 
609   virtual void HandleTranslationUnit(ASTContext &Ctx) {
610     SourceManager &SM = Ctx.getSourceManager();
611 
612     // Collect declared entities.
613     CollectEntitiesVisitor(SM, Entities, PP, PPTracker, HadErrors)
614         .TraverseDecl(Ctx.getTranslationUnitDecl());
615 
616     // Collect macro definitions.
617     for (Preprocessor::macro_iterator M = PP.macro_begin(),
618                                       MEnd = PP.macro_end();
619          M != MEnd; ++M) {
620       Location Loc(SM, M->second->getLocation());
621       if (!Loc)
622         continue;
623 
624       Entities.add(M->first->getName().str(), Entry::EK_Macro, Loc);
625     }
626 
627     // Merge header contents.
628     Entities.mergeCurHeaderContents();
629   }
630 
631 private:
632   EntityMap &Entities;
633   PreprocessorTracker &PPTracker;
634   Preprocessor &PP;
635   int &HadErrors;
636 };
637 
638 class CollectEntitiesAction : public SyntaxOnlyAction {
639 public:
640   CollectEntitiesAction(EntityMap &Entities,
641                         PreprocessorTracker &preprocessorTracker,
642                         int &HadErrors)
643       : Entities(Entities), PPTracker(preprocessorTracker),
644         HadErrors(HadErrors) {}
645 
646 protected:
647   std::unique_ptr<clang::ASTConsumer>
648   CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override {
649     return llvm::make_unique<CollectEntitiesConsumer>(
650         Entities, PPTracker, CI.getPreprocessor(), InFile, HadErrors);
651   }
652 
653 private:
654   EntityMap &Entities;
655   PreprocessorTracker &PPTracker;
656   int &HadErrors;
657 };
658 
659 class ModularizeFrontendActionFactory : public FrontendActionFactory {
660 public:
661   ModularizeFrontendActionFactory(EntityMap &Entities,
662                                   PreprocessorTracker &preprocessorTracker,
663                                   int &HadErrors)
664       : Entities(Entities), PPTracker(preprocessorTracker),
665         HadErrors(HadErrors) {}
666 
667   virtual CollectEntitiesAction *create() {
668     return new CollectEntitiesAction(Entities, PPTracker, HadErrors);
669   }
670 
671 private:
672   EntityMap &Entities;
673   PreprocessorTracker &PPTracker;
674   int &HadErrors;
675 };
676 
677 int main(int Argc, const char **Argv) {
678 
679   // Save program name for error messages.
680   Argv0 = Argv[0];
681 
682   // Save program arguments for use in module.map comment.
683   CommandLine = sys::path::stem(sys::path::filename(Argv0));
684   for (int ArgIndex = 1; ArgIndex < Argc; ArgIndex++) {
685     CommandLine.append(" ");
686     CommandLine.append(Argv[ArgIndex]);
687   }
688 
689   // This causes options to be parsed.
690   cl::ParseCommandLineOptions(Argc, Argv, "modularize.\n");
691 
692   // No go if we have no header list file.
693   if (ListFileName.size() == 0) {
694     cl::PrintHelpMessage();
695     return 1;
696   }
697 
698   // Get header file names and dependencies.
699   SmallVector<std::string, 32> Headers;
700   DependencyMap Dependencies;
701   if (std::error_code EC = getHeaderFileNames(Headers, Dependencies,
702                                               ListFileName, HeaderPrefix)) {
703     errs() << Argv[0] << ": error: Unable to get header list '" << ListFileName
704            << "': " << EC.message() << '\n';
705     return 1;
706   }
707 
708   // If we are in assistant mode, output the module map and quit.
709   if (ModuleMapPath.length() != 0) {
710     if (!createModuleMap(ModuleMapPath, Headers, Dependencies, HeaderPrefix,
711                          RootModule))
712       return 1; // Failed.
713     return 0;   // Success - Skip checks in assistant mode.
714   }
715 
716   // Create the compilation database.
717   SmallString<256> PathBuf;
718   sys::fs::current_path(PathBuf);
719   std::unique_ptr<CompilationDatabase> Compilations;
720   Compilations.reset(
721       new FixedCompilationDatabase(Twine(PathBuf), CC1Arguments));
722 
723   // Create preprocessor tracker, to watch for macro and conditional problems.
724   std::unique_ptr<PreprocessorTracker> PPTracker(PreprocessorTracker::create());
725 
726   // Parse all of the headers, detecting duplicates.
727   EntityMap Entities;
728   ClangTool Tool(*Compilations, Headers);
729   Tool.appendArgumentsAdjuster(getAddDependenciesAdjuster(Dependencies));
730   int HadErrors = 0;
731   ModularizeFrontendActionFactory Factory(Entities, *PPTracker, HadErrors);
732   HadErrors |= Tool.run(&Factory);
733 
734   // Create a place to save duplicate entity locations, separate bins per kind.
735   typedef SmallVector<Location, 8> LocationArray;
736   typedef SmallVector<LocationArray, Entry::EK_NumberOfKinds> EntryBinArray;
737   EntryBinArray EntryBins;
738   int KindIndex;
739   for (KindIndex = 0; KindIndex < Entry::EK_NumberOfKinds; ++KindIndex) {
740     LocationArray Array;
741     EntryBins.push_back(Array);
742   }
743 
744   // Check for the same entity being defined in multiple places.
745   for (EntityMap::iterator E = Entities.begin(), EEnd = Entities.end();
746        E != EEnd; ++E) {
747     // If only one occurrence, exit early.
748     if (E->second.size() == 1)
749       continue;
750     // Clear entity locations.
751     for (EntryBinArray::iterator CI = EntryBins.begin(), CE = EntryBins.end();
752          CI != CE; ++CI) {
753       CI->clear();
754     }
755     // Walk the entities of a single name, collecting the locations,
756     // separated into separate bins.
757     for (unsigned I = 0, N = E->second.size(); I != N; ++I) {
758       EntryBins[E->second[I].Kind].push_back(E->second[I].Loc);
759     }
760     // Report any duplicate entity definition errors.
761     int KindIndex = 0;
762     for (EntryBinArray::iterator DI = EntryBins.begin(), DE = EntryBins.end();
763          DI != DE; ++DI, ++KindIndex) {
764       int ECount = DI->size();
765       // If only 1 occurrence of this entity, skip it, as we only report duplicates.
766       if (ECount <= 1)
767         continue;
768       LocationArray::iterator FI = DI->begin();
769       StringRef kindName = Entry::getKindName((Entry::EntryKind)KindIndex);
770       errs() << "error: " << kindName << " '" << E->first()
771              << "' defined at multiple locations:\n";
772       for (LocationArray::iterator FE = DI->end(); FI != FE; ++FI) {
773         errs() << "    " << FI->File->getName() << ":" << FI->Line << ":"
774                << FI->Column << "\n";
775       }
776       HadErrors = 1;
777     }
778   }
779 
780   // Complain about macro instance in header files that differ based on how
781   // they are included.
782   if (PPTracker->reportInconsistentMacros(errs()))
783     HadErrors = 1;
784 
785   // Complain about preprocessor conditional directives in header files that
786   // differ based on how they are included.
787   if (PPTracker->reportInconsistentConditionals(errs()))
788     HadErrors = 1;
789 
790   // Complain about any headers that have contents that differ based on how
791   // they are included.
792   // FIXME: Could we provide information about which preprocessor conditionals
793   // are involved?
794   for (DenseMap<const FileEntry *, HeaderContents>::iterator
795            H = Entities.HeaderContentMismatches.begin(),
796            HEnd = Entities.HeaderContentMismatches.end();
797        H != HEnd; ++H) {
798     if (H->second.empty()) {
799       errs() << "internal error: phantom header content mismatch\n";
800       continue;
801     }
802 
803     HadErrors = 1;
804     errs() << "error: header '" << H->first->getName()
805            << "' has different contents depending on how it was included.\n";
806     for (unsigned I = 0, N = H->second.size(); I != N; ++I) {
807       errs() << "note: '" << H->second[I].Name << "' in "
808              << H->second[I].Loc.File->getName() << " at "
809              << H->second[I].Loc.Line << ":" << H->second[I].Loc.Column
810              << " not always provided\n";
811     }
812   }
813 
814   return HadErrors;
815 }
816