use escape string.
[oota-llvm.git] / utils / TableGen / ClangDiagnosticsEmitter.cpp
1 //=- 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"
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/Streams.h"
19 #include "llvm/ADT/DenseSet.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/ADT/VectorExtras.h"
22 #include <set>
23 #include <map>
24 using namespace llvm;
25
26 //===----------------------------------------------------------------------===//
27 // Generic routines for all Clang TableGen backends.
28 //===----------------------------------------------------------------------===//
29
30 typedef std::vector<Record*> RecordVector;
31 typedef std::vector<Record*> SuperClassVector;
32 typedef std::vector<RecordVal> RecordValVector;
33
34 static const RecordVal* findRecordVal(const Record& R, const std::string &key) {  
35   const RecordValVector &Vals = R.getValues();
36   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
43 static 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
48 //===----------------------------------------------------------------------===//
49 // Warning Tables (.inc file) generation.
50 //===----------------------------------------------------------------------===//
51
52 static void ProcessDiag(std::ostream &OS, const Record *DiagClass,
53                         const Record &R) {
54   OS << "DIAG(" << R.getName() << ", ";
55   OS << R.getValueAsDef("Class")->getName();
56   OS << ", diag::" << R.getValueAsDef("DefaultMapping")->getName();
57   OS << ", \"";
58   std::string S = R.getValueAsString("Text");
59   EscapeString(S);
60   OS << S << "\")\n";
61 }
62
63 void 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   
69   // 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   
82   for (RecordVector::const_iterator I=Diags.begin(), E=Diags.end(); I!=E; ++I) {
83     const Record &R = **I;
84     // Filter by component.
85     if (!Component.empty() && Component != R.getValueAsString("Component"))
86       continue;
87     
88     ProcessDiag(OS, DiagClass, R);
89   }
90 }
91
92 //===----------------------------------------------------------------------===//
93 // Warning Group Tables generation.
94 //===----------------------------------------------------------------------===//
95
96 static 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
104 namespace {
105 struct VISIBILITY_HIDDEN CompareOptName {  
106   bool operator()(const Record* A, const Record* B) const {
107     return getOptName(A) < getOptName(B);
108   }
109 };
110 }
111
112 typedef std::set<const Record*> DiagnosticSet;
113 typedef std::map<const Record*, DiagnosticSet, CompareOptName> OptionMap;
114 typedef llvm::DenseSet<const ListInit*> VisitedLists;
115
116 static void BuildGroup(DiagnosticSet& DS, VisitedLists &Visited, const Init* X);
117
118 static 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
134 static 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")) {
140     if (const RecordVal *V = findRecordVal(*Def, "Members"))
141       if (const ListInit *LV = dynamic_cast<const ListInit*>(V->getValue()))
142         BuildGroup(DS, Visited, LV);
143
144     return;
145   }
146   
147   if (Def->isSubClassOf("DiagnosticControlled"))
148     DS.insert(Def);
149 }
150
151 static 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
161 void ClangDiagGroupsEmitter::run(std::ostream &OS) {
162   // Build up a map from options to controlled diagnostics.
163   OptionMap OM;
164   
165   const RecordVector &Opts = Records.getAllDerivedDefinitions("Option");
166   for (RecordVector::const_iterator I=Opts.begin(), E=Opts.end(); I != E; ++I)
167     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) {    
175     // 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   }
190     
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) {
195     if (first)
196       first = false;
197     else
198       OS << ',';
199     
200     OS << "\n  {\"" << getOptName(I->first)
201        << "\", DIAGS(" << I->first->getName() << ")}";
202   }
203   OS << "\n};\n";
204 }