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