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 llvm_unreachable("Unhandled CheckerRegistry::StateFromCmdLine enum"); 116 } 117 118 LLVM_DUMP_METHOD void 119 CheckerRegistry::CheckerInfo::dumpToStream(llvm::raw_ostream &Out) const { 120 // The description can be just checked in Checkers.inc, the point here is to 121 // debug whether we succeeded in parsing it. Same with documentation uri. 122 Out << FullName << " (" << toString(State) << (IsHidden ? ", hidden" : "") 123 << ")\n"; 124 Out << " Options:\n"; 125 for (const CmdLineOption &Option : CmdLineOptions) { 126 Out << " "; 127 Option.dumpToStream(Out); 128 Out << '\n'; 129 } 130 Out << " Dependencies:\n"; 131 for (const CheckerInfo *Dependency : Dependencies) { 132 Out << " " << Dependency->FullName << '\n'; 133 } 134 Out << " Weak dependencies:\n"; 135 for (const CheckerInfo *Dependency : WeakDependencies) { 136 Out << " " << Dependency->FullName << '\n'; 137 } 138 } 139 140 LLVM_DUMP_METHOD void 141 CheckerRegistry::PackageInfo::dumpToStream(llvm::raw_ostream &Out) const { 142 Out << FullName << "\n"; 143 Out << " Options:\n"; 144 for (const CmdLineOption &Option : CmdLineOptions) { 145 Out << " "; 146 Option.dumpToStream(Out); 147 Out << '\n'; 148 } 149 } 150 151 //===----------------------------------------------------------------------===// 152 // Methods of CheckerRegistry. 153 //===----------------------------------------------------------------------===// 154 155 CheckerRegistry::CheckerInfoListRange 156 CheckerRegistry::getMutableCheckersForCmdLineArg(StringRef CmdLineArg) { 157 auto It = binaryFind(Checkers, CmdLineArg); 158 159 if (!isInPackage(*It, CmdLineArg)) 160 return {Checkers.end(), Checkers.end()}; 161 162 // See how large the package is. 163 // If the package doesn't exist, assume the option refers to a single 164 // checker. 165 size_t Size = 1; 166 llvm::StringMap<size_t>::const_iterator PackageSize = 167 PackageSizes.find(CmdLineArg); 168 169 if (PackageSize != PackageSizes.end()) 170 Size = PackageSize->getValue(); 171 172 return {It, It + Size}; 173 } 174 175 CheckerRegistry::CheckerRegistry( 176 ArrayRef<std::string> Plugins, DiagnosticsEngine &Diags, 177 AnalyzerOptions &AnOpts, 178 ArrayRef<std::function<void(CheckerRegistry &)>> CheckerRegistrationFns) 179 : Diags(Diags), AnOpts(AnOpts) { 180 181 // Register builtin checkers. 182 #define GET_CHECKERS 183 #define CHECKER(FULLNAME, CLASS, HELPTEXT, DOC_URI, IS_HIDDEN) \ 184 addChecker(register##CLASS, shouldRegister##CLASS, FULLNAME, HELPTEXT, \ 185 DOC_URI, IS_HIDDEN); 186 187 #define GET_PACKAGES 188 #define PACKAGE(FULLNAME) addPackage(FULLNAME); 189 190 #include "clang/StaticAnalyzer/Checkers/Checkers.inc" 191 #undef CHECKER 192 #undef GET_CHECKERS 193 #undef PACKAGE 194 #undef GET_PACKAGES 195 196 // Register checkers from plugins. 197 for (const std::string &Plugin : Plugins) { 198 // Get access to the plugin. 199 std::string ErrorMsg; 200 DynamicLibrary Lib = 201 DynamicLibrary::getPermanentLibrary(Plugin.c_str(), &ErrorMsg); 202 if (!Lib.isValid()) { 203 Diags.Report(diag::err_fe_unable_to_load_plugin) << Plugin << ErrorMsg; 204 continue; 205 } 206 207 // See if its compatible with this build of clang. 208 const char *PluginAPIVersion = static_cast<const char *>( 209 Lib.getAddressOfSymbol("clang_analyzerAPIVersionString")); 210 211 if (!isCompatibleAPIVersion(PluginAPIVersion)) { 212 Diags.Report(diag::warn_incompatible_analyzer_plugin_api) 213 << llvm::sys::path::filename(Plugin); 214 Diags.Report(diag::note_incompatible_analyzer_plugin_api) 215 << CLANG_ANALYZER_API_VERSION_STRING << PluginAPIVersion; 216 continue; 217 } 218 219 // Register its checkers. 220 RegisterCheckersFn RegisterPluginCheckers = 221 reinterpret_cast<RegisterCheckersFn>( 222 Lib.getAddressOfSymbol("clang_registerCheckers")); 223 if (RegisterPluginCheckers) 224 RegisterPluginCheckers(*this); 225 } 226 227 // Register statically linked checkers, that aren't generated from the tblgen 228 // file, but rather passed their registry function as a parameter in 229 // checkerRegistrationFns. 230 231 for (const auto &Fn : CheckerRegistrationFns) 232 Fn(*this); 233 234 // Sort checkers for efficient collection. 235 // FIXME: Alphabetical sort puts 'experimental' in the middle. 236 // Would it be better to name it '~experimental' or something else 237 // that's ASCIIbetically last? 238 llvm::sort(Packages, PackageNameLT{}); 239 llvm::sort(Checkers, CheckerNameLT{}); 240 241 #define GET_CHECKER_DEPENDENCIES 242 243 #define CHECKER_DEPENDENCY(FULLNAME, DEPENDENCY) \ 244 addDependency(FULLNAME, DEPENDENCY); 245 246 #define GET_CHECKER_WEAK_DEPENDENCIES 247 248 #define CHECKER_WEAK_DEPENDENCY(FULLNAME, DEPENDENCY) \ 249 addWeakDependency(FULLNAME, DEPENDENCY); 250 251 #define GET_CHECKER_OPTIONS 252 #define CHECKER_OPTION(TYPE, FULLNAME, CMDFLAG, DESC, DEFAULT_VAL, \ 253 DEVELOPMENT_STATUS, IS_HIDDEN) \ 254 addCheckerOption(TYPE, FULLNAME, CMDFLAG, DEFAULT_VAL, DESC, \ 255 DEVELOPMENT_STATUS, IS_HIDDEN); 256 257 #define GET_PACKAGE_OPTIONS 258 #define PACKAGE_OPTION(TYPE, FULLNAME, CMDFLAG, DESC, DEFAULT_VAL, \ 259 DEVELOPMENT_STATUS, IS_HIDDEN) \ 260 addPackageOption(TYPE, FULLNAME, CMDFLAG, DEFAULT_VAL, DESC, \ 261 DEVELOPMENT_STATUS, IS_HIDDEN); 262 263 #include "clang/StaticAnalyzer/Checkers/Checkers.inc" 264 #undef CHECKER_DEPENDENCY 265 #undef GET_CHECKER_DEPENDENCIES 266 #undef CHECKER_WEAK_DEPENDENCY 267 #undef GET_CHECKER_WEAK_DEPENDENCIES 268 #undef CHECKER_OPTION 269 #undef GET_CHECKER_OPTIONS 270 #undef PACKAGE_OPTION 271 #undef GET_PACKAGE_OPTIONS 272 273 resolveDependencies<true>(); 274 resolveDependencies<false>(); 275 276 #ifndef NDEBUG 277 for (auto &DepPair : Dependencies) { 278 for (auto &WeakDepPair : WeakDependencies) { 279 // Some assertions to enforce that strong dependencies are relations in 280 // between purely modeling checkers, and weak dependencies are about 281 // diagnostics. 282 assert(WeakDepPair != DepPair && 283 "A checker cannot strong and weak depend on the same checker!"); 284 assert(WeakDepPair.first != DepPair.second && 285 "A strong dependency mustn't have weak dependencies!"); 286 assert(WeakDepPair.second != DepPair.second && 287 "A strong dependency mustn't be a weak dependency as well!"); 288 } 289 } 290 #endif 291 292 resolveCheckerAndPackageOptions(); 293 294 // Parse '-analyzer-checker' and '-analyzer-disable-checker' options from the 295 // command line. 296 for (const std::pair<std::string, bool> &Opt : AnOpts.CheckersAndPackages) { 297 CheckerInfoListRange CheckerForCmdLineArg = 298 getMutableCheckersForCmdLineArg(Opt.first); 299 300 if (CheckerForCmdLineArg.begin() == CheckerForCmdLineArg.end()) { 301 Diags.Report(diag::err_unknown_analyzer_checker_or_package) << Opt.first; 302 Diags.Report(diag::note_suggest_disabling_all_checkers); 303 } 304 305 for (CheckerInfo &checker : CheckerForCmdLineArg) { 306 checker.State = Opt.second ? StateFromCmdLine::State_Enabled 307 : StateFromCmdLine::State_Disabled; 308 } 309 } 310 validateCheckerOptions(); 311 } 312 313 //===----------------------------------------------------------------------===// 314 // Dependency resolving. 315 //===----------------------------------------------------------------------===// 316 317 template <typename IsEnabledFn> 318 static bool 319 collectStrongDependencies(const CheckerRegistry::ConstCheckerInfoList &Deps, 320 const CheckerManager &Mgr, 321 CheckerRegistry::CheckerInfoSet &Ret, 322 IsEnabledFn IsEnabled); 323 324 /// Collects weak dependencies in \p enabledCheckers. 325 template <typename IsEnabledFn> 326 static void 327 collectWeakDependencies(const CheckerRegistry::ConstCheckerInfoList &Deps, 328 const CheckerManager &Mgr, 329 CheckerRegistry::CheckerInfoSet &Ret, 330 IsEnabledFn IsEnabled); 331 332 void CheckerRegistry::initializeRegistry(const CheckerManager &Mgr) { 333 // First, we calculate the list of enabled checkers as specified by the 334 // invocation. Weak dependencies will not enable their unspecified strong 335 // depenencies, but its only after resolving strong dependencies for all 336 // checkers when we know whether they will be enabled. 337 CheckerInfoSet Tmp; 338 auto IsEnabledFromCmdLine = [&](const CheckerInfo *Checker) { 339 return !Checker->isDisabled(Mgr); 340 }; 341 for (const CheckerInfo &Checker : Checkers) { 342 if (!Checker.isEnabled(Mgr)) 343 continue; 344 345 CheckerInfoSet Deps; 346 if (!collectStrongDependencies(Checker.Dependencies, Mgr, Deps, 347 IsEnabledFromCmdLine)) { 348 // If we failed to enable any of the dependencies, don't enable this 349 // checker. 350 continue; 351 } 352 353 Tmp.insert(Deps.begin(), Deps.end()); 354 355 // Enable the checker. 356 Tmp.insert(&Checker); 357 } 358 359 // Calculate enabled checkers with the correct registration order. As this is 360 // done recursively, its arguably cheaper, but for sure less error prone to 361 // recalculate from scratch. 362 auto IsEnabled = [&](const CheckerInfo *Checker) { 363 return llvm::is_contained(Tmp, Checker); 364 }; 365 for (const CheckerInfo &Checker : Checkers) { 366 if (!Checker.isEnabled(Mgr)) 367 continue; 368 369 CheckerInfoSet Deps; 370 371 collectWeakDependencies(Checker.WeakDependencies, Mgr, Deps, IsEnabled); 372 373 if (!collectStrongDependencies(Checker.Dependencies, Mgr, Deps, 374 IsEnabledFromCmdLine)) { 375 // If we failed to enable any of the dependencies, don't enable this 376 // checker. 377 continue; 378 } 379 380 // Note that set_union also preserves the order of insertion. 381 EnabledCheckers.set_union(Deps); 382 EnabledCheckers.insert(&Checker); 383 } 384 } 385 386 template <typename IsEnabledFn> 387 static bool 388 collectStrongDependencies(const CheckerRegistry::ConstCheckerInfoList &Deps, 389 const CheckerManager &Mgr, 390 CheckerRegistry::CheckerInfoSet &Ret, 391 IsEnabledFn IsEnabled) { 392 393 for (const CheckerRegistry::CheckerInfo *Dependency : Deps) { 394 if (!IsEnabled(Dependency)) 395 return false; 396 397 // Collect dependencies recursively. 398 if (!collectStrongDependencies(Dependency->Dependencies, Mgr, Ret, 399 IsEnabled)) 400 return false; 401 Ret.insert(Dependency); 402 } 403 404 return true; 405 } 406 407 template <typename IsEnabledFn> 408 static void 409 collectWeakDependencies(const CheckerRegistry::ConstCheckerInfoList &WeakDeps, 410 const CheckerManager &Mgr, 411 CheckerRegistry::CheckerInfoSet &Ret, 412 IsEnabledFn IsEnabled) { 413 414 for (const CheckerRegistry::CheckerInfo *Dependency : WeakDeps) { 415 // Don't enable this checker if strong dependencies are unsatisfied, but 416 // assume that weak dependencies are transitive. 417 collectWeakDependencies(Dependency->WeakDependencies, Mgr, Ret, IsEnabled); 418 419 if (IsEnabled(Dependency) && 420 collectStrongDependencies(Dependency->Dependencies, Mgr, Ret, 421 IsEnabled)) 422 Ret.insert(Dependency); 423 } 424 } 425 426 template <bool IsWeak> void CheckerRegistry::resolveDependencies() { 427 for (const std::pair<StringRef, StringRef> &Entry : 428 (IsWeak ? WeakDependencies : Dependencies)) { 429 430 auto CheckerIt = binaryFind(Checkers, Entry.first); 431 assert(CheckerIt != Checkers.end() && CheckerIt->FullName == Entry.first && 432 "Failed to find the checker while attempting to set up its " 433 "dependencies!"); 434 435 auto DependencyIt = binaryFind(Checkers, Entry.second); 436 assert(DependencyIt != Checkers.end() && 437 DependencyIt->FullName == Entry.second && 438 "Failed to find the dependency of a checker!"); 439 440 if (IsWeak) 441 CheckerIt->WeakDependencies.emplace_back(&*DependencyIt); 442 else 443 CheckerIt->Dependencies.emplace_back(&*DependencyIt); 444 } 445 } 446 447 void CheckerRegistry::addDependency(StringRef FullName, StringRef Dependency) { 448 Dependencies.emplace_back(FullName, Dependency); 449 } 450 451 void CheckerRegistry::addWeakDependency(StringRef FullName, 452 StringRef Dependency) { 453 WeakDependencies.emplace_back(FullName, Dependency); 454 } 455 456 //===----------------------------------------------------------------------===// 457 // Checker option resolving and validating. 458 //===----------------------------------------------------------------------===// 459 460 /// Insert the checker/package option to AnalyzerOptions' config table, and 461 /// validate it, if the user supplied it on the command line. 462 static void insertAndValidate(StringRef FullName, 463 const CheckerRegistry::CmdLineOption &Option, 464 AnalyzerOptions &AnOpts, 465 DiagnosticsEngine &Diags) { 466 467 std::string FullOption = (FullName + ":" + Option.OptionName).str(); 468 469 auto It = 470 AnOpts.Config.insert({FullOption, std::string(Option.DefaultValStr)}); 471 472 // Insertation was successful -- CmdLineOption's constructor will validate 473 // whether values received from plugins or TableGen files are correct. 474 if (It.second) 475 return; 476 477 // Insertion failed, the user supplied this package/checker option on the 478 // command line. If the supplied value is invalid, we'll restore the option 479 // to it's default value, and if we're in non-compatibility mode, we'll also 480 // emit an error. 481 482 StringRef SuppliedValue = It.first->getValue(); 483 484 if (Option.OptionType == "bool") { 485 if (SuppliedValue != "true" && SuppliedValue != "false") { 486 if (AnOpts.ShouldEmitErrorsOnInvalidConfigValue) { 487 Diags.Report(diag::err_analyzer_checker_option_invalid_input) 488 << FullOption << "a boolean value"; 489 } 490 491 It.first->setValue(std::string(Option.DefaultValStr)); 492 } 493 return; 494 } 495 496 if (Option.OptionType == "int") { 497 int Tmp; 498 bool HasFailed = SuppliedValue.getAsInteger(0, Tmp); 499 if (HasFailed) { 500 if (AnOpts.ShouldEmitErrorsOnInvalidConfigValue) { 501 Diags.Report(diag::err_analyzer_checker_option_invalid_input) 502 << FullOption << "an integer value"; 503 } 504 505 It.first->setValue(std::string(Option.DefaultValStr)); 506 } 507 return; 508 } 509 } 510 511 template <class T> 512 static void 513 insertOptionToCollection(StringRef FullName, T &Collection, 514 const CheckerRegistry::CmdLineOption &Option, 515 AnalyzerOptions &AnOpts, DiagnosticsEngine &Diags) { 516 auto It = binaryFind(Collection, FullName); 517 assert(It != Collection.end() && 518 "Failed to find the checker while attempting to add a command line " 519 "option to it!"); 520 521 insertAndValidate(FullName, Option, AnOpts, Diags); 522 523 It->CmdLineOptions.emplace_back(Option); 524 } 525 526 void CheckerRegistry::resolveCheckerAndPackageOptions() { 527 for (const std::pair<StringRef, CmdLineOption> &CheckerOptEntry : 528 CheckerOptions) { 529 insertOptionToCollection(CheckerOptEntry.first, Checkers, 530 CheckerOptEntry.second, AnOpts, Diags); 531 } 532 533 for (const std::pair<StringRef, CmdLineOption> &PackageOptEntry : 534 PackageOptions) { 535 insertOptionToCollection(PackageOptEntry.first, Packages, 536 PackageOptEntry.second, AnOpts, Diags); 537 } 538 } 539 540 void CheckerRegistry::addPackage(StringRef FullName) { 541 Packages.emplace_back(PackageInfo(FullName)); 542 } 543 544 void CheckerRegistry::addPackageOption(StringRef OptionType, 545 StringRef PackageFullName, 546 StringRef OptionName, 547 StringRef DefaultValStr, 548 StringRef Description, 549 StringRef DevelopmentStatus, 550 bool IsHidden) { 551 PackageOptions.emplace_back( 552 PackageFullName, CmdLineOption{OptionType, OptionName, DefaultValStr, 553 Description, DevelopmentStatus, IsHidden}); 554 } 555 556 void CheckerRegistry::addChecker(InitializationFunction Rfn, 557 ShouldRegisterFunction Sfn, StringRef Name, 558 StringRef Desc, StringRef DocsUri, 559 bool IsHidden) { 560 Checkers.emplace_back(Rfn, Sfn, Name, Desc, DocsUri, IsHidden); 561 562 // Record the presence of the checker in its packages. 563 StringRef PackageName, LeafName; 564 std::tie(PackageName, LeafName) = Name.rsplit(PackageSeparator); 565 while (!LeafName.empty()) { 566 PackageSizes[PackageName] += 1; 567 std::tie(PackageName, LeafName) = PackageName.rsplit(PackageSeparator); 568 } 569 } 570 571 void CheckerRegistry::addCheckerOption(StringRef OptionType, 572 StringRef CheckerFullName, 573 StringRef OptionName, 574 StringRef DefaultValStr, 575 StringRef Description, 576 StringRef DevelopmentStatus, 577 bool IsHidden) { 578 CheckerOptions.emplace_back( 579 CheckerFullName, CmdLineOption{OptionType, OptionName, DefaultValStr, 580 Description, DevelopmentStatus, IsHidden}); 581 } 582 583 void CheckerRegistry::initializeManager(CheckerManager &CheckerMgr) const { 584 // Initialize the CheckerManager with all enabled checkers. 585 for (const auto *Checker : EnabledCheckers) { 586 CheckerMgr.setCurrentCheckerName(CheckerNameRef(Checker->FullName)); 587 Checker->Initialize(CheckerMgr); 588 } 589 } 590 591 static void 592 isOptionContainedIn(const CheckerRegistry::CmdLineOptionList &OptionList, 593 StringRef SuppliedChecker, StringRef SuppliedOption, 594 const AnalyzerOptions &AnOpts, DiagnosticsEngine &Diags) { 595 596 if (!AnOpts.ShouldEmitErrorsOnInvalidConfigValue) 597 return; 598 599 using CmdLineOption = CheckerRegistry::CmdLineOption; 600 601 auto SameOptName = [SuppliedOption](const CmdLineOption &Opt) { 602 return Opt.OptionName == SuppliedOption; 603 }; 604 605 const auto *OptionIt = llvm::find_if(OptionList, SameOptName); 606 607 if (OptionIt == OptionList.end()) { 608 Diags.Report(diag::err_analyzer_checker_option_unknown) 609 << SuppliedChecker << SuppliedOption; 610 return; 611 } 612 } 613 614 void CheckerRegistry::validateCheckerOptions() const { 615 for (const auto &Config : AnOpts.Config) { 616 617 StringRef SuppliedCheckerOrPackage; 618 StringRef SuppliedOption; 619 std::tie(SuppliedCheckerOrPackage, SuppliedOption) = 620 Config.getKey().split(':'); 621 622 if (SuppliedOption.empty()) 623 continue; 624 625 // AnalyzerOptions' config table contains the user input, so an entry could 626 // look like this: 627 // 628 // cor:NoFalsePositives=true 629 // 630 // Since lower_bound would look for the first element *not less* than "cor", 631 // it would return with an iterator to the first checker in the core, so we 632 // we really have to use find here, which uses operator==. 633 auto CheckerIt = 634 llvm::find(Checkers, CheckerInfo(SuppliedCheckerOrPackage)); 635 if (CheckerIt != Checkers.end()) { 636 isOptionContainedIn(CheckerIt->CmdLineOptions, SuppliedCheckerOrPackage, 637 SuppliedOption, AnOpts, Diags); 638 continue; 639 } 640 641 const auto *PackageIt = 642 llvm::find(Packages, PackageInfo(SuppliedCheckerOrPackage)); 643 if (PackageIt != Packages.end()) { 644 isOptionContainedIn(PackageIt->CmdLineOptions, SuppliedCheckerOrPackage, 645 SuppliedOption, AnOpts, Diags); 646 continue; 647 } 648 649 Diags.Report(diag::err_unknown_analyzer_checker_or_package) 650 << SuppliedCheckerOrPackage; 651 } 652 } 653 654 //===----------------------------------------------------------------------===// 655 // Printing functions. 656 //===----------------------------------------------------------------------===// 657 658 void CheckerRegistry::printCheckerWithDescList(raw_ostream &Out, 659 size_t MaxNameChars) const { 660 // FIXME: Print available packages. 661 662 Out << "CHECKERS:\n"; 663 664 // Find the maximum option length. 665 size_t OptionFieldWidth = 0; 666 for (const auto &Checker : Checkers) { 667 // Limit the amount of padding we are willing to give up for alignment. 668 // Package.Name Description [Hidden] 669 size_t NameLength = Checker.FullName.size(); 670 if (NameLength <= MaxNameChars) 671 OptionFieldWidth = std::max(OptionFieldWidth, NameLength); 672 } 673 674 const size_t InitialPad = 2; 675 676 auto Print = [=](llvm::raw_ostream &Out, const CheckerInfo &Checker, 677 StringRef Description) { 678 AnalyzerOptions::printFormattedEntry(Out, {Checker.FullName, Description}, 679 InitialPad, OptionFieldWidth); 680 Out << '\n'; 681 }; 682 683 for (const auto &Checker : Checkers) { 684 // The order of this if branches is significant, we wouldn't like to display 685 // developer checkers even in the alpha output. For example, 686 // alpha.cplusplus.IteratorModeling is a modeling checker, hence it's hidden 687 // by default, and users (even when the user is a developer of an alpha 688 // checker) shouldn't normally tinker with whether they should be enabled. 689 690 if (Checker.IsHidden) { 691 if (AnOpts.ShowCheckerHelpDeveloper) 692 Print(Out, Checker, Checker.Desc); 693 continue; 694 } 695 696 if (Checker.FullName.startswith("alpha")) { 697 if (AnOpts.ShowCheckerHelpAlpha) 698 Print(Out, Checker, 699 ("(Enable only for development!) " + Checker.Desc).str()); 700 continue; 701 } 702 703 if (AnOpts.ShowCheckerHelp) 704 Print(Out, Checker, Checker.Desc); 705 } 706 } 707 708 void CheckerRegistry::printEnabledCheckerList(raw_ostream &Out) const { 709 for (const auto *i : EnabledCheckers) 710 Out << i->FullName << '\n'; 711 } 712 713 void CheckerRegistry::printCheckerOptionList(raw_ostream &Out) const { 714 Out << "OVERVIEW: Clang Static Analyzer Checker and Package Option List\n\n"; 715 Out << "USAGE: -analyzer-config <OPTION1=VALUE,OPTION2=VALUE,...>\n\n"; 716 Out << " -analyzer-config OPTION1=VALUE, -analyzer-config " 717 "OPTION2=VALUE, ...\n\n"; 718 Out << "OPTIONS:\n\n"; 719 720 std::multimap<StringRef, const CmdLineOption &> OptionMap; 721 722 for (const CheckerInfo &Checker : Checkers) { 723 for (const CmdLineOption &Option : Checker.CmdLineOptions) { 724 OptionMap.insert({Checker.FullName, Option}); 725 } 726 } 727 728 for (const PackageInfo &Package : Packages) { 729 for (const CmdLineOption &Option : Package.CmdLineOptions) { 730 OptionMap.insert({Package.FullName, Option}); 731 } 732 } 733 734 auto Print = [] (llvm::raw_ostream &Out, StringRef FullOption, StringRef Desc) { 735 AnalyzerOptions::printFormattedEntry(Out, {FullOption, Desc}, 736 /*InitialPad*/ 2, 737 /*EntryWidth*/ 50, 738 /*MinLineWidth*/ 90); 739 Out << "\n\n"; 740 }; 741 for (const std::pair<const StringRef, const CmdLineOption &> &Entry : 742 OptionMap) { 743 const CmdLineOption &Option = Entry.second; 744 std::string FullOption = (Entry.first + ":" + Option.OptionName).str(); 745 746 std::string Desc = 747 ("(" + Option.OptionType + ") " + Option.Description + " (default: " + 748 (Option.DefaultValStr.empty() ? "\"\"" : Option.DefaultValStr) + ")") 749 .str(); 750 751 // The list of these if branches is significant, we wouldn't like to 752 // display hidden alpha checker options for 753 // -analyzer-checker-option-help-alpha. 754 755 if (Option.IsHidden) { 756 if (AnOpts.ShowCheckerOptionDeveloperList) 757 Print(Out, FullOption, Desc); 758 continue; 759 } 760 761 if (Option.DevelopmentStatus == "alpha" || 762 Entry.first.startswith("alpha")) { 763 if (AnOpts.ShowCheckerOptionAlphaList) 764 Print(Out, FullOption, 765 llvm::Twine("(Enable only for development!) " + Desc).str()); 766 continue; 767 } 768 769 if (AnOpts.ShowCheckerOptionList) 770 Print(Out, FullOption, Desc); 771 } 772 } 773