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/StringMap.h"
20 #include "llvm/ADT/SmallString.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.
54 getCategoryFromDiagGroup(const Record *Group,
55 DiagGroupParentMap &DiagGroupParents) {
56 // If the DiagGroup has a category, return it.
57 std::string CatName = Group->getValueAsString("CategoryName");
58 if (!CatName.empty()) return CatName;
60 // The diag group may the subgroup of one or more other diagnostic groups,
61 // check these for a category as well.
62 const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
63 for (unsigned i = 0, e = Parents.size(); i != e; ++i) {
64 CatName = getCategoryFromDiagGroup(Parents[i], DiagGroupParents);
65 if (!CatName.empty()) return CatName;
70 /// getDiagnosticCategory - Return the category that the specified diagnostic
72 static std::string getDiagnosticCategory(const Record *R,
73 DiagGroupParentMap &DiagGroupParents) {
74 // If the diagnostic is in a group, and that group has a category, use it.
75 if (DefInit *Group = dynamic_cast<DefInit*>(R->getValueInit("Group"))) {
76 // Check the diagnostic's diag group for a category.
77 std::string CatName = getCategoryFromDiagGroup(Group->getDef(),
79 if (!CatName.empty()) return CatName;
82 // If the diagnostic itself has a category, get it.
83 return R->getValueAsString("CategoryName");
87 class DiagCategoryIDMap {
88 RecordKeeper &Records;
89 StringMap<unsigned> CategoryIDs;
90 std::vector<std::string> CategoryStrings;
92 DiagCategoryIDMap(RecordKeeper &records) : Records(records) {
93 DiagGroupParentMap ParentInfo(Records);
95 // The zero'th category is "".
96 CategoryStrings.push_back("");
99 std::vector<Record*> Diags =
100 Records.getAllDerivedDefinitions("Diagnostic");
101 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
102 std::string Category = getDiagnosticCategory(Diags[i], ParentInfo);
103 if (Category.empty()) continue; // Skip diags with no category.
105 unsigned &ID = CategoryIDs[Category];
106 if (ID != 0) continue; // Already seen.
108 ID = CategoryStrings.size();
109 CategoryStrings.push_back(Category);
113 unsigned getID(StringRef CategoryString) {
114 return CategoryIDs[CategoryString];
117 typedef std::vector<std::string>::iterator iterator;
118 iterator begin() { return CategoryStrings.begin(); }
119 iterator end() { return CategoryStrings.end(); }
123 std::vector<const Record*> DiagsInGroup;
124 std::vector<std::string> SubGroups;
127 } // end anonymous namespace.
129 /// \brief Invert the 1-[0/1] mapping of diags to group into a one to many
130 /// mapping of groups to diags in the group.
131 static void groupDiagnostics(const std::vector<Record*> &Diags,
132 const std::vector<Record*> &DiagGroups,
133 std::map<std::string, GroupInfo> &DiagsInGroup) {
134 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
135 const Record *R = Diags[i];
136 DefInit *DI = dynamic_cast<DefInit*>(R->getValueInit("Group"));
137 if (DI == 0) continue;
138 std::string GroupName = DI->getDef()->getValueAsString("GroupName");
139 DiagsInGroup[GroupName].DiagsInGroup.push_back(R);
142 // Add all DiagGroup's to the DiagsInGroup list to make sure we pick up empty
143 // groups (these are warnings that GCC supports that clang never produces).
144 for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
145 Record *Group = DiagGroups[i];
146 GroupInfo &GI = DiagsInGroup[Group->getValueAsString("GroupName")];
148 std::vector<Record*> SubGroups = Group->getValueAsListOfDefs("SubGroups");
149 for (unsigned j = 0, e = SubGroups.size(); j != e; ++j)
150 GI.SubGroups.push_back(SubGroups[j]->getValueAsString("GroupName"));
153 // Assign unique ID numbers to the groups.
155 for (std::map<std::string, GroupInfo>::iterator
156 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I, ++IDNo)
157 I->second.IDNo = IDNo;
160 //===----------------------------------------------------------------------===//
161 // Warning Tables (.inc file) generation.
162 //===----------------------------------------------------------------------===//
164 void ClangDiagsDefsEmitter::run(raw_ostream &OS) {
165 // Write the #if guard
166 if (!Component.empty()) {
167 std::string ComponentName = StringRef(Component).upper();
168 OS << "#ifdef " << ComponentName << "START\n";
169 OS << "__" << ComponentName << "START = DIAG_START_" << ComponentName
171 OS << "#undef " << ComponentName << "START\n";
175 const std::vector<Record*> &Diags =
176 Records.getAllDerivedDefinitions("Diagnostic");
178 std::vector<Record*> DiagGroups
179 = Records.getAllDerivedDefinitions("DiagGroup");
181 std::map<std::string, GroupInfo> DiagsInGroup;
182 groupDiagnostics(Diags, DiagGroups, DiagsInGroup);
184 DiagCategoryIDMap CategoryIDs(Records);
185 DiagGroupParentMap DGParentMap(Records);
187 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
188 const Record &R = *Diags[i];
189 // Filter by component.
190 if (!Component.empty() && Component != R.getValueAsString("Component"))
193 OS << "DIAG(" << R.getName() << ", ";
194 OS << R.getValueAsDef("Class")->getName();
195 OS << ", diag::" << R.getValueAsDef("DefaultMapping")->getName();
197 // Description string.
199 OS.write_escaped(R.getValueAsString("Text")) << '"';
201 // Warning associated with the diagnostic. This is stored as an index into
202 // the alphabetically sorted warning table.
203 if (DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Group"))) {
204 std::map<std::string, GroupInfo>::iterator I =
205 DiagsInGroup.find(DI->getDef()->getValueAsString("GroupName"));
206 assert(I != DiagsInGroup.end());
207 OS << ", " << I->second.IDNo;
213 if (R.getValueAsBit("SFINAE"))
218 // Access control bit
219 if (R.getValueAsBit("AccessControl"))
224 // FIXME: This condition is just to avoid temporary revlock, it can be
226 if (R.getValue("WarningNoWerror")) {
227 // Default warning has no Werror bit.
228 if (R.getValueAsBit("WarningNoWerror"))
233 // Default warning show in system header bit.
234 if (R.getValueAsBit("WarningShowInSystemHeader"))
241 OS << ", " << CategoryIDs.getID(getDiagnosticCategory(&R, DGParentMap));
246 //===----------------------------------------------------------------------===//
247 // Warning Group Tables generation
248 //===----------------------------------------------------------------------===//
250 static std::string getDiagCategoryEnum(llvm::StringRef name) {
252 return "DiagCat_None";
253 SmallString<256> enumName = llvm::StringRef("DiagCat_");
254 for (llvm::StringRef::iterator I = name.begin(), E = name.end(); I != E; ++I)
255 enumName += isalnum(*I) ? *I : '_';
256 return enumName.str();
259 void ClangDiagGroupsEmitter::run(raw_ostream &OS) {
260 // Compute a mapping from a DiagGroup to all of its parents.
261 DiagGroupParentMap DGParentMap(Records);
263 std::vector<Record*> Diags =
264 Records.getAllDerivedDefinitions("Diagnostic");
266 std::vector<Record*> DiagGroups
267 = Records.getAllDerivedDefinitions("DiagGroup");
269 std::map<std::string, GroupInfo> DiagsInGroup;
270 groupDiagnostics(Diags, DiagGroups, DiagsInGroup);
272 // Walk through the groups emitting an array for each diagnostic of the diags
273 // that are mapped to.
274 OS << "\n#ifdef GET_DIAG_ARRAYS\n";
276 for (std::map<std::string, GroupInfo>::iterator
277 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
278 MaxLen = std::max(MaxLen, (unsigned)I->first.size());
280 std::vector<const Record*> &V = I->second.DiagsInGroup;
282 OS << "static const short DiagArray" << I->second.IDNo << "[] = { ";
283 for (unsigned i = 0, e = V.size(); i != e; ++i)
284 OS << "diag::" << V[i]->getName() << ", ";
288 const std::vector<std::string> &SubGroups = I->second.SubGroups;
289 if (!SubGroups.empty()) {
290 OS << "static const short DiagSubGroup" << I->second.IDNo << "[] = { ";
291 for (unsigned i = 0, e = SubGroups.size(); i != e; ++i) {
292 std::map<std::string, GroupInfo>::iterator RI =
293 DiagsInGroup.find(SubGroups[i]);
294 assert(RI != DiagsInGroup.end() && "Referenced without existing?");
295 OS << RI->second.IDNo << ", ";
300 OS << "#endif // GET_DIAG_ARRAYS\n\n";
302 // Emit the table now.
303 OS << "\n#ifdef GET_DIAG_TABLE\n";
304 for (std::map<std::string, GroupInfo>::iterator
305 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
306 // Group option string.
308 OS << I->first.size() << ", ";
310 if (I->first.find_first_not_of("abcdefghijklmnopqrstuvwxyz"
311 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
312 "0123456789!@#$%^*-+=:?")!=std::string::npos)
313 throw "Invalid character in diagnostic group '" + I->first + "'";
314 OS.write_escaped(I->first) << "\","
315 << std::string(MaxLen-I->first.size()+1, ' ');
317 // Diagnostics in the group.
318 if (I->second.DiagsInGroup.empty())
321 OS << "DiagArray" << I->second.IDNo << ", ";
324 if (I->second.SubGroups.empty())
327 OS << "DiagSubGroup" << I->second.IDNo;
330 OS << "#endif // GET_DIAG_TABLE\n\n";
332 // Emit the category table next.
333 DiagCategoryIDMap CategoriesByID(Records);
334 OS << "\n#ifdef GET_CATEGORY_TABLE\n";
335 for (DiagCategoryIDMap::iterator I = CategoriesByID.begin(),
336 E = CategoriesByID.end(); I != E; ++I)
337 OS << "CATEGORY(\"" << *I << "\", " << getDiagCategoryEnum(*I) << ")\n";
338 OS << "#endif // GET_CATEGORY_TABLE\n\n";
341 //===----------------------------------------------------------------------===//
342 // Diagnostic name index generation
343 //===----------------------------------------------------------------------===//
346 struct RecordIndexElement
348 RecordIndexElement() {}
349 explicit RecordIndexElement(Record const &R):
355 struct RecordIndexElementSorter :
356 public std::binary_function<RecordIndexElement, RecordIndexElement, bool> {
358 bool operator()(RecordIndexElement const &Lhs,
359 RecordIndexElement const &Rhs) const {
360 return Lhs.Name < Rhs.Name;
365 } // end anonymous namespace.
367 void ClangDiagsIndexNameEmitter::run(raw_ostream &OS) {
368 const std::vector<Record*> &Diags =
369 Records.getAllDerivedDefinitions("Diagnostic");
371 std::vector<RecordIndexElement> Index;
372 Index.reserve(Diags.size());
373 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
374 const Record &R = *(Diags[i]);
375 Index.push_back(RecordIndexElement(R));
378 std::sort(Index.begin(), Index.end(), RecordIndexElementSorter());
380 for (unsigned i = 0, e = Index.size(); i != e; ++i) {
381 const RecordIndexElement &R = Index[i];
383 OS << "DIAG_NAME_INDEX(" << R.Name << ")\n";