Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:37:10

0001 //===- CheckerRegistryData.h ------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file contains the data structures to which the TableGen file Checkers.td
0010 // maps to, as well as what was parsed from the the specific invocation (whether
0011 // a checker/package is enabled, their options values, etc).
0012 //
0013 // The parsing of the invocation is done by CheckerRegistry, which is found in
0014 // the Frontend library. This allows the Core and Checkers libraries to utilize
0015 // this information, such as enforcing rules on checker dependency bug emission,
0016 // ensuring all checker options were queried, etc.
0017 //
0018 //===----------------------------------------------------------------------===//
0019 
0020 #ifndef LLVM_CLANG_STATICANALYZER_CORE_CHECKERREGISTRYDATA_H
0021 #define LLVM_CLANG_STATICANALYZER_CORE_CHECKERREGISTRYDATA_H
0022 
0023 #include "clang/Basic/LLVM.h"
0024 #include "llvm/ADT/SetVector.h"
0025 #include "llvm/ADT/StringMap.h"
0026 #include "llvm/ADT/StringRef.h"
0027 #include "llvm/Support/raw_ostream.h"
0028 
0029 namespace clang {
0030 
0031 class AnalyzerOptions;
0032 
0033 namespace ento {
0034 
0035 class CheckerManager;
0036 
0037 /// Initialization functions perform any necessary setup for a checker.
0038 /// They should include a call to CheckerManager::registerChecker.
0039 using RegisterCheckerFn = void (*)(CheckerManager &);
0040 using ShouldRegisterFunction = bool (*)(const CheckerManager &);
0041 
0042 /// Specifies a command line option. It may either belong to a checker or a
0043 /// package.
0044 struct CmdLineOption {
0045   StringRef OptionType;
0046   StringRef OptionName;
0047   StringRef DefaultValStr;
0048   StringRef Description;
0049   StringRef DevelopmentStatus;
0050   bool IsHidden;
0051 
0052   CmdLineOption(StringRef OptionType, StringRef OptionName,
0053                 StringRef DefaultValStr, StringRef Description,
0054                 StringRef DevelopmentStatus, bool IsHidden)
0055       : OptionType(OptionType), OptionName(OptionName),
0056         DefaultValStr(DefaultValStr), Description(Description),
0057         DevelopmentStatus(DevelopmentStatus), IsHidden(IsHidden) {
0058 
0059     assert((OptionType == "bool" || OptionType == "string" ||
0060             OptionType == "int") &&
0061            "Unknown command line option type!");
0062 
0063     assert((OptionType != "bool" ||
0064             (DefaultValStr == "true" || DefaultValStr == "false")) &&
0065            "Invalid value for boolean command line option! Maybe incorrect "
0066            "parameters to the addCheckerOption or addPackageOption method?");
0067 
0068     int Tmp;
0069     assert((OptionType != "int" || !DefaultValStr.getAsInteger(0, Tmp)) &&
0070            "Invalid value for integer command line option! Maybe incorrect "
0071            "parameters to the addCheckerOption or addPackageOption method?");
0072     (void)Tmp;
0073 
0074     assert((DevelopmentStatus == "alpha" || DevelopmentStatus == "beta" ||
0075             DevelopmentStatus == "released") &&
0076            "Invalid development status!");
0077   }
0078 
0079   LLVM_DUMP_METHOD void dump() const;
0080   LLVM_DUMP_METHOD void dumpToStream(llvm::raw_ostream &Out) const;
0081 };
0082 
0083 using CmdLineOptionList = llvm::SmallVector<CmdLineOption, 0>;
0084 
0085 struct CheckerInfo;
0086 
0087 using CheckerInfoList = std::vector<CheckerInfo>;
0088 using CheckerInfoListRange = llvm::iterator_range<CheckerInfoList::iterator>;
0089 using ConstCheckerInfoList = llvm::SmallVector<const CheckerInfo *, 0>;
0090 using CheckerInfoSet = llvm::SetVector<const CheckerInfo *>;
0091 
0092 /// Specifies a checker. Note that this isn't what we call a checker object,
0093 /// it merely contains everything required to create one.
0094 struct CheckerInfo {
0095   enum class StateFromCmdLine {
0096     // This checker wasn't explicitly enabled or disabled.
0097     State_Unspecified,
0098     // This checker was explicitly disabled.
0099     State_Disabled,
0100     // This checker was explicitly enabled.
0101     State_Enabled
0102   };
0103 
0104   RegisterCheckerFn Initialize = nullptr;
0105   ShouldRegisterFunction ShouldRegister = nullptr;
0106   StringRef FullName;
0107   StringRef Desc;
0108   StringRef DocumentationUri;
0109   CmdLineOptionList CmdLineOptions;
0110   bool IsHidden = false;
0111   StateFromCmdLine State = StateFromCmdLine::State_Unspecified;
0112 
0113   ConstCheckerInfoList Dependencies;
0114   ConstCheckerInfoList WeakDependencies;
0115 
0116   bool isEnabled(const CheckerManager &mgr) const {
0117     return State == StateFromCmdLine::State_Enabled && ShouldRegister(mgr);
0118   }
0119 
0120   bool isDisabled(const CheckerManager &mgr) const {
0121     return State == StateFromCmdLine::State_Disabled || !ShouldRegister(mgr);
0122   }
0123 
0124   // Since each checker must have a different full name, we can identify
0125   // CheckerInfo objects by them.
0126   bool operator==(const CheckerInfo &Rhs) const {
0127     return FullName == Rhs.FullName;
0128   }
0129 
0130   CheckerInfo(RegisterCheckerFn Fn, ShouldRegisterFunction sfn, StringRef Name,
0131               StringRef Desc, StringRef DocsUri, bool IsHidden)
0132       : Initialize(Fn), ShouldRegister(sfn), FullName(Name), Desc(Desc),
0133         DocumentationUri(DocsUri), IsHidden(IsHidden) {}
0134 
0135   // Used for lower_bound.
0136   explicit CheckerInfo(StringRef FullName) : FullName(FullName) {}
0137 
0138   LLVM_DUMP_METHOD void dump() const;
0139   LLVM_DUMP_METHOD void dumpToStream(llvm::raw_ostream &Out) const;
0140 };
0141 
0142 using StateFromCmdLine = CheckerInfo::StateFromCmdLine;
0143 
0144 /// Specifies a package. Each package option is implicitly an option for all
0145 /// checkers within the package.
0146 struct PackageInfo {
0147   StringRef FullName;
0148   CmdLineOptionList CmdLineOptions;
0149 
0150   // Since each package must have a different full name, we can identify
0151   // CheckerInfo objects by them.
0152   bool operator==(const PackageInfo &Rhs) const {
0153     return FullName == Rhs.FullName;
0154   }
0155 
0156   explicit PackageInfo(StringRef FullName) : FullName(FullName) {}
0157 
0158   LLVM_DUMP_METHOD void dump() const;
0159   LLVM_DUMP_METHOD void dumpToStream(llvm::raw_ostream &Out) const;
0160 };
0161 
0162 using PackageInfoList = llvm::SmallVector<PackageInfo, 0>;
0163 
0164 namespace checker_registry {
0165 
0166 template <class T> struct FullNameLT {
0167   bool operator()(const T &Lhs, const T &Rhs) {
0168     return Lhs.FullName < Rhs.FullName;
0169   }
0170 };
0171 
0172 using PackageNameLT = FullNameLT<PackageInfo>;
0173 using CheckerNameLT = FullNameLT<CheckerInfo>;
0174 
0175 template <class CheckerOrPackageInfoList>
0176 std::conditional_t<std::is_const<CheckerOrPackageInfoList>::value,
0177                    typename CheckerOrPackageInfoList::const_iterator,
0178                    typename CheckerOrPackageInfoList::iterator>
0179 binaryFind(CheckerOrPackageInfoList &Collection, StringRef FullName) {
0180 
0181   using CheckerOrPackage = typename CheckerOrPackageInfoList::value_type;
0182   using CheckerOrPackageFullNameLT = FullNameLT<CheckerOrPackage>;
0183 
0184   assert(llvm::is_sorted(Collection, CheckerOrPackageFullNameLT{}) &&
0185          "In order to efficiently gather checkers/packages, this function "
0186          "expects them to be already sorted!");
0187 
0188   return llvm::lower_bound(Collection, CheckerOrPackage(FullName),
0189                            CheckerOrPackageFullNameLT{});
0190 }
0191 } // namespace checker_registry
0192 
0193 struct CheckerRegistryData {
0194 public:
0195   CheckerInfoSet EnabledCheckers;
0196 
0197   CheckerInfoList Checkers;
0198   PackageInfoList Packages;
0199   /// Used for counting how many checkers belong to a certain package in the
0200   /// \c Checkers field. For convenience purposes.
0201   llvm::StringMap<size_t> PackageSizes;
0202 
0203   /// Contains all (FullName, CmdLineOption) pairs. Similarly to dependencies,
0204   /// we only modify the actual CheckerInfo and PackageInfo objects once all
0205   /// of them have been added.
0206   llvm::SmallVector<std::pair<StringRef, CmdLineOption>, 0> PackageOptions;
0207   llvm::SmallVector<std::pair<StringRef, CmdLineOption>, 0> CheckerOptions;
0208 
0209   llvm::SmallVector<std::pair<StringRef, StringRef>, 0> Dependencies;
0210   llvm::SmallVector<std::pair<StringRef, StringRef>, 0> WeakDependencies;
0211 
0212   CheckerInfoListRange getMutableCheckersForCmdLineArg(StringRef CmdLineArg);
0213 
0214   /// Prints the name and description of all checkers in this registry.
0215   /// This output is not intended to be machine-parseable.
0216   void printCheckerWithDescList(const AnalyzerOptions &AnOpts, raw_ostream &Out,
0217                                 size_t MaxNameChars = 30) const;
0218   void printEnabledCheckerList(raw_ostream &Out) const;
0219   void printCheckerOptionList(const AnalyzerOptions &AnOpts,
0220                               raw_ostream &Out) const;
0221 };
0222 
0223 } // namespace ento
0224 } // namespace clang
0225 
0226 #endif // LLVM_CLANG_STATICANALYZER_CORE_CHECKERREGISTRYDATA_H