1 //=- ClangDiagnosticsEmitter.cpp - Generate Clang diagnostics tables -*- C++ -*-
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // These tablegen backends emit Clang diagnostics tables.
12 //===----------------------------------------------------------------------===//
14 #include "ClangDiagnosticsEmitter.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/Streams.h"
18 #include "llvm/ADT/VectorExtras.h"
19 #include "llvm/ADT/DenseSet.h"
25 //===----------------------------------------------------------------------===//
26 // Generic routines for all Clang TableGen backens.
27 //===----------------------------------------------------------------------===//
29 typedef std::vector<Record*> RecordVector;
30 typedef std::vector<Record*> SuperClassVector;
31 typedef std::vector<RecordVal> RecordValVector;
33 static const RecordVal* findRecordVal(const Record& R, const std::string &key) {
34 const RecordValVector &Vals = R.getValues();
35 for (RecordValVector::const_iterator I=Vals.begin(), E=Vals.end(); I!=E; ++I)
36 if ((*I).getName() == key)
42 static 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)
45 if ((*I)->isSubClassOf(DiagClass))
51 static void EmitEscaped(std::ostream& OS, const std::string &s) {
52 for (std::string::const_iterator I=s.begin(), E=s.end(); I!=E; ++I)
54 default: OS << *I; break;
55 case '\"': OS << "\\" << *I; break;
56 case '\\': OS << "\\\\"; break;
60 static void EmitAllCaps(std::ostream& OS, const std::string &s) {
61 for (std::string::const_iterator I=s.begin(), E=s.end(); I!=E; ++I)
62 OS << char(toupper(*I));
65 //===----------------------------------------------------------------------===//
66 // Warning Tables (.inc file) generation.
67 //===----------------------------------------------------------------------===//
69 static void ProcessDiag(std::ostream& OS, const Record* DiagClass,
72 const Record* DiagKind = getDiagKind(DiagClass, R);
76 OS << "DIAG(" << R.getName() << ", ";
77 EmitAllCaps(OS, DiagKind->getName());
79 const RecordVal* Text = findRecordVal(R, "Text");
80 assert(Text && "No 'Text' entry in Diagnostic.");
81 const StringInit* TextVal = dynamic_cast<const StringInit*>(Text->getValue());
82 assert(TextVal && "Value 'Text' must be a string.");
84 EmitEscaped(OS, TextVal->getValue());
88 void ClangDiagsDefsEmitter::run(std::ostream &OS) {
89 const RecordVector &Diags = Records.getAllDerivedDefinitions("Diagnostic");
91 const Record* DiagClass = Records.getClass("Diagnostic");
92 assert(DiagClass && "No Diagnostic class defined.");
94 // Write the #if guard
95 if (!Component.empty()) {
97 EmitAllCaps(OS, Component);
99 EmitAllCaps(OS, Component);
100 OS << "START = DIAG_START_";
101 EmitAllCaps(OS, Component);
103 EmitAllCaps(OS, Component);
104 OS << "START\n#endif\n";
107 for (RecordVector::const_iterator I=Diags.begin(), E=Diags.end(); I!=E; ++I) {
108 if (!Component.empty()) {
109 const RecordVal* V = findRecordVal(**I, "Component");
113 const StringInit* SV = dynamic_cast<const StringInit*>(V->getValue());
114 if (SV->getValue() != Component)
118 ProcessDiag(OS, DiagClass, **I);
122 //===----------------------------------------------------------------------===//
123 // Warning Group Tables generation.
124 //===----------------------------------------------------------------------===//
126 typedef std::set<const Record*> DiagnosticSet;
127 typedef std::map<const Record*, DiagnosticSet> OptionMap;
128 typedef llvm::DenseSet<const ListInit*> VisitedLists;
130 static void BuildGroup(DiagnosticSet& DS, VisitedLists &Visited, const Init* X);
132 static void BuildGroup(DiagnosticSet &DS, VisitedLists &Visited,
133 const ListInit* LV) {
135 // Simple hack to prevent including a list multiple times. This may be useful
136 // if one declares an Option by including a bunch of other Options that
137 // include other Options, etc.
138 if (Visited.count(LV))
143 // Iterate through the list and grab all DiagnosticControlled.
144 for (ListInit::const_iterator I = LV->begin(), E = LV->end(); I!=E; ++I)
145 BuildGroup(DS, Visited, *I);
148 static void BuildGroup(DiagnosticSet& DS, VisitedLists &Visited,
151 // If an Option includes another Option, inline the Diagnostics of the
153 if (Def->isSubClassOf("Option")) {
154 if (const RecordVal* V = findRecordVal(*Def, "Members"))
155 if (const ListInit* LV = dynamic_cast<const ListInit*>(V->getValue()))
156 BuildGroup(DS, Visited, LV);
161 if (Def->isSubClassOf("DiagnosticControlled"))
165 static void BuildGroup(DiagnosticSet& DS, VisitedLists &Visited,
168 if (const DefInit *D = dynamic_cast<const DefInit*>(X))
169 BuildGroup(DS, Visited, D->getDef());
171 // We may have some other cases here in the future.
175 void ClangOptionsEmitter::run(std::ostream &OS) {
176 // Build up a map from options to controlled diagnostics.
179 const RecordVector &Opts = Records.getAllDerivedDefinitions("Option");
180 for (RecordVector::const_iterator I=Opts.begin(), E=Opts.end(); I!=E; ++I)
181 if (const RecordVal* V = findRecordVal(**I, "Members"))
182 if (const ListInit* LV = dynamic_cast<const ListInit*>(V->getValue())) {
183 VisitedLists Visited;
184 BuildGroup(OM[*I], Visited, LV);
187 // Iterate through the OptionMap and emit the declarations.
188 for (OptionMap::iterator I = OM.begin(), E = OM.end(); I!=E; ++I) {
189 // Output the option.
190 OS << "static const diag::kind " << I->first->getName() << "[] = { ";
192 DiagnosticSet &DS = I->second;
194 for (DiagnosticSet::iterator I2 = DS.begin(), E2 = DS.end(); I2!=E2; ++I2) {
200 OS << "diag::" << (*I2)->getName();
205 // Now emit the OptionTable table.
206 OS << "\nstatic const WarningOption OptionTable[] = {";
208 for (OptionMap::iterator I = OM.begin(), E = OM.end(); I!=E; ++I) {
209 const RecordVal *V = findRecordVal(*I->first, "Name");
210 assert(V && "Options must have a 'Name' value.");
211 const StringInit* SV = dynamic_cast<const StringInit*>(V->getValue());
212 assert(SV && "'Name' entry must be a string.");
219 OS << "\n {\"" << SV->getValue()
220 << "\", DIAGS(" << I->first->getName() << ")}";