blob: 40af9f6000d09eab2e20ab7df21667a600e818e1 [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"
Chandler Carruth30bc63f2012-12-04 09:53:39 +000015#include "llvm/ADT/Optional.h"
16#include "llvm/ADT/PointerUnion.h"
Jordan Rose98e73692013-01-10 18:50:46 +000017#include "llvm/ADT/SetVector.h"
18#include "llvm/ADT/SmallPtrSet.h"
Peter Collingbourne51d77772011-10-06 13:03:08 +000019#include "llvm/ADT/SmallString.h"
Jordan Rose98e73692013-01-10 18:50:46 +000020#include "llvm/ADT/SmallVector.h"
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +000021#include "llvm/ADT/StringMap.h"
Jordan Rose98e73692013-01-10 18:50:46 +000022#include "llvm/ADT/Twine.h"
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +000023#include "llvm/Support/Compiler.h"
24#include "llvm/Support/Debug.h"
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +000025#include "llvm/TableGen/Error.h"
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +000026#include "llvm/TableGen/Record.h"
Craig Topper354f20a2013-08-29 05:18:04 +000027#include "llvm/TableGen/StringToOffsetTable.h"
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +000028#include "llvm/TableGen/TableGenBackend.h"
Peter Collingbourne51d77772011-10-06 13:03:08 +000029#include <algorithm>
Joerg Sonnenberger7094dee2012-08-10 10:58:18 +000030#include <cctype>
Peter Collingbourne51d77772011-10-06 13:03:08 +000031#include <functional>
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +000032#include <map>
Benjamin Kramerd49cb202012-02-15 20:57:03 +000033#include <set>
Peter Collingbourne51d77772011-10-06 13:03:08 +000034using namespace llvm;
35
36//===----------------------------------------------------------------------===//
37// Diagnostic category computation code.
38//===----------------------------------------------------------------------===//
39
40namespace {
41class DiagGroupParentMap {
42 RecordKeeper &Records;
43 std::map<const Record*, std::vector<Record*> > Mapping;
44public:
45 DiagGroupParentMap(RecordKeeper &records) : Records(records) {
46 std::vector<Record*> DiagGroups
47 = Records.getAllDerivedDefinitions("DiagGroup");
48 for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
49 std::vector<Record*> SubGroups =
50 DiagGroups[i]->getValueAsListOfDefs("SubGroups");
51 for (unsigned j = 0, e = SubGroups.size(); j != e; ++j)
52 Mapping[SubGroups[j]].push_back(DiagGroups[i]);
53 }
54 }
Craig Topper519f26b2013-07-19 21:43:59 +000055
Peter Collingbourne51d77772011-10-06 13:03:08 +000056 const std::vector<Record*> &getParents(const Record *Group) {
57 return Mapping[Group];
58 }
59};
60} // end anonymous namespace.
61
Peter Collingbourne51d77772011-10-06 13:03:08 +000062static std::string
63getCategoryFromDiagGroup(const Record *Group,
64 DiagGroupParentMap &DiagGroupParents) {
65 // If the DiagGroup has a category, return it.
66 std::string CatName = Group->getValueAsString("CategoryName");
67 if (!CatName.empty()) return CatName;
Craig Topper519f26b2013-07-19 21:43:59 +000068
Peter Collingbourne51d77772011-10-06 13:03:08 +000069 // The diag group may the subgroup of one or more other diagnostic groups,
70 // check these for a category as well.
71 const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
72 for (unsigned i = 0, e = Parents.size(); i != e; ++i) {
73 CatName = getCategoryFromDiagGroup(Parents[i], DiagGroupParents);
74 if (!CatName.empty()) return CatName;
75 }
76 return "";
77}
78
79/// getDiagnosticCategory - Return the category that the specified diagnostic
80/// lives in.
81static std::string getDiagnosticCategory(const Record *R,
82 DiagGroupParentMap &DiagGroupParents) {
83 // If the diagnostic is in a group, and that group has a category, use it.
Sean Silva1ab46322012-10-10 20:25:43 +000084 if (DefInit *Group = dyn_cast<DefInit>(R->getValueInit("Group"))) {
Peter Collingbourne51d77772011-10-06 13:03:08 +000085 // Check the diagnostic's diag group for a category.
86 std::string CatName = getCategoryFromDiagGroup(Group->getDef(),
87 DiagGroupParents);
88 if (!CatName.empty()) return CatName;
89 }
Ted Kremeneke8cf7d12012-07-07 05:53:30 +000090
Peter Collingbourne51d77772011-10-06 13:03:08 +000091 // If the diagnostic itself has a category, get it.
92 return R->getValueAsString("CategoryName");
93}
94
95namespace {
96 class DiagCategoryIDMap {
97 RecordKeeper &Records;
98 StringMap<unsigned> CategoryIDs;
99 std::vector<std::string> CategoryStrings;
100 public:
101 DiagCategoryIDMap(RecordKeeper &records) : Records(records) {
102 DiagGroupParentMap ParentInfo(Records);
Craig Topper519f26b2013-07-19 21:43:59 +0000103
Peter Collingbourne51d77772011-10-06 13:03:08 +0000104 // The zero'th category is "".
105 CategoryStrings.push_back("");
106 CategoryIDs[""] = 0;
Craig Topper519f26b2013-07-19 21:43:59 +0000107
Peter Collingbourne51d77772011-10-06 13:03:08 +0000108 std::vector<Record*> Diags =
109 Records.getAllDerivedDefinitions("Diagnostic");
110 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
111 std::string Category = getDiagnosticCategory(Diags[i], ParentInfo);
112 if (Category.empty()) continue; // Skip diags with no category.
Craig Topper519f26b2013-07-19 21:43:59 +0000113
Peter Collingbourne51d77772011-10-06 13:03:08 +0000114 unsigned &ID = CategoryIDs[Category];
115 if (ID != 0) continue; // Already seen.
Craig Topper519f26b2013-07-19 21:43:59 +0000116
Peter Collingbourne51d77772011-10-06 13:03:08 +0000117 ID = CategoryStrings.size();
118 CategoryStrings.push_back(Category);
119 }
120 }
Craig Topper519f26b2013-07-19 21:43:59 +0000121
Peter Collingbourne51d77772011-10-06 13:03:08 +0000122 unsigned getID(StringRef CategoryString) {
123 return CategoryIDs[CategoryString];
124 }
Craig Topper519f26b2013-07-19 21:43:59 +0000125
Craig Topper85c48512013-07-21 22:20:10 +0000126 typedef std::vector<std::string>::const_iterator const_iterator;
127 const_iterator begin() const { return CategoryStrings.begin(); }
128 const_iterator end() const { return CategoryStrings.end(); }
Peter Collingbourne51d77772011-10-06 13:03:08 +0000129 };
Argyrios Kyrtzidisd42236e2012-03-06 00:00:38 +0000130
131 struct GroupInfo {
132 std::vector<const Record*> DiagsInGroup;
133 std::vector<std::string> SubGroups;
134 unsigned IDNo;
Jordan Rose98e73692013-01-10 18:50:46 +0000135
136 const Record *ExplicitDef;
137
138 GroupInfo() : ExplicitDef(0) {}
Argyrios Kyrtzidisd42236e2012-03-06 00:00:38 +0000139 };
Peter Collingbourne51d77772011-10-06 13:03:08 +0000140} // end anonymous namespace.
141
Jordan Rose98e73692013-01-10 18:50:46 +0000142static bool beforeThanCompare(const Record *LHS, const Record *RHS) {
143 assert(!LHS->getLoc().empty() && !RHS->getLoc().empty());
144 return
145 LHS->getLoc().front().getPointer() < RHS->getLoc().front().getPointer();
146}
147
148static bool beforeThanCompareGroups(const GroupInfo *LHS, const GroupInfo *RHS){
149 assert(!LHS->DiagsInGroup.empty() && !RHS->DiagsInGroup.empty());
150 return beforeThanCompare(LHS->DiagsInGroup.front(),
151 RHS->DiagsInGroup.front());
152}
153
154static SMRange findSuperClassRange(const Record *R, StringRef SuperName) {
155 ArrayRef<Record *> Supers = R->getSuperClasses();
156
157 for (size_t i = 0, e = Supers.size(); i < e; ++i)
158 if (Supers[i]->getName() == SuperName)
159 return R->getSuperClassRanges()[i];
160
161 return SMRange();
162}
163
Argyrios Kyrtzidisd42236e2012-03-06 00:00:38 +0000164/// \brief Invert the 1-[0/1] mapping of diags to group into a one to many
165/// mapping of groups to diags in the group.
166static void groupDiagnostics(const std::vector<Record*> &Diags,
167 const std::vector<Record*> &DiagGroups,
168 std::map<std::string, GroupInfo> &DiagsInGroup) {
Jordan Rose98e73692013-01-10 18:50:46 +0000169
Argyrios Kyrtzidisd42236e2012-03-06 00:00:38 +0000170 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
171 const Record *R = Diags[i];
Sean Silva1ab46322012-10-10 20:25:43 +0000172 DefInit *DI = dyn_cast<DefInit>(R->getValueInit("Group"));
Argyrios Kyrtzidisd42236e2012-03-06 00:00:38 +0000173 if (DI == 0) continue;
Richard Smith46484772012-05-04 19:05:50 +0000174 assert(R->getValueAsDef("Class")->getName() != "CLASS_NOTE" &&
175 "Note can't be in a DiagGroup");
Argyrios Kyrtzidisd42236e2012-03-06 00:00:38 +0000176 std::string GroupName = DI->getDef()->getValueAsString("GroupName");
177 DiagsInGroup[GroupName].DiagsInGroup.push_back(R);
178 }
Jordan Rose98e73692013-01-10 18:50:46 +0000179
180 typedef SmallPtrSet<GroupInfo *, 16> GroupSetTy;
181 GroupSetTy ImplicitGroups;
182
Argyrios Kyrtzidisd42236e2012-03-06 00:00:38 +0000183 // Add all DiagGroup's to the DiagsInGroup list to make sure we pick up empty
184 // groups (these are warnings that GCC supports that clang never produces).
185 for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
186 Record *Group = DiagGroups[i];
187 GroupInfo &GI = DiagsInGroup[Group->getValueAsString("GroupName")];
Jordan Rose98e73692013-01-10 18:50:46 +0000188 if (Group->isAnonymous()) {
189 if (GI.DiagsInGroup.size() > 1)
190 ImplicitGroups.insert(&GI);
191 } else {
192 if (GI.ExplicitDef)
193 assert(GI.ExplicitDef == Group);
194 else
195 GI.ExplicitDef = Group;
196 }
197
Argyrios Kyrtzidisd42236e2012-03-06 00:00:38 +0000198 std::vector<Record*> SubGroups = Group->getValueAsListOfDefs("SubGroups");
199 for (unsigned j = 0, e = SubGroups.size(); j != e; ++j)
200 GI.SubGroups.push_back(SubGroups[j]->getValueAsString("GroupName"));
201 }
Craig Topper519f26b2013-07-19 21:43:59 +0000202
Argyrios Kyrtzidisd42236e2012-03-06 00:00:38 +0000203 // Assign unique ID numbers to the groups.
204 unsigned IDNo = 0;
205 for (std::map<std::string, GroupInfo>::iterator
206 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I, ++IDNo)
207 I->second.IDNo = IDNo;
Jordan Rose98e73692013-01-10 18:50:46 +0000208
209 // Sort the implicit groups, so we can warn about them deterministically.
210 SmallVector<GroupInfo *, 16> SortedGroups(ImplicitGroups.begin(),
211 ImplicitGroups.end());
212 for (SmallVectorImpl<GroupInfo *>::iterator I = SortedGroups.begin(),
213 E = SortedGroups.end();
214 I != E; ++I) {
215 MutableArrayRef<const Record *> GroupDiags = (*I)->DiagsInGroup;
216 std::sort(GroupDiags.begin(), GroupDiags.end(), beforeThanCompare);
217 }
218 std::sort(SortedGroups.begin(), SortedGroups.end(), beforeThanCompareGroups);
219
220 // Warn about the same group being used anonymously in multiple places.
221 for (SmallVectorImpl<GroupInfo *>::const_iterator I = SortedGroups.begin(),
222 E = SortedGroups.end();
223 I != E; ++I) {
224 ArrayRef<const Record *> GroupDiags = (*I)->DiagsInGroup;
225
226 if ((*I)->ExplicitDef) {
227 std::string Name = (*I)->ExplicitDef->getValueAsString("GroupName");
228 for (ArrayRef<const Record *>::const_iterator DI = GroupDiags.begin(),
229 DE = GroupDiags.end();
230 DI != DE; ++DI) {
231 const DefInit *GroupInit = cast<DefInit>((*DI)->getValueInit("Group"));
232 const Record *NextDiagGroup = GroupInit->getDef();
233 if (NextDiagGroup == (*I)->ExplicitDef)
234 continue;
235
236 SMRange InGroupRange = findSuperClassRange(*DI, "InGroup");
237 SmallString<64> Replacement;
238 if (InGroupRange.isValid()) {
239 Replacement += "InGroup<";
240 Replacement += (*I)->ExplicitDef->getName();
241 Replacement += ">";
242 }
243 SMFixIt FixIt(InGroupRange, Replacement.str());
244
245 SrcMgr.PrintMessage(NextDiagGroup->getLoc().front(),
246 SourceMgr::DK_Error,
247 Twine("group '") + Name +
248 "' is referred to anonymously",
Dmitri Gribenko566bf622013-05-05 01:03:47 +0000249 None,
Jordan Rose98e73692013-01-10 18:50:46 +0000250 InGroupRange.isValid() ? FixIt
251 : ArrayRef<SMFixIt>());
252 SrcMgr.PrintMessage((*I)->ExplicitDef->getLoc().front(),
253 SourceMgr::DK_Note, "group defined here");
254 }
255 } else {
256 // If there's no existing named group, we should just warn once and use
257 // notes to list all the other cases.
258 ArrayRef<const Record *>::const_iterator DI = GroupDiags.begin(),
259 DE = GroupDiags.end();
260 assert(DI != DE && "We only care about groups with multiple uses!");
261
262 const DefInit *GroupInit = cast<DefInit>((*DI)->getValueInit("Group"));
263 const Record *NextDiagGroup = GroupInit->getDef();
264 std::string Name = NextDiagGroup->getValueAsString("GroupName");
265
266 SMRange InGroupRange = findSuperClassRange(*DI, "InGroup");
267 SrcMgr.PrintMessage(NextDiagGroup->getLoc().front(),
268 SourceMgr::DK_Error,
269 Twine("group '") + Name +
270 "' is referred to anonymously",
271 InGroupRange);
272
273 for (++DI; DI != DE; ++DI) {
274 GroupInit = cast<DefInit>((*DI)->getValueInit("Group"));
275 InGroupRange = findSuperClassRange(*DI, "InGroup");
276 SrcMgr.PrintMessage(GroupInit->getDef()->getLoc().front(),
277 SourceMgr::DK_Note, "also referenced here",
278 InGroupRange);
279 }
280 }
281 }
Argyrios Kyrtzidisd42236e2012-03-06 00:00:38 +0000282}
Peter Collingbourne51d77772011-10-06 13:03:08 +0000283
284//===----------------------------------------------------------------------===//
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000285// Infer members of -Wpedantic.
286//===----------------------------------------------------------------------===//
287
288typedef std::vector<const Record *> RecordVec;
289typedef llvm::DenseSet<const Record *> RecordSet;
290typedef llvm::PointerUnion<RecordVec*, RecordSet*> VecOrSet;
291
292namespace {
293class InferPedantic {
294 typedef llvm::DenseMap<const Record*,
Ted Kremenek943f9092013-02-21 01:29:01 +0000295 std::pair<unsigned, Optional<unsigned> > > GMap;
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000296
297 DiagGroupParentMap &DiagGroupParents;
298 const std::vector<Record*> &Diags;
299 const std::vector<Record*> DiagGroups;
300 std::map<std::string, GroupInfo> &DiagsInGroup;
301 llvm::DenseSet<const Record*> DiagsSet;
302 GMap GroupCount;
303public:
304 InferPedantic(DiagGroupParentMap &DiagGroupParents,
305 const std::vector<Record*> &Diags,
306 const std::vector<Record*> &DiagGroups,
307 std::map<std::string, GroupInfo> &DiagsInGroup)
308 : DiagGroupParents(DiagGroupParents),
309 Diags(Diags),
310 DiagGroups(DiagGroups),
311 DiagsInGroup(DiagsInGroup) {}
312
313 /// Compute the set of diagnostics and groups that are immediately
314 /// in -Wpedantic.
315 void compute(VecOrSet DiagsInPedantic,
316 VecOrSet GroupsInPedantic);
317
318private:
319 /// Determine whether a group is a subgroup of another group.
320 bool isSubGroupOfGroup(const Record *Group,
321 llvm::StringRef RootGroupName);
322
323 /// Determine if the diagnostic is an extension.
324 bool isExtension(const Record *Diag);
325
Ted Kremenekbb5185c2012-08-10 20:50:00 +0000326 /// Determine if the diagnostic is off by default.
327 bool isOffByDefault(const Record *Diag);
328
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000329 /// Increment the count for a group, and transitively marked
330 /// parent groups when appropriate.
331 void markGroup(const Record *Group);
332
333 /// Return true if the diagnostic is in a pedantic group.
334 bool groupInPedantic(const Record *Group, bool increment = false);
335};
336} // end anonymous namespace
337
338bool InferPedantic::isSubGroupOfGroup(const Record *Group,
339 llvm::StringRef GName) {
340
341 const std::string &GroupName = Group->getValueAsString("GroupName");
342 if (GName == GroupName)
343 return true;
344
345 const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
346 for (unsigned i = 0, e = Parents.size(); i != e; ++i)
347 if (isSubGroupOfGroup(Parents[i], GName))
348 return true;
349
350 return false;
351}
352
353/// Determine if the diagnostic is an extension.
354bool InferPedantic::isExtension(const Record *Diag) {
355 const std::string &ClsName = Diag->getValueAsDef("Class")->getName();
356 return ClsName == "CLASS_EXTENSION";
357}
358
Ted Kremenekbb5185c2012-08-10 20:50:00 +0000359bool InferPedantic::isOffByDefault(const Record *Diag) {
360 const std::string &DefMap = Diag->getValueAsDef("DefaultMapping")->getName();
361 return DefMap == "MAP_IGNORE";
362}
363
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000364bool InferPedantic::groupInPedantic(const Record *Group, bool increment) {
365 GMap::mapped_type &V = GroupCount[Group];
366 // Lazily compute the threshold value for the group count.
367 if (!V.second.hasValue()) {
368 const GroupInfo &GI = DiagsInGroup[Group->getValueAsString("GroupName")];
369 V.second = GI.SubGroups.size() + GI.DiagsInGroup.size();
370 }
371
372 if (increment)
373 ++V.first;
374
375 // Consider a group in -Wpendatic IFF if has at least one diagnostic
376 // or subgroup AND all of those diagnostics and subgroups are covered
377 // by -Wpedantic via our computation.
378 return V.first != 0 && V.first == V.second.getValue();
379}
380
381void InferPedantic::markGroup(const Record *Group) {
382 // If all the diagnostics and subgroups have been marked as being
383 // covered by -Wpedantic, increment the count of parent groups. Once the
384 // group's count is equal to the number of subgroups and diagnostics in
385 // that group, we can safely add this group to -Wpedantic.
386 if (groupInPedantic(Group, /* increment */ true)) {
387 const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
388 for (unsigned i = 0, e = Parents.size(); i != e; ++i)
389 markGroup(Parents[i]);
390 }
391}
392
393void InferPedantic::compute(VecOrSet DiagsInPedantic,
394 VecOrSet GroupsInPedantic) {
Ted Kremenekbb5185c2012-08-10 20:50:00 +0000395 // All extensions that are not on by default are implicitly in the
396 // "pedantic" group. For those that aren't explicitly included in -Wpedantic,
397 // mark them for consideration to be included in -Wpedantic directly.
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000398 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
399 Record *R = Diags[i];
Ted Kremenekbb5185c2012-08-10 20:50:00 +0000400 if (isExtension(R) && isOffByDefault(R)) {
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000401 DiagsSet.insert(R);
Sean Silva1ab46322012-10-10 20:25:43 +0000402 if (DefInit *Group = dyn_cast<DefInit>(R->getValueInit("Group"))) {
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000403 const Record *GroupRec = Group->getDef();
404 if (!isSubGroupOfGroup(GroupRec, "pedantic")) {
405 markGroup(GroupRec);
406 }
407 }
408 }
409 }
410
411 // Compute the set of diagnostics that are directly in -Wpedantic. We
412 // march through Diags a second time to ensure the results are emitted
413 // in deterministic order.
414 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
415 Record *R = Diags[i];
416 if (!DiagsSet.count(R))
417 continue;
418 // Check if the group is implicitly in -Wpedantic. If so,
419 // the diagnostic should not be directly included in the -Wpedantic
420 // diagnostic group.
Sean Silva1ab46322012-10-10 20:25:43 +0000421 if (DefInit *Group = dyn_cast<DefInit>(R->getValueInit("Group")))
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000422 if (groupInPedantic(Group->getDef()))
423 continue;
424
425 // The diagnostic is not included in a group that is (transitively) in
426 // -Wpedantic. Include it in -Wpedantic directly.
427 if (RecordVec *V = DiagsInPedantic.dyn_cast<RecordVec*>())
428 V->push_back(R);
429 else {
430 DiagsInPedantic.get<RecordSet*>()->insert(R);
431 }
432 }
433
434 if (!GroupsInPedantic)
435 return;
436
437 // Compute the set of groups that are directly in -Wpedantic. We
438 // march through the groups to ensure the results are emitted
439 /// in a deterministc order.
440 for (unsigned i = 0, ei = DiagGroups.size(); i != ei; ++i) {
441 Record *Group = DiagGroups[i];
442 if (!groupInPedantic(Group))
443 continue;
444
445 unsigned ParentsInPedantic = 0;
446 const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
447 for (unsigned j = 0, ej = Parents.size(); j != ej; ++j) {
448 if (groupInPedantic(Parents[j]))
449 ++ParentsInPedantic;
450 }
451 // If all the parents are in -Wpedantic, this means that this diagnostic
452 // group will be indirectly included by -Wpedantic already. In that
453 // case, do not add it directly to -Wpedantic. If the group has no
454 // parents, obviously it should go into -Wpedantic.
455 if (Parents.size() > 0 && ParentsInPedantic == Parents.size())
456 continue;
457
458 if (RecordVec *V = GroupsInPedantic.dyn_cast<RecordVec*>())
459 V->push_back(Group);
460 else {
461 GroupsInPedantic.get<RecordSet*>()->insert(Group);
462 }
463 }
464}
465
466//===----------------------------------------------------------------------===//
Peter Collingbourne51d77772011-10-06 13:03:08 +0000467// Warning Tables (.inc file) generation.
468//===----------------------------------------------------------------------===//
469
Ted Kremenek4a535362012-08-07 05:01:49 +0000470static bool isError(const Record &Diag) {
471 const std::string &ClsName = Diag.getValueAsDef("Class")->getName();
472 return ClsName == "CLASS_ERROR";
473}
474
Stephen Hines651f13c2014-04-23 16:59:28 -0700475static bool isRemark(const Record &Diag) {
476 const std::string &ClsName = Diag.getValueAsDef("Class")->getName();
477 return ClsName == "CLASS_REMARK";
478}
479
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000480/// ClangDiagsDefsEmitter - The top-level class emits .def files containing
481/// declarations of Clang diagnostics.
482namespace clang {
483void EmitClangDiagsDefs(RecordKeeper &Records, raw_ostream &OS,
484 const std::string &Component) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000485 // Write the #if guard
486 if (!Component.empty()) {
Benjamin Kramer90c78922011-11-06 20:36:48 +0000487 std::string ComponentName = StringRef(Component).upper();
Peter Collingbourne51d77772011-10-06 13:03:08 +0000488 OS << "#ifdef " << ComponentName << "START\n";
489 OS << "__" << ComponentName << "START = DIAG_START_" << ComponentName
490 << ",\n";
491 OS << "#undef " << ComponentName << "START\n";
492 OS << "#endif\n\n";
493 }
494
495 const std::vector<Record*> &Diags =
496 Records.getAllDerivedDefinitions("Diagnostic");
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000497
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000498 std::vector<Record*> DiagGroups
499 = Records.getAllDerivedDefinitions("DiagGroup");
Argyrios Kyrtzidisd42236e2012-03-06 00:00:38 +0000500
501 std::map<std::string, GroupInfo> DiagsInGroup;
502 groupDiagnostics(Diags, DiagGroups, DiagsInGroup);
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000503
Peter Collingbourne51d77772011-10-06 13:03:08 +0000504 DiagCategoryIDMap CategoryIDs(Records);
505 DiagGroupParentMap DGParentMap(Records);
506
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000507 // Compute the set of diagnostics that are in -Wpedantic.
508 RecordSet DiagsInPedantic;
509 InferPedantic inferPedantic(DGParentMap, Diags, DiagGroups, DiagsInGroup);
510 inferPedantic.compute(&DiagsInPedantic, (RecordVec*)0);
511
Peter Collingbourne51d77772011-10-06 13:03:08 +0000512 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
513 const Record &R = *Diags[i];
Craig Topper519f26b2013-07-19 21:43:59 +0000514
Ted Kremenek4a535362012-08-07 05:01:49 +0000515 // Check if this is an error that is accidentally in a warning
516 // group.
517 if (isError(R)) {
Sean Silva1ab46322012-10-10 20:25:43 +0000518 if (DefInit *Group = dyn_cast<DefInit>(R.getValueInit("Group"))) {
Ted Kremenek4a535362012-08-07 05:01:49 +0000519 const Record *GroupRec = Group->getDef();
520 const std::string &GroupName = GroupRec->getValueAsString("GroupName");
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +0000521 PrintFatalError(R.getLoc(), "Error " + R.getName() +
522 " cannot be in a warning group [" + GroupName + "]");
Ted Kremenek4a535362012-08-07 05:01:49 +0000523 }
524 }
525
Stephen Hines651f13c2014-04-23 16:59:28 -0700526 // Check that all remarks have an associated diagnostic group.
527 if (isRemark(R)) {
528 if (!isa<DefInit>(R.getValueInit("Group"))) {
529 PrintFatalError(R.getLoc(), "Error " + R.getName() +
530 " not in any diagnostic group");
531 }
532 }
533
Peter Collingbourne51d77772011-10-06 13:03:08 +0000534 // Filter by component.
535 if (!Component.empty() && Component != R.getValueAsString("Component"))
536 continue;
537
538 OS << "DIAG(" << R.getName() << ", ";
539 OS << R.getValueAsDef("Class")->getName();
540 OS << ", diag::" << R.getValueAsDef("DefaultMapping")->getName();
Craig Topper519f26b2013-07-19 21:43:59 +0000541
Peter Collingbourne51d77772011-10-06 13:03:08 +0000542 // Description string.
543 OS << ", \"";
544 OS.write_escaped(R.getValueAsString("Text")) << '"';
Craig Topper519f26b2013-07-19 21:43:59 +0000545
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000546 // Warning associated with the diagnostic. This is stored as an index into
547 // the alphabetically sorted warning table.
Sean Silva1ab46322012-10-10 20:25:43 +0000548 if (DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group"))) {
Argyrios Kyrtzidisd42236e2012-03-06 00:00:38 +0000549 std::map<std::string, GroupInfo>::iterator I =
550 DiagsInGroup.find(DI->getDef()->getValueAsString("GroupName"));
551 assert(I != DiagsInGroup.end());
552 OS << ", " << I->second.IDNo;
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000553 } else if (DiagsInPedantic.count(&R)) {
554 std::map<std::string, GroupInfo>::iterator I =
555 DiagsInGroup.find("pedantic");
556 assert(I != DiagsInGroup.end() && "pedantic group not defined");
557 OS << ", " << I->second.IDNo;
Peter Collingbourne51d77772011-10-06 13:03:08 +0000558 } else {
Benjamin Kramerd49cb202012-02-15 20:57:03 +0000559 OS << ", 0";
Peter Collingbourne51d77772011-10-06 13:03:08 +0000560 }
561
Richard Smith3347b492013-11-12 02:41:45 +0000562 // SFINAE response.
563 OS << ", " << R.getValueAsDef("SFINAE")->getName();
564
565 // Default warning has no Werror bit.
566 if (R.getValueAsBit("WarningNoWerror"))
Peter Collingbourne51d77772011-10-06 13:03:08 +0000567 OS << ", true";
568 else
569 OS << ", false";
570
Richard Smith3347b492013-11-12 02:41:45 +0000571 // Default warning show in system header bit.
572 if (R.getValueAsBit("WarningShowInSystemHeader"))
Peter Collingbourne51d77772011-10-06 13:03:08 +0000573 OS << ", true";
574 else
575 OS << ", false";
576
Peter Collingbourne51d77772011-10-06 13:03:08 +0000577 // Category number.
578 OS << ", " << CategoryIDs.getID(getDiagnosticCategory(&R, DGParentMap));
Peter Collingbourne51d77772011-10-06 13:03:08 +0000579 OS << ")\n";
580 }
581}
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000582} // end namespace clang
Peter Collingbourne51d77772011-10-06 13:03:08 +0000583
584//===----------------------------------------------------------------------===//
585// Warning Group Tables generation
586//===----------------------------------------------------------------------===//
587
588static std::string getDiagCategoryEnum(llvm::StringRef name) {
589 if (name.empty())
590 return "DiagCat_None";
Dylan Noblesmith36d59272012-02-13 12:32:26 +0000591 SmallString<256> enumName = llvm::StringRef("DiagCat_");
Peter Collingbourne51d77772011-10-06 13:03:08 +0000592 for (llvm::StringRef::iterator I = name.begin(), E = name.end(); I != E; ++I)
593 enumName += isalnum(*I) ? *I : '_';
594 return enumName.str();
595}
Craig Topper519f26b2013-07-19 21:43:59 +0000596
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000597namespace clang {
598void EmitClangDiagGroups(RecordKeeper &Records, raw_ostream &OS) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000599 // Compute a mapping from a DiagGroup to all of its parents.
600 DiagGroupParentMap DGParentMap(Records);
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000601
Peter Collingbourne51d77772011-10-06 13:03:08 +0000602 std::vector<Record*> Diags =
603 Records.getAllDerivedDefinitions("Diagnostic");
Craig Topper519f26b2013-07-19 21:43:59 +0000604
Peter Collingbourne51d77772011-10-06 13:03:08 +0000605 std::vector<Record*> DiagGroups
606 = Records.getAllDerivedDefinitions("DiagGroup");
Argyrios Kyrtzidisd42236e2012-03-06 00:00:38 +0000607
608 std::map<std::string, GroupInfo> DiagsInGroup;
609 groupDiagnostics(Diags, DiagGroups, DiagsInGroup);
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000610
611 // All extensions are implicitly in the "pedantic" group. Record the
612 // implicit set of groups in the "pedantic" group, and use this information
613 // later when emitting the group information for Pedantic.
614 RecordVec DiagsInPedantic;
615 RecordVec GroupsInPedantic;
616 InferPedantic inferPedantic(DGParentMap, Diags, DiagGroups, DiagsInGroup);
617 inferPedantic.compute(&DiagsInPedantic, &GroupsInPedantic);
618
Peter Collingbourne51d77772011-10-06 13:03:08 +0000619 // Walk through the groups emitting an array for each diagnostic of the diags
620 // that are mapped to.
621 OS << "\n#ifdef GET_DIAG_ARRAYS\n";
622 unsigned MaxLen = 0;
Craig Topperb1aa16a2013-08-28 04:02:50 +0000623 OS << "static const int16_t DiagArrays[] = {\n"
624 << " /* Empty */ -1,\n";
Craig Topper85c48512013-07-21 22:20:10 +0000625 for (std::map<std::string, GroupInfo>::const_iterator
Peter Collingbourne51d77772011-10-06 13:03:08 +0000626 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
627 MaxLen = std::max(MaxLen, (unsigned)I->first.size());
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000628 const bool IsPedantic = I->first == "pedantic";
629
Craig Topper85c48512013-07-21 22:20:10 +0000630 const std::vector<const Record*> &V = I->second.DiagsInGroup;
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000631 if (!V.empty() || (IsPedantic && !DiagsInPedantic.empty())) {
Craig Topperb1aa16a2013-08-28 04:02:50 +0000632 OS << " /* DiagArray" << I->second.IDNo << " */ ";
Peter Collingbourne51d77772011-10-06 13:03:08 +0000633 for (unsigned i = 0, e = V.size(); i != e; ++i)
634 OS << "diag::" << V[i]->getName() << ", ";
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000635 // Emit the diagnostics implicitly in "pedantic".
636 if (IsPedantic) {
637 for (unsigned i = 0, e = DiagsInPedantic.size(); i != e; ++i)
638 OS << "diag::" << DiagsInPedantic[i]->getName() << ", ";
639 }
Craig Topperb1aa16a2013-08-28 04:02:50 +0000640 OS << "-1,\n";
Peter Collingbourne51d77772011-10-06 13:03:08 +0000641 }
Craig Topperb1aa16a2013-08-28 04:02:50 +0000642 }
643 OS << "};\n\n";
644
645 OS << "static const int16_t DiagSubGroups[] = {\n"
646 << " /* Empty */ -1,\n";
647 for (std::map<std::string, GroupInfo>::const_iterator
648 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
649 const bool IsPedantic = I->first == "pedantic";
Craig Topper519f26b2013-07-19 21:43:59 +0000650
Peter Collingbourne51d77772011-10-06 13:03:08 +0000651 const std::vector<std::string> &SubGroups = I->second.SubGroups;
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000652 if (!SubGroups.empty() || (IsPedantic && !GroupsInPedantic.empty())) {
Craig Topperb1aa16a2013-08-28 04:02:50 +0000653 OS << " /* DiagSubGroup" << I->second.IDNo << " */ ";
Peter Collingbourne51d77772011-10-06 13:03:08 +0000654 for (unsigned i = 0, e = SubGroups.size(); i != e; ++i) {
Craig Topper85c48512013-07-21 22:20:10 +0000655 std::map<std::string, GroupInfo>::const_iterator RI =
Peter Collingbourne51d77772011-10-06 13:03:08 +0000656 DiagsInGroup.find(SubGroups[i]);
657 assert(RI != DiagsInGroup.end() && "Referenced without existing?");
658 OS << RI->second.IDNo << ", ";
659 }
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000660 // Emit the groups implicitly in "pedantic".
661 if (IsPedantic) {
662 for (unsigned i = 0, e = GroupsInPedantic.size(); i != e; ++i) {
663 const std::string &GroupName =
664 GroupsInPedantic[i]->getValueAsString("GroupName");
Craig Topper85c48512013-07-21 22:20:10 +0000665 std::map<std::string, GroupInfo>::const_iterator RI =
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000666 DiagsInGroup.find(GroupName);
667 assert(RI != DiagsInGroup.end() && "Referenced without existing?");
668 OS << RI->second.IDNo << ", ";
669 }
670 }
671
Craig Topperb1aa16a2013-08-28 04:02:50 +0000672 OS << "-1,\n";
Peter Collingbourne51d77772011-10-06 13:03:08 +0000673 }
674 }
Craig Topper354f20a2013-08-29 05:18:04 +0000675 OS << "};\n\n";
676
677 StringToOffsetTable GroupNames;
678 for (std::map<std::string, GroupInfo>::const_iterator
679 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
680 // Store a pascal-style length byte at the beginning of the string.
681 std::string Name = char(I->first.size()) + I->first;
682 GroupNames.GetOrAddStringOffset(Name, false);
683 }
684
685 OS << "static const char DiagGroupNames[] = {\n";
686 GroupNames.EmitString(OS);
687 OS << "};\n\n";
688
Peter Collingbourne51d77772011-10-06 13:03:08 +0000689 OS << "#endif // GET_DIAG_ARRAYS\n\n";
Craig Topper519f26b2013-07-19 21:43:59 +0000690
Peter Collingbourne51d77772011-10-06 13:03:08 +0000691 // Emit the table now.
692 OS << "\n#ifdef GET_DIAG_TABLE\n";
Craig Topperb1aa16a2013-08-28 04:02:50 +0000693 unsigned SubGroupIndex = 1, DiagArrayIndex = 1;
Craig Topper85c48512013-07-21 22:20:10 +0000694 for (std::map<std::string, GroupInfo>::const_iterator
Peter Collingbourne51d77772011-10-06 13:03:08 +0000695 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
696 // Group option string.
Craig Topper354f20a2013-08-29 05:18:04 +0000697 OS << " { /* ";
Benjamin Kramer037ad1b2011-11-15 12:54:53 +0000698 if (I->first.find_first_not_of("abcdefghijklmnopqrstuvwxyz"
699 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
700 "0123456789!@#$%^*-+=:?")!=std::string::npos)
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +0000701 PrintFatalError("Invalid character in diagnostic group '" +
702 I->first + "'");
Craig Topper354f20a2013-08-29 05:18:04 +0000703 OS << I->first << " */ " << std::string(MaxLen-I->first.size(), ' ');
704 // Store a pascal-style length byte at the beginning of the string.
705 std::string Name = char(I->first.size()) + I->first;
706 OS << GroupNames.GetOrAddStringOffset(Name, false) << ", ";
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000707
708 // Special handling for 'pedantic'.
709 const bool IsPedantic = I->first == "pedantic";
710
Peter Collingbourne51d77772011-10-06 13:03:08 +0000711 // Diagnostics in the group.
Craig Topperb1aa16a2013-08-28 04:02:50 +0000712 const std::vector<const Record*> &V = I->second.DiagsInGroup;
713 const bool hasDiags = !V.empty() ||
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000714 (IsPedantic && !DiagsInPedantic.empty());
Craig Topperb1aa16a2013-08-28 04:02:50 +0000715 if (hasDiags) {
716 OS << "/* DiagArray" << I->second.IDNo << " */ "
717 << DiagArrayIndex << ", ";
718 if (IsPedantic)
719 DiagArrayIndex += DiagsInPedantic.size();
720 DiagArrayIndex += V.size() + 1;
721 } else {
Craig Topper354f20a2013-08-29 05:18:04 +0000722 OS << "/* Empty */ 0, ";
Craig Topperb1aa16a2013-08-28 04:02:50 +0000723 }
Craig Topper519f26b2013-07-19 21:43:59 +0000724
Peter Collingbourne51d77772011-10-06 13:03:08 +0000725 // Subgroups.
Craig Topperb1aa16a2013-08-28 04:02:50 +0000726 const std::vector<std::string> &SubGroups = I->second.SubGroups;
727 const bool hasSubGroups = !SubGroups.empty() ||
Ted Kremeneke8cf7d12012-07-07 05:53:30 +0000728 (IsPedantic && !GroupsInPedantic.empty());
Craig Topperb1aa16a2013-08-28 04:02:50 +0000729 if (hasSubGroups) {
730 OS << "/* DiagSubGroup" << I->second.IDNo << " */ " << SubGroupIndex;
731 if (IsPedantic)
732 SubGroupIndex += GroupsInPedantic.size();
733 SubGroupIndex += SubGroups.size() + 1;
734 } else {
Craig Topper354f20a2013-08-29 05:18:04 +0000735 OS << "/* Empty */ 0";
Craig Topperb1aa16a2013-08-28 04:02:50 +0000736 }
Peter Collingbourne51d77772011-10-06 13:03:08 +0000737 OS << " },\n";
738 }
739 OS << "#endif // GET_DIAG_TABLE\n\n";
Craig Topper519f26b2013-07-19 21:43:59 +0000740
Peter Collingbourne51d77772011-10-06 13:03:08 +0000741 // Emit the category table next.
742 DiagCategoryIDMap CategoriesByID(Records);
743 OS << "\n#ifdef GET_CATEGORY_TABLE\n";
Craig Topper85c48512013-07-21 22:20:10 +0000744 for (DiagCategoryIDMap::const_iterator I = CategoriesByID.begin(),
Peter Collingbourne51d77772011-10-06 13:03:08 +0000745 E = CategoriesByID.end(); I != E; ++I)
746 OS << "CATEGORY(\"" << *I << "\", " << getDiagCategoryEnum(*I) << ")\n";
747 OS << "#endif // GET_CATEGORY_TABLE\n\n";
748}
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000749} // end namespace clang
Peter Collingbourne51d77772011-10-06 13:03:08 +0000750
751//===----------------------------------------------------------------------===//
752// Diagnostic name index generation
753//===----------------------------------------------------------------------===//
754
755namespace {
756struct RecordIndexElement
757{
758 RecordIndexElement() {}
759 explicit RecordIndexElement(Record const &R):
760 Name(R.getName()) {}
Craig Topper519f26b2013-07-19 21:43:59 +0000761
Peter Collingbourne51d77772011-10-06 13:03:08 +0000762 std::string Name;
763};
Peter Collingbourne51d77772011-10-06 13:03:08 +0000764} // end anonymous namespace.
765
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000766namespace clang {
767void EmitClangDiagsIndexName(RecordKeeper &Records, raw_ostream &OS) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000768 const std::vector<Record*> &Diags =
769 Records.getAllDerivedDefinitions("Diagnostic");
Craig Topper519f26b2013-07-19 21:43:59 +0000770
Peter Collingbourne51d77772011-10-06 13:03:08 +0000771 std::vector<RecordIndexElement> Index;
772 Index.reserve(Diags.size());
773 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
Craig Topper519f26b2013-07-19 21:43:59 +0000774 const Record &R = *(Diags[i]);
Peter Collingbourne51d77772011-10-06 13:03:08 +0000775 Index.push_back(RecordIndexElement(R));
776 }
Craig Topper519f26b2013-07-19 21:43:59 +0000777
Stephen Hines651f13c2014-04-23 16:59:28 -0700778 std::sort(Index.begin(), Index.end(),
779 [](const RecordIndexElement &Lhs,
780 const RecordIndexElement &Rhs) { return Lhs.Name < Rhs.Name; });
Craig Topper519f26b2013-07-19 21:43:59 +0000781
Peter Collingbourne51d77772011-10-06 13:03:08 +0000782 for (unsigned i = 0, e = Index.size(); i != e; ++i) {
783 const RecordIndexElement &R = Index[i];
Craig Topper519f26b2013-07-19 21:43:59 +0000784
Peter Collingbourne51d77772011-10-06 13:03:08 +0000785 OS << "DIAG_NAME_INDEX(" << R.Name << ")\n";
786 }
787}
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000788} // end namespace clang