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"
28 //===----------------------------------------------------------------------===//
29 // Diagnostic category computation code.
30 //===----------------------------------------------------------------------===//
33 class DiagGroupParentMap {
34 RecordKeeper &Records;
35 std::map<const Record*, std::vector<Record*> > Mapping;
37 DiagGroupParentMap(RecordKeeper &records) : Records(records) {
38 std::vector<Record*> DiagGroups
39 = Records.getAllDerivedDefinitions("DiagGroup");
40 for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
41 std::vector<Record*> SubGroups =
42 DiagGroups[i]->getValueAsListOfDefs("SubGroups");
43 for (unsigned j = 0, e = SubGroups.size(); j != e; ++j)
44 Mapping[SubGroups[j]].push_back(DiagGroups[i]);
48 const std::vector<Record*> &getParents(const Record *Group) {
49 return Mapping[Group];
52 } // end anonymous namespace.
56 getCategoryFromDiagGroup(const Record *Group,
57 DiagGroupParentMap &DiagGroupParents) {
58 // If the DiagGroup has a category, return it.
59 std::string CatName = Group->getValueAsString("CategoryName");
60 if (!CatName.empty()) return CatName;
62 // The diag group may the subgroup of one or more other diagnostic groups,
63 // check these for a category as well.
64 const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
65 for (unsigned i = 0, e = Parents.size(); i != e; ++i) {
66 CatName = getCategoryFromDiagGroup(Parents[i], DiagGroupParents);
67 if (!CatName.empty()) return CatName;
72 /// getDiagnosticCategory - Return the category that the specified diagnostic
74 static std::string getDiagnosticCategory(const Record *R,
75 DiagGroupParentMap &DiagGroupParents) {
76 // If the diagnostic is in a group, and that group has a category, use it.
77 if (DefInit *Group = dynamic_cast<DefInit*>(R->getValueInit("Group"))) {
78 // Check the diagnostic's diag group for a category.
79 std::string CatName = getCategoryFromDiagGroup(Group->getDef(),
81 if (!CatName.empty()) return CatName;
84 // If the diagnostic itself has a category, get it.
85 return R->getValueAsString("CategoryName");
89 class DiagCategoryIDMap {
90 RecordKeeper &Records;
91 StringMap<unsigned> CategoryIDs;
92 std::vector<std::string> CategoryStrings;
94 DiagCategoryIDMap(RecordKeeper &records) : Records(records) {
95 DiagGroupParentMap ParentInfo(Records);
97 // The zero'th category is "".
98 CategoryStrings.push_back("");
101 std::vector<Record*> Diags =
102 Records.getAllDerivedDefinitions("Diagnostic");
103 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
104 std::string Category = getDiagnosticCategory(Diags[i], ParentInfo);
105 if (Category.empty()) continue; // Skip diags with no category.
107 unsigned &ID = CategoryIDs[Category];
108 if (ID != 0) continue; // Already seen.
110 ID = CategoryStrings.size();
111 CategoryStrings.push_back(Category);
115 unsigned getID(StringRef CategoryString) {
116 return CategoryIDs[CategoryString];
119 typedef std::vector<std::string>::iterator iterator;
120 iterator begin() { return CategoryStrings.begin(); }
121 iterator end() { return CategoryStrings.end(); }
123 } // end anonymous namespace.
126 //===----------------------------------------------------------------------===//
127 // Warning Tables (.inc file) generation.
128 //===----------------------------------------------------------------------===//
130 void ClangDiagsDefsEmitter::run(raw_ostream &OS) {
131 // Write the #if guard
132 if (!Component.empty()) {
133 std::string ComponentName = UppercaseString(Component);
134 OS << "#ifdef " << ComponentName << "START\n";
135 OS << "__" << ComponentName << "START = DIAG_START_" << ComponentName
137 OS << "#undef " << ComponentName << "START\n";
141 const std::vector<Record*> &Diags =
142 Records.getAllDerivedDefinitions("Diagnostic");
144 DiagCategoryIDMap CategoryIDs(Records);
145 DiagGroupParentMap DGParentMap(Records);
147 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
148 const Record &R = *Diags[i];
149 // Filter by component.
150 if (!Component.empty() && Component != R.getValueAsString("Component"))
153 OS << "DIAG(" << R.getName() << ", ";
154 OS << R.getValueAsDef("Class")->getName();
155 OS << ", diag::" << R.getValueAsDef("DefaultMapping")->getName();
157 // Description string.
159 OS.write_escaped(R.getValueAsString("Text")) << '"';
161 // Warning associated with the diagnostic.
162 if (DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Group"))) {
164 OS.write_escaped(DI->getDef()->getValueAsString("GroupName")) << '"';
170 if (R.getValueAsBit("SFINAE"))
175 // Access control bit
176 if (R.getValueAsBit("AccessControl"))
182 OS << ", " << CategoryIDs.getID(getDiagnosticCategory(&R, DGParentMap));
186 OS.write_escaped(R.getValueAsString("Brief")) << '"';
190 OS.write_escaped(R.getValueAsString("Explanation")) << '"';
195 //===----------------------------------------------------------------------===//
196 // Warning Group Tables generation
197 //===----------------------------------------------------------------------===//
201 std::vector<const Record*> DiagsInGroup;
202 std::vector<std::string> SubGroups;
205 } // end anonymous namespace.
207 void ClangDiagGroupsEmitter::run(raw_ostream &OS) {
208 // Compute a mapping from a DiagGroup to all of its parents.
209 DiagGroupParentMap DGParentMap(Records);
211 // Invert the 1-[0/1] mapping of diags to group into a one to many mapping of
212 // groups to diags in the group.
213 std::map<std::string, GroupInfo> DiagsInGroup;
215 std::vector<Record*> Diags =
216 Records.getAllDerivedDefinitions("Diagnostic");
217 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
218 const Record *R = Diags[i];
219 DefInit *DI = dynamic_cast<DefInit*>(R->getValueInit("Group"));
220 if (DI == 0) continue;
221 std::string GroupName = DI->getDef()->getValueAsString("GroupName");
222 DiagsInGroup[GroupName].DiagsInGroup.push_back(R);
225 // Add all DiagGroup's to the DiagsInGroup list to make sure we pick up empty
226 // groups (these are warnings that GCC supports that clang never produces).
227 std::vector<Record*> DiagGroups
228 = Records.getAllDerivedDefinitions("DiagGroup");
229 for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
230 Record *Group = DiagGroups[i];
231 GroupInfo &GI = DiagsInGroup[Group->getValueAsString("GroupName")];
233 std::vector<Record*> SubGroups = Group->getValueAsListOfDefs("SubGroups");
234 for (unsigned j = 0, e = SubGroups.size(); j != e; ++j)
235 GI.SubGroups.push_back(SubGroups[j]->getValueAsString("GroupName"));
238 // Assign unique ID numbers to the groups.
240 for (std::map<std::string, GroupInfo>::iterator
241 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I, ++IDNo)
242 I->second.IDNo = IDNo;
244 // Walk through the groups emitting an array for each diagnostic of the diags
245 // that are mapped to.
246 OS << "\n#ifdef GET_DIAG_ARRAYS\n";
248 for (std::map<std::string, GroupInfo>::iterator
249 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
250 MaxLen = std::max(MaxLen, (unsigned)I->first.size());
252 std::vector<const Record*> &V = I->second.DiagsInGroup;
254 OS << "static const short DiagArray" << I->second.IDNo << "[] = { ";
255 for (unsigned i = 0, e = V.size(); i != e; ++i)
256 OS << "diag::" << V[i]->getName() << ", ";
260 const std::vector<std::string> &SubGroups = I->second.SubGroups;
261 if (!SubGroups.empty()) {
262 OS << "static const short DiagSubGroup" << I->second.IDNo << "[] = { ";
263 for (unsigned i = 0, e = SubGroups.size(); i != e; ++i) {
264 std::map<std::string, GroupInfo>::iterator RI =
265 DiagsInGroup.find(SubGroups[i]);
266 assert(RI != DiagsInGroup.end() && "Referenced without existing?");
267 OS << RI->second.IDNo << ", ";
272 OS << "#endif // GET_DIAG_ARRAYS\n\n";
274 // Emit the table now.
275 OS << "\n#ifdef GET_DIAG_TABLE\n";
276 for (std::map<std::string, GroupInfo>::iterator
277 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
278 // Group option string.
280 OS.write_escaped(I->first) << "\","
281 << std::string(MaxLen-I->first.size()+1, ' ');
283 // Diagnostics in the group.
284 if (I->second.DiagsInGroup.empty())
287 OS << "DiagArray" << I->second.IDNo << ", ";
290 if (I->second.SubGroups.empty())
293 OS << "DiagSubGroup" << I->second.IDNo;
296 OS << "#endif // GET_DIAG_TABLE\n\n";
298 // Emit the category table next.
299 DiagCategoryIDMap CategoriesByID(Records);
300 OS << "\n#ifdef GET_CATEGORY_TABLE\n";
301 for (DiagCategoryIDMap::iterator I = CategoriesByID.begin(),
302 E = CategoriesByID.end(); I != E; ++I)
303 OS << "CATEGORY(\"" << *I << "\")\n";
304 OS << "#endif // GET_CATEGORY_TABLE\n\n";
307 //===----------------------------------------------------------------------===//
308 // Diagnostic name index generation
309 //===----------------------------------------------------------------------===//
312 struct RecordIndexElement
314 RecordIndexElement() {}
315 explicit RecordIndexElement(Record const &R):
321 struct RecordIndexElementSorter :
322 public std::binary_function<RecordIndexElement, RecordIndexElement, bool> {
324 bool operator()(RecordIndexElement const &Lhs,
325 RecordIndexElement const &Rhs) const {
326 return Lhs.Name < Rhs.Name;
331 } // end anonymous namespace.
333 void ClangDiagsIndexNameEmitter::run(raw_ostream &OS) {
334 const std::vector<Record*> &Diags =
335 Records.getAllDerivedDefinitions("Diagnostic");
337 std::vector<RecordIndexElement> Index;
338 Index.reserve(Diags.size());
339 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
340 const Record &R = *(Diags[i]);
341 Index.push_back(RecordIndexElement(R));
344 std::sort(Index.begin(), Index.end(), RecordIndexElementSorter());
346 for (unsigned i = 0, e = Index.size(); i != e; ++i) {
347 const RecordIndexElement &R = Index[i];
349 OS << "DIAG_NAME_INDEX(" << R.Name << ")\n";