1 //===- CheckerRegistry.cpp - Maintains all available checkers -------------===// 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 #include "clang/StaticAnalyzer/Frontend/CheckerRegistry.h" 10 #include "clang/Basic/Diagnostic.h" 11 #include "clang/Basic/LLVM.h" 12 #include "clang/Driver/DriverDiagnostic.h" 13 #include "clang/Frontend/FrontendDiagnostic.h" 14 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" 15 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h" 16 #include "clang/StaticAnalyzer/Core/CheckerManager.h" 17 #include "llvm/ADT/STLExtras.h" 18 #include "llvm/ADT/SetVector.h" 19 #include "llvm/ADT/StringMap.h" 20 #include "llvm/ADT/StringRef.h" 21 #include "llvm/Support/DynamicLibrary.h" 22 #include "llvm/Support/Path.h" 23 #include "llvm/Support/raw_ostream.h" 24 #include <algorithm> 25 26 using namespace clang; 27 using namespace ento; 28 using llvm::sys::DynamicLibrary; 29 30 //===----------------------------------------------------------------------===// 31 // Utilities. 32 //===----------------------------------------------------------------------===// 33 34 using RegisterCheckersFn = void (*)(CheckerRegistry &); 35 36 static bool isCompatibleAPIVersion(const char *VersionString) { 37 // If the version string is null, its not an analyzer plugin. 38 if (!VersionString) 39 return false; 40 41 // For now, none of the static analyzer API is considered stable. 42 // Versions must match exactly. 43 return strcmp(VersionString, CLANG_ANALYZER_API_VERSION_STRING) == 0; 44 } 45 46 namespace { 47 template <class T> struct FullNameLT { 48 bool operator()(const T &Lhs, const T &Rhs) { 49 return Lhs.FullName < Rhs.FullName; 50 } 51 }; 52 53 using PackageNameLT = FullNameLT<CheckerRegistry::PackageInfo>; 54 using CheckerNameLT = FullNameLT<CheckerRegistry::CheckerInfo>; 55 } // end of anonymous namespace 56 57 template <class CheckerOrPackageInfoList> 58 static std::conditional_t<std::is_const<CheckerOrPackageInfoList>::value, 59 typename CheckerOrPackageInfoList::const_iterator, 60 typename CheckerOrPackageInfoList::iterator> 61 binaryFind(CheckerOrPackageInfoList &Collection, StringRef FullName) { 62 63 using CheckerOrPackage = typename CheckerOrPackageInfoList::value_type; 64 using CheckerOrPackageFullNameLT = FullNameLT<CheckerOrPackage>; 65 66 assert(llvm::is_sorted(Collection, CheckerOrPackageFullNameLT{}) && 67 "In order to efficiently gather checkers/packages, this function " 68 "expects them to be already sorted!"); 69 70 return llvm::lower_bound(Collection, CheckerOrPackage(FullName), 71 CheckerOrPackageFullNameLT{}); 72 } 73 74 static constexpr char PackageSeparator = '.'; 75 76 static bool isInPackage(const CheckerRegistry::CheckerInfo &Checker, 77 StringRef PackageName) { 78 // Does the checker's full name have the package as a prefix? 79 if (!Checker.FullName.startswith(PackageName)) 80 return false; 81 82 // Is the package actually just the name of a specific checker? 83 if (Checker.FullName.size() == PackageName.size()) 84 return true; 85 86 // Is the checker in the package (or a subpackage)? 87 if (Checker.FullName[PackageName.size()] == PackageSeparator) 88 return true; 89 90 return false; 91 } 92 93 //===----------------------------------------------------------------------===// 94 // Methods of CmdLineOption, PackageInfo and CheckerInfo. 95 //===----------------------------------------------------------------------===// 96 97 LLVM_DUMP_METHOD void 98 CheckerRegistry::CmdLineOption::dumpToStream(llvm::raw_ostream &Out) const { 99 // The description can be just checked in Checkers.inc, the point here is to 100 // debug whether we succeeded in parsing it. 101 Out << OptionName << " (" << OptionType << ", " 102 << (IsHidden ? "hidden, " : "") << DevelopmentStatus << ") default: \"" 103 << DefaultValStr; 104 } 105 106 static StringRef toString(CheckerRegistry::StateFromCmdLine Kind) { 107 switch (Kind) { 108 case CheckerRegistry::StateFromCmdLine::State_Disabled: 109 return "Disabled"; 110 case CheckerRegistry::StateFromCmdLine::State_Enabled: 111 return "Enabled"; 112 case CheckerRegistry::StateFromCmdLine::State_Unspecified: 113 return "Unspecified"; 114 } 115 } 116 117 LLVM_DUMP_METHOD void 118 CheckerRegistry::CheckerInfo::dumpToStream(llvm::raw_ostream &Out) const { 119 // The description can be just checked in Checkers.inc, the point here is to 120 // debug whether we succeeded in parsing it. Same with documentation uri. 121 Out << FullName << " (" << toString(State) << (IsHidden ? ", hidden" : "") 122 << ")\n"; 123 Out << " Options:\n"; 124 for (const CmdLineOption &Option : CmdLineOptions) { 125 Out << " "; 126 Option.dumpToStream(Out); 127 Out << '\n'; 128 } 129 Out << " Dependencies:\n"; 130 for (const CheckerInfo *Dependency : Dependencies) { 131 Out << " " << Dependency->FullName << '\n'; 132 } 133 } 134 135 LLVM_DUMP_METHOD void 136 CheckerRegistry::PackageInfo::dumpToStream(llvm::raw_ostream &Out) const { 137 Out << FullName << "\n"; 138 Out << " Options:\n"; 139 for (const CmdLineOption &Option : CmdLineOptions) { 140 Out << " "; 141 Option.dumpToStream(Out); 142 Out << '\n'; 143 } 144 } 145 146 //===----------------------------------------------------------------------===// 147 // Methods of CheckerRegistry. 148 //===----------------------------------------------------------------------===// 149 150 CheckerRegistry::CheckerInfoListRange 151 CheckerRegistry::getMutableCheckersForCmdLineArg(StringRef CmdLineArg) { 152 auto It = binaryFind(Checkers, CmdLineArg); 153 154 if (!isInPackage(*It, CmdLineArg)) 155 return {Checkers.end(), Checkers.end()}; 156 157 // See how large the package is. 158 // If the package doesn't exist, assume the option refers to a single 159 // checker. 160 size_t Size = 1; 161 llvm::StringMap<size_t>::const_iterator PackageSize = 162 PackageSizes.find(CmdLineArg); 163 164 if (PackageSize != PackageSizes.end()) 165 Size = PackageSize->getValue(); 166 167 return {It, It + Size}; 168 } 169 170 CheckerRegistry::CheckerRegistry( 171 ArrayRef<std::string> Plugins, DiagnosticsEngine &Diags, 172 AnalyzerOptions &AnOpts, 173 ArrayRef<std::function<void(CheckerRegistry &)>> CheckerRegistrationFns) 174 : Diags(Diags), AnOpts(AnOpts) { 175 176 // Register builtin checkers. 177 #define GET_CHECKERS 178 #define CHECKER(FULLNAME, CLASS, HELPTEXT, DOC_URI, IS_HIDDEN) \ 179 addChecker(register##CLASS, shouldRegister##CLASS, FULLNAME, HELPTEXT, \ 180 DOC_URI, IS_HIDDEN); 181 182 #define GET_PACKAGES 183 #define PACKAGE(FULLNAME) addPackage(FULLNAME); 184 185 #include "clang/StaticAnalyzer/Checkers/Checkers.inc" 186 #undef CHECKER 187 #undef GET_CHECKERS 188 #undef PACKAGE 189 #undef GET_PACKAGES 190 191 // Register checkers from plugins. 192 for (const std::string &Plugin : Plugins) { 193 // Get access to the plugin. 194 std::string ErrorMsg; 195 DynamicLibrary Lib = 196 DynamicLibrary::getPermanentLibrary(Plugin.c_str(), &ErrorMsg); 197 if (!Lib.isValid()) { 198 Diags.Report(diag::err_fe_unable_to_load_plugin) << Plugin << ErrorMsg; 199 continue; 200 } 201 202 // See if its compatible with this build of clang. 203 const char *PluginAPIVersion = static_cast<const char *>( 204 Lib.getAddressOfSymbol("clang_analyzerAPIVersionString")); 205 206 if (!isCompatibleAPIVersion(PluginAPIVersion)) { 207 Diags.Report(diag::warn_incompatible_analyzer_plugin_api) 208 << llvm::sys::path::filename(Plugin); 209 Diags.Report(diag::note_incompatible_analyzer_plugin_api) 210 << CLANG_ANALYZER_API_VERSION_STRING << PluginAPIVersion; 211 continue; 212 } 213 214 // Register its checkers. 215 RegisterCheckersFn RegisterPluginCheckers = 216 reinterpret_cast<RegisterCheckersFn>( 217 Lib.getAddressOfSymbol("clang_registerCheckers")); 218 if (RegisterPluginCheckers) 219 RegisterPluginCheckers(*this); 220 } 221 222 // Register statically linked checkers, that aren't generated from the tblgen 223 // file, but rather passed their registry function as a parameter in 224 // checkerRegistrationFns. 225 226 for (const auto &Fn : CheckerRegistrationFns) 227 Fn(*this); 228 229 // Sort checkers for efficient collection. 230 // FIXME: Alphabetical sort puts 'experimental' in the middle. 231 // Would it be better to name it '~experimental' or something else 232 // that's ASCIIbetically last? 233 llvm::sort(Packages, PackageNameLT{}); 234 llvm::sort(Checkers, CheckerNameLT{}); 235 236 #define GET_CHECKER_DEPENDENCIES 237 238 #define CHECKER_DEPENDENCY(FULLNAME, DEPENDENCY) \ 239 addDependency(FULLNAME, DEPENDENCY); 240 241 #define GET_CHECKER_OPTIONS 242 #define CHECKER_OPTION(TYPE, FULLNAME, CMDFLAG, DESC, DEFAULT_VAL, \ 243 DEVELOPMENT_STATUS, IS_HIDDEN) \ 244 addCheckerOption(TYPE, FULLNAME, CMDFLAG, DEFAULT_VAL, DESC, \ 245 DEVELOPMENT_STATUS, IS_HIDDEN); 246 247 #define GET_PACKAGE_OPTIONS 248 #define PACKAGE_OPTION(TYPE, FULLNAME, CMDFLAG, DESC, DEFAULT_VAL, \ 249 DEVELOPMENT_STATUS, IS_HIDDEN) \ 250 addPackageOption(TYPE, FULLNAME, CMDFLAG, DEFAULT_VAL, DESC, \ 251 DEVELOPMENT_STATUS, IS_HIDDEN); 252 253 #include "clang/StaticAnalyzer/Checkers/Checkers.inc" 254 #undef CHECKER_DEPENDENCY 255 #undef GET_CHECKER_DEPENDENCIES 256 #undef CHECKER_OPTION 257 #undef GET_CHECKER_OPTIONS 258 #undef PACKAGE_OPTION 259 #undef GET_PACKAGE_OPTIONS 260 261 resolveDependencies(); 262 resolveCheckerAndPackageOptions(); 263 264 // Parse '-analyzer-checker' and '-analyzer-disable-checker' options from the 265 // command line. 266 for (const std::pair<std::string, bool> &Opt : AnOpts.CheckersAndPackages) { 267 CheckerInfoListRange CheckerForCmdLineArg = 268 getMutableCheckersForCmdLineArg(Opt.first); 269 270 if (CheckerForCmdLineArg.begin() == CheckerForCmdLineArg.end()) { 271 Diags.Report(diag::err_unknown_analyzer_checker_or_package) << Opt.first; 272 Diags.Report(diag::note_suggest_disabling_all_checkers); 273 } 274 275 for (CheckerInfo &checker : CheckerForCmdLineArg) { 276 checker.State = Opt.second ? StateFromCmdLine::State_Enabled 277 : StateFromCmdLine::State_Disabled; 278 } 279 } 280 validateCheckerOptions(); 281 } 282 283 /// Collects dependenies in \p enabledCheckers. Return None on failure. 284 LLVM_NODISCARD 285 static llvm::Optional<CheckerRegistry::CheckerInfoSet> 286 collectDependencies(const CheckerRegistry::CheckerInfo &checker, 287 const CheckerManager &Mgr); 288 289 void CheckerRegistry::initializeRegistry(const CheckerManager &Mgr) { 290 for (const CheckerInfo &Checker : Checkers) { 291 if (!Checker.isEnabled(Mgr)) 292 continue; 293 294 // Recursively enable its dependencies. 295 llvm::Optional<CheckerInfoSet> Deps = collectDependencies(Checker, Mgr); 296 297 if (!Deps) { 298 // If we failed to enable any of the dependencies, don't enable this 299 // checker. 300 continue; 301 } 302 303 // Note that set_union also preserves the order of insertion. 304 EnabledCheckers.set_union(*Deps); 305 306 // Enable the checker. 307 EnabledCheckers.insert(&Checker); 308 } 309 } 310 311 /// Collects dependencies in \p ret, returns false on failure. 312 static bool 313 collectDependenciesImpl(const CheckerRegistry::ConstCheckerInfoList &Deps, 314 const CheckerManager &Mgr, 315 CheckerRegistry::CheckerInfoSet &Ret); 316 317 /// Collects dependenies in \p enabledCheckers. Return None on failure. 318 LLVM_NODISCARD 319 static llvm::Optional<CheckerRegistry::CheckerInfoSet> 320 collectDependencies(const CheckerRegistry::CheckerInfo &checker, 321 const CheckerManager &Mgr) { 322 323 CheckerRegistry::CheckerInfoSet Ret; 324 // Add dependencies to the enabled checkers only if all of them can be 325 // enabled. 326 if (!collectDependenciesImpl(checker.Dependencies, Mgr, Ret)) 327 return None; 328 329 return Ret; 330 } 331 332 static bool 333 collectDependenciesImpl(const CheckerRegistry::ConstCheckerInfoList &Deps, 334 const CheckerManager &Mgr, 335 CheckerRegistry::CheckerInfoSet &Ret) { 336 337 for (const CheckerRegistry::CheckerInfo *Dependency : Deps) { 338 339 if (Dependency->isDisabled(Mgr)) 340 return false; 341 342 // Collect dependencies recursively. 343 if (!collectDependenciesImpl(Dependency->Dependencies, Mgr, Ret)) 344 return false; 345 346 Ret.insert(Dependency); 347 } 348 349 return true; 350 } 351 352 void CheckerRegistry::resolveDependencies() { 353 for (const std::pair<StringRef, StringRef> &Entry : Dependencies) { 354 auto CheckerIt = binaryFind(Checkers, Entry.first); 355 assert(CheckerIt != Checkers.end() && CheckerIt->FullName == Entry.first && 356 "Failed to find the checker while attempting to set up its " 357 "dependencies!"); 358 359 auto DependencyIt = binaryFind(Checkers, Entry.second); 360 assert(DependencyIt != Checkers.end() && 361 DependencyIt->FullName == Entry.second && 362 "Failed to find the dependency of a checker!"); 363 364 CheckerIt->Dependencies.emplace_back(&*DependencyIt); 365 } 366 } 367 368 void CheckerRegistry::addDependency(StringRef FullName, StringRef Dependency) { 369 Dependencies.emplace_back(FullName, Dependency); 370 } 371 372 /// Insert the checker/package option to AnalyzerOptions' config table, and 373 /// validate it, if the user supplied it on the command line. 374 static void insertAndValidate(StringRef FullName, 375 const CheckerRegistry::CmdLineOption &Option, 376 AnalyzerOptions &AnOpts, 377 DiagnosticsEngine &Diags) { 378 379 std::string FullOption = (FullName + ":" + Option.OptionName).str(); 380 381 auto It = 382 AnOpts.Config.insert({FullOption, std::string(Option.DefaultValStr)}); 383 384 // Insertation was successful -- CmdLineOption's constructor will validate 385 // whether values received from plugins or TableGen files are correct. 386 if (It.second) 387 return; 388 389 // Insertion failed, the user supplied this package/checker option on the 390 // command line. If the supplied value is invalid, we'll restore the option 391 // to it's default value, and if we're in non-compatibility mode, we'll also 392 // emit an error. 393 394 StringRef SuppliedValue = It.first->getValue(); 395 396 if (Option.OptionType == "bool") { 397 if (SuppliedValue != "true" && SuppliedValue != "false") { 398 if (AnOpts.ShouldEmitErrorsOnInvalidConfigValue) { 399 Diags.Report(diag::err_analyzer_checker_option_invalid_input) 400 << FullOption << "a boolean value"; 401 } 402 403 It.first->setValue(std::string(Option.DefaultValStr)); 404 } 405 return; 406 } 407 408 if (Option.OptionType == "int") { 409 int Tmp; 410 bool HasFailed = SuppliedValue.getAsInteger(0, Tmp); 411 if (HasFailed) { 412 if (AnOpts.ShouldEmitErrorsOnInvalidConfigValue) { 413 Diags.Report(diag::err_analyzer_checker_option_invalid_input) 414 << FullOption << "an integer value"; 415 } 416 417 It.first->setValue(std::string(Option.DefaultValStr)); 418 } 419 return; 420 } 421 } 422 423 template <class T> 424 static void 425 insertOptionToCollection(StringRef FullName, T &Collection, 426 const CheckerRegistry::CmdLineOption &Option, 427 AnalyzerOptions &AnOpts, DiagnosticsEngine &Diags) { 428 auto It = binaryFind(Collection, FullName); 429 assert(It != Collection.end() && 430 "Failed to find the checker while attempting to add a command line " 431 "option to it!"); 432 433 insertAndValidate(FullName, Option, AnOpts, Diags); 434 435 It->CmdLineOptions.emplace_back(Option); 436 } 437 438 void CheckerRegistry::resolveCheckerAndPackageOptions() { 439 for (const std::pair<StringRef, CmdLineOption> &CheckerOptEntry : 440 CheckerOptions) { 441 insertOptionToCollection(CheckerOptEntry.first, Checkers, 442 CheckerOptEntry.second, AnOpts, Diags); 443 } 444 445 for (const std::pair<StringRef, CmdLineOption> &PackageOptEntry : 446 PackageOptions) { 447 insertOptionToCollection(PackageOptEntry.first, Packages, 448 PackageOptEntry.second, AnOpts, Diags); 449 } 450 } 451 452 void CheckerRegistry::addPackage(StringRef FullName) { 453 Packages.emplace_back(PackageInfo(FullName)); 454 } 455 456 void CheckerRegistry::addPackageOption(StringRef OptionType, 457 StringRef PackageFullName, 458 StringRef OptionName, 459 StringRef DefaultValStr, 460 StringRef Description, 461 StringRef DevelopmentStatus, 462 bool IsHidden) { 463 PackageOptions.emplace_back( 464 PackageFullName, CmdLineOption{OptionType, OptionName, DefaultValStr, 465 Description, DevelopmentStatus, IsHidden}); 466 } 467 468 void CheckerRegistry::addChecker(InitializationFunction Rfn, 469 ShouldRegisterFunction Sfn, StringRef Name, 470 StringRef Desc, StringRef DocsUri, 471 bool IsHidden) { 472 Checkers.emplace_back(Rfn, Sfn, Name, Desc, DocsUri, IsHidden); 473 474 // Record the presence of the checker in its packages. 475 StringRef PackageName, LeafName; 476 std::tie(PackageName, LeafName) = Name.rsplit(PackageSeparator); 477 while (!LeafName.empty()) { 478 PackageSizes[PackageName] += 1; 479 std::tie(PackageName, LeafName) = PackageName.rsplit(PackageSeparator); 480 } 481 } 482 483 void CheckerRegistry::addCheckerOption(StringRef OptionType, 484 StringRef CheckerFullName, 485 StringRef OptionName, 486 StringRef DefaultValStr, 487 StringRef Description, 488 StringRef DevelopmentStatus, 489 bool IsHidden) { 490 CheckerOptions.emplace_back( 491 CheckerFullName, CmdLineOption{OptionType, OptionName, DefaultValStr, 492 Description, DevelopmentStatus, IsHidden}); 493 } 494 495 void CheckerRegistry::initializeManager(CheckerManager &CheckerMgr) const { 496 // Initialize the CheckerManager with all enabled checkers. 497 for (const auto *Checker : EnabledCheckers) { 498 CheckerMgr.setCurrentCheckerName(CheckerNameRef(Checker->FullName)); 499 Checker->Initialize(CheckerMgr); 500 } 501 } 502 503 static void 504 isOptionContainedIn(const CheckerRegistry::CmdLineOptionList &OptionList, 505 StringRef SuppliedChecker, StringRef SuppliedOption, 506 const AnalyzerOptions &AnOpts, DiagnosticsEngine &Diags) { 507 508 if (!AnOpts.ShouldEmitErrorsOnInvalidConfigValue) 509 return; 510 511 using CmdLineOption = CheckerRegistry::CmdLineOption; 512 513 auto SameOptName = [SuppliedOption](const CmdLineOption &Opt) { 514 return Opt.OptionName == SuppliedOption; 515 }; 516 517 auto OptionIt = llvm::find_if(OptionList, SameOptName); 518 519 if (OptionIt == OptionList.end()) { 520 Diags.Report(diag::err_analyzer_checker_option_unknown) 521 << SuppliedChecker << SuppliedOption; 522 return; 523 } 524 } 525 526 void CheckerRegistry::validateCheckerOptions() const { 527 for (const auto &Config : AnOpts.Config) { 528 529 StringRef SuppliedCheckerOrPackage; 530 StringRef SuppliedOption; 531 std::tie(SuppliedCheckerOrPackage, SuppliedOption) = 532 Config.getKey().split(':'); 533 534 if (SuppliedOption.empty()) 535 continue; 536 537 // AnalyzerOptions' config table contains the user input, so an entry could 538 // look like this: 539 // 540 // cor:NoFalsePositives=true 541 // 542 // Since lower_bound would look for the first element *not less* than "cor", 543 // it would return with an iterator to the first checker in the core, so we 544 // we really have to use find here, which uses operator==. 545 auto CheckerIt = 546 llvm::find(Checkers, CheckerInfo(SuppliedCheckerOrPackage)); 547 if (CheckerIt != Checkers.end()) { 548 isOptionContainedIn(CheckerIt->CmdLineOptions, SuppliedCheckerOrPackage, 549 SuppliedOption, AnOpts, Diags); 550 continue; 551 } 552 553 auto PackageIt = 554 llvm::find(Packages, PackageInfo(SuppliedCheckerOrPackage)); 555 if (PackageIt != Packages.end()) { 556 isOptionContainedIn(PackageIt->CmdLineOptions, SuppliedCheckerOrPackage, 557 SuppliedOption, AnOpts, Diags); 558 continue; 559 } 560 561 Diags.Report(diag::err_unknown_analyzer_checker_or_package) 562 << SuppliedCheckerOrPackage; 563 } 564 } 565 566 //===----------------------------------------------------------------------===// 567 // Printing functions. 568 //===----------------------------------------------------------------------===// 569 570 void CheckerRegistry::printCheckerWithDescList(raw_ostream &Out, 571 size_t MaxNameChars) const { 572 // FIXME: Print available packages. 573 574 Out << "CHECKERS:\n"; 575 576 // Find the maximum option length. 577 size_t OptionFieldWidth = 0; 578 for (const auto &Checker : Checkers) { 579 // Limit the amount of padding we are willing to give up for alignment. 580 // Package.Name Description [Hidden] 581 size_t NameLength = Checker.FullName.size(); 582 if (NameLength <= MaxNameChars) 583 OptionFieldWidth = std::max(OptionFieldWidth, NameLength); 584 } 585 586 const size_t InitialPad = 2; 587 588 auto Print = [=](llvm::raw_ostream &Out, const CheckerInfo &Checker, 589 StringRef Description) { 590 AnalyzerOptions::printFormattedEntry(Out, {Checker.FullName, Description}, 591 InitialPad, OptionFieldWidth); 592 Out << '\n'; 593 }; 594 595 for (const auto &Checker : Checkers) { 596 // The order of this if branches is significant, we wouldn't like to display 597 // developer checkers even in the alpha output. For example, 598 // alpha.cplusplus.IteratorModeling is a modeling checker, hence it's hidden 599 // by default, and users (even when the user is a developer of an alpha 600 // checker) shouldn't normally tinker with whether they should be enabled. 601 602 if (Checker.IsHidden) { 603 if (AnOpts.ShowCheckerHelpDeveloper) 604 Print(Out, Checker, Checker.Desc); 605 continue; 606 } 607 608 if (Checker.FullName.startswith("alpha")) { 609 if (AnOpts.ShowCheckerHelpAlpha) 610 Print(Out, Checker, 611 ("(Enable only for development!) " + Checker.Desc).str()); 612 continue; 613 } 614 615 if (AnOpts.ShowCheckerHelp) 616 Print(Out, Checker, Checker.Desc); 617 } 618 } 619 620 void CheckerRegistry::printEnabledCheckerList(raw_ostream &Out) const { 621 for (const auto *i : EnabledCheckers) 622 Out << i->FullName << '\n'; 623 } 624 625 void CheckerRegistry::printCheckerOptionList(raw_ostream &Out) const { 626 Out << "OVERVIEW: Clang Static Analyzer Checker and Package Option List\n\n"; 627 Out << "USAGE: -analyzer-config <OPTION1=VALUE,OPTION2=VALUE,...>\n\n"; 628 Out << " -analyzer-config OPTION1=VALUE, -analyzer-config " 629 "OPTION2=VALUE, ...\n\n"; 630 Out << "OPTIONS:\n\n"; 631 632 std::multimap<StringRef, const CmdLineOption &> OptionMap; 633 634 for (const CheckerInfo &Checker : Checkers) { 635 for (const CmdLineOption &Option : Checker.CmdLineOptions) { 636 OptionMap.insert({Checker.FullName, Option}); 637 } 638 } 639 640 for (const PackageInfo &Package : Packages) { 641 for (const CmdLineOption &Option : Package.CmdLineOptions) { 642 OptionMap.insert({Package.FullName, Option}); 643 } 644 } 645 646 auto Print = [] (llvm::raw_ostream &Out, StringRef FullOption, StringRef Desc) { 647 AnalyzerOptions::printFormattedEntry(Out, {FullOption, Desc}, 648 /*InitialPad*/ 2, 649 /*EntryWidth*/ 50, 650 /*MinLineWidth*/ 90); 651 Out << "\n\n"; 652 }; 653 for (const std::pair<const StringRef, const CmdLineOption &> &Entry : 654 OptionMap) { 655 const CmdLineOption &Option = Entry.second; 656 std::string FullOption = (Entry.first + ":" + Option.OptionName).str(); 657 658 std::string Desc = 659 ("(" + Option.OptionType + ") " + Option.Description + " (default: " + 660 (Option.DefaultValStr.empty() ? "\"\"" : Option.DefaultValStr) + ")") 661 .str(); 662 663 // The list of these if branches is significant, we wouldn't like to 664 // display hidden alpha checker options for 665 // -analyzer-checker-option-help-alpha. 666 667 if (Option.IsHidden) { 668 if (AnOpts.ShowCheckerOptionDeveloperList) 669 Print(Out, FullOption, Desc); 670 continue; 671 } 672 673 if (Option.DevelopmentStatus == "alpha" || 674 Entry.first.startswith("alpha")) { 675 if (AnOpts.ShowCheckerOptionAlphaList) 676 Print(Out, FullOption, 677 llvm::Twine("(Enable only for development!) " + Desc).str()); 678 continue; 679 } 680 681 if (AnOpts.ShowCheckerOptionList) 682 Print(Out, FullOption, Desc); 683 } 684 } 685