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