ClangSACheckersEmitter.cpp 11 KB

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