blob: b1472a87cc10a3bbe305ffc548b1be49322048be [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"
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +000021#include "llvm/TableGen/Error.h"
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +000022#include "llvm/TableGen/Record.h"
23#include "llvm/TableGen/TableGenBackend.h"
Peter Collingbourne51d77772011-10-06 13:03:08 +000024#include <algorithm>
Joerg Sonnenberger7094dee2012-08-10 10:58:18 +000025#include <cctype>
Peter Collingbourne51d77772011-10-06 13:03:08 +000026#include <functional>
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +000027#include <map>
Benjamin Kramerd49cb202012-02-15 20:57:03 +000028#include <set>
Peter Collingbourne51d77772011-10-06 13:03:08 +000029using namespace llvm;
30
31//===----------------------------------------------------------------------===//
32// Diagnostic category computation code.
33//===----------------------------------------------------------------------===//
34
35namespace {
36class DiagGroupParentMap {
37 RecordKeeper &Records;
38 std::map<const Record*, std::vector<Record*> > Mapping;
39public:
40 DiagGroupParentMap(RecordKeeper &records) : Records(records) {
41 std::vector<Record*> DiagGroups
42 = Records.getAllDerivedDefinitions("DiagGroup");
43 for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
44 std::vector<Record*> SubGroups =
45 DiagGroups[i]->getValueAsListOfDefs("SubGroups");
46 for (unsigned j = 0, e = SubGroups.size(); j != e; ++j)
47 Mapping[SubGroups[j]].push_back(DiagGroups[i]);
48 }
49 }
50
51 const std::vector<Record*> &getParents(const Record *Group) {
52 return Mapping[Group];
53 }
54};
55} // end anonymous namespace.
56
Peter Collingbourne51d77772011-10-06 13:03:08 +000057static std::string
58getCategoryFromDiagGroup(const Record *Group,
59 DiagGroupParentMap &DiagGroupParents) {
60 // If the DiagGroup has a category, return it.
61 std::string CatName = Group->getValueAsString("CategoryName");
62 if (!CatName.empty()) return CatName;
63
64 // The diag group may the subgroup of one or more other diagnostic groups,
65 // check these for a category as well.
66 const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
67 for (unsigned i = 0, e = Parents.size(); i != e; ++i) {
68 CatName = getCategoryFromDiagGroup(Parents[i], DiagGroupParents);
69 if (!CatName.empty()) return CatName;
70 }
71 return "";
72}
73
74/// getDiagnosticCategory - Return the category that the specified diagnostic
75/// lives in.
76static std::string getDiagnosticCategory(const Record *R,
77 DiagGroupParentMap &DiagGroupParents) {
78 // If the diagnostic is in a group, and that group has a category, use it.
Sean Silva1ab46322012-10-10 20:25:43 +000079 if (DefInit *Group = dyn_cast<DefInit>(R->getValueInit("Group"))) {
Peter Collingbourne51d77772011-10-06 13:03:08 +000080 // Check the diagnostic's diag group for a category.
81 std::string CatName = getCategoryFromDiagGroup(Group->getDef(),
82 DiagGroupParents);
83 if (!CatName.empty()) return CatName;
84 }
Ted Kremeneke8cf7d12012-07-07 05:53:30 +000085
Peter Collingbourne51d77772011-10-06 13:03:08 +000086 // If the diagnostic itself has a category, get it.
87 return R->getValueAsString("CategoryName");
88}
89
90namespace {
91 class DiagCategoryIDMap {
92 RecordKeeper &Records;
93 StringMap<unsigned> CategoryIDs;
94 std::vector<std::string> CategoryStrings;
95 public:
96 DiagCategoryIDMap(RecordKeeper &records) : Records(records) {
97 DiagGroupParentMap ParentInfo(Records);
98
99 // The zero'th category is "".
100 CategoryStrings.push_back("");
101 CategoryIDs[""] = 0;
102
103 std::vector<Record*> Diags =
104 Records.getAllDerivedDefinitions("Diagnostic");
105 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
106 std::string Category = getDiagnosticCategory(Diags[i], ParentInfo);
107 if (Category.empty()) continue; // Skip diags with no category.
108
109 unsigned &ID = CategoryIDs[Category];
110 if (ID != 0) continue; // Already seen.
111
112 ID = CategoryStrings.size();
113 CategoryStrings.push_back(Category);
114 }
115 }
116
117 unsigned getID(StringRef CategoryString) {
118 return CategoryIDs[CategoryString];
119 }
120
121 typedef std::vector<std::string>::iterator iterator;
122 iterator begin() { return CategoryStrings.begin(); }
123 iterator end() { return CategoryStrings.end(); }
124 };
Argyrios Kyrtzidisd42236e2012-03-06 00:00:38 +0000125
126 struct GroupInfo {
127 std::vector<const Record*> DiagsInGroup;
128 std::vector<std::string> SubGroups;
129 unsigned IDNo;
130 };
Peter Collingbourne51d77772011-10-06 13:03:08 +0000131} // end anonymous namespace.
132
Argyrios Kyrtzidisd42236e2012-03-06 00:00:38 +0000133/// \brief Invert the 1-[0/1] mapping of diags to group into a one to many
134/// mapping of groups to diags in the group.
135static void groupDiagnostics(const std::vector<Record*> &Diags,
136 const std::vector<Record*> &DiagGroups,
137 std::map<std::string, GroupInfo> &DiagsInGroup) {
138 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
139 const Record *R = Diags[i];
Sean Silva1ab46322012-10-10 20:25:43 +0000140 DefInit *DI = dyn_cast<DefInit>(R->getValueInit("Group"));
Argyrios Kyrtzidisd42236e2012-03-06 00:00:38 +0000141 if (DI == 0) continue;
Richard Smith46484772012-05-04 19:05:50 +0000142 assert(R->getValueAsDef("Class")->getName() != "CLASS_NOTE" &&
143 "Note can't be in a DiagGroup");
Argyrios Kyrtzidisd42236e2012-03-06 00:00:38 +0000144 std::string GroupName = DI->getDef()->getValueAsString("GroupName");
145 DiagsInGroup[GroupName].DiagsInGroup.push_back(R);
146 }
147
148 // Add all DiagGroup's to the DiagsInGroup list to make sure we pick up empty
149 // groups (these are warnings that GCC supports that clang never produces).
150 for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
151 Record *Group = DiagGroups[i];
152 GroupInfo &GI = DiagsInGroup[Group->getValueAsString("GroupName")];
153
154 std::vector<Record*> SubGroups = Group->getValueAsListOfDefs("SubGroups");
155 for (unsigned j = 0, e = SubGroups.size(); j != e; ++j)
156 GI.SubGroups.push_back(SubGroups[j]->getValueAsString("GroupName"));
157 }
158
159 // Assign unique ID numbers to the groups.
160 unsigned IDNo = 0;
161 for (std::map<std::string, GroupInfo>::iterator
162 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I, ++IDNo)
163 I->second.IDNo = IDNo;
164}
Peter Collingbourne51d77772011-10-06 13:03:08 +0000165
166//===----------------------------------------------------------------------===//
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000167// Infer members of -Wpedantic.
168//===----------------------------------------------------------------------===//
169
170typedef std::vector<const Record *> RecordVec;
171typedef llvm::DenseSet<const Record *> RecordSet;
172typedef llvm::PointerUnion<RecordVec*, RecordSet*> VecOrSet;
173
174namespace {
175class InferPedantic {
176 typedef llvm::DenseMap<const Record*,
177 std::pair<unsigned, llvm::Optional<unsigned> > > GMap;
178
179 DiagGroupParentMap &DiagGroupParents;
180 const std::vector<Record*> &Diags;
181 const std::vector<Record*> DiagGroups;
182 std::map<std::string, GroupInfo> &DiagsInGroup;
183 llvm::DenseSet<const Record*> DiagsSet;
184 GMap GroupCount;
185public:
186 InferPedantic(DiagGroupParentMap &DiagGroupParents,
187 const std::vector<Record*> &Diags,
188 const std::vector<Record*> &DiagGroups,
189 std::map<std::string, GroupInfo> &DiagsInGroup)
190 : DiagGroupParents(DiagGroupParents),
191 Diags(Diags),
192 DiagGroups(DiagGroups),
193 DiagsInGroup(DiagsInGroup) {}
194
195 /// Compute the set of diagnostics and groups that are immediately
196 /// in -Wpedantic.
197 void compute(VecOrSet DiagsInPedantic,
198 VecOrSet GroupsInPedantic);
199
200private:
201 /// Determine whether a group is a subgroup of another group.
202 bool isSubGroupOfGroup(const Record *Group,
203 llvm::StringRef RootGroupName);
204
205 /// Determine if the diagnostic is an extension.
206 bool isExtension(const Record *Diag);
207
Ted Kremenekbb5185c2012-08-10 20:50:00 +0000208 /// Determine if the diagnostic is off by default.
209 bool isOffByDefault(const Record *Diag);
210
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000211 /// Increment the count for a group, and transitively marked
212 /// parent groups when appropriate.
213 void markGroup(const Record *Group);
214
215 /// Return true if the diagnostic is in a pedantic group.
216 bool groupInPedantic(const Record *Group, bool increment = false);
217};
218} // end anonymous namespace
219
220bool InferPedantic::isSubGroupOfGroup(const Record *Group,
221 llvm::StringRef GName) {
222
223 const std::string &GroupName = Group->getValueAsString("GroupName");
224 if (GName == GroupName)
225 return true;
226
227 const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
228 for (unsigned i = 0, e = Parents.size(); i != e; ++i)
229 if (isSubGroupOfGroup(Parents[i], GName))
230 return true;
231
232 return false;
233}
234
235/// Determine if the diagnostic is an extension.
236bool InferPedantic::isExtension(const Record *Diag) {
237 const std::string &ClsName = Diag->getValueAsDef("Class")->getName();
238 return ClsName == "CLASS_EXTENSION";
239}
240
Ted Kremenekbb5185c2012-08-10 20:50:00 +0000241bool InferPedantic::isOffByDefault(const Record *Diag) {
242 const std::string &DefMap = Diag->getValueAsDef("DefaultMapping")->getName();
243 return DefMap == "MAP_IGNORE";
244}
245
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000246bool InferPedantic::groupInPedantic(const Record *Group, bool increment) {
247 GMap::mapped_type &V = GroupCount[Group];
248 // Lazily compute the threshold value for the group count.
249 if (!V.second.hasValue()) {
250 const GroupInfo &GI = DiagsInGroup[Group->getValueAsString("GroupName")];
251 V.second = GI.SubGroups.size() + GI.DiagsInGroup.size();
252 }
253
254 if (increment)
255 ++V.first;
256
257 // Consider a group in -Wpendatic IFF if has at least one diagnostic
258 // or subgroup AND all of those diagnostics and subgroups are covered
259 // by -Wpedantic via our computation.
260 return V.first != 0 && V.first == V.second.getValue();
261}
262
263void InferPedantic::markGroup(const Record *Group) {
264 // If all the diagnostics and subgroups have been marked as being
265 // covered by -Wpedantic, increment the count of parent groups. Once the
266 // group's count is equal to the number of subgroups and diagnostics in
267 // that group, we can safely add this group to -Wpedantic.
268 if (groupInPedantic(Group, /* increment */ true)) {
269 const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
270 for (unsigned i = 0, e = Parents.size(); i != e; ++i)
271 markGroup(Parents[i]);
272 }
273}
274
275void InferPedantic::compute(VecOrSet DiagsInPedantic,
276 VecOrSet GroupsInPedantic) {
Ted Kremenekbb5185c2012-08-10 20:50:00 +0000277 // All extensions that are not on by default are implicitly in the
278 // "pedantic" group. For those that aren't explicitly included in -Wpedantic,
279 // mark them for consideration to be included in -Wpedantic directly.
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000280 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
281 Record *R = Diags[i];
Ted Kremenekbb5185c2012-08-10 20:50:00 +0000282 if (isExtension(R) && isOffByDefault(R)) {
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000283 DiagsSet.insert(R);
Sean Silva1ab46322012-10-10 20:25:43 +0000284 if (DefInit *Group = dyn_cast<DefInit>(R->getValueInit("Group"))) {
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000285 const Record *GroupRec = Group->getDef();
286 if (!isSubGroupOfGroup(GroupRec, "pedantic")) {
287 markGroup(GroupRec);
288 }
289 }
290 }
291 }
292
293 // Compute the set of diagnostics that are directly in -Wpedantic. We
294 // march through Diags a second time to ensure the results are emitted
295 // in deterministic order.
296 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
297 Record *R = Diags[i];
298 if (!DiagsSet.count(R))
299 continue;
300 // Check if the group is implicitly in -Wpedantic. If so,
301 // the diagnostic should not be directly included in the -Wpedantic
302 // diagnostic group.
Sean Silva1ab46322012-10-10 20:25:43 +0000303 if (DefInit *Group = dyn_cast<DefInit>(R->getValueInit("Group")))
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000304 if (groupInPedantic(Group->getDef()))
305 continue;
306
307 // The diagnostic is not included in a group that is (transitively) in
308 // -Wpedantic. Include it in -Wpedantic directly.
309 if (RecordVec *V = DiagsInPedantic.dyn_cast<RecordVec*>())
310 V->push_back(R);
311 else {
312 DiagsInPedantic.get<RecordSet*>()->insert(R);
313 }
314 }
315
316 if (!GroupsInPedantic)
317 return;
318
319 // Compute the set of groups that are directly in -Wpedantic. We
320 // march through the groups to ensure the results are emitted
321 /// in a deterministc order.
322 for (unsigned i = 0, ei = DiagGroups.size(); i != ei; ++i) {
323 Record *Group = DiagGroups[i];
324 if (!groupInPedantic(Group))
325 continue;
326
327 unsigned ParentsInPedantic = 0;
328 const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
329 for (unsigned j = 0, ej = Parents.size(); j != ej; ++j) {
330 if (groupInPedantic(Parents[j]))
331 ++ParentsInPedantic;
332 }
333 // If all the parents are in -Wpedantic, this means that this diagnostic
334 // group will be indirectly included by -Wpedantic already. In that
335 // case, do not add it directly to -Wpedantic. If the group has no
336 // parents, obviously it should go into -Wpedantic.
337 if (Parents.size() > 0 && ParentsInPedantic == Parents.size())
338 continue;
339
340 if (RecordVec *V = GroupsInPedantic.dyn_cast<RecordVec*>())
341 V->push_back(Group);
342 else {
343 GroupsInPedantic.get<RecordSet*>()->insert(Group);
344 }
345 }
346}
347
348//===----------------------------------------------------------------------===//
Peter Collingbourne51d77772011-10-06 13:03:08 +0000349// Warning Tables (.inc file) generation.
350//===----------------------------------------------------------------------===//
351
Ted Kremenek4a535362012-08-07 05:01:49 +0000352static bool isError(const Record &Diag) {
353 const std::string &ClsName = Diag.getValueAsDef("Class")->getName();
354 return ClsName == "CLASS_ERROR";
355}
356
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000357/// ClangDiagsDefsEmitter - The top-level class emits .def files containing
358/// declarations of Clang diagnostics.
359namespace clang {
360void EmitClangDiagsDefs(RecordKeeper &Records, raw_ostream &OS,
361 const std::string &Component) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000362 // Write the #if guard
363 if (!Component.empty()) {
Benjamin Kramer90c78922011-11-06 20:36:48 +0000364 std::string ComponentName = StringRef(Component).upper();
Peter Collingbourne51d77772011-10-06 13:03:08 +0000365 OS << "#ifdef " << ComponentName << "START\n";
366 OS << "__" << ComponentName << "START = DIAG_START_" << ComponentName
367 << ",\n";
368 OS << "#undef " << ComponentName << "START\n";
369 OS << "#endif\n\n";
370 }
371
372 const std::vector<Record*> &Diags =
373 Records.getAllDerivedDefinitions("Diagnostic");
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000374
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000375 std::vector<Record*> DiagGroups
376 = Records.getAllDerivedDefinitions("DiagGroup");
Argyrios Kyrtzidisd42236e2012-03-06 00:00:38 +0000377
378 std::map<std::string, GroupInfo> DiagsInGroup;
379 groupDiagnostics(Diags, DiagGroups, DiagsInGroup);
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000380
Peter Collingbourne51d77772011-10-06 13:03:08 +0000381 DiagCategoryIDMap CategoryIDs(Records);
382 DiagGroupParentMap DGParentMap(Records);
383
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000384 // Compute the set of diagnostics that are in -Wpedantic.
385 RecordSet DiagsInPedantic;
386 InferPedantic inferPedantic(DGParentMap, Diags, DiagGroups, DiagsInGroup);
387 inferPedantic.compute(&DiagsInPedantic, (RecordVec*)0);
388
Peter Collingbourne51d77772011-10-06 13:03:08 +0000389 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
390 const Record &R = *Diags[i];
Ted Kremenek4a535362012-08-07 05:01:49 +0000391
392 // Check if this is an error that is accidentally in a warning
393 // group.
394 if (isError(R)) {
Sean Silva1ab46322012-10-10 20:25:43 +0000395 if (DefInit *Group = dyn_cast<DefInit>(R.getValueInit("Group"))) {
Ted Kremenek4a535362012-08-07 05:01:49 +0000396 const Record *GroupRec = Group->getDef();
397 const std::string &GroupName = GroupRec->getValueAsString("GroupName");
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +0000398 PrintFatalError(R.getLoc(), "Error " + R.getName() +
399 " cannot be in a warning group [" + GroupName + "]");
Ted Kremenek4a535362012-08-07 05:01:49 +0000400 }
401 }
402
Peter Collingbourne51d77772011-10-06 13:03:08 +0000403 // Filter by component.
404 if (!Component.empty() && Component != R.getValueAsString("Component"))
405 continue;
406
407 OS << "DIAG(" << R.getName() << ", ";
408 OS << R.getValueAsDef("Class")->getName();
409 OS << ", diag::" << R.getValueAsDef("DefaultMapping")->getName();
410
411 // Description string.
412 OS << ", \"";
413 OS.write_escaped(R.getValueAsString("Text")) << '"';
414
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000415 // Warning associated with the diagnostic. This is stored as an index into
416 // the alphabetically sorted warning table.
Sean Silva1ab46322012-10-10 20:25:43 +0000417 if (DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group"))) {
Argyrios Kyrtzidisd42236e2012-03-06 00:00:38 +0000418 std::map<std::string, GroupInfo>::iterator I =
419 DiagsInGroup.find(DI->getDef()->getValueAsString("GroupName"));
420 assert(I != DiagsInGroup.end());
421 OS << ", " << I->second.IDNo;
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000422 } else if (DiagsInPedantic.count(&R)) {
423 std::map<std::string, GroupInfo>::iterator I =
424 DiagsInGroup.find("pedantic");
425 assert(I != DiagsInGroup.end() && "pedantic group not defined");
426 OS << ", " << I->second.IDNo;
Peter Collingbourne51d77772011-10-06 13:03:08 +0000427 } else {
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000428 OS << ", 0";
Peter Collingbourne51d77772011-10-06 13:03:08 +0000429 }
430
431 // SFINAE bit
432 if (R.getValueAsBit("SFINAE"))
433 OS << ", true";
434 else
435 OS << ", false";
436
437 // Access control bit
438 if (R.getValueAsBit("AccessControl"))
439 OS << ", true";
440 else
441 OS << ", false";
442
443 // FIXME: This condition is just to avoid temporary revlock, it can be
444 // removed.
445 if (R.getValue("WarningNoWerror")) {
446 // Default warning has no Werror bit.
447 if (R.getValueAsBit("WarningNoWerror"))
448 OS << ", true";
449 else
450 OS << ", false";
451
452 // Default warning show in system header bit.
453 if (R.getValueAsBit("WarningShowInSystemHeader"))
454 OS << ", true";
455 else
456 OS << ", false";
457 }
458
459 // Category number.
460 OS << ", " << CategoryIDs.getID(getDiagnosticCategory(&R, DGParentMap));
Peter Collingbourne51d77772011-10-06 13:03:08 +0000461 OS << ")\n";
462 }
463}
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000464} // end namespace clang
Peter Collingbourne51d77772011-10-06 13:03:08 +0000465
466//===----------------------------------------------------------------------===//
467// Warning Group Tables generation
468//===----------------------------------------------------------------------===//
469
470static std::string getDiagCategoryEnum(llvm::StringRef name) {
471 if (name.empty())
472 return "DiagCat_None";
Dylan Noblesmith36d59272012-02-13 12:32:26 +0000473 SmallString<256> enumName = llvm::StringRef("DiagCat_");
Peter Collingbourne51d77772011-10-06 13:03:08 +0000474 for (llvm::StringRef::iterator I = name.begin(), E = name.end(); I != E; ++I)
475 enumName += isalnum(*I) ? *I : '_';
476 return enumName.str();
477}
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000478
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000479namespace clang {
480void EmitClangDiagGroups(RecordKeeper &Records, raw_ostream &OS) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000481 // Compute a mapping from a DiagGroup to all of its parents.
482 DiagGroupParentMap DGParentMap(Records);
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000483
Peter Collingbourne51d77772011-10-06 13:03:08 +0000484 std::vector<Record*> Diags =
485 Records.getAllDerivedDefinitions("Diagnostic");
Peter Collingbourne51d77772011-10-06 13:03:08 +0000486
Peter Collingbourne51d77772011-10-06 13:03:08 +0000487 std::vector<Record*> DiagGroups
488 = Records.getAllDerivedDefinitions("DiagGroup");
Argyrios Kyrtzidisd42236e2012-03-06 00:00:38 +0000489
490 std::map<std::string, GroupInfo> DiagsInGroup;
491 groupDiagnostics(Diags, DiagGroups, DiagsInGroup);
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000492
493 // All extensions are implicitly in the "pedantic" group. Record the
494 // implicit set of groups in the "pedantic" group, and use this information
495 // later when emitting the group information for Pedantic.
496 RecordVec DiagsInPedantic;
497 RecordVec GroupsInPedantic;
498 InferPedantic inferPedantic(DGParentMap, Diags, DiagGroups, DiagsInGroup);
499 inferPedantic.compute(&DiagsInPedantic, &GroupsInPedantic);
500
Peter Collingbourne51d77772011-10-06 13:03:08 +0000501 // Walk through the groups emitting an array for each diagnostic of the diags
502 // that are mapped to.
503 OS << "\n#ifdef GET_DIAG_ARRAYS\n";
504 unsigned MaxLen = 0;
505 for (std::map<std::string, GroupInfo>::iterator
506 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
507 MaxLen = std::max(MaxLen, (unsigned)I->first.size());
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000508 const bool IsPedantic = I->first == "pedantic";
509
Peter Collingbourne51d77772011-10-06 13:03:08 +0000510 std::vector<const Record*> &V = I->second.DiagsInGroup;
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000511 if (!V.empty() || (IsPedantic && !DiagsInPedantic.empty())) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000512 OS << "static const short DiagArray" << I->second.IDNo << "[] = { ";
513 for (unsigned i = 0, e = V.size(); i != e; ++i)
514 OS << "diag::" << V[i]->getName() << ", ";
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000515 // Emit the diagnostics implicitly in "pedantic".
516 if (IsPedantic) {
517 for (unsigned i = 0, e = DiagsInPedantic.size(); i != e; ++i)
518 OS << "diag::" << DiagsInPedantic[i]->getName() << ", ";
519 }
Peter Collingbourne51d77772011-10-06 13:03:08 +0000520 OS << "-1 };\n";
521 }
522
523 const std::vector<std::string> &SubGroups = I->second.SubGroups;
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000524 if (!SubGroups.empty() || (IsPedantic && !GroupsInPedantic.empty())) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000525 OS << "static const short DiagSubGroup" << I->second.IDNo << "[] = { ";
526 for (unsigned i = 0, e = SubGroups.size(); i != e; ++i) {
527 std::map<std::string, GroupInfo>::iterator RI =
528 DiagsInGroup.find(SubGroups[i]);
529 assert(RI != DiagsInGroup.end() && "Referenced without existing?");
530 OS << RI->second.IDNo << ", ";
531 }
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000532 // Emit the groups implicitly in "pedantic".
533 if (IsPedantic) {
534 for (unsigned i = 0, e = GroupsInPedantic.size(); i != e; ++i) {
535 const std::string &GroupName =
536 GroupsInPedantic[i]->getValueAsString("GroupName");
537 std::map<std::string, GroupInfo>::iterator RI =
538 DiagsInGroup.find(GroupName);
539 assert(RI != DiagsInGroup.end() && "Referenced without existing?");
540 OS << RI->second.IDNo << ", ";
541 }
542 }
543
Peter Collingbourne51d77772011-10-06 13:03:08 +0000544 OS << "-1 };\n";
545 }
546 }
547 OS << "#endif // GET_DIAG_ARRAYS\n\n";
548
549 // Emit the table now.
550 OS << "\n#ifdef GET_DIAG_TABLE\n";
551 for (std::map<std::string, GroupInfo>::iterator
552 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
553 // Group option string.
554 OS << " { ";
555 OS << I->first.size() << ", ";
556 OS << "\"";
Benjamin Kramer037ad1b2011-11-15 12:54:53 +0000557 if (I->first.find_first_not_of("abcdefghijklmnopqrstuvwxyz"
558 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
559 "0123456789!@#$%^*-+=:?")!=std::string::npos)
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +0000560 PrintFatalError("Invalid character in diagnostic group '" +
561 I->first + "'");
Peter Collingbourne51d77772011-10-06 13:03:08 +0000562 OS.write_escaped(I->first) << "\","
563 << std::string(MaxLen-I->first.size()+1, ' ');
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000564
565 // Special handling for 'pedantic'.
566 const bool IsPedantic = I->first == "pedantic";
567
Peter Collingbourne51d77772011-10-06 13:03:08 +0000568 // Diagnostics in the group.
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000569 const bool hasDiags = !I->second.DiagsInGroup.empty() ||
570 (IsPedantic && !DiagsInPedantic.empty());
571 if (!hasDiags)
Peter Collingbourne51d77772011-10-06 13:03:08 +0000572 OS << "0, ";
573 else
574 OS << "DiagArray" << I->second.IDNo << ", ";
575
576 // Subgroups.
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000577 const bool hasSubGroups = !I->second.SubGroups.empty() ||
578 (IsPedantic && !GroupsInPedantic.empty());
579 if (!hasSubGroups)
Peter Collingbourne51d77772011-10-06 13:03:08 +0000580 OS << 0;
581 else
582 OS << "DiagSubGroup" << I->second.IDNo;
583 OS << " },\n";
584 }
585 OS << "#endif // GET_DIAG_TABLE\n\n";
586
587 // Emit the category table next.
588 DiagCategoryIDMap CategoriesByID(Records);
589 OS << "\n#ifdef GET_CATEGORY_TABLE\n";
590 for (DiagCategoryIDMap::iterator I = CategoriesByID.begin(),
591 E = CategoriesByID.end(); I != E; ++I)
592 OS << "CATEGORY(\"" << *I << "\", " << getDiagCategoryEnum(*I) << ")\n";
593 OS << "#endif // GET_CATEGORY_TABLE\n\n";
594}
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000595} // end namespace clang
Peter Collingbourne51d77772011-10-06 13:03:08 +0000596
597//===----------------------------------------------------------------------===//
598// Diagnostic name index generation
599//===----------------------------------------------------------------------===//
600
601namespace {
602struct RecordIndexElement
603{
604 RecordIndexElement() {}
605 explicit RecordIndexElement(Record const &R):
606 Name(R.getName()) {}
607
608 std::string Name;
609};
610
611struct RecordIndexElementSorter :
612 public std::binary_function<RecordIndexElement, RecordIndexElement, bool> {
613
614 bool operator()(RecordIndexElement const &Lhs,
615 RecordIndexElement const &Rhs) const {
616 return Lhs.Name < Rhs.Name;
617 }
618
619};
620
621} // end anonymous namespace.
622
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000623namespace clang {
624void EmitClangDiagsIndexName(RecordKeeper &Records, raw_ostream &OS) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000625 const std::vector<Record*> &Diags =
626 Records.getAllDerivedDefinitions("Diagnostic");
627
628 std::vector<RecordIndexElement> Index;
629 Index.reserve(Diags.size());
630 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
631 const Record &R = *(Diags[i]);
632 Index.push_back(RecordIndexElement(R));
633 }
634
635 std::sort(Index.begin(), Index.end(), RecordIndexElementSorter());
636
637 for (unsigned i = 0, e = Index.size(); i != e; ++i) {
638 const RecordIndexElement &R = Index[i];
639
640 OS << "DIAG_NAME_INDEX(" << R.Name << ")\n";
641 }
642}
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000643} // end namespace clang