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"
26 //===----------------------------------------------------------------------===//
27 // Diagnostic category computation code.
28 //===----------------------------------------------------------------------===//
31 class DiagGroupParentMap {
32 std::map<const Record*, std::vector<Record*> > Mapping;
34 DiagGroupParentMap() {
35 std::vector<Record*> DiagGroups
36 = Records.getAllDerivedDefinitions("DiagGroup");
37 for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
38 std::vector<Record*> SubGroups =
39 DiagGroups[i]->getValueAsListOfDefs("SubGroups");
40 for (unsigned j = 0, e = SubGroups.size(); j != e; ++j)
41 Mapping[SubGroups[j]].push_back(DiagGroups[i]);
45 const std::vector<Record*> &getParents(const Record *Group) {
46 return Mapping[Group];
49 } // end anonymous namespace.
53 getCategoryFromDiagGroup(const Record *Group,
54 DiagGroupParentMap &DiagGroupParents) {
55 // If the DiagGroup has a category, return it.
56 std::string CatName = Group->getValueAsString("CategoryName");
57 if (!CatName.empty()) return CatName;
59 // The diag group may the subgroup of one or more other diagnostic groups,
60 // check these for a category as well.
61 const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
62 for (unsigned i = 0, e = Parents.size(); i != e; ++i) {
63 CatName = getCategoryFromDiagGroup(Parents[i], DiagGroupParents);
64 if (!CatName.empty()) return CatName;
69 /// getDiagnosticCategory - Return the category that the specified diagnostic
71 static std::string getDiagnosticCategory(const Record *R,
72 DiagGroupParentMap &DiagGroupParents) {
73 // If the diagnostic itself has a category, get it.
74 std::string CatName = R->getValueAsString("CategoryName");
75 if (!CatName.empty()) return CatName;
77 DefInit *Group = dynamic_cast<DefInit*>(R->getValueInit("Group"));
78 if (Group == 0) return "";
80 // Check the diagnostic's diag group for a category.
81 return getCategoryFromDiagGroup(Group->getDef(), DiagGroupParents);
85 class DiagCategoryIDMap {
86 StringMap<unsigned> CategoryIDs;
87 std::vector<std::string> CategoryStrings;
90 DiagGroupParentMap ParentInfo;
92 // The zero'th category is "".
93 CategoryStrings.push_back("");
96 std::vector<Record*> Diags =
97 Records.getAllDerivedDefinitions("Diagnostic");
98 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
99 std::string Category = getDiagnosticCategory(Diags[i], ParentInfo);
100 if (Category.empty()) continue; // Skip diags with no category.
102 unsigned &ID = CategoryIDs[Category];
103 if (ID != 0) continue; // Already seen.
105 ID = CategoryStrings.size();
106 CategoryStrings.push_back(Category);
110 unsigned getID(StringRef CategoryString) {
111 return CategoryIDs[CategoryString];
114 typedef std::vector<std::string>::iterator iterator;
115 iterator begin() { return CategoryStrings.begin(); }
116 iterator end() { return CategoryStrings.end(); }
118 } // end anonymous namespace.
122 //===----------------------------------------------------------------------===//
123 // Warning Tables (.inc file) generation.
124 //===----------------------------------------------------------------------===//
126 void ClangDiagsDefsEmitter::run(raw_ostream &OS) {
127 // Write the #if guard
128 if (!Component.empty()) {
129 std::string ComponentName = UppercaseString(Component);
130 OS << "#ifdef " << ComponentName << "START\n";
131 OS << "__" << ComponentName << "START = DIAG_START_" << ComponentName
133 OS << "#undef " << ComponentName << "START\n";
137 const std::vector<Record*> &Diags =
138 Records.getAllDerivedDefinitions("Diagnostic");
140 DiagCategoryIDMap CategoryIDs;
141 DiagGroupParentMap DGParentMap;
143 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
144 const Record &R = *Diags[i];
145 // Filter by component.
146 if (!Component.empty() && Component != R.getValueAsString("Component"))
149 OS << "DIAG(" << R.getName() << ", ";
150 OS << R.getValueAsDef("Class")->getName();
151 OS << ", diag::" << R.getValueAsDef("DefaultMapping")->getName();
153 // Description string.
155 OS.write_escaped(R.getValueAsString("Text")) << '"';
157 // Warning associated with the diagnostic.
158 if (DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Group"))) {
160 OS.write_escaped(DI->getDef()->getValueAsString("GroupName")) << '"';
166 if (R.getValueAsBit("SFINAE"))
172 OS << ", " << CategoryIDs.getID(getDiagnosticCategory(&R, DGParentMap));
177 //===----------------------------------------------------------------------===//
178 // Warning Group Tables generation
179 //===----------------------------------------------------------------------===//
182 std::vector<const Record*> DiagsInGroup;
183 std::vector<std::string> SubGroups;
187 void ClangDiagGroupsEmitter::run(raw_ostream &OS) {
188 // Compute a mapping from a DiagGroup to all of its parents.
189 DiagGroupParentMap DGParentMap;
191 // Invert the 1-[0/1] mapping of diags to group into a one to many mapping of
192 // groups to diags in the group.
193 std::map<std::string, GroupInfo> DiagsInGroup;
195 std::vector<Record*> Diags =
196 Records.getAllDerivedDefinitions("Diagnostic");
197 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
198 const Record *R = Diags[i];
199 DefInit *DI = dynamic_cast<DefInit*>(R->getValueInit("Group"));
200 if (DI == 0) continue;
201 std::string GroupName = DI->getDef()->getValueAsString("GroupName");
202 DiagsInGroup[GroupName].DiagsInGroup.push_back(R);
205 // Add all DiagGroup's to the DiagsInGroup list to make sure we pick up empty
206 // groups (these are warnings that GCC supports that clang never produces).
207 std::vector<Record*> DiagGroups
208 = Records.getAllDerivedDefinitions("DiagGroup");
209 for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
210 Record *Group = DiagGroups[i];
211 GroupInfo &GI = DiagsInGroup[Group->getValueAsString("GroupName")];
213 std::vector<Record*> SubGroups = Group->getValueAsListOfDefs("SubGroups");
214 for (unsigned j = 0, e = SubGroups.size(); j != e; ++j)
215 GI.SubGroups.push_back(SubGroups[j]->getValueAsString("GroupName"));
218 // Assign unique ID numbers to the groups.
220 for (std::map<std::string, GroupInfo>::iterator
221 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I, ++IDNo)
222 I->second.IDNo = IDNo;
224 // Walk through the groups emitting an array for each diagnostic of the diags
225 // that are mapped to.
226 OS << "\n#ifdef GET_DIAG_ARRAYS\n";
228 for (std::map<std::string, GroupInfo>::iterator
229 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
230 MaxLen = std::max(MaxLen, (unsigned)I->first.size());
232 std::vector<const Record*> &V = I->second.DiagsInGroup;
234 OS << "static const short DiagArray" << I->second.IDNo << "[] = { ";
235 for (unsigned i = 0, e = V.size(); i != e; ++i)
236 OS << "diag::" << V[i]->getName() << ", ";
240 const std::vector<std::string> &SubGroups = I->second.SubGroups;
241 if (!SubGroups.empty()) {
242 OS << "static const short DiagSubGroup" << I->second.IDNo << "[] = { ";
243 for (unsigned i = 0, e = SubGroups.size(); i != e; ++i) {
244 std::map<std::string, GroupInfo>::iterator RI =
245 DiagsInGroup.find(SubGroups[i]);
246 assert(RI != DiagsInGroup.end() && "Referenced without existing?");
247 OS << RI->second.IDNo << ", ";
252 OS << "#endif // GET_DIAG_ARRAYS\n\n";
254 // Emit the table now.
255 OS << "\n#ifdef GET_DIAG_TABLE\n";
256 for (std::map<std::string, GroupInfo>::iterator
257 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
258 // Group option string.
260 OS.write_escaped(I->first) << "\","
261 << std::string(MaxLen-I->first.size()+1, ' ');
263 // Diagnostics in the group.
264 if (I->second.DiagsInGroup.empty())
267 OS << "DiagArray" << I->second.IDNo << ", ";
270 if (I->second.SubGroups.empty())
273 OS << "DiagSubGroup" << I->second.IDNo;
276 OS << "#endif // GET_DIAG_TABLE\n\n";
278 // Emit the category table next.
279 DiagCategoryIDMap CategoriesByID;
280 OS << "\n#ifdef GET_CATEGORY_TABLE\n";
281 for (DiagCategoryIDMap::iterator I = CategoriesByID.begin(),
282 E = CategoriesByID.end(); I != E; ++I)
283 OS << "CATEGORY(\"" << *I << "\")\n";
284 OS << "#endif // GET_CATEGORY_TABLE\n\n";