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/Compiler.h"
18 #include "llvm/Support/Streams.h"
19 #include "llvm/ADT/VectorExtras.h"
20 #include "llvm/ADT/DenseSet.h"
25 //===----------------------------------------------------------------------===//
26 // Generic routines for all Clang TableGen backends.
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 void EmitEscaped(std::ostream& OS, const std::string &s) {
43 for (std::string::const_iterator I=s.begin(), E=s.end(); I!=E; ++I)
45 default: OS << *I; break;
46 case '\"': OS << "\\" << *I; break;
47 case '\\': OS << "\\\\"; break;
51 static 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));
56 //===----------------------------------------------------------------------===//
57 // Warning Tables (.inc file) generation.
58 //===----------------------------------------------------------------------===//
60 static void ProcessDiag(std::ostream &OS, const Record *DiagClass,
62 OS << "DIAG(" << R.getName() << ", ";
63 OS << R.getValueAsDef("Class")->getName();
64 OS << ", diag::" << R.getValueAsDef("DefaultMapping")->getName();
66 EmitEscaped(OS, R.getValueAsString("Text"));
70 void ClangDiagsDefsEmitter::run(std::ostream &OS) {
71 const RecordVector &Diags = Records.getAllDerivedDefinitions("Diagnostic");
73 const Record* DiagClass = Records.getClass("Diagnostic");
74 assert(DiagClass && "No Diagnostic class defined.");
76 // Write the #if guard
77 if (!Component.empty()) {
79 EmitAllCaps(OS, Component);
81 EmitAllCaps(OS, Component);
82 OS << "START = DIAG_START_";
83 EmitAllCaps(OS, Component);
85 EmitAllCaps(OS, Component);
86 OS << "START\n#endif\n";
89 for (RecordVector::const_iterator I=Diags.begin(), E=Diags.end(); I!=E; ++I) {
90 const Record &R = **I;
91 // Filter by component.
92 if (!Component.empty() && Component != R.getValueAsString("Component"))
95 ProcessDiag(OS, DiagClass, R);
99 //===----------------------------------------------------------------------===//
100 // Warning Group Tables generation.
101 //===----------------------------------------------------------------------===//
103 static 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();
112 struct VISIBILITY_HIDDEN CompareOptName {
113 bool operator()(const Record* A, const Record* B) const {
114 return getOptName(A) < getOptName(B);
119 typedef std::set<const Record*> DiagnosticSet;
120 typedef std::map<const Record*, DiagnosticSet, CompareOptName> OptionMap;
121 typedef llvm::DenseSet<const ListInit*> VisitedLists;
123 static void BuildGroup(DiagnosticSet& DS, VisitedLists &Visited, const Init* X);
125 static void BuildGroup(DiagnosticSet &DS, VisitedLists &Visited,
126 const ListInit* LV) {
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))
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);
141 static void BuildGroup(DiagnosticSet& DS, VisitedLists &Visited,
144 // If an Option includes another Option, inline the Diagnostics of the
146 if (Def->isSubClassOf("Option")) {
147 if (const RecordVal *V = findRecordVal(*Def, "Members"))
148 if (const ListInit *LV = dynamic_cast<const ListInit*>(V->getValue()))
149 BuildGroup(DS, Visited, LV);
154 if (Def->isSubClassOf("DiagnosticControlled"))
158 static void BuildGroup(DiagnosticSet& DS, VisitedLists &Visited,
161 if (const DefInit *D = dynamic_cast<const DefInit*>(X))
162 BuildGroup(DS, Visited, D->getDef());
164 // We may have some other cases here in the future.
168 void ClangOptionsEmitter::run(std::ostream &OS) {
169 // Build up a map from options to controlled diagnostics.
172 const RecordVector &Opts = Records.getAllDerivedDefinitions("Option");
173 for (RecordVector::const_iterator I=Opts.begin(), E=Opts.end(); I != E; ++I)
174 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);
180 // Iterate through the OptionMap and emit the declarations.
181 for (OptionMap::iterator I = OM.begin(), E = OM.end(); I!=E; ++I) {
182 // Output the option.
183 OS << "static const diag::kind " << I->first->getName() << "[] = { ";
185 DiagnosticSet &DS = I->second;
187 for (DiagnosticSet::iterator I2 = DS.begin(), E2 = DS.end(); I2!=E2; ++I2) {
193 OS << "diag::" << (*I2)->getName();
198 // Now emit the OptionTable table.
199 OS << "\nstatic const WarningOption OptionTable[] = {";
201 for (OptionMap::iterator I = OM.begin(), E = OM.end(); I!=E; ++I) {
207 OS << "\n {\"" << getOptName(I->first)
208 << "\", DIAGS(" << I->first->getName() << ")}";