blob: 0a48e75681fc828da3863c1b69e9bf4400093040 [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"
John McCallf14bacc2011-06-15 21:43:52 +000021#include "llvm/ADT/SmallString.h"
Chris Lattner457cde42009-04-15 20:13:18 +000022#include "llvm/ADT/VectorExtras.h"
Ted Kremenek8b9d0272009-03-18 21:16:16 +000023#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.
David Greene05bce0b2011-07-29 22:43:06 +000077 if (DefInit *Group = dynamic_cast<DefInit*>(R->getValueInit("Group"))) {
Chris Lattnerec5a0b32010-05-24 21:55:47 +000078 // 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.
David Greene05bce0b2011-07-29 22:43:06 +0000162 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 {
Argyrios Kyrtzidis804cb232011-05-25 05:04:22 +0000166 OS << ", \"\"";
Chris Lattnerbfc01042009-04-16 05:52:18 +0000167 }
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
John McCallf14bacc2011-06-15 21:43:52 +0000199static 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
Argyrios Kyrtzidisd91ef792011-02-13 07:51:19 +0000208namespace {
Chris Lattnerd0d34942009-04-16 00:53:25 +0000209struct GroupInfo {
210 std::vector<const Record*> DiagsInGroup;
211 std::vector<std::string> SubGroups;
Chris Lattner9efac562009-04-16 03:16:12 +0000212 unsigned IDNo;
Chris Lattnerd0d34942009-04-16 00:53:25 +0000213};
Argyrios Kyrtzidisd91ef792011-02-13 07:51:19 +0000214} // end anonymous namespace.
Chris Lattnerd0d34942009-04-16 00:53:25 +0000215
Daniel Dunbar1a551802009-07-03 00:10:29 +0000216void ClangDiagGroupsEmitter::run(raw_ostream &OS) {
Chris Lattneraf1465b2010-05-04 20:44:23 +0000217 // Compute a mapping from a DiagGroup to all of its parents.
Chris Lattner67db8832010-12-13 00:23:57 +0000218 DiagGroupParentMap DGParentMap(Records);
Chris Lattneraf1465b2010-05-04 20:44:23 +0000219
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000220 // Invert the 1-[0/1] mapping of diags to group into a one to many mapping of
221 // groups to diags in the group.
Chris Lattnerd0d34942009-04-16 00:53:25 +0000222 std::map<std::string, GroupInfo> DiagsInGroup;
Chris Lattner457cde42009-04-15 20:13:18 +0000223
Chris Lattnera0ed8ca2009-04-15 22:33:02 +0000224 std::vector<Record*> Diags =
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000225 Records.getAllDerivedDefinitions("Diagnostic");
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000226 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
227 const Record *R = Diags[i];
David Greene05bce0b2011-07-29 22:43:06 +0000228 DefInit *DI = dynamic_cast<DefInit*>(R->getValueInit("Group"));
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000229 if (DI == 0) continue;
Chris Lattnerd0d34942009-04-16 00:53:25 +0000230 std::string GroupName = DI->getDef()->getValueAsString("GroupName");
231 DiagsInGroup[GroupName].DiagsInGroup.push_back(R);
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000232 }
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000233
Chris Lattnera0ed8ca2009-04-15 22:33:02 +0000234 // 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).
Chris Lattneraf1465b2010-05-04 20:44:23 +0000236 std::vector<Record*> DiagGroups
237 = Records.getAllDerivedDefinitions("DiagGroup");
238 for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
239 Record *Group = DiagGroups[i];
Chris Lattnerd0d34942009-04-16 00:53:25 +0000240 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"));
Chris Lattnera0ed8ca2009-04-15 22:33:02 +0000245 }
246
Chris Lattner9efac562009-04-16 03:16:12 +0000247 // 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
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000253 // 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";
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000256 unsigned MaxLen = 0;
Chris Lattnerd0d34942009-04-16 00:53:25 +0000257 for (std::map<std::string, GroupInfo>::iterator
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000258 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
259 MaxLen = std::max(MaxLen, (unsigned)I->first.size());
Ted Kremenek3ac82fe2009-03-18 21:28:47 +0000260
Chris Lattnerd0d34942009-04-16 00:53:25 +0000261 std::vector<const Record*> &V = I->second.DiagsInGroup;
Chris Lattner9efac562009-04-16 03:16:12 +0000262 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 }
Chris Lattnerd0d34942009-04-16 00:53:25 +0000268
Chris Lattner9efac562009-04-16 03:16:12 +0000269 const std::vector<std::string> &SubGroups = I->second.SubGroups;
270 if (!SubGroups.empty()) {
Chandler Carruth93ca7b62010-05-13 07:43:47 +0000271 OS << "static const short DiagSubGroup" << I->second.IDNo << "[] = { ";
Chris Lattner9efac562009-04-16 03:16:12 +0000272 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 }
Ted Kremenek3ac82fe2009-03-18 21:28:47 +0000280 }
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000281 OS << "#endif // GET_DIAG_ARRAYS\n\n";
282
283 // Emit the table now.
284 OS << "\n#ifdef GET_DIAG_TABLE\n";
Chris Lattnerd0d34942009-04-16 00:53:25 +0000285 for (std::map<std::string, GroupInfo>::iterator
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000286 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
Chris Lattnerd0d34942009-04-16 00:53:25 +0000287 // Group option string.
Argyrios Kyrtzidis804cb232011-05-25 05:04:22 +0000288 OS << " { ";
289 OS << I->first.size() << ", ";
290 OS << "\"";
Daniel Dunbar3446cf12009-10-17 20:43:19 +0000291 OS.write_escaped(I->first) << "\","
292 << std::string(MaxLen-I->first.size()+1, ' ');
Chris Lattnerd0d34942009-04-16 00:53:25 +0000293
294 // Diagnostics in the group.
295 if (I->second.DiagsInGroup.empty())
296 OS << "0, ";
297 else
Chris Lattner9efac562009-04-16 03:16:12 +0000298 OS << "DiagArray" << I->second.IDNo << ", ";
Chris Lattnerd0d34942009-04-16 00:53:25 +0000299
Chris Lattner9efac562009-04-16 03:16:12 +0000300 // Subgroups.
301 if (I->second.SubGroups.empty())
302 OS << 0;
303 else
304 OS << "DiagSubGroup" << I->second.IDNo;
Chris Lattnerd0d34942009-04-16 00:53:25 +0000305 OS << " },\n";
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000306 }
307 OS << "#endif // GET_DIAG_TABLE\n\n";
Chris Lattneraf1465b2010-05-04 20:44:23 +0000308
309 // Emit the category table next.
Chris Lattner67db8832010-12-13 00:23:57 +0000310 DiagCategoryIDMap CategoriesByID(Records);
Chris Lattneraf1465b2010-05-04 20:44:23 +0000311 OS << "\n#ifdef GET_CATEGORY_TABLE\n";
312 for (DiagCategoryIDMap::iterator I = CategoriesByID.begin(),
313 E = CategoriesByID.end(); I != E; ++I)
John McCallf14bacc2011-06-15 21:43:52 +0000314 OS << "CATEGORY(\"" << *I << "\", " << getDiagCategoryEnum(*I) << ")\n";
Chris Lattneraf1465b2010-05-04 20:44:23 +0000315 OS << "#endif // GET_CATEGORY_TABLE\n\n";
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000316}
Douglas Gregor4954e9f2011-04-15 22:04:07 +0000317
318//===----------------------------------------------------------------------===//
319// Diagnostic name index generation
320//===----------------------------------------------------------------------===//
321
322namespace {
323struct RecordIndexElement
324{
325 RecordIndexElement() {}
326 explicit RecordIndexElement(Record const &R):
327 Name(R.getName()) {}
328
329 std::string Name;
330};
331
332struct 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
344void 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}