blob: da2fb70b4a573f2bb31e2aede4c511950676e17c [file] [log] [blame]
Peter Collingbourne51d77772011-10-06 13:03:08 +00001//=- ClangDiagnosticsEmitter.cpp - Generate Clang diagnostics 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// These tablegen backends emit Clang diagnostics tables.
11//
12//===----------------------------------------------------------------------===//
13
14#include "ClangDiagnosticsEmitter.h"
15#include "llvm/TableGen/Record.h"
16#include "llvm/Support/Debug.h"
17#include "llvm/Support/Compiler.h"
18#include "llvm/ADT/DenseSet.h"
19#include "llvm/ADT/StringExtras.h"
20#include "llvm/ADT/StringMap.h"
21#include "llvm/ADT/SmallString.h"
22#include "llvm/ADT/VectorExtras.h"
23#include <map>
24#include <algorithm>
25#include <functional>
26using namespace llvm;
27
28//===----------------------------------------------------------------------===//
29// Diagnostic category computation code.
30//===----------------------------------------------------------------------===//
31
32namespace {
33class DiagGroupParentMap {
34 RecordKeeper &Records;
35 std::map<const Record*, std::vector<Record*> > Mapping;
36public:
37 DiagGroupParentMap(RecordKeeper &records) : Records(records) {
38 std::vector<Record*> DiagGroups
39 = Records.getAllDerivedDefinitions("DiagGroup");
40 for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
41 std::vector<Record*> SubGroups =
42 DiagGroups[i]->getValueAsListOfDefs("SubGroups");
43 for (unsigned j = 0, e = SubGroups.size(); j != e; ++j)
44 Mapping[SubGroups[j]].push_back(DiagGroups[i]);
45 }
46 }
47
48 const std::vector<Record*> &getParents(const Record *Group) {
49 return Mapping[Group];
50 }
51};
52} // end anonymous namespace.
53
54
55static std::string
56getCategoryFromDiagGroup(const Record *Group,
57 DiagGroupParentMap &DiagGroupParents) {
58 // If the DiagGroup has a category, return it.
59 std::string CatName = Group->getValueAsString("CategoryName");
60 if (!CatName.empty()) return CatName;
61
62 // The diag group may the subgroup of one or more other diagnostic groups,
63 // check these for a category as well.
64 const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
65 for (unsigned i = 0, e = Parents.size(); i != e; ++i) {
66 CatName = getCategoryFromDiagGroup(Parents[i], DiagGroupParents);
67 if (!CatName.empty()) return CatName;
68 }
69 return "";
70}
71
72/// getDiagnosticCategory - Return the category that the specified diagnostic
73/// lives in.
74static std::string getDiagnosticCategory(const Record *R,
75 DiagGroupParentMap &DiagGroupParents) {
76 // If the diagnostic is in a group, and that group has a category, use it.
77 if (DefInit *Group = dynamic_cast<DefInit*>(R->getValueInit("Group"))) {
78 // Check the diagnostic's diag group for a category.
79 std::string CatName = getCategoryFromDiagGroup(Group->getDef(),
80 DiagGroupParents);
81 if (!CatName.empty()) return CatName;
82 }
83
84 // If the diagnostic itself has a category, get it.
85 return R->getValueAsString("CategoryName");
86}
87
88namespace {
89 class DiagCategoryIDMap {
90 RecordKeeper &Records;
91 StringMap<unsigned> CategoryIDs;
92 std::vector<std::string> CategoryStrings;
93 public:
94 DiagCategoryIDMap(RecordKeeper &records) : Records(records) {
95 DiagGroupParentMap ParentInfo(Records);
96
97 // The zero'th category is "".
98 CategoryStrings.push_back("");
99 CategoryIDs[""] = 0;
100
101 std::vector<Record*> Diags =
102 Records.getAllDerivedDefinitions("Diagnostic");
103 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
104 std::string Category = getDiagnosticCategory(Diags[i], ParentInfo);
105 if (Category.empty()) continue; // Skip diags with no category.
106
107 unsigned &ID = CategoryIDs[Category];
108 if (ID != 0) continue; // Already seen.
109
110 ID = CategoryStrings.size();
111 CategoryStrings.push_back(Category);
112 }
113 }
114
115 unsigned getID(StringRef CategoryString) {
116 return CategoryIDs[CategoryString];
117 }
118
119 typedef std::vector<std::string>::iterator iterator;
120 iterator begin() { return CategoryStrings.begin(); }
121 iterator end() { return CategoryStrings.end(); }
122 };
123} // end anonymous namespace.
124
125
126//===----------------------------------------------------------------------===//
127// Warning Tables (.inc file) generation.
128//===----------------------------------------------------------------------===//
129
130void ClangDiagsDefsEmitter::run(raw_ostream &OS) {
131 // Write the #if guard
132 if (!Component.empty()) {
133 std::string ComponentName = UppercaseString(Component);
134 OS << "#ifdef " << ComponentName << "START\n";
135 OS << "__" << ComponentName << "START = DIAG_START_" << ComponentName
136 << ",\n";
137 OS << "#undef " << ComponentName << "START\n";
138 OS << "#endif\n\n";
139 }
140
141 const std::vector<Record*> &Diags =
142 Records.getAllDerivedDefinitions("Diagnostic");
143
144 DiagCategoryIDMap CategoryIDs(Records);
145 DiagGroupParentMap DGParentMap(Records);
146
147 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
148 const Record &R = *Diags[i];
149 // Filter by component.
150 if (!Component.empty() && Component != R.getValueAsString("Component"))
151 continue;
152
153 OS << "DIAG(" << R.getName() << ", ";
154 OS << R.getValueAsDef("Class")->getName();
155 OS << ", diag::" << R.getValueAsDef("DefaultMapping")->getName();
156
157 // Description string.
158 OS << ", \"";
159 OS.write_escaped(R.getValueAsString("Text")) << '"';
160
161 // Warning associated with the diagnostic.
162 if (DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Group"))) {
163 OS << ", \"";
164 OS.write_escaped(DI->getDef()->getValueAsString("GroupName")) << '"';
165 } else {
166 OS << ", \"\"";
167 }
168
169 // SFINAE bit
170 if (R.getValueAsBit("SFINAE"))
171 OS << ", true";
172 else
173 OS << ", false";
174
175 // Access control bit
176 if (R.getValueAsBit("AccessControl"))
177 OS << ", true";
178 else
179 OS << ", false";
180
181 // FIXME: This condition is just to avoid temporary revlock, it can be
182 // removed.
183 if (R.getValue("WarningNoWerror")) {
184 // Default warning has no Werror bit.
185 if (R.getValueAsBit("WarningNoWerror"))
186 OS << ", true";
187 else
188 OS << ", false";
189
190 // Default warning show in system header bit.
191 if (R.getValueAsBit("WarningShowInSystemHeader"))
192 OS << ", true";
193 else
194 OS << ", false";
195 }
196
197 // Category number.
198 OS << ", " << CategoryIDs.getID(getDiagnosticCategory(&R, DGParentMap));
199
200 // Brief
201 OS << ", \"";
202 OS.write_escaped(R.getValueAsString("Brief")) << '"';
203
204 // Explanation
205 OS << ", \"";
206 OS.write_escaped(R.getValueAsString("Explanation")) << '"';
207 OS << ")\n";
208 }
209}
210
211//===----------------------------------------------------------------------===//
212// Warning Group Tables generation
213//===----------------------------------------------------------------------===//
214
215static std::string getDiagCategoryEnum(llvm::StringRef name) {
216 if (name.empty())
217 return "DiagCat_None";
218 llvm::SmallString<256> enumName = llvm::StringRef("DiagCat_");
219 for (llvm::StringRef::iterator I = name.begin(), E = name.end(); I != E; ++I)
220 enumName += isalnum(*I) ? *I : '_';
221 return enumName.str();
222}
223
224namespace {
225struct GroupInfo {
226 std::vector<const Record*> DiagsInGroup;
227 std::vector<std::string> SubGroups;
228 unsigned IDNo;
229};
230} // end anonymous namespace.
231
232void ClangDiagGroupsEmitter::run(raw_ostream &OS) {
233 // Compute a mapping from a DiagGroup to all of its parents.
234 DiagGroupParentMap DGParentMap(Records);
235
236 // Invert the 1-[0/1] mapping of diags to group into a one to many mapping of
237 // groups to diags in the group.
238 std::map<std::string, GroupInfo> DiagsInGroup;
239
240 std::vector<Record*> Diags =
241 Records.getAllDerivedDefinitions("Diagnostic");
242 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
243 const Record *R = Diags[i];
244 DefInit *DI = dynamic_cast<DefInit*>(R->getValueInit("Group"));
245 if (DI == 0) continue;
246 std::string GroupName = DI->getDef()->getValueAsString("GroupName");
247 DiagsInGroup[GroupName].DiagsInGroup.push_back(R);
248 }
249
250 // Add all DiagGroup's to the DiagsInGroup list to make sure we pick up empty
251 // groups (these are warnings that GCC supports that clang never produces).
252 std::vector<Record*> DiagGroups
253 = Records.getAllDerivedDefinitions("DiagGroup");
254 for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
255 Record *Group = DiagGroups[i];
256 GroupInfo &GI = DiagsInGroup[Group->getValueAsString("GroupName")];
257
258 std::vector<Record*> SubGroups = Group->getValueAsListOfDefs("SubGroups");
259 for (unsigned j = 0, e = SubGroups.size(); j != e; ++j)
260 GI.SubGroups.push_back(SubGroups[j]->getValueAsString("GroupName"));
261 }
262
263 // Assign unique ID numbers to the groups.
264 unsigned IDNo = 0;
265 for (std::map<std::string, GroupInfo>::iterator
266 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I, ++IDNo)
267 I->second.IDNo = IDNo;
268
269 // Walk through the groups emitting an array for each diagnostic of the diags
270 // that are mapped to.
271 OS << "\n#ifdef GET_DIAG_ARRAYS\n";
272 unsigned MaxLen = 0;
273 for (std::map<std::string, GroupInfo>::iterator
274 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
275 MaxLen = std::max(MaxLen, (unsigned)I->first.size());
276
277 std::vector<const Record*> &V = I->second.DiagsInGroup;
278 if (!V.empty()) {
279 OS << "static const short DiagArray" << I->second.IDNo << "[] = { ";
280 for (unsigned i = 0, e = V.size(); i != e; ++i)
281 OS << "diag::" << V[i]->getName() << ", ";
282 OS << "-1 };\n";
283 }
284
285 const std::vector<std::string> &SubGroups = I->second.SubGroups;
286 if (!SubGroups.empty()) {
287 OS << "static const short DiagSubGroup" << I->second.IDNo << "[] = { ";
288 for (unsigned i = 0, e = SubGroups.size(); i != e; ++i) {
289 std::map<std::string, GroupInfo>::iterator RI =
290 DiagsInGroup.find(SubGroups[i]);
291 assert(RI != DiagsInGroup.end() && "Referenced without existing?");
292 OS << RI->second.IDNo << ", ";
293 }
294 OS << "-1 };\n";
295 }
296 }
297 OS << "#endif // GET_DIAG_ARRAYS\n\n";
298
299 // Emit the table now.
300 OS << "\n#ifdef GET_DIAG_TABLE\n";
301 for (std::map<std::string, GroupInfo>::iterator
302 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
303 // Group option string.
304 OS << " { ";
305 OS << I->first.size() << ", ";
306 OS << "\"";
307 OS.write_escaped(I->first) << "\","
308 << std::string(MaxLen-I->first.size()+1, ' ');
309
310 // Diagnostics in the group.
311 if (I->second.DiagsInGroup.empty())
312 OS << "0, ";
313 else
314 OS << "DiagArray" << I->second.IDNo << ", ";
315
316 // Subgroups.
317 if (I->second.SubGroups.empty())
318 OS << 0;
319 else
320 OS << "DiagSubGroup" << I->second.IDNo;
321 OS << " },\n";
322 }
323 OS << "#endif // GET_DIAG_TABLE\n\n";
324
325 // Emit the category table next.
326 DiagCategoryIDMap CategoriesByID(Records);
327 OS << "\n#ifdef GET_CATEGORY_TABLE\n";
328 for (DiagCategoryIDMap::iterator I = CategoriesByID.begin(),
329 E = CategoriesByID.end(); I != E; ++I)
330 OS << "CATEGORY(\"" << *I << "\", " << getDiagCategoryEnum(*I) << ")\n";
331 OS << "#endif // GET_CATEGORY_TABLE\n\n";
332}
333
334//===----------------------------------------------------------------------===//
335// Diagnostic name index generation
336//===----------------------------------------------------------------------===//
337
338namespace {
339struct RecordIndexElement
340{
341 RecordIndexElement() {}
342 explicit RecordIndexElement(Record const &R):
343 Name(R.getName()) {}
344
345 std::string Name;
346};
347
348struct RecordIndexElementSorter :
349 public std::binary_function<RecordIndexElement, RecordIndexElement, bool> {
350
351 bool operator()(RecordIndexElement const &Lhs,
352 RecordIndexElement const &Rhs) const {
353 return Lhs.Name < Rhs.Name;
354 }
355
356};
357
358} // end anonymous namespace.
359
360void ClangDiagsIndexNameEmitter::run(raw_ostream &OS) {
361 const std::vector<Record*> &Diags =
362 Records.getAllDerivedDefinitions("Diagnostic");
363
364 std::vector<RecordIndexElement> Index;
365 Index.reserve(Diags.size());
366 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
367 const Record &R = *(Diags[i]);
368 Index.push_back(RecordIndexElement(R));
369 }
370
371 std::sort(Index.begin(), Index.end(), RecordIndexElementSorter());
372
373 for (unsigned i = 0, e = Index.size(); i != e; ++i) {
374 const RecordIndexElement &R = Index[i];
375
376 OS << "DIAG_NAME_INDEX(" << R.Name << ")\n";
377 }
378}