blob: 147e2fd1b8d65e9759949cce3be5f18f63352e8f [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
Ted Kremenek04a847e2009-03-13 22:21:17 +000042static void EmitEscaped(std::ostream& OS, const std::string &s) {
43 for (std::string::const_iterator I=s.begin(), E=s.end(); I!=E; ++I)
44 switch (*I) {
45 default: OS << *I; break;
46 case '\"': OS << "\\" << *I; break;
47 case '\\': OS << "\\\\"; break;
48 }
49}
50
Ted Kremenek557f7f82009-03-13 22:53:41 +000051static void EmitAllCaps(std::ostream& OS, const std::string &s) {
52 for (std::string::const_iterator I=s.begin(), E=s.end(); I!=E; ++I)
53 OS << char(toupper(*I));
54}
55
Ted Kremenek8b9d0272009-03-18 21:16:16 +000056//===----------------------------------------------------------------------===//
57// Warning Tables (.inc file) generation.
58//===----------------------------------------------------------------------===//
59
Chris Lattnerf1624aa2009-04-15 06:26:49 +000060static void ProcessDiag(std::ostream &OS, const Record *DiagClass,
61 const Record &R) {
Ted Kremenek04a847e2009-03-13 22:21:17 +000062 OS << "DIAG(" << R.getName() << ", ";
Chris Lattnera71c4942009-04-15 16:55:46 +000063 OS << R.getValueAsDef("Class")->getName();
Chris Lattner5b66c042009-04-15 16:43:18 +000064 OS << ", diag::" << R.getValueAsDef("DefaultMapping")->getName();
Ted Kremenek04a847e2009-03-13 22:21:17 +000065 OS << ", \"";
Chris Lattner5b66c042009-04-15 16:43:18 +000066 EmitEscaped(OS, R.getValueAsString("Text"));
Ted Kremenek04a847e2009-03-13 22:21:17 +000067 OS << "\")\n";
68}
69
70void ClangDiagsDefsEmitter::run(std::ostream &OS) {
71 const RecordVector &Diags = Records.getAllDerivedDefinitions("Diagnostic");
72
73 const Record* DiagClass = Records.getClass("Diagnostic");
74 assert(DiagClass && "No Diagnostic class defined.");
75
Ted Kremenek557f7f82009-03-13 22:53:41 +000076 // Write the #if guard
77 if (!Component.empty()) {
78 OS << "#ifdef ";
79 EmitAllCaps(OS, Component);
80 OS << "START\n__";
81 EmitAllCaps(OS, Component);
82 OS << "START = DIAG_START_";
83 EmitAllCaps(OS, Component);
84 OS << ",\n#undef ";
85 EmitAllCaps(OS, Component);
86 OS << "START\n#endif\n";
87 }
88
Ted Kremenek04a847e2009-03-13 22:21:17 +000089 for (RecordVector::const_iterator I=Diags.begin(), E=Diags.end(); I!=E; ++I) {
Chris Lattnerf1624aa2009-04-15 06:26:49 +000090 const Record &R = **I;
91 // Filter by component.
92 if (!Component.empty() && Component != R.getValueAsString("Component"))
93 continue;
Ted Kremenek557f7f82009-03-13 22:53:41 +000094
Chris Lattnerf1624aa2009-04-15 06:26:49 +000095 ProcessDiag(OS, DiagClass, R);
Ted Kremenek04a847e2009-03-13 22:21:17 +000096 }
Ted Kremenek557f7f82009-03-13 22:53:41 +000097}
Ted Kremenek8b9d0272009-03-18 21:16:16 +000098
99//===----------------------------------------------------------------------===//
100// Warning Group Tables generation.
101//===----------------------------------------------------------------------===//
102
Ted Kremenek0f9d5102009-03-18 21:36:46 +0000103static const std::string &getOptName(const Record *R) {
104 const RecordVal *V = findRecordVal(*R, "Name");
105 assert(V && "Options must have a 'Name' value.");
106 const StringInit* SV = dynamic_cast<const StringInit*>(V->getValue());
107 assert(SV && "'Name' entry must be a string.");
108 return SV->getValue();
109}
110
111namespace {
112struct VISIBILITY_HIDDEN CompareOptName {
Ted Kremenekef66abe2009-04-01 18:24:22 +0000113 bool operator()(const Record* A, const Record* B) const {
114 return getOptName(A) < getOptName(B);
Ted Kremenek0f9d5102009-03-18 21:36:46 +0000115 }
116};
117}
118
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000119typedef std::set<const Record*> DiagnosticSet;
Ted Kremenek0f9d5102009-03-18 21:36:46 +0000120typedef std::map<const Record*, DiagnosticSet, CompareOptName> OptionMap;
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000121typedef llvm::DenseSet<const ListInit*> VisitedLists;
122
123static void BuildGroup(DiagnosticSet& DS, VisitedLists &Visited, const Init* X);
124
125static void BuildGroup(DiagnosticSet &DS, VisitedLists &Visited,
126 const ListInit* LV) {
127
128 // Simple hack to prevent including a list multiple times. This may be useful
129 // if one declares an Option by including a bunch of other Options that
130 // include other Options, etc.
131 if (Visited.count(LV))
132 return;
133
134 Visited.insert(LV);
135
136 // Iterate through the list and grab all DiagnosticControlled.
137 for (ListInit::const_iterator I = LV->begin(), E = LV->end(); I!=E; ++I)
138 BuildGroup(DS, Visited, *I);
139}
140
141static void BuildGroup(DiagnosticSet& DS, VisitedLists &Visited,
142 const Record *Def) {
143
144 // If an Option includes another Option, inline the Diagnostics of the
145 // included Option.
146 if (Def->isSubClassOf("Option")) {
Chris Lattnerf1624aa2009-04-15 06:26:49 +0000147 if (const RecordVal *V = findRecordVal(*Def, "Members"))
148 if (const ListInit *LV = dynamic_cast<const ListInit*>(V->getValue()))
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000149 BuildGroup(DS, Visited, LV);
150
151 return;
152 }
153
154 if (Def->isSubClassOf("DiagnosticControlled"))
155 DS.insert(Def);
156}
157
158static void BuildGroup(DiagnosticSet& DS, VisitedLists &Visited,
159 const Init* X) {
160
161 if (const DefInit *D = dynamic_cast<const DefInit*>(X))
162 BuildGroup(DS, Visited, D->getDef());
163
164 // We may have some other cases here in the future.
165}
166
167
168void ClangOptionsEmitter::run(std::ostream &OS) {
169 // Build up a map from options to controlled diagnostics.
Chris Lattnerf1624aa2009-04-15 06:26:49 +0000170 OptionMap OM;
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000171
172 const RecordVector &Opts = Records.getAllDerivedDefinitions("Option");
Chris Lattnerf1624aa2009-04-15 06:26:49 +0000173 for (RecordVector::const_iterator I=Opts.begin(), E=Opts.end(); I != E; ++I)
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000174 if (const RecordVal* V = findRecordVal(**I, "Members"))
175 if (const ListInit* LV = dynamic_cast<const ListInit*>(V->getValue())) {
176 VisitedLists Visited;
177 BuildGroup(OM[*I], Visited, LV);
178 }
179
180 // Iterate through the OptionMap and emit the declarations.
181 for (OptionMap::iterator I = OM.begin(), E = OM.end(); I!=E; ++I) {
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000182 // Output the option.
183 OS << "static const diag::kind " << I->first->getName() << "[] = { ";
184
185 DiagnosticSet &DS = I->second;
186 bool first = true;
187 for (DiagnosticSet::iterator I2 = DS.begin(), E2 = DS.end(); I2!=E2; ++I2) {
188 if (first)
189 first = false;
190 else
191 OS << ", ";
192
193 OS << "diag::" << (*I2)->getName();
194 }
195 OS << " };\n";
196 }
Ted Kremenek3ac82fe2009-03-18 21:28:47 +0000197
198 // Now emit the OptionTable table.
199 OS << "\nstatic const WarningOption OptionTable[] = {";
200 bool first = true;
201 for (OptionMap::iterator I = OM.begin(), E = OM.end(); I!=E; ++I) {
Ted Kremenek3ac82fe2009-03-18 21:28:47 +0000202 if (first)
203 first = false;
204 else
205 OS << ',';
206
Ted Kremenek0f9d5102009-03-18 21:36:46 +0000207 OS << "\n {\"" << getOptName(I->first)
Ted Kremenek3ac82fe2009-03-18 21:28:47 +0000208 << "\", DIAGS(" << I->first->getName() << ")}";
209 }
210 OS << "\n};\n";
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000211}