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