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