PageRenderTime 142ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/contrib/llvm/utils/TableGen/ClangSACheckersEmitter.cpp

https://github.com/okuoku/freebsd-head
C++ | 319 lines | 239 code | 44 blank | 36 comment | 64 complexity | 9a7df7608503e4a614751deae9ae76f1 MD5 | raw file
  1. //=- ClangSACheckersEmitter.cpp - Generate Clang SA checkers tables -*- C++ -*-
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This tablegen backend emits Clang Static Analyzer checkers tables.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "ClangSACheckersEmitter.h"
  14. #include "Record.h"
  15. #include "llvm/ADT/DenseSet.h"
  16. #include <map>
  17. #include <string>
  18. using namespace llvm;
  19. //===----------------------------------------------------------------------===//
  20. // Static Analyzer Checkers Tables generation
  21. //===----------------------------------------------------------------------===//
  22. /// \brief True if it is specified hidden or a parent package is specified
  23. /// as hidden, otherwise false.
  24. static bool isHidden(const Record &R) {
  25. if (R.getValueAsBit("Hidden"))
  26. return true;
  27. // Not declared as hidden, check the parent package if it is hidden.
  28. if (DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("ParentPackage")))
  29. return isHidden(*DI->getDef());
  30. return false;
  31. }
  32. static bool isCheckerNamed(const Record *R) {
  33. return !R->getValueAsString("CheckerName").empty();
  34. }
  35. static std::string getPackageFullName(const Record *R);
  36. static std::string getParentPackageFullName(const Record *R) {
  37. std::string name;
  38. if (DefInit *DI = dynamic_cast<DefInit*>(R->getValueInit("ParentPackage")))
  39. name = getPackageFullName(DI->getDef());
  40. return name;
  41. }
  42. static std::string getPackageFullName(const Record *R) {
  43. std::string name = getParentPackageFullName(R);
  44. if (!name.empty()) name += ".";
  45. return name + R->getValueAsString("PackageName");
  46. }
  47. static std::string getCheckerFullName(const Record *R) {
  48. std::string name = getParentPackageFullName(R);
  49. if (isCheckerNamed(R)) {
  50. if (!name.empty()) name += ".";
  51. name += R->getValueAsString("CheckerName");
  52. }
  53. return name;
  54. }
  55. static std::string getStringValue(const Record &R, StringRef field) {
  56. if (StringInit *
  57. SI = dynamic_cast<StringInit*>(R.getValueInit(field)))
  58. return SI->getValue();
  59. return std::string();
  60. }
  61. namespace {
  62. struct GroupInfo {
  63. llvm::DenseSet<const Record*> Checkers;
  64. llvm::DenseSet<const Record *> SubGroups;
  65. bool Hidden;
  66. unsigned Index;
  67. GroupInfo() : Hidden(false) { }
  68. };
  69. }
  70. static void addPackageToCheckerGroup(const Record *package, const Record *group,
  71. llvm::DenseMap<const Record *, GroupInfo *> &recordGroupMap) {
  72. llvm::DenseSet<const Record *> &checkers = recordGroupMap[package]->Checkers;
  73. for (llvm::DenseSet<const Record *>::iterator
  74. I = checkers.begin(), E = checkers.end(); I != E; ++I)
  75. recordGroupMap[group]->Checkers.insert(*I);
  76. llvm::DenseSet<const Record *> &subGroups = recordGroupMap[package]->SubGroups;
  77. for (llvm::DenseSet<const Record *>::iterator
  78. I = subGroups.begin(), E = subGroups.end(); I != E; ++I)
  79. addPackageToCheckerGroup(*I, group, recordGroupMap);
  80. }
  81. void ClangSACheckersEmitter::run(raw_ostream &OS) {
  82. std::vector<Record*> checkers = Records.getAllDerivedDefinitions("Checker");
  83. llvm::DenseMap<const Record *, unsigned> checkerRecIndexMap;
  84. for (unsigned i = 0, e = checkers.size(); i != e; ++i)
  85. checkerRecIndexMap[checkers[i]] = i;
  86. // Invert the mapping of checkers to package/group into a one to many
  87. // mapping of packages/groups to checkers.
  88. std::map<std::string, GroupInfo> groupInfoByName;
  89. llvm::DenseMap<const Record *, GroupInfo *> recordGroupMap;
  90. std::vector<Record*> packages = Records.getAllDerivedDefinitions("Package");
  91. for (unsigned i = 0, e = packages.size(); i != e; ++i) {
  92. Record *R = packages[i];
  93. std::string fullName = getPackageFullName(R);
  94. if (!fullName.empty()) {
  95. GroupInfo &info = groupInfoByName[fullName];
  96. info.Hidden = isHidden(*R);
  97. recordGroupMap[R] = &info;
  98. }
  99. }
  100. std::vector<Record*>
  101. checkerGroups = Records.getAllDerivedDefinitions("CheckerGroup");
  102. for (unsigned i = 0, e = checkerGroups.size(); i != e; ++i) {
  103. Record *R = checkerGroups[i];
  104. std::string name = R->getValueAsString("GroupName");
  105. if (!name.empty()) {
  106. GroupInfo &info = groupInfoByName[name];
  107. recordGroupMap[R] = &info;
  108. }
  109. }
  110. for (unsigned i = 0, e = checkers.size(); i != e; ++i) {
  111. Record *R = checkers[i];
  112. Record *package = 0;
  113. if (DefInit *
  114. DI = dynamic_cast<DefInit*>(R->getValueInit("ParentPackage")))
  115. package = DI->getDef();
  116. if (!isCheckerNamed(R) && !package)
  117. throw "Checker '" + R->getName() + "' is neither named, nor in a package!";
  118. if (isCheckerNamed(R)) {
  119. // Create a pseudo-group to hold this checker.
  120. std::string fullName = getCheckerFullName(R);
  121. GroupInfo &info = groupInfoByName[fullName];
  122. info.Hidden = R->getValueAsBit("Hidden");
  123. recordGroupMap[R] = &info;
  124. info.Checkers.insert(R);
  125. } else {
  126. recordGroupMap[package]->Checkers.insert(R);
  127. }
  128. Record *currR = isCheckerNamed(R) ? R : package;
  129. // Insert the checker and its parent packages into the subgroups set of
  130. // the corresponding parent package.
  131. while (DefInit *DI
  132. = dynamic_cast<DefInit*>(currR->getValueInit("ParentPackage"))) {
  133. Record *parentPackage = DI->getDef();
  134. recordGroupMap[parentPackage]->SubGroups.insert(currR);
  135. currR = parentPackage;
  136. }
  137. // Insert the checker into the set of its group.
  138. if (DefInit *DI = dynamic_cast<DefInit*>(R->getValueInit("Group")))
  139. recordGroupMap[DI->getDef()]->Checkers.insert(R);
  140. }
  141. // If a package is in group, add all its checkers and its sub-packages
  142. // checkers into the group.
  143. for (unsigned i = 0, e = packages.size(); i != e; ++i)
  144. if (DefInit *DI = dynamic_cast<DefInit*>(packages[i]->getValueInit("Group")))
  145. addPackageToCheckerGroup(packages[i], DI->getDef(), recordGroupMap);
  146. typedef std::map<std::string, const Record *> SortedRecords;
  147. typedef llvm::DenseMap<const Record *, unsigned> RecToSortIndex;
  148. SortedRecords sortedGroups;
  149. RecToSortIndex groupToSortIndex;
  150. OS << "\n#ifdef GET_GROUPS\n";
  151. {
  152. for (unsigned i = 0, e = checkerGroups.size(); i != e; ++i)
  153. sortedGroups[checkerGroups[i]->getValueAsString("GroupName")]
  154. = checkerGroups[i];
  155. unsigned sortIndex = 0;
  156. for (SortedRecords::iterator
  157. I = sortedGroups.begin(), E = sortedGroups.end(); I != E; ++I) {
  158. const Record *R = I->second;
  159. OS << "GROUP(" << "\"";
  160. OS.write_escaped(R->getValueAsString("GroupName")) << "\"";
  161. OS << ")\n";
  162. groupToSortIndex[R] = sortIndex++;
  163. }
  164. }
  165. OS << "#endif // GET_GROUPS\n\n";
  166. OS << "\n#ifdef GET_PACKAGES\n";
  167. {
  168. SortedRecords sortedPackages;
  169. for (unsigned i = 0, e = packages.size(); i != e; ++i)
  170. sortedPackages[getPackageFullName(packages[i])] = packages[i];
  171. for (SortedRecords::iterator
  172. I = sortedPackages.begin(), E = sortedPackages.end(); I != E; ++I) {
  173. const Record &R = *I->second;
  174. OS << "PACKAGE(" << "\"";
  175. OS.write_escaped(getPackageFullName(&R)) << "\", ";
  176. // Group index
  177. if (DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Group")))
  178. OS << groupToSortIndex[DI->getDef()] << ", ";
  179. else
  180. OS << "-1, ";
  181. // Hidden bit
  182. if (isHidden(R))
  183. OS << "true";
  184. else
  185. OS << "false";
  186. OS << ")\n";
  187. }
  188. }
  189. OS << "#endif // GET_PACKAGES\n\n";
  190. OS << "\n#ifdef GET_CHECKERS\n";
  191. for (unsigned i = 0, e = checkers.size(); i != e; ++i) {
  192. const Record &R = *checkers[i];
  193. OS << "CHECKER(" << "\"";
  194. std::string name;
  195. if (isCheckerNamed(&R))
  196. name = getCheckerFullName(&R);
  197. OS.write_escaped(name) << "\", ";
  198. OS << R.getName() << ", ";
  199. OS << getStringValue(R, "DescFile") << ", ";
  200. OS << "\"";
  201. OS.write_escaped(getStringValue(R, "HelpText")) << "\", ";
  202. // Group index
  203. if (DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Group")))
  204. OS << groupToSortIndex[DI->getDef()] << ", ";
  205. else
  206. OS << "-1, ";
  207. // Hidden bit
  208. if (isHidden(R))
  209. OS << "true";
  210. else
  211. OS << "false";
  212. OS << ")\n";
  213. }
  214. OS << "#endif // GET_CHECKERS\n\n";
  215. unsigned index = 0;
  216. for (std::map<std::string, GroupInfo>::iterator
  217. I = groupInfoByName.begin(), E = groupInfoByName.end(); I != E; ++I)
  218. I->second.Index = index++;
  219. // Walk through the packages/groups/checkers emitting an array for each
  220. // set of checkers and an array for each set of subpackages.
  221. OS << "\n#ifdef GET_MEMBER_ARRAYS\n";
  222. unsigned maxLen = 0;
  223. for (std::map<std::string, GroupInfo>::iterator
  224. I = groupInfoByName.begin(), E = groupInfoByName.end(); I != E; ++I) {
  225. maxLen = std::max(maxLen, (unsigned)I->first.size());
  226. llvm::DenseSet<const Record *> &checkers = I->second.Checkers;
  227. if (!checkers.empty()) {
  228. OS << "static const short CheckerArray" << I->second.Index << "[] = { ";
  229. // Make the output order deterministic.
  230. std::map<int, const Record *> sorted;
  231. for (llvm::DenseSet<const Record *>::iterator
  232. I = checkers.begin(), E = checkers.end(); I != E; ++I)
  233. sorted[(*I)->getID()] = *I;
  234. for (std::map<int, const Record *>::iterator
  235. I = sorted.begin(), E = sorted.end(); I != E; ++I)
  236. OS << checkerRecIndexMap[I->second] << ", ";
  237. OS << "-1 };\n";
  238. }
  239. llvm::DenseSet<const Record *> &subGroups = I->second.SubGroups;
  240. if (!subGroups.empty()) {
  241. OS << "static const short SubPackageArray" << I->second.Index << "[] = { ";
  242. // Make the output order deterministic.
  243. std::map<int, const Record *> sorted;
  244. for (llvm::DenseSet<const Record *>::iterator
  245. I = subGroups.begin(), E = subGroups.end(); I != E; ++I)
  246. sorted[(*I)->getID()] = *I;
  247. for (std::map<int, const Record *>::iterator
  248. I = sorted.begin(), E = sorted.end(); I != E; ++I) {
  249. OS << recordGroupMap[I->second]->Index << ", ";
  250. }
  251. OS << "-1 };\n";
  252. }
  253. }
  254. OS << "#endif // GET_MEMBER_ARRAYS\n\n";
  255. OS << "\n#ifdef GET_CHECKNAME_TABLE\n";
  256. for (std::map<std::string, GroupInfo>::iterator
  257. I = groupInfoByName.begin(), E = groupInfoByName.end(); I != E; ++I) {
  258. // Group option string.
  259. OS << " { \"";
  260. OS.write_escaped(I->first) << "\","
  261. << std::string(maxLen-I->first.size()+1, ' ');
  262. if (I->second.Checkers.empty())
  263. OS << "0, ";
  264. else
  265. OS << "CheckerArray" << I->second.Index << ", ";
  266. // Subgroups.
  267. if (I->second.SubGroups.empty())
  268. OS << "0, ";
  269. else
  270. OS << "SubPackageArray" << I->second.Index << ", ";
  271. OS << (I->second.Hidden ? "true" : "false");
  272. OS << " },\n";
  273. }
  274. OS << "#endif // GET_CHECKNAME_TABLE\n\n";
  275. }