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 "ClangDiagnosticsEmitter.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/ADT/DenseSet.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/ADT/VectorExtras.h"
27 //===----------------------------------------------------------------------===//
28 // Diagnostic category computation code.
29 //===----------------------------------------------------------------------===//
32 class DiagGroupParentMap {
33 RecordKeeper &Records;
34 std::map<const Record*, std::vector<Record*> > Mapping;
36 DiagGroupParentMap(RecordKeeper &records) : Records(records) {
37 std::vector<Record*> DiagGroups
38 = Records.getAllDerivedDefinitions("DiagGroup");
39 for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
40 std::vector<Record*> SubGroups =
41 DiagGroups[i]->getValueAsListOfDefs("SubGroups");
42 for (unsigned j = 0, e = SubGroups.size(); j != e; ++j)
43 Mapping[SubGroups[j]].push_back(DiagGroups[i]);
47 const std::vector<Record*> &getParents(const Record *Group) {
48 return Mapping[Group];
51 } // end anonymous namespace.
55 getCategoryFromDiagGroup(const Record *Group,
56 DiagGroupParentMap &DiagGroupParents) {
57 // If the DiagGroup has a category, return it.
58 std::string CatName = Group->getValueAsString("CategoryName");
59 if (!CatName.empty()) return CatName;
61 // The diag group may the subgroup of one or more other diagnostic groups,
62 // check these for a category as well.
63 const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
64 for (unsigned i = 0, e = Parents.size(); i != e; ++i) {
65 CatName = getCategoryFromDiagGroup(Parents[i], DiagGroupParents);
66 if (!CatName.empty()) return CatName;
71 /// getDiagnosticCategory - Return the category that the specified diagnostic
73 static std::string getDiagnosticCategory(const Record *R,
74 DiagGroupParentMap &DiagGroupParents) {
75 // If the diagnostic is in a group, and that group has a category, use it.
76 if (DefInit *Group = dynamic_cast<DefInit*>(R->getValueInit("Group"))) {
77 // Check the diagnostic's diag group for a category.
78 std::string CatName = getCategoryFromDiagGroup(Group->getDef(),
80 if (!CatName.empty()) return CatName;
83 // If the diagnostic itself has a category, get it.
84 return R->getValueAsString("CategoryName");
88 class DiagCategoryIDMap {
89 RecordKeeper &Records;
90 StringMap<unsigned> CategoryIDs;
91 std::vector<std::string> CategoryStrings;
93 DiagCategoryIDMap(RecordKeeper &records) : Records(records) {
94 DiagGroupParentMap ParentInfo(Records);
96 // The zero'th category is "".
97 CategoryStrings.push_back("");
100 std::vector<Record*> Diags =
101 Records.getAllDerivedDefinitions("Diagnostic");
102 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
103 std::string Category = getDiagnosticCategory(Diags[i], ParentInfo);
104 if (Category.empty()) continue; // Skip diags with no category.
106 unsigned &ID = CategoryIDs[Category];
107 if (ID != 0) continue; // Already seen.
109 ID = CategoryStrings.size();
110 CategoryStrings.push_back(Category);
114 unsigned getID(StringRef CategoryString) {
115 return CategoryIDs[CategoryString];
118 typedef std::vector<std::string>::iterator iterator;
119 iterator begin() { return CategoryStrings.begin(); }
120 iterator end() { return CategoryStrings.end(); }
122 } // end anonymous namespace.
125 //===----------------------------------------------------------------------===//
126 // Warning Tables (.inc file) generation.
127 //===----------------------------------------------------------------------===//
129 void ClangDiagsDefsEmitter::run(raw_ostream &OS) {
130 // Write the #if guard
131 if (!Component.empty()) {
132 std::string ComponentName = UppercaseString(Component);
133 OS << "#ifdef " << ComponentName << "START\n";
134 OS << "__" << ComponentName << "START = DIAG_START_" << ComponentName
136 OS << "#undef " << ComponentName << "START\n";
140 const std::vector<Record*> &Diags =
141 Records.getAllDerivedDefinitions("Diagnostic");
143 DiagCategoryIDMap CategoryIDs(Records);
144 DiagGroupParentMap DGParentMap(Records);
146 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
147 const Record &R = *Diags[i];
148 // Filter by component.
149 if (!Component.empty() && Component != R.getValueAsString("Component"))
152 OS << "DIAG(" << R.getName() << ", ";
153 OS << R.getValueAsDef("Class")->getName();
154 OS << ", diag::" << R.getValueAsDef("DefaultMapping")->getName();
156 // Description string.
158 OS.write_escaped(R.getValueAsString("Text")) << '"';
160 // Warning associated with the diagnostic.
161 if (DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Group"))) {
163 OS.write_escaped(DI->getDef()->getValueAsString("GroupName")) << '"';
169 if (R.getValueAsBit("SFINAE"))
174 // Access control bit
175 if (R.getValueAsBit("AccessControl"))
181 OS << ", " << CategoryIDs.getID(getDiagnosticCategory(&R, DGParentMap));
185 OS.write_escaped(R.getValueAsString("Brief")) << '"';
189 OS.write_escaped(R.getValueAsString("Explanation")) << '"';
194 //===----------------------------------------------------------------------===//
195 // Warning Group Tables generation
196 //===----------------------------------------------------------------------===//
200 std::vector<const Record*> DiagsInGroup;
201 std::vector<std::string> SubGroups;
204 } // end anonymous namespace.
206 void ClangDiagGroupsEmitter::run(raw_ostream &OS) {
207 // Compute a mapping from a DiagGroup to all of its parents.
208 DiagGroupParentMap DGParentMap(Records);
210 // Invert the 1-[0/1] mapping of diags to group into a one to many mapping of
211 // groups to diags in the group.
212 std::map<std::string, GroupInfo> DiagsInGroup;
214 std::vector<Record*> Diags =
215 Records.getAllDerivedDefinitions("Diagnostic");
216 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
217 const Record *R = Diags[i];
218 DefInit *DI = dynamic_cast<DefInit*>(R->getValueInit("Group"));
219 if (DI == 0) continue;
220 std::string GroupName = DI->getDef()->getValueAsString("GroupName");
221 DiagsInGroup[GroupName].DiagsInGroup.push_back(R);
224 // Add all DiagGroup's to the DiagsInGroup list to make sure we pick up empty
225 // groups (these are warnings that GCC supports that clang never produces).
226 std::vector<Record*> DiagGroups
227 = Records.getAllDerivedDefinitions("DiagGroup");
228 for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
229 Record *Group = DiagGroups[i];
230 GroupInfo &GI = DiagsInGroup[Group->getValueAsString("GroupName")];
232 std::vector<Record*> SubGroups = Group->getValueAsListOfDefs("SubGroups");
233 for (unsigned j = 0, e = SubGroups.size(); j != e; ++j)
234 GI.SubGroups.push_back(SubGroups[j]->getValueAsString("GroupName"));
237 // Assign unique ID numbers to the groups.
239 for (std::map<std::string, GroupInfo>::iterator
240 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I, ++IDNo)
241 I->second.IDNo = IDNo;
243 // Walk through the groups emitting an array for each diagnostic of the diags
244 // that are mapped to.
245 OS << "\n#ifdef GET_DIAG_ARRAYS\n";
247 for (std::map<std::string, GroupInfo>::iterator
248 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
249 MaxLen = std::max(MaxLen, (unsigned)I->first.size());
251 std::vector<const Record*> &V = I->second.DiagsInGroup;
253 OS << "static const short DiagArray" << I->second.IDNo << "[] = { ";
254 for (unsigned i = 0, e = V.size(); i != e; ++i)
255 OS << "diag::" << V[i]->getName() << ", ";
259 const std::vector<std::string> &SubGroups = I->second.SubGroups;
260 if (!SubGroups.empty()) {
261 OS << "static const short DiagSubGroup" << I->second.IDNo << "[] = { ";
262 for (unsigned i = 0, e = SubGroups.size(); i != e; ++i) {
263 std::map<std::string, GroupInfo>::iterator RI =
264 DiagsInGroup.find(SubGroups[i]);
265 assert(RI != DiagsInGroup.end() && "Referenced without existing?");
266 OS << RI->second.IDNo << ", ";
271 OS << "#endif // GET_DIAG_ARRAYS\n\n";
273 // Emit the table now.
274 OS << "\n#ifdef GET_DIAG_TABLE\n";
275 for (std::map<std::string, GroupInfo>::iterator
276 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
277 // Group option string.
279 OS << I->first.size() << ", ";
281 OS.write_escaped(I->first) << "\","
282 << std::string(MaxLen-I->first.size()+1, ' ');
284 // Diagnostics in the group.
285 if (I->second.DiagsInGroup.empty())
288 OS << "DiagArray" << I->second.IDNo << ", ";
291 if (I->second.SubGroups.empty())
294 OS << "DiagSubGroup" << I->second.IDNo;
297 OS << "#endif // GET_DIAG_TABLE\n\n";
299 // Emit the category table next.
300 DiagCategoryIDMap CategoriesByID(Records);
301 OS << "\n#ifdef GET_CATEGORY_TABLE\n";
302 for (DiagCategoryIDMap::iterator I = CategoriesByID.begin(),
303 E = CategoriesByID.end(); I != E; ++I)
304 OS << "CATEGORY(\"" << *I << "\")\n";
305 OS << "#endif // GET_CATEGORY_TABLE\n\n";
308 //===----------------------------------------------------------------------===//
309 // Diagnostic name index generation
310 //===----------------------------------------------------------------------===//
313 struct RecordIndexElement
315 RecordIndexElement() {}
316 explicit RecordIndexElement(Record const &R):
322 struct RecordIndexElementSorter :
323 public std::binary_function<RecordIndexElement, RecordIndexElement, bool> {
325 bool operator()(RecordIndexElement const &Lhs,
326 RecordIndexElement const &Rhs) const {
327 return Lhs.Name < Rhs.Name;
332 } // end anonymous namespace.
334 void ClangDiagsIndexNameEmitter::run(raw_ostream &OS) {
335 const std::vector<Record*> &Diags =
336 Records.getAllDerivedDefinitions("Diagnostic");
338 std::vector<RecordIndexElement> Index;
339 Index.reserve(Diags.size());
340 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
341 const Record &R = *(Diags[i]);
342 Index.push_back(RecordIndexElement(R));
345 std::sort(Index.begin(), Index.end(), RecordIndexElementSorter());
347 for (unsigned i = 0, e = Index.size(); i != e; ++i) {
348 const RecordIndexElement &R = Index[i];
350 OS << "DIAG_NAME_INDEX(" << R.Name << ")\n";