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