1
//=- ClangDiagnosticsEmitter.cpp - Generate Clang diagnostics tables -*- C++ -*-
3
// The LLVM Compiler Infrastructure
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
8
//===----------------------------------------------------------------------===//
10
// These tablegen backends emit Clang diagnostics tables.
12
//===----------------------------------------------------------------------===//
14
#include "ClangDiagnosticsEmitter.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"
26
//===----------------------------------------------------------------------===//
27
// Diagnostic category computation code.
28
//===----------------------------------------------------------------------===//
31
class DiagGroupParentMap {
32
std::map<const Record*, std::vector<Record*> > Mapping;
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]);
45
const std::vector<Record*> &getParents(const Record *Group) {
46
return Mapping[Group];
49
} // end anonymous namespace.
53
getCategoryFromDiagGroup(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;
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;
69
/// getDiagnosticCategory - Return the category that the specified diagnostic
71
static std::string getDiagnosticCategory(const Record *R,
72
DiagGroupParentMap &DiagGroupParents) {
73
// 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(),
78
if (!CatName.empty()) return CatName;
81
// If the diagnostic itself has a category, get it.
82
return R->getValueAsString("CategoryName");
86
class DiagCategoryIDMap {
87
StringMap<unsigned> CategoryIDs;
88
std::vector<std::string> CategoryStrings;
91
DiagGroupParentMap ParentInfo;
93
// The zero'th category is "".
94
CategoryStrings.push_back("");
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.
103
unsigned &ID = CategoryIDs[Category];
104
if (ID != 0) continue; // Already seen.
106
ID = CategoryStrings.size();
107
CategoryStrings.push_back(Category);
111
unsigned getID(StringRef CategoryString) {
112
return CategoryIDs[CategoryString];
115
typedef std::vector<std::string>::iterator iterator;
116
iterator begin() { return CategoryStrings.begin(); }
117
iterator end() { return CategoryStrings.end(); }
119
} // end anonymous namespace.
123
//===----------------------------------------------------------------------===//
124
// Warning Tables (.inc file) generation.
125
//===----------------------------------------------------------------------===//
127
void ClangDiagsDefsEmitter::run(raw_ostream &OS) {
128
// Write the #if guard
129
if (!Component.empty()) {
130
std::string ComponentName = UppercaseString(Component);
131
OS << "#ifdef " << ComponentName << "START\n";
132
OS << "__" << ComponentName << "START = DIAG_START_" << ComponentName
134
OS << "#undef " << ComponentName << "START\n";
138
const std::vector<Record*> &Diags =
139
Records.getAllDerivedDefinitions("Diagnostic");
141
DiagCategoryIDMap CategoryIDs;
142
DiagGroupParentMap DGParentMap;
144
for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
145
const Record &R = *Diags[i];
146
// Filter by component.
147
if (!Component.empty() && Component != R.getValueAsString("Component"))
150
OS << "DIAG(" << R.getName() << ", ";
151
OS << R.getValueAsDef("Class")->getName();
152
OS << ", diag::" << R.getValueAsDef("DefaultMapping")->getName();
154
// Description string.
156
OS.write_escaped(R.getValueAsString("Text")) << '"';
158
// Warning associated with the diagnostic.
159
if (DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Group"))) {
161
OS.write_escaped(DI->getDef()->getValueAsString("GroupName")) << '"';
167
if (R.getValueAsBit("SFINAE"))
173
OS << ", " << CategoryIDs.getID(getDiagnosticCategory(&R, DGParentMap));
178
//===----------------------------------------------------------------------===//
179
// Warning Group Tables generation
180
//===----------------------------------------------------------------------===//
183
std::vector<const Record*> DiagsInGroup;
184
std::vector<std::string> SubGroups;
188
void ClangDiagGroupsEmitter::run(raw_ostream &OS) {
189
// Compute a mapping from a DiagGroup to all of its parents.
190
DiagGroupParentMap DGParentMap;
192
// Invert the 1-[0/1] mapping of diags to group into a one to many mapping of
193
// groups to diags in the group.
194
std::map<std::string, GroupInfo> DiagsInGroup;
196
std::vector<Record*> Diags =
197
Records.getAllDerivedDefinitions("Diagnostic");
198
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;
202
std::string GroupName = DI->getDef()->getValueAsString("GroupName");
203
DiagsInGroup[GroupName].DiagsInGroup.push_back(R);
206
// 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).
208
std::vector<Record*> DiagGroups
209
= Records.getAllDerivedDefinitions("DiagGroup");
210
for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
211
Record *Group = DiagGroups[i];
212
GroupInfo &GI = DiagsInGroup[Group->getValueAsString("GroupName")];
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"));
219
// Assign unique ID numbers to the groups.
221
for (std::map<std::string, GroupInfo>::iterator
222
I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I, ++IDNo)
223
I->second.IDNo = IDNo;
225
// 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";
229
for (std::map<std::string, GroupInfo>::iterator
230
I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
231
MaxLen = std::max(MaxLen, (unsigned)I->first.size());
233
std::vector<const Record*> &V = I->second.DiagsInGroup;
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() << ", ";
241
const std::vector<std::string> &SubGroups = I->second.SubGroups;
242
if (!SubGroups.empty()) {
243
OS << "static const short DiagSubGroup" << I->second.IDNo << "[] = { ";
244
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 << ", ";
253
OS << "#endif // GET_DIAG_ARRAYS\n\n";
255
// Emit the table now.
256
OS << "\n#ifdef GET_DIAG_TABLE\n";
257
for (std::map<std::string, GroupInfo>::iterator
258
I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
259
// Group option string.
261
OS.write_escaped(I->first) << "\","
262
<< std::string(MaxLen-I->first.size()+1, ' ');
264
// Diagnostics in the group.
265
if (I->second.DiagsInGroup.empty())
268
OS << "DiagArray" << I->second.IDNo << ", ";
271
if (I->second.SubGroups.empty())
274
OS << "DiagSubGroup" << I->second.IDNo;
277
OS << "#endif // GET_DIAG_TABLE\n\n";
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";