Initial work to improve documentation for Clang's diagnostics, from Matthieu Monrocq
[oota-llvm.git] / utils / TableGen / ClangDiagnosticsEmitter.cpp
1 //=- ClangDiagnosticsEmitter.cpp - Generate Clang diagnostics tables -*- C++ -*-
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // These tablegen backends emit Clang diagnostics tables.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "ClangDiagnosticsEmitter.h"
15 #include "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/VectorExtras.h"
22 #include <set>
23 #include <map>
24 #include <algorithm>
25 #include <functional>
26 using namespace llvm;
27
28 //===----------------------------------------------------------------------===//
29 // Diagnostic category computation code.
30 //===----------------------------------------------------------------------===//
31
32 namespace {
33 class DiagGroupParentMap {
34   RecordKeeper &Records;
35   std::map<const Record*, std::vector<Record*> > Mapping;
36 public:
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]);
45     }
46   }
47   
48   const std::vector<Record*> &getParents(const Record *Group) {
49     return Mapping[Group];
50   }
51 };
52 } // end anonymous namespace.
53
54
55 static std::string
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;
61   
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;
68   }
69   return "";
70 }
71
72 /// getDiagnosticCategory - Return the category that the specified diagnostic
73 /// lives in.
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(),
80                                                    DiagGroupParents);
81     if (!CatName.empty()) return CatName;
82   }
83   
84   // If the diagnostic itself has a category, get it.
85   return R->getValueAsString("CategoryName");
86 }
87
88 namespace {
89   class DiagCategoryIDMap {
90     RecordKeeper &Records;
91     StringMap<unsigned> CategoryIDs;
92     std::vector<std::string> CategoryStrings;
93   public:
94     DiagCategoryIDMap(RecordKeeper &records) : Records(records) {
95       DiagGroupParentMap ParentInfo(Records);
96       
97       // The zero'th category is "".
98       CategoryStrings.push_back("");
99       CategoryIDs[""] = 0;
100       
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.
106         
107         unsigned &ID = CategoryIDs[Category];
108         if (ID != 0) continue;  // Already seen.
109         
110         ID = CategoryStrings.size();
111         CategoryStrings.push_back(Category);
112       }
113     }
114     
115     unsigned getID(StringRef CategoryString) {
116       return CategoryIDs[CategoryString];
117     }
118     
119     typedef std::vector<std::string>::iterator iterator;
120     iterator begin() { return CategoryStrings.begin(); }
121     iterator end() { return CategoryStrings.end(); }
122   };
123 } // end anonymous namespace.
124
125
126 //===----------------------------------------------------------------------===//
127 // Warning Tables (.inc file) generation.
128 //===----------------------------------------------------------------------===//
129
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
136        << ",\n";
137     OS << "#undef " << ComponentName << "START\n";
138     OS << "#endif\n\n";
139   }
140
141   const std::vector<Record*> &Diags =
142     Records.getAllDerivedDefinitions("Diagnostic");
143   
144   DiagCategoryIDMap CategoryIDs(Records);
145   DiagGroupParentMap DGParentMap(Records);
146
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"))
151       continue;
152     
153     OS << "DIAG(" << R.getName() << ", ";
154     OS << R.getValueAsDef("Class")->getName();
155     OS << ", diag::" << R.getValueAsDef("DefaultMapping")->getName();
156     
157     // Description string.
158     OS << ", \"";
159     OS.write_escaped(R.getValueAsString("Text")) << '"';
160     
161     // Warning associated with the diagnostic.
162     if (DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Group"))) {
163       OS << ", \"";
164       OS.write_escaped(DI->getDef()->getValueAsString("GroupName")) << '"';
165     } else {
166       OS << ", 0";
167     }
168
169     // SFINAE bit
170     if (R.getValueAsBit("SFINAE"))
171       OS << ", true";
172     else
173       OS << ", false";
174
175     // Access control bit
176     if (R.getValueAsBit("AccessControl"))
177       OS << ", true";
178     else
179       OS << ", false";
180
181     // Category number.
182     OS << ", " << CategoryIDs.getID(getDiagnosticCategory(&R, DGParentMap));
183
184     // Brief
185     OS << ", \"";
186     OS.write_escaped(R.getValueAsString("Brief")) << '"';
187
188     // Explanation 
189     OS << ", \"";
190     OS.write_escaped(R.getValueAsString("Explanation")) << '"';
191     OS << ")\n";
192   }
193 }
194
195 //===----------------------------------------------------------------------===//
196 // Warning Group Tables generation
197 //===----------------------------------------------------------------------===//
198
199 namespace {
200 struct GroupInfo {
201   std::vector<const Record*> DiagsInGroup;
202   std::vector<std::string> SubGroups;
203   unsigned IDNo;
204 };
205 } // end anonymous namespace.
206
207 void ClangDiagGroupsEmitter::run(raw_ostream &OS) {
208   // Compute a mapping from a DiagGroup to all of its parents.
209   DiagGroupParentMap DGParentMap(Records);
210   
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;
214   
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);
223   }
224   
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")];
232     
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"));
236   }
237   
238   // Assign unique ID numbers to the groups.
239   unsigned IDNo = 0;
240   for (std::map<std::string, GroupInfo>::iterator
241        I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I, ++IDNo)
242     I->second.IDNo = IDNo;
243   
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";
247   unsigned MaxLen = 0;
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());
251     
252     std::vector<const Record*> &V = I->second.DiagsInGroup;
253     if (!V.empty()) {
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() << ", ";
257       OS << "-1 };\n";
258     }
259     
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 << ", ";
268       }
269       OS << "-1 };\n";
270     }
271   }
272   OS << "#endif // GET_DIAG_ARRAYS\n\n";
273   
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.
279     OS << "  { \"";
280     OS.write_escaped(I->first) << "\","
281                                << std::string(MaxLen-I->first.size()+1, ' ');
282     
283     // Diagnostics in the group.
284     if (I->second.DiagsInGroup.empty())
285       OS << "0, ";
286     else
287       OS << "DiagArray" << I->second.IDNo << ", ";
288     
289     // Subgroups.
290     if (I->second.SubGroups.empty())
291       OS << 0;
292     else
293       OS << "DiagSubGroup" << I->second.IDNo;
294     OS << " },\n";
295   }
296   OS << "#endif // GET_DIAG_TABLE\n\n";
297   
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";
305 }
306
307 //===----------------------------------------------------------------------===//
308 // Diagnostic name index generation
309 //===----------------------------------------------------------------------===//
310
311 namespace {
312 struct RecordIndexElement
313 {
314   RecordIndexElement() {}
315   explicit RecordIndexElement(Record const &R):
316     Name(R.getName()) {}
317   
318   std::string Name;
319 };
320
321 struct RecordIndexElementSorter :
322   public std::binary_function<RecordIndexElement, RecordIndexElement, bool> {
323   
324   bool operator()(RecordIndexElement const &Lhs,
325                   RecordIndexElement const &Rhs) const {
326     return Lhs.Name < Rhs.Name;
327   }
328   
329 };
330
331 } // end anonymous namespace.
332
333 void ClangDiagsIndexNameEmitter::run(raw_ostream &OS) {
334   const std::vector<Record*> &Diags =
335     Records.getAllDerivedDefinitions("Diagnostic");
336   
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));
342   }
343   
344   std::sort(Index.begin(), Index.end(), RecordIndexElementSorter());
345   
346   for (unsigned i = 0, e = Index.size(); i != e; ++i) {
347     const RecordIndexElement &R = Index[i];
348     
349     OS << "DIAG_NAME_INDEX(" << R.Name << ")\n";
350   }
351 }