blob: 1588f0b6367930de6a33dbb93b6422c3637a5426 [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;
Ted Kremenek04a847e2009-03-13 22:21:17 +000031
Ted Kremenek557f7f82009-03-13 22:53:41 +000032static const RecordVal* findRecordVal(const Record& R, const std::string &key) {
Chris Lattner9371c332009-04-15 20:16:12 +000033 typedef std::vector<RecordVal> RecordValVector;
Ted Kremenek557f7f82009-03-13 22:53:41 +000034 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 Kremenek8b9d0272009-03-18 21:16:16 +000042//===----------------------------------------------------------------------===//
43// Warning Tables (.inc file) generation.
44//===----------------------------------------------------------------------===//
45
Chris Lattnerf1624aa2009-04-15 06:26:49 +000046static void ProcessDiag(std::ostream &OS, const Record *DiagClass,
47 const Record &R) {
Ted Kremenek04a847e2009-03-13 22:21:17 +000048 OS << "DIAG(" << R.getName() << ", ";
Chris Lattnera71c4942009-04-15 16:55:46 +000049 OS << R.getValueAsDef("Class")->getName();
Chris Lattner5b66c042009-04-15 16:43:18 +000050 OS << ", diag::" << R.getValueAsDef("DefaultMapping")->getName();
Ted Kremenek04a847e2009-03-13 22:21:17 +000051 OS << ", \"";
Chris Lattner457cde42009-04-15 20:13:18 +000052 std::string S = R.getValueAsString("Text");
53 EscapeString(S);
54 OS << S << "\")\n";
Ted Kremenek04a847e2009-03-13 22:21:17 +000055}
56
57void ClangDiagsDefsEmitter::run(std::ostream &OS) {
58 const RecordVector &Diags = Records.getAllDerivedDefinitions("Diagnostic");
59
60 const Record* DiagClass = Records.getClass("Diagnostic");
61 assert(DiagClass && "No Diagnostic class defined.");
62
Ted Kremenek557f7f82009-03-13 22:53:41 +000063 // Write the #if guard
64 if (!Component.empty()) {
Chris Lattner9371c332009-04-15 20:16:12 +000065 std::string ComponentName = UppercaseString(Component);
66 OS << "#ifdef " << ComponentName << "START\n";
67 OS << "__" << ComponentName << "START = DIAG_START_" << ComponentName
68 << ",\n";
69 OS << "#undef " << ComponentName << "START\n";
70 OS << "#endif\n";
Ted Kremenek557f7f82009-03-13 22:53:41 +000071 }
72
Ted Kremenek04a847e2009-03-13 22:21:17 +000073 for (RecordVector::const_iterator I=Diags.begin(), E=Diags.end(); I!=E; ++I) {
Chris Lattnerf1624aa2009-04-15 06:26:49 +000074 const Record &R = **I;
75 // Filter by component.
76 if (!Component.empty() && Component != R.getValueAsString("Component"))
77 continue;
Ted Kremenek557f7f82009-03-13 22:53:41 +000078
Chris Lattnerf1624aa2009-04-15 06:26:49 +000079 ProcessDiag(OS, DiagClass, R);
Ted Kremenek04a847e2009-03-13 22:21:17 +000080 }
Ted Kremenek557f7f82009-03-13 22:53:41 +000081}
Ted Kremenek8b9d0272009-03-18 21:16:16 +000082
83//===----------------------------------------------------------------------===//
84// Warning Group Tables generation.
85//===----------------------------------------------------------------------===//
86
Ted Kremenek0f9d5102009-03-18 21:36:46 +000087static const std::string &getOptName(const Record *R) {
88 const RecordVal *V = findRecordVal(*R, "Name");
89 assert(V && "Options must have a 'Name' value.");
90 const StringInit* SV = dynamic_cast<const StringInit*>(V->getValue());
91 assert(SV && "'Name' entry must be a string.");
92 return SV->getValue();
93}
94
95namespace {
96struct VISIBILITY_HIDDEN CompareOptName {
Ted Kremenekef66abe2009-04-01 18:24:22 +000097 bool operator()(const Record* A, const Record* B) const {
98 return getOptName(A) < getOptName(B);
Ted Kremenek0f9d5102009-03-18 21:36:46 +000099 }
100};
101}
102
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000103typedef std::set<const Record*> DiagnosticSet;
Ted Kremenek0f9d5102009-03-18 21:36:46 +0000104typedef std::map<const Record*, DiagnosticSet, CompareOptName> OptionMap;
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000105typedef llvm::DenseSet<const ListInit*> VisitedLists;
106
107static void BuildGroup(DiagnosticSet& DS, VisitedLists &Visited, const Init* X);
108
109static void BuildGroup(DiagnosticSet &DS, VisitedLists &Visited,
110 const ListInit* LV) {
111
112 // Simple hack to prevent including a list multiple times. This may be useful
113 // if one declares an Option by including a bunch of other Options that
114 // include other Options, etc.
115 if (Visited.count(LV))
116 return;
117
118 Visited.insert(LV);
119
120 // Iterate through the list and grab all DiagnosticControlled.
121 for (ListInit::const_iterator I = LV->begin(), E = LV->end(); I!=E; ++I)
122 BuildGroup(DS, Visited, *I);
123}
124
125static void BuildGroup(DiagnosticSet& DS, VisitedLists &Visited,
126 const Record *Def) {
127
128 // If an Option includes another Option, inline the Diagnostics of the
129 // included Option.
130 if (Def->isSubClassOf("Option")) {
Chris Lattnerf1624aa2009-04-15 06:26:49 +0000131 if (const RecordVal *V = findRecordVal(*Def, "Members"))
132 if (const ListInit *LV = dynamic_cast<const ListInit*>(V->getValue()))
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000133 BuildGroup(DS, Visited, LV);
134
135 return;
136 }
137
138 if (Def->isSubClassOf("DiagnosticControlled"))
139 DS.insert(Def);
140}
141
142static void BuildGroup(DiagnosticSet& DS, VisitedLists &Visited,
143 const Init* X) {
144
145 if (const DefInit *D = dynamic_cast<const DefInit*>(X))
146 BuildGroup(DS, Visited, D->getDef());
147
148 // We may have some other cases here in the future.
149}
150
151
Chris Lattnerac9e7da2009-04-15 20:02:32 +0000152void ClangDiagGroupsEmitter::run(std::ostream &OS) {
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000153 // Build up a map from options to controlled diagnostics.
Chris Lattnerf1624aa2009-04-15 06:26:49 +0000154 OptionMap OM;
Chris Lattner457cde42009-04-15 20:13:18 +0000155
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000156 const RecordVector &Opts = Records.getAllDerivedDefinitions("Option");
Chris Lattnerf1624aa2009-04-15 06:26:49 +0000157 for (RecordVector::const_iterator I=Opts.begin(), E=Opts.end(); I != E; ++I)
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000158 if (const RecordVal* V = findRecordVal(**I, "Members"))
159 if (const ListInit* LV = dynamic_cast<const ListInit*>(V->getValue())) {
160 VisitedLists Visited;
161 BuildGroup(OM[*I], Visited, LV);
162 }
163
164 // Iterate through the OptionMap and emit the declarations.
165 for (OptionMap::iterator I = OM.begin(), E = OM.end(); I!=E; ++I) {
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000166 // Output the option.
167 OS << "static const diag::kind " << I->first->getName() << "[] = { ";
168
169 DiagnosticSet &DS = I->second;
170 bool first = true;
171 for (DiagnosticSet::iterator I2 = DS.begin(), E2 = DS.end(); I2!=E2; ++I2) {
172 if (first)
173 first = false;
174 else
175 OS << ", ";
176
177 OS << "diag::" << (*I2)->getName();
178 }
179 OS << " };\n";
180 }
Ted Kremenek3ac82fe2009-03-18 21:28:47 +0000181
182 // Now emit the OptionTable table.
183 OS << "\nstatic const WarningOption OptionTable[] = {";
184 bool first = true;
185 for (OptionMap::iterator I = OM.begin(), E = OM.end(); I!=E; ++I) {
Ted Kremenek3ac82fe2009-03-18 21:28:47 +0000186 if (first)
187 first = false;
188 else
189 OS << ',';
190
Ted Kremenek0f9d5102009-03-18 21:36:46 +0000191 OS << "\n {\"" << getOptName(I->first)
Ted Kremenek3ac82fe2009-03-18 21:28:47 +0000192 << "\", DIAGS(" << I->first->getName() << ")}";
193 }
194 OS << "\n};\n";
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000195}