blob: 76b484e7837b38732d00a695581e23b62906ee3e [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) {
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 Kyrtzidis87acf192012-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 Rosec3b23aa2013-01-10 18:50:46 +0000169
Argyrios Kyrtzidis87acf192012-03-06 00:00:38 +0000170 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
171 const Record *R = Diags[i];
Sean Silva1c4aaa82012-10-10 20:25:43 +0000172 DefInit *DI = dyn_cast<DefInit>(R->getValueInit("Group"));
Craig Topper8ae12032014-05-07 06:21:57 +0000173 if (!DI)
174 continue;
Richard Smithce52ca12012-05-04 19:05:50 +0000175 assert(R->getValueAsDef("Class")->getName() != "CLASS_NOTE" &&
176 "Note can't be in a DiagGroup");
Argyrios Kyrtzidis87acf192012-03-06 00:00:38 +0000177 std::string GroupName = DI->getDef()->getValueAsString("GroupName");
178 DiagsInGroup[GroupName].DiagsInGroup.push_back(R);
179 }
Jordan Rosec3b23aa2013-01-10 18:50:46 +0000180
181 typedef SmallPtrSet<GroupInfo *, 16> GroupSetTy;
182 GroupSetTy ImplicitGroups;
183
Argyrios Kyrtzidis87acf192012-03-06 00:00:38 +0000184 // Add all DiagGroup's to the DiagsInGroup list to make sure we pick up empty
185 // groups (these are warnings that GCC supports that clang never produces).
186 for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
187 Record *Group = DiagGroups[i];
188 GroupInfo &GI = DiagsInGroup[Group->getValueAsString("GroupName")];
Jordan Rosec3b23aa2013-01-10 18:50:46 +0000189 if (Group->isAnonymous()) {
190 if (GI.DiagsInGroup.size() > 1)
191 ImplicitGroups.insert(&GI);
192 } else {
193 if (GI.ExplicitDef)
194 assert(GI.ExplicitDef == Group);
195 else
196 GI.ExplicitDef = Group;
197 }
198
Argyrios Kyrtzidis87acf192012-03-06 00:00:38 +0000199 std::vector<Record*> SubGroups = Group->getValueAsListOfDefs("SubGroups");
200 for (unsigned j = 0, e = SubGroups.size(); j != e; ++j)
201 GI.SubGroups.push_back(SubGroups[j]->getValueAsString("GroupName"));
202 }
Craig Topperf3932e32013-07-19 21:43:59 +0000203
Argyrios Kyrtzidis87acf192012-03-06 00:00:38 +0000204 // Assign unique ID numbers to the groups.
205 unsigned IDNo = 0;
206 for (std::map<std::string, GroupInfo>::iterator
207 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I, ++IDNo)
208 I->second.IDNo = IDNo;
Jordan Rosec3b23aa2013-01-10 18:50:46 +0000209
210 // Sort the implicit groups, so we can warn about them deterministically.
211 SmallVector<GroupInfo *, 16> SortedGroups(ImplicitGroups.begin(),
212 ImplicitGroups.end());
213 for (SmallVectorImpl<GroupInfo *>::iterator I = SortedGroups.begin(),
214 E = SortedGroups.end();
215 I != E; ++I) {
216 MutableArrayRef<const Record *> GroupDiags = (*I)->DiagsInGroup;
217 std::sort(GroupDiags.begin(), GroupDiags.end(), beforeThanCompare);
218 }
219 std::sort(SortedGroups.begin(), SortedGroups.end(), beforeThanCompareGroups);
220
221 // Warn about the same group being used anonymously in multiple places.
222 for (SmallVectorImpl<GroupInfo *>::const_iterator I = SortedGroups.begin(),
223 E = SortedGroups.end();
224 I != E; ++I) {
225 ArrayRef<const Record *> GroupDiags = (*I)->DiagsInGroup;
226
227 if ((*I)->ExplicitDef) {
228 std::string Name = (*I)->ExplicitDef->getValueAsString("GroupName");
229 for (ArrayRef<const Record *>::const_iterator DI = GroupDiags.begin(),
230 DE = GroupDiags.end();
231 DI != DE; ++DI) {
232 const DefInit *GroupInit = cast<DefInit>((*DI)->getValueInit("Group"));
233 const Record *NextDiagGroup = GroupInit->getDef();
234 if (NextDiagGroup == (*I)->ExplicitDef)
235 continue;
236
237 SMRange InGroupRange = findSuperClassRange(*DI, "InGroup");
238 SmallString<64> Replacement;
239 if (InGroupRange.isValid()) {
240 Replacement += "InGroup<";
241 Replacement += (*I)->ExplicitDef->getName();
242 Replacement += ">";
243 }
244 SMFixIt FixIt(InGroupRange, Replacement.str());
245
246 SrcMgr.PrintMessage(NextDiagGroup->getLoc().front(),
247 SourceMgr::DK_Error,
248 Twine("group '") + Name +
249 "' is referred to anonymously",
Dmitri Gribenko010316c2013-05-05 01:03:47 +0000250 None,
Jordan Rosec3b23aa2013-01-10 18:50:46 +0000251 InGroupRange.isValid() ? FixIt
252 : ArrayRef<SMFixIt>());
253 SrcMgr.PrintMessage((*I)->ExplicitDef->getLoc().front(),
254 SourceMgr::DK_Note, "group defined here");
255 }
256 } else {
257 // If there's no existing named group, we should just warn once and use
258 // notes to list all the other cases.
259 ArrayRef<const Record *>::const_iterator DI = GroupDiags.begin(),
260 DE = GroupDiags.end();
261 assert(DI != DE && "We only care about groups with multiple uses!");
262
263 const DefInit *GroupInit = cast<DefInit>((*DI)->getValueInit("Group"));
264 const Record *NextDiagGroup = GroupInit->getDef();
265 std::string Name = NextDiagGroup->getValueAsString("GroupName");
266
267 SMRange InGroupRange = findSuperClassRange(*DI, "InGroup");
268 SrcMgr.PrintMessage(NextDiagGroup->getLoc().front(),
269 SourceMgr::DK_Error,
270 Twine("group '") + Name +
271 "' is referred to anonymously",
272 InGroupRange);
273
274 for (++DI; DI != DE; ++DI) {
275 GroupInit = cast<DefInit>((*DI)->getValueInit("Group"));
276 InGroupRange = findSuperClassRange(*DI, "InGroup");
277 SrcMgr.PrintMessage(GroupInit->getDef()->getLoc().front(),
278 SourceMgr::DK_Note, "also referenced here",
279 InGroupRange);
280 }
281 }
282 }
Argyrios Kyrtzidis87acf192012-03-06 00:00:38 +0000283}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000284
285//===----------------------------------------------------------------------===//
Ted Kremenekb22ea2a2012-07-07 05:53:30 +0000286// Infer members of -Wpedantic.
287//===----------------------------------------------------------------------===//
288
289typedef std::vector<const Record *> RecordVec;
290typedef llvm::DenseSet<const Record *> RecordSet;
291typedef llvm::PointerUnion<RecordVec*, RecordSet*> VecOrSet;
292
293namespace {
294class InferPedantic {
295 typedef llvm::DenseMap<const Record*,
Ted Kremenek03325582013-02-21 01:29:01 +0000296 std::pair<unsigned, Optional<unsigned> > > GMap;
Ted Kremenekb22ea2a2012-07-07 05:53:30 +0000297
298 DiagGroupParentMap &DiagGroupParents;
299 const std::vector<Record*> &Diags;
300 const std::vector<Record*> DiagGroups;
301 std::map<std::string, GroupInfo> &DiagsInGroup;
302 llvm::DenseSet<const Record*> DiagsSet;
303 GMap GroupCount;
304public:
305 InferPedantic(DiagGroupParentMap &DiagGroupParents,
306 const std::vector<Record*> &Diags,
307 const std::vector<Record*> &DiagGroups,
308 std::map<std::string, GroupInfo> &DiagsInGroup)
309 : DiagGroupParents(DiagGroupParents),
310 Diags(Diags),
311 DiagGroups(DiagGroups),
312 DiagsInGroup(DiagsInGroup) {}
313
314 /// Compute the set of diagnostics and groups that are immediately
315 /// in -Wpedantic.
316 void compute(VecOrSet DiagsInPedantic,
317 VecOrSet GroupsInPedantic);
318
319private:
320 /// Determine whether a group is a subgroup of another group.
321 bool isSubGroupOfGroup(const Record *Group,
322 llvm::StringRef RootGroupName);
323
324 /// Determine if the diagnostic is an extension.
325 bool isExtension(const Record *Diag);
326
Ted Kremenek5d858cb2012-08-10 20:50:00 +0000327 /// Determine if the diagnostic is off by default.
328 bool isOffByDefault(const Record *Diag);
329
Ted Kremenekb22ea2a2012-07-07 05:53:30 +0000330 /// Increment the count for a group, and transitively marked
331 /// parent groups when appropriate.
332 void markGroup(const Record *Group);
333
334 /// Return true if the diagnostic is in a pedantic group.
335 bool groupInPedantic(const Record *Group, bool increment = false);
336};
337} // end anonymous namespace
338
339bool InferPedantic::isSubGroupOfGroup(const Record *Group,
340 llvm::StringRef GName) {
341
342 const std::string &GroupName = Group->getValueAsString("GroupName");
343 if (GName == GroupName)
344 return true;
345
346 const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
347 for (unsigned i = 0, e = Parents.size(); i != e; ++i)
348 if (isSubGroupOfGroup(Parents[i], GName))
349 return true;
350
351 return false;
352}
353
354/// Determine if the diagnostic is an extension.
355bool InferPedantic::isExtension(const Record *Diag) {
356 const std::string &ClsName = Diag->getValueAsDef("Class")->getName();
357 return ClsName == "CLASS_EXTENSION";
358}
359
Ted Kremenek5d858cb2012-08-10 20:50:00 +0000360bool InferPedantic::isOffByDefault(const Record *Diag) {
Alp Toker46df1c02014-06-12 10:15:20 +0000361 const std::string &DefSeverity =
362 Diag->getValueAsDef("DefaultSeverity")->getValueAsString("Name");
363 return DefSeverity == "Ignored";
Ted Kremenek5d858cb2012-08-10 20:50:00 +0000364}
365
Ted Kremenekb22ea2a2012-07-07 05:53:30 +0000366bool InferPedantic::groupInPedantic(const Record *Group, bool increment) {
367 GMap::mapped_type &V = GroupCount[Group];
368 // Lazily compute the threshold value for the group count.
369 if (!V.second.hasValue()) {
370 const GroupInfo &GI = DiagsInGroup[Group->getValueAsString("GroupName")];
371 V.second = GI.SubGroups.size() + GI.DiagsInGroup.size();
372 }
373
374 if (increment)
375 ++V.first;
376
377 // Consider a group in -Wpendatic IFF if has at least one diagnostic
378 // or subgroup AND all of those diagnostics and subgroups are covered
379 // by -Wpedantic via our computation.
380 return V.first != 0 && V.first == V.second.getValue();
381}
382
383void InferPedantic::markGroup(const Record *Group) {
384 // If all the diagnostics and subgroups have been marked as being
385 // covered by -Wpedantic, increment the count of parent groups. Once the
386 // group's count is equal to the number of subgroups and diagnostics in
387 // that group, we can safely add this group to -Wpedantic.
388 if (groupInPedantic(Group, /* increment */ true)) {
389 const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
390 for (unsigned i = 0, e = Parents.size(); i != e; ++i)
391 markGroup(Parents[i]);
392 }
393}
394
395void InferPedantic::compute(VecOrSet DiagsInPedantic,
396 VecOrSet GroupsInPedantic) {
Ted Kremenek5d858cb2012-08-10 20:50:00 +0000397 // All extensions that are not on by default are implicitly in the
398 // "pedantic" group. For those that aren't explicitly included in -Wpedantic,
399 // mark them for consideration to be included in -Wpedantic directly.
Ted Kremenekb22ea2a2012-07-07 05:53:30 +0000400 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
401 Record *R = Diags[i];
Ted Kremenek5d858cb2012-08-10 20:50:00 +0000402 if (isExtension(R) && isOffByDefault(R)) {
Ted Kremenekb22ea2a2012-07-07 05:53:30 +0000403 DiagsSet.insert(R);
Sean Silva1c4aaa82012-10-10 20:25:43 +0000404 if (DefInit *Group = dyn_cast<DefInit>(R->getValueInit("Group"))) {
Ted Kremenekb22ea2a2012-07-07 05:53:30 +0000405 const Record *GroupRec = Group->getDef();
406 if (!isSubGroupOfGroup(GroupRec, "pedantic")) {
407 markGroup(GroupRec);
408 }
409 }
410 }
411 }
412
413 // Compute the set of diagnostics that are directly in -Wpedantic. We
414 // march through Diags a second time to ensure the results are emitted
415 // in deterministic order.
416 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
417 Record *R = Diags[i];
418 if (!DiagsSet.count(R))
419 continue;
420 // Check if the group is implicitly in -Wpedantic. If so,
421 // the diagnostic should not be directly included in the -Wpedantic
422 // diagnostic group.
Sean Silva1c4aaa82012-10-10 20:25:43 +0000423 if (DefInit *Group = dyn_cast<DefInit>(R->getValueInit("Group")))
Ted Kremenekb22ea2a2012-07-07 05:53:30 +0000424 if (groupInPedantic(Group->getDef()))
425 continue;
426
427 // The diagnostic is not included in a group that is (transitively) in
428 // -Wpedantic. Include it in -Wpedantic directly.
429 if (RecordVec *V = DiagsInPedantic.dyn_cast<RecordVec*>())
430 V->push_back(R);
431 else {
432 DiagsInPedantic.get<RecordSet*>()->insert(R);
433 }
434 }
435
436 if (!GroupsInPedantic)
437 return;
438
439 // Compute the set of groups that are directly in -Wpedantic. We
440 // march through the groups to ensure the results are emitted
441 /// in a deterministc order.
442 for (unsigned i = 0, ei = DiagGroups.size(); i != ei; ++i) {
443 Record *Group = DiagGroups[i];
444 if (!groupInPedantic(Group))
445 continue;
446
447 unsigned ParentsInPedantic = 0;
448 const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
449 for (unsigned j = 0, ej = Parents.size(); j != ej; ++j) {
450 if (groupInPedantic(Parents[j]))
451 ++ParentsInPedantic;
452 }
453 // If all the parents are in -Wpedantic, this means that this diagnostic
454 // group will be indirectly included by -Wpedantic already. In that
455 // case, do not add it directly to -Wpedantic. If the group has no
456 // parents, obviously it should go into -Wpedantic.
457 if (Parents.size() > 0 && ParentsInPedantic == Parents.size())
458 continue;
459
460 if (RecordVec *V = GroupsInPedantic.dyn_cast<RecordVec*>())
461 V->push_back(Group);
462 else {
463 GroupsInPedantic.get<RecordSet*>()->insert(Group);
464 }
465 }
466}
467
468//===----------------------------------------------------------------------===//
Peter Collingbournebee583f2011-10-06 13:03:08 +0000469// Warning Tables (.inc file) generation.
470//===----------------------------------------------------------------------===//
471
Ted Kremenek72492392012-08-07 05:01:49 +0000472static bool isError(const Record &Diag) {
473 const std::string &ClsName = Diag.getValueAsDef("Class")->getName();
474 return ClsName == "CLASS_ERROR";
475}
476
Tobias Grosser74160242014-02-28 09:11:08 +0000477static bool isRemark(const Record &Diag) {
478 const std::string &ClsName = Diag.getValueAsDef("Class")->getName();
479 return ClsName == "CLASS_REMARK";
480}
481
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +0000482/// ClangDiagsDefsEmitter - The top-level class emits .def files containing
483/// declarations of Clang diagnostics.
484namespace clang {
485void EmitClangDiagsDefs(RecordKeeper &Records, raw_ostream &OS,
486 const std::string &Component) {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000487 // Write the #if guard
488 if (!Component.empty()) {
Benjamin Kramer44f91da2011-11-06 20:36:48 +0000489 std::string ComponentName = StringRef(Component).upper();
Peter Collingbournebee583f2011-10-06 13:03:08 +0000490 OS << "#ifdef " << ComponentName << "START\n";
491 OS << "__" << ComponentName << "START = DIAG_START_" << ComponentName
492 << ",\n";
493 OS << "#undef " << ComponentName << "START\n";
494 OS << "#endif\n\n";
495 }
496
497 const std::vector<Record*> &Diags =
498 Records.getAllDerivedDefinitions("Diagnostic");
Benjamin Kramera8cafe22012-02-15 20:57:03 +0000499
Benjamin Kramera8cafe22012-02-15 20:57:03 +0000500 std::vector<Record*> DiagGroups
501 = Records.getAllDerivedDefinitions("DiagGroup");
Argyrios Kyrtzidis87acf192012-03-06 00:00:38 +0000502
503 std::map<std::string, GroupInfo> DiagsInGroup;
504 groupDiagnostics(Diags, DiagGroups, DiagsInGroup);
Benjamin Kramera8cafe22012-02-15 20:57:03 +0000505
Peter Collingbournebee583f2011-10-06 13:03:08 +0000506 DiagCategoryIDMap CategoryIDs(Records);
507 DiagGroupParentMap DGParentMap(Records);
508
Ted Kremenekb22ea2a2012-07-07 05:53:30 +0000509 // Compute the set of diagnostics that are in -Wpedantic.
510 RecordSet DiagsInPedantic;
511 InferPedantic inferPedantic(DGParentMap, Diags, DiagGroups, DiagsInGroup);
Craig Topper8ae12032014-05-07 06:21:57 +0000512 inferPedantic.compute(&DiagsInPedantic, (RecordVec*)nullptr);
Ted Kremenekb22ea2a2012-07-07 05:53:30 +0000513
Peter Collingbournebee583f2011-10-06 13:03:08 +0000514 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
515 const Record &R = *Diags[i];
Craig Topperf3932e32013-07-19 21:43:59 +0000516
Ted Kremenek72492392012-08-07 05:01:49 +0000517 // Check if this is an error that is accidentally in a warning
518 // group.
519 if (isError(R)) {
Sean Silva1c4aaa82012-10-10 20:25:43 +0000520 if (DefInit *Group = dyn_cast<DefInit>(R.getValueInit("Group"))) {
Ted Kremenek72492392012-08-07 05:01:49 +0000521 const Record *GroupRec = Group->getDef();
522 const std::string &GroupName = GroupRec->getValueAsString("GroupName");
Joerg Sonnenberger691a16b2012-10-25 16:37:08 +0000523 PrintFatalError(R.getLoc(), "Error " + R.getName() +
524 " cannot be in a warning group [" + GroupName + "]");
Ted Kremenek72492392012-08-07 05:01:49 +0000525 }
526 }
527
Tobias Grosser74160242014-02-28 09:11:08 +0000528 // Check that all remarks have an associated diagnostic group.
529 if (isRemark(R)) {
530 if (!isa<DefInit>(R.getValueInit("Group"))) {
531 PrintFatalError(R.getLoc(), "Error " + R.getName() +
532 " not in any diagnostic group");
533 }
534 }
535
Peter Collingbournebee583f2011-10-06 13:03:08 +0000536 // Filter by component.
537 if (!Component.empty() && Component != R.getValueAsString("Component"))
538 continue;
539
540 OS << "DIAG(" << R.getName() << ", ";
541 OS << R.getValueAsDef("Class")->getName();
Alp Toker46df1c02014-06-12 10:15:20 +0000542 OS << ", (unsigned)diag::Severity::"
543 << R.getValueAsDef("DefaultSeverity")->getValueAsString("Name");
Craig Topperf3932e32013-07-19 21:43:59 +0000544
Peter Collingbournebee583f2011-10-06 13:03:08 +0000545 // Description string.
546 OS << ", \"";
547 OS.write_escaped(R.getValueAsString("Text")) << '"';
Craig Topperf3932e32013-07-19 21:43:59 +0000548
Benjamin Kramera8cafe22012-02-15 20:57:03 +0000549 // Warning associated with the diagnostic. This is stored as an index into
550 // the alphabetically sorted warning table.
Sean Silva1c4aaa82012-10-10 20:25:43 +0000551 if (DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group"))) {
Argyrios Kyrtzidis87acf192012-03-06 00:00:38 +0000552 std::map<std::string, GroupInfo>::iterator I =
553 DiagsInGroup.find(DI->getDef()->getValueAsString("GroupName"));
554 assert(I != DiagsInGroup.end());
555 OS << ", " << I->second.IDNo;
Ted Kremenekb22ea2a2012-07-07 05:53:30 +0000556 } else if (DiagsInPedantic.count(&R)) {
557 std::map<std::string, GroupInfo>::iterator I =
558 DiagsInGroup.find("pedantic");
559 assert(I != DiagsInGroup.end() && "pedantic group not defined");
560 OS << ", " << I->second.IDNo;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000561 } else {
Benjamin Kramera8cafe22012-02-15 20:57:03 +0000562 OS << ", 0";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000563 }
564
Richard Smith16e1b072013-11-12 02:41:45 +0000565 // SFINAE response.
566 OS << ", " << R.getValueAsDef("SFINAE")->getName();
567
568 // Default warning has no Werror bit.
569 if (R.getValueAsBit("WarningNoWerror"))
Peter Collingbournebee583f2011-10-06 13:03:08 +0000570 OS << ", true";
571 else
572 OS << ", false";
573
Richard Smith16e1b072013-11-12 02:41:45 +0000574 // Default warning show in system header bit.
575 if (R.getValueAsBit("WarningShowInSystemHeader"))
Peter Collingbournebee583f2011-10-06 13:03:08 +0000576 OS << ", true";
577 else
578 OS << ", false";
579
Peter Collingbournebee583f2011-10-06 13:03:08 +0000580 // Category number.
581 OS << ", " << CategoryIDs.getID(getDiagnosticCategory(&R, DGParentMap));
Peter Collingbournebee583f2011-10-06 13:03:08 +0000582 OS << ")\n";
583 }
584}
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +0000585} // end namespace clang
Peter Collingbournebee583f2011-10-06 13:03:08 +0000586
587//===----------------------------------------------------------------------===//
588// Warning Group Tables generation
589//===----------------------------------------------------------------------===//
590
591static std::string getDiagCategoryEnum(llvm::StringRef name) {
592 if (name.empty())
593 return "DiagCat_None";
Dylan Noblesmithf1a13f22012-02-13 12:32:26 +0000594 SmallString<256> enumName = llvm::StringRef("DiagCat_");
Peter Collingbournebee583f2011-10-06 13:03:08 +0000595 for (llvm::StringRef::iterator I = name.begin(), E = name.end(); I != E; ++I)
596 enumName += isalnum(*I) ? *I : '_';
597 return enumName.str();
598}
Craig Topperf3932e32013-07-19 21:43:59 +0000599
Tobias Grossercfc57bb2014-05-06 22:06:56 +0000600/// \brief Emit the array of diagnostic subgroups.
601///
602/// The array of diagnostic subgroups contains for each group a list of its
603/// subgroups. The individual lists are separated by '-1'. Groups with no
604/// subgroups are skipped.
605///
606/// \code
607/// static const int16_t DiagSubGroups[] = {
608/// /* Empty */ -1,
609/// /* DiagSubGroup0 */ 142, -1,
610/// /* DiagSubGroup13 */ 265, 322, 399, -1
611/// }
612/// \endcode
613///
614static void emitDiagSubGroups(std::map<std::string, GroupInfo> &DiagsInGroup,
615 RecordVec &GroupsInPedantic, raw_ostream &OS) {
616 OS << "static const int16_t DiagSubGroups[] = {\n"
617 << " /* Empty */ -1,\n";
618 for (auto const &I : DiagsInGroup) {
619 const bool IsPedantic = I.first == "pedantic";
620
621 const std::vector<std::string> &SubGroups = I.second.SubGroups;
622 if (!SubGroups.empty() || (IsPedantic && !GroupsInPedantic.empty())) {
623 OS << " /* DiagSubGroup" << I.second.IDNo << " */ ";
624 for (auto const &SubGroup : SubGroups) {
625 std::map<std::string, GroupInfo>::const_iterator RI =
626 DiagsInGroup.find(SubGroup);
627 assert(RI != DiagsInGroup.end() && "Referenced without existing?");
628 OS << RI->second.IDNo << ", ";
629 }
630 // Emit the groups implicitly in "pedantic".
631 if (IsPedantic) {
632 for (auto const &Group : GroupsInPedantic) {
633 const std::string &GroupName = Group->getValueAsString("GroupName");
634 std::map<std::string, GroupInfo>::const_iterator RI =
635 DiagsInGroup.find(GroupName);
636 assert(RI != DiagsInGroup.end() && "Referenced without existing?");
637 OS << RI->second.IDNo << ", ";
638 }
639 }
640
641 OS << "-1,\n";
642 }
643 }
644 OS << "};\n\n";
645}
646
647/// \brief Emit the list of diagnostic arrays.
648///
649/// This data structure is a large array that contains itself arrays of varying
650/// size. Each array represents a list of diagnostics. The different arrays are
651/// separated by the value '-1'.
652///
653/// \code
654/// static const int16_t DiagArrays[] = {
655/// /* Empty */ -1,
656/// /* DiagArray1 */ diag::warn_pragma_message,
657/// -1,
658/// /* DiagArray2 */ diag::warn_abs_too_small,
659/// diag::warn_unsigned_abs,
660/// diag::warn_wrong_absolute_value_type,
661/// -1
662/// };
663/// \endcode
664///
665static void emitDiagArrays(std::map<std::string, GroupInfo> &DiagsInGroup,
666 RecordVec &DiagsInPedantic, raw_ostream &OS) {
667 OS << "static const int16_t DiagArrays[] = {\n"
668 << " /* Empty */ -1,\n";
669 for (auto const &I : DiagsInGroup) {
670 const bool IsPedantic = I.first == "pedantic";
671
672 const std::vector<const Record *> &V = I.second.DiagsInGroup;
673 if (!V.empty() || (IsPedantic && !DiagsInPedantic.empty())) {
674 OS << " /* DiagArray" << I.second.IDNo << " */ ";
675 for (auto *Record : V)
676 OS << "diag::" << Record->getName() << ", ";
677 // Emit the diagnostics implicitly in "pedantic".
678 if (IsPedantic) {
679 for (auto const &Diag : DiagsInPedantic)
680 OS << "diag::" << Diag->getName() << ", ";
681 }
682 OS << "-1,\n";
683 }
684 }
685 OS << "};\n\n";
686}
687
688/// \brief Emit a list of group names.
689///
690/// This creates a long string which by itself contains a list of pascal style
691/// strings, which consist of a length byte directly followed by the string.
692///
693/// \code
694/// static const char DiagGroupNames[] = {
695/// \000\020#pragma-messages\t#warnings\020CFString-literal"
696/// };
697/// \endcode
698static void emitDiagGroupNames(StringToOffsetTable &GroupNames,
699 raw_ostream &OS) {
700 OS << "static const char DiagGroupNames[] = {\n";
701 GroupNames.EmitString(OS);
702 OS << "};\n\n";
703}
704
705/// \brief Emit diagnostic arrays and related data structures.
706///
707/// This creates the actual diagnostic array, an array of diagnostic subgroups
708/// and an array of subgroup names.
709///
710/// \code
711/// #ifdef GET_DIAG_ARRAYS
712/// static const int16_t DiagArrays[];
713/// static const int16_t DiagSubGroups[];
714/// static const char DiagGroupNames[];
715/// #endif
716/// \endcode
717static void emitAllDiagArrays(std::map<std::string, GroupInfo> &DiagsInGroup,
718 RecordVec &DiagsInPedantic,
719 RecordVec &GroupsInPedantic,
720 StringToOffsetTable &GroupNames,
721 raw_ostream &OS) {
722 OS << "\n#ifdef GET_DIAG_ARRAYS\n";
723 emitDiagArrays(DiagsInGroup, DiagsInPedantic, OS);
724 emitDiagSubGroups(DiagsInGroup, GroupsInPedantic, OS);
725 emitDiagGroupNames(GroupNames, OS);
726 OS << "#endif // GET_DIAG_ARRAYS\n\n";
727}
728
729/// \brief Emit diagnostic table.
730///
731/// The table is sorted by the name of the diagnostic group. Each element
732/// consists of the name of the diagnostic group (given as offset in the
733/// group name table), a reference to a list of diagnostics (optional) and a
734/// reference to a set of subgroups (optional).
735///
736/// \code
737/// #ifdef GET_DIAG_TABLE
738/// {/* abi */ 159, /* DiagArray11 */ 19, /* Empty */ 0},
739/// {/* aggregate-return */ 180, /* Empty */ 0, /* Empty */ 0},
740/// {/* all */ 197, /* Empty */ 0, /* DiagSubGroup13 */ 3},
741/// {/* deprecated */ 1981,/* DiagArray1 */ 348, /* DiagSubGroup3 */ 9},
742/// #endif
743/// \endcode
744static void emitDiagTable(std::map<std::string, GroupInfo> &DiagsInGroup,
745 RecordVec &DiagsInPedantic,
746 RecordVec &GroupsInPedantic,
747 StringToOffsetTable &GroupNames, raw_ostream &OS) {
748 unsigned MaxLen = 0;
749
750 for (auto const &I: DiagsInGroup)
751 MaxLen = std::max(MaxLen, (unsigned)I.first.size());
752
753 OS << "\n#ifdef GET_DIAG_TABLE\n";
754 unsigned SubGroupIndex = 1, DiagArrayIndex = 1;
755 for (auto const &I: DiagsInGroup) {
756 // Group option string.
757 OS << " { /* ";
758 if (I.first.find_first_not_of("abcdefghijklmnopqrstuvwxyz"
759 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
760 "0123456789!@#$%^*-+=:?") !=
761 std::string::npos)
762 PrintFatalError("Invalid character in diagnostic group '" + I.first +
763 "'");
764 OS << I.first << " */ " << std::string(MaxLen - I.first.size(), ' ');
765 // Store a pascal-style length byte at the beginning of the string.
766 std::string Name = char(I.first.size()) + I.first;
767 OS << GroupNames.GetOrAddStringOffset(Name, false) << ", ";
768
769 // Special handling for 'pedantic'.
770 const bool IsPedantic = I.first == "pedantic";
771
772 // Diagnostics in the group.
773 const std::vector<const Record *> &V = I.second.DiagsInGroup;
774 const bool hasDiags =
775 !V.empty() || (IsPedantic && !DiagsInPedantic.empty());
776 if (hasDiags) {
777 OS << "/* DiagArray" << I.second.IDNo << " */ " << DiagArrayIndex
778 << ", ";
779 if (IsPedantic)
780 DiagArrayIndex += DiagsInPedantic.size();
781 DiagArrayIndex += V.size() + 1;
782 } else {
783 OS << "/* Empty */ 0, ";
784 }
785
786 // Subgroups.
787 const std::vector<std::string> &SubGroups = I.second.SubGroups;
788 const bool hasSubGroups =
789 !SubGroups.empty() || (IsPedantic && !GroupsInPedantic.empty());
790 if (hasSubGroups) {
791 OS << "/* DiagSubGroup" << I.second.IDNo << " */ " << SubGroupIndex;
792 if (IsPedantic)
793 SubGroupIndex += GroupsInPedantic.size();
794 SubGroupIndex += SubGroups.size() + 1;
795 } else {
796 OS << "/* Empty */ 0";
797 }
798
799 OS << " },\n";
800 }
801 OS << "#endif // GET_DIAG_TABLE\n\n";
802}
803
804/// \brief Emit the table of diagnostic categories.
805///
806/// The table has the form of macro calls that have two parameters. The
807/// category's name as well as an enum that represents the category. The
808/// table can be used by defining the macro 'CATEGORY' and including this
809/// table right after.
810///
811/// \code
812/// #ifdef GET_CATEGORY_TABLE
813/// CATEGORY("Semantic Issue", DiagCat_Semantic_Issue)
814/// CATEGORY("Lambda Issue", DiagCat_Lambda_Issue)
815/// #endif
816/// \endcode
817static void emitCategoryTable(RecordKeeper &Records, raw_ostream &OS) {
818 DiagCategoryIDMap CategoriesByID(Records);
819 OS << "\n#ifdef GET_CATEGORY_TABLE\n";
820 for (auto const &C : CategoriesByID)
821 OS << "CATEGORY(\"" << C << "\", " << getDiagCategoryEnum(C) << ")\n";
822 OS << "#endif // GET_CATEGORY_TABLE\n\n";
823}
824
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +0000825namespace clang {
826void EmitClangDiagGroups(RecordKeeper &Records, raw_ostream &OS) {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000827 // Compute a mapping from a DiagGroup to all of its parents.
828 DiagGroupParentMap DGParentMap(Records);
Ted Kremenekb22ea2a2012-07-07 05:53:30 +0000829
Tobias Grossercfc57bb2014-05-06 22:06:56 +0000830 std::vector<Record *> Diags = Records.getAllDerivedDefinitions("Diagnostic");
Craig Topperf3932e32013-07-19 21:43:59 +0000831
Tobias Grossercfc57bb2014-05-06 22:06:56 +0000832 std::vector<Record *> DiagGroups =
833 Records.getAllDerivedDefinitions("DiagGroup");
Argyrios Kyrtzidis87acf192012-03-06 00:00:38 +0000834
835 std::map<std::string, GroupInfo> DiagsInGroup;
836 groupDiagnostics(Diags, DiagGroups, DiagsInGroup);
Ted Kremenekb22ea2a2012-07-07 05:53:30 +0000837
838 // All extensions are implicitly in the "pedantic" group. Record the
839 // implicit set of groups in the "pedantic" group, and use this information
840 // later when emitting the group information for Pedantic.
841 RecordVec DiagsInPedantic;
842 RecordVec GroupsInPedantic;
843 InferPedantic inferPedantic(DGParentMap, Diags, DiagGroups, DiagsInGroup);
844 inferPedantic.compute(&DiagsInPedantic, &GroupsInPedantic);
845
Craig Topperda7cf8a2013-08-29 05:18:04 +0000846 StringToOffsetTable GroupNames;
847 for (std::map<std::string, GroupInfo>::const_iterator
Tobias Grossercfc57bb2014-05-06 22:06:56 +0000848 I = DiagsInGroup.begin(),
849 E = DiagsInGroup.end();
850 I != E; ++I) {
Craig Topperda7cf8a2013-08-29 05:18:04 +0000851 // Store a pascal-style length byte at the beginning of the string.
852 std::string Name = char(I->first.size()) + I->first;
853 GroupNames.GetOrAddStringOffset(Name, false);
854 }
855
Tobias Grossercfc57bb2014-05-06 22:06:56 +0000856 emitAllDiagArrays(DiagsInGroup, DiagsInPedantic, GroupsInPedantic, GroupNames,
857 OS);
858 emitDiagTable(DiagsInGroup, DiagsInPedantic, GroupsInPedantic, GroupNames,
859 OS);
860 emitCategoryTable(Records, OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +0000861}
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +0000862} // end namespace clang
Peter Collingbournebee583f2011-10-06 13:03:08 +0000863
864//===----------------------------------------------------------------------===//
865// Diagnostic name index generation
866//===----------------------------------------------------------------------===//
867
868namespace {
869struct RecordIndexElement
870{
871 RecordIndexElement() {}
872 explicit RecordIndexElement(Record const &R):
873 Name(R.getName()) {}
Craig Topperf3932e32013-07-19 21:43:59 +0000874
Peter Collingbournebee583f2011-10-06 13:03:08 +0000875 std::string Name;
876};
Peter Collingbournebee583f2011-10-06 13:03:08 +0000877} // end anonymous namespace.
878
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +0000879namespace clang {
880void EmitClangDiagsIndexName(RecordKeeper &Records, raw_ostream &OS) {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000881 const std::vector<Record*> &Diags =
882 Records.getAllDerivedDefinitions("Diagnostic");
Craig Topperf3932e32013-07-19 21:43:59 +0000883
Peter Collingbournebee583f2011-10-06 13:03:08 +0000884 std::vector<RecordIndexElement> Index;
885 Index.reserve(Diags.size());
886 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
Craig Topperf3932e32013-07-19 21:43:59 +0000887 const Record &R = *(Diags[i]);
Peter Collingbournebee583f2011-10-06 13:03:08 +0000888 Index.push_back(RecordIndexElement(R));
889 }
Craig Topperf3932e32013-07-19 21:43:59 +0000890
Benjamin Kramerbbdd7642014-03-01 14:48:57 +0000891 std::sort(Index.begin(), Index.end(),
892 [](const RecordIndexElement &Lhs,
893 const RecordIndexElement &Rhs) { return Lhs.Name < Rhs.Name; });
Craig Topperf3932e32013-07-19 21:43:59 +0000894
Peter Collingbournebee583f2011-10-06 13:03:08 +0000895 for (unsigned i = 0, e = Index.size(); i != e; ++i) {
896 const RecordIndexElement &R = Index[i];
Craig Topperf3932e32013-07-19 21:43:59 +0000897
Peter Collingbournebee583f2011-10-06 13:03:08 +0000898 OS << "DIAG_NAME_INDEX(" << R.Name << ")\n";
899 }
900}
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +0000901} // end namespace clang