blob: 5a5f4795c27778f3899d9cd169405262a82d4804 [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"
Ted Kremenek8b9d0272009-03-18 21:16:16 +000019#include "llvm/ADT/DenseSet.h"
Chris Lattner457cde42009-04-15 20:13:18 +000020#include "llvm/ADT/StringExtras.h"
21#include "llvm/ADT/VectorExtras.h"
Ted Kremenek8b9d0272009-03-18 21:16:16 +000022#include <set>
23#include <map>
Ted Kremenek04a847e2009-03-13 22:21:17 +000024using 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
Ted Kremenek557f7f82009-03-13 22:53:41 +000043static void EmitAllCaps(std::ostream& OS, const std::string &s) {
44 for (std::string::const_iterator I=s.begin(), E=s.end(); I!=E; ++I)
45 OS << char(toupper(*I));
46}
47
Ted Kremenek8b9d0272009-03-18 21:16:16 +000048//===----------------------------------------------------------------------===//
49// Warning Tables (.inc file) generation.
50//===----------------------------------------------------------------------===//
51
Chris Lattnerf1624aa2009-04-15 06:26:49 +000052static void ProcessDiag(std::ostream &OS, const Record *DiagClass,
53 const Record &R) {
Ted Kremenek04a847e2009-03-13 22:21:17 +000054 OS << "DIAG(" << R.getName() << ", ";
Chris Lattnera71c4942009-04-15 16:55:46 +000055 OS << R.getValueAsDef("Class")->getName();
Chris Lattner5b66c042009-04-15 16:43:18 +000056 OS << ", diag::" << R.getValueAsDef("DefaultMapping")->getName();
Ted Kremenek04a847e2009-03-13 22:21:17 +000057 OS << ", \"";
Chris Lattner457cde42009-04-15 20:13:18 +000058 std::string S = R.getValueAsString("Text");
59 EscapeString(S);
60 OS << S << "\")\n";
Ted Kremenek04a847e2009-03-13 22:21:17 +000061}
62
63void ClangDiagsDefsEmitter::run(std::ostream &OS) {
64 const RecordVector &Diags = Records.getAllDerivedDefinitions("Diagnostic");
65
66 const Record* DiagClass = Records.getClass("Diagnostic");
67 assert(DiagClass && "No Diagnostic class defined.");
68
Ted Kremenek557f7f82009-03-13 22:53:41 +000069 // Write the #if guard
70 if (!Component.empty()) {
71 OS << "#ifdef ";
72 EmitAllCaps(OS, Component);
73 OS << "START\n__";
74 EmitAllCaps(OS, Component);
75 OS << "START = DIAG_START_";
76 EmitAllCaps(OS, Component);
77 OS << ",\n#undef ";
78 EmitAllCaps(OS, Component);
79 OS << "START\n#endif\n";
80 }
81
Ted Kremenek04a847e2009-03-13 22:21:17 +000082 for (RecordVector::const_iterator I=Diags.begin(), E=Diags.end(); I!=E; ++I) {
Chris Lattnerf1624aa2009-04-15 06:26:49 +000083 const Record &R = **I;
84 // Filter by component.
85 if (!Component.empty() && Component != R.getValueAsString("Component"))
86 continue;
Ted Kremenek557f7f82009-03-13 22:53:41 +000087
Chris Lattnerf1624aa2009-04-15 06:26:49 +000088 ProcessDiag(OS, DiagClass, R);
Ted Kremenek04a847e2009-03-13 22:21:17 +000089 }
Ted Kremenek557f7f82009-03-13 22:53:41 +000090}
Ted Kremenek8b9d0272009-03-18 21:16:16 +000091
92//===----------------------------------------------------------------------===//
93// Warning Group Tables generation.
94//===----------------------------------------------------------------------===//
95
Ted Kremenek0f9d5102009-03-18 21:36:46 +000096static const std::string &getOptName(const Record *R) {
97 const RecordVal *V = findRecordVal(*R, "Name");
98 assert(V && "Options must have a 'Name' value.");
99 const StringInit* SV = dynamic_cast<const StringInit*>(V->getValue());
100 assert(SV && "'Name' entry must be a string.");
101 return SV->getValue();
102}
103
104namespace {
105struct VISIBILITY_HIDDEN CompareOptName {
Ted Kremenekef66abe2009-04-01 18:24:22 +0000106 bool operator()(const Record* A, const Record* B) const {
107 return getOptName(A) < getOptName(B);
Ted Kremenek0f9d5102009-03-18 21:36:46 +0000108 }
109};
110}
111
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000112typedef std::set<const Record*> DiagnosticSet;
Ted Kremenek0f9d5102009-03-18 21:36:46 +0000113typedef std::map<const Record*, DiagnosticSet, CompareOptName> OptionMap;
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000114typedef llvm::DenseSet<const ListInit*> VisitedLists;
115
116static void BuildGroup(DiagnosticSet& DS, VisitedLists &Visited, const Init* X);
117
118static void BuildGroup(DiagnosticSet &DS, VisitedLists &Visited,
119 const ListInit* LV) {
120
121 // Simple hack to prevent including a list multiple times. This may be useful
122 // if one declares an Option by including a bunch of other Options that
123 // include other Options, etc.
124 if (Visited.count(LV))
125 return;
126
127 Visited.insert(LV);
128
129 // Iterate through the list and grab all DiagnosticControlled.
130 for (ListInit::const_iterator I = LV->begin(), E = LV->end(); I!=E; ++I)
131 BuildGroup(DS, Visited, *I);
132}
133
134static void BuildGroup(DiagnosticSet& DS, VisitedLists &Visited,
135 const Record *Def) {
136
137 // If an Option includes another Option, inline the Diagnostics of the
138 // included Option.
139 if (Def->isSubClassOf("Option")) {
Chris Lattnerf1624aa2009-04-15 06:26:49 +0000140 if (const RecordVal *V = findRecordVal(*Def, "Members"))
141 if (const ListInit *LV = dynamic_cast<const ListInit*>(V->getValue()))
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000142 BuildGroup(DS, Visited, LV);
143
144 return;
145 }
146
147 if (Def->isSubClassOf("DiagnosticControlled"))
148 DS.insert(Def);
149}
150
151static void BuildGroup(DiagnosticSet& DS, VisitedLists &Visited,
152 const Init* X) {
153
154 if (const DefInit *D = dynamic_cast<const DefInit*>(X))
155 BuildGroup(DS, Visited, D->getDef());
156
157 // We may have some other cases here in the future.
158}
159
160
Chris Lattnerac9e7da2009-04-15 20:02:32 +0000161void ClangDiagGroupsEmitter::run(std::ostream &OS) {
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000162 // Build up a map from options to controlled diagnostics.
Chris Lattnerf1624aa2009-04-15 06:26:49 +0000163 OptionMap OM;
Chris Lattner457cde42009-04-15 20:13:18 +0000164
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000165 const RecordVector &Opts = Records.getAllDerivedDefinitions("Option");
Chris Lattnerf1624aa2009-04-15 06:26:49 +0000166 for (RecordVector::const_iterator I=Opts.begin(), E=Opts.end(); I != E; ++I)
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000167 if (const RecordVal* V = findRecordVal(**I, "Members"))
168 if (const ListInit* LV = dynamic_cast<const ListInit*>(V->getValue())) {
169 VisitedLists Visited;
170 BuildGroup(OM[*I], Visited, LV);
171 }
172
173 // Iterate through the OptionMap and emit the declarations.
174 for (OptionMap::iterator I = OM.begin(), E = OM.end(); I!=E; ++I) {
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000175 // Output the option.
176 OS << "static const diag::kind " << I->first->getName() << "[] = { ";
177
178 DiagnosticSet &DS = I->second;
179 bool first = true;
180 for (DiagnosticSet::iterator I2 = DS.begin(), E2 = DS.end(); I2!=E2; ++I2) {
181 if (first)
182 first = false;
183 else
184 OS << ", ";
185
186 OS << "diag::" << (*I2)->getName();
187 }
188 OS << " };\n";
189 }
Ted Kremenek3ac82fe2009-03-18 21:28:47 +0000190
191 // Now emit the OptionTable table.
192 OS << "\nstatic const WarningOption OptionTable[] = {";
193 bool first = true;
194 for (OptionMap::iterator I = OM.begin(), E = OM.end(); I!=E; ++I) {
Ted Kremenek3ac82fe2009-03-18 21:28:47 +0000195 if (first)
196 first = false;
197 else
198 OS << ',';
199
Ted Kremenek0f9d5102009-03-18 21:36:46 +0000200 OS << "\n {\"" << getOptName(I->first)
Ted Kremenek3ac82fe2009-03-18 21:28:47 +0000201 << "\", DIAGS(" << I->first->getName() << ")}";
202 }
203 OS << "\n};\n";
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000204}