blob: cb65cea31a2b4b97ee7f6164816cfcb617df5f83 [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//===----------------------------------------------------------------------===//
27// Generic routines for all Clang TableGen backens.
28//===----------------------------------------------------------------------===//
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)
46 if ((*I)->isSubClassOf(DiagClass))
47 return *I;
48
49 return 0;
50}
51
52static void EmitEscaped(std::ostream& OS, const std::string &s) {
53 for (std::string::const_iterator I=s.begin(), E=s.end(); I!=E; ++I)
54 switch (*I) {
55 default: OS << *I; break;
56 case '\"': OS << "\\" << *I; break;
57 case '\\': OS << "\\\\"; break;
58 }
59}
60
Ted Kremenek557f7f82009-03-13 22:53:41 +000061static void EmitAllCaps(std::ostream& OS, const std::string &s) {
62 for (std::string::const_iterator I=s.begin(), E=s.end(); I!=E; ++I)
63 OS << char(toupper(*I));
64}
65
Ted Kremenek8b9d0272009-03-18 21:16:16 +000066//===----------------------------------------------------------------------===//
67// Warning Tables (.inc file) generation.
68//===----------------------------------------------------------------------===//
69
Ted Kremenek04a847e2009-03-13 22:21:17 +000070static void ProcessDiag(std::ostream& OS, const Record* DiagClass,
71 const Record& R) {
72
73 const Record* DiagKind = getDiagKind(DiagClass, R);
74 if (!DiagKind)
75 return;
76
77 OS << "DIAG(" << R.getName() << ", ";
Ted Kremenek557f7f82009-03-13 22:53:41 +000078 EmitAllCaps(OS, DiagKind->getName());
Ted Kremenek04a847e2009-03-13 22:21:17 +000079
Ted Kremenek557f7f82009-03-13 22:53:41 +000080 const RecordVal* Text = findRecordVal(R, "Text");
Ted Kremenek04a847e2009-03-13 22:21:17 +000081 assert(Text && "No 'Text' entry in Diagnostic.");
82 const StringInit* TextVal = dynamic_cast<const StringInit*>(Text->getValue());
83 assert(TextVal && "Value 'Text' must be a string.");
84 OS << ", \"";
85 EmitEscaped(OS, TextVal->getValue());
86 OS << "\")\n";
87}
88
89void ClangDiagsDefsEmitter::run(std::ostream &OS) {
90 const RecordVector &Diags = Records.getAllDerivedDefinitions("Diagnostic");
91
92 const Record* DiagClass = Records.getClass("Diagnostic");
93 assert(DiagClass && "No Diagnostic class defined.");
94
Ted Kremenek557f7f82009-03-13 22:53:41 +000095 // Write the #if guard
96 if (!Component.empty()) {
97 OS << "#ifdef ";
98 EmitAllCaps(OS, Component);
99 OS << "START\n__";
100 EmitAllCaps(OS, Component);
101 OS << "START = DIAG_START_";
102 EmitAllCaps(OS, Component);
103 OS << ",\n#undef ";
104 EmitAllCaps(OS, Component);
105 OS << "START\n#endif\n";
106 }
107
Ted Kremenek04a847e2009-03-13 22:21:17 +0000108 for (RecordVector::const_iterator I=Diags.begin(), E=Diags.end(); I!=E; ++I) {
Ted Kremenek557f7f82009-03-13 22:53:41 +0000109 if (!Component.empty()) {
110 const RecordVal* V = findRecordVal(**I, "Component");
111 if (!V)
112 continue;
113
114 const StringInit* SV = dynamic_cast<const StringInit*>(V->getValue());
115 if (SV->getValue() != Component)
116 continue;
117 }
118
Ted Kremenek04a847e2009-03-13 22:21:17 +0000119 ProcessDiag(OS, DiagClass, **I);
120 }
Ted Kremenek557f7f82009-03-13 22:53:41 +0000121}
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000122
123//===----------------------------------------------------------------------===//
124// Warning Group Tables generation.
125//===----------------------------------------------------------------------===//
126
Ted Kremenek0f9d5102009-03-18 21:36:46 +0000127static const std::string &getOptName(const Record *R) {
128 const RecordVal *V = findRecordVal(*R, "Name");
129 assert(V && "Options must have a 'Name' value.");
130 const StringInit* SV = dynamic_cast<const StringInit*>(V->getValue());
131 assert(SV && "'Name' entry must be a string.");
132 return SV->getValue();
133}
134
135namespace {
136struct VISIBILITY_HIDDEN CompareOptName {
137 bool operator()(const Record* A, const Record* B) {
138 return getOptName(A) < getOptName(B);
139 }
140};
141}
142
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000143typedef std::set<const Record*> DiagnosticSet;
Ted Kremenek0f9d5102009-03-18 21:36:46 +0000144typedef std::map<const Record*, DiagnosticSet, CompareOptName> OptionMap;
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000145typedef llvm::DenseSet<const ListInit*> VisitedLists;
146
147static void BuildGroup(DiagnosticSet& DS, VisitedLists &Visited, const Init* X);
148
149static void BuildGroup(DiagnosticSet &DS, VisitedLists &Visited,
150 const ListInit* LV) {
151
152 // Simple hack to prevent including a list multiple times. This may be useful
153 // if one declares an Option by including a bunch of other Options that
154 // include other Options, etc.
155 if (Visited.count(LV))
156 return;
157
158 Visited.insert(LV);
159
160 // Iterate through the list and grab all DiagnosticControlled.
161 for (ListInit::const_iterator I = LV->begin(), E = LV->end(); I!=E; ++I)
162 BuildGroup(DS, Visited, *I);
163}
164
165static void BuildGroup(DiagnosticSet& DS, VisitedLists &Visited,
166 const Record *Def) {
167
168 // If an Option includes another Option, inline the Diagnostics of the
169 // included Option.
170 if (Def->isSubClassOf("Option")) {
171 if (const RecordVal* V = findRecordVal(*Def, "Members"))
172 if (const ListInit* LV = dynamic_cast<const ListInit*>(V->getValue()))
173 BuildGroup(DS, Visited, LV);
174
175 return;
176 }
177
178 if (Def->isSubClassOf("DiagnosticControlled"))
179 DS.insert(Def);
180}
181
182static void BuildGroup(DiagnosticSet& DS, VisitedLists &Visited,
183 const Init* X) {
184
185 if (const DefInit *D = dynamic_cast<const DefInit*>(X))
186 BuildGroup(DS, Visited, D->getDef());
187
188 // We may have some other cases here in the future.
189}
190
191
192void ClangOptionsEmitter::run(std::ostream &OS) {
193 // Build up a map from options to controlled diagnostics.
194 OptionMap OM;
195
196 const RecordVector &Opts = Records.getAllDerivedDefinitions("Option");
197 for (RecordVector::const_iterator I=Opts.begin(), E=Opts.end(); I!=E; ++I)
198 if (const RecordVal* V = findRecordVal(**I, "Members"))
199 if (const ListInit* LV = dynamic_cast<const ListInit*>(V->getValue())) {
200 VisitedLists Visited;
201 BuildGroup(OM[*I], Visited, LV);
202 }
203
204 // Iterate through the OptionMap and emit the declarations.
205 for (OptionMap::iterator I = OM.begin(), E = OM.end(); I!=E; ++I) {
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000206 // Output the option.
207 OS << "static const diag::kind " << I->first->getName() << "[] = { ";
208
209 DiagnosticSet &DS = I->second;
210 bool first = true;
211 for (DiagnosticSet::iterator I2 = DS.begin(), E2 = DS.end(); I2!=E2; ++I2) {
212 if (first)
213 first = false;
214 else
215 OS << ", ";
216
217 OS << "diag::" << (*I2)->getName();
218 }
219 OS << " };\n";
220 }
Ted Kremenek3ac82fe2009-03-18 21:28:47 +0000221
222 // Now emit the OptionTable table.
223 OS << "\nstatic const WarningOption OptionTable[] = {";
224 bool first = true;
225 for (OptionMap::iterator I = OM.begin(), E = OM.end(); I!=E; ++I) {
Ted Kremenek3ac82fe2009-03-18 21:28:47 +0000226 if (first)
227 first = false;
228 else
229 OS << ',';
230
Ted Kremenek0f9d5102009-03-18 21:36:46 +0000231 OS << "\n {\"" << getOptName(I->first)
Ted Kremenek3ac82fe2009-03-18 21:28:47 +0000232 << "\", DIAGS(" << I->first->getName() << ")}";
233 }
234 OS << "\n};\n";
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000235}