]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/utils/TableGen/ClangDiagnosticsEmitter.cpp
Copy head to stable/9 as part of 9.0-RELEASE release cycle.
[FreeBSD/stable/9.git] / contrib / llvm / 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/SmallString.h"
22 #include "llvm/ADT/VectorExtras.h"
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 << ", \"\"";
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 static std::string getDiagCategoryEnum(llvm::StringRef name) {
200   if (name.empty())
201     return "DiagCat_None";
202   llvm::SmallString<256> enumName = llvm::StringRef("DiagCat_");
203   for (llvm::StringRef::iterator I = name.begin(), E = name.end(); I != E; ++I)
204     enumName += isalnum(*I) ? *I : '_';
205   return enumName.str();
206 }
207
208 namespace {
209 struct GroupInfo {
210   std::vector<const Record*> DiagsInGroup;
211   std::vector<std::string> SubGroups;
212   unsigned IDNo;
213 };
214 } // end anonymous namespace.
215
216 void ClangDiagGroupsEmitter::run(raw_ostream &OS) {
217   // Compute a mapping from a DiagGroup to all of its parents.
218   DiagGroupParentMap DGParentMap(Records);
219   
220   // Invert the 1-[0/1] mapping of diags to group into a one to many mapping of
221   // groups to diags in the group.
222   std::map<std::string, GroupInfo> DiagsInGroup;
223   
224   std::vector<Record*> Diags =
225     Records.getAllDerivedDefinitions("Diagnostic");
226   for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
227     const Record *R = Diags[i];
228     DefInit *DI = dynamic_cast<DefInit*>(R->getValueInit("Group"));
229     if (DI == 0) continue;
230     std::string GroupName = DI->getDef()->getValueAsString("GroupName");
231     DiagsInGroup[GroupName].DiagsInGroup.push_back(R);
232   }
233   
234   // Add all DiagGroup's to the DiagsInGroup list to make sure we pick up empty
235   // groups (these are warnings that GCC supports that clang never produces).
236   std::vector<Record*> DiagGroups
237     = Records.getAllDerivedDefinitions("DiagGroup");
238   for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
239     Record *Group = DiagGroups[i];
240     GroupInfo &GI = DiagsInGroup[Group->getValueAsString("GroupName")];
241     
242     std::vector<Record*> SubGroups = Group->getValueAsListOfDefs("SubGroups");
243     for (unsigned j = 0, e = SubGroups.size(); j != e; ++j)
244       GI.SubGroups.push_back(SubGroups[j]->getValueAsString("GroupName"));
245   }
246   
247   // Assign unique ID numbers to the groups.
248   unsigned IDNo = 0;
249   for (std::map<std::string, GroupInfo>::iterator
250        I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I, ++IDNo)
251     I->second.IDNo = IDNo;
252   
253   // Walk through the groups emitting an array for each diagnostic of the diags
254   // that are mapped to.
255   OS << "\n#ifdef GET_DIAG_ARRAYS\n";
256   unsigned MaxLen = 0;
257   for (std::map<std::string, GroupInfo>::iterator
258        I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
259     MaxLen = std::max(MaxLen, (unsigned)I->first.size());
260     
261     std::vector<const Record*> &V = I->second.DiagsInGroup;
262     if (!V.empty()) {
263       OS << "static const short DiagArray" << I->second.IDNo << "[] = { ";
264       for (unsigned i = 0, e = V.size(); i != e; ++i)
265         OS << "diag::" << V[i]->getName() << ", ";
266       OS << "-1 };\n";
267     }
268     
269     const std::vector<std::string> &SubGroups = I->second.SubGroups;
270     if (!SubGroups.empty()) {
271       OS << "static const short DiagSubGroup" << I->second.IDNo << "[] = { ";
272       for (unsigned i = 0, e = SubGroups.size(); i != e; ++i) {
273         std::map<std::string, GroupInfo>::iterator RI =
274           DiagsInGroup.find(SubGroups[i]);
275         assert(RI != DiagsInGroup.end() && "Referenced without existing?");
276         OS << RI->second.IDNo << ", ";
277       }
278       OS << "-1 };\n";
279     }
280   }
281   OS << "#endif // GET_DIAG_ARRAYS\n\n";
282   
283   // Emit the table now.
284   OS << "\n#ifdef GET_DIAG_TABLE\n";
285   for (std::map<std::string, GroupInfo>::iterator
286        I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
287     // Group option string.
288     OS << "  { ";
289     OS << I->first.size() << ", ";
290     OS << "\"";
291     OS.write_escaped(I->first) << "\","
292                                << std::string(MaxLen-I->first.size()+1, ' ');
293     
294     // Diagnostics in the group.
295     if (I->second.DiagsInGroup.empty())
296       OS << "0, ";
297     else
298       OS << "DiagArray" << I->second.IDNo << ", ";
299     
300     // Subgroups.
301     if (I->second.SubGroups.empty())
302       OS << 0;
303     else
304       OS << "DiagSubGroup" << I->second.IDNo;
305     OS << " },\n";
306   }
307   OS << "#endif // GET_DIAG_TABLE\n\n";
308   
309   // Emit the category table next.
310   DiagCategoryIDMap CategoriesByID(Records);
311   OS << "\n#ifdef GET_CATEGORY_TABLE\n";
312   for (DiagCategoryIDMap::iterator I = CategoriesByID.begin(),
313        E = CategoriesByID.end(); I != E; ++I)
314     OS << "CATEGORY(\"" << *I << "\", " << getDiagCategoryEnum(*I) << ")\n";
315   OS << "#endif // GET_CATEGORY_TABLE\n\n";
316 }
317
318 //===----------------------------------------------------------------------===//
319 // Diagnostic name index generation
320 //===----------------------------------------------------------------------===//
321
322 namespace {
323 struct RecordIndexElement
324 {
325   RecordIndexElement() {}
326   explicit RecordIndexElement(Record const &R):
327     Name(R.getName()) {}
328   
329   std::string Name;
330 };
331
332 struct RecordIndexElementSorter :
333   public std::binary_function<RecordIndexElement, RecordIndexElement, bool> {
334   
335   bool operator()(RecordIndexElement const &Lhs,
336                   RecordIndexElement const &Rhs) const {
337     return Lhs.Name < Rhs.Name;
338   }
339   
340 };
341
342 } // end anonymous namespace.
343
344 void ClangDiagsIndexNameEmitter::run(raw_ostream &OS) {
345   const std::vector<Record*> &Diags =
346     Records.getAllDerivedDefinitions("Diagnostic");
347   
348   std::vector<RecordIndexElement> Index;
349   Index.reserve(Diags.size());
350   for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
351     const Record &R = *(Diags[i]);    
352     Index.push_back(RecordIndexElement(R));
353   }
354   
355   std::sort(Index.begin(), Index.end(), RecordIndexElementSorter());
356   
357   for (unsigned i = 0, e = Index.size(); i != e; ++i) {
358     const RecordIndexElement &R = Index[i];
359     
360     OS << "DIAG_NAME_INDEX(" << R.Name << ")\n";
361   }
362 }