blob: bbc2bdb065fe7ba9962176381453652a1009c607 [file] [log] [blame]
Peter Collingbournebee583f2011-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 Collingbournebee583f2011-10-06 13:03:08 +000014#include "llvm/ADT/DenseSet.h"
Chandler Carruth59ff16c2012-12-04 09:53:39 +000015#include "llvm/ADT/Optional.h"
16#include "llvm/ADT/PointerUnion.h"
Jordan Rosec3b23aa2013-01-10 18:50:46 +000017#include "llvm/ADT/SetVector.h"
18#include "llvm/ADT/SmallPtrSet.h"
Peter Collingbournebee583f2011-10-06 13:03:08 +000019#include "llvm/ADT/SmallString.h"
Jordan Rosec3b23aa2013-01-10 18:50:46 +000020#include "llvm/ADT/SmallVector.h"
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +000021#include "llvm/ADT/StringMap.h"
Jordan Rosec3b23aa2013-01-10 18:50:46 +000022#include "llvm/ADT/Twine.h"
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +000023#include "llvm/Support/Compiler.h"
24#include "llvm/Support/Debug.h"
Joerg Sonnenberger691a16b2012-10-25 16:37:08 +000025#include "llvm/TableGen/Error.h"
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +000026#include "llvm/TableGen/Record.h"
Craig Topperda7cf8a2013-08-29 05:18:04 +000027#include "llvm/TableGen/StringToOffsetTable.h"
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +000028#include "llvm/TableGen/TableGenBackend.h"
Peter Collingbournebee583f2011-10-06 13:03:08 +000029#include <algorithm>
Joerg Sonnenberger42cf2682012-08-10 10:58:18 +000030#include <cctype>
Peter Collingbournebee583f2011-10-06 13:03:08 +000031#include <functional>
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +000032#include <map>
Benjamin Kramera8cafe22012-02-15 20:57:03 +000033#include <set>
Peter Collingbournebee583f2011-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 Topperf3932e32013-07-19 21:43:59 +000055
Peter Collingbournebee583f2011-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 Collingbournebee583f2011-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 Topperf3932e32013-07-19 21:43:59 +000068
Peter Collingbournebee583f2011-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 Silva1c4aaa82012-10-10 20:25:43 +000084 if (DefInit *Group = dyn_cast<DefInit>(R->getValueInit("Group"))) {
Peter Collingbournebee583f2011-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 Kremenekb22ea2a2012-07-07 05:53:30 +000090
Peter Collingbournebee583f2011-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 Topperf3932e32013-07-19 21:43:59 +0000103
Peter Collingbournebee583f2011-10-06 13:03:08 +0000104 // The zero'th category is "".
105 CategoryStrings.push_back("");
106 CategoryIDs[""] = 0;
Craig Topperf3932e32013-07-19 21:43:59 +0000107
Peter Collingbournebee583f2011-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 Topperf3932e32013-07-19 21:43:59 +0000113
Peter Collingbournebee583f2011-10-06 13:03:08 +0000114 unsigned &ID = CategoryIDs[Category];
115 if (ID != 0) continue; // Already seen.
Craig Topperf3932e32013-07-19 21:43:59 +0000116
Peter Collingbournebee583f2011-10-06 13:03:08 +0000117 ID = CategoryStrings.size();
118 CategoryStrings.push_back(Category);
119 }
120 }
Craig Topperf3932e32013-07-19 21:43:59 +0000121
Peter Collingbournebee583f2011-10-06 13:03:08 +0000122 unsigned getID(StringRef CategoryString) {
123 return CategoryIDs[CategoryString];
124 }
Craig Topperf3932e32013-07-19 21:43:59 +0000125
Craig Toppera9bcac52013-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 Collingbournebee583f2011-10-06 13:03:08 +0000129 };
Argyrios Kyrtzidis87acf192012-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 Rosec3b23aa2013-01-10 18:50:46 +0000135
136 const Record *ExplicitDef;
137
Craig Topper8ae12032014-05-07 06:21:57 +0000138 GroupInfo() : ExplicitDef(nullptr) {}
Argyrios Kyrtzidis87acf192012-03-06 00:00:38 +0000139 };
Peter Collingbournebee583f2011-10-06 13:03:08 +0000140} // end anonymous namespace.
141
Jordan Rosec3b23aa2013-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) {
Craig Topper25761242016-01-18 19:52:54 +0000155 ArrayRef<std::pair<Record *, SMRange>> Supers = R->getSuperClasses();
156 auto I = std::find_if(Supers.begin(), Supers.end(),
157 [&](const std::pair<Record *, SMRange> &SuperPair) {
158 return SuperPair.first->getName() == SuperName;
159 });
160 return (I != Supers.end()) ? I->second : SMRange();
Jordan Rosec3b23aa2013-01-10 18:50:46 +0000161}
162
Argyrios Kyrtzidis87acf192012-03-06 00:00:38 +0000163/// \brief Invert the 1-[0/1] mapping of diags to group into a one to many
164/// mapping of groups to diags in the group.
165static void groupDiagnostics(const std::vector<Record*> &Diags,
166 const std::vector<Record*> &DiagGroups,
167 std::map<std::string, GroupInfo> &DiagsInGroup) {
Jordan Rosec3b23aa2013-01-10 18:50:46 +0000168
Argyrios Kyrtzidis87acf192012-03-06 00:00:38 +0000169 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
170 const Record *R = Diags[i];
Sean Silva1c4aaa82012-10-10 20:25:43 +0000171 DefInit *DI = dyn_cast<DefInit>(R->getValueInit("Group"));
Craig Topper8ae12032014-05-07 06:21:57 +0000172 if (!DI)
173 continue;
Richard Smithce52ca12012-05-04 19:05:50 +0000174 assert(R->getValueAsDef("Class")->getName() != "CLASS_NOTE" &&
175 "Note can't be in a DiagGroup");
Argyrios Kyrtzidis87acf192012-03-06 00:00:38 +0000176 std::string GroupName = DI->getDef()->getValueAsString("GroupName");
177 DiagsInGroup[GroupName].DiagsInGroup.push_back(R);
178 }
Jordan Rosec3b23aa2013-01-10 18:50:46 +0000179
180 typedef SmallPtrSet<GroupInfo *, 16> GroupSetTy;
181 GroupSetTy ImplicitGroups;
182
Argyrios Kyrtzidis87acf192012-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 Rosec3b23aa2013-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 Kyrtzidis87acf192012-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 Topperf3932e32013-07-19 21:43:59 +0000202
Argyrios Kyrtzidis87acf192012-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 Rosec3b23aa2013-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 }
Yaron Keren92e1b622015-03-18 10:17:07 +0000243 SMFixIt FixIt(InGroupRange, Replacement);
Jordan Rosec3b23aa2013-01-10 18:50:46 +0000244
245 SrcMgr.PrintMessage(NextDiagGroup->getLoc().front(),
246 SourceMgr::DK_Error,
247 Twine("group '") + Name +
248 "' is referred to anonymously",
Dmitri Gribenko010316c2013-05-05 01:03:47 +0000249 None,
Jordan Rosec3b23aa2013-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 Kyrtzidis87acf192012-03-06 00:00:38 +0000282}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000283
284//===----------------------------------------------------------------------===//
Ted Kremenekb22ea2a2012-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 Kremenek03325582013-02-21 01:29:01 +0000295 std::pair<unsigned, Optional<unsigned> > > GMap;
Ted Kremenekb22ea2a2012-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 Kremenek5d858cb2012-08-10 20:50:00 +0000326 /// Determine if the diagnostic is off by default.
327 bool isOffByDefault(const Record *Diag);
328
Ted Kremenekb22ea2a2012-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 Kremenek5d858cb2012-08-10 20:50:00 +0000359bool InferPedantic::isOffByDefault(const Record *Diag) {
Alp Toker46df1c02014-06-12 10:15:20 +0000360 const std::string &DefSeverity =
361 Diag->getValueAsDef("DefaultSeverity")->getValueAsString("Name");
362 return DefSeverity == "Ignored";
Ted Kremenek5d858cb2012-08-10 20:50:00 +0000363}
364
Ted Kremenekb22ea2a2012-07-07 05:53:30 +0000365bool InferPedantic::groupInPedantic(const Record *Group, bool increment) {
366 GMap::mapped_type &V = GroupCount[Group];
367 // Lazily compute the threshold value for the group count.
368 if (!V.second.hasValue()) {
369 const GroupInfo &GI = DiagsInGroup[Group->getValueAsString("GroupName")];
370 V.second = GI.SubGroups.size() + GI.DiagsInGroup.size();
371 }
372
373 if (increment)
374 ++V.first;
375
376 // Consider a group in -Wpendatic IFF if has at least one diagnostic
377 // or subgroup AND all of those diagnostics and subgroups are covered
378 // by -Wpedantic via our computation.
379 return V.first != 0 && V.first == V.second.getValue();
380}
381
382void InferPedantic::markGroup(const Record *Group) {
383 // If all the diagnostics and subgroups have been marked as being
384 // covered by -Wpedantic, increment the count of parent groups. Once the
385 // group's count is equal to the number of subgroups and diagnostics in
386 // that group, we can safely add this group to -Wpedantic.
387 if (groupInPedantic(Group, /* increment */ true)) {
388 const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
389 for (unsigned i = 0, e = Parents.size(); i != e; ++i)
390 markGroup(Parents[i]);
391 }
392}
393
394void InferPedantic::compute(VecOrSet DiagsInPedantic,
395 VecOrSet GroupsInPedantic) {
Ted Kremenek5d858cb2012-08-10 20:50:00 +0000396 // All extensions that are not on by default are implicitly in the
397 // "pedantic" group. For those that aren't explicitly included in -Wpedantic,
398 // mark them for consideration to be included in -Wpedantic directly.
Ted Kremenekb22ea2a2012-07-07 05:53:30 +0000399 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
400 Record *R = Diags[i];
Ted Kremenek5d858cb2012-08-10 20:50:00 +0000401 if (isExtension(R) && isOffByDefault(R)) {
Ted Kremenekb22ea2a2012-07-07 05:53:30 +0000402 DiagsSet.insert(R);
Sean Silva1c4aaa82012-10-10 20:25:43 +0000403 if (DefInit *Group = dyn_cast<DefInit>(R->getValueInit("Group"))) {
Ted Kremenekb22ea2a2012-07-07 05:53:30 +0000404 const Record *GroupRec = Group->getDef();
405 if (!isSubGroupOfGroup(GroupRec, "pedantic")) {
406 markGroup(GroupRec);
407 }
408 }
409 }
410 }
411
412 // Compute the set of diagnostics that are directly in -Wpedantic. We
413 // march through Diags a second time to ensure the results are emitted
414 // in deterministic order.
415 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
416 Record *R = Diags[i];
417 if (!DiagsSet.count(R))
418 continue;
419 // Check if the group is implicitly in -Wpedantic. If so,
420 // the diagnostic should not be directly included in the -Wpedantic
421 // diagnostic group.
Sean Silva1c4aaa82012-10-10 20:25:43 +0000422 if (DefInit *Group = dyn_cast<DefInit>(R->getValueInit("Group")))
Ted Kremenekb22ea2a2012-07-07 05:53:30 +0000423 if (groupInPedantic(Group->getDef()))
424 continue;
425
426 // The diagnostic is not included in a group that is (transitively) in
427 // -Wpedantic. Include it in -Wpedantic directly.
428 if (RecordVec *V = DiagsInPedantic.dyn_cast<RecordVec*>())
429 V->push_back(R);
430 else {
431 DiagsInPedantic.get<RecordSet*>()->insert(R);
432 }
433 }
434
435 if (!GroupsInPedantic)
436 return;
437
438 // Compute the set of groups that are directly in -Wpedantic. We
439 // march through the groups to ensure the results are emitted
440 /// in a deterministc order.
441 for (unsigned i = 0, ei = DiagGroups.size(); i != ei; ++i) {
442 Record *Group = DiagGroups[i];
443 if (!groupInPedantic(Group))
444 continue;
445
446 unsigned ParentsInPedantic = 0;
447 const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
448 for (unsigned j = 0, ej = Parents.size(); j != ej; ++j) {
449 if (groupInPedantic(Parents[j]))
450 ++ParentsInPedantic;
451 }
452 // If all the parents are in -Wpedantic, this means that this diagnostic
453 // group will be indirectly included by -Wpedantic already. In that
454 // case, do not add it directly to -Wpedantic. If the group has no
455 // parents, obviously it should go into -Wpedantic.
456 if (Parents.size() > 0 && ParentsInPedantic == Parents.size())
457 continue;
458
459 if (RecordVec *V = GroupsInPedantic.dyn_cast<RecordVec*>())
460 V->push_back(Group);
461 else {
462 GroupsInPedantic.get<RecordSet*>()->insert(Group);
463 }
464 }
465}
466
467//===----------------------------------------------------------------------===//
Peter Collingbournebee583f2011-10-06 13:03:08 +0000468// Warning Tables (.inc file) generation.
469//===----------------------------------------------------------------------===//
470
Ted Kremenek72492392012-08-07 05:01:49 +0000471static bool isError(const Record &Diag) {
472 const std::string &ClsName = Diag.getValueAsDef("Class")->getName();
473 return ClsName == "CLASS_ERROR";
474}
475
Tobias Grosser74160242014-02-28 09:11:08 +0000476static bool isRemark(const Record &Diag) {
477 const std::string &ClsName = Diag.getValueAsDef("Class")->getName();
478 return ClsName == "CLASS_REMARK";
479}
480
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +0000481/// ClangDiagsDefsEmitter - The top-level class emits .def files containing
482/// declarations of Clang diagnostics.
483namespace clang {
484void EmitClangDiagsDefs(RecordKeeper &Records, raw_ostream &OS,
485 const std::string &Component) {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000486 // Write the #if guard
487 if (!Component.empty()) {
Benjamin Kramer44f91da2011-11-06 20:36:48 +0000488 std::string ComponentName = StringRef(Component).upper();
Peter Collingbournebee583f2011-10-06 13:03:08 +0000489 OS << "#ifdef " << ComponentName << "START\n";
490 OS << "__" << ComponentName << "START = DIAG_START_" << ComponentName
491 << ",\n";
492 OS << "#undef " << ComponentName << "START\n";
493 OS << "#endif\n\n";
494 }
495
496 const std::vector<Record*> &Diags =
497 Records.getAllDerivedDefinitions("Diagnostic");
Benjamin Kramera8cafe22012-02-15 20:57:03 +0000498
Benjamin Kramera8cafe22012-02-15 20:57:03 +0000499 std::vector<Record*> DiagGroups
500 = Records.getAllDerivedDefinitions("DiagGroup");
Argyrios Kyrtzidis87acf192012-03-06 00:00:38 +0000501
502 std::map<std::string, GroupInfo> DiagsInGroup;
503 groupDiagnostics(Diags, DiagGroups, DiagsInGroup);
Benjamin Kramera8cafe22012-02-15 20:57:03 +0000504
Peter Collingbournebee583f2011-10-06 13:03:08 +0000505 DiagCategoryIDMap CategoryIDs(Records);
506 DiagGroupParentMap DGParentMap(Records);
507
Ted Kremenekb22ea2a2012-07-07 05:53:30 +0000508 // Compute the set of diagnostics that are in -Wpedantic.
509 RecordSet DiagsInPedantic;
510 InferPedantic inferPedantic(DGParentMap, Diags, DiagGroups, DiagsInGroup);
Craig Topper8ae12032014-05-07 06:21:57 +0000511 inferPedantic.compute(&DiagsInPedantic, (RecordVec*)nullptr);
Ted Kremenekb22ea2a2012-07-07 05:53:30 +0000512
Peter Collingbournebee583f2011-10-06 13:03:08 +0000513 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
514 const Record &R = *Diags[i];
Craig Topperf3932e32013-07-19 21:43:59 +0000515
Ted Kremenek72492392012-08-07 05:01:49 +0000516 // Check if this is an error that is accidentally in a warning
517 // group.
518 if (isError(R)) {
Sean Silva1c4aaa82012-10-10 20:25:43 +0000519 if (DefInit *Group = dyn_cast<DefInit>(R.getValueInit("Group"))) {
Ted Kremenek72492392012-08-07 05:01:49 +0000520 const Record *GroupRec = Group->getDef();
521 const std::string &GroupName = GroupRec->getValueAsString("GroupName");
Joerg Sonnenberger691a16b2012-10-25 16:37:08 +0000522 PrintFatalError(R.getLoc(), "Error " + R.getName() +
523 " cannot be in a warning group [" + GroupName + "]");
Ted Kremenek72492392012-08-07 05:01:49 +0000524 }
525 }
526
Tobias Grosser74160242014-02-28 09:11:08 +0000527 // Check that all remarks have an associated diagnostic group.
528 if (isRemark(R)) {
529 if (!isa<DefInit>(R.getValueInit("Group"))) {
530 PrintFatalError(R.getLoc(), "Error " + R.getName() +
531 " not in any diagnostic group");
532 }
533 }
534
Peter Collingbournebee583f2011-10-06 13:03:08 +0000535 // Filter by component.
536 if (!Component.empty() && Component != R.getValueAsString("Component"))
537 continue;
538
539 OS << "DIAG(" << R.getName() << ", ";
540 OS << R.getValueAsDef("Class")->getName();
Alp Toker46df1c02014-06-12 10:15:20 +0000541 OS << ", (unsigned)diag::Severity::"
542 << R.getValueAsDef("DefaultSeverity")->getValueAsString("Name");
Craig Topperf3932e32013-07-19 21:43:59 +0000543
Peter Collingbournebee583f2011-10-06 13:03:08 +0000544 // Description string.
545 OS << ", \"";
546 OS.write_escaped(R.getValueAsString("Text")) << '"';
Craig Topperf3932e32013-07-19 21:43:59 +0000547
Benjamin Kramera8cafe22012-02-15 20:57:03 +0000548 // Warning associated with the diagnostic. This is stored as an index into
549 // the alphabetically sorted warning table.
Sean Silva1c4aaa82012-10-10 20:25:43 +0000550 if (DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group"))) {
Argyrios Kyrtzidis87acf192012-03-06 00:00:38 +0000551 std::map<std::string, GroupInfo>::iterator I =
552 DiagsInGroup.find(DI->getDef()->getValueAsString("GroupName"));
553 assert(I != DiagsInGroup.end());
554 OS << ", " << I->second.IDNo;
Ted Kremenekb22ea2a2012-07-07 05:53:30 +0000555 } else if (DiagsInPedantic.count(&R)) {
556 std::map<std::string, GroupInfo>::iterator I =
557 DiagsInGroup.find("pedantic");
558 assert(I != DiagsInGroup.end() && "pedantic group not defined");
559 OS << ", " << I->second.IDNo;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000560 } else {
Benjamin Kramera8cafe22012-02-15 20:57:03 +0000561 OS << ", 0";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000562 }
563
Richard Smith16e1b072013-11-12 02:41:45 +0000564 // SFINAE response.
565 OS << ", " << R.getValueAsDef("SFINAE")->getName();
566
567 // Default warning has no Werror bit.
568 if (R.getValueAsBit("WarningNoWerror"))
Peter Collingbournebee583f2011-10-06 13:03:08 +0000569 OS << ", true";
570 else
571 OS << ", false";
572
Alp Toker04278ec2014-06-16 13:56:47 +0000573 if (R.getValueAsBit("ShowInSystemHeader"))
Peter Collingbournebee583f2011-10-06 13:03:08 +0000574 OS << ", true";
575 else
576 OS << ", false";
577
Peter Collingbournebee583f2011-10-06 13:03:08 +0000578 // Category number.
579 OS << ", " << CategoryIDs.getID(getDiagnosticCategory(&R, DGParentMap));
Peter Collingbournebee583f2011-10-06 13:03:08 +0000580 OS << ")\n";
581 }
582}
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +0000583} // end namespace clang
Peter Collingbournebee583f2011-10-06 13:03:08 +0000584
585//===----------------------------------------------------------------------===//
586// Warning Group Tables generation
587//===----------------------------------------------------------------------===//
588
589static std::string getDiagCategoryEnum(llvm::StringRef name) {
590 if (name.empty())
591 return "DiagCat_None";
Dylan Noblesmithf1a13f22012-02-13 12:32:26 +0000592 SmallString<256> enumName = llvm::StringRef("DiagCat_");
Peter Collingbournebee583f2011-10-06 13:03:08 +0000593 for (llvm::StringRef::iterator I = name.begin(), E = name.end(); I != E; ++I)
594 enumName += isalnum(*I) ? *I : '_';
595 return enumName.str();
596}
Craig Topperf3932e32013-07-19 21:43:59 +0000597
Tobias Grossercfc57bb2014-05-06 22:06:56 +0000598/// \brief Emit the array of diagnostic subgroups.
599///
600/// The array of diagnostic subgroups contains for each group a list of its
601/// subgroups. The individual lists are separated by '-1'. Groups with no
602/// subgroups are skipped.
603///
604/// \code
605/// static const int16_t DiagSubGroups[] = {
606/// /* Empty */ -1,
607/// /* DiagSubGroup0 */ 142, -1,
608/// /* DiagSubGroup13 */ 265, 322, 399, -1
609/// }
610/// \endcode
611///
612static void emitDiagSubGroups(std::map<std::string, GroupInfo> &DiagsInGroup,
613 RecordVec &GroupsInPedantic, raw_ostream &OS) {
614 OS << "static const int16_t DiagSubGroups[] = {\n"
615 << " /* Empty */ -1,\n";
616 for (auto const &I : DiagsInGroup) {
617 const bool IsPedantic = I.first == "pedantic";
618
619 const std::vector<std::string> &SubGroups = I.second.SubGroups;
620 if (!SubGroups.empty() || (IsPedantic && !GroupsInPedantic.empty())) {
621 OS << " /* DiagSubGroup" << I.second.IDNo << " */ ";
622 for (auto const &SubGroup : SubGroups) {
623 std::map<std::string, GroupInfo>::const_iterator RI =
624 DiagsInGroup.find(SubGroup);
625 assert(RI != DiagsInGroup.end() && "Referenced without existing?");
626 OS << RI->second.IDNo << ", ";
627 }
628 // Emit the groups implicitly in "pedantic".
629 if (IsPedantic) {
630 for (auto const &Group : GroupsInPedantic) {
631 const std::string &GroupName = Group->getValueAsString("GroupName");
632 std::map<std::string, GroupInfo>::const_iterator RI =
633 DiagsInGroup.find(GroupName);
634 assert(RI != DiagsInGroup.end() && "Referenced without existing?");
635 OS << RI->second.IDNo << ", ";
636 }
637 }
638
639 OS << "-1,\n";
640 }
641 }
642 OS << "};\n\n";
643}
644
645/// \brief Emit the list of diagnostic arrays.
646///
647/// This data structure is a large array that contains itself arrays of varying
648/// size. Each array represents a list of diagnostics. The different arrays are
649/// separated by the value '-1'.
650///
651/// \code
652/// static const int16_t DiagArrays[] = {
653/// /* Empty */ -1,
654/// /* DiagArray1 */ diag::warn_pragma_message,
655/// -1,
656/// /* DiagArray2 */ diag::warn_abs_too_small,
657/// diag::warn_unsigned_abs,
658/// diag::warn_wrong_absolute_value_type,
659/// -1
660/// };
661/// \endcode
662///
663static void emitDiagArrays(std::map<std::string, GroupInfo> &DiagsInGroup,
664 RecordVec &DiagsInPedantic, raw_ostream &OS) {
665 OS << "static const int16_t DiagArrays[] = {\n"
666 << " /* Empty */ -1,\n";
667 for (auto const &I : DiagsInGroup) {
668 const bool IsPedantic = I.first == "pedantic";
669
670 const std::vector<const Record *> &V = I.second.DiagsInGroup;
671 if (!V.empty() || (IsPedantic && !DiagsInPedantic.empty())) {
672 OS << " /* DiagArray" << I.second.IDNo << " */ ";
673 for (auto *Record : V)
674 OS << "diag::" << Record->getName() << ", ";
675 // Emit the diagnostics implicitly in "pedantic".
676 if (IsPedantic) {
677 for (auto const &Diag : DiagsInPedantic)
678 OS << "diag::" << Diag->getName() << ", ";
679 }
680 OS << "-1,\n";
681 }
682 }
683 OS << "};\n\n";
684}
685
686/// \brief Emit a list of group names.
687///
688/// This creates a long string which by itself contains a list of pascal style
689/// strings, which consist of a length byte directly followed by the string.
690///
691/// \code
692/// static const char DiagGroupNames[] = {
693/// \000\020#pragma-messages\t#warnings\020CFString-literal"
694/// };
695/// \endcode
696static void emitDiagGroupNames(StringToOffsetTable &GroupNames,
697 raw_ostream &OS) {
698 OS << "static const char DiagGroupNames[] = {\n";
699 GroupNames.EmitString(OS);
700 OS << "};\n\n";
701}
702
703/// \brief Emit diagnostic arrays and related data structures.
704///
705/// This creates the actual diagnostic array, an array of diagnostic subgroups
706/// and an array of subgroup names.
707///
708/// \code
709/// #ifdef GET_DIAG_ARRAYS
710/// static const int16_t DiagArrays[];
711/// static const int16_t DiagSubGroups[];
712/// static const char DiagGroupNames[];
713/// #endif
714/// \endcode
715static void emitAllDiagArrays(std::map<std::string, GroupInfo> &DiagsInGroup,
716 RecordVec &DiagsInPedantic,
717 RecordVec &GroupsInPedantic,
718 StringToOffsetTable &GroupNames,
719 raw_ostream &OS) {
720 OS << "\n#ifdef GET_DIAG_ARRAYS\n";
721 emitDiagArrays(DiagsInGroup, DiagsInPedantic, OS);
722 emitDiagSubGroups(DiagsInGroup, GroupsInPedantic, OS);
723 emitDiagGroupNames(GroupNames, OS);
724 OS << "#endif // GET_DIAG_ARRAYS\n\n";
725}
726
727/// \brief Emit diagnostic table.
728///
729/// The table is sorted by the name of the diagnostic group. Each element
730/// consists of the name of the diagnostic group (given as offset in the
731/// group name table), a reference to a list of diagnostics (optional) and a
732/// reference to a set of subgroups (optional).
733///
734/// \code
735/// #ifdef GET_DIAG_TABLE
736/// {/* abi */ 159, /* DiagArray11 */ 19, /* Empty */ 0},
737/// {/* aggregate-return */ 180, /* Empty */ 0, /* Empty */ 0},
738/// {/* all */ 197, /* Empty */ 0, /* DiagSubGroup13 */ 3},
739/// {/* deprecated */ 1981,/* DiagArray1 */ 348, /* DiagSubGroup3 */ 9},
740/// #endif
741/// \endcode
742static void emitDiagTable(std::map<std::string, GroupInfo> &DiagsInGroup,
743 RecordVec &DiagsInPedantic,
744 RecordVec &GroupsInPedantic,
745 StringToOffsetTable &GroupNames, raw_ostream &OS) {
746 unsigned MaxLen = 0;
747
748 for (auto const &I: DiagsInGroup)
749 MaxLen = std::max(MaxLen, (unsigned)I.first.size());
750
751 OS << "\n#ifdef GET_DIAG_TABLE\n";
752 unsigned SubGroupIndex = 1, DiagArrayIndex = 1;
753 for (auto const &I: DiagsInGroup) {
754 // Group option string.
755 OS << " { /* ";
756 if (I.first.find_first_not_of("abcdefghijklmnopqrstuvwxyz"
757 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
758 "0123456789!@#$%^*-+=:?") !=
759 std::string::npos)
760 PrintFatalError("Invalid character in diagnostic group '" + I.first +
761 "'");
762 OS << I.first << " */ " << std::string(MaxLen - I.first.size(), ' ');
763 // Store a pascal-style length byte at the beginning of the string.
764 std::string Name = char(I.first.size()) + I.first;
765 OS << GroupNames.GetOrAddStringOffset(Name, false) << ", ";
766
767 // Special handling for 'pedantic'.
768 const bool IsPedantic = I.first == "pedantic";
769
770 // Diagnostics in the group.
771 const std::vector<const Record *> &V = I.second.DiagsInGroup;
772 const bool hasDiags =
773 !V.empty() || (IsPedantic && !DiagsInPedantic.empty());
774 if (hasDiags) {
775 OS << "/* DiagArray" << I.second.IDNo << " */ " << DiagArrayIndex
776 << ", ";
777 if (IsPedantic)
778 DiagArrayIndex += DiagsInPedantic.size();
779 DiagArrayIndex += V.size() + 1;
780 } else {
781 OS << "/* Empty */ 0, ";
782 }
783
784 // Subgroups.
785 const std::vector<std::string> &SubGroups = I.second.SubGroups;
786 const bool hasSubGroups =
787 !SubGroups.empty() || (IsPedantic && !GroupsInPedantic.empty());
788 if (hasSubGroups) {
789 OS << "/* DiagSubGroup" << I.second.IDNo << " */ " << SubGroupIndex;
790 if (IsPedantic)
791 SubGroupIndex += GroupsInPedantic.size();
792 SubGroupIndex += SubGroups.size() + 1;
793 } else {
794 OS << "/* Empty */ 0";
795 }
796
797 OS << " },\n";
798 }
799 OS << "#endif // GET_DIAG_TABLE\n\n";
800}
801
802/// \brief Emit the table of diagnostic categories.
803///
804/// The table has the form of macro calls that have two parameters. The
805/// category's name as well as an enum that represents the category. The
806/// table can be used by defining the macro 'CATEGORY' and including this
807/// table right after.
808///
809/// \code
810/// #ifdef GET_CATEGORY_TABLE
811/// CATEGORY("Semantic Issue", DiagCat_Semantic_Issue)
812/// CATEGORY("Lambda Issue", DiagCat_Lambda_Issue)
813/// #endif
814/// \endcode
815static void emitCategoryTable(RecordKeeper &Records, raw_ostream &OS) {
816 DiagCategoryIDMap CategoriesByID(Records);
817 OS << "\n#ifdef GET_CATEGORY_TABLE\n";
818 for (auto const &C : CategoriesByID)
819 OS << "CATEGORY(\"" << C << "\", " << getDiagCategoryEnum(C) << ")\n";
820 OS << "#endif // GET_CATEGORY_TABLE\n\n";
821}
822
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +0000823namespace clang {
824void EmitClangDiagGroups(RecordKeeper &Records, raw_ostream &OS) {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000825 // Compute a mapping from a DiagGroup to all of its parents.
826 DiagGroupParentMap DGParentMap(Records);
Ted Kremenekb22ea2a2012-07-07 05:53:30 +0000827
Tobias Grossercfc57bb2014-05-06 22:06:56 +0000828 std::vector<Record *> Diags = Records.getAllDerivedDefinitions("Diagnostic");
Craig Topperf3932e32013-07-19 21:43:59 +0000829
Tobias Grossercfc57bb2014-05-06 22:06:56 +0000830 std::vector<Record *> DiagGroups =
831 Records.getAllDerivedDefinitions("DiagGroup");
Argyrios Kyrtzidis87acf192012-03-06 00:00:38 +0000832
833 std::map<std::string, GroupInfo> DiagsInGroup;
834 groupDiagnostics(Diags, DiagGroups, DiagsInGroup);
Ted Kremenekb22ea2a2012-07-07 05:53:30 +0000835
836 // All extensions are implicitly in the "pedantic" group. Record the
837 // implicit set of groups in the "pedantic" group, and use this information
838 // later when emitting the group information for Pedantic.
839 RecordVec DiagsInPedantic;
840 RecordVec GroupsInPedantic;
841 InferPedantic inferPedantic(DGParentMap, Diags, DiagGroups, DiagsInGroup);
842 inferPedantic.compute(&DiagsInPedantic, &GroupsInPedantic);
843
Craig Topperda7cf8a2013-08-29 05:18:04 +0000844 StringToOffsetTable GroupNames;
845 for (std::map<std::string, GroupInfo>::const_iterator
Tobias Grossercfc57bb2014-05-06 22:06:56 +0000846 I = DiagsInGroup.begin(),
847 E = DiagsInGroup.end();
848 I != E; ++I) {
Craig Topperda7cf8a2013-08-29 05:18:04 +0000849 // Store a pascal-style length byte at the beginning of the string.
850 std::string Name = char(I->first.size()) + I->first;
851 GroupNames.GetOrAddStringOffset(Name, false);
852 }
853
Tobias Grossercfc57bb2014-05-06 22:06:56 +0000854 emitAllDiagArrays(DiagsInGroup, DiagsInPedantic, GroupsInPedantic, GroupNames,
855 OS);
856 emitDiagTable(DiagsInGroup, DiagsInPedantic, GroupsInPedantic, GroupNames,
857 OS);
858 emitCategoryTable(Records, OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +0000859}
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +0000860} // end namespace clang
Peter Collingbournebee583f2011-10-06 13:03:08 +0000861
862//===----------------------------------------------------------------------===//
863// Diagnostic name index generation
864//===----------------------------------------------------------------------===//
865
866namespace {
867struct RecordIndexElement
868{
869 RecordIndexElement() {}
870 explicit RecordIndexElement(Record const &R):
871 Name(R.getName()) {}
Craig Topperf3932e32013-07-19 21:43:59 +0000872
Peter Collingbournebee583f2011-10-06 13:03:08 +0000873 std::string Name;
874};
Peter Collingbournebee583f2011-10-06 13:03:08 +0000875} // end anonymous namespace.
876
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +0000877namespace clang {
878void EmitClangDiagsIndexName(RecordKeeper &Records, raw_ostream &OS) {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000879 const std::vector<Record*> &Diags =
880 Records.getAllDerivedDefinitions("Diagnostic");
Craig Topperf3932e32013-07-19 21:43:59 +0000881
Peter Collingbournebee583f2011-10-06 13:03:08 +0000882 std::vector<RecordIndexElement> Index;
883 Index.reserve(Diags.size());
884 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
Craig Topperf3932e32013-07-19 21:43:59 +0000885 const Record &R = *(Diags[i]);
Peter Collingbournebee583f2011-10-06 13:03:08 +0000886 Index.push_back(RecordIndexElement(R));
887 }
Craig Topperf3932e32013-07-19 21:43:59 +0000888
Benjamin Kramerbbdd7642014-03-01 14:48:57 +0000889 std::sort(Index.begin(), Index.end(),
890 [](const RecordIndexElement &Lhs,
891 const RecordIndexElement &Rhs) { return Lhs.Name < Rhs.Name; });
Craig Topperf3932e32013-07-19 21:43:59 +0000892
Peter Collingbournebee583f2011-10-06 13:03:08 +0000893 for (unsigned i = 0, e = Index.size(); i != e; ++i) {
894 const RecordIndexElement &R = Index[i];
Craig Topperf3932e32013-07-19 21:43:59 +0000895
Peter Collingbournebee583f2011-10-06 13:03:08 +0000896 OS << "DIAG_NAME_INDEX(" << R.Name << ")\n";
897 }
898}
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +0000899} // end namespace clang