blob: 4c480ec4727143b71a026b80e3b8d1eb228654e7 [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
Ted Kremenek25570a92012-07-06 23:07:31 +000014#include "llvm/ADT/PointerUnion.h"
Peter Collingbourne51d77772011-10-06 13:03:08 +000015#include "llvm/ADT/DenseSet.h"
Peter Collingbourne51d77772011-10-06 13:03:08 +000016#include "llvm/ADT/SmallString.h"
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +000017#include "llvm/ADT/StringMap.h"
Ted Kremenek25570a92012-07-06 23:07:31 +000018#include "llvm/ADT/Optional.h"
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +000019#include "llvm/Support/Compiler.h"
20#include "llvm/Support/Debug.h"
21#include "llvm/TableGen/Record.h"
22#include "llvm/TableGen/TableGenBackend.h"
Peter Collingbourne51d77772011-10-06 13:03:08 +000023#include <algorithm>
24#include <functional>
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +000025#include <map>
Benjamin Kramerd49cb202012-02-15 20:57:03 +000026#include <set>
Peter Collingbourne51d77772011-10-06 13:03:08 +000027using namespace llvm;
28
29//===----------------------------------------------------------------------===//
30// Diagnostic category computation code.
31//===----------------------------------------------------------------------===//
32
33namespace {
34class DiagGroupParentMap {
35 RecordKeeper &Records;
36 std::map<const Record*, std::vector<Record*> > Mapping;
37public:
38 DiagGroupParentMap(RecordKeeper &records) : Records(records) {
39 std::vector<Record*> DiagGroups
40 = Records.getAllDerivedDefinitions("DiagGroup");
41 for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
42 std::vector<Record*> SubGroups =
43 DiagGroups[i]->getValueAsListOfDefs("SubGroups");
44 for (unsigned j = 0, e = SubGroups.size(); j != e; ++j)
45 Mapping[SubGroups[j]].push_back(DiagGroups[i]);
46 }
47 }
48
49 const std::vector<Record*> &getParents(const Record *Group) {
50 return Mapping[Group];
51 }
52};
53} // end anonymous namespace.
54
Peter Collingbourne51d77772011-10-06 13:03:08 +000055static 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 }
Ted Kremenek25570a92012-07-06 23:07:31 +000083
Peter Collingbourne51d77772011-10-06 13:03:08 +000084 // 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 };
Argyrios Kyrtzidisd42236e2012-03-06 00:00:38 +0000123
124 struct GroupInfo {
125 std::vector<const Record*> DiagsInGroup;
126 std::vector<std::string> SubGroups;
127 unsigned IDNo;
128 };
Peter Collingbourne51d77772011-10-06 13:03:08 +0000129} // end anonymous namespace.
130
Argyrios Kyrtzidisd42236e2012-03-06 00:00:38 +0000131/// \brief Invert the 1-[0/1] mapping of diags to group into a one to many
132/// mapping of groups to diags in the group.
133static void groupDiagnostics(const std::vector<Record*> &Diags,
134 const std::vector<Record*> &DiagGroups,
135 std::map<std::string, GroupInfo> &DiagsInGroup) {
136 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
137 const Record *R = Diags[i];
138 DefInit *DI = dynamic_cast<DefInit*>(R->getValueInit("Group"));
139 if (DI == 0) continue;
Richard Smith46484772012-05-04 19:05:50 +0000140 assert(R->getValueAsDef("Class")->getName() != "CLASS_NOTE" &&
141 "Note can't be in a DiagGroup");
Argyrios Kyrtzidisd42236e2012-03-06 00:00:38 +0000142 std::string GroupName = DI->getDef()->getValueAsString("GroupName");
143 DiagsInGroup[GroupName].DiagsInGroup.push_back(R);
144 }
145
146 // Add all DiagGroup's to the DiagsInGroup list to make sure we pick up empty
147 // groups (these are warnings that GCC supports that clang never produces).
148 for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
149 Record *Group = DiagGroups[i];
150 GroupInfo &GI = DiagsInGroup[Group->getValueAsString("GroupName")];
151
152 std::vector<Record*> SubGroups = Group->getValueAsListOfDefs("SubGroups");
153 for (unsigned j = 0, e = SubGroups.size(); j != e; ++j)
154 GI.SubGroups.push_back(SubGroups[j]->getValueAsString("GroupName"));
155 }
156
157 // Assign unique ID numbers to the groups.
158 unsigned IDNo = 0;
159 for (std::map<std::string, GroupInfo>::iterator
160 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I, ++IDNo)
161 I->second.IDNo = IDNo;
162}
Peter Collingbourne51d77772011-10-06 13:03:08 +0000163
164//===----------------------------------------------------------------------===//
Ted Kremenek25570a92012-07-06 23:07:31 +0000165// Infer members of -Wpedantic.
166//===----------------------------------------------------------------------===//
167
168typedef std::vector<const Record *> RecordVec;
169typedef llvm::DenseSet<const Record *> RecordSet;
170typedef llvm::PointerUnion<RecordVec*, RecordSet*> VecOrSet;
171
172namespace {
173class InferPedantic {
174 typedef llvm::DenseMap<const Record*,
175 std::pair<unsigned, llvm::Optional<unsigned> > > GMap;
176
177 DiagGroupParentMap &DiagGroupParents;
178 const std::vector<Record*> &Diags;
179 const std::vector<Record*> DiagGroups;
180 std::map<std::string, GroupInfo> &DiagsInGroup;
181 llvm::DenseSet<const Record*> DiagsSet;
182 GMap GroupCount;
183public:
184 InferPedantic(DiagGroupParentMap &DiagGroupParents,
185 const std::vector<Record*> &Diags,
186 const std::vector<Record*> &DiagGroups,
187 std::map<std::string, GroupInfo> &DiagsInGroup)
188 : DiagGroupParents(DiagGroupParents),
189 Diags(Diags),
190 DiagGroups(DiagGroups),
191 DiagsInGroup(DiagsInGroup) {}
192
193 /// Compute the set of diagnostics and groups that are immediately
194 /// in -Wpedantic.
195 void compute(VecOrSet DiagsInPedantic,
196 VecOrSet GroupsInPedantic);
197
198private:
199 /// Determine whether a group is a subgroup of another group.
200 bool isSubGroupOfGroup(const Record *Group,
201 llvm::StringRef RootGroupName);
202
203 /// Determine if the diagnostic is an extension.
204 bool isExtension(const Record *Diag);
205
206 /// Increment the count for a group, and transitively marked
207 /// parent groups when appropriate.
208 void markGroup(const Record *Group);
209
210 /// Return true if the diagnostic is in a pedantic group.
211 bool groupInPedantic(const Record *Group, bool increment = false);
212};
213} // end anonymous namespace
214
215bool InferPedantic::isSubGroupOfGroup(const Record *Group,
216 llvm::StringRef GName) {
217
218 const std::string &GroupName = Group->getValueAsString("GroupName");
219 if (GName == GroupName)
220 return true;
221
222 const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
223 for (unsigned i = 0, e = Parents.size(); i != e; ++i)
224 if (isSubGroupOfGroup(Parents[i], GName))
225 return true;
226
227 return false;
228}
229
230/// Determine if the diagnostic is an extension.
231bool InferPedantic::isExtension(const Record *Diag) {
232 const std::string &ClsName = Diag->getValueAsDef("Class")->getName();
233 return ClsName == "CLASS_EXTENSION";
234}
235
236bool InferPedantic::groupInPedantic(const Record *Group, bool increment) {
237 GMap::mapped_type &V = GroupCount[Group];
238 // Lazily compute the threshold value for the group count.
239 if (!V.second.hasValue()) {
240 const GroupInfo &GI = DiagsInGroup[Group->getValueAsString("GroupName")];
241 V.second = GI.SubGroups.size() + GI.DiagsInGroup.size();
242 }
243
244 if (increment)
245 ++V.first;
246
247 // Consider a group in -Wpendatic IFF if has at least one diagnostic
248 // or subgroup AND all of those diagnostics and subgroups are covered
249 // by -Wpedantic via our computation.
250 return V.first != 0 && V.first == V.second.getValue();
251}
252
253void InferPedantic::markGroup(const Record *Group) {
254 // If all the diagnostics and subgroups have been marked as being
255 // covered by -Wpedantic, increment the count of parent groups. Once the
256 // group's count is equal to the number of subgroups and diagnostics in
257 // that group, we can safely add this group to -Wpedantic.
258 if (groupInPedantic(Group, /* increment */ true)) {
259 const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
260 for (unsigned i = 0, e = Parents.size(); i != e; ++i)
261 markGroup(Parents[i]);
262 }
263}
264
265void InferPedantic::compute(VecOrSet DiagsInPedantic,
266 VecOrSet GroupsInPedantic) {
267 // All extensions are implicitly in the "pedantic" group. For those that
268 // aren't explicitly included in -Wpedantic, mark them for consideration
269 // to be included in -Wpedantic directly.
270 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
271 Record *R = Diags[i];
272 if (isExtension(R))
273 DiagsSet.insert(R);
274 if (DefInit *Group = dynamic_cast<DefInit*>(R->getValueInit("Group"))) {
275 const Record *GroupRec = Group->getDef();
276 if (!isSubGroupOfGroup(GroupRec, "pedantic")) {
277 markGroup(GroupRec);
278 }
279 }
280 }
281
282 // Compute the set of diagnostics that are directly in -Wpedantic. We
283 // march through Diags a second time to ensure the results are emitted
284 // in deterministic order.
285 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
286 Record *R = Diags[i];
287 if (!DiagsSet.count(R))
288 continue;
289 // Check if the group is implicitly in -Wpedantic. If so,
290 // the diagnostic should not be directly included in the -Wpedantic
291 // diagnostic group.
292 if (DefInit *Group = dynamic_cast<DefInit*>(R->getValueInit("Group")))
293 if (groupInPedantic(Group->getDef()))
294 continue;
295
296 // The diagnostic is not included in a group that is (transitively) in
297 // -Wpedantic. Include it in -Wpedantic directly.
298 if (RecordVec *V = DiagsInPedantic.dyn_cast<RecordVec*>())
299 V->push_back(R);
300 else {
301 DiagsInPedantic.get<RecordSet*>()->insert(R);
302 }
303 }
304
305 if (!GroupsInPedantic)
306 return;
307
308 // Compute the set of groups that are directly in -Wpedantic. We
309 // march through the groups to ensure the results are emitted
310 /// in a deterministc order.
311 for (unsigned i = 0, ei = DiagGroups.size(); i != ei; ++i) {
312 Record *Group = DiagGroups[i];
313 if (!groupInPedantic(Group))
314 continue;
315
316 unsigned ParentsInPedantic = 0;
317 const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
318 for (unsigned j = 0, ej = Parents.size(); j != ej; ++j) {
319 if (groupInPedantic(Parents[j]))
320 ++ParentsInPedantic;
321 }
322 // If all the parents are in -Wpedantic, this means that this diagnostic
323 // group will be indirectly included by -Wpedantic already. In that
324 // case, do not add it directly to -Wpedantic. If the group has no
325 // parents, obviously it should go into -Wpedantic.
326 if (Parents.size() > 0 && ParentsInPedantic == Parents.size())
327 continue;
328
329 if (RecordVec *V = GroupsInPedantic.dyn_cast<RecordVec*>())
330 V->push_back(Group);
331 else {
332 GroupsInPedantic.get<RecordSet*>()->insert(Group);
333 }
334 }
335}
336
337//===----------------------------------------------------------------------===//
Peter Collingbourne51d77772011-10-06 13:03:08 +0000338// Warning Tables (.inc file) generation.
339//===----------------------------------------------------------------------===//
340
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000341/// ClangDiagsDefsEmitter - The top-level class emits .def files containing
342/// declarations of Clang diagnostics.
343namespace clang {
344void EmitClangDiagsDefs(RecordKeeper &Records, raw_ostream &OS,
345 const std::string &Component) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000346 // Write the #if guard
347 if (!Component.empty()) {
Benjamin Kramer90c78922011-11-06 20:36:48 +0000348 std::string ComponentName = StringRef(Component).upper();
Peter Collingbourne51d77772011-10-06 13:03:08 +0000349 OS << "#ifdef " << ComponentName << "START\n";
350 OS << "__" << ComponentName << "START = DIAG_START_" << ComponentName
351 << ",\n";
352 OS << "#undef " << ComponentName << "START\n";
353 OS << "#endif\n\n";
354 }
355
356 const std::vector<Record*> &Diags =
357 Records.getAllDerivedDefinitions("Diagnostic");
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000358
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000359 std::vector<Record*> DiagGroups
360 = Records.getAllDerivedDefinitions("DiagGroup");
Argyrios Kyrtzidisd42236e2012-03-06 00:00:38 +0000361
362 std::map<std::string, GroupInfo> DiagsInGroup;
363 groupDiagnostics(Diags, DiagGroups, DiagsInGroup);
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000364
Peter Collingbourne51d77772011-10-06 13:03:08 +0000365 DiagCategoryIDMap CategoryIDs(Records);
366 DiagGroupParentMap DGParentMap(Records);
367
Ted Kremenek25570a92012-07-06 23:07:31 +0000368 // Compute the set of diagnostics that are in -Wpedantic.
369 RecordSet DiagsInPedantic;
370 InferPedantic inferPedantic(DGParentMap, Diags, DiagGroups, DiagsInGroup);
371 inferPedantic.compute(&DiagsInPedantic, (RecordVec*)0);
372
Peter Collingbourne51d77772011-10-06 13:03:08 +0000373 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
374 const Record &R = *Diags[i];
375 // Filter by component.
376 if (!Component.empty() && Component != R.getValueAsString("Component"))
377 continue;
378
379 OS << "DIAG(" << R.getName() << ", ";
380 OS << R.getValueAsDef("Class")->getName();
381 OS << ", diag::" << R.getValueAsDef("DefaultMapping")->getName();
382
383 // Description string.
384 OS << ", \"";
385 OS.write_escaped(R.getValueAsString("Text")) << '"';
386
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000387 // Warning associated with the diagnostic. This is stored as an index into
388 // the alphabetically sorted warning table.
Peter Collingbourne51d77772011-10-06 13:03:08 +0000389 if (DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Group"))) {
Argyrios Kyrtzidisd42236e2012-03-06 00:00:38 +0000390 std::map<std::string, GroupInfo>::iterator I =
391 DiagsInGroup.find(DI->getDef()->getValueAsString("GroupName"));
392 assert(I != DiagsInGroup.end());
393 OS << ", " << I->second.IDNo;
Ted Kremenek25570a92012-07-06 23:07:31 +0000394 } else if (DiagsInPedantic.count(&R)) {
395 std::map<std::string, GroupInfo>::iterator I =
396 DiagsInGroup.find("pedantic");
397 assert(I != DiagsInGroup.end() && "pedantic group not defined");
398 OS << ", " << I->second.IDNo;
Peter Collingbourne51d77772011-10-06 13:03:08 +0000399 } else {
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000400 OS << ", 0";
Peter Collingbourne51d77772011-10-06 13:03:08 +0000401 }
402
403 // SFINAE bit
404 if (R.getValueAsBit("SFINAE"))
405 OS << ", true";
406 else
407 OS << ", false";
408
409 // Access control bit
410 if (R.getValueAsBit("AccessControl"))
411 OS << ", true";
412 else
413 OS << ", false";
414
415 // FIXME: This condition is just to avoid temporary revlock, it can be
416 // removed.
417 if (R.getValue("WarningNoWerror")) {
418 // Default warning has no Werror bit.
419 if (R.getValueAsBit("WarningNoWerror"))
420 OS << ", true";
421 else
422 OS << ", false";
423
424 // Default warning show in system header bit.
425 if (R.getValueAsBit("WarningShowInSystemHeader"))
426 OS << ", true";
427 else
428 OS << ", false";
429 }
430
431 // Category number.
432 OS << ", " << CategoryIDs.getID(getDiagnosticCategory(&R, DGParentMap));
Peter Collingbourne51d77772011-10-06 13:03:08 +0000433 OS << ")\n";
434 }
435}
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000436} // end namespace clang
Peter Collingbourne51d77772011-10-06 13:03:08 +0000437
438//===----------------------------------------------------------------------===//
439// Warning Group Tables generation
440//===----------------------------------------------------------------------===//
441
442static std::string getDiagCategoryEnum(llvm::StringRef name) {
443 if (name.empty())
444 return "DiagCat_None";
Dylan Noblesmith36d59272012-02-13 12:32:26 +0000445 SmallString<256> enumName = llvm::StringRef("DiagCat_");
Peter Collingbourne51d77772011-10-06 13:03:08 +0000446 for (llvm::StringRef::iterator I = name.begin(), E = name.end(); I != E; ++I)
447 enumName += isalnum(*I) ? *I : '_';
448 return enumName.str();
449}
Ted Kremenek25570a92012-07-06 23:07:31 +0000450
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000451namespace clang {
452void EmitClangDiagGroups(RecordKeeper &Records, raw_ostream &OS) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000453 // Compute a mapping from a DiagGroup to all of its parents.
454 DiagGroupParentMap DGParentMap(Records);
Ted Kremenek25570a92012-07-06 23:07:31 +0000455
Peter Collingbourne51d77772011-10-06 13:03:08 +0000456 std::vector<Record*> Diags =
457 Records.getAllDerivedDefinitions("Diagnostic");
Peter Collingbourne51d77772011-10-06 13:03:08 +0000458
Peter Collingbourne51d77772011-10-06 13:03:08 +0000459 std::vector<Record*> DiagGroups
460 = Records.getAllDerivedDefinitions("DiagGroup");
Argyrios Kyrtzidisd42236e2012-03-06 00:00:38 +0000461
462 std::map<std::string, GroupInfo> DiagsInGroup;
463 groupDiagnostics(Diags, DiagGroups, DiagsInGroup);
Ted Kremenek25570a92012-07-06 23:07:31 +0000464
465 // All extensions are implicitly in the "pedantic" group. Record the
466 // implicit set of groups in the "pedantic" group, and use this information
467 // later when emitting the group information for Pedantic.
468 RecordVec DiagsInPedantic;
469 RecordVec GroupsInPedantic;
470 InferPedantic inferPedantic(DGParentMap, Diags, DiagGroups, DiagsInGroup);
471 inferPedantic.compute(&DiagsInPedantic, &GroupsInPedantic);
472
Peter Collingbourne51d77772011-10-06 13:03:08 +0000473 // Walk through the groups emitting an array for each diagnostic of the diags
474 // that are mapped to.
475 OS << "\n#ifdef GET_DIAG_ARRAYS\n";
476 unsigned MaxLen = 0;
477 for (std::map<std::string, GroupInfo>::iterator
478 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
479 MaxLen = std::max(MaxLen, (unsigned)I->first.size());
Ted Kremenek25570a92012-07-06 23:07:31 +0000480 const bool IsPedantic = I->first == "pedantic";
481
Peter Collingbourne51d77772011-10-06 13:03:08 +0000482 std::vector<const Record*> &V = I->second.DiagsInGroup;
Ted Kremenek25570a92012-07-06 23:07:31 +0000483 if (!V.empty() || (IsPedantic && !DiagsInPedantic.empty())) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000484 OS << "static const short DiagArray" << I->second.IDNo << "[] = { ";
485 for (unsigned i = 0, e = V.size(); i != e; ++i)
486 OS << "diag::" << V[i]->getName() << ", ";
Ted Kremenek25570a92012-07-06 23:07:31 +0000487 // Emit the diagnostics implicitly in "pedantic".
488 if (IsPedantic) {
489 for (unsigned i = 0, e = DiagsInPedantic.size(); i != e; ++i)
490 OS << "diag::" << DiagsInPedantic[i]->getName() << ", ";
491 }
Peter Collingbourne51d77772011-10-06 13:03:08 +0000492 OS << "-1 };\n";
493 }
494
495 const std::vector<std::string> &SubGroups = I->second.SubGroups;
Ted Kremenek25570a92012-07-06 23:07:31 +0000496 if (!SubGroups.empty() || (IsPedantic && !GroupsInPedantic.empty())) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000497 OS << "static const short DiagSubGroup" << I->second.IDNo << "[] = { ";
498 for (unsigned i = 0, e = SubGroups.size(); i != e; ++i) {
499 std::map<std::string, GroupInfo>::iterator RI =
500 DiagsInGroup.find(SubGroups[i]);
501 assert(RI != DiagsInGroup.end() && "Referenced without existing?");
502 OS << RI->second.IDNo << ", ";
503 }
Ted Kremenek25570a92012-07-06 23:07:31 +0000504 // Emit the groups implicitly in "pedantic".
505 if (IsPedantic) {
506 for (unsigned i = 0, e = GroupsInPedantic.size(); i != e; ++i) {
507 const std::string &GroupName =
508 GroupsInPedantic[i]->getValueAsString("GroupName");
509 std::map<std::string, GroupInfo>::iterator RI =
510 DiagsInGroup.find(GroupName);
511 assert(RI != DiagsInGroup.end() && "Referenced without existing?");
512 OS << RI->second.IDNo << ", ";
513 }
514 }
515
Peter Collingbourne51d77772011-10-06 13:03:08 +0000516 OS << "-1 };\n";
517 }
518 }
519 OS << "#endif // GET_DIAG_ARRAYS\n\n";
520
521 // Emit the table now.
522 OS << "\n#ifdef GET_DIAG_TABLE\n";
523 for (std::map<std::string, GroupInfo>::iterator
524 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
525 // Group option string.
526 OS << " { ";
527 OS << I->first.size() << ", ";
528 OS << "\"";
Benjamin Kramer037ad1b2011-11-15 12:54:53 +0000529 if (I->first.find_first_not_of("abcdefghijklmnopqrstuvwxyz"
530 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
531 "0123456789!@#$%^*-+=:?")!=std::string::npos)
532 throw "Invalid character in diagnostic group '" + I->first + "'";
Peter Collingbourne51d77772011-10-06 13:03:08 +0000533 OS.write_escaped(I->first) << "\","
534 << std::string(MaxLen-I->first.size()+1, ' ');
Ted Kremenek25570a92012-07-06 23:07:31 +0000535
536 // Special handling for 'pedantic'.
537 const bool IsPedantic = I->first == "pedantic";
538
Peter Collingbourne51d77772011-10-06 13:03:08 +0000539 // Diagnostics in the group.
Ted Kremenek25570a92012-07-06 23:07:31 +0000540 const bool hasDiags = !I->second.DiagsInGroup.empty() ||
541 (IsPedantic && !DiagsInPedantic.empty());
542 if (!hasDiags)
Peter Collingbourne51d77772011-10-06 13:03:08 +0000543 OS << "0, ";
544 else
545 OS << "DiagArray" << I->second.IDNo << ", ";
546
547 // Subgroups.
Ted Kremenek25570a92012-07-06 23:07:31 +0000548 const bool hasSubGroups = !I->second.SubGroups.empty() ||
549 (IsPedantic && !GroupsInPedantic.empty());
550 if (!hasSubGroups)
Peter Collingbourne51d77772011-10-06 13:03:08 +0000551 OS << 0;
552 else
553 OS << "DiagSubGroup" << I->second.IDNo;
554 OS << " },\n";
555 }
556 OS << "#endif // GET_DIAG_TABLE\n\n";
557
558 // Emit the category table next.
559 DiagCategoryIDMap CategoriesByID(Records);
560 OS << "\n#ifdef GET_CATEGORY_TABLE\n";
561 for (DiagCategoryIDMap::iterator I = CategoriesByID.begin(),
562 E = CategoriesByID.end(); I != E; ++I)
563 OS << "CATEGORY(\"" << *I << "\", " << getDiagCategoryEnum(*I) << ")\n";
564 OS << "#endif // GET_CATEGORY_TABLE\n\n";
565}
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000566} // end namespace clang
Peter Collingbourne51d77772011-10-06 13:03:08 +0000567
568//===----------------------------------------------------------------------===//
569// Diagnostic name index generation
570//===----------------------------------------------------------------------===//
571
572namespace {
573struct RecordIndexElement
574{
575 RecordIndexElement() {}
576 explicit RecordIndexElement(Record const &R):
577 Name(R.getName()) {}
578
579 std::string Name;
580};
581
582struct RecordIndexElementSorter :
583 public std::binary_function<RecordIndexElement, RecordIndexElement, bool> {
584
585 bool operator()(RecordIndexElement const &Lhs,
586 RecordIndexElement const &Rhs) const {
587 return Lhs.Name < Rhs.Name;
588 }
589
590};
591
592} // end anonymous namespace.
593
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000594namespace clang {
595void EmitClangDiagsIndexName(RecordKeeper &Records, raw_ostream &OS) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000596 const std::vector<Record*> &Diags =
597 Records.getAllDerivedDefinitions("Diagnostic");
598
599 std::vector<RecordIndexElement> Index;
600 Index.reserve(Diags.size());
601 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
602 const Record &R = *(Diags[i]);
603 Index.push_back(RecordIndexElement(R));
604 }
605
606 std::sort(Index.begin(), Index.end(), RecordIndexElementSorter());
607
608 for (unsigned i = 0, e = Index.size(); i != e; ++i) {
609 const RecordIndexElement &R = Index[i];
610
611 OS << "DIAG_NAME_INDEX(" << R.Name << ")\n";
612 }
613}
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000614} // end namespace clang