blob: 75b6252c4f9f70961d40bbab9b4c3033c903ff0e [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>
Ted Kremenek04a847e2009-03-13 22:21:17 +000024using namespace llvm;
Ted Kremenek8b9d0272009-03-18 21:16:16 +000025
26//===----------------------------------------------------------------------===//
Chris Lattneraf1465b2010-05-04 20:44:23 +000027// Diagnostic category computation code.
28//===----------------------------------------------------------------------===//
29
30namespace {
31class DiagGroupParentMap {
32 std::map<const Record*, std::vector<Record*> > Mapping;
33public:
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]);
42 }
43 }
44
45 const std::vector<Record*> &getParents(const Record *Group) {
46 return Mapping[Group];
47 }
48};
49} // end anonymous namespace.
50
51
52static std::string
53getCategoryFromDiagGroup(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;
58
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;
65 }
66 return "";
67}
68
69/// getDiagnosticCategory - Return the category that the specified diagnostic
70/// lives in.
71static std::string getDiagnosticCategory(const Record *R,
72 DiagGroupParentMap &DiagGroupParents) {
Chris Lattnerec5a0b32010-05-24 21:55:47 +000073 // If the diagnostic is in a group, and that group has a category, use it.
74 if (DefInit *Group = dynamic_cast<DefInit*>(R->getValueInit("Group"))) {
75 // Check the diagnostic's diag group for a category.
76 std::string CatName = getCategoryFromDiagGroup(Group->getDef(),
77 DiagGroupParents);
78 if (!CatName.empty()) return CatName;
79 }
80
Chris Lattneraf1465b2010-05-04 20:44:23 +000081 // If the diagnostic itself has a category, get it.
Chris Lattnerec5a0b32010-05-24 21:55:47 +000082 return R->getValueAsString("CategoryName");
Chris Lattneraf1465b2010-05-04 20:44:23 +000083}
84
85namespace {
86 class DiagCategoryIDMap {
87 StringMap<unsigned> CategoryIDs;
88 std::vector<std::string> CategoryStrings;
89 public:
90 DiagCategoryIDMap() {
91 DiagGroupParentMap ParentInfo;
92
93 // The zero'th category is "".
94 CategoryStrings.push_back("");
95 CategoryIDs[""] = 0;
96
97 std::vector<Record*> Diags =
98 Records.getAllDerivedDefinitions("Diagnostic");
99 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
100 std::string Category = getDiagnosticCategory(Diags[i], ParentInfo);
101 if (Category.empty()) continue; // Skip diags with no category.
102
103 unsigned &ID = CategoryIDs[Category];
104 if (ID != 0) continue; // Already seen.
105
106 ID = CategoryStrings.size();
107 CategoryStrings.push_back(Category);
108 }
109 }
110
111 unsigned getID(StringRef CategoryString) {
112 return CategoryIDs[CategoryString];
113 }
114
115 typedef std::vector<std::string>::iterator iterator;
116 iterator begin() { return CategoryStrings.begin(); }
117 iterator end() { return CategoryStrings.end(); }
118 };
119} // end anonymous namespace.
120
121
122
123//===----------------------------------------------------------------------===//
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000124// Warning Tables (.inc file) generation.
125//===----------------------------------------------------------------------===//
126
Daniel Dunbar1a551802009-07-03 00:10:29 +0000127void ClangDiagsDefsEmitter::run(raw_ostream &OS) {
Ted Kremenek557f7f82009-03-13 22:53:41 +0000128 // Write the #if guard
129 if (!Component.empty()) {
Chris Lattner9371c332009-04-15 20:16:12 +0000130 std::string ComponentName = UppercaseString(Component);
131 OS << "#ifdef " << ComponentName << "START\n";
132 OS << "__" << ComponentName << "START = DIAG_START_" << ComponentName
133 << ",\n";
134 OS << "#undef " << ComponentName << "START\n";
Kovarththanan Rajaratname7e891f2010-03-18 13:16:38 +0000135 OS << "#endif\n\n";
Ted Kremenek557f7f82009-03-13 22:53:41 +0000136 }
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000137
138 const std::vector<Record*> &Diags =
139 Records.getAllDerivedDefinitions("Diagnostic");
Ted Kremenek557f7f82009-03-13 22:53:41 +0000140
Chris Lattneraf1465b2010-05-04 20:44:23 +0000141 DiagCategoryIDMap CategoryIDs;
142 DiagGroupParentMap DGParentMap;
143
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000144 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
145 const Record &R = *Diags[i];
Chris Lattnerf1624aa2009-04-15 06:26:49 +0000146 // Filter by component.
147 if (!Component.empty() && Component != R.getValueAsString("Component"))
148 continue;
Ted Kremenek557f7f82009-03-13 22:53:41 +0000149
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000150 OS << "DIAG(" << R.getName() << ", ";
151 OS << R.getValueAsDef("Class")->getName();
152 OS << ", diag::" << R.getValueAsDef("DefaultMapping")->getName();
Chris Lattnerbfc01042009-04-16 05:52:18 +0000153
154 // Description string.
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000155 OS << ", \"";
Daniel Dunbar3446cf12009-10-17 20:43:19 +0000156 OS.write_escaped(R.getValueAsString("Text")) << '"';
Chris Lattnerbfc01042009-04-16 05:52:18 +0000157
158 // Warning associated with the diagnostic.
159 if (DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Group"))) {
Daniel Dunbar3446cf12009-10-17 20:43:19 +0000160 OS << ", \"";
161 OS.write_escaped(DI->getDef()->getValueAsString("GroupName")) << '"';
Chris Lattnerbfc01042009-04-16 05:52:18 +0000162 } else {
163 OS << ", 0";
164 }
Douglas Gregor956244b2009-06-14 07:24:49 +0000165
166 // SFINAE bit
167 if (R.getValueAsBit("SFINAE"))
168 OS << ", true";
169 else
170 OS << ", false";
Chris Lattneraf1465b2010-05-04 20:44:23 +0000171
172 // Category number.
173 OS << ", " << CategoryIDs.getID(getDiagnosticCategory(&R, DGParentMap));
Chris Lattnerbfc01042009-04-16 05:52:18 +0000174 OS << ")\n";
Ted Kremenek04a847e2009-03-13 22:21:17 +0000175 }
Ted Kremenek557f7f82009-03-13 22:53:41 +0000176}
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000177
178//===----------------------------------------------------------------------===//
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000179// Warning Group Tables generation
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000180//===----------------------------------------------------------------------===//
181
Chris Lattnerd0d34942009-04-16 00:53:25 +0000182struct GroupInfo {
183 std::vector<const Record*> DiagsInGroup;
184 std::vector<std::string> SubGroups;
Chris Lattner9efac562009-04-16 03:16:12 +0000185 unsigned IDNo;
Chris Lattnerd0d34942009-04-16 00:53:25 +0000186};
187
Daniel Dunbar1a551802009-07-03 00:10:29 +0000188void ClangDiagGroupsEmitter::run(raw_ostream &OS) {
Chris Lattneraf1465b2010-05-04 20:44:23 +0000189 // Compute a mapping from a DiagGroup to all of its parents.
190 DiagGroupParentMap DGParentMap;
191
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000192 // Invert the 1-[0/1] mapping of diags to group into a one to many mapping of
193 // groups to diags in the group.
Chris Lattnerd0d34942009-04-16 00:53:25 +0000194 std::map<std::string, GroupInfo> DiagsInGroup;
Chris Lattner457cde42009-04-15 20:13:18 +0000195
Chris Lattnera0ed8ca2009-04-15 22:33:02 +0000196 std::vector<Record*> Diags =
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000197 Records.getAllDerivedDefinitions("Diagnostic");
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000198 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
199 const Record *R = Diags[i];
200 DefInit *DI = dynamic_cast<DefInit*>(R->getValueInit("Group"));
201 if (DI == 0) continue;
Chris Lattnerd0d34942009-04-16 00:53:25 +0000202 std::string GroupName = DI->getDef()->getValueAsString("GroupName");
203 DiagsInGroup[GroupName].DiagsInGroup.push_back(R);
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000204 }
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000205
Chris Lattnera0ed8ca2009-04-15 22:33:02 +0000206 // Add all DiagGroup's to the DiagsInGroup list to make sure we pick up empty
207 // groups (these are warnings that GCC supports that clang never produces).
Chris Lattneraf1465b2010-05-04 20:44:23 +0000208 std::vector<Record*> DiagGroups
209 = Records.getAllDerivedDefinitions("DiagGroup");
210 for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
211 Record *Group = DiagGroups[i];
Chris Lattnerd0d34942009-04-16 00:53:25 +0000212 GroupInfo &GI = DiagsInGroup[Group->getValueAsString("GroupName")];
213
214 std::vector<Record*> SubGroups = Group->getValueAsListOfDefs("SubGroups");
215 for (unsigned j = 0, e = SubGroups.size(); j != e; ++j)
216 GI.SubGroups.push_back(SubGroups[j]->getValueAsString("GroupName"));
Chris Lattnera0ed8ca2009-04-15 22:33:02 +0000217 }
218
Chris Lattner9efac562009-04-16 03:16:12 +0000219 // Assign unique ID numbers to the groups.
220 unsigned IDNo = 0;
221 for (std::map<std::string, GroupInfo>::iterator
222 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I, ++IDNo)
223 I->second.IDNo = IDNo;
224
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000225 // Walk through the groups emitting an array for each diagnostic of the diags
226 // that are mapped to.
227 OS << "\n#ifdef GET_DIAG_ARRAYS\n";
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000228 unsigned MaxLen = 0;
Chris Lattnerd0d34942009-04-16 00:53:25 +0000229 for (std::map<std::string, GroupInfo>::iterator
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000230 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
231 MaxLen = std::max(MaxLen, (unsigned)I->first.size());
Ted Kremenek3ac82fe2009-03-18 21:28:47 +0000232
Chris Lattnerd0d34942009-04-16 00:53:25 +0000233 std::vector<const Record*> &V = I->second.DiagsInGroup;
Chris Lattner9efac562009-04-16 03:16:12 +0000234 if (!V.empty()) {
235 OS << "static const short DiagArray" << I->second.IDNo << "[] = { ";
236 for (unsigned i = 0, e = V.size(); i != e; ++i)
237 OS << "diag::" << V[i]->getName() << ", ";
238 OS << "-1 };\n";
239 }
Chris Lattnerd0d34942009-04-16 00:53:25 +0000240
Chris Lattner9efac562009-04-16 03:16:12 +0000241 const std::vector<std::string> &SubGroups = I->second.SubGroups;
242 if (!SubGroups.empty()) {
Chandler Carruth93ca7b62010-05-13 07:43:47 +0000243 OS << "static const short DiagSubGroup" << I->second.IDNo << "[] = { ";
Chris Lattner9efac562009-04-16 03:16:12 +0000244 for (unsigned i = 0, e = SubGroups.size(); i != e; ++i) {
245 std::map<std::string, GroupInfo>::iterator RI =
246 DiagsInGroup.find(SubGroups[i]);
247 assert(RI != DiagsInGroup.end() && "Referenced without existing?");
248 OS << RI->second.IDNo << ", ";
249 }
250 OS << "-1 };\n";
251 }
Ted Kremenek3ac82fe2009-03-18 21:28:47 +0000252 }
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000253 OS << "#endif // GET_DIAG_ARRAYS\n\n";
254
255 // Emit the table now.
256 OS << "\n#ifdef GET_DIAG_TABLE\n";
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) {
Chris Lattnerd0d34942009-04-16 00:53:25 +0000259 // Group option string.
Daniel Dunbar3446cf12009-10-17 20:43:19 +0000260 OS << " { \"";
261 OS.write_escaped(I->first) << "\","
262 << std::string(MaxLen-I->first.size()+1, ' ');
Chris Lattnerd0d34942009-04-16 00:53:25 +0000263
264 // Diagnostics in the group.
265 if (I->second.DiagsInGroup.empty())
266 OS << "0, ";
267 else
Chris Lattner9efac562009-04-16 03:16:12 +0000268 OS << "DiagArray" << I->second.IDNo << ", ";
Chris Lattnerd0d34942009-04-16 00:53:25 +0000269
Chris Lattner9efac562009-04-16 03:16:12 +0000270 // Subgroups.
271 if (I->second.SubGroups.empty())
272 OS << 0;
273 else
274 OS << "DiagSubGroup" << I->second.IDNo;
Chris Lattnerd0d34942009-04-16 00:53:25 +0000275 OS << " },\n";
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000276 }
277 OS << "#endif // GET_DIAG_TABLE\n\n";
Chris Lattneraf1465b2010-05-04 20:44:23 +0000278
279 // Emit the category table next.
280 DiagCategoryIDMap CategoriesByID;
281 OS << "\n#ifdef GET_CATEGORY_TABLE\n";
282 for (DiagCategoryIDMap::iterator I = CategoriesByID.begin(),
283 E = CategoriesByID.end(); I != E; ++I)
284 OS << "CATEGORY(\"" << *I << "\")\n";
285 OS << "#endif // GET_CATEGORY_TABLE\n\n";
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000286}