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 "llvm/ADT/StringMap.h"
14 #include "llvm/TableGen/Error.h"
15 #include "llvm/TableGen/Record.h"
16 #include "llvm/TableGen/TableGenBackend.h"
22 //===----------------------------------------------------------------------===//
23 // Static Analyzer Checkers Tables generation
24 //===----------------------------------------------------------------------===//
26 static std::string getPackageFullName(const Record *R);
28 static std::string getParentPackageFullName(const Record *R) {
30 if (DefInit *DI = dyn_cast<DefInit>(R->getValueInit("ParentPackage")))
31 name = getPackageFullName(DI->getDef());
35 static std::string getPackageFullName(const Record *R) {
36 std::string name = getParentPackageFullName(R);
39 assert(!R->getValueAsString("PackageName").empty());
40 name += R->getValueAsString("PackageName");
44 static std::string getCheckerFullName(const Record *R) {
45 std::string name = getParentPackageFullName(R);
48 assert(!R->getValueAsString("CheckerName").empty());
49 name += R->getValueAsString("CheckerName");
53 static std::string getStringValue(const Record &R, StringRef field) {
54 if (StringInit *SI = dyn_cast<StringInit>(R.getValueInit(field)))
55 return SI->getValue();
59 // Calculates the integer value representing the BitsInit object
60 static inline uint64_t getValueFromBitsInit(const BitsInit *B, const Record &R) {
61 assert(B->getNumBits() <= sizeof(uint64_t) * 8 && "BitInits' too long!");
64 for (unsigned i = 0, e = B->getNumBits(); i != e; ++i) {
65 const auto *Bit = dyn_cast<BitInit>(B->getBit(i));
67 Value |= uint64_t(Bit->getValue()) << i;
69 PrintFatalError(R.getLoc(),
70 "missing Documentation for " + getCheckerFullName(&R));
75 static std::string getCheckerDocs(const Record &R) {
76 StringRef LandingPage;
77 if (BitsInit *BI = R.getValueAsBitsInit("Documentation")) {
78 uint64_t V = getValueFromBitsInit(BI, R);
80 LandingPage = "available_checks.html";
82 LandingPage = "alpha_checks.html";
85 if (LandingPage.empty())
88 return (llvm::Twine("https://clang-analyzer.llvm.org/") + LandingPage + "#" +
89 getCheckerFullName(&R))
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
96 static std::string getCheckerOptionType(const Record &R) {
97 if (BitsInit *BI = R.getValueAsBitsInit("Type")) {
98 switch(getValueFromBitsInit(BI, R)) {
107 PrintFatalError(R.getLoc(),
108 "unable to parse command line option type for "
109 + getCheckerFullName(&R));
113 static std::string getDevelopmentStage(const Record &R) {
114 if (BitsInit *BI = R.getValueAsBitsInit("DevelopmentStage")) {
115 switch(getValueFromBitsInit(BI, R)) {
123 PrintFatalError(R.getLoc(),
124 "unable to parse command line option type for "
125 + getCheckerFullName(&R));
129 static bool isHidden(const Record *R) {
130 if (R->getValueAsBit("Hidden"))
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());
140 static void printChecker(llvm::raw_ostream &OS, const Record &R) {
141 OS << "CHECKER(" << "\"";
142 OS.write_escaped(getCheckerFullName(&R)) << "\", ";
143 OS << R.getName() << ", ";
145 OS.write_escaped(getStringValue(R, "HelpText")) << "\", ";
147 OS.write_escaped(getCheckerDocs(R));
158 static void printOption(llvm::raw_ostream &OS, StringRef FullName,
161 OS.write_escaped(getCheckerOptionType(R)) << "\", \"";
162 OS.write_escaped(FullName) << "\", ";
163 OS << '\"' << getStringValue(R, "CmdFlag") << "\", ";
165 OS.write_escaped(getStringValue(R, "Desc")) << "\", ";
167 OS.write_escaped(getStringValue(R, "DefaultVal")) << "\", ";
169 OS << getDevelopmentStage(R) << "\", ";
171 if (!R.getValueAsBit("Hidden"))
178 void 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"
318 } // end namespace clang