Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1 | //=- ClangSACheckersEmitter.cpp - Generate Clang SA checkers tables -*- C++ -*- |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This tablegen backend emits Clang Static Analyzer checkers tables. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Kristof Umann | 35fc356f | 2018-11-12 17:49:51 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/StringMap.h" |
Joerg Sonnenberger | 691a16b | 2012-10-25 16:37:08 +0000 | [diff] [blame] | 14 | #include "llvm/TableGen/Error.h" |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 15 | #include "llvm/TableGen/Record.h" |
| 16 | #include "llvm/TableGen/TableGenBackend.h" |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 17 | #include <map> |
| 18 | #include <string> |
Kristof Umann | 35fc356f | 2018-11-12 17:49:51 +0000 | [diff] [blame] | 19 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | // Static Analyzer Checkers Tables generation |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 26 | static std::string getPackageFullName(const Record *R); |
| 27 | |
| 28 | static std::string getParentPackageFullName(const Record *R) { |
| 29 | std::string name; |
Sean Silva | 1c4aaa8 | 2012-10-10 20:25:43 +0000 | [diff] [blame] | 30 | if (DefInit *DI = dyn_cast<DefInit>(R->getValueInit("ParentPackage"))) |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 31 | name = getPackageFullName(DI->getDef()); |
| 32 | return name; |
| 33 | } |
| 34 | |
| 35 | static std::string getPackageFullName(const Record *R) { |
| 36 | std::string name = getParentPackageFullName(R); |
Kristof Umann | 35fc356f | 2018-11-12 17:49:51 +0000 | [diff] [blame] | 37 | if (!name.empty()) |
| 38 | name += "."; |
| 39 | assert(!R->getValueAsString("PackageName").empty()); |
Craig Topper | 0064858 | 2017-05-31 19:01:22 +0000 | [diff] [blame] | 40 | name += R->getValueAsString("PackageName"); |
| 41 | return name; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | static std::string getCheckerFullName(const Record *R) { |
| 45 | std::string name = getParentPackageFullName(R); |
Kristof Umann | 35fc356f | 2018-11-12 17:49:51 +0000 | [diff] [blame] | 46 | if (!name.empty()) |
| 47 | name += "."; |
| 48 | assert(!R->getValueAsString("CheckerName").empty()); |
| 49 | name += R->getValueAsString("CheckerName"); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 50 | return name; |
| 51 | } |
| 52 | |
| 53 | static std::string getStringValue(const Record &R, StringRef field) { |
Sean Silva | 1c4aaa8 | 2012-10-10 20:25:43 +0000 | [diff] [blame] | 54 | if (StringInit *SI = dyn_cast<StringInit>(R.getValueInit(field))) |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 55 | return SI->getValue(); |
| 56 | return std::string(); |
| 57 | } |
| 58 | |
Aaron Ballman | 2f234cb | 2018-12-20 20:20:20 +0000 | [diff] [blame] | 59 | // Calculates the integer value representing the BitsInit object |
Aaron Ballman | c682c19 | 2018-12-22 15:31:57 +0000 | [diff] [blame] | 60 | static inline uint64_t getValueFromBitsInit(const BitsInit *B, const Record &R) { |
Aaron Ballman | 2f234cb | 2018-12-20 20:20:20 +0000 | [diff] [blame] | 61 | assert(B->getNumBits() <= sizeof(uint64_t) * 8 && "BitInits' too long!"); |
| 62 | |
| 63 | uint64_t Value = 0; |
| 64 | for (unsigned i = 0, e = B->getNumBits(); i != e; ++i) { |
Aaron Ballman | 88b3670 | 2018-12-21 19:16:38 +0000 | [diff] [blame] | 65 | const auto *Bit = dyn_cast<BitInit>(B->getBit(i)); |
| 66 | if (Bit) |
| 67 | Value |= uint64_t(Bit->getValue()) << i; |
| 68 | else |
Aaron Ballman | c682c19 | 2018-12-22 15:31:57 +0000 | [diff] [blame] | 69 | PrintFatalError(R.getLoc(), |
| 70 | "missing Documentation for " + getCheckerFullName(&R)); |
Aaron Ballman | 2f234cb | 2018-12-20 20:20:20 +0000 | [diff] [blame] | 71 | } |
| 72 | return Value; |
| 73 | } |
| 74 | |
| 75 | static std::string getCheckerDocs(const Record &R) { |
| 76 | StringRef LandingPage; |
| 77 | if (BitsInit *BI = R.getValueAsBitsInit("Documentation")) { |
Aaron Ballman | c682c19 | 2018-12-22 15:31:57 +0000 | [diff] [blame] | 78 | uint64_t V = getValueFromBitsInit(BI, R); |
Aaron Ballman | 2f234cb | 2018-12-20 20:20:20 +0000 | [diff] [blame] | 79 | if (V == 1) |
| 80 | LandingPage = "available_checks.html"; |
| 81 | else if (V == 2) |
| 82 | LandingPage = "alpha_checks.html"; |
| 83 | } |
| 84 | |
| 85 | if (LandingPage.empty()) |
| 86 | return ""; |
| 87 | |
| 88 | return (llvm::Twine("https://clang-analyzer.llvm.org/") + LandingPage + "#" + |
| 89 | getCheckerFullName(&R)) |
| 90 | .str(); |
| 91 | } |
| 92 | |
Kristof Umann | b4788b2 | 2019-04-19 12:32:10 +0000 | [diff] [blame] | 93 | /// Retrieves the type from a CmdOptionTypeEnum typed Record object. Note that |
| 94 | /// the class itself has to be modified for adding a new option type in |
| 95 | /// CheckerBase.td. |
| 96 | static std::string getCheckerOptionType(const Record &R) { |
| 97 | if (BitsInit *BI = R.getValueAsBitsInit("Type")) { |
| 98 | switch(getValueFromBitsInit(BI, R)) { |
| 99 | case 0: |
| 100 | return "int"; |
| 101 | case 1: |
| 102 | return "string"; |
| 103 | case 2: |
| 104 | return "bool"; |
| 105 | } |
| 106 | } |
| 107 | PrintFatalError(R.getLoc(), |
| 108 | "unable to parse command line option type for " |
| 109 | + getCheckerFullName(&R)); |
| 110 | return ""; |
| 111 | } |
| 112 | |
Kristof Umann | ac95c86 | 2019-05-23 22:52:09 +0000 | [diff] [blame] | 113 | static std::string getDevelopmentStage(const Record &R) { |
| 114 | if (BitsInit *BI = R.getValueAsBitsInit("DevelopmentStage")) { |
| 115 | switch(getValueFromBitsInit(BI, R)) { |
| 116 | case 0: |
| 117 | return "alpha"; |
| 118 | case 1: |
| 119 | return "released"; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | PrintFatalError(R.getLoc(), |
| 124 | "unable to parse command line option type for " |
| 125 | + getCheckerFullName(&R)); |
| 126 | return ""; |
| 127 | } |
| 128 | |
Kristof Umann | 9f7fc98 | 2019-05-01 19:56:47 +0000 | [diff] [blame] | 129 | static bool isHidden(const Record *R) { |
| 130 | if (R->getValueAsBit("Hidden")) |
| 131 | return true; |
Kristof Umann | 7e55ed8 | 2019-05-23 22:07:16 +0000 | [diff] [blame] | 132 | |
Kristof Umann | 9f7fc98 | 2019-05-01 19:56:47 +0000 | [diff] [blame] | 133 | // Not declared as hidden, check the parent package if it is hidden. |
| 134 | if (DefInit *DI = dyn_cast<DefInit>(R->getValueInit("ParentPackage"))) |
| 135 | return isHidden(DI->getDef()); |
| 136 | |
| 137 | return false; |
| 138 | } |
| 139 | |
Kristof Umann | 8fd74eb | 2019-01-26 20:06:54 +0000 | [diff] [blame] | 140 | static void printChecker(llvm::raw_ostream &OS, const Record &R) { |
Kristof Umann | 7e55ed8 | 2019-05-23 22:07:16 +0000 | [diff] [blame] | 141 | OS << "CHECKER(" << "\""; |
| 142 | OS.write_escaped(getCheckerFullName(&R)) << "\", "; |
| 143 | OS << R.getName() << ", "; |
| 144 | OS << "\""; |
| 145 | OS.write_escaped(getStringValue(R, "HelpText")) << "\", "; |
| 146 | OS << "\""; |
| 147 | OS.write_escaped(getCheckerDocs(R)); |
| 148 | OS << "\", "; |
Kristof Umann | 9f7fc98 | 2019-05-01 19:56:47 +0000 | [diff] [blame] | 149 | |
Kristof Umann | 7e55ed8 | 2019-05-23 22:07:16 +0000 | [diff] [blame] | 150 | if (!isHidden(&R)) |
| 151 | OS << "false"; |
| 152 | else |
| 153 | OS << "true"; |
Kristof Umann | 9f7fc98 | 2019-05-01 19:56:47 +0000 | [diff] [blame] | 154 | |
Kristof Umann | 7e55ed8 | 2019-05-23 22:07:16 +0000 | [diff] [blame] | 155 | OS << ")\n"; |
| 156 | } |
| 157 | |
| 158 | static void printOption(llvm::raw_ostream &OS, StringRef FullName, |
| 159 | const Record &R) { |
| 160 | OS << "\""; |
| 161 | OS.write_escaped(getCheckerOptionType(R)) << "\", \""; |
| 162 | OS.write_escaped(FullName) << "\", "; |
| 163 | OS << '\"' << getStringValue(R, "CmdFlag") << "\", "; |
| 164 | OS << '\"'; |
| 165 | OS.write_escaped(getStringValue(R, "Desc")) << "\", "; |
| 166 | OS << '\"'; |
| 167 | OS.write_escaped(getStringValue(R, "DefaultVal")) << "\", "; |
Kristof Umann | ac95c86 | 2019-05-23 22:52:09 +0000 | [diff] [blame] | 168 | OS << '\"'; |
| 169 | OS << getDevelopmentStage(R) << "\", "; |
Kristof Umann | 7e55ed8 | 2019-05-23 22:07:16 +0000 | [diff] [blame] | 170 | |
| 171 | if (!R.getValueAsBit("Hidden")) |
| 172 | OS << "false"; |
| 173 | else |
| 174 | OS << "true"; |
Kristof Umann | 8fd74eb | 2019-01-26 20:06:54 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 177 | namespace clang { |
| 178 | void EmitClangSACheckers(RecordKeeper &Records, raw_ostream &OS) { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 179 | std::vector<Record*> checkers = Records.getAllDerivedDefinitions("Checker"); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 180 | std::vector<Record*> packages = Records.getAllDerivedDefinitions("Package"); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 181 | |
Kristof Umann | 35fc356f | 2018-11-12 17:49:51 +0000 | [diff] [blame] | 182 | using SortedRecords = llvm::StringMap<const Record *>; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 183 | |
Aaron Ballman | 2f234cb | 2018-12-20 20:20:20 +0000 | [diff] [blame] | 184 | OS << "// This file is automatically generated. Do not edit this file by " |
| 185 | "hand.\n"; |
| 186 | |
Kristof Umann | 8fd74eb | 2019-01-26 20:06:54 +0000 | [diff] [blame] | 187 | // Emit packages. |
| 188 | // |
| 189 | // PACKAGE(PACKAGENAME) |
| 190 | // - PACKAGENAME: The name of the package. |
| 191 | OS << "\n" |
| 192 | "#ifdef GET_PACKAGES\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 193 | { |
| 194 | SortedRecords sortedPackages; |
| 195 | for (unsigned i = 0, e = packages.size(); i != e; ++i) |
| 196 | sortedPackages[getPackageFullName(packages[i])] = packages[i]; |
| 197 | |
| 198 | for (SortedRecords::iterator |
| 199 | I = sortedPackages.begin(), E = sortedPackages.end(); I != E; ++I) { |
| 200 | const Record &R = *I->second; |
| 201 | |
| 202 | OS << "PACKAGE(" << "\""; |
Kristof Umann | 35fc356f | 2018-11-12 17:49:51 +0000 | [diff] [blame] | 203 | OS.write_escaped(getPackageFullName(&R)) << '\"'; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 204 | OS << ")\n"; |
| 205 | } |
| 206 | } |
Kristof Umann | 8fd74eb | 2019-01-26 20:06:54 +0000 | [diff] [blame] | 207 | OS << "#endif // GET_PACKAGES\n" |
| 208 | "\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 209 | |
Kristof Umann | b4788b2 | 2019-04-19 12:32:10 +0000 | [diff] [blame] | 210 | // Emit a package option. |
| 211 | // |
| 212 | // PACKAGE_OPTION(OPTIONTYPE, PACKAGENAME, OPTIONNAME, DESCRIPTION, DEFAULT) |
| 213 | // - OPTIONTYPE: Type of the option, whether it's integer or boolean etc. |
| 214 | // This is important for validating user input. Note that |
| 215 | // it's a string, rather than an actual type: since we can |
| 216 | // load checkers runtime, we can't use template hackery for |
| 217 | // sorting this out compile-time. |
| 218 | // - PACKAGENAME: Name of the package. |
| 219 | // - OPTIONNAME: Name of the option. |
| 220 | // - DESCRIPTION |
| 221 | // - DEFAULT: The default value for this option. |
| 222 | // |
| 223 | // The full option can be specified in the command like like this: |
| 224 | // -analyzer-config PACKAGENAME:OPTIONNAME=VALUE |
| 225 | OS << "\n" |
| 226 | "#ifdef GET_PACKAGE_OPTIONS\n"; |
| 227 | for (const Record *Package : packages) { |
| 228 | |
| 229 | if (Package->isValueUnset("PackageOptions")) |
| 230 | continue; |
| 231 | |
| 232 | std::vector<Record *> PackageOptions = Package |
| 233 | ->getValueAsListOfDefs("PackageOptions"); |
| 234 | for (Record *PackageOpt : PackageOptions) { |
Kristof Umann | 7e55ed8 | 2019-05-23 22:07:16 +0000 | [diff] [blame] | 235 | OS << "PACKAGE_OPTION("; |
| 236 | printOption(OS, getPackageFullName(Package), *PackageOpt); |
Kristof Umann | b4788b2 | 2019-04-19 12:32:10 +0000 | [diff] [blame] | 237 | OS << ")\n"; |
| 238 | } |
| 239 | } |
| 240 | OS << "#endif // GET_PACKAGE_OPTIONS\n" |
| 241 | "\n"; |
| 242 | |
Kristof Umann | 8fd74eb | 2019-01-26 20:06:54 +0000 | [diff] [blame] | 243 | // Emit checkers. |
| 244 | // |
| 245 | // CHECKER(FULLNAME, CLASS, HELPTEXT) |
| 246 | // - FULLNAME: The full name of the checker, including packages, e.g.: |
| 247 | // alpha.cplusplus.UninitializedObject |
| 248 | // - CLASS: The name of the checker, with "Checker" appended, e.g.: |
| 249 | // UninitializedObjectChecker |
| 250 | // - HELPTEXT: The description of the checker. |
| 251 | OS << "\n" |
| 252 | "#ifdef GET_CHECKERS\n" |
| 253 | "\n"; |
| 254 | for (const Record *checker : checkers) { |
| 255 | printChecker(OS, *checker); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 256 | } |
Kristof Umann | 8fd74eb | 2019-01-26 20:06:54 +0000 | [diff] [blame] | 257 | OS << "\n" |
| 258 | "#endif // GET_CHECKERS\n" |
| 259 | "\n"; |
| 260 | |
| 261 | // Emit dependencies. |
| 262 | // |
| 263 | // CHECKER_DEPENDENCY(FULLNAME, DEPENDENCY) |
| 264 | // - FULLNAME: The full name of the checker that depends on another checker. |
| 265 | // - DEPENDENCY: The full name of the checker FULLNAME depends on. |
| 266 | OS << "\n" |
| 267 | "#ifdef GET_CHECKER_DEPENDENCIES\n"; |
Kristof Umann | b4788b2 | 2019-04-19 12:32:10 +0000 | [diff] [blame] | 268 | for (const Record *Checker : checkers) { |
| 269 | if (Checker->isValueUnset("Dependencies")) |
Kristof Umann | 8fd74eb | 2019-01-26 20:06:54 +0000 | [diff] [blame] | 270 | continue; |
| 271 | |
| 272 | for (const Record *Dependency : |
Kristof Umann | b4788b2 | 2019-04-19 12:32:10 +0000 | [diff] [blame] | 273 | Checker->getValueAsListOfDefs("Dependencies")) { |
Kristof Umann | 8fd74eb | 2019-01-26 20:06:54 +0000 | [diff] [blame] | 274 | OS << "CHECKER_DEPENDENCY("; |
| 275 | OS << '\"'; |
Kristof Umann | b4788b2 | 2019-04-19 12:32:10 +0000 | [diff] [blame] | 276 | OS.write_escaped(getCheckerFullName(Checker)) << "\", "; |
Kristof Umann | 8fd74eb | 2019-01-26 20:06:54 +0000 | [diff] [blame] | 277 | OS << '\"'; |
| 278 | OS.write_escaped(getCheckerFullName(Dependency)) << '\"'; |
| 279 | OS << ")\n"; |
| 280 | } |
| 281 | } |
| 282 | OS << "\n" |
| 283 | "#endif // GET_CHECKER_DEPENDENCIES\n"; |
Kristof Umann | b4788b2 | 2019-04-19 12:32:10 +0000 | [diff] [blame] | 284 | |
| 285 | // Emit a package option. |
| 286 | // |
| 287 | // CHECKER_OPTION(OPTIONTYPE, CHECKERNAME, OPTIONNAME, DESCRIPTION, DEFAULT) |
| 288 | // - OPTIONTYPE: Type of the option, whether it's integer or boolean etc. |
| 289 | // This is important for validating user input. Note that |
| 290 | // it's a string, rather than an actual type: since we can |
| 291 | // load checkers runtime, we can't use template hackery for |
| 292 | // sorting this out compile-time. |
| 293 | // - CHECKERNAME: Name of the package. |
| 294 | // - OPTIONNAME: Name of the option. |
| 295 | // - DESCRIPTION |
| 296 | // - DEFAULT: The default value for this option. |
| 297 | // |
| 298 | // The full option can be specified in the command like like this: |
| 299 | // -analyzer-config CHECKERNAME:OPTIONNAME=VALUE |
| 300 | OS << "\n" |
| 301 | "#ifdef GET_CHECKER_OPTIONS\n"; |
| 302 | for (const Record *Checker : checkers) { |
| 303 | |
| 304 | if (Checker->isValueUnset("CheckerOptions")) |
| 305 | continue; |
| 306 | |
| 307 | std::vector<Record *> CheckerOptions = Checker |
| 308 | ->getValueAsListOfDefs("CheckerOptions"); |
| 309 | for (Record *CheckerOpt : CheckerOptions) { |
Kristof Umann | 7e55ed8 | 2019-05-23 22:07:16 +0000 | [diff] [blame] | 310 | OS << "CHECKER_OPTION("; |
| 311 | printOption(OS, getCheckerFullName(Checker), *CheckerOpt); |
| 312 | OS << ")\n"; |
Kristof Umann | b4788b2 | 2019-04-19 12:32:10 +0000 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | OS << "#endif // GET_CHECKER_OPTIONS\n" |
| 316 | "\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 317 | } |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 318 | } // end namespace clang |