Ted Kremenek | 04a847e | 2009-03-13 22:21:17 +0000 | [diff] [blame] | 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" |
Ted Kremenek | 0f9d510 | 2009-03-18 21:36:46 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | 04a847e | 2009-03-13 22:21:17 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Streams.h" |
| 19 | #include "llvm/ADT/VectorExtras.h" |
Ted Kremenek | 8b9d027 | 2009-03-18 21:16:16 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/DenseSet.h" |
| 21 | #include <set> |
| 22 | #include <map> |
Ted Kremenek | 04a847e | 2009-03-13 22:21:17 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace llvm; |
Ted Kremenek | 8b9d027 | 2009-03-18 21:16:16 +0000 | [diff] [blame] | 25 | |
| 26 | //===----------------------------------------------------------------------===// |
Jim Grosbach | da4231f | 2009-03-26 16:17:51 +0000 | [diff] [blame^] | 27 | // Generic routines for all Clang TableGen backends. |
Ted Kremenek | 8b9d027 | 2009-03-18 21:16:16 +0000 | [diff] [blame] | 28 | //===----------------------------------------------------------------------===// |
| 29 | |
Ted Kremenek | 04a847e | 2009-03-13 22:21:17 +0000 | [diff] [blame] | 30 | typedef std::vector<Record*> RecordVector; |
| 31 | typedef std::vector<Record*> SuperClassVector; |
| 32 | typedef std::vector<RecordVal> RecordValVector; |
| 33 | |
Ted Kremenek | 557f7f8 | 2009-03-13 22:53:41 +0000 | [diff] [blame] | 34 | static const RecordVal* findRecordVal(const Record& R, const std::string &key) { |
| 35 | const RecordValVector &Vals = R.getValues(); |
Ted Kremenek | 04a847e | 2009-03-13 22:21:17 +0000 | [diff] [blame] | 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 const Record* getDiagKind(const Record* DiagClass, const Record &R) { |
| 44 | const SuperClassVector &SC = R.getSuperClasses(); |
| 45 | for (SuperClassVector::const_iterator I=SC.begin(), E=SC.end(); I!=E; ++I) |
Ted Kremenek | 7fae82f | 2009-03-19 17:18:09 +0000 | [diff] [blame] | 46 | if ((*I)->isSubClassOf(DiagClass) && |
| 47 | (*I)->getName() != "DiagnosticControlled") |
Ted Kremenek | 04a847e | 2009-03-13 22:21:17 +0000 | [diff] [blame] | 48 | return *I; |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | static void EmitEscaped(std::ostream& OS, const std::string &s) { |
| 54 | for (std::string::const_iterator I=s.begin(), E=s.end(); I!=E; ++I) |
| 55 | switch (*I) { |
| 56 | default: OS << *I; break; |
| 57 | case '\"': OS << "\\" << *I; break; |
| 58 | case '\\': OS << "\\\\"; break; |
| 59 | } |
| 60 | } |
| 61 | |
Ted Kremenek | 557f7f8 | 2009-03-13 22:53:41 +0000 | [diff] [blame] | 62 | static void EmitAllCaps(std::ostream& OS, const std::string &s) { |
| 63 | for (std::string::const_iterator I=s.begin(), E=s.end(); I!=E; ++I) |
| 64 | OS << char(toupper(*I)); |
| 65 | } |
| 66 | |
Ted Kremenek | 8b9d027 | 2009-03-18 21:16:16 +0000 | [diff] [blame] | 67 | //===----------------------------------------------------------------------===// |
| 68 | // Warning Tables (.inc file) generation. |
| 69 | //===----------------------------------------------------------------------===// |
| 70 | |
Ted Kremenek | 04a847e | 2009-03-13 22:21:17 +0000 | [diff] [blame] | 71 | static void ProcessDiag(std::ostream& OS, const Record* DiagClass, |
| 72 | const Record& R) { |
| 73 | |
| 74 | const Record* DiagKind = getDiagKind(DiagClass, R); |
| 75 | if (!DiagKind) |
| 76 | return; |
| 77 | |
| 78 | OS << "DIAG(" << R.getName() << ", "; |
Ted Kremenek | 557f7f8 | 2009-03-13 22:53:41 +0000 | [diff] [blame] | 79 | EmitAllCaps(OS, DiagKind->getName()); |
Ted Kremenek | 04a847e | 2009-03-13 22:21:17 +0000 | [diff] [blame] | 80 | |
Ted Kremenek | 557f7f8 | 2009-03-13 22:53:41 +0000 | [diff] [blame] | 81 | const RecordVal* Text = findRecordVal(R, "Text"); |
Ted Kremenek | 04a847e | 2009-03-13 22:21:17 +0000 | [diff] [blame] | 82 | assert(Text && "No 'Text' entry in Diagnostic."); |
| 83 | const StringInit* TextVal = dynamic_cast<const StringInit*>(Text->getValue()); |
| 84 | assert(TextVal && "Value 'Text' must be a string."); |
| 85 | OS << ", \""; |
| 86 | EmitEscaped(OS, TextVal->getValue()); |
| 87 | OS << "\")\n"; |
| 88 | } |
| 89 | |
| 90 | void ClangDiagsDefsEmitter::run(std::ostream &OS) { |
| 91 | const RecordVector &Diags = Records.getAllDerivedDefinitions("Diagnostic"); |
| 92 | |
| 93 | const Record* DiagClass = Records.getClass("Diagnostic"); |
| 94 | assert(DiagClass && "No Diagnostic class defined."); |
| 95 | |
Ted Kremenek | 557f7f8 | 2009-03-13 22:53:41 +0000 | [diff] [blame] | 96 | // Write the #if guard |
| 97 | if (!Component.empty()) { |
| 98 | OS << "#ifdef "; |
| 99 | EmitAllCaps(OS, Component); |
| 100 | OS << "START\n__"; |
| 101 | EmitAllCaps(OS, Component); |
| 102 | OS << "START = DIAG_START_"; |
| 103 | EmitAllCaps(OS, Component); |
| 104 | OS << ",\n#undef "; |
| 105 | EmitAllCaps(OS, Component); |
| 106 | OS << "START\n#endif\n"; |
| 107 | } |
| 108 | |
Ted Kremenek | 04a847e | 2009-03-13 22:21:17 +0000 | [diff] [blame] | 109 | for (RecordVector::const_iterator I=Diags.begin(), E=Diags.end(); I!=E; ++I) { |
Ted Kremenek | 557f7f8 | 2009-03-13 22:53:41 +0000 | [diff] [blame] | 110 | if (!Component.empty()) { |
| 111 | const RecordVal* V = findRecordVal(**I, "Component"); |
| 112 | if (!V) |
| 113 | continue; |
| 114 | |
| 115 | const StringInit* SV = dynamic_cast<const StringInit*>(V->getValue()); |
Ted Kremenek | 13b9bf9 | 2009-03-23 21:54:33 +0000 | [diff] [blame] | 116 | if (!SV || SV->getValue() != Component) |
Ted Kremenek | 557f7f8 | 2009-03-13 22:53:41 +0000 | [diff] [blame] | 117 | continue; |
| 118 | } |
| 119 | |
Ted Kremenek | 04a847e | 2009-03-13 22:21:17 +0000 | [diff] [blame] | 120 | ProcessDiag(OS, DiagClass, **I); |
| 121 | } |
Ted Kremenek | 557f7f8 | 2009-03-13 22:53:41 +0000 | [diff] [blame] | 122 | } |
Ted Kremenek | 8b9d027 | 2009-03-18 21:16:16 +0000 | [diff] [blame] | 123 | |
| 124 | //===----------------------------------------------------------------------===// |
| 125 | // Warning Group Tables generation. |
| 126 | //===----------------------------------------------------------------------===// |
| 127 | |
Ted Kremenek | 0f9d510 | 2009-03-18 21:36:46 +0000 | [diff] [blame] | 128 | static const std::string &getOptName(const Record *R) { |
| 129 | const RecordVal *V = findRecordVal(*R, "Name"); |
| 130 | assert(V && "Options must have a 'Name' value."); |
| 131 | const StringInit* SV = dynamic_cast<const StringInit*>(V->getValue()); |
| 132 | assert(SV && "'Name' entry must be a string."); |
| 133 | return SV->getValue(); |
| 134 | } |
| 135 | |
| 136 | namespace { |
| 137 | struct VISIBILITY_HIDDEN CompareOptName { |
| 138 | bool operator()(const Record* A, const Record* B) { |
| 139 | return getOptName(A) < getOptName(B); |
| 140 | } |
| 141 | }; |
| 142 | } |
| 143 | |
Ted Kremenek | 8b9d027 | 2009-03-18 21:16:16 +0000 | [diff] [blame] | 144 | typedef std::set<const Record*> DiagnosticSet; |
Ted Kremenek | 0f9d510 | 2009-03-18 21:36:46 +0000 | [diff] [blame] | 145 | typedef std::map<const Record*, DiagnosticSet, CompareOptName> OptionMap; |
Ted Kremenek | 8b9d027 | 2009-03-18 21:16:16 +0000 | [diff] [blame] | 146 | typedef llvm::DenseSet<const ListInit*> VisitedLists; |
| 147 | |
| 148 | static void BuildGroup(DiagnosticSet& DS, VisitedLists &Visited, const Init* X); |
| 149 | |
| 150 | static void BuildGroup(DiagnosticSet &DS, VisitedLists &Visited, |
| 151 | const ListInit* LV) { |
| 152 | |
| 153 | // Simple hack to prevent including a list multiple times. This may be useful |
| 154 | // if one declares an Option by including a bunch of other Options that |
| 155 | // include other Options, etc. |
| 156 | if (Visited.count(LV)) |
| 157 | return; |
| 158 | |
| 159 | Visited.insert(LV); |
| 160 | |
| 161 | // Iterate through the list and grab all DiagnosticControlled. |
| 162 | for (ListInit::const_iterator I = LV->begin(), E = LV->end(); I!=E; ++I) |
| 163 | BuildGroup(DS, Visited, *I); |
| 164 | } |
| 165 | |
| 166 | static void BuildGroup(DiagnosticSet& DS, VisitedLists &Visited, |
| 167 | const Record *Def) { |
| 168 | |
| 169 | // If an Option includes another Option, inline the Diagnostics of the |
| 170 | // included Option. |
| 171 | if (Def->isSubClassOf("Option")) { |
| 172 | if (const RecordVal* V = findRecordVal(*Def, "Members")) |
| 173 | if (const ListInit* LV = dynamic_cast<const ListInit*>(V->getValue())) |
| 174 | BuildGroup(DS, Visited, LV); |
| 175 | |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | if (Def->isSubClassOf("DiagnosticControlled")) |
| 180 | DS.insert(Def); |
| 181 | } |
| 182 | |
| 183 | static void BuildGroup(DiagnosticSet& DS, VisitedLists &Visited, |
| 184 | const Init* X) { |
| 185 | |
| 186 | if (const DefInit *D = dynamic_cast<const DefInit*>(X)) |
| 187 | BuildGroup(DS, Visited, D->getDef()); |
| 188 | |
| 189 | // We may have some other cases here in the future. |
| 190 | } |
| 191 | |
| 192 | |
| 193 | void ClangOptionsEmitter::run(std::ostream &OS) { |
| 194 | // Build up a map from options to controlled diagnostics. |
| 195 | OptionMap OM; |
| 196 | |
| 197 | const RecordVector &Opts = Records.getAllDerivedDefinitions("Option"); |
| 198 | for (RecordVector::const_iterator I=Opts.begin(), E=Opts.end(); I!=E; ++I) |
| 199 | if (const RecordVal* V = findRecordVal(**I, "Members")) |
| 200 | if (const ListInit* LV = dynamic_cast<const ListInit*>(V->getValue())) { |
| 201 | VisitedLists Visited; |
| 202 | BuildGroup(OM[*I], Visited, LV); |
| 203 | } |
| 204 | |
| 205 | // Iterate through the OptionMap and emit the declarations. |
| 206 | for (OptionMap::iterator I = OM.begin(), E = OM.end(); I!=E; ++I) { |
Ted Kremenek | 8b9d027 | 2009-03-18 21:16:16 +0000 | [diff] [blame] | 207 | // Output the option. |
| 208 | OS << "static const diag::kind " << I->first->getName() << "[] = { "; |
| 209 | |
| 210 | DiagnosticSet &DS = I->second; |
| 211 | bool first = true; |
| 212 | for (DiagnosticSet::iterator I2 = DS.begin(), E2 = DS.end(); I2!=E2; ++I2) { |
| 213 | if (first) |
| 214 | first = false; |
| 215 | else |
| 216 | OS << ", "; |
| 217 | |
| 218 | OS << "diag::" << (*I2)->getName(); |
| 219 | } |
| 220 | OS << " };\n"; |
| 221 | } |
Ted Kremenek | 3ac82fe | 2009-03-18 21:28:47 +0000 | [diff] [blame] | 222 | |
| 223 | // Now emit the OptionTable table. |
| 224 | OS << "\nstatic const WarningOption OptionTable[] = {"; |
| 225 | bool first = true; |
| 226 | for (OptionMap::iterator I = OM.begin(), E = OM.end(); I!=E; ++I) { |
Ted Kremenek | 3ac82fe | 2009-03-18 21:28:47 +0000 | [diff] [blame] | 227 | if (first) |
| 228 | first = false; |
| 229 | else |
| 230 | OS << ','; |
| 231 | |
Ted Kremenek | 0f9d510 | 2009-03-18 21:36:46 +0000 | [diff] [blame] | 232 | OS << "\n {\"" << getOptName(I->first) |
Ted Kremenek | 3ac82fe | 2009-03-18 21:28:47 +0000 | [diff] [blame] | 233 | << "\", DIAGS(" << I->first->getName() << ")}"; |
| 234 | } |
| 235 | OS << "\n};\n"; |
Ted Kremenek | 8b9d027 | 2009-03-18 21:16:16 +0000 | [diff] [blame] | 236 | } |