blob: 0e3527f7c930996bbcf9a462d65508d95a436b47 [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 Kremeneke8cf7d12012-07-07 05:53:30 +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 Kremeneke8cf7d12012-07-07 05:53:30 +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 Kremeneke8cf7d12012-07-07 05:53:30 +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 Kremeneke8cf7d12012-07-07 05:53:30 +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
283 // Compute the set of diagnostics that are directly in -Wpedantic. We
284 // march through Diags a second time to ensure the results are emitted
285 // in deterministic order.
286 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
287 Record *R = Diags[i];
288 if (!DiagsSet.count(R))
289 continue;
290 // Check if the group is implicitly in -Wpedantic. If so,
291 // the diagnostic should not be directly included in the -Wpedantic
292 // diagnostic group.
293 if (DefInit *Group = dynamic_cast<DefInit*>(R->getValueInit("Group")))
294 if (groupInPedantic(Group->getDef()))
295 continue;
296
297 // The diagnostic is not included in a group that is (transitively) in
298 // -Wpedantic. Include it in -Wpedantic directly.
299 if (RecordVec *V = DiagsInPedantic.dyn_cast<RecordVec*>())
300 V->push_back(R);
301 else {
302 DiagsInPedantic.get<RecordSet*>()->insert(R);
303 }
304 }
305
306 if (!GroupsInPedantic)
307 return;
308
309 // Compute the set of groups that are directly in -Wpedantic. We
310 // march through the groups to ensure the results are emitted
311 /// in a deterministc order.
312 for (unsigned i = 0, ei = DiagGroups.size(); i != ei; ++i) {
313 Record *Group = DiagGroups[i];
314 if (!groupInPedantic(Group))
315 continue;
316
317 unsigned ParentsInPedantic = 0;
318 const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
319 for (unsigned j = 0, ej = Parents.size(); j != ej; ++j) {
320 if (groupInPedantic(Parents[j]))
321 ++ParentsInPedantic;
322 }
323 // If all the parents are in -Wpedantic, this means that this diagnostic
324 // group will be indirectly included by -Wpedantic already. In that
325 // case, do not add it directly to -Wpedantic. If the group has no
326 // parents, obviously it should go into -Wpedantic.
327 if (Parents.size() > 0 && ParentsInPedantic == Parents.size())
328 continue;
329
330 if (RecordVec *V = GroupsInPedantic.dyn_cast<RecordVec*>())
331 V->push_back(Group);
332 else {
333 GroupsInPedantic.get<RecordSet*>()->insert(Group);
334 }
335 }
336}
337
338//===----------------------------------------------------------------------===//
Peter Collingbourne51d77772011-10-06 13:03:08 +0000339// Warning Tables (.inc file) generation.
340//===----------------------------------------------------------------------===//
341
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000342/// ClangDiagsDefsEmitter - The top-level class emits .def files containing
343/// declarations of Clang diagnostics.
344namespace clang {
345void EmitClangDiagsDefs(RecordKeeper &Records, raw_ostream &OS,
346 const std::string &Component) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000347 // Write the #if guard
348 if (!Component.empty()) {
Benjamin Kramer90c78922011-11-06 20:36:48 +0000349 std::string ComponentName = StringRef(Component).upper();
Peter Collingbourne51d77772011-10-06 13:03:08 +0000350 OS << "#ifdef " << ComponentName << "START\n";
351 OS << "__" << ComponentName << "START = DIAG_START_" << ComponentName
352 << ",\n";
353 OS << "#undef " << ComponentName << "START\n";
354 OS << "#endif\n\n";
355 }
356
357 const std::vector<Record*> &Diags =
358 Records.getAllDerivedDefinitions("Diagnostic");
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000359
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000360 std::vector<Record*> DiagGroups
361 = Records.getAllDerivedDefinitions("DiagGroup");
Argyrios Kyrtzidisd42236e2012-03-06 00:00:38 +0000362
363 std::map<std::string, GroupInfo> DiagsInGroup;
364 groupDiagnostics(Diags, DiagGroups, DiagsInGroup);
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000365
Peter Collingbourne51d77772011-10-06 13:03:08 +0000366 DiagCategoryIDMap CategoryIDs(Records);
367 DiagGroupParentMap DGParentMap(Records);
368
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000369 // Compute the set of diagnostics that are in -Wpedantic.
370 RecordSet DiagsInPedantic;
371 InferPedantic inferPedantic(DGParentMap, Diags, DiagGroups, DiagsInGroup);
372 inferPedantic.compute(&DiagsInPedantic, (RecordVec*)0);
373
Peter Collingbourne51d77772011-10-06 13:03:08 +0000374 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
375 const Record &R = *Diags[i];
376 // Filter by component.
377 if (!Component.empty() && Component != R.getValueAsString("Component"))
378 continue;
379
380 OS << "DIAG(" << R.getName() << ", ";
381 OS << R.getValueAsDef("Class")->getName();
382 OS << ", diag::" << R.getValueAsDef("DefaultMapping")->getName();
383
384 // Description string.
385 OS << ", \"";
386 OS.write_escaped(R.getValueAsString("Text")) << '"';
387
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000388 // Warning associated with the diagnostic. This is stored as an index into
389 // the alphabetically sorted warning table.
Peter Collingbourne51d77772011-10-06 13:03:08 +0000390 if (DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Group"))) {
Argyrios Kyrtzidisd42236e2012-03-06 00:00:38 +0000391 std::map<std::string, GroupInfo>::iterator I =
392 DiagsInGroup.find(DI->getDef()->getValueAsString("GroupName"));
393 assert(I != DiagsInGroup.end());
394 OS << ", " << I->second.IDNo;
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000395 } else if (DiagsInPedantic.count(&R)) {
396 std::map<std::string, GroupInfo>::iterator I =
397 DiagsInGroup.find("pedantic");
398 assert(I != DiagsInGroup.end() && "pedantic group not defined");
399 OS << ", " << I->second.IDNo;
Peter Collingbourne51d77772011-10-06 13:03:08 +0000400 } else {
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000401 OS << ", 0";
Peter Collingbourne51d77772011-10-06 13:03:08 +0000402 }
403
404 // SFINAE bit
405 if (R.getValueAsBit("SFINAE"))
406 OS << ", true";
407 else
408 OS << ", false";
409
410 // Access control bit
411 if (R.getValueAsBit("AccessControl"))
412 OS << ", true";
413 else
414 OS << ", false";
415
416 // FIXME: This condition is just to avoid temporary revlock, it can be
417 // removed.
418 if (R.getValue("WarningNoWerror")) {
419 // Default warning has no Werror bit.
420 if (R.getValueAsBit("WarningNoWerror"))
421 OS << ", true";
422 else
423 OS << ", false";
424
425 // Default warning show in system header bit.
426 if (R.getValueAsBit("WarningShowInSystemHeader"))
427 OS << ", true";
428 else
429 OS << ", false";
430 }
431
432 // Category number.
433 OS << ", " << CategoryIDs.getID(getDiagnosticCategory(&R, DGParentMap));
Peter Collingbourne51d77772011-10-06 13:03:08 +0000434 OS << ")\n";
435 }
436}
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000437} // end namespace clang
Peter Collingbourne51d77772011-10-06 13:03:08 +0000438
439//===----------------------------------------------------------------------===//
440// Warning Group Tables generation
441//===----------------------------------------------------------------------===//
442
443static std::string getDiagCategoryEnum(llvm::StringRef name) {
444 if (name.empty())
445 return "DiagCat_None";
Dylan Noblesmith36d59272012-02-13 12:32:26 +0000446 SmallString<256> enumName = llvm::StringRef("DiagCat_");
Peter Collingbourne51d77772011-10-06 13:03:08 +0000447 for (llvm::StringRef::iterator I = name.begin(), E = name.end(); I != E; ++I)
448 enumName += isalnum(*I) ? *I : '_';
449 return enumName.str();
450}
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000451
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000452namespace clang {
453void EmitClangDiagGroups(RecordKeeper &Records, raw_ostream &OS) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000454 // Compute a mapping from a DiagGroup to all of its parents.
455 DiagGroupParentMap DGParentMap(Records);
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000456
Peter Collingbourne51d77772011-10-06 13:03:08 +0000457 std::vector<Record*> Diags =
458 Records.getAllDerivedDefinitions("Diagnostic");
Peter Collingbourne51d77772011-10-06 13:03:08 +0000459
Peter Collingbourne51d77772011-10-06 13:03:08 +0000460 std::vector<Record*> DiagGroups
461 = Records.getAllDerivedDefinitions("DiagGroup");
Argyrios Kyrtzidisd42236e2012-03-06 00:00:38 +0000462
463 std::map<std::string, GroupInfo> DiagsInGroup;
464 groupDiagnostics(Diags, DiagGroups, DiagsInGroup);
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000465
466 // All extensions are implicitly in the "pedantic" group. Record the
467 // implicit set of groups in the "pedantic" group, and use this information
468 // later when emitting the group information for Pedantic.
469 RecordVec DiagsInPedantic;
470 RecordVec GroupsInPedantic;
471 InferPedantic inferPedantic(DGParentMap, Diags, DiagGroups, DiagsInGroup);
472 inferPedantic.compute(&DiagsInPedantic, &GroupsInPedantic);
473
Peter Collingbourne51d77772011-10-06 13:03:08 +0000474 // Walk through the groups emitting an array for each diagnostic of the diags
475 // that are mapped to.
476 OS << "\n#ifdef GET_DIAG_ARRAYS\n";
477 unsigned MaxLen = 0;
478 for (std::map<std::string, GroupInfo>::iterator
479 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
480 MaxLen = std::max(MaxLen, (unsigned)I->first.size());
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000481 const bool IsPedantic = I->first == "pedantic";
482
Peter Collingbourne51d77772011-10-06 13:03:08 +0000483 std::vector<const Record*> &V = I->second.DiagsInGroup;
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000484 if (!V.empty() || (IsPedantic && !DiagsInPedantic.empty())) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000485 OS << "static const short DiagArray" << I->second.IDNo << "[] = { ";
486 for (unsigned i = 0, e = V.size(); i != e; ++i)
487 OS << "diag::" << V[i]->getName() << ", ";
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000488 // Emit the diagnostics implicitly in "pedantic".
489 if (IsPedantic) {
490 for (unsigned i = 0, e = DiagsInPedantic.size(); i != e; ++i)
491 OS << "diag::" << DiagsInPedantic[i]->getName() << ", ";
492 }
Peter Collingbourne51d77772011-10-06 13:03:08 +0000493 OS << "-1 };\n";
494 }
495
496 const std::vector<std::string> &SubGroups = I->second.SubGroups;
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000497 if (!SubGroups.empty() || (IsPedantic && !GroupsInPedantic.empty())) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000498 OS << "static const short DiagSubGroup" << I->second.IDNo << "[] = { ";
499 for (unsigned i = 0, e = SubGroups.size(); i != e; ++i) {
500 std::map<std::string, GroupInfo>::iterator RI =
501 DiagsInGroup.find(SubGroups[i]);
502 assert(RI != DiagsInGroup.end() && "Referenced without existing?");
503 OS << RI->second.IDNo << ", ";
504 }
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000505 // Emit the groups implicitly in "pedantic".
506 if (IsPedantic) {
507 for (unsigned i = 0, e = GroupsInPedantic.size(); i != e; ++i) {
508 const std::string &GroupName =
509 GroupsInPedantic[i]->getValueAsString("GroupName");
510 std::map<std::string, GroupInfo>::iterator RI =
511 DiagsInGroup.find(GroupName);
512 assert(RI != DiagsInGroup.end() && "Referenced without existing?");
513 OS << RI->second.IDNo << ", ";
514 }
515 }
516
Peter Collingbourne51d77772011-10-06 13:03:08 +0000517 OS << "-1 };\n";
518 }
519 }
520 OS << "#endif // GET_DIAG_ARRAYS\n\n";
521
522 // Emit the table now.
523 OS << "\n#ifdef GET_DIAG_TABLE\n";
524 for (std::map<std::string, GroupInfo>::iterator
525 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
526 // Group option string.
527 OS << " { ";
528 OS << I->first.size() << ", ";
529 OS << "\"";
Benjamin Kramer037ad1b2011-11-15 12:54:53 +0000530 if (I->first.find_first_not_of("abcdefghijklmnopqrstuvwxyz"
531 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
532 "0123456789!@#$%^*-+=:?")!=std::string::npos)
533 throw "Invalid character in diagnostic group '" + I->first + "'";
Peter Collingbourne51d77772011-10-06 13:03:08 +0000534 OS.write_escaped(I->first) << "\","
535 << std::string(MaxLen-I->first.size()+1, ' ');
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000536
537 // Special handling for 'pedantic'.
538 const bool IsPedantic = I->first == "pedantic";
539
Peter Collingbourne51d77772011-10-06 13:03:08 +0000540 // Diagnostics in the group.
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000541 const bool hasDiags = !I->second.DiagsInGroup.empty() ||
542 (IsPedantic && !DiagsInPedantic.empty());
543 if (!hasDiags)
Peter Collingbourne51d77772011-10-06 13:03:08 +0000544 OS << "0, ";
545 else
546 OS << "DiagArray" << I->second.IDNo << ", ";
547
548 // Subgroups.
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000549 const bool hasSubGroups = !I->second.SubGroups.empty() ||
550 (IsPedantic && !GroupsInPedantic.empty());
551 if (!hasSubGroups)
Peter Collingbourne51d77772011-10-06 13:03:08 +0000552 OS << 0;
553 else
554 OS << "DiagSubGroup" << I->second.IDNo;
555 OS << " },\n";
556 }
557 OS << "#endif // GET_DIAG_TABLE\n\n";
558
559 // Emit the category table next.
560 DiagCategoryIDMap CategoriesByID(Records);
561 OS << "\n#ifdef GET_CATEGORY_TABLE\n";
562 for (DiagCategoryIDMap::iterator I = CategoriesByID.begin(),
563 E = CategoriesByID.end(); I != E; ++I)
564 OS << "CATEGORY(\"" << *I << "\", " << getDiagCategoryEnum(*I) << ")\n";
565 OS << "#endif // GET_CATEGORY_TABLE\n\n";
566}
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000567} // end namespace clang
Peter Collingbourne51d77772011-10-06 13:03:08 +0000568
569//===----------------------------------------------------------------------===//
570// Diagnostic name index generation
571//===----------------------------------------------------------------------===//
572
573namespace {
574struct RecordIndexElement
575{
576 RecordIndexElement() {}
577 explicit RecordIndexElement(Record const &R):
578 Name(R.getName()) {}
579
580 std::string Name;
581};
582
583struct RecordIndexElementSorter :
584 public std::binary_function<RecordIndexElement, RecordIndexElement, bool> {
585
586 bool operator()(RecordIndexElement const &Lhs,
587 RecordIndexElement const &Rhs) const {
588 return Lhs.Name < Rhs.Name;
589 }
590
591};
592
593} // end anonymous namespace.
594
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000595namespace clang {
596void EmitClangDiagsIndexName(RecordKeeper &Records, raw_ostream &OS) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000597 const std::vector<Record*> &Diags =
598 Records.getAllDerivedDefinitions("Diagnostic");
599
600 std::vector<RecordIndexElement> Index;
601 Index.reserve(Diags.size());
602 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
603 const Record &R = *(Diags[i]);
604 Index.push_back(RecordIndexElement(R));
605 }
606
607 std::sort(Index.begin(), Index.end(), RecordIndexElementSorter());
608
609 for (unsigned i = 0, e = Index.size(); i != e; ++i) {
610 const RecordIndexElement &R = Index[i];
611
612 OS << "DIAG_NAME_INDEX(" << R.Name << ")\n";
613 }
614}
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000615} // end namespace clang