blob: 5364f596872e03fe1fa806ebc726009336e960a0 [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 Kremenek04a847e2009-03-13 22:21:17 +000018#include "llvm/Support/Streams.h"
19#include "llvm/ADT/VectorExtras.h"
Ted Kremenek8b9d0272009-03-18 21:16:16 +000020#include "llvm/ADT/DenseSet.h"
21#include <set>
22#include <map>
Ted Kremenek04a847e2009-03-13 22:21:17 +000023
24using namespace llvm;
Ted Kremenek8b9d0272009-03-18 21:16:16 +000025
26//===----------------------------------------------------------------------===//
Jim Grosbachda4231f2009-03-26 16:17:51 +000027// Generic routines for all Clang TableGen backends.
Ted Kremenek8b9d0272009-03-18 21:16:16 +000028//===----------------------------------------------------------------------===//
29
Ted Kremenek04a847e2009-03-13 22:21:17 +000030typedef std::vector<Record*> RecordVector;
31typedef std::vector<Record*> SuperClassVector;
32typedef std::vector<RecordVal> RecordValVector;
33
Ted Kremenek557f7f82009-03-13 22:53:41 +000034static const RecordVal* findRecordVal(const Record& R, const std::string &key) {
35 const RecordValVector &Vals = R.getValues();
Ted Kremenek04a847e2009-03-13 22:21:17 +000036 for (RecordValVector::const_iterator I=Vals.begin(), E=Vals.end(); I!=E; ++I)
37 if ((*I).getName() == key)
38 return &*I;
39
40 return 0;
41}
42
43static const Record* getDiagKind(const Record* DiagClass, const Record &R) {
44 const SuperClassVector &SC = R.getSuperClasses();
45 for (SuperClassVector::const_iterator I=SC.begin(), E=SC.end(); I!=E; ++I)
Ted Kremenek7fae82f2009-03-19 17:18:09 +000046 if ((*I)->isSubClassOf(DiagClass) &&
47 (*I)->getName() != "DiagnosticControlled")
Ted Kremenek04a847e2009-03-13 22:21:17 +000048 return *I;
49
50 return 0;
51}
52
53static void EmitEscaped(std::ostream& OS, const std::string &s) {
54 for (std::string::const_iterator I=s.begin(), E=s.end(); I!=E; ++I)
55 switch (*I) {
56 default: OS << *I; break;
57 case '\"': OS << "\\" << *I; break;
58 case '\\': OS << "\\\\"; break;
59 }
60}
61
Ted Kremenek557f7f82009-03-13 22:53:41 +000062static void EmitAllCaps(std::ostream& OS, const std::string &s) {
63 for (std::string::const_iterator I=s.begin(), E=s.end(); I!=E; ++I)
64 OS << char(toupper(*I));
65}
66
Ted Kremenek8b9d0272009-03-18 21:16:16 +000067//===----------------------------------------------------------------------===//
68// Warning Tables (.inc file) generation.
69//===----------------------------------------------------------------------===//
70
Ted Kremenek04a847e2009-03-13 22:21:17 +000071static void ProcessDiag(std::ostream& OS, const Record* DiagClass,
72 const Record& R) {
73
74 const Record* DiagKind = getDiagKind(DiagClass, R);
75 if (!DiagKind)
76 return;
77
78 OS << "DIAG(" << R.getName() << ", ";
Ted Kremenek557f7f82009-03-13 22:53:41 +000079 EmitAllCaps(OS, DiagKind->getName());
Ted Kremenek04a847e2009-03-13 22:21:17 +000080
Ted Kremenek557f7f82009-03-13 22:53:41 +000081 const RecordVal* Text = findRecordVal(R, "Text");
Ted Kremenek04a847e2009-03-13 22:21:17 +000082 assert(Text && "No 'Text' entry in Diagnostic.");
83 const StringInit* TextVal = dynamic_cast<const StringInit*>(Text->getValue());
84 assert(TextVal && "Value 'Text' must be a string.");
85 OS << ", \"";
86 EmitEscaped(OS, TextVal->getValue());
87 OS << "\")\n";
88}
89
90void ClangDiagsDefsEmitter::run(std::ostream &OS) {
91 const RecordVector &Diags = Records.getAllDerivedDefinitions("Diagnostic");
92
93 const Record* DiagClass = Records.getClass("Diagnostic");
94 assert(DiagClass && "No Diagnostic class defined.");
95
Ted Kremenek557f7f82009-03-13 22:53:41 +000096 // Write the #if guard
97 if (!Component.empty()) {
98 OS << "#ifdef ";
99 EmitAllCaps(OS, Component);
100 OS << "START\n__";
101 EmitAllCaps(OS, Component);
102 OS << "START = DIAG_START_";
103 EmitAllCaps(OS, Component);
104 OS << ",\n#undef ";
105 EmitAllCaps(OS, Component);
106 OS << "START\n#endif\n";
107 }
108
Ted Kremenek04a847e2009-03-13 22:21:17 +0000109 for (RecordVector::const_iterator I=Diags.begin(), E=Diags.end(); I!=E; ++I) {
Ted Kremenek557f7f82009-03-13 22:53:41 +0000110 if (!Component.empty()) {
111 const RecordVal* V = findRecordVal(**I, "Component");
112 if (!V)
113 continue;
114
115 const StringInit* SV = dynamic_cast<const StringInit*>(V->getValue());
Ted Kremenek13b9bf92009-03-23 21:54:33 +0000116 if (!SV || SV->getValue() != Component)
Ted Kremenek557f7f82009-03-13 22:53:41 +0000117 continue;
118 }
119
Ted Kremenek04a847e2009-03-13 22:21:17 +0000120 ProcessDiag(OS, DiagClass, **I);
121 }
Ted Kremenek557f7f82009-03-13 22:53:41 +0000122}
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000123
124//===----------------------------------------------------------------------===//
125// Warning Group Tables generation.
126//===----------------------------------------------------------------------===//
127
Ted Kremenek0f9d5102009-03-18 21:36:46 +0000128static const std::string &getOptName(const Record *R) {
129 const RecordVal *V = findRecordVal(*R, "Name");
130 assert(V && "Options must have a 'Name' value.");
131 const StringInit* SV = dynamic_cast<const StringInit*>(V->getValue());
132 assert(SV && "'Name' entry must be a string.");
133 return SV->getValue();
134}
135
136namespace {
137struct VISIBILITY_HIDDEN CompareOptName {
Ted Kremenekef66abe2009-04-01 18:24:22 +0000138 bool operator()(const Record* A, const Record* B) const {
139 return getOptName(A) < getOptName(B);
Ted Kremenek0f9d5102009-03-18 21:36:46 +0000140 }
141};
142}
143
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000144typedef std::set<const Record*> DiagnosticSet;
Ted Kremenek0f9d5102009-03-18 21:36:46 +0000145typedef std::map<const Record*, DiagnosticSet, CompareOptName> OptionMap;
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000146typedef llvm::DenseSet<const ListInit*> VisitedLists;
147
148static void BuildGroup(DiagnosticSet& DS, VisitedLists &Visited, const Init* X);
149
150static void BuildGroup(DiagnosticSet &DS, VisitedLists &Visited,
151 const ListInit* LV) {
152
153 // Simple hack to prevent including a list multiple times. This may be useful
154 // if one declares an Option by including a bunch of other Options that
155 // include other Options, etc.
156 if (Visited.count(LV))
157 return;
158
159 Visited.insert(LV);
160
161 // Iterate through the list and grab all DiagnosticControlled.
162 for (ListInit::const_iterator I = LV->begin(), E = LV->end(); I!=E; ++I)
163 BuildGroup(DS, Visited, *I);
164}
165
166static void BuildGroup(DiagnosticSet& DS, VisitedLists &Visited,
167 const Record *Def) {
168
169 // If an Option includes another Option, inline the Diagnostics of the
170 // included Option.
171 if (Def->isSubClassOf("Option")) {
172 if (const RecordVal* V = findRecordVal(*Def, "Members"))
173 if (const ListInit* LV = dynamic_cast<const ListInit*>(V->getValue()))
174 BuildGroup(DS, Visited, LV);
175
176 return;
177 }
178
179 if (Def->isSubClassOf("DiagnosticControlled"))
180 DS.insert(Def);
181}
182
183static void BuildGroup(DiagnosticSet& DS, VisitedLists &Visited,
184 const Init* X) {
185
186 if (const DefInit *D = dynamic_cast<const DefInit*>(X))
187 BuildGroup(DS, Visited, D->getDef());
188
189 // We may have some other cases here in the future.
190}
191
192
193void ClangOptionsEmitter::run(std::ostream &OS) {
194 // Build up a map from options to controlled diagnostics.
195 OptionMap OM;
196
197 const RecordVector &Opts = Records.getAllDerivedDefinitions("Option");
198 for (RecordVector::const_iterator I=Opts.begin(), E=Opts.end(); I!=E; ++I)
199 if (const RecordVal* V = findRecordVal(**I, "Members"))
200 if (const ListInit* LV = dynamic_cast<const ListInit*>(V->getValue())) {
201 VisitedLists Visited;
202 BuildGroup(OM[*I], Visited, LV);
203 }
204
205 // Iterate through the OptionMap and emit the declarations.
206 for (OptionMap::iterator I = OM.begin(), E = OM.end(); I!=E; ++I) {
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000207 // Output the option.
208 OS << "static const diag::kind " << I->first->getName() << "[] = { ";
209
210 DiagnosticSet &DS = I->second;
211 bool first = true;
212 for (DiagnosticSet::iterator I2 = DS.begin(), E2 = DS.end(); I2!=E2; ++I2) {
213 if (first)
214 first = false;
215 else
216 OS << ", ";
217
218 OS << "diag::" << (*I2)->getName();
219 }
220 OS << " };\n";
221 }
Ted Kremenek3ac82fe2009-03-18 21:28:47 +0000222
223 // Now emit the OptionTable table.
224 OS << "\nstatic const WarningOption OptionTable[] = {";
225 bool first = true;
226 for (OptionMap::iterator I = OM.begin(), E = OM.end(); I!=E; ++I) {
Ted Kremenek3ac82fe2009-03-18 21:28:47 +0000227 if (first)
228 first = false;
229 else
230 OS << ',';
231
Ted Kremenek0f9d5102009-03-18 21:36:46 +0000232 OS << "\n {\"" << getOptName(I->first)
Ted Kremenek3ac82fe2009-03-18 21:28:47 +0000233 << "\", DIAGS(" << I->first->getName() << ")}";
234 }
235 OS << "\n};\n";
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000236}