1 //=- ClangSACheckersEmitter.cpp - Generate Clang SA checkers tables -*- C++ -*-
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
7 //===----------------------------------------------------------------------===//
9 // This tablegen backend emits Clang Static Analyzer checkers tables.
11 //===----------------------------------------------------------------------===//
13 #include "TableGenBackends.h"
14 #include "llvm/ADT/StringMap.h"
15 #include "llvm/TableGen/Error.h"
16 #include "llvm/TableGen/Record.h"
17 #include "llvm/TableGen/TableGenBackend.h"
23 //===----------------------------------------------------------------------===//
24 // Static Analyzer Checkers Tables generation
25 //===----------------------------------------------------------------------===//
27 static std::string getPackageFullName(const Record *R);
29 static std::string getParentPackageFullName(const Record *R) {
31 if (DefInit *DI = dyn_cast<DefInit>(R->getValueInit("ParentPackage")))
32 name = getPackageFullName(DI->getDef());
36 static std::string getPackageFullName(const Record *R) {
37 std::string name = getParentPackageFullName(R);
40 assert(!R->getValueAsString("PackageName").empty());
41 name += R->getValueAsString("PackageName");
45 static std::string getCheckerFullName(const Record *R) {
46 std::string name = getParentPackageFullName(R);
49 assert(!R->getValueAsString("CheckerName").empty());
50 name += R->getValueAsString("CheckerName");
54 static std::string getStringValue(const Record &R, StringRef field) {
55 if (StringInit *SI = dyn_cast<StringInit>(R.getValueInit(field)))
56 return SI->getValue();
60 // Calculates the integer value representing the BitsInit object
61 static inline uint64_t getValueFromBitsInit(const BitsInit *B, const Record &R) {
62 assert(B->getNumBits() <= sizeof(uint64_t) * 8 && "BitInits' too long!");
65 for (unsigned i = 0, e = B->getNumBits(); i != e; ++i) {
66 const auto *Bit = dyn_cast<BitInit>(B->getBit(i));
68 Value |= uint64_t(Bit->getValue()) << i;
70 PrintFatalError(R.getLoc(),
71 "missing Documentation for " + getCheckerFullName(&R));
76 static std::string getCheckerDocs(const Record &R) {
77 StringRef LandingPage;
78 if (BitsInit *BI = R.getValueAsBitsInit("Documentation")) {
79 uint64_t V = getValueFromBitsInit(BI, R);
81 LandingPage = "available_checks.html";
83 LandingPage = "alpha_checks.html";
86 if (LandingPage.empty())
89 return (llvm::Twine("https://clang-analyzer.llvm.org/") + LandingPage + "#" +
90 getCheckerFullName(&R))
94 /// Retrieves the type from a CmdOptionTypeEnum typed Record object. Note that
95 /// the class itself has to be modified for adding a new option type in
97 static std::string getCheckerOptionType(const Record &R) {
98 if (BitsInit *BI = R.getValueAsBitsInit("Type")) {
99 switch(getValueFromBitsInit(BI, R)) {
108 PrintFatalError(R.getLoc(),
109 "unable to parse command line option type for "
110 + getCheckerFullName(&R));
114 static std::string getDevelopmentStage(const Record &R) {
115 if (BitsInit *BI = R.getValueAsBitsInit("DevelopmentStage")) {
116 switch(getValueFromBitsInit(BI, R)) {
124 PrintFatalError(R.getLoc(),
125 "unable to parse command line option type for "
126 + getCheckerFullName(&R));
130 static bool isHidden(const Record *R) {
131 if (R->getValueAsBit("Hidden"))
134 // Not declared as hidden, check the parent package if it is hidden.
135 if (DefInit *DI = dyn_cast<DefInit>(R->getValueInit("ParentPackage")))
136 return isHidden(DI->getDef());
141 static void printChecker(llvm::raw_ostream &OS, const Record &R) {
142 OS << "CHECKER(" << "\"";
143 OS.write_escaped(getCheckerFullName(&R)) << "\", ";
144 OS << R.getName() << ", ";
146 OS.write_escaped(getStringValue(R, "HelpText")) << "\", ";
148 OS.write_escaped(getCheckerDocs(R));
159 static void printOption(llvm::raw_ostream &OS, StringRef FullName,
162 OS.write_escaped(getCheckerOptionType(R)) << "\", \"";
163 OS.write_escaped(FullName) << "\", ";
164 OS << '\"' << getStringValue(R, "CmdFlag") << "\", ";
166 OS.write_escaped(getStringValue(R, "Desc")) << "\", ";
168 OS.write_escaped(getStringValue(R, "DefaultVal")) << "\", ";
170 OS << getDevelopmentStage(R) << "\", ";
172 if (!R.getValueAsBit("Hidden"))
178 void clang::EmitClangSACheckers(RecordKeeper &Records, raw_ostream &OS) {
179 std::vector<Record*> checkers = Records.getAllDerivedDefinitions("Checker");
180 std::vector<Record*> packages = Records.getAllDerivedDefinitions("Package");
182 using SortedRecords = llvm::StringMap<const Record *>;
184 OS << "// This file is automatically generated. Do not edit this file by "
189 // PACKAGE(PACKAGENAME)
190 // - PACKAGENAME: The name of the package.
192 "#ifdef GET_PACKAGES\n";
194 SortedRecords sortedPackages;
195 for (unsigned i = 0, e = packages.size(); i != e; ++i)
196 sortedPackages[getPackageFullName(packages[i])] = packages[i];
198 for (SortedRecords::iterator
199 I = sortedPackages.begin(), E = sortedPackages.end(); I != E; ++I) {
200 const Record &R = *I->second;
202 OS << "PACKAGE(" << "\"";
203 OS.write_escaped(getPackageFullName(&R)) << '\"';
207 OS << "#endif // GET_PACKAGES\n"
210 // Emit a package option.
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.
221 // - DEFAULT: The default value for this option.
223 // The full option can be specified in the command like like this:
224 // -analyzer-config PACKAGENAME:OPTIONNAME=VALUE
226 "#ifdef GET_PACKAGE_OPTIONS\n";
227 for (const Record *Package : packages) {
229 if (Package->isValueUnset("PackageOptions"))
232 std::vector<Record *> PackageOptions = Package
233 ->getValueAsListOfDefs("PackageOptions");
234 for (Record *PackageOpt : PackageOptions) {
235 OS << "PACKAGE_OPTION(";
236 printOption(OS, getPackageFullName(Package), *PackageOpt);
240 OS << "#endif // GET_PACKAGE_OPTIONS\n"
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.
252 "#ifdef GET_CHECKERS\n"
254 for (const Record *checker : checkers) {
255 printChecker(OS, *checker);
258 "#endif // GET_CHECKERS\n"
261 // Emit dependencies.
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.
267 "#ifdef GET_CHECKER_DEPENDENCIES\n";
268 for (const Record *Checker : checkers) {
269 if (Checker->isValueUnset("Dependencies"))
272 for (const Record *Dependency :
273 Checker->getValueAsListOfDefs("Dependencies")) {
274 OS << "CHECKER_DEPENDENCY(";
276 OS.write_escaped(getCheckerFullName(Checker)) << "\", ";
278 OS.write_escaped(getCheckerFullName(Dependency)) << '\"';
283 "#endif // GET_CHECKER_DEPENDENCIES\n";
285 // Emit a package option.
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.
296 // - DEFAULT: The default value for this option.
298 // The full option can be specified in the command like like this:
299 // -analyzer-config CHECKERNAME:OPTIONNAME=VALUE
301 "#ifdef GET_CHECKER_OPTIONS\n";
302 for (const Record *Checker : checkers) {
304 if (Checker->isValueUnset("CheckerOptions"))
307 std::vector<Record *> CheckerOptions = Checker
308 ->getValueAsListOfDefs("CheckerOptions");
309 for (Record *CheckerOpt : CheckerOptions) {
310 OS << "CHECKER_OPTION(";
311 printOption(OS, getCheckerFullName(Checker), *CheckerOpt);
315 OS << "#endif // GET_CHECKER_OPTIONS\n"