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 error_code getHeaderFileNames(SmallVectorImpl<std::string> &HeaderFileNames,
219                               DependencyMap &Dependencies,
220                               StringRef ListFileName, StringRef HeaderPrefix) {
221   // By default, use the path component of the list file name.
222   SmallString<256> HeaderDirectory(ListFileName);
223   sys::path::remove_filename(HeaderDirectory);
224   SmallString<256> CurrentDirectory;
225   sys::fs::current_path(CurrentDirectory);
226 
227   // Get the prefix if we have one.
228   if (HeaderPrefix.size() != 0)
229     HeaderDirectory = HeaderPrefix;
230 
231   // Read the header list file into a buffer.
232   std::unique_ptr<MemoryBuffer> listBuffer;
233   if (error_code ec = MemoryBuffer::getFile(ListFileName, listBuffer)) {
234     return ec;
235   }
236 
237   // Parse the header list into strings.
238   SmallVector<StringRef, 32> Strings;
239   listBuffer->getBuffer().split(Strings, "\n", -1, false);
240 
241   // Collect the header file names from the string list.
242   for (SmallVectorImpl<StringRef>::iterator I = Strings.begin(),
243                                             E = Strings.end();
244        I != E; ++I) {
245     StringRef Line = I->trim();
246     // Ignore comments and empty lines.
247     if (Line.empty() || (Line[0] == '#'))
248       continue;
249     std::pair<StringRef, StringRef> TargetAndDependents = Line.split(':');
250     SmallString<256> HeaderFileName;
251     // Prepend header file name prefix if it's not absolute.
252     if (sys::path::is_absolute(TargetAndDependents.first))
253       llvm::sys::path::native(TargetAndDependents.first, HeaderFileName);
254     else {
255       if (HeaderDirectory.size() != 0)
256         HeaderFileName = HeaderDirectory;
257       else
258         HeaderFileName = CurrentDirectory;
259       sys::path::append(HeaderFileName, TargetAndDependents.first);
260       sys::path::native(HeaderFileName);
261     }
262     // Handle optional dependencies.
263     DependentsVector Dependents;
264     SmallVector<StringRef, 4> DependentsList;
265     TargetAndDependents.second.split(DependentsList, " ", -1, false);
266     int Count = DependentsList.size();
267     for (int Index = 0; Index < Count; ++Index) {
268       SmallString<256> Dependent;
269       if (sys::path::is_absolute(DependentsList[Index]))
270         Dependent = DependentsList[Index];
271       else {
272         if (HeaderDirectory.size() != 0)
273           Dependent = HeaderDirectory;
274         else
275           Dependent = CurrentDirectory;
276         sys::path::append(Dependent, DependentsList[Index]);
277       }
278       sys::path::native(Dependent);
279       Dependents.push_back(Dependent.str());
280     }
281     // Save the resulting header file path and dependencies.
282     HeaderFileNames.push_back(HeaderFileName.str());
283     Dependencies[HeaderFileName.str()] = Dependents;
284   }
285 
286   return error_code();
287 }
288 
289 // Helper function for finding the input file in an arguments list.
290 std::string findInputFile(const CommandLineArguments &CLArgs) {
291   std::unique_ptr<OptTable> Opts(createDriverOptTable());
292   const unsigned IncludedFlagsBitmask = options::CC1Option;
293   unsigned MissingArgIndex, MissingArgCount;
294   SmallVector<const char *, 256> Argv;
295   for (CommandLineArguments::const_iterator I = CLArgs.begin(),
296                                             E = CLArgs.end();
297        I != E; ++I)
298     Argv.push_back(I->c_str());
299   std::unique_ptr<InputArgList> Args(
300       Opts->ParseArgs(Argv.data(), Argv.data() + Argv.size(), MissingArgIndex,
301                       MissingArgCount, IncludedFlagsBitmask));
302   std::vector<std::string> Inputs = Args->getAllArgValues(OPT_INPUT);
303   return Inputs.back();
304 }
305 
306 // We provide this derivation to add in "-include (file)" arguments for header
307 // dependencies.
308 class AddDependenciesAdjuster : public ArgumentsAdjuster {
309 public:
310   AddDependenciesAdjuster(DependencyMap &Dependencies)
311       : Dependencies(Dependencies) {}
312 
313 private:
314   // Callback for adjusting commandline arguments.
315   CommandLineArguments Adjust(const CommandLineArguments &Args) {
316     std::string InputFile = findInputFile(Args);
317     DependentsVector &FileDependents = Dependencies[InputFile];
318     int Count = FileDependents.size();
319     if (Count == 0)
320       return Args;
321     CommandLineArguments NewArgs(Args);
322     for (int Index = 0; Index < Count; ++Index) {
323       NewArgs.push_back("-include");
324       std::string File(std::string("\"") + FileDependents[Index] +
325                        std::string("\""));
326       NewArgs.push_back(FileDependents[Index]);
327     }
328     return NewArgs;
329   }
330   DependencyMap &Dependencies;
331 };
332 
333 // FIXME: The Location class seems to be something that we might
334 // want to design to be applicable to a wider range of tools, and stick it
335 // somewhere into Tooling/ in mainline
336 struct Location {
337   const FileEntry *File;
338   unsigned Line, Column;
339 
340   Location() : File(), Line(), Column() {}
341 
342   Location(SourceManager &SM, SourceLocation Loc) : File(), Line(), Column() {
343     Loc = SM.getExpansionLoc(Loc);
344     if (Loc.isInvalid())
345       return;
346 
347     std::pair<FileID, unsigned> Decomposed = SM.getDecomposedLoc(Loc);
348     File = SM.getFileEntryForID(Decomposed.first);
349     if (!File)
350       return;
351 
352     Line = SM.getLineNumber(Decomposed.first, Decomposed.second);
353     Column = SM.getColumnNumber(Decomposed.first, Decomposed.second);
354   }
355 
356   operator bool() const { return File != nullptr; }
357 
358   friend bool operator==(const Location &X, const Location &Y) {
359     return X.File == Y.File && X.Line == Y.Line && X.Column == Y.Column;
360   }
361 
362   friend bool operator!=(const Location &X, const Location &Y) {
363     return !(X == Y);
364   }
365 
366   friend bool operator<(const Location &X, const Location &Y) {
367     if (X.File != Y.File)
368       return X.File < Y.File;
369     if (X.Line != Y.Line)
370       return X.Line < Y.Line;
371     return X.Column < Y.Column;
372   }
373   friend bool operator>(const Location &X, const Location &Y) { return Y < X; }
374   friend bool operator<=(const Location &X, const Location &Y) {
375     return !(Y < X);
376   }
377   friend bool operator>=(const Location &X, const Location &Y) {
378     return !(X < Y);
379   }
380 };
381 
382 struct Entry {
383   enum EntryKind {
384     EK_Tag,
385     EK_Value,
386     EK_Macro,
387 
388     EK_NumberOfKinds
389   } Kind;
390 
391   Location Loc;
392 
393   StringRef getKindName() { return getKindName(Kind); }
394   static StringRef getKindName(EntryKind kind);
395 };
396 
397 // Return a string representing the given kind.
398 StringRef Entry::getKindName(Entry::EntryKind kind) {
399   switch (kind) {
400   case EK_Tag:
401     return "tag";
402   case EK_Value:
403     return "value";
404   case EK_Macro:
405     return "macro";
406   case EK_NumberOfKinds:
407     break;
408   }
409   llvm_unreachable("invalid Entry kind");
410 }
411 
412 struct HeaderEntry {
413   std::string Name;
414   Location Loc;
415 
416   friend bool operator==(const HeaderEntry &X, const HeaderEntry &Y) {
417     return X.Loc == Y.Loc && X.Name == Y.Name;
418   }
419   friend bool operator!=(const HeaderEntry &X, const HeaderEntry &Y) {
420     return !(X == Y);
421   }
422   friend bool operator<(const HeaderEntry &X, const HeaderEntry &Y) {
423     return X.Loc < Y.Loc || (X.Loc == Y.Loc && X.Name < Y.Name);
424   }
425   friend bool operator>(const HeaderEntry &X, const HeaderEntry &Y) {
426     return Y < X;
427   }
428   friend bool operator<=(const HeaderEntry &X, const HeaderEntry &Y) {
429     return !(Y < X);
430   }
431   friend bool operator>=(const HeaderEntry &X, const HeaderEntry &Y) {
432     return !(X < Y);
433   }
434 };
435 
436 typedef std::vector<HeaderEntry> HeaderContents;
437 
438 class EntityMap : public StringMap<SmallVector<Entry, 2> > {
439 public:
440   DenseMap<const FileEntry *, HeaderContents> HeaderContentMismatches;
441 
442   void add(const std::string &Name, enum Entry::EntryKind Kind, Location Loc) {
443     // Record this entity in its header.
444     HeaderEntry HE = { Name, Loc };
445     CurHeaderContents[Loc.File].push_back(HE);
446 
447     // Check whether we've seen this entry before.
448     SmallVector<Entry, 2> &Entries = (*this)[Name];
449     for (unsigned I = 0, N = Entries.size(); I != N; ++I) {
450       if (Entries[I].Kind == Kind && Entries[I].Loc == Loc)
451         return;
452     }
453 
454     // We have not seen this entry before; record it.
455     Entry E = { Kind, Loc };
456     Entries.push_back(E);
457   }
458 
459   void mergeCurHeaderContents() {
460     for (DenseMap<const FileEntry *, HeaderContents>::iterator
461              H = CurHeaderContents.begin(),
462              HEnd = CurHeaderContents.end();
463          H != HEnd; ++H) {
464       // Sort contents.
465       std::sort(H->second.begin(), H->second.end());
466 
467       // Check whether we've seen this header before.
468       DenseMap<const FileEntry *, HeaderContents>::iterator KnownH =
469           AllHeaderContents.find(H->first);
470       if (KnownH == AllHeaderContents.end()) {
471         // We haven't seen this header before; record its contents.
472         AllHeaderContents.insert(*H);
473         continue;
474       }
475 
476       // If the header contents are the same, we're done.
477       if (H->second == KnownH->second)
478         continue;
479 
480       // Determine what changed.
481       std::set_symmetric_difference(
482           H->second.begin(), H->second.end(), KnownH->second.begin(),
483           KnownH->second.end(),
484           std::back_inserter(HeaderContentMismatches[H->first]));
485     }
486 
487     CurHeaderContents.clear();
488   }
489 
490 private:
491   DenseMap<const FileEntry *, HeaderContents> CurHeaderContents;
492   DenseMap<const FileEntry *, HeaderContents> AllHeaderContents;
493 };
494 
495 class CollectEntitiesVisitor
496     : public RecursiveASTVisitor<CollectEntitiesVisitor> {
497 public:
498   CollectEntitiesVisitor(SourceManager &SM, EntityMap &Entities,
499                          Preprocessor &PP, PreprocessorTracker &PPTracker,
500                          int &HadErrors)
501       : SM(SM), Entities(Entities), PP(PP), PPTracker(PPTracker),
502         HadErrors(HadErrors) {}
503 
504   bool TraverseStmt(Stmt *S) { return true; }
505   bool TraverseType(QualType T) { return true; }
506   bool TraverseTypeLoc(TypeLoc TL) { return true; }
507   bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) { return true; }
508   bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
509     return true;
510   }
511   bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo) {
512     return true;
513   }
514   bool TraverseTemplateName(TemplateName Template) { return true; }
515   bool TraverseTemplateArgument(const TemplateArgument &Arg) { return true; }
516   bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) {
517     return true;
518   }
519   bool TraverseTemplateArguments(const TemplateArgument *Args,
520                                  unsigned NumArgs) {
521     return true;
522   }
523   bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { return true; }
524   bool TraverseLambdaCapture(LambdaCapture C) { return true; }
525 
526   // Check 'extern "*" {}' block for #include directives.
527   bool VisitLinkageSpecDecl(LinkageSpecDecl *D) {
528     // Bail if not a block.
529     if (!D->hasBraces())
530       return true;
531     SourceRange BlockRange = D->getSourceRange();
532     const char *LinkageLabel;
533     switch (D->getLanguage()) {
534     case LinkageSpecDecl::lang_c:
535       LinkageLabel = "extern \"C\" {}";
536       break;
537     case LinkageSpecDecl::lang_cxx:
538       LinkageLabel = "extern \"C++\" {}";
539       break;
540     }
541     if (!PPTracker.checkForIncludesInBlock(PP, BlockRange, LinkageLabel,
542                                            errs()))
543       HadErrors = 1;
544     return true;
545   }
546 
547   // Check 'namespace (name) {}' block for #include directives.
548   bool VisitNamespaceDecl(const NamespaceDecl *D) {
549     SourceRange BlockRange = D->getSourceRange();
550     std::string Label("namespace ");
551     Label += D->getName();
552     Label += " {}";
553     if (!PPTracker.checkForIncludesInBlock(PP, BlockRange, Label.c_str(),
554                                            errs()))
555       HadErrors = 1;
556     return true;
557   }
558 
559   // Collect definition entities.
560   bool VisitNamedDecl(NamedDecl *ND) {
561     // We only care about file-context variables.
562     if (!ND->getDeclContext()->isFileContext())
563       return true;
564 
565     // Skip declarations that tend to be properly multiply-declared.
566     if (isa<NamespaceDecl>(ND) || isa<UsingDirectiveDecl>(ND) ||
567         isa<NamespaceAliasDecl>(ND) ||
568         isa<ClassTemplateSpecializationDecl>(ND) || isa<UsingDecl>(ND) ||
569         isa<ClassTemplateDecl>(ND) || isa<TemplateTypeParmDecl>(ND) ||
570         isa<TypeAliasTemplateDecl>(ND) || isa<UsingShadowDecl>(ND) ||
571         isa<FunctionDecl>(ND) || isa<FunctionTemplateDecl>(ND) ||
572         (isa<TagDecl>(ND) &&
573          !cast<TagDecl>(ND)->isThisDeclarationADefinition()))
574       return true;
575 
576     // Skip anonymous declarations.
577     if (!ND->getDeclName())
578       return true;
579 
580     // Get the qualified name.
581     std::string Name;
582     llvm::raw_string_ostream OS(Name);
583     ND->printQualifiedName(OS);
584     OS.flush();
585     if (Name.empty())
586       return true;
587 
588     Location Loc(SM, ND->getLocation());
589     if (!Loc)
590       return true;
591 
592     Entities.add(Name, isa<TagDecl>(ND) ? Entry::EK_Tag : Entry::EK_Value, Loc);
593     return true;
594   }
595 
596 private:
597   SourceManager &SM;
598   EntityMap &Entities;
599   Preprocessor &PP;
600   PreprocessorTracker &PPTracker;
601   int &HadErrors;
602 };
603 
604 class CollectEntitiesConsumer : public ASTConsumer {
605 public:
606   CollectEntitiesConsumer(EntityMap &Entities,
607                           PreprocessorTracker &preprocessorTracker,
608                           Preprocessor &PP, StringRef InFile, int &HadErrors)
609       : Entities(Entities), PPTracker(preprocessorTracker), PP(PP),
610         HadErrors(HadErrors) {
611     PPTracker.handlePreprocessorEntry(PP, InFile);
612   }
613 
614   ~CollectEntitiesConsumer() { PPTracker.handlePreprocessorExit(); }
615 
616   virtual void HandleTranslationUnit(ASTContext &Ctx) {
617     SourceManager &SM = Ctx.getSourceManager();
618 
619     // Collect declared entities.
620     CollectEntitiesVisitor(SM, Entities, PP, PPTracker, HadErrors)
621         .TraverseDecl(Ctx.getTranslationUnitDecl());
622 
623     // Collect macro definitions.
624     for (Preprocessor::macro_iterator M = PP.macro_begin(),
625                                       MEnd = PP.macro_end();
626          M != MEnd; ++M) {
627       Location Loc(SM, M->second->getLocation());
628       if (!Loc)
629         continue;
630 
631       Entities.add(M->first->getName().str(), Entry::EK_Macro, Loc);
632     }
633 
634     // Merge header contents.
635     Entities.mergeCurHeaderContents();
636   }
637 
638 private:
639   EntityMap &Entities;
640   PreprocessorTracker &PPTracker;
641   Preprocessor &PP;
642   int &HadErrors;
643 };
644 
645 class CollectEntitiesAction : public SyntaxOnlyAction {
646 public:
647   CollectEntitiesAction(EntityMap &Entities,
648                         PreprocessorTracker &preprocessorTracker,
649                         int &HadErrors)
650       : Entities(Entities), PPTracker(preprocessorTracker),
651         HadErrors(HadErrors) {}
652 
653 protected:
654   virtual clang::ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
655                                                 StringRef InFile) {
656     return new CollectEntitiesConsumer(Entities, PPTracker,
657                                        CI.getPreprocessor(), InFile, HadErrors);
658   }
659 
660 private:
661   EntityMap &Entities;
662   PreprocessorTracker &PPTracker;
663   int &HadErrors;
664 };
665 
666 class ModularizeFrontendActionFactory : public FrontendActionFactory {
667 public:
668   ModularizeFrontendActionFactory(EntityMap &Entities,
669                                   PreprocessorTracker &preprocessorTracker,
670                                   int &HadErrors)
671       : Entities(Entities), PPTracker(preprocessorTracker),
672         HadErrors(HadErrors) {}
673 
674   virtual CollectEntitiesAction *create() {
675     return new CollectEntitiesAction(Entities, PPTracker, HadErrors);
676   }
677 
678 private:
679   EntityMap &Entities;
680   PreprocessorTracker &PPTracker;
681   int &HadErrors;
682 };
683 
684 int main(int Argc, const char **Argv) {
685 
686   // Save program name for error messages.
687   Argv0 = Argv[0];
688 
689   // Save program arguments for use in module.map comment.
690   CommandLine = sys::path::stem(sys::path::filename(Argv0));
691   for (int ArgIndex = 1; ArgIndex < Argc; ArgIndex++) {
692     CommandLine.append(" ");
693     CommandLine.append(Argv[ArgIndex]);
694   }
695 
696   // This causes options to be parsed.
697   cl::ParseCommandLineOptions(Argc, Argv, "modularize.\n");
698 
699   // No go if we have no header list file.
700   if (ListFileName.size() == 0) {
701     cl::PrintHelpMessage();
702     return 1;
703   }
704 
705   // Get header file names and dependencies.
706   SmallVector<std::string, 32> Headers;
707   DependencyMap Dependencies;
708   if (error_code EC = getHeaderFileNames(Headers, Dependencies, ListFileName,
709                                          HeaderPrefix)) {
710     errs() << Argv[0] << ": error: Unable to get header list '" << ListFileName
711            << "': " << EC.message() << '\n';
712     return 1;
713   }
714 
715   // If we are in assistant mode, output the module map and quit.
716   if (ModuleMapPath.length() != 0) {
717     if (!createModuleMap(ModuleMapPath, Headers, Dependencies, HeaderPrefix,
718                          RootModule))
719       return 1; // Failed.
720     return 0;   // Success - Skip checks in assistant mode.
721   }
722 
723   // Create the compilation database.
724   SmallString<256> PathBuf;
725   sys::fs::current_path(PathBuf);
726   std::unique_ptr<CompilationDatabase> Compilations;
727   Compilations.reset(
728       new FixedCompilationDatabase(Twine(PathBuf), CC1Arguments));
729 
730   // Create preprocessor tracker, to watch for macro and conditional problems.
731   std::unique_ptr<PreprocessorTracker> PPTracker(PreprocessorTracker::create());
732 
733   // Parse all of the headers, detecting duplicates.
734   EntityMap Entities;
735   ClangTool Tool(*Compilations, Headers);
736   Tool.appendArgumentsAdjuster(new AddDependenciesAdjuster(Dependencies));
737   int HadErrors = 0;
738   HadErrors |= Tool.run(
739       new ModularizeFrontendActionFactory(Entities, *PPTracker, HadErrors));
740 
741   // Create a place to save duplicate entity locations, separate bins per kind.
742   typedef SmallVector<Location, 8> LocationArray;
743   typedef SmallVector<LocationArray, Entry::EK_NumberOfKinds> EntryBinArray;
744   EntryBinArray EntryBins;
745   int KindIndex;
746   for (KindIndex = 0; KindIndex < Entry::EK_NumberOfKinds; ++KindIndex) {
747     LocationArray Array;
748     EntryBins.push_back(Array);
749   }
750 
751   // Check for the same entity being defined in multiple places.
752   for (EntityMap::iterator E = Entities.begin(), EEnd = Entities.end();
753        E != EEnd; ++E) {
754     // If only one occurrence, exit early.
755     if (E->second.size() == 1)
756       continue;
757     // Clear entity locations.
758     for (EntryBinArray::iterator CI = EntryBins.begin(), CE = EntryBins.end();
759          CI != CE; ++CI) {
760       CI->clear();
761     }
762     // Walk the entities of a single name, collecting the locations,
763     // separated into separate bins.
764     for (unsigned I = 0, N = E->second.size(); I != N; ++I) {
765       EntryBins[E->second[I].Kind].push_back(E->second[I].Loc);
766     }
767     // Report any duplicate entity definition errors.
768     int KindIndex = 0;
769     for (EntryBinArray::iterator DI = EntryBins.begin(), DE = EntryBins.end();
770          DI != DE; ++DI, ++KindIndex) {
771       int ECount = DI->size();
772       // If only 1 occurrence of this entity, skip it, as we only report duplicates.
773       if (ECount <= 1)
774         continue;
775       LocationArray::iterator FI = DI->begin();
776       StringRef kindName = Entry::getKindName((Entry::EntryKind)KindIndex);
777       errs() << "error: " << kindName << " '" << E->first()
778              << "' defined at multiple locations:\n";
779       for (LocationArray::iterator FE = DI->end(); FI != FE; ++FI) {
780         errs() << "    " << FI->File->getName() << ":" << FI->Line << ":"
781                << FI->Column << "\n";
782       }
783       HadErrors = 1;
784     }
785   }
786 
787   // Complain about macro instance in header files that differ based on how
788   // they are included.
789   if (PPTracker->reportInconsistentMacros(errs()))
790     HadErrors = 1;
791 
792   // Complain about preprocessor conditional directives in header files that
793   // differ based on how they are included.
794   if (PPTracker->reportInconsistentConditionals(errs()))
795     HadErrors = 1;
796 
797   // Complain about any headers that have contents that differ based on how
798   // they are included.
799   // FIXME: Could we provide information about which preprocessor conditionals
800   // are involved?
801   for (DenseMap<const FileEntry *, HeaderContents>::iterator
802            H = Entities.HeaderContentMismatches.begin(),
803            HEnd = Entities.HeaderContentMismatches.end();
804        H != HEnd; ++H) {
805     if (H->second.empty()) {
806       errs() << "internal error: phantom header content mismatch\n";
807       continue;
808     }
809 
810     HadErrors = 1;
811     errs() << "error: header '" << H->first->getName()
812            << "' has different contents depending on how it was included.\n";
813     for (unsigned I = 0, N = H->second.size(); I != N; ++I) {
814       errs() << "note: '" << H->second[I].Name << "' in "
815              << H->second[I].Loc.File->getName() << " at "
816              << H->second[I].Loc.Line << ":" << H->second[I].Loc.Column
817              << " not always provided\n";
818     }
819   }
820 
821   return HadErrors;
822 }
823