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"
15 #include "llvm/TableGen/Record.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/SmallString.h"
22 #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"))
181 // FIXME: This condition is just to avoid temporary revlock, it can be
183 if (R.getValue("WarningNoWerror")) {
184 // Default warning has no Werror bit.
185 if (R.getValueAsBit("WarningNoWerror"))
190 // Default warning show in system header bit.
191 if (R.getValueAsBit("WarningShowInSystemHeader"))
198 OS << ", " << CategoryIDs.getID(getDiagnosticCategory(&R, DGParentMap));
202 OS.write_escaped(R.getValueAsString("Brief")) << '"';
206 OS.write_escaped(R.getValueAsString("Explanation")) << '"';
211 //===----------------------------------------------------------------------===//
212 // Warning Group Tables generation
213 //===----------------------------------------------------------------------===//
215 static std::string getDiagCategoryEnum(llvm::StringRef name) {
217 return "DiagCat_None";
218 llvm::SmallString<256> enumName = llvm::StringRef("DiagCat_");
219 for (llvm::StringRef::iterator I = name.begin(), E = name.end(); I != E; ++I)
220 enumName += isalnum(*I) ? *I : '_';
221 return enumName.str();
226 std::vector<const Record*> DiagsInGroup;
227 std::vector<std::string> SubGroups;
230 } // end anonymous namespace.
232 void ClangDiagGroupsEmitter::run(raw_ostream &OS) {
233 // Compute a mapping from a DiagGroup to all of its parents.
234 DiagGroupParentMap DGParentMap(Records);
236 // Invert the 1-[0/1] mapping of diags to group into a one to many mapping of
237 // groups to diags in the group.
238 std::map<std::string, GroupInfo> DiagsInGroup;
240 std::vector<Record*> Diags =
241 Records.getAllDerivedDefinitions("Diagnostic");
242 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
243 const Record *R = Diags[i];
244 DefInit *DI = dynamic_cast<DefInit*>(R->getValueInit("Group"));
245 if (DI == 0) continue;
246 std::string GroupName = DI->getDef()->getValueAsString("GroupName");
247 DiagsInGroup[GroupName].DiagsInGroup.push_back(R);
250 // Add all DiagGroup's to the DiagsInGroup list to make sure we pick up empty
251 // groups (these are warnings that GCC supports that clang never produces).
252 std::vector<Record*> DiagGroups
253 = Records.getAllDerivedDefinitions("DiagGroup");
254 for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
255 Record *Group = DiagGroups[i];
256 GroupInfo &GI = DiagsInGroup[Group->getValueAsString("GroupName")];
258 std::vector<Record*> SubGroups = Group->getValueAsListOfDefs("SubGroups");
259 for (unsigned j = 0, e = SubGroups.size(); j != e; ++j)
260 GI.SubGroups.push_back(SubGroups[j]->getValueAsString("GroupName"));
263 // Assign unique ID numbers to the groups.
265 for (std::map<std::string, GroupInfo>::iterator
266 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I, ++IDNo)
267 I->second.IDNo = IDNo;
269 // Walk through the groups emitting an array for each diagnostic of the diags
270 // that are mapped to.
271 OS << "\n#ifdef GET_DIAG_ARRAYS\n";
273 for (std::map<std::string, GroupInfo>::iterator
274 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
275 MaxLen = std::max(MaxLen, (unsigned)I->first.size());
277 std::vector<const Record*> &V = I->second.DiagsInGroup;
279 OS << "static const short DiagArray" << I->second.IDNo << "[] = { ";
280 for (unsigned i = 0, e = V.size(); i != e; ++i)
281 OS << "diag::" << V[i]->getName() << ", ";
285 const std::vector<std::string> &SubGroups = I->second.SubGroups;
286 if (!SubGroups.empty()) {
287 OS << "static const short DiagSubGroup" << I->second.IDNo << "[] = { ";
288 for (unsigned i = 0, e = SubGroups.size(); i != e; ++i) {
289 std::map<std::string, GroupInfo>::iterator RI =
290 DiagsInGroup.find(SubGroups[i]);
291 assert(RI != DiagsInGroup.end() && "Referenced without existing?");
292 OS << RI->second.IDNo << ", ";
297 OS << "#endif // GET_DIAG_ARRAYS\n\n";
299 // Emit the table now.
300 OS << "\n#ifdef GET_DIAG_TABLE\n";
301 for (std::map<std::string, GroupInfo>::iterator
302 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
303 // Group option string.
305 OS << I->first.size() << ", ";
307 OS.write_escaped(I->first) << "\","
308 << std::string(MaxLen-I->first.size()+1, ' ');
310 // Diagnostics in the group.
311 if (I->second.DiagsInGroup.empty())
314 OS << "DiagArray" << I->second.IDNo << ", ";
317 if (I->second.SubGroups.empty())
320 OS << "DiagSubGroup" << I->second.IDNo;
323 OS << "#endif // GET_DIAG_TABLE\n\n";
325 // Emit the category table next.
326 DiagCategoryIDMap CategoriesByID(Records);
327 OS << "\n#ifdef GET_CATEGORY_TABLE\n";
328 for (DiagCategoryIDMap::iterator I = CategoriesByID.begin(),
329 E = CategoriesByID.end(); I != E; ++I)
330 OS << "CATEGORY(\"" << *I << "\", " << getDiagCategoryEnum(*I) << ")\n";
331 OS << "#endif // GET_CATEGORY_TABLE\n\n";
334 //===----------------------------------------------------------------------===//
335 // Diagnostic name index generation
336 //===----------------------------------------------------------------------===//
339 struct RecordIndexElement
341 RecordIndexElement() {}
342 explicit RecordIndexElement(Record const &R):
348 struct RecordIndexElementSorter :
349 public std::binary_function<RecordIndexElement, RecordIndexElement, bool> {
351 bool operator()(RecordIndexElement const &Lhs,
352 RecordIndexElement const &Rhs) const {
353 return Lhs.Name < Rhs.Name;
358 } // end anonymous namespace.
360 void ClangDiagsIndexNameEmitter::run(raw_ostream &OS) {
361 const std::vector<Record*> &Diags =
362 Records.getAllDerivedDefinitions("Diagnostic");
364 std::vector<RecordIndexElement> Index;
365 Index.reserve(Diags.size());
366 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
367 const Record &R = *(Diags[i]);
368 Index.push_back(RecordIndexElement(R));
371 std::sort(Index.begin(), Index.end(), RecordIndexElementSorter());
373 for (unsigned i = 0, e = Index.size(); i != e; ++i) {
374 const RecordIndexElement &R = Index[i];
376 OS << "DIAG_NAME_INDEX(" << R.Name << ")\n";