blob: 8865db36b6c3751802fd733a47065e2274d281b4 [file] [log] [blame]
Argyrios Kyrtzidis50a47e92011-02-14 17:58:52 +00001//=- 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
14#include "ClangSACheckersEmitter.h"
15#include "Record.h"
16#include "llvm/ADT/DenseSet.h"
17#include <map>
18#include <string>
19using namespace llvm;
20
21//===----------------------------------------------------------------------===//
22// Static Analyzer Checkers Tables generation
23//===----------------------------------------------------------------------===//
24
25/// \brief True if it is specified hidden or a parent package is specified
26/// as hidden, otherwise false.
27static bool isHidden(const Record &R) {
28 if (R.getValueAsBit("Hidden"))
29 return true;
30 // Not declared as hidden, check the parent package if it is hidden.
31 if (DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("ParentPackage")))
32 return isHidden(*DI->getDef());
33
34 return false;
35}
36
Argyrios Kyrtzidisab8f3fd2011-02-15 07:42:16 +000037static bool isCheckerNamed(const Record *R) {
38 return !R->getValueAsString("CheckerName").empty();
39}
Argyrios Kyrtzidis50a47e92011-02-14 17:58:52 +000040
Argyrios Kyrtzidisab8f3fd2011-02-15 07:42:16 +000041static std::string getPackageFullName(const Record *R);
42
43static std::string getParentPackageFullName(const Record *R) {
Argyrios Kyrtzidis50a47e92011-02-14 17:58:52 +000044 std::string name;
45 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValueInit("ParentPackage")))
46 name = getPackageFullName(DI->getDef());
47 return name;
48}
49
Argyrios Kyrtzidisab8f3fd2011-02-15 07:42:16 +000050static std::string getPackageFullName(const Record *R) {
Argyrios Kyrtzidis50a47e92011-02-14 17:58:52 +000051 std::string name = getParentPackageFullName(R);
52 if (!name.empty()) name += ".";
53 return name + R->getValueAsString("PackageName");
54}
55
Argyrios Kyrtzidisab8f3fd2011-02-15 07:42:16 +000056static std::string getCheckerFullName(const Record *R) {
Argyrios Kyrtzidis50a47e92011-02-14 17:58:52 +000057 std::string name = getParentPackageFullName(R);
Argyrios Kyrtzidisab8f3fd2011-02-15 07:42:16 +000058 if (isCheckerNamed(R)) {
59 if (!name.empty()) name += ".";
60 name += R->getValueAsString("CheckerName");
61 }
62 return name;
63}
64
65static std::string getStringValue(const Record &R, StringRef field) {
66 if (StringInit *
67 SI = dynamic_cast<StringInit*>(R.getValueInit(field)))
68 return SI->getValue();
69 return std::string();
Argyrios Kyrtzidis50a47e92011-02-14 17:58:52 +000070}
71
72namespace {
73struct GroupInfo {
74 std::vector<const Record*> Checkers;
75 llvm::DenseSet<const Record *> SubGroups;
Argyrios Kyrtzidisab8f3fd2011-02-15 07:42:16 +000076 bool Hidden;
Argyrios Kyrtzidis50a47e92011-02-14 17:58:52 +000077 unsigned Index;
Argyrios Kyrtzidisab8f3fd2011-02-15 07:42:16 +000078
79 GroupInfo() : Hidden(false) { }
Argyrios Kyrtzidis50a47e92011-02-14 17:58:52 +000080};
81}
82
83void ClangSACheckersEmitter::run(raw_ostream &OS) {
84 std::vector<Record*> checkers = Records.getAllDerivedDefinitions("Checker");
85 llvm::DenseMap<const Record *, unsigned> checkerRecIndexMap;
86 for (unsigned i = 0, e = checkers.size(); i != e; ++i)
87 checkerRecIndexMap[checkers[i]] = i;
88
89 OS << "\n#ifdef GET_CHECKERS\n";
90 for (unsigned i = 0, e = checkers.size(); i != e; ++i) {
91 const Record &R = *checkers[i];
Argyrios Kyrtzidisab8f3fd2011-02-15 07:42:16 +000092
Argyrios Kyrtzidis50a47e92011-02-14 17:58:52 +000093 OS << "CHECKER(" << "\"";
Argyrios Kyrtzidisab8f3fd2011-02-15 07:42:16 +000094 std::string name;
95 if (isCheckerNamed(&R))
96 name = getCheckerFullName(&R);
97 OS.write_escaped(name) << "\", ";
98 OS << R.getName() << ", ";
99 OS << getStringValue(R, "DescFile") << ", ";
Argyrios Kyrtzidis50a47e92011-02-14 17:58:52 +0000100 OS << "\"";
Argyrios Kyrtzidisab8f3fd2011-02-15 07:42:16 +0000101 OS.write_escaped(getStringValue(R, "HelpText")) << "\", ";
Argyrios Kyrtzidis50a47e92011-02-14 17:58:52 +0000102 // Hidden bit
103 if (isHidden(R))
104 OS << "true";
105 else
106 OS << "false";
107 OS << ")\n";
108 }
109 OS << "#endif // GET_CHECKERS\n\n";
110
111 // Invert the mapping of checkers to package/group into a one to many
112 // mapping of packages/groups to checkers.
113 std::map<std::string, GroupInfo> groupInfoByName;
114 llvm::DenseMap<const Record *, GroupInfo *> recordGroupMap;
115
116 std::vector<Record*> packages = Records.getAllDerivedDefinitions("Package");
117 for (unsigned i = 0, e = packages.size(); i != e; ++i) {
118 Record *R = packages[i];
119 std::string fullName = getPackageFullName(R);
120 if (!fullName.empty()) {
121 GroupInfo &info = groupInfoByName[fullName];
Argyrios Kyrtzidisab8f3fd2011-02-15 07:42:16 +0000122 info.Hidden = isHidden(*R);
Argyrios Kyrtzidis50a47e92011-02-14 17:58:52 +0000123 recordGroupMap[R] = &info;
124 }
125 }
126
127 std::vector<Record*>
128 checkerGroups = Records.getAllDerivedDefinitions("CheckerGroup");
129 for (unsigned i = 0, e = checkerGroups.size(); i != e; ++i) {
130 Record *R = checkerGroups[i];
131 std::string name = R->getValueAsString("GroupName");
132 if (!name.empty()) {
133 GroupInfo &info = groupInfoByName[name];
134 recordGroupMap[R] = &info;
135 }
136 }
137
138 for (unsigned i = 0, e = checkers.size(); i != e; ++i) {
139 Record *R = checkers[i];
Argyrios Kyrtzidisab8f3fd2011-02-15 07:42:16 +0000140 Record *package = 0;
141 if (DefInit *
142 DI = dynamic_cast<DefInit*>(R->getValueInit("ParentPackage")))
143 package = DI->getDef();
144 if (!isCheckerNamed(R) && !package)
145 throw "Checker '" + R->getName() + "' is neither named, nor in a package!";
146
147 if (isCheckerNamed(R)) {
148 // Create a pseudo-group to hold this checker.
149 std::string fullName = getCheckerFullName(R);
Argyrios Kyrtzidis50a47e92011-02-14 17:58:52 +0000150 GroupInfo &info = groupInfoByName[fullName];
Argyrios Kyrtzidis8bef8002011-02-24 21:33:49 +0000151 info.Hidden = R->getValueAsBit("Hidden");
Argyrios Kyrtzidis50a47e92011-02-14 17:58:52 +0000152 recordGroupMap[R] = &info;
153 info.Checkers.push_back(R);
Argyrios Kyrtzidisab8f3fd2011-02-15 07:42:16 +0000154 } else {
155 recordGroupMap[package]->Checkers.push_back(R);
Argyrios Kyrtzidis50a47e92011-02-14 17:58:52 +0000156 }
Argyrios Kyrtzidisab8f3fd2011-02-15 07:42:16 +0000157
158 Record *currR = isCheckerNamed(R) ? R : package;
159 // Insert the checker and its parent packages into the subgroups set of
160 // the corresponding parent package.
161 while (DefInit *DI
162 = dynamic_cast<DefInit*>(currR->getValueInit("ParentPackage"))) {
163 Record *parentPackage = DI->getDef();
164 recordGroupMap[parentPackage]->SubGroups.insert(currR);
165 currR = parentPackage;
166 }
167 // Insert the checker into the set of its group.
168 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValueInit("Group")))
169 recordGroupMap[DI->getDef()]->Checkers.push_back(R);
Argyrios Kyrtzidis50a47e92011-02-14 17:58:52 +0000170 }
171
172 unsigned index = 0;
173 for (std::map<std::string, GroupInfo>::iterator
174 I = groupInfoByName.begin(), E = groupInfoByName.end(); I != E; ++I)
175 I->second.Index = index++;
176
177 // Walk through the packages/groups/checkers emitting an array for each
178 // set of checkers and an array for each set of subpackages.
179
180 OS << "\n#ifdef GET_MEMBER_ARRAYS\n";
181 unsigned maxLen = 0;
182 for (std::map<std::string, GroupInfo>::iterator
183 I = groupInfoByName.begin(), E = groupInfoByName.end(); I != E; ++I) {
184 maxLen = std::max(maxLen, (unsigned)I->first.size());
185
186 std::vector<const Record*> &V = I->second.Checkers;
187 if (!V.empty()) {
188 OS << "static const short CheckerArray" << I->second.Index << "[] = { ";
189 for (unsigned i = 0, e = V.size(); i != e; ++i)
190 OS << checkerRecIndexMap[V[i]] << ", ";
191 OS << "-1 };\n";
192 }
193
194 llvm::DenseSet<const Record *> &subGroups = I->second.SubGroups;
195 if (!subGroups.empty()) {
196 OS << "static const short SubPackageArray" << I->second.Index << "[] = { ";
197 for (llvm::DenseSet<const Record *>::iterator
198 I = subGroups.begin(), E = subGroups.end(); I != E; ++I) {
199 OS << recordGroupMap[*I]->Index << ", ";
200 }
201 OS << "-1 };\n";
202 }
203 }
204 OS << "#endif // GET_MEMBER_ARRAYS\n\n";
205
206 OS << "\n#ifdef GET_CHECKNAME_TABLE\n";
207 for (std::map<std::string, GroupInfo>::iterator
208 I = groupInfoByName.begin(), E = groupInfoByName.end(); I != E; ++I) {
209 // Group option string.
210 OS << " { \"";
211 OS.write_escaped(I->first) << "\","
212 << std::string(maxLen-I->first.size()+1, ' ');
213
214 if (I->second.Checkers.empty())
215 OS << "0, ";
216 else
217 OS << "CheckerArray" << I->second.Index << ", ";
218
219 // Subgroups.
220 if (I->second.SubGroups.empty())
Argyrios Kyrtzidisab8f3fd2011-02-15 07:42:16 +0000221 OS << "0, ";
Argyrios Kyrtzidis50a47e92011-02-14 17:58:52 +0000222 else
Argyrios Kyrtzidisab8f3fd2011-02-15 07:42:16 +0000223 OS << "SubPackageArray" << I->second.Index << ", ";
224
225 OS << (I->second.Hidden ? "true" : "false");
226
Argyrios Kyrtzidis50a47e92011-02-14 17:58:52 +0000227 OS << " },\n";
228 }
229 OS << "#endif // GET_CHECKNAME_TABLE\n\n";
230}