clang-tools  10.0.0svn
Modularize.cpp
Go to the documentation of this file.
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
10 //
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.
14 //
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.
21 //
22 // Modularize takes as input either one or more module maps (by default,
23 // "module.modulemap") or one or more text files contatining lists of headers
24 // to check.
25 //
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.
33 //
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.
41 //
42 // Modularize also accepts regular clang front-end arguments.
43 //
44 // Usage: modularize [(modularize options)]
45 // [(include-files_list)|(module map)]+ [(front-end-options) ...]
46 //
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.
83 //
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
87 //
88 // Modularization Issue Checks
89 //
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:
93 //
94 // error: '(symbol)' defined at multiple locations:
95 // (file):(row):(column)
96 // (file):(row):(column)
97 //
98 // error: header '(file)' has different contents depending on how it was
99 // included
100 //
101 // The latter might be followed by messages like the following:
102 //
103 // note: '(symbol)' in (file) at (row):(column) not always provided
104 //
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:
108 //
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.
128 //
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:
132 //
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.
141 //
142 // See PreprocessorTracker.cpp for additional details.
143 //
144 // Module Map Coverage Check
145 //
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.
157 //
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.
163 //
164 // Warning message have the form:
165 //
166 // warning: module.modulemap does not account for file: Level3A.h
167 //
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.
171 //
172 // Module Map Assistant - Module Map Generation
173 //
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.
180 //
181 // An example command line for generating a module.modulemap file:
182 //
183 // modularize -module-map-path=module.modulemap -root-module=myroot \
184 // headerlist.txt
185 //
186 // Note that if the headers in the header list have partial paths, sub-modules
187 // will be created for the subdirectires 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.
190 //
191 // See the ModuleAssistant.cpp file comments for additional details about the
192 // implementation of the assistant mode.
193 //
194 // Future directions:
195 //
196 // Basically, we want to add new checks for whatever we can check with respect
197 // to checking headers for module'ability.
198 //
199 // Some ideas:
200 //
201 // 1. Omit duplicate "not always provided" messages
202 //
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.
205 //
206 // 3. Try to figure out the preprocessor conditional directives that
207 // contribute to problems and tie them to the inconsistent definitions.
208 //
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.
216 //
217 // 5. What else?
218 //
219 // General clean-up and refactoring:
220 //
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
224 //
225 //===----------------------------------------------------------------------===//
226 
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 <fstream>
251 #include <iterator>
252 #include <string>
253 #include <vector>
254 
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;
262 
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);
268 
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>..."));
273 
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."));
281 
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."));
289 
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."));
297 
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."));
302 
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."));
311 
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::ZeroOrMore, cl::value_desc("path"));
316 
317 // Option for disabling the coverage check.
318 static cl::opt<bool>
319 NoCoverageCheck("no-coverage-check", cl::init(false),
320 cl::desc("Don't do the coverage check."));
321 
322 // Option for just doing the coverage check.
323 static cl::opt<bool>
324 CoverageCheckOnly("coverage-check-only", cl::init(false),
325 cl::desc("Only do the coverage check."));
326 
327 // Option for displaying lists of good, bad, and mixed files.
328 static cl::opt<bool>
329 DisplayFileLists("display-file-lists", cl::init(false),
330 cl::desc("Display lists of good files (no compile errors), problem files,"
331  " and a combined list with problem files preceded by a '#'."));
332 
333 // Save the program name for error messages.
334 const char *Argv0;
335 // Save the command line for comments.
336 std::string CommandLine;
337 
338 // Helper function for finding the input file in an arguments list.
339 static std::string findInputFile(const CommandLineArguments &CLArgs) {
340  const unsigned IncludedFlagsBitmask = options::CC1Option;
341  unsigned MissingArgIndex, MissingArgCount;
342  SmallVector<const char *, 256> Argv;
343  for (auto I = CLArgs.begin(), E = CLArgs.end(); I != E; ++I)
344  Argv.push_back(I->c_str());
345  InputArgList Args = getDriverOptTable().ParseArgs(
346  Argv, MissingArgIndex, MissingArgCount, IncludedFlagsBitmask);
347  std::vector<std::string> Inputs = Args.getAllArgValues(OPT_INPUT);
348  return ModularizeUtilities::getCanonicalPath(Inputs.back());
349 }
350 
351 // This arguments adjuster inserts "-include (file)" arguments for header
352 // dependencies. It also inserts a "-w" option and a "-x c++",
353 // if no other "-x" option is present.
354 static ArgumentsAdjuster
356  return [&Dependencies](const CommandLineArguments &Args,
357  StringRef /*unused*/) {
358  std::string InputFile = findInputFile(Args);
359  DependentsVector &FileDependents = Dependencies[InputFile];
360  CommandLineArguments NewArgs(Args);
361  if (int Count = FileDependents.size()) {
362  for (int Index = 0; Index < Count; ++Index) {
363  NewArgs.push_back("-include");
364  std::string File(std::string("\"") + FileDependents[Index] +
365  std::string("\""));
366  NewArgs.push_back(FileDependents[Index]);
367  }
368  }
369  // Ignore warnings. (Insert after "clang_tool" at beginning.)
370  NewArgs.insert(NewArgs.begin() + 1, "-w");
371  // Since we are compiling .h files, assume C++ unless given a -x option.
372  if (!llvm::is_contained(NewArgs, "-x")) {
373  NewArgs.insert(NewArgs.begin() + 2, "-x");
374  NewArgs.insert(NewArgs.begin() + 3, "c++");
375  }
376  return NewArgs;
377  };
378 }
379 
380 // FIXME: The Location class seems to be something that we might
381 // want to design to be applicable to a wider range of tools, and stick it
382 // somewhere into Tooling/ in mainline
383 struct Location {
384  const FileEntry *File;
385  unsigned Line, Column;
386 
387  Location() : File(), Line(), Column() {}
388 
389  Location(SourceManager &SM, SourceLocation Loc) : File(), Line(), Column() {
390  Loc = SM.getExpansionLoc(Loc);
391  if (Loc.isInvalid())
392  return;
393 
394  std::pair<FileID, unsigned> Decomposed = SM.getDecomposedLoc(Loc);
395  File = SM.getFileEntryForID(Decomposed.first);
396  if (!File)
397  return;
398 
399  Line = SM.getLineNumber(Decomposed.first, Decomposed.second);
400  Column = SM.getColumnNumber(Decomposed.first, Decomposed.second);
401  }
402 
403  operator bool() const { return File != nullptr; }
404 
405  friend bool operator==(const Location &X, const Location &Y) {
406  return X.File == Y.File && X.Line == Y.Line && X.Column == Y.Column;
407  }
408 
409  friend bool operator!=(const Location &X, const Location &Y) {
410  return !(X == Y);
411  }
412 
413  friend bool operator<(const Location &X, const Location &Y) {
414  if (X.File != Y.File)
415  return X.File < Y.File;
416  if (X.Line != Y.Line)
417  return X.Line < Y.Line;
418  return X.Column < Y.Column;
419  }
420  friend bool operator>(const Location &X, const Location &Y) { return Y < X; }
421  friend bool operator<=(const Location &X, const Location &Y) {
422  return !(Y < X);
423  }
424  friend bool operator>=(const Location &X, const Location &Y) {
425  return !(X < Y);
426  }
427 };
428 
429 struct Entry {
430  enum EntryKind {
434 
435  EK_NumberOfKinds
436  } Kind;
437 
439 
440  StringRef getKindName() { return getKindName(Kind); }
441  static StringRef getKindName(EntryKind kind);
442 };
443 
444 // Return a string representing the given kind.
446  switch (kind) {
447  case EK_Tag:
448  return "tag";
449  case EK_Value:
450  return "value";
451  case EK_Macro:
452  return "macro";
453  case EK_NumberOfKinds:
454  break;
455  }
456  llvm_unreachable("invalid Entry kind");
457 }
458 
459 struct HeaderEntry {
460  std::string Name;
462 
463  friend bool operator==(const HeaderEntry &X, const HeaderEntry &Y) {
464  return X.Loc == Y.Loc && X.Name == Y.Name;
465  }
466  friend bool operator!=(const HeaderEntry &X, const HeaderEntry &Y) {
467  return !(X == Y);
468  }
469  friend bool operator<(const HeaderEntry &X, const HeaderEntry &Y) {
470  return X.Loc < Y.Loc || (X.Loc == Y.Loc && X.Name < Y.Name);
471  }
472  friend bool operator>(const HeaderEntry &X, const HeaderEntry &Y) {
473  return Y < X;
474  }
475  friend bool operator<=(const HeaderEntry &X, const HeaderEntry &Y) {
476  return !(Y < X);
477  }
478  friend bool operator>=(const HeaderEntry &X, const HeaderEntry &Y) {
479  return !(X < Y);
480  }
481 };
482 
483 typedef std::vector<HeaderEntry> HeaderContents;
484 
485 class EntityMap : public StringMap<SmallVector<Entry, 2> > {
486 public:
487  DenseMap<const FileEntry *, HeaderContents> HeaderContentMismatches;
488 
489  void add(const std::string &Name, enum Entry::EntryKind Kind, Location Loc) {
490  // Record this entity in its header.
491  HeaderEntry HE = { Name, Loc };
492  CurHeaderContents[Loc.File].push_back(HE);
493 
494  // Check whether we've seen this entry before.
495  SmallVector<Entry, 2> &Entries = (*this)[Name];
496  for (unsigned I = 0, N = Entries.size(); I != N; ++I) {
497  if (Entries[I].Kind == Kind && Entries[I].Loc == Loc)
498  return;
499  }
500 
501  // We have not seen this entry before; record it.
502  Entry E = { Kind, Loc };
503  Entries.push_back(E);
504  }
505 
507  for (DenseMap<const FileEntry *, HeaderContents>::iterator
508  H = CurHeaderContents.begin(),
509  HEnd = CurHeaderContents.end();
510  H != HEnd; ++H) {
511  // Sort contents.
512  std::sort(H->second.begin(), H->second.end());
513 
514  // Check whether we've seen this header before.
515  DenseMap<const FileEntry *, HeaderContents>::iterator KnownH =
516  AllHeaderContents.find(H->first);
517  if (KnownH == AllHeaderContents.end()) {
518  // We haven't seen this header before; record its contents.
519  AllHeaderContents.insert(*H);
520  continue;
521  }
522 
523  // If the header contents are the same, we're done.
524  if (H->second == KnownH->second)
525  continue;
526 
527  // Determine what changed.
528  std::set_symmetric_difference(
529  H->second.begin(), H->second.end(), KnownH->second.begin(),
530  KnownH->second.end(),
531  std::back_inserter(HeaderContentMismatches[H->first]));
532  }
533 
534  CurHeaderContents.clear();
535  }
536 
537 private:
538  DenseMap<const FileEntry *, HeaderContents> CurHeaderContents;
539  DenseMap<const FileEntry *, HeaderContents> AllHeaderContents;
540 };
541 
543  : public RecursiveASTVisitor<CollectEntitiesVisitor> {
544 public:
545  CollectEntitiesVisitor(SourceManager &SM, EntityMap &Entities,
546  Preprocessor &PP, PreprocessorTracker &PPTracker,
547  int &HadErrors)
548  : SM(SM), Entities(Entities), PP(PP), PPTracker(PPTracker),
549  HadErrors(HadErrors) {}
550 
551  bool TraverseStmt(Stmt *S) { return true; }
552  bool TraverseType(QualType T) { return true; }
553  bool TraverseTypeLoc(TypeLoc TL) { return true; }
554  bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) { return true; }
555  bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
556  return true;
557  }
558  bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo) {
559  return true;
560  }
561  bool TraverseTemplateName(TemplateName Template) { return true; }
562  bool TraverseTemplateArgument(const TemplateArgument &Arg) { return true; }
563  bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) {
564  return true;
565  }
566  bool TraverseTemplateArguments(const TemplateArgument *Args,
567  unsigned NumArgs) {
568  return true;
569  }
570  bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { return true; }
571  bool TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C,
572  Expr *Init) {
573  return true;
574  }
575 
576  // Check 'extern "*" {}' block for #include directives.
577  bool VisitLinkageSpecDecl(LinkageSpecDecl *D) {
578  // Bail if not a block.
579  if (!D->hasBraces())
580  return true;
581  SourceRange BlockRange = D->getSourceRange();
582  const char *LinkageLabel;
583  switch (D->getLanguage()) {
584  case LinkageSpecDecl::lang_c:
585  LinkageLabel = "extern \"C\" {}";
586  break;
587  case LinkageSpecDecl::lang_cxx:
588  case LinkageSpecDecl::lang_cxx_11:
589  case LinkageSpecDecl::lang_cxx_14:
590  LinkageLabel = "extern \"C++\" {}";
591  break;
592  }
593  if (!PPTracker.checkForIncludesInBlock(PP, BlockRange, LinkageLabel,
594  errs()))
595  HadErrors = 1;
596  return true;
597  }
598 
599  // Check 'namespace (name) {}' block for #include directives.
600  bool VisitNamespaceDecl(const NamespaceDecl *D) {
601  SourceRange BlockRange = D->getSourceRange();
602  std::string Label("namespace ");
603  Label += D->getName();
604  Label += " {}";
605  if (!PPTracker.checkForIncludesInBlock(PP, BlockRange, Label.c_str(),
606  errs()))
607  HadErrors = 1;
608  return true;
609  }
610 
611  // Collect definition entities.
612  bool VisitNamedDecl(NamedDecl *ND) {
613  // We only care about file-context variables.
614  if (!ND->getDeclContext()->isFileContext())
615  return true;
616 
617  // Skip declarations that tend to be properly multiply-declared.
618  if (isa<NamespaceDecl>(ND) || isa<UsingDirectiveDecl>(ND) ||
619  isa<NamespaceAliasDecl>(ND) ||
620  isa<ClassTemplateSpecializationDecl>(ND) || isa<UsingDecl>(ND) ||
621  isa<ClassTemplateDecl>(ND) || isa<TemplateTypeParmDecl>(ND) ||
622  isa<TypeAliasTemplateDecl>(ND) || isa<UsingShadowDecl>(ND) ||
623  isa<FunctionDecl>(ND) || isa<FunctionTemplateDecl>(ND) ||
624  (isa<TagDecl>(ND) &&
625  !cast<TagDecl>(ND)->isThisDeclarationADefinition()))
626  return true;
627 
628  // Skip anonymous declarations.
629  if (!ND->getDeclName())
630  return true;
631 
632  // Get the qualified name.
633  std::string Name;
634  llvm::raw_string_ostream OS(Name);
635  ND->printQualifiedName(OS);
636  OS.flush();
637  if (Name.empty())
638  return true;
639 
640  Location Loc(SM, ND->getLocation());
641  if (!Loc)
642  return true;
643 
644  Entities.add(Name, isa<TagDecl>(ND) ? Entry::EK_Tag : Entry::EK_Value, Loc);
645  return true;
646  }
647 
648 private:
649  SourceManager &SM;
650  EntityMap &Entities;
651  Preprocessor &PP;
652  PreprocessorTracker &PPTracker;
653  int &HadErrors;
654 };
655 
657 public:
659  PreprocessorTracker &preprocessorTracker,
660  Preprocessor &PP, StringRef InFile, int &HadErrors)
661  : Entities(Entities), PPTracker(preprocessorTracker), PP(PP),
662  HadErrors(HadErrors) {
663  PPTracker.handlePreprocessorEntry(PP, InFile);
664  }
665 
666  ~CollectEntitiesConsumer() override { PPTracker.handlePreprocessorExit(); }
667 
668  void HandleTranslationUnit(ASTContext &Ctx) override {
669  SourceManager &SM = Ctx.getSourceManager();
670 
671  // Collect declared entities.
672  CollectEntitiesVisitor(SM, Entities, PP, PPTracker, HadErrors)
673  .TraverseDecl(Ctx.getTranslationUnitDecl());
674 
675  // Collect macro definitions.
676  for (Preprocessor::macro_iterator M = PP.macro_begin(),
677  MEnd = PP.macro_end();
678  M != MEnd; ++M) {
679  Location Loc(SM, M->second.getLatest()->getLocation());
680  if (!Loc)
681  continue;
682 
683  Entities.add(M->first->getName().str(), Entry::EK_Macro, Loc);
684  }
685 
686  // Merge header contents.
687  Entities.mergeCurHeaderContents();
688  }
689 
690 private:
691  EntityMap &Entities;
692  PreprocessorTracker &PPTracker;
693  Preprocessor &PP;
694  int &HadErrors;
695 };
696 
698 public:
700  PreprocessorTracker &preprocessorTracker,
701  int &HadErrors)
702  : Entities(Entities), PPTracker(preprocessorTracker),
703  HadErrors(HadErrors) {}
704 
705 protected:
706  std::unique_ptr<clang::ASTConsumer>
707  CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override {
708  return std::make_unique<CollectEntitiesConsumer>(
709  Entities, PPTracker, CI.getPreprocessor(), InFile, HadErrors);
710  }
711 
712 private:
713  EntityMap &Entities;
714  PreprocessorTracker &PPTracker;
715  int &HadErrors;
716 };
717 
718 class ModularizeFrontendActionFactory : public FrontendActionFactory {
719 public:
721  PreprocessorTracker &preprocessorTracker,
722  int &HadErrors)
723  : Entities(Entities), PPTracker(preprocessorTracker),
724  HadErrors(HadErrors) {}
725 
726  std::unique_ptr<FrontendAction> create() override {
727  return std::make_unique<CollectEntitiesAction>(Entities, PPTracker,
728  HadErrors);
729  }
730 
731 private:
732  EntityMap &Entities;
733  PreprocessorTracker &PPTracker;
734  int &HadErrors;
735 };
736 
738  : public RecursiveASTVisitor<CompileCheckVisitor> {
739 public:
741 
742  bool TraverseStmt(Stmt *S) { return true; }
743  bool TraverseType(QualType T) { return true; }
744  bool TraverseTypeLoc(TypeLoc TL) { return true; }
745  bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) { return true; }
746  bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
747  return true;
748  }
749  bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo) {
750  return true;
751  }
752  bool TraverseTemplateName(TemplateName Template) { return true; }
753  bool TraverseTemplateArgument(const TemplateArgument &Arg) { return true; }
754  bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) {
755  return true;
756  }
757  bool TraverseTemplateArguments(const TemplateArgument *Args,
758  unsigned NumArgs) {
759  return true;
760  }
761  bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { return true; }
762  bool TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C,
763  Expr *Init) {
764  return true;
765  }
766 
767  // Check 'extern "*" {}' block for #include directives.
768  bool VisitLinkageSpecDecl(LinkageSpecDecl *D) {
769  return true;
770  }
771 
772  // Check 'namespace (name) {}' block for #include directives.
773  bool VisitNamespaceDecl(const NamespaceDecl *D) {
774  return true;
775  }
776 
777  // Collect definition entities.
778  bool VisitNamedDecl(NamedDecl *ND) {
779  return true;
780  }
781 };
782 
784 public:
786 
787  void HandleTranslationUnit(ASTContext &Ctx) override {
788  CompileCheckVisitor().TraverseDecl(Ctx.getTranslationUnitDecl());
789  }
790 };
791 
793 public:
795 
796 protected:
797  std::unique_ptr<clang::ASTConsumer>
798  CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override {
799  return std::make_unique<CompileCheckConsumer>();
800  }
801 };
802 
803 class CompileCheckFrontendActionFactory : public FrontendActionFactory {
804 public:
806 
807  std::unique_ptr<FrontendAction> create() override {
808  return std::make_unique<CompileCheckAction>();
809  }
810 };
811 
812 int main(int Argc, const char **Argv) {
813 
814  // Save program name for error messages.
815  Argv0 = Argv[0];
816 
817  // Save program arguments for use in module.modulemap comment.
818  CommandLine = sys::path::stem(sys::path::filename(Argv0));
819  for (int ArgIndex = 1; ArgIndex < Argc; ArgIndex++) {
820  CommandLine.append(" ");
821  CommandLine.append(Argv[ArgIndex]);
822  }
823 
824  // This causes options to be parsed.
825  cl::ParseCommandLineOptions(Argc, Argv, "modularize.\n");
826 
827  // No go if we have no header list file.
828  if (ListFileNames.size() == 0) {
829  cl::PrintHelpMessage();
830  return 1;
831  }
832 
833  std::unique_ptr<ModularizeUtilities> ModUtil;
834  int HadErrors = 0;
835 
836  ModUtil.reset(
837  ModularizeUtilities::createModularizeUtilities(
839 
840  // Get header file names and dependencies.
841  if (ModUtil->loadAllHeaderListsAndDependencies())
842  HadErrors = 1;
843 
844  // If we are in assistant mode, output the module map and quit.
845  if (ModuleMapPath.length() != 0) {
846  if (!createModuleMap(ModuleMapPath, ModUtil->HeaderFileNames,
847  ModUtil->ProblemFileNames,
848  ModUtil->Dependencies, HeaderPrefix, RootModule))
849  return 1; // Failed.
850  return 0; // Success - Skip checks in assistant mode.
851  }
852 
853  // If we're doing module maps.
854  if (!NoCoverageCheck && ModUtil->HasModuleMap) {
855  // Do coverage check.
856  if (ModUtil->doCoverageCheck(IncludePaths, CommandLine))
857  HadErrors = 1;
858  }
859 
860  // Bail early if only doing the coverage check.
861  if (CoverageCheckOnly)
862  return HadErrors;
863 
864  // Create the compilation database.
865  SmallString<256> PathBuf;
866  sys::fs::current_path(PathBuf);
867  std::unique_ptr<CompilationDatabase> Compilations;
868  Compilations.reset(
869  new FixedCompilationDatabase(Twine(PathBuf), CC1Arguments));
870 
871  // Create preprocessor tracker, to watch for macro and conditional problems.
872  std::unique_ptr<PreprocessorTracker> PPTracker(
873  PreprocessorTracker::create(ModUtil->HeaderFileNames,
875 
876  // Coolect entities here.
877  EntityMap Entities;
878 
879  // Because we can't easily determine which files failed
880  // during the tool run, if we're collecting the file lists
881  // for display, we do a first compile pass on individual
882  // files to find which ones don't compile stand-alone.
883  if (DisplayFileLists) {
884  // First, make a pass to just get compile errors.
885  for (auto &CompileCheckFile : ModUtil->HeaderFileNames) {
886  llvm::SmallVector<std::string, 32> CompileCheckFileArray;
887  CompileCheckFileArray.push_back(CompileCheckFile);
888  ClangTool CompileCheckTool(*Compilations, CompileCheckFileArray);
889  CompileCheckTool.appendArgumentsAdjuster(
890  getModularizeArgumentsAdjuster(ModUtil->Dependencies));
891  int CompileCheckFileErrors = 0;
892  // FIXME: use newFrontendActionFactory.
893  CompileCheckFrontendActionFactory CompileCheckFactory;
894  CompileCheckFileErrors |= CompileCheckTool.run(&CompileCheckFactory);
895  if (CompileCheckFileErrors != 0) {
896  ModUtil->addUniqueProblemFile(CompileCheckFile); // Save problem file.
897  HadErrors |= 1;
898  }
899  else
900  ModUtil->addNoCompileErrorsFile(CompileCheckFile); // Save good file.
901  }
902  }
903 
904  // Then we make another pass on the good files to do the rest of the work.
905  ClangTool Tool(*Compilations,
906  (DisplayFileLists ? ModUtil->GoodFileNames : ModUtil->HeaderFileNames));
907  Tool.appendArgumentsAdjuster(
908  getModularizeArgumentsAdjuster(ModUtil->Dependencies));
909  ModularizeFrontendActionFactory Factory(Entities, *PPTracker, HadErrors);
910  HadErrors |= Tool.run(&Factory);
911 
912  // Create a place to save duplicate entity locations, separate bins per kind.
913  typedef SmallVector<Location, 8> LocationArray;
914  typedef SmallVector<LocationArray, Entry::EK_NumberOfKinds> EntryBinArray;
915  EntryBinArray EntryBins;
916  int KindIndex;
917  for (KindIndex = 0; KindIndex < Entry::EK_NumberOfKinds; ++KindIndex) {
918  LocationArray Array;
919  EntryBins.push_back(Array);
920  }
921 
922  // Check for the same entity being defined in multiple places.
923  for (EntityMap::iterator E = Entities.begin(), EEnd = Entities.end();
924  E != EEnd; ++E) {
925  // If only one occurrence, exit early.
926  if (E->second.size() == 1)
927  continue;
928  // Clear entity locations.
929  for (EntryBinArray::iterator CI = EntryBins.begin(), CE = EntryBins.end();
930  CI != CE; ++CI) {
931  CI->clear();
932  }
933  // Walk the entities of a single name, collecting the locations,
934  // separated into separate bins.
935  for (unsigned I = 0, N = E->second.size(); I != N; ++I) {
936  EntryBins[E->second[I].Kind].push_back(E->second[I].Loc);
937  }
938  // Report any duplicate entity definition errors.
939  int KindIndex = 0;
940  for (EntryBinArray::iterator DI = EntryBins.begin(), DE = EntryBins.end();
941  DI != DE; ++DI, ++KindIndex) {
942  int ECount = DI->size();
943  // If only 1 occurrence of this entity, skip it, we only report duplicates.
944  if (ECount <= 1)
945  continue;
946  LocationArray::iterator FI = DI->begin();
947  StringRef kindName = Entry::getKindName((Entry::EntryKind)KindIndex);
948  errs() << "error: " << kindName << " '" << E->first()
949  << "' defined at multiple locations:\n";
950  for (LocationArray::iterator FE = DI->end(); FI != FE; ++FI) {
951  errs() << " " << FI->File->getName() << ":" << FI->Line << ":"
952  << FI->Column << "\n";
953  ModUtil->addUniqueProblemFile(FI->File->getName());
954  }
955  HadErrors = 1;
956  }
957  }
958 
959  // Complain about macro instance in header files that differ based on how
960  // they are included.
961  if (PPTracker->reportInconsistentMacros(errs()))
962  HadErrors = 1;
963 
964  // Complain about preprocessor conditional directives in header files that
965  // differ based on how they are included.
966  if (PPTracker->reportInconsistentConditionals(errs()))
967  HadErrors = 1;
968 
969  // Complain about any headers that have contents that differ based on how
970  // they are included.
971  // FIXME: Could we provide information about which preprocessor conditionals
972  // are involved?
973  for (DenseMap<const FileEntry *, HeaderContents>::iterator
974  H = Entities.HeaderContentMismatches.begin(),
975  HEnd = Entities.HeaderContentMismatches.end();
976  H != HEnd; ++H) {
977  if (H->second.empty()) {
978  errs() << "internal error: phantom header content mismatch\n";
979  continue;
980  }
981 
982  HadErrors = 1;
983  ModUtil->addUniqueProblemFile(H->first->getName());
984  errs() << "error: header '" << H->first->getName()
985  << "' has different contents depending on how it was included.\n";
986  for (unsigned I = 0, N = H->second.size(); I != N; ++I) {
987  errs() << "note: '" << H->second[I].Name << "' in "
988  << H->second[I].Loc.File->getName() << " at "
989  << H->second[I].Loc.Line << ":" << H->second[I].Loc.Column
990  << " not always provided\n";
991  }
992  }
993 
994  if (DisplayFileLists) {
995  ModUtil->displayProblemFiles();
996  ModUtil->displayGoodFiles();
997  ModUtil->displayCombinedFiles();
998  }
999 
1000  return HadErrors;
1001 }
static cl::opt< bool > DisplayFileLists("display-file-lists", cl::init(false), cl::desc("Display lists of good files (no compile errors), problem files," " and a combined list with problem files preceded by a '#'."))
SourceLocation Loc
&#39;#&#39; location in the include directive
bool TraverseTemplateArguments(const TemplateArgument *Args, unsigned NumArgs)
Definition: Modularize.cpp:566
friend bool operator>(const Location &X, const Location &Y)
Definition: Modularize.cpp:420
void HandleTranslationUnit(ASTContext &Ctx) override
Definition: Modularize.cpp:668
void HandleTranslationUnit(ASTContext &Ctx) override
Definition: Modularize.cpp:787
DenseMap< const FileEntry *, HeaderContents > HeaderContentMismatches
Definition: Modularize.cpp:487
unsigned Column
Definition: Modularize.cpp:385
std::unique_ptr< clang::ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Definition: Modularize.cpp:707
Some operations such as code completion produce a set of candidates.
Location Loc
Definition: Modularize.cpp:461
static cl::list< std::string > CC1Arguments(cl::ConsumeAfter, cl::desc("<arguments to be passed to front end>..."))
Common definitions for Modularize.
friend bool operator!=(const Location &X, const Location &Y)
Definition: Modularize.cpp:409
static cl::opt< bool > CoverageCheckOnly("coverage-check-only", cl::init(false), cl::desc("Only do the coverage check."))
bool TraverseConstructorInitializer(CXXCtorInitializer *Init)
Definition: Modularize.cpp:570
friend bool operator<=(const Location &X, const Location &Y)
Definition: Modularize.cpp:421
llvm::SmallVector< std::string, 4 > DependentsVector
Definition: Modularize.h:31
bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo)
Definition: Modularize.cpp:558
const char * Argv0
Definition: Modularize.cpp:334
StringRef getKindName()
Definition: Modularize.cpp:440
bool TraverseTemplateArguments(const TemplateArgument *Args, unsigned NumArgs)
Definition: Modularize.cpp:757
bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc)
Definition: Modularize.cpp:754
bool TraverseTemplateName(TemplateName Template)
Definition: Modularize.cpp:752
static cl::opt< std::string > HeaderPrefix("prefix", cl::init(""), cl::desc("Prepend header file paths with this prefix." " If not specified," " the files are considered to be relative to the header list file."))
CollectEntitiesAction(EntityMap &Entities, PreprocessorTracker &preprocessorTracker, int &HadErrors)
Definition: Modularize.cpp:699
bool VisitNamedDecl(NamedDecl *ND)
Definition: Modularize.cpp:778
int Column
std::unique_ptr< FrontendAction > create() override
Definition: Modularize.cpp:807
static ArgumentsAdjuster getModularizeArgumentsAdjuster(DependencyMap &Dependencies)
Definition: Modularize.cpp:355
static cl::opt< std::string > ModuleMapPath("module-map-path", cl::init(""), cl::desc("Turn on module map output and specify output path or file name." " If no path is specified and if prefix option is specified," " use prefix for file path."))
static cl::list< std::string > ListFileNames(cl::Positional, cl::value_desc("list"), cl::desc("<list of one or more header list files>"), cl::CommaSeparated)
bool TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C, Expr *Init)
Definition: Modularize.cpp:571
ModularizeFrontendActionFactory(EntityMap &Entities, PreprocessorTracker &preprocessorTracker, int &HadErrors)
Definition: Modularize.cpp:720
friend bool operator>(const HeaderEntry &X, const HeaderEntry &Y)
Definition: Modularize.cpp:472
ModularizeUtilities class definition.
friend bool operator>=(const HeaderEntry &X, const HeaderEntry &Y)
Definition: Modularize.cpp:478
void mergeCurHeaderContents()
Definition: Modularize.cpp:506
bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS)
Definition: Modularize.cpp:746
static ClangTidyModuleRegistry::Add< AbseilModule > X("abseil-module", "Add Abseil checks.")
const FileEntry * File
Definition: Modularize.cpp:384
BindArgumentKind Kind
static cl::opt< bool > BlockCheckHeaderListOnly("block-check-header-list-only", cl::init(false), cl::desc("Only warn if #include directives are inside extern or namespace" " blocks if the included header is in the header list."))
Context Ctx
static std::string findInputFile(const CommandLineArguments &CLArgs)
Definition: Modularize.cpp:339
Location Loc
Definition: Modularize.cpp:438
friend bool operator<(const Location &X, const Location &Y)
Definition: Modularize.cpp:413
void add(const std::string &Name, enum Entry::EntryKind Kind, Location Loc)
Definition: Modularize.cpp:489
bool VisitNamespaceDecl(const NamespaceDecl *D)
Definition: Modularize.cpp:773
bool TraverseStmt(Stmt *S)
Definition: Modularize.cpp:551
bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo)
Definition: Modularize.cpp:749
bool TraverseConstructorInitializer(CXXCtorInitializer *Init)
Definition: Modularize.cpp:761
bool VisitNamespaceDecl(const NamespaceDecl *D)
Definition: Modularize.cpp:600
std::unique_ptr< clang::ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Definition: Modularize.cpp:798
friend bool operator==(const HeaderEntry &X, const HeaderEntry &Y)
Definition: Modularize.cpp:463
std::unique_ptr< FrontendAction > create() override
Definition: Modularize.cpp:726
bool createModuleMap(llvm::StringRef ModuleMapPath, llvm::ArrayRef< std::string > HeaderFileNames, llvm::ArrayRef< std::string > ProblemFileNames, DependencyMap &Dependencies, llvm::StringRef HeaderPrefix, llvm::StringRef RootModuleName)
Create the module map file.
friend bool operator<(const HeaderEntry &X, const HeaderEntry &Y)
Definition: Modularize.cpp:469
static constexpr llvm::StringLiteral Name
bool TraverseStmt(Stmt *S)
Definition: Modularize.cpp:742
int main(int Argc, const char **Argv)
Definition: Modularize.cpp:812
const Decl * D
Definition: XRefs.cpp:849
bool VisitLinkageSpecDecl(LinkageSpecDecl *D)
Definition: Modularize.cpp:768
bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS)
Definition: Modularize.cpp:554
~CollectEntitiesConsumer() override
Definition: Modularize.cpp:666
std::string CommandLine
Definition: Modularize.cpp:336
bool TraverseTypeLoc(TypeLoc TL)
Definition: Modularize.cpp:553
llvm::StringMap< DependentsVector > DependencyMap
Definition: Modularize.h:32
static cl::opt< bool > NoCoverageCheck("no-coverage-check", cl::init(false), cl::desc("Don't do the coverage check."))
static cl::list< std::string > IncludePaths("I", cl::desc("Include path for coverage check."), cl::ZeroOrMore, cl::value_desc("path"))
bool TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C, Expr *Init)
Definition: Modularize.cpp:762
friend bool operator!=(const HeaderEntry &X, const HeaderEntry &Y)
Definition: Modularize.cpp:466
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::Optional< std::string > getCanonicalPath(const FileEntry *F, const SourceManager &SourceMgr)
Get the canonical path of F.
Definition: SourceCode.cpp:625
static cl::opt< std::string > RootModule("root-module", cl::init(""), cl::desc("Specify the name of the root module."))
Macro expansions and preprocessor conditional consistency checker.
std::string Name
Definition: Modularize.cpp:460
friend bool operator>=(const Location &X, const Location &Y)
Definition: Modularize.cpp:424
friend bool operator<=(const HeaderEntry &X, const HeaderEntry &Y)
Definition: Modularize.cpp:475
friend bool operator==(const Location &X, const Location &Y)
Definition: Modularize.cpp:405
bool VisitLinkageSpecDecl(LinkageSpecDecl *D)
Definition: Modularize.cpp:577
bool TraverseTemplateName(TemplateName Template)
Definition: Modularize.cpp:561
Location(SourceManager &SM, SourceLocation Loc)
Definition: Modularize.cpp:389
unsigned Line
Definition: Modularize.cpp:385
bool TraverseType(QualType T)
Definition: Modularize.cpp:743
static cl::opt< std::string > ProblemFilesList("problem-files-list", cl::init(""), cl::desc("List of files with compilation or modularization problems for" " assistant mode. This will be excluded."))
std::vector< HeaderEntry > HeaderContents
Definition: Modularize.cpp:483
bool VisitNamedDecl(NamedDecl *ND)
Definition: Modularize.cpp:612
CollectEntitiesConsumer(EntityMap &Entities, PreprocessorTracker &preprocessorTracker, Preprocessor &PP, StringRef InFile, int &HadErrors)
Definition: Modularize.cpp:658
Preprocessor tracker for modularize.
bool TraverseTypeLoc(TypeLoc TL)
Definition: Modularize.cpp:744
bool TraverseType(QualType T)
Definition: Modularize.cpp:552
bool TraverseTemplateArgument(const TemplateArgument &Arg)
Definition: Modularize.cpp:753
bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS)
Definition: Modularize.cpp:555
bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc)
Definition: Modularize.cpp:563
CollectEntitiesVisitor(SourceManager &SM, EntityMap &Entities, Preprocessor &PP, PreprocessorTracker &PPTracker, int &HadErrors)
Definition: Modularize.cpp:545
bool TraverseTemplateArgument(const TemplateArgument &Arg)
Definition: Modularize.cpp:562
const SymbolIndex * Index
Definition: Dexp.cpp:84
bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS)
Definition: Modularize.cpp:745