blob: d5cd4d71ca8c6a2c65c8c7125a313bf143581e57 [file] [log] [blame]
Ted Kremenek04a847e2009-03-13 22:21:17 +00001//=- 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"
Ted Kremenek0f9d5102009-03-18 21:36:46 +000017#include "llvm/Support/Compiler.h"
Ted Kremenek8b9d0272009-03-18 21:16:16 +000018#include "llvm/ADT/DenseSet.h"
Chris Lattner457cde42009-04-15 20:13:18 +000019#include "llvm/ADT/StringExtras.h"
Chris Lattneraf1465b2010-05-04 20:44:23 +000020#include "llvm/ADT/StringMap.h"
Chris Lattner457cde42009-04-15 20:13:18 +000021#include "llvm/ADT/VectorExtras.h"
Ted Kremenek8b9d0272009-03-18 21:16:16 +000022#include <set>
23#include <map>
Douglas Gregor4954e9f2011-04-15 22:04:07 +000024#include <algorithm>
25#include <functional>
Ted Kremenek04a847e2009-03-13 22:21:17 +000026using namespace llvm;
Ted Kremenek8b9d0272009-03-18 21:16:16 +000027
28//===----------------------------------------------------------------------===//
Chris Lattneraf1465b2010-05-04 20:44:23 +000029// Diagnostic category computation code.
30//===----------------------------------------------------------------------===//
31
32namespace {
33class DiagGroupParentMap {
Chris Lattner67db8832010-12-13 00:23:57 +000034 RecordKeeper &Records;
Chris Lattneraf1465b2010-05-04 20:44:23 +000035 std::map<const Record*, std::vector<Record*> > Mapping;
36public:
Chris Lattner67db8832010-12-13 00:23:57 +000037 DiagGroupParentMap(RecordKeeper &records) : Records(records) {
Chris Lattneraf1465b2010-05-04 20:44:23 +000038 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
55static std::string
56getCategoryFromDiagGroup(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.
74static std::string getDiagnosticCategory(const Record *R,
75 DiagGroupParentMap &DiagGroupParents) {
Chris Lattnerec5a0b32010-05-24 21:55:47 +000076 // 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
Chris Lattneraf1465b2010-05-04 20:44:23 +000084 // If the diagnostic itself has a category, get it.
Chris Lattnerec5a0b32010-05-24 21:55:47 +000085 return R->getValueAsString("CategoryName");
Chris Lattneraf1465b2010-05-04 20:44:23 +000086}
87
88namespace {
89 class DiagCategoryIDMap {
Chris Lattner67db8832010-12-13 00:23:57 +000090 RecordKeeper &Records;
Chris Lattneraf1465b2010-05-04 20:44:23 +000091 StringMap<unsigned> CategoryIDs;
92 std::vector<std::string> CategoryStrings;
93 public:
Chris Lattner67db8832010-12-13 00:23:57 +000094 DiagCategoryIDMap(RecordKeeper &records) : Records(records) {
95 DiagGroupParentMap ParentInfo(Records);
Chris Lattneraf1465b2010-05-04 20:44:23 +000096
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
Chris Lattneraf1465b2010-05-04 20:44:23 +0000126//===----------------------------------------------------------------------===//
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000127// Warning Tables (.inc file) generation.
128//===----------------------------------------------------------------------===//
129
Daniel Dunbar1a551802009-07-03 00:10:29 +0000130void ClangDiagsDefsEmitter::run(raw_ostream &OS) {
Ted Kremenek557f7f82009-03-13 22:53:41 +0000131 // Write the #if guard
132 if (!Component.empty()) {
Chris Lattner9371c332009-04-15 20:16:12 +0000133 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";
Kovarththanan Rajaratname7e891f2010-03-18 13:16:38 +0000138 OS << "#endif\n\n";
Ted Kremenek557f7f82009-03-13 22:53:41 +0000139 }
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000140
141 const std::vector<Record*> &Diags =
142 Records.getAllDerivedDefinitions("Diagnostic");
Ted Kremenek557f7f82009-03-13 22:53:41 +0000143
Chris Lattner67db8832010-12-13 00:23:57 +0000144 DiagCategoryIDMap CategoryIDs(Records);
145 DiagGroupParentMap DGParentMap(Records);
Chris Lattneraf1465b2010-05-04 20:44:23 +0000146
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000147 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
148 const Record &R = *Diags[i];
Chris Lattnerf1624aa2009-04-15 06:26:49 +0000149 // Filter by component.
150 if (!Component.empty() && Component != R.getValueAsString("Component"))
151 continue;
Ted Kremenek557f7f82009-03-13 22:53:41 +0000152
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000153 OS << "DIAG(" << R.getName() << ", ";
154 OS << R.getValueAsDef("Class")->getName();
155 OS << ", diag::" << R.getValueAsDef("DefaultMapping")->getName();
Chris Lattnerbfc01042009-04-16 05:52:18 +0000156
157 // Description string.
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000158 OS << ", \"";
Daniel Dunbar3446cf12009-10-17 20:43:19 +0000159 OS.write_escaped(R.getValueAsString("Text")) << '"';
Chris Lattnerbfc01042009-04-16 05:52:18 +0000160
161 // Warning associated with the diagnostic.
162 if (DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Group"))) {
Daniel Dunbar3446cf12009-10-17 20:43:19 +0000163 OS << ", \"";
164 OS.write_escaped(DI->getDef()->getValueAsString("GroupName")) << '"';
Chris Lattnerbfc01042009-04-16 05:52:18 +0000165 } else {
166 OS << ", 0";
167 }
Douglas Gregor956244b2009-06-14 07:24:49 +0000168
169 // SFINAE bit
170 if (R.getValueAsBit("SFINAE"))
171 OS << ", true";
172 else
173 OS << ", false";
Douglas Gregor75f6e892011-01-27 21:06:17 +0000174
175 // Access control bit
176 if (R.getValueAsBit("AccessControl"))
177 OS << ", true";
178 else
179 OS << ", false";
180
Chris Lattneraf1465b2010-05-04 20:44:23 +0000181 // Category number.
182 OS << ", " << CategoryIDs.getID(getDiagnosticCategory(&R, DGParentMap));
Douglas Gregor4954e9f2011-04-15 22:04:07 +0000183
184 // Brief
185 OS << ", \"";
186 OS.write_escaped(R.getValueAsString("Brief")) << '"';
187
188 // Explanation
189 OS << ", \"";
190 OS.write_escaped(R.getValueAsString("Explanation")) << '"';
Chris Lattnerbfc01042009-04-16 05:52:18 +0000191 OS << ")\n";
Ted Kremenek04a847e2009-03-13 22:21:17 +0000192 }
Ted Kremenek557f7f82009-03-13 22:53:41 +0000193}
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000194
195//===----------------------------------------------------------------------===//
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000196// Warning Group Tables generation
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000197//===----------------------------------------------------------------------===//
198
Argyrios Kyrtzidisd91ef792011-02-13 07:51:19 +0000199namespace {
Chris Lattnerd0d34942009-04-16 00:53:25 +0000200struct GroupInfo {
201 std::vector<const Record*> DiagsInGroup;
202 std::vector<std::string> SubGroups;
Chris Lattner9efac562009-04-16 03:16:12 +0000203 unsigned IDNo;
Chris Lattnerd0d34942009-04-16 00:53:25 +0000204};
Argyrios Kyrtzidisd91ef792011-02-13 07:51:19 +0000205} // end anonymous namespace.
Chris Lattnerd0d34942009-04-16 00:53:25 +0000206
Daniel Dunbar1a551802009-07-03 00:10:29 +0000207void ClangDiagGroupsEmitter::run(raw_ostream &OS) {
Chris Lattneraf1465b2010-05-04 20:44:23 +0000208 // Compute a mapping from a DiagGroup to all of its parents.
Chris Lattner67db8832010-12-13 00:23:57 +0000209 DiagGroupParentMap DGParentMap(Records);
Chris Lattneraf1465b2010-05-04 20:44:23 +0000210
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000211 // Invert the 1-[0/1] mapping of diags to group into a one to many mapping of
212 // groups to diags in the group.
Chris Lattnerd0d34942009-04-16 00:53:25 +0000213 std::map<std::string, GroupInfo> DiagsInGroup;
Chris Lattner457cde42009-04-15 20:13:18 +0000214
Chris Lattnera0ed8ca2009-04-15 22:33:02 +0000215 std::vector<Record*> Diags =
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000216 Records.getAllDerivedDefinitions("Diagnostic");
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000217 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;
Chris Lattnerd0d34942009-04-16 00:53:25 +0000221 std::string GroupName = DI->getDef()->getValueAsString("GroupName");
222 DiagsInGroup[GroupName].DiagsInGroup.push_back(R);
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000223 }
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000224
Chris Lattnera0ed8ca2009-04-15 22:33:02 +0000225 // 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).
Chris Lattneraf1465b2010-05-04 20:44:23 +0000227 std::vector<Record*> DiagGroups
228 = Records.getAllDerivedDefinitions("DiagGroup");
229 for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
230 Record *Group = DiagGroups[i];
Chris Lattnerd0d34942009-04-16 00:53:25 +0000231 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"));
Chris Lattnera0ed8ca2009-04-15 22:33:02 +0000236 }
237
Chris Lattner9efac562009-04-16 03:16:12 +0000238 // 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
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000244 // 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";
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000247 unsigned MaxLen = 0;
Chris Lattnerd0d34942009-04-16 00:53:25 +0000248 for (std::map<std::string, GroupInfo>::iterator
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000249 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
250 MaxLen = std::max(MaxLen, (unsigned)I->first.size());
Ted Kremenek3ac82fe2009-03-18 21:28:47 +0000251
Chris Lattnerd0d34942009-04-16 00:53:25 +0000252 std::vector<const Record*> &V = I->second.DiagsInGroup;
Chris Lattner9efac562009-04-16 03:16:12 +0000253 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 }
Chris Lattnerd0d34942009-04-16 00:53:25 +0000259
Chris Lattner9efac562009-04-16 03:16:12 +0000260 const std::vector<std::string> &SubGroups = I->second.SubGroups;
261 if (!SubGroups.empty()) {
Chandler Carruth93ca7b62010-05-13 07:43:47 +0000262 OS << "static const short DiagSubGroup" << I->second.IDNo << "[] = { ";
Chris Lattner9efac562009-04-16 03:16:12 +0000263 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 }
Ted Kremenek3ac82fe2009-03-18 21:28:47 +0000271 }
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000272 OS << "#endif // GET_DIAG_ARRAYS\n\n";
273
274 // Emit the table now.
275 OS << "\n#ifdef GET_DIAG_TABLE\n";
Chris Lattnerd0d34942009-04-16 00:53:25 +0000276 for (std::map<std::string, GroupInfo>::iterator
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000277 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
Chris Lattnerd0d34942009-04-16 00:53:25 +0000278 // Group option string.
Daniel Dunbar3446cf12009-10-17 20:43:19 +0000279 OS << " { \"";
280 OS.write_escaped(I->first) << "\","
281 << std::string(MaxLen-I->first.size()+1, ' ');
Chris Lattnerd0d34942009-04-16 00:53:25 +0000282
283 // Diagnostics in the group.
284 if (I->second.DiagsInGroup.empty())
285 OS << "0, ";
286 else
Chris Lattner9efac562009-04-16 03:16:12 +0000287 OS << "DiagArray" << I->second.IDNo << ", ";
Chris Lattnerd0d34942009-04-16 00:53:25 +0000288
Chris Lattner9efac562009-04-16 03:16:12 +0000289 // Subgroups.
290 if (I->second.SubGroups.empty())
291 OS << 0;
292 else
293 OS << "DiagSubGroup" << I->second.IDNo;
Chris Lattnerd0d34942009-04-16 00:53:25 +0000294 OS << " },\n";
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000295 }
296 OS << "#endif // GET_DIAG_TABLE\n\n";
Chris Lattneraf1465b2010-05-04 20:44:23 +0000297
298 // Emit the category table next.
Chris Lattner67db8832010-12-13 00:23:57 +0000299 DiagCategoryIDMap CategoriesByID(Records);
Chris Lattneraf1465b2010-05-04 20:44:23 +0000300 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";
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000305}
Douglas Gregor4954e9f2011-04-15 22:04:07 +0000306
307//===----------------------------------------------------------------------===//
308// Diagnostic name index generation
309//===----------------------------------------------------------------------===//
310
311namespace {
312struct RecordIndexElement
313{
314 RecordIndexElement() {}
315 explicit RecordIndexElement(Record const &R):
316 Name(R.getName()) {}
317
318 std::string Name;
319};
320
321struct 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
333void 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}