blob: 2df7ca97ce4f47234d53b4cb9866787e3502d585 [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
Peter Collingbourne51d77772011-10-06 13:03:08 +000014#include "llvm/ADT/DenseSet.h"
Peter Collingbourne51d77772011-10-06 13:03:08 +000015#include "llvm/ADT/SmallString.h"
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +000016#include "llvm/ADT/StringMap.h"
17#include "llvm/Support/Compiler.h"
18#include "llvm/Support/Debug.h"
19#include "llvm/TableGen/Record.h"
20#include "llvm/TableGen/TableGenBackend.h"
Peter Collingbourne51d77772011-10-06 13:03:08 +000021#include <algorithm>
22#include <functional>
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +000023#include <map>
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 }
NAKAMURA Takumi3b4c5322012-07-07 02:48:02 +000081
Peter Collingbourne51d77772011-10-06 13:03:08 +000082 // 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
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000166/// ClangDiagsDefsEmitter - The top-level class emits .def files containing
167/// declarations of Clang diagnostics.
168namespace clang {
169void EmitClangDiagsDefs(RecordKeeper &Records, raw_ostream &OS,
170 const std::string &Component) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000171 // Write the #if guard
172 if (!Component.empty()) {
Benjamin Kramer90c78922011-11-06 20:36:48 +0000173 std::string ComponentName = StringRef(Component).upper();
Peter Collingbourne51d77772011-10-06 13:03:08 +0000174 OS << "#ifdef " << ComponentName << "START\n";
175 OS << "__" << ComponentName << "START = DIAG_START_" << ComponentName
176 << ",\n";
177 OS << "#undef " << ComponentName << "START\n";
178 OS << "#endif\n\n";
179 }
180
181 const std::vector<Record*> &Diags =
182 Records.getAllDerivedDefinitions("Diagnostic");
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000183
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000184 std::vector<Record*> DiagGroups
185 = Records.getAllDerivedDefinitions("DiagGroup");
Argyrios Kyrtzidisd42236e2012-03-06 00:00:38 +0000186
187 std::map<std::string, GroupInfo> DiagsInGroup;
188 groupDiagnostics(Diags, DiagGroups, DiagsInGroup);
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000189
Peter Collingbourne51d77772011-10-06 13:03:08 +0000190 DiagCategoryIDMap CategoryIDs(Records);
191 DiagGroupParentMap DGParentMap(Records);
192
193 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
194 const Record &R = *Diags[i];
195 // Filter by component.
196 if (!Component.empty() && Component != R.getValueAsString("Component"))
197 continue;
198
199 OS << "DIAG(" << R.getName() << ", ";
200 OS << R.getValueAsDef("Class")->getName();
201 OS << ", diag::" << R.getValueAsDef("DefaultMapping")->getName();
202
203 // Description string.
204 OS << ", \"";
205 OS.write_escaped(R.getValueAsString("Text")) << '"';
206
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000207 // Warning associated with the diagnostic. This is stored as an index into
208 // the alphabetically sorted warning table.
Peter Collingbourne51d77772011-10-06 13:03:08 +0000209 if (DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Group"))) {
Argyrios Kyrtzidisd42236e2012-03-06 00:00:38 +0000210 std::map<std::string, GroupInfo>::iterator I =
211 DiagsInGroup.find(DI->getDef()->getValueAsString("GroupName"));
212 assert(I != DiagsInGroup.end());
213 OS << ", " << I->second.IDNo;
Peter Collingbourne51d77772011-10-06 13:03:08 +0000214 } else {
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000215 OS << ", 0";
Peter Collingbourne51d77772011-10-06 13:03:08 +0000216 }
217
218 // SFINAE bit
219 if (R.getValueAsBit("SFINAE"))
220 OS << ", true";
221 else
222 OS << ", false";
223
224 // Access control bit
225 if (R.getValueAsBit("AccessControl"))
226 OS << ", true";
227 else
228 OS << ", false";
229
230 // FIXME: This condition is just to avoid temporary revlock, it can be
231 // removed.
232 if (R.getValue("WarningNoWerror")) {
233 // Default warning has no Werror bit.
234 if (R.getValueAsBit("WarningNoWerror"))
235 OS << ", true";
236 else
237 OS << ", false";
238
239 // Default warning show in system header bit.
240 if (R.getValueAsBit("WarningShowInSystemHeader"))
241 OS << ", true";
242 else
243 OS << ", false";
244 }
245
246 // Category number.
247 OS << ", " << CategoryIDs.getID(getDiagnosticCategory(&R, DGParentMap));
Peter Collingbourne51d77772011-10-06 13:03:08 +0000248 OS << ")\n";
249 }
250}
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000251} // end namespace clang
Peter Collingbourne51d77772011-10-06 13:03:08 +0000252
253//===----------------------------------------------------------------------===//
254// Warning Group Tables generation
255//===----------------------------------------------------------------------===//
256
257static std::string getDiagCategoryEnum(llvm::StringRef name) {
258 if (name.empty())
259 return "DiagCat_None";
Dylan Noblesmith36d59272012-02-13 12:32:26 +0000260 SmallString<256> enumName = llvm::StringRef("DiagCat_");
Peter Collingbourne51d77772011-10-06 13:03:08 +0000261 for (llvm::StringRef::iterator I = name.begin(), E = name.end(); I != E; ++I)
262 enumName += isalnum(*I) ? *I : '_';
263 return enumName.str();
264}
NAKAMURA Takumi3b4c5322012-07-07 02:48:02 +0000265
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000266namespace clang {
267void EmitClangDiagGroups(RecordKeeper &Records, raw_ostream &OS) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000268 // Compute a mapping from a DiagGroup to all of its parents.
269 DiagGroupParentMap DGParentMap(Records);
NAKAMURA Takumi3b4c5322012-07-07 02:48:02 +0000270
Peter Collingbourne51d77772011-10-06 13:03:08 +0000271 std::vector<Record*> Diags =
272 Records.getAllDerivedDefinitions("Diagnostic");
Peter Collingbourne51d77772011-10-06 13:03:08 +0000273
Peter Collingbourne51d77772011-10-06 13:03:08 +0000274 std::vector<Record*> DiagGroups
275 = Records.getAllDerivedDefinitions("DiagGroup");
Argyrios Kyrtzidisd42236e2012-03-06 00:00:38 +0000276
277 std::map<std::string, GroupInfo> DiagsInGroup;
278 groupDiagnostics(Diags, DiagGroups, DiagsInGroup);
NAKAMURA Takumi3b4c5322012-07-07 02:48:02 +0000279
Peter Collingbourne51d77772011-10-06 13:03:08 +0000280 // Walk through the groups emitting an array for each diagnostic of the diags
281 // that are mapped to.
282 OS << "\n#ifdef GET_DIAG_ARRAYS\n";
283 unsigned MaxLen = 0;
284 for (std::map<std::string, GroupInfo>::iterator
285 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
286 MaxLen = std::max(MaxLen, (unsigned)I->first.size());
NAKAMURA Takumi3b4c5322012-07-07 02:48:02 +0000287
Peter Collingbourne51d77772011-10-06 13:03:08 +0000288 std::vector<const Record*> &V = I->second.DiagsInGroup;
NAKAMURA Takumi3b4c5322012-07-07 02:48:02 +0000289 if (!V.empty()) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000290 OS << "static const short DiagArray" << I->second.IDNo << "[] = { ";
291 for (unsigned i = 0, e = V.size(); i != e; ++i)
292 OS << "diag::" << V[i]->getName() << ", ";
293 OS << "-1 };\n";
294 }
295
296 const std::vector<std::string> &SubGroups = I->second.SubGroups;
NAKAMURA Takumi3b4c5322012-07-07 02:48:02 +0000297 if (!SubGroups.empty()) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000298 OS << "static const short DiagSubGroup" << I->second.IDNo << "[] = { ";
299 for (unsigned i = 0, e = SubGroups.size(); i != e; ++i) {
300 std::map<std::string, GroupInfo>::iterator RI =
301 DiagsInGroup.find(SubGroups[i]);
302 assert(RI != DiagsInGroup.end() && "Referenced without existing?");
303 OS << RI->second.IDNo << ", ";
304 }
305 OS << "-1 };\n";
306 }
307 }
308 OS << "#endif // GET_DIAG_ARRAYS\n\n";
309
310 // Emit the table now.
311 OS << "\n#ifdef GET_DIAG_TABLE\n";
312 for (std::map<std::string, GroupInfo>::iterator
313 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
314 // Group option string.
315 OS << " { ";
316 OS << I->first.size() << ", ";
317 OS << "\"";
Benjamin Kramer037ad1b2011-11-15 12:54:53 +0000318 if (I->first.find_first_not_of("abcdefghijklmnopqrstuvwxyz"
319 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
320 "0123456789!@#$%^*-+=:?")!=std::string::npos)
321 throw "Invalid character in diagnostic group '" + I->first + "'";
Peter Collingbourne51d77772011-10-06 13:03:08 +0000322 OS.write_escaped(I->first) << "\","
323 << std::string(MaxLen-I->first.size()+1, ' ');
NAKAMURA Takumi3b4c5322012-07-07 02:48:02 +0000324
Peter Collingbourne51d77772011-10-06 13:03:08 +0000325 // Diagnostics in the group.
NAKAMURA Takumi3b4c5322012-07-07 02:48:02 +0000326 if (I->second.DiagsInGroup.empty())
Peter Collingbourne51d77772011-10-06 13:03:08 +0000327 OS << "0, ";
328 else
329 OS << "DiagArray" << I->second.IDNo << ", ";
330
331 // Subgroups.
NAKAMURA Takumi3b4c5322012-07-07 02:48:02 +0000332 if (I->second.SubGroups.empty())
Peter Collingbourne51d77772011-10-06 13:03:08 +0000333 OS << 0;
334 else
335 OS << "DiagSubGroup" << I->second.IDNo;
336 OS << " },\n";
337 }
338 OS << "#endif // GET_DIAG_TABLE\n\n";
339
340 // Emit the category table next.
341 DiagCategoryIDMap CategoriesByID(Records);
342 OS << "\n#ifdef GET_CATEGORY_TABLE\n";
343 for (DiagCategoryIDMap::iterator I = CategoriesByID.begin(),
344 E = CategoriesByID.end(); I != E; ++I)
345 OS << "CATEGORY(\"" << *I << "\", " << getDiagCategoryEnum(*I) << ")\n";
346 OS << "#endif // GET_CATEGORY_TABLE\n\n";
347}
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000348} // end namespace clang
Peter Collingbourne51d77772011-10-06 13:03:08 +0000349
350//===----------------------------------------------------------------------===//
351// Diagnostic name index generation
352//===----------------------------------------------------------------------===//
353
354namespace {
355struct RecordIndexElement
356{
357 RecordIndexElement() {}
358 explicit RecordIndexElement(Record const &R):
359 Name(R.getName()) {}
360
361 std::string Name;
362};
363
364struct RecordIndexElementSorter :
365 public std::binary_function<RecordIndexElement, RecordIndexElement, bool> {
366
367 bool operator()(RecordIndexElement const &Lhs,
368 RecordIndexElement const &Rhs) const {
369 return Lhs.Name < Rhs.Name;
370 }
371
372};
373
374} // end anonymous namespace.
375
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000376namespace clang {
377void EmitClangDiagsIndexName(RecordKeeper &Records, raw_ostream &OS) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000378 const std::vector<Record*> &Diags =
379 Records.getAllDerivedDefinitions("Diagnostic");
380
381 std::vector<RecordIndexElement> Index;
382 Index.reserve(Diags.size());
383 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
384 const Record &R = *(Diags[i]);
385 Index.push_back(RecordIndexElement(R));
386 }
387
388 std::sort(Index.begin(), Index.end(), RecordIndexElementSorter());
389
390 for (unsigned i = 0, e = Index.size(); i != e; ++i) {
391 const RecordIndexElement &R = Index[i];
392
393 OS << "DIAG_NAME_INDEX(" << R.Name << ")\n";
394 }
395}
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000396} // end namespace clang