clang-tools 20.0.0git
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 containing 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.modulemap 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.modulemap file. Note
58// that you will likely need to edit this file to suit the needs of
59// your 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
67// module.modulemap 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 subdirectories involved, assuming that the
188// subdirectories contain headers to be grouped into a module, but still with
189// individual modules for the headers in the subdirectory.
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 <iterator>
251#include <map>
252#include <string>
253#include <vector>
254
255using namespace clang;
256using namespace clang::driver;
257using namespace clang::driver::options;
258using namespace clang::tooling;
259using namespace llvm;
260using namespace llvm::opt;
261using namespace Modularize;
262
263// Option to specify a file name for a list of header files to check.
264static 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.
270static 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.
275static 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.
284static 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.
292static 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.
299static cl::opt<std::string>
300RootModule("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.
307static cl::opt<bool>
308BlockCheckHeaderListOnly("block-check-header-list-only", cl::init(false),
309cl::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.
313static cl::list<std::string>
314 IncludePaths("I", cl::desc("Include path for coverage check."),
315 cl::value_desc("path"));
316
317// Option for disabling the coverage check.
318static cl::opt<bool> NoCoverageCheck("no-coverage-check",
319 cl::desc("Don't do the coverage check."));
320
321// Option for just doing the coverage check.
322static cl::opt<bool>
323CoverageCheckOnly("coverage-check-only", cl::init(false),
324cl::desc("Only do the coverage check."));
325
326// Option for displaying lists of good, bad, and mixed files.
327static cl::opt<bool>
328DisplayFileLists("display-file-lists", cl::init(false),
329cl::desc("Display lists of good files (no compile errors), problem files,"
330 " and a combined list with problem files preceded by a '#'."));
331
332// Save the program name for error messages.
333const char *Argv0;
334// Save the command line for comments.
335std::string CommandLine;
336
337// Helper function for finding the input file in an arguments list.
338static std::string findInputFile(const CommandLineArguments &CLArgs) {
339 llvm::opt::Visibility VisibilityMask(options::CC1Option);
340 unsigned MissingArgIndex, MissingArgCount;
341 SmallVector<const char *, 256> Argv;
342 for (auto I = CLArgs.begin(), E = CLArgs.end(); I != E; ++I)
343 Argv.push_back(I->c_str());
344 InputArgList Args = getDriverOptTable().ParseArgs(
345 Argv, MissingArgIndex, MissingArgCount, VisibilityMask);
346 std::vector<std::string> Inputs = Args.getAllArgValues(OPT_INPUT);
347 return ModularizeUtilities::getCanonicalPath(Inputs.back());
348}
349
350// This arguments adjuster inserts "-include (file)" arguments for header
351// dependencies. It also inserts a "-w" option and a "-x c++",
352// if no other "-x" option is present.
353static ArgumentsAdjuster
355 return [&Dependencies](const CommandLineArguments &Args,
356 StringRef /*unused*/) {
357 std::string InputFile = findInputFile(Args);
358 DependentsVector &FileDependents = Dependencies[InputFile];
359 CommandLineArguments NewArgs(Args);
360 if (int Count = FileDependents.size()) {
361 for (int Index = 0; Index < Count; ++Index) {
362 NewArgs.push_back("-include");
363 std::string File(std::string("\"") + FileDependents[Index] +
364 std::string("\""));
365 NewArgs.push_back(FileDependents[Index]);
366 }
367 }
368 // Ignore warnings. (Insert after "clang_tool" at beginning.)
369 NewArgs.insert(NewArgs.begin() + 1, "-w");
370 // Since we are compiling .h files, assume C++ unless given a -x option.
371 if (!llvm::is_contained(NewArgs, "-x")) {
372 NewArgs.insert(NewArgs.begin() + 2, "-x");
373 NewArgs.insert(NewArgs.begin() + 3, "c++");
374 }
375 return NewArgs;
376 };
377}
378
379// FIXME: The Location class seems to be something that we might
380// want to design to be applicable to a wider range of tools, and stick it
381// somewhere into Tooling/ in mainline
382struct Location {
383 OptionalFileEntryRef File;
384 unsigned Line, Column;
385
386 Location() : File(), Line(), Column() {}
387
388 Location(SourceManager &SM, SourceLocation Loc) : File(), Line(), Column() {
389 Loc = SM.getExpansionLoc(Loc);
390 if (Loc.isInvalid())
391 return;
392
393 std::pair<FileID, unsigned> Decomposed = SM.getDecomposedLoc(Loc);
394 File = SM.getFileEntryRefForID(Decomposed.first);
395 if (!File)
396 return;
397
398 Line = SM.getLineNumber(Decomposed.first, Decomposed.second);
399 Column = SM.getColumnNumber(Decomposed.first, Decomposed.second);
400 }
401
402 operator bool() const { return File != nullptr; }
403
404 friend bool operator==(const Location &X, const Location &Y) {
405 return X.File == Y.File && X.Line == Y.Line && X.Column == Y.Column;
406 }
407
408 friend bool operator!=(const Location &X, const Location &Y) {
409 return !(X == Y);
410 }
411
412 friend bool operator<(const Location &X, const Location &Y) {
413 if (X.File != Y.File)
414 return X.File < Y.File;
415 if (X.Line != Y.Line)
416 return X.Line < Y.Line;
417 return X.Column < Y.Column;
418 }
419 friend bool operator>(const Location &X, const Location &Y) { return Y < X; }
420 friend bool operator<=(const Location &X, const Location &Y) {
421 return !(Y < X);
422 }
423 friend bool operator>=(const Location &X, const Location &Y) {
424 return !(X < Y);
425 }
426};
427
428struct Entry {
433
436
438
439 StringRef getKindName() { return getKindName(Kind); }
440 static StringRef getKindName(EntryKind kind);
441};
442
443// Return a string representing the given kind.
445 switch (kind) {
446 case EK_Tag:
447 return "tag";
448 case EK_Value:
449 return "value";
450 case EK_Macro:
451 return "macro";
452 case EK_NumberOfKinds:
453 break;
454 }
455 llvm_unreachable("invalid Entry kind");
456}
457
459 std::string Name;
461
462 friend bool operator==(const HeaderEntry &X, const HeaderEntry &Y) {
463 return X.Loc == Y.Loc && X.Name == Y.Name;
464 }
465 friend bool operator!=(const HeaderEntry &X, const HeaderEntry &Y) {
466 return !(X == Y);
467 }
468 friend bool operator<(const HeaderEntry &X, const HeaderEntry &Y) {
469 return X.Loc < Y.Loc || (X.Loc == Y.Loc && X.Name < Y.Name);
470 }
471 friend bool operator>(const HeaderEntry &X, const HeaderEntry &Y) {
472 return Y < X;
473 }
474 friend bool operator<=(const HeaderEntry &X, const HeaderEntry &Y) {
475 return !(Y < X);
476 }
477 friend bool operator>=(const HeaderEntry &X, const HeaderEntry &Y) {
478 return !(X < Y);
479 }
480};
481
482typedef std::vector<HeaderEntry> HeaderContents;
483
484class EntityMap : public std::map<std::string, SmallVector<Entry, 2>> {
485public:
486 DenseMap<FileEntryRef, HeaderContents> HeaderContentMismatches;
487
488 void add(const std::string &Name, enum Entry::EntryKind Kind, Location Loc) {
489 // Record this entity in its header.
490 HeaderEntry HE = { Name, Loc };
491 CurHeaderContents[*Loc.File].push_back(HE);
492
493 // Check whether we've seen this entry before.
494 SmallVector<Entry, 2> &Entries = (*this)[Name];
495 for (unsigned I = 0, N = Entries.size(); I != N; ++I) {
496 if (Entries[I].Kind == Kind && Entries[I].Loc == Loc)
497 return;
498 }
499
500 // We have not seen this entry before; record it.
501 Entry E = { Kind, Loc };
502 Entries.push_back(E);
503 }
504
506 for (auto H = CurHeaderContents.begin(), HEnd = CurHeaderContents.end();
507 H != HEnd; ++H) {
508 // Sort contents.
509 llvm::sort(H->second);
510
511 // Record this header and its contents if we haven't seen it before.
512 auto [KnownH, Inserted] = AllHeaderContents.insert(*H);
513 if (Inserted)
514 continue;
515
516 // If the header contents are the same, we're done.
517 if (H->second == KnownH->second)
518 continue;
519
520 // Determine what changed.
521 std::set_symmetric_difference(
522 H->second.begin(), H->second.end(), KnownH->second.begin(),
523 KnownH->second.end(),
524 std::back_inserter(HeaderContentMismatches[H->first]));
525 }
526
527 CurHeaderContents.clear();
528 }
529
530private:
531 DenseMap<FileEntryRef, HeaderContents> CurHeaderContents;
532 DenseMap<FileEntryRef, HeaderContents> AllHeaderContents;
533};
534
536 : public RecursiveASTVisitor<CollectEntitiesVisitor> {
537public:
538 CollectEntitiesVisitor(SourceManager &SM, EntityMap &Entities,
539 Preprocessor &PP, PreprocessorTracker &PPTracker,
540 int &HadErrors)
541 : SM(SM), Entities(Entities), PP(PP), PPTracker(PPTracker),
542 HadErrors(HadErrors) {}
543
544 bool TraverseStmt(Stmt *S) { return true; }
545 bool TraverseType(QualType T) { return true; }
546 bool TraverseTypeLoc(TypeLoc TL) { return true; }
547 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) { return true; }
548 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
549 return true;
550 }
551 bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo) {
552 return true;
553 }
554 bool TraverseTemplateName(TemplateName Template) { return true; }
555 bool TraverseTemplateArgument(const TemplateArgument &Arg) { return true; }
556 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) {
557 return true;
558 }
559 bool TraverseTemplateArguments(ArrayRef<TemplateArgument>) { return true; }
560 bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { return true; }
561 bool TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C,
562 Expr *Init) {
563 return true;
564 }
565
566 // Check 'extern "*" {}' block for #include directives.
567 bool VisitLinkageSpecDecl(LinkageSpecDecl *D) {
568 // Bail if not a block.
569 if (!D->hasBraces())
570 return true;
571 SourceRange BlockRange = D->getSourceRange();
572 const char *LinkageLabel;
573 switch (D->getLanguage()) {
574 case LinkageSpecLanguageIDs::C:
575 LinkageLabel = "extern \"C\" {}";
576 break;
577 case LinkageSpecLanguageIDs::CXX:
578 LinkageLabel = "extern \"C++\" {}";
579 break;
580 }
581 if (!PPTracker.checkForIncludesInBlock(PP, BlockRange, LinkageLabel,
582 errs()))
583 HadErrors = 1;
584 return true;
585 }
586
587 // Check 'namespace (name) {}' block for #include directives.
588 bool VisitNamespaceDecl(const NamespaceDecl *D) {
589 SourceRange BlockRange = D->getSourceRange();
590 std::string Label("namespace ");
591 Label += D->getName();
592 Label += " {}";
593 if (!PPTracker.checkForIncludesInBlock(PP, BlockRange, Label.c_str(),
594 errs()))
595 HadErrors = 1;
596 return true;
597 }
598
599 // Collect definition entities.
600 bool VisitNamedDecl(NamedDecl *ND) {
601 // We only care about file-context variables.
602 if (!ND->getDeclContext()->isFileContext())
603 return true;
604
605 // Skip declarations that tend to be properly multiply-declared.
606 if (isa<NamespaceDecl>(ND) || isa<UsingDirectiveDecl>(ND) ||
607 isa<NamespaceAliasDecl>(ND) ||
608 isa<ClassTemplateSpecializationDecl>(ND) || isa<UsingDecl>(ND) ||
609 isa<ClassTemplateDecl>(ND) || isa<TemplateTypeParmDecl>(ND) ||
610 isa<TypeAliasTemplateDecl>(ND) || isa<UsingShadowDecl>(ND) ||
611 isa<FunctionDecl>(ND) || isa<FunctionTemplateDecl>(ND) ||
612 (isa<TagDecl>(ND) &&
613 !cast<TagDecl>(ND)->isThisDeclarationADefinition()))
614 return true;
615
616 // Skip anonymous declarations.
617 if (!ND->getDeclName())
618 return true;
619
620 // Get the qualified name.
621 std::string Name;
622 llvm::raw_string_ostream OS(Name);
623 ND->printQualifiedName(OS);
624 if (Name.empty())
625 return true;
626
627 Location Loc(SM, ND->getLocation());
628 if (!Loc)
629 return true;
630
631 Entities.add(Name, isa<TagDecl>(ND) ? Entry::EK_Tag : Entry::EK_Value, Loc);
632 return true;
633 }
634
635private:
636 SourceManager &SM;
637 EntityMap &Entities;
638 Preprocessor &PP;
639 PreprocessorTracker &PPTracker;
640 int &HadErrors;
641};
642
644public:
646 PreprocessorTracker &preprocessorTracker,
647 Preprocessor &PP, StringRef InFile, int &HadErrors)
648 : Entities(Entities), PPTracker(preprocessorTracker), PP(PP),
649 HadErrors(HadErrors) {
650 PPTracker.handlePreprocessorEntry(PP, InFile);
651 }
652
654
655 void HandleTranslationUnit(ASTContext &Ctx) override {
656 SourceManager &SM = Ctx.getSourceManager();
657
658 // Collect declared entities.
659 CollectEntitiesVisitor(SM, Entities, PP, PPTracker, HadErrors)
660 .TraverseDecl(Ctx.getTranslationUnitDecl());
661
662 // Collect macro definitions.
663 for (Preprocessor::macro_iterator M = PP.macro_begin(),
664 MEnd = PP.macro_end();
665 M != MEnd; ++M) {
666 Location Loc(SM, M->second.getLatest()->getLocation());
667 if (!Loc)
668 continue;
669
670 Entities.add(M->first->getName().str(), Entry::EK_Macro, Loc);
671 }
672
673 // Merge header contents.
674 Entities.mergeCurHeaderContents();
675 }
676
677private:
678 EntityMap &Entities;
679 PreprocessorTracker &PPTracker;
680 Preprocessor &PP;
681 int &HadErrors;
682};
683
685public:
687 PreprocessorTracker &preprocessorTracker,
688 int &HadErrors)
689 : Entities(Entities), PPTracker(preprocessorTracker),
690 HadErrors(HadErrors) {}
691
692protected:
693 std::unique_ptr<clang::ASTConsumer>
694 CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override {
695 return std::make_unique<CollectEntitiesConsumer>(
696 Entities, PPTracker, CI.getPreprocessor(), InFile, HadErrors);
697 }
698
699private:
700 EntityMap &Entities;
701 PreprocessorTracker &PPTracker;
702 int &HadErrors;
703};
704
706public:
708 PreprocessorTracker &preprocessorTracker,
709 int &HadErrors)
710 : Entities(Entities), PPTracker(preprocessorTracker),
711 HadErrors(HadErrors) {}
712
713 std::unique_ptr<FrontendAction> create() override {
714 return std::make_unique<CollectEntitiesAction>(Entities, PPTracker,
715 HadErrors);
716 }
717
718private:
719 EntityMap &Entities;
720 PreprocessorTracker &PPTracker;
721 int &HadErrors;
722};
723
725 : public RecursiveASTVisitor<CompileCheckVisitor> {
726public:
728
729 bool TraverseStmt(Stmt *S) { return true; }
730 bool TraverseType(QualType T) { return true; }
731 bool TraverseTypeLoc(TypeLoc TL) { return true; }
732 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS) { return true; }
733 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
734 return true;
735 }
736 bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo) {
737 return true;
738 }
739 bool TraverseTemplateName(TemplateName Template) { return true; }
740 bool TraverseTemplateArgument(const TemplateArgument &Arg) { return true; }
741 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) {
742 return true;
743 }
744 bool TraverseTemplateArguments(ArrayRef<TemplateArgument>) { return true; }
745 bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { return true; }
746 bool TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C,
747 Expr *Init) {
748 return true;
749 }
750
751 // Check 'extern "*" {}' block for #include directives.
752 bool VisitLinkageSpecDecl(LinkageSpecDecl *D) {
753 return true;
754 }
755
756 // Check 'namespace (name) {}' block for #include directives.
757 bool VisitNamespaceDecl(const NamespaceDecl *D) {
758 return true;
759 }
760
761 // Collect definition entities.
762 bool VisitNamedDecl(NamedDecl *ND) {
763 return true;
764 }
765};
766
768public:
770
771 void HandleTranslationUnit(ASTContext &Ctx) override {
772 CompileCheckVisitor().TraverseDecl(Ctx.getTranslationUnitDecl());
773 }
774};
775
777public:
779
780protected:
781 std::unique_ptr<clang::ASTConsumer>
782 CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override {
783 return std::make_unique<CompileCheckConsumer>();
784 }
785};
786
788public:
790
791 std::unique_ptr<FrontendAction> create() override {
792 return std::make_unique<CompileCheckAction>();
793 }
794};
795
796int main(int Argc, const char **Argv) {
797
798 // Save program name for error messages.
799 Argv0 = Argv[0];
800
801 // Save program arguments for use in module.modulemap comment.
802 CommandLine = std::string(sys::path::stem(sys::path::filename(Argv0)));
803 for (int ArgIndex = 1; ArgIndex < Argc; ArgIndex++) {
804 CommandLine.append(" ");
805 CommandLine.append(Argv[ArgIndex]);
806 }
807
808 // This causes options to be parsed.
809 cl::ParseCommandLineOptions(Argc, Argv, "modularize.\n");
810
811 // No go if we have no header list file.
812 if (ListFileNames.size() == 0) {
813 cl::PrintHelpMessage();
814 return 1;
815 }
816
817 std::unique_ptr<ModularizeUtilities> ModUtil;
818 int HadErrors = 0;
819
820 ModUtil.reset(
823
824 // Get header file names and dependencies.
825 if (ModUtil->loadAllHeaderListsAndDependencies())
826 HadErrors = 1;
827
828 // If we are in assistant mode, output the module map and quit.
829 if (ModuleMapPath.length() != 0) {
830 if (!createModuleMap(ModuleMapPath, ModUtil->HeaderFileNames,
831 ModUtil->ProblemFileNames,
832 ModUtil->Dependencies, HeaderPrefix, RootModule))
833 return 1; // Failed.
834 return 0; // Success - Skip checks in assistant mode.
835 }
836
837 // If we're doing module maps.
838 if (!NoCoverageCheck && ModUtil->HasModuleMap) {
839 // Do coverage check.
840 if (ModUtil->doCoverageCheck(IncludePaths, CommandLine))
841 HadErrors = 1;
842 }
843
844 // Bail early if only doing the coverage check.
846 return HadErrors;
847
848 // Create the compilation database.
849 SmallString<256> PathBuf;
850 sys::fs::current_path(PathBuf);
851 std::unique_ptr<CompilationDatabase> Compilations;
852 Compilations.reset(
853 new FixedCompilationDatabase(Twine(PathBuf), CC1Arguments));
854
855 // Create preprocessor tracker, to watch for macro and conditional problems.
856 std::unique_ptr<PreprocessorTracker> PPTracker(
857 PreprocessorTracker::create(ModUtil->HeaderFileNames,
858 BlockCheckHeaderListOnly));
859
860 // Coolect entities here.
861 EntityMap Entities;
862
863 // Because we can't easily determine which files failed
864 // during the tool run, if we're collecting the file lists
865 // for display, we do a first compile pass on individual
866 // files to find which ones don't compile stand-alone.
867 if (DisplayFileLists) {
868 // First, make a pass to just get compile errors.
869 for (auto &CompileCheckFile : ModUtil->HeaderFileNames) {
870 llvm::SmallVector<std::string, 32> CompileCheckFileArray;
871 CompileCheckFileArray.push_back(CompileCheckFile);
872 ClangTool CompileCheckTool(*Compilations, CompileCheckFileArray);
873 CompileCheckTool.appendArgumentsAdjuster(
874 getModularizeArgumentsAdjuster(ModUtil->Dependencies));
875 int CompileCheckFileErrors = 0;
876 // FIXME: use newFrontendActionFactory.
877 CompileCheckFrontendActionFactory CompileCheckFactory;
878 CompileCheckFileErrors |= CompileCheckTool.run(&CompileCheckFactory);
879 if (CompileCheckFileErrors != 0) {
880 ModUtil->addUniqueProblemFile(CompileCheckFile); // Save problem file.
881 HadErrors |= 1;
882 }
883 else
884 ModUtil->addNoCompileErrorsFile(CompileCheckFile); // Save good file.
885 }
886 }
887
888 // Then we make another pass on the good files to do the rest of the work.
889 ClangTool Tool(*Compilations,
890 (DisplayFileLists ? ModUtil->GoodFileNames : ModUtil->HeaderFileNames));
891 Tool.appendArgumentsAdjuster(
892 getModularizeArgumentsAdjuster(ModUtil->Dependencies));
893 ModularizeFrontendActionFactory Factory(Entities, *PPTracker, HadErrors);
894 HadErrors |= Tool.run(&Factory);
895
896 // Create a place to save duplicate entity locations, separate bins per kind.
897 typedef SmallVector<Location, 8> LocationArray;
898 typedef SmallVector<LocationArray, Entry::EK_NumberOfKinds> EntryBinArray;
899 EntryBinArray EntryBins;
900 int KindIndex;
901 for (KindIndex = 0; KindIndex < Entry::EK_NumberOfKinds; ++KindIndex) {
902 LocationArray Array;
903 EntryBins.push_back(Array);
904 }
905
906 // Check for the same entity being defined in multiple places.
907 for (EntityMap::iterator E = Entities.begin(), EEnd = Entities.end();
908 E != EEnd; ++E) {
909 // If only one occurrence, exit early.
910 if (E->second.size() == 1)
911 continue;
912 // Clear entity locations.
913 for (EntryBinArray::iterator CI = EntryBins.begin(), CE = EntryBins.end();
914 CI != CE; ++CI) {
915 CI->clear();
916 }
917 // Walk the entities of a single name, collecting the locations,
918 // separated into separate bins.
919 for (unsigned I = 0, N = E->second.size(); I != N; ++I) {
920 EntryBins[E->second[I].Kind].push_back(E->second[I].Loc);
921 }
922 // Report any duplicate entity definition errors.
923 int KindIndex = 0;
924 for (EntryBinArray::iterator DI = EntryBins.begin(), DE = EntryBins.end();
925 DI != DE; ++DI, ++KindIndex) {
926 int ECount = DI->size();
927 // If only 1 occurrence of this entity, skip it, we only report duplicates.
928 if (ECount <= 1)
929 continue;
930 LocationArray::iterator FI = DI->begin();
931 StringRef kindName = Entry::getKindName((Entry::EntryKind)KindIndex);
932 errs() << "error: " << kindName << " '" << E->first
933 << "' defined at multiple locations:\n";
934 for (LocationArray::iterator FE = DI->end(); FI != FE; ++FI) {
935 errs() << " " << FI->File->getName() << ":" << FI->Line << ":"
936 << FI->Column << "\n";
937 ModUtil->addUniqueProblemFile(std::string(FI->File->getName()));
938 }
939 HadErrors = 1;
940 }
941 }
942
943 // Complain about macro instance in header files that differ based on how
944 // they are included.
945 if (PPTracker->reportInconsistentMacros(errs()))
946 HadErrors = 1;
947
948 // Complain about preprocessor conditional directives in header files that
949 // differ based on how they are included.
950 if (PPTracker->reportInconsistentConditionals(errs()))
951 HadErrors = 1;
952
953 // Complain about any headers that have contents that differ based on how
954 // they are included.
955 // FIXME: Could we provide information about which preprocessor conditionals
956 // are involved?
957 for (auto H = Entities.HeaderContentMismatches.begin(),
958 HEnd = Entities.HeaderContentMismatches.end();
959 H != HEnd; ++H) {
960 if (H->second.empty()) {
961 errs() << "internal error: phantom header content mismatch\n";
962 continue;
963 }
964
965 HadErrors = 1;
966 ModUtil->addUniqueProblemFile(std::string(H->first.getName()));
967 errs() << "error: header '" << H->first.getName()
968 << "' has different contents depending on how it was included.\n";
969 for (unsigned I = 0, N = H->second.size(); I != N; ++I) {
970 errs() << "note: '" << H->second[I].Name << "' in "
971 << H->second[I].Loc.File->getName() << " at "
972 << H->second[I].Loc.Line << ":" << H->second[I].Loc.Column
973 << " not always provided\n";
974 }
975 }
976
977 if (DisplayFileLists) {
978 ModUtil->displayProblemFiles();
979 ModUtil->displayGoodFiles();
980 ModUtil->displayCombinedFiles();
981 }
982
983 return HadErrors;
984}
const Expr * E
BindArgumentKind Kind
CaptureExpr CE
llvm::SmallString< 256U > Name
llvm::raw_ostream & OS
const Criteria C
std::string Label
SourceLocation Loc
int X
ModularizeUtilities class definition.
static cl::list< std::string > IncludePaths("I", cl::desc("Include path for coverage check."), cl::value_desc("path"))
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."))
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 '#'."))
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:482
static cl::opt< bool > CoverageCheckOnly("coverage-check-only", cl::init(false), cl::desc("Only do the coverage check."))
static cl::opt< std::string > RootModule("root-module", cl::init(""), cl::desc("Specify the name of the root module."))
static cl::opt< bool > NoCoverageCheck("no-coverage-check", cl::desc("Don't do the coverage check."))
static ArgumentsAdjuster getModularizeArgumentsAdjuster(DependencyMap &Dependencies)
Definition: Modularize.cpp:354
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)
const char * Argv0
Definition: Modularize.cpp:333
std::string CommandLine
Definition: Modularize.cpp:335
int main(int Argc, const char **Argv)
Definition: Modularize.cpp:796
static std::string findInputFile(const CommandLineArguments &CLArgs)
Definition: Modularize.cpp:338
static cl::list< std::string > CC1Arguments(cl::ConsumeAfter, cl::desc("<arguments to be passed to front end>..."))
Common definitions for Modularize.
llvm::SmallVector< std::string, 4 > DependentsVector
Definition: Modularize.h:31
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.
llvm::StringMap< DependentsVector > DependencyMap
Definition: Modularize.h:32
Macro expansions and preprocessor conditional consistency checker.
const google::protobuf::Message & M
Definition: Server.cpp:356
std::unique_ptr< CompilerInvocation > CI
llvm::json::Object Args
Definition: Trace.cpp:138
std::unique_ptr< clang::ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Definition: Modularize.cpp:694
CollectEntitiesAction(EntityMap &Entities, PreprocessorTracker &preprocessorTracker, int &HadErrors)
Definition: Modularize.cpp:686
CollectEntitiesConsumer(EntityMap &Entities, PreprocessorTracker &preprocessorTracker, Preprocessor &PP, StringRef InFile, int &HadErrors)
Definition: Modularize.cpp:645
void HandleTranslationUnit(ASTContext &Ctx) override
Definition: Modularize.cpp:655
~CollectEntitiesConsumer() override
Definition: Modularize.cpp:653
bool TraverseStmt(Stmt *S)
Definition: Modularize.cpp:544
bool VisitNamespaceDecl(const NamespaceDecl *D)
Definition: Modularize.cpp:588
bool TraverseConstructorInitializer(CXXCtorInitializer *Init)
Definition: Modularize.cpp:560
bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS)
Definition: Modularize.cpp:548
bool TraverseTemplateArguments(ArrayRef< TemplateArgument >)
Definition: Modularize.cpp:559
bool TraverseTypeLoc(TypeLoc TL)
Definition: Modularize.cpp:546
bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo)
Definition: Modularize.cpp:551
CollectEntitiesVisitor(SourceManager &SM, EntityMap &Entities, Preprocessor &PP, PreprocessorTracker &PPTracker, int &HadErrors)
Definition: Modularize.cpp:538
bool TraverseTemplateArgument(const TemplateArgument &Arg)
Definition: Modularize.cpp:555
bool VisitNamedDecl(NamedDecl *ND)
Definition: Modularize.cpp:600
bool TraverseType(QualType T)
Definition: Modularize.cpp:545
bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc)
Definition: Modularize.cpp:556
bool TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C, Expr *Init)
Definition: Modularize.cpp:561
bool VisitLinkageSpecDecl(LinkageSpecDecl *D)
Definition: Modularize.cpp:567
bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS)
Definition: Modularize.cpp:547
bool TraverseTemplateName(TemplateName Template)
Definition: Modularize.cpp:554
std::unique_ptr< clang::ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Definition: Modularize.cpp:782
void HandleTranslationUnit(ASTContext &Ctx) override
Definition: Modularize.cpp:771
std::unique_ptr< FrontendAction > create() override
Definition: Modularize.cpp:791
bool TraverseStmt(Stmt *S)
Definition: Modularize.cpp:729
bool TraverseType(QualType T)
Definition: Modularize.cpp:730
bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo)
Definition: Modularize.cpp:736
bool TraverseTypeLoc(TypeLoc TL)
Definition: Modularize.cpp:731
bool TraverseTemplateArguments(ArrayRef< TemplateArgument >)
Definition: Modularize.cpp:744
bool TraverseTemplateArgument(const TemplateArgument &Arg)
Definition: Modularize.cpp:740
bool VisitLinkageSpecDecl(LinkageSpecDecl *D)
Definition: Modularize.cpp:752
bool VisitNamespaceDecl(const NamespaceDecl *D)
Definition: Modularize.cpp:757
bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS)
Definition: Modularize.cpp:732
bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc)
Definition: Modularize.cpp:741
bool TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C, Expr *Init)
Definition: Modularize.cpp:746
bool VisitNamedDecl(NamedDecl *ND)
Definition: Modularize.cpp:762
bool TraverseTemplateName(TemplateName Template)
Definition: Modularize.cpp:739
bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS)
Definition: Modularize.cpp:733
bool TraverseConstructorInitializer(CXXCtorInitializer *Init)
Definition: Modularize.cpp:745
void mergeCurHeaderContents()
Definition: Modularize.cpp:505
void add(const std::string &Name, enum Entry::EntryKind Kind, Location Loc)
Definition: Modularize.cpp:488
DenseMap< FileEntryRef, HeaderContents > HeaderContentMismatches
Definition: Modularize.cpp:486
std::unique_ptr< FrontendAction > create() override
Definition: Modularize.cpp:713
ModularizeFrontendActionFactory(EntityMap &Entities, PreprocessorTracker &preprocessorTracker, int &HadErrors)
Definition: Modularize.cpp:707
static std::string getCanonicalPath(llvm::StringRef FilePath)
Convert header path to canonical form.
static ModularizeUtilities * createModularizeUtilities(std::vector< std::string > &InputPaths, llvm::StringRef Prefix, llvm::StringRef ProblemFilesListPath)
Create instance of ModularizeUtilities.
Preprocessor tracker for modularize.
virtual bool checkForIncludesInBlock(clang::Preprocessor &PP, clang::SourceRange BlockSourceRange, const char *BlockIdentifierMessage, llvm::raw_ostream &OS)=0
virtual void handlePreprocessorEntry(clang::Preprocessor &PP, llvm::StringRef RootHeaderFile)=0
static PreprocessorTracker * create(llvm::SmallVector< std::string, 32 > &Headers, bool DoBlockCheckHeaderListOnly)
virtual void handlePreprocessorExit()=0
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Some operations such as code completion produce a set of candidates.
Definition: Generators.h:58
StringRef getKindName()
Definition: Modularize.cpp:439
enum Entry::EntryKind Kind
@ EK_Macro
Definition: Modularize.cpp:432
@ EK_Value
Definition: Modularize.cpp:431
@ EK_NumberOfKinds
Definition: Modularize.cpp:434
Location Loc
Definition: Modularize.cpp:437
Location Loc
Definition: Modularize.cpp:460
friend bool operator>(const HeaderEntry &X, const HeaderEntry &Y)
Definition: Modularize.cpp:471
friend bool operator<=(const HeaderEntry &X, const HeaderEntry &Y)
Definition: Modularize.cpp:474
std::string Name
Definition: Modularize.cpp:459
friend bool operator==(const HeaderEntry &X, const HeaderEntry &Y)
Definition: Modularize.cpp:462
friend bool operator<(const HeaderEntry &X, const HeaderEntry &Y)
Definition: Modularize.cpp:468
friend bool operator!=(const HeaderEntry &X, const HeaderEntry &Y)
Definition: Modularize.cpp:465
friend bool operator>=(const HeaderEntry &X, const HeaderEntry &Y)
Definition: Modularize.cpp:477
friend bool operator<=(const Location &X, const Location &Y)
Definition: Modularize.cpp:420
friend bool operator==(const Location &X, const Location &Y)
Definition: Modularize.cpp:404
friend bool operator>=(const Location &X, const Location &Y)
Definition: Modularize.cpp:423
unsigned Line
Definition: Modularize.cpp:384
Location(SourceManager &SM, SourceLocation Loc)
Definition: Modularize.cpp:388
friend bool operator<(const Location &X, const Location &Y)
Definition: Modularize.cpp:412
unsigned Column
Definition: Modularize.cpp:384
friend bool operator!=(const Location &X, const Location &Y)
Definition: Modularize.cpp:408
OptionalFileEntryRef File
Definition: Modularize.cpp:383
friend bool operator>(const Location &X, const Location &Y)
Definition: Modularize.cpp:419