blob: fb7a1c4d6da51bdde272a3512bd1abbf33057f4a [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 +000023using namespace llvm;
Ted Kremenek8b9d0272009-03-18 21:16:16 +000024
25//===----------------------------------------------------------------------===//
Jim Grosbachda4231f2009-03-26 16:17:51 +000026// Generic routines for all Clang TableGen backends.
Ted Kremenek8b9d0272009-03-18 21:16:16 +000027//===----------------------------------------------------------------------===//
28
Ted Kremenek04a847e2009-03-13 22:21:17 +000029typedef std::vector<Record*> RecordVector;
30typedef std::vector<Record*> SuperClassVector;
31typedef std::vector<RecordVal> RecordValVector;
32
Ted Kremenek557f7f82009-03-13 22:53:41 +000033static const RecordVal* findRecordVal(const Record& R, const std::string &key) {
34 const RecordValVector &Vals = R.getValues();
Ted Kremenek04a847e2009-03-13 22:21:17 +000035 for (RecordValVector::const_iterator I=Vals.begin(), E=Vals.end(); I!=E; ++I)
36 if ((*I).getName() == key)
37 return &*I;
38
39 return 0;
40}
41
42static const Record* getDiagKind(const Record* DiagClass, const Record &R) {
43 const SuperClassVector &SC = R.getSuperClasses();
44 for (SuperClassVector::const_iterator I=SC.begin(), E=SC.end(); I!=E; ++I)
Ted Kremenek7fae82f2009-03-19 17:18:09 +000045 if ((*I)->isSubClassOf(DiagClass) &&
46 (*I)->getName() != "DiagnosticControlled")
Ted Kremenek04a847e2009-03-13 22:21:17 +000047 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
Chris Lattnerf1624aa2009-04-15 06:26:49 +000070static void ProcessDiag(std::ostream &OS, const Record *DiagClass,
71 const Record &R) {
Ted Kremenek04a847e2009-03-13 22:21:17 +000072
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) {
Chris Lattnerf1624aa2009-04-15 06:26:49 +0000109 const Record &R = **I;
110 // Filter by component.
111 if (!Component.empty() && Component != R.getValueAsString("Component"))
112 continue;
Ted Kremenek557f7f82009-03-13 22:53:41 +0000113
Chris Lattnerf1624aa2009-04-15 06:26:49 +0000114 ProcessDiag(OS, DiagClass, R);
Ted Kremenek04a847e2009-03-13 22:21:17 +0000115 }
Ted Kremenek557f7f82009-03-13 22:53:41 +0000116}
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000117
118//===----------------------------------------------------------------------===//
119// Warning Group Tables generation.
120//===----------------------------------------------------------------------===//
121
Ted Kremenek0f9d5102009-03-18 21:36:46 +0000122static const std::string &getOptName(const Record *R) {
123 const RecordVal *V = findRecordVal(*R, "Name");
124 assert(V && "Options must have a 'Name' value.");
125 const StringInit* SV = dynamic_cast<const StringInit*>(V->getValue());
126 assert(SV && "'Name' entry must be a string.");
127 return SV->getValue();
128}
129
130namespace {
131struct VISIBILITY_HIDDEN CompareOptName {
Ted Kremenekef66abe2009-04-01 18:24:22 +0000132 bool operator()(const Record* A, const Record* B) const {
133 return getOptName(A) < getOptName(B);
Ted Kremenek0f9d5102009-03-18 21:36:46 +0000134 }
135};
136}
137
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000138typedef std::set<const Record*> DiagnosticSet;
Ted Kremenek0f9d5102009-03-18 21:36:46 +0000139typedef std::map<const Record*, DiagnosticSet, CompareOptName> OptionMap;
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000140typedef llvm::DenseSet<const ListInit*> VisitedLists;
141
142static void BuildGroup(DiagnosticSet& DS, VisitedLists &Visited, const Init* X);
143
144static void BuildGroup(DiagnosticSet &DS, VisitedLists &Visited,
145 const ListInit* LV) {
146
147 // Simple hack to prevent including a list multiple times. This may be useful
148 // if one declares an Option by including a bunch of other Options that
149 // include other Options, etc.
150 if (Visited.count(LV))
151 return;
152
153 Visited.insert(LV);
154
155 // Iterate through the list and grab all DiagnosticControlled.
156 for (ListInit::const_iterator I = LV->begin(), E = LV->end(); I!=E; ++I)
157 BuildGroup(DS, Visited, *I);
158}
159
160static void BuildGroup(DiagnosticSet& DS, VisitedLists &Visited,
161 const Record *Def) {
162
163 // If an Option includes another Option, inline the Diagnostics of the
164 // included Option.
165 if (Def->isSubClassOf("Option")) {
Chris Lattnerf1624aa2009-04-15 06:26:49 +0000166 if (const RecordVal *V = findRecordVal(*Def, "Members"))
167 if (const ListInit *LV = dynamic_cast<const ListInit*>(V->getValue()))
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000168 BuildGroup(DS, Visited, LV);
169
170 return;
171 }
172
173 if (Def->isSubClassOf("DiagnosticControlled"))
174 DS.insert(Def);
175}
176
177static void BuildGroup(DiagnosticSet& DS, VisitedLists &Visited,
178 const Init* X) {
179
180 if (const DefInit *D = dynamic_cast<const DefInit*>(X))
181 BuildGroup(DS, Visited, D->getDef());
182
183 // We may have some other cases here in the future.
184}
185
186
187void ClangOptionsEmitter::run(std::ostream &OS) {
188 // Build up a map from options to controlled diagnostics.
Chris Lattnerf1624aa2009-04-15 06:26:49 +0000189 OptionMap OM;
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000190
191 const RecordVector &Opts = Records.getAllDerivedDefinitions("Option");
Chris Lattnerf1624aa2009-04-15 06:26:49 +0000192 for (RecordVector::const_iterator I=Opts.begin(), E=Opts.end(); I != E; ++I)
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000193 if (const RecordVal* V = findRecordVal(**I, "Members"))
194 if (const ListInit* LV = dynamic_cast<const ListInit*>(V->getValue())) {
195 VisitedLists Visited;
196 BuildGroup(OM[*I], Visited, LV);
197 }
198
199 // Iterate through the OptionMap and emit the declarations.
200 for (OptionMap::iterator I = OM.begin(), E = OM.end(); I!=E; ++I) {
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000201 // Output the option.
202 OS << "static const diag::kind " << I->first->getName() << "[] = { ";
203
204 DiagnosticSet &DS = I->second;
205 bool first = true;
206 for (DiagnosticSet::iterator I2 = DS.begin(), E2 = DS.end(); I2!=E2; ++I2) {
207 if (first)
208 first = false;
209 else
210 OS << ", ";
211
212 OS << "diag::" << (*I2)->getName();
213 }
214 OS << " };\n";
215 }
Ted Kremenek3ac82fe2009-03-18 21:28:47 +0000216
217 // Now emit the OptionTable table.
218 OS << "\nstatic const WarningOption OptionTable[] = {";
219 bool first = true;
220 for (OptionMap::iterator I = OM.begin(), E = OM.end(); I!=E; ++I) {
Ted Kremenek3ac82fe2009-03-18 21:28:47 +0000221 if (first)
222 first = false;
223 else
224 OS << ',';
225
Ted Kremenek0f9d5102009-03-18 21:36:46 +0000226 OS << "\n {\"" << getOptName(I->first)
Ted Kremenek3ac82fe2009-03-18 21:28:47 +0000227 << "\", DIAGS(" << I->first->getName() << ")}";
228 }
229 OS << "\n};\n";
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000230}