1 //=- ClangDiagnosticsEmitter.cpp - Generate Clang diagnostics tables -*- C++ -*-
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // These tablegen backends emit Clang diagnostics tables.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/DenseSet.h"
15 #include "llvm/ADT/Optional.h"
16 #include "llvm/ADT/PointerUnion.h"
17 #include "llvm/ADT/SetVector.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/Support/Compiler.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/TableGen/Error.h"
26 #include "llvm/TableGen/Record.h"
27 #include "llvm/TableGen/StringToOffsetTable.h"
28 #include "llvm/TableGen/TableGenBackend.h"
36 //===----------------------------------------------------------------------===//
37 // Diagnostic category computation code.
38 //===----------------------------------------------------------------------===//
41 class DiagGroupParentMap {
42 RecordKeeper &Records;
43 std::map<const Record*, std::vector<Record*> > Mapping;
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]);
56 const std::vector<Record*> &getParents(const Record *Group) {
57 return Mapping[Group];
60 } // end anonymous namespace.
63 getCategoryFromDiagGroup(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;
69 // 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;
79 /// getDiagnosticCategory - Return the category that the specified diagnostic
81 static 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.
84 if (DefInit *Group = dyn_cast<DefInit>(R->getValueInit("Group"))) {
85 // Check the diagnostic's diag group for a category.
86 std::string CatName = getCategoryFromDiagGroup(Group->getDef(),
88 if (!CatName.empty()) return CatName;
91 // If the diagnostic itself has a category, get it.
92 return R->getValueAsString("CategoryName");
96 class DiagCategoryIDMap {
97 RecordKeeper &Records;
98 StringMap<unsigned> CategoryIDs;
99 std::vector<std::string> CategoryStrings;
101 DiagCategoryIDMap(RecordKeeper &records) : Records(records) {
102 DiagGroupParentMap ParentInfo(Records);
104 // The zero'th category is "".
105 CategoryStrings.push_back("");
108 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.
114 unsigned &ID = CategoryIDs[Category];
115 if (ID != 0) continue; // Already seen.
117 ID = CategoryStrings.size();
118 CategoryStrings.push_back(Category);
122 unsigned getID(StringRef CategoryString) {
123 return CategoryIDs[CategoryString];
126 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(); }
132 std::vector<const Record*> DiagsInGroup;
133 std::vector<std::string> SubGroups;
136 const Record *ExplicitDef;
138 GroupInfo() : ExplicitDef(0) {}
140 } // end anonymous namespace.
142 static bool beforeThanCompare(const Record *LHS, const Record *RHS) {
143 assert(!LHS->getLoc().empty() && !RHS->getLoc().empty());
145 LHS->getLoc().front().getPointer() < RHS->getLoc().front().getPointer();
148 static 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());
154 static SMRange findSuperClassRange(const Record *R, StringRef SuperName) {
155 ArrayRef<Record *> Supers = R->getSuperClasses();
157 for (size_t i = 0, e = Supers.size(); i < e; ++i)
158 if (Supers[i]->getName() == SuperName)
159 return R->getSuperClassRanges()[i];
164 /// \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.
166 static void groupDiagnostics(const std::vector<Record*> &Diags,
167 const std::vector<Record*> &DiagGroups,
168 std::map<std::string, GroupInfo> &DiagsInGroup) {
170 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
171 const Record *R = Diags[i];
172 DefInit *DI = dyn_cast<DefInit>(R->getValueInit("Group"));
173 if (DI == 0) continue;
174 assert(R->getValueAsDef("Class")->getName() != "CLASS_NOTE" &&
175 "Note can't be in a DiagGroup");
176 std::string GroupName = DI->getDef()->getValueAsString("GroupName");
177 DiagsInGroup[GroupName].DiagsInGroup.push_back(R);
180 typedef SmallPtrSet<GroupInfo *, 16> GroupSetTy;
181 GroupSetTy ImplicitGroups;
183 // 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")];
188 if (Group->isAnonymous()) {
189 if (GI.DiagsInGroup.size() > 1)
190 ImplicitGroups.insert(&GI);
193 assert(GI.ExplicitDef == Group);
195 GI.ExplicitDef = Group;
198 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"));
203 // Assign unique ID numbers to the groups.
205 for (std::map<std::string, GroupInfo>::iterator
206 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I, ++IDNo)
207 I->second.IDNo = IDNo;
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();
215 MutableArrayRef<const Record *> GroupDiags = (*I)->DiagsInGroup;
216 std::sort(GroupDiags.begin(), GroupDiags.end(), beforeThanCompare);
218 std::sort(SortedGroups.begin(), SortedGroups.end(), beforeThanCompareGroups);
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();
224 ArrayRef<const Record *> GroupDiags = (*I)->DiagsInGroup;
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();
231 const DefInit *GroupInit = cast<DefInit>((*DI)->getValueInit("Group"));
232 const Record *NextDiagGroup = GroupInit->getDef();
233 if (NextDiagGroup == (*I)->ExplicitDef)
236 SMRange InGroupRange = findSuperClassRange(*DI, "InGroup");
237 SmallString<64> Replacement;
238 if (InGroupRange.isValid()) {
239 Replacement += "InGroup<";
240 Replacement += (*I)->ExplicitDef->getName();
243 SMFixIt FixIt(InGroupRange, Replacement.str());
245 SrcMgr.PrintMessage(NextDiagGroup->getLoc().front(),
247 Twine("group '") + Name +
248 "' is referred to anonymously",
250 InGroupRange.isValid() ? FixIt
251 : ArrayRef<SMFixIt>());
252 SrcMgr.PrintMessage((*I)->ExplicitDef->getLoc().front(),
253 SourceMgr::DK_Note, "group defined here");
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!");
262 const DefInit *GroupInit = cast<DefInit>((*DI)->getValueInit("Group"));
263 const Record *NextDiagGroup = GroupInit->getDef();
264 std::string Name = NextDiagGroup->getValueAsString("GroupName");
266 SMRange InGroupRange = findSuperClassRange(*DI, "InGroup");
267 SrcMgr.PrintMessage(NextDiagGroup->getLoc().front(),
269 Twine("group '") + Name +
270 "' is referred to anonymously",
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",
284 //===----------------------------------------------------------------------===//
285 // Infer members of -Wpedantic.
286 //===----------------------------------------------------------------------===//
288 typedef std::vector<const Record *> RecordVec;
289 typedef llvm::DenseSet<const Record *> RecordSet;
290 typedef llvm::PointerUnion<RecordVec*, RecordSet*> VecOrSet;
293 class InferPedantic {
294 typedef llvm::DenseMap<const Record*,
295 std::pair<unsigned, Optional<unsigned> > > GMap;
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;
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),
310 DiagGroups(DiagGroups),
311 DiagsInGroup(DiagsInGroup) {}
313 /// Compute the set of diagnostics and groups that are immediately
315 void compute(VecOrSet DiagsInPedantic,
316 VecOrSet GroupsInPedantic);
319 /// Determine whether a group is a subgroup of another group.
320 bool isSubGroupOfGroup(const Record *Group,
321 llvm::StringRef RootGroupName);
323 /// Determine if the diagnostic is an extension.
324 bool isExtension(const Record *Diag);
326 /// Determine if the diagnostic is off by default.
327 bool isOffByDefault(const Record *Diag);
329 /// Increment the count for a group, and transitively marked
330 /// parent groups when appropriate.
331 void markGroup(const Record *Group);
333 /// Return true if the diagnostic is in a pedantic group.
334 bool groupInPedantic(const Record *Group, bool increment = false);
336 } // end anonymous namespace
338 bool InferPedantic::isSubGroupOfGroup(const Record *Group,
339 llvm::StringRef GName) {
341 const std::string &GroupName = Group->getValueAsString("GroupName");
342 if (GName == GroupName)
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))
353 /// Determine if the diagnostic is an extension.
354 bool InferPedantic::isExtension(const Record *Diag) {
355 const std::string &ClsName = Diag->getValueAsDef("Class")->getName();
356 return ClsName == "CLASS_EXTENSION";
359 bool InferPedantic::isOffByDefault(const Record *Diag) {
360 const std::string &DefMap = Diag->getValueAsDef("DefaultMapping")->getName();
361 return DefMap == "MAP_IGNORE";
364 bool 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();
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();
381 void 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]);
393 void InferPedantic::compute(VecOrSet DiagsInPedantic,
394 VecOrSet GroupsInPedantic) {
395 // 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.
398 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
399 Record *R = Diags[i];
400 if (isExtension(R) && isOffByDefault(R)) {
402 if (DefInit *Group = dyn_cast<DefInit>(R->getValueInit("Group"))) {
403 const Record *GroupRec = Group->getDef();
404 if (!isSubGroupOfGroup(GroupRec, "pedantic")) {
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))
418 // Check if the group is implicitly in -Wpedantic. If so,
419 // the diagnostic should not be directly included in the -Wpedantic
421 if (DefInit *Group = dyn_cast<DefInit>(R->getValueInit("Group")))
422 if (groupInPedantic(Group->getDef()))
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*>())
430 DiagsInPedantic.get<RecordSet*>()->insert(R);
434 if (!GroupsInPedantic)
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))
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]))
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())
458 if (RecordVec *V = GroupsInPedantic.dyn_cast<RecordVec*>())
461 GroupsInPedantic.get<RecordSet*>()->insert(Group);
466 //===----------------------------------------------------------------------===//
467 // Warning Tables (.inc file) generation.
468 //===----------------------------------------------------------------------===//
470 static bool isError(const Record &Diag) {
471 const std::string &ClsName = Diag.getValueAsDef("Class")->getName();
472 return ClsName == "CLASS_ERROR";
475 /// ClangDiagsDefsEmitter - The top-level class emits .def files containing
476 /// declarations of Clang diagnostics.
478 void EmitClangDiagsDefs(RecordKeeper &Records, raw_ostream &OS,
479 const std::string &Component) {
480 // Write the #if guard
481 if (!Component.empty()) {
482 std::string ComponentName = StringRef(Component).upper();
483 OS << "#ifdef " << ComponentName << "START\n";
484 OS << "__" << ComponentName << "START = DIAG_START_" << ComponentName
486 OS << "#undef " << ComponentName << "START\n";
490 const std::vector<Record*> &Diags =
491 Records.getAllDerivedDefinitions("Diagnostic");
493 std::vector<Record*> DiagGroups
494 = Records.getAllDerivedDefinitions("DiagGroup");
496 std::map<std::string, GroupInfo> DiagsInGroup;
497 groupDiagnostics(Diags, DiagGroups, DiagsInGroup);
499 DiagCategoryIDMap CategoryIDs(Records);
500 DiagGroupParentMap DGParentMap(Records);
502 // Compute the set of diagnostics that are in -Wpedantic.
503 RecordSet DiagsInPedantic;
504 InferPedantic inferPedantic(DGParentMap, Diags, DiagGroups, DiagsInGroup);
505 inferPedantic.compute(&DiagsInPedantic, (RecordVec*)0);
507 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
508 const Record &R = *Diags[i];
510 // Check if this is an error that is accidentally in a warning
513 if (DefInit *Group = dyn_cast<DefInit>(R.getValueInit("Group"))) {
514 const Record *GroupRec = Group->getDef();
515 const std::string &GroupName = GroupRec->getValueAsString("GroupName");
516 PrintFatalError(R.getLoc(), "Error " + R.getName() +
517 " cannot be in a warning group [" + GroupName + "]");
521 // Filter by component.
522 if (!Component.empty() && Component != R.getValueAsString("Component"))
525 OS << "DIAG(" << R.getName() << ", ";
526 OS << R.getValueAsDef("Class")->getName();
527 OS << ", diag::" << R.getValueAsDef("DefaultMapping")->getName();
529 // Description string.
531 OS.write_escaped(R.getValueAsString("Text")) << '"';
533 // Warning associated with the diagnostic. This is stored as an index into
534 // the alphabetically sorted warning table.
535 if (DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group"))) {
536 std::map<std::string, GroupInfo>::iterator I =
537 DiagsInGroup.find(DI->getDef()->getValueAsString("GroupName"));
538 assert(I != DiagsInGroup.end());
539 OS << ", " << I->second.IDNo;
540 } else if (DiagsInPedantic.count(&R)) {
541 std::map<std::string, GroupInfo>::iterator I =
542 DiagsInGroup.find("pedantic");
543 assert(I != DiagsInGroup.end() && "pedantic group not defined");
544 OS << ", " << I->second.IDNo;
550 OS << ", " << R.getValueAsDef("SFINAE")->getName();
552 // Default warning has no Werror bit.
553 if (R.getValueAsBit("WarningNoWerror"))
558 // Default warning show in system header bit.
559 if (R.getValueAsBit("WarningShowInSystemHeader"))
565 OS << ", " << CategoryIDs.getID(getDiagnosticCategory(&R, DGParentMap));
569 } // end namespace clang
571 //===----------------------------------------------------------------------===//
572 // Warning Group Tables generation
573 //===----------------------------------------------------------------------===//
575 static std::string getDiagCategoryEnum(llvm::StringRef name) {
577 return "DiagCat_None";
578 SmallString<256> enumName = llvm::StringRef("DiagCat_");
579 for (llvm::StringRef::iterator I = name.begin(), E = name.end(); I != E; ++I)
580 enumName += isalnum(*I) ? *I : '_';
581 return enumName.str();
585 void EmitClangDiagGroups(RecordKeeper &Records, raw_ostream &OS) {
586 // Compute a mapping from a DiagGroup to all of its parents.
587 DiagGroupParentMap DGParentMap(Records);
589 std::vector<Record*> Diags =
590 Records.getAllDerivedDefinitions("Diagnostic");
592 std::vector<Record*> DiagGroups
593 = Records.getAllDerivedDefinitions("DiagGroup");
595 std::map<std::string, GroupInfo> DiagsInGroup;
596 groupDiagnostics(Diags, DiagGroups, DiagsInGroup);
598 // All extensions are implicitly in the "pedantic" group. Record the
599 // implicit set of groups in the "pedantic" group, and use this information
600 // later when emitting the group information for Pedantic.
601 RecordVec DiagsInPedantic;
602 RecordVec GroupsInPedantic;
603 InferPedantic inferPedantic(DGParentMap, Diags, DiagGroups, DiagsInGroup);
604 inferPedantic.compute(&DiagsInPedantic, &GroupsInPedantic);
606 // Walk through the groups emitting an array for each diagnostic of the diags
607 // that are mapped to.
608 OS << "\n#ifdef GET_DIAG_ARRAYS\n";
610 OS << "static const int16_t DiagArrays[] = {\n"
611 << " /* Empty */ -1,\n";
612 for (std::map<std::string, GroupInfo>::const_iterator
613 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
614 MaxLen = std::max(MaxLen, (unsigned)I->first.size());
615 const bool IsPedantic = I->first == "pedantic";
617 const std::vector<const Record*> &V = I->second.DiagsInGroup;
618 if (!V.empty() || (IsPedantic && !DiagsInPedantic.empty())) {
619 OS << " /* DiagArray" << I->second.IDNo << " */ ";
620 for (unsigned i = 0, e = V.size(); i != e; ++i)
621 OS << "diag::" << V[i]->getName() << ", ";
622 // Emit the diagnostics implicitly in "pedantic".
624 for (unsigned i = 0, e = DiagsInPedantic.size(); i != e; ++i)
625 OS << "diag::" << DiagsInPedantic[i]->getName() << ", ";
632 OS << "static const int16_t DiagSubGroups[] = {\n"
633 << " /* Empty */ -1,\n";
634 for (std::map<std::string, GroupInfo>::const_iterator
635 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
636 const bool IsPedantic = I->first == "pedantic";
638 const std::vector<std::string> &SubGroups = I->second.SubGroups;
639 if (!SubGroups.empty() || (IsPedantic && !GroupsInPedantic.empty())) {
640 OS << " /* DiagSubGroup" << I->second.IDNo << " */ ";
641 for (unsigned i = 0, e = SubGroups.size(); i != e; ++i) {
642 std::map<std::string, GroupInfo>::const_iterator RI =
643 DiagsInGroup.find(SubGroups[i]);
644 assert(RI != DiagsInGroup.end() && "Referenced without existing?");
645 OS << RI->second.IDNo << ", ";
647 // Emit the groups implicitly in "pedantic".
649 for (unsigned i = 0, e = GroupsInPedantic.size(); i != e; ++i) {
650 const std::string &GroupName =
651 GroupsInPedantic[i]->getValueAsString("GroupName");
652 std::map<std::string, GroupInfo>::const_iterator RI =
653 DiagsInGroup.find(GroupName);
654 assert(RI != DiagsInGroup.end() && "Referenced without existing?");
655 OS << RI->second.IDNo << ", ";
664 StringToOffsetTable GroupNames;
665 for (std::map<std::string, GroupInfo>::const_iterator
666 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
667 // Store a pascal-style length byte at the beginning of the string.
668 std::string Name = char(I->first.size()) + I->first;
669 GroupNames.GetOrAddStringOffset(Name, false);
672 OS << "static const char DiagGroupNames[] = {\n";
673 GroupNames.EmitString(OS);
676 OS << "#endif // GET_DIAG_ARRAYS\n\n";
678 // Emit the table now.
679 OS << "\n#ifdef GET_DIAG_TABLE\n";
680 unsigned SubGroupIndex = 1, DiagArrayIndex = 1;
681 for (std::map<std::string, GroupInfo>::const_iterator
682 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
683 // Group option string.
685 if (I->first.find_first_not_of("abcdefghijklmnopqrstuvwxyz"
686 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
687 "0123456789!@#$%^*-+=:?")!=std::string::npos)
688 PrintFatalError("Invalid character in diagnostic group '" +
690 OS << I->first << " */ " << std::string(MaxLen-I->first.size(), ' ');
691 // Store a pascal-style length byte at the beginning of the string.
692 std::string Name = char(I->first.size()) + I->first;
693 OS << GroupNames.GetOrAddStringOffset(Name, false) << ", ";
695 // Special handling for 'pedantic'.
696 const bool IsPedantic = I->first == "pedantic";
698 // Diagnostics in the group.
699 const std::vector<const Record*> &V = I->second.DiagsInGroup;
700 const bool hasDiags = !V.empty() ||
701 (IsPedantic && !DiagsInPedantic.empty());
703 OS << "/* DiagArray" << I->second.IDNo << " */ "
704 << DiagArrayIndex << ", ";
706 DiagArrayIndex += DiagsInPedantic.size();
707 DiagArrayIndex += V.size() + 1;
709 OS << "/* Empty */ 0, ";
713 const std::vector<std::string> &SubGroups = I->second.SubGroups;
714 const bool hasSubGroups = !SubGroups.empty() ||
715 (IsPedantic && !GroupsInPedantic.empty());
717 OS << "/* DiagSubGroup" << I->second.IDNo << " */ " << SubGroupIndex;
719 SubGroupIndex += GroupsInPedantic.size();
720 SubGroupIndex += SubGroups.size() + 1;
722 OS << "/* Empty */ 0";
726 OS << "#endif // GET_DIAG_TABLE\n\n";
728 // Emit the category table next.
729 DiagCategoryIDMap CategoriesByID(Records);
730 OS << "\n#ifdef GET_CATEGORY_TABLE\n";
731 for (DiagCategoryIDMap::const_iterator I = CategoriesByID.begin(),
732 E = CategoriesByID.end(); I != E; ++I)
733 OS << "CATEGORY(\"" << *I << "\", " << getDiagCategoryEnum(*I) << ")\n";
734 OS << "#endif // GET_CATEGORY_TABLE\n\n";
736 } // end namespace clang
738 //===----------------------------------------------------------------------===//
739 // Diagnostic name index generation
740 //===----------------------------------------------------------------------===//
743 struct RecordIndexElement
745 RecordIndexElement() {}
746 explicit RecordIndexElement(Record const &R):
752 struct RecordIndexElementSorter :
753 public std::binary_function<RecordIndexElement, RecordIndexElement, bool> {
755 bool operator()(RecordIndexElement const &Lhs,
756 RecordIndexElement const &Rhs) const {
757 return Lhs.Name < Rhs.Name;
762 } // end anonymous namespace.
765 void EmitClangDiagsIndexName(RecordKeeper &Records, raw_ostream &OS) {
766 const std::vector<Record*> &Diags =
767 Records.getAllDerivedDefinitions("Diagnostic");
769 std::vector<RecordIndexElement> Index;
770 Index.reserve(Diags.size());
771 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
772 const Record &R = *(Diags[i]);
773 Index.push_back(RecordIndexElement(R));
776 std::sort(Index.begin(), Index.end(), RecordIndexElementSorter());
778 for (unsigned i = 0, e = Index.size(); i != e; ++i) {
779 const RecordIndexElement &R = Index[i];
781 OS << "DIAG_NAME_INDEX(" << R.Name << ")\n";
784 } // end namespace clang