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 | 8b9d027 | 2009-03-18 21:16:16 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseSet.h" |
Chris Lattner | 457cde4 | 2009-04-15 20:13:18 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | af1465b | 2010-05-04 20:44:23 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringMap.h" |
John McCall | f14bacc | 2011-06-15 21:43:52 +0000 | [diff] [blame^] | 21 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | 457cde4 | 2009-04-15 20:13:18 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/VectorExtras.h" |
Ted Kremenek | 8b9d027 | 2009-03-18 21:16:16 +0000 | [diff] [blame] | 23 | #include <map> |
Douglas Gregor | 4954e9f | 2011-04-15 22:04:07 +0000 | [diff] [blame] | 24 | #include <algorithm> |
| 25 | #include <functional> |
Ted Kremenek | 04a847e | 2009-03-13 22:21:17 +0000 | [diff] [blame] | 26 | using namespace llvm; |
Ted Kremenek | 8b9d027 | 2009-03-18 21:16:16 +0000 | [diff] [blame] | 27 | |
| 28 | //===----------------------------------------------------------------------===// |
Chris Lattner | af1465b | 2010-05-04 20:44:23 +0000 | [diff] [blame] | 29 | // Diagnostic category computation code. |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
| 32 | namespace { |
| 33 | class DiagGroupParentMap { |
Chris Lattner | 67db883 | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 34 | RecordKeeper &Records; |
Chris Lattner | af1465b | 2010-05-04 20:44:23 +0000 | [diff] [blame] | 35 | std::map<const Record*, std::vector<Record*> > Mapping; |
| 36 | public: |
Chris Lattner | 67db883 | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 37 | DiagGroupParentMap(RecordKeeper &records) : Records(records) { |
Chris Lattner | af1465b | 2010-05-04 20:44:23 +0000 | [diff] [blame] | 38 | std::vector<Record*> DiagGroups |
| 39 | = Records.getAllDerivedDefinitions("DiagGroup"); |
| 40 | for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) { |
| 41 | std::vector<Record*> SubGroups = |
| 42 | DiagGroups[i]->getValueAsListOfDefs("SubGroups"); |
| 43 | for (unsigned j = 0, e = SubGroups.size(); j != e; ++j) |
| 44 | Mapping[SubGroups[j]].push_back(DiagGroups[i]); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | const std::vector<Record*> &getParents(const Record *Group) { |
| 49 | return Mapping[Group]; |
| 50 | } |
| 51 | }; |
| 52 | } // end anonymous namespace. |
| 53 | |
| 54 | |
| 55 | static std::string |
| 56 | getCategoryFromDiagGroup(const Record *Group, |
| 57 | DiagGroupParentMap &DiagGroupParents) { |
| 58 | // If the DiagGroup has a category, return it. |
| 59 | std::string CatName = Group->getValueAsString("CategoryName"); |
| 60 | if (!CatName.empty()) return CatName; |
| 61 | |
| 62 | // The diag group may the subgroup of one or more other diagnostic groups, |
| 63 | // check these for a category as well. |
| 64 | const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group); |
| 65 | for (unsigned i = 0, e = Parents.size(); i != e; ++i) { |
| 66 | CatName = getCategoryFromDiagGroup(Parents[i], DiagGroupParents); |
| 67 | if (!CatName.empty()) return CatName; |
| 68 | } |
| 69 | return ""; |
| 70 | } |
| 71 | |
| 72 | /// getDiagnosticCategory - Return the category that the specified diagnostic |
| 73 | /// lives in. |
| 74 | static std::string getDiagnosticCategory(const Record *R, |
| 75 | DiagGroupParentMap &DiagGroupParents) { |
Chris Lattner | ec5a0b3 | 2010-05-24 21:55:47 +0000 | [diff] [blame] | 76 | // If the diagnostic is in a group, and that group has a category, use it. |
| 77 | if (DefInit *Group = dynamic_cast<DefInit*>(R->getValueInit("Group"))) { |
| 78 | // Check the diagnostic's diag group for a category. |
| 79 | std::string CatName = getCategoryFromDiagGroup(Group->getDef(), |
| 80 | DiagGroupParents); |
| 81 | if (!CatName.empty()) return CatName; |
| 82 | } |
| 83 | |
Chris Lattner | af1465b | 2010-05-04 20:44:23 +0000 | [diff] [blame] | 84 | // If the diagnostic itself has a category, get it. |
Chris Lattner | ec5a0b3 | 2010-05-24 21:55:47 +0000 | [diff] [blame] | 85 | return R->getValueAsString("CategoryName"); |
Chris Lattner | af1465b | 2010-05-04 20:44:23 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | namespace { |
| 89 | class DiagCategoryIDMap { |
Chris Lattner | 67db883 | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 90 | RecordKeeper &Records; |
Chris Lattner | af1465b | 2010-05-04 20:44:23 +0000 | [diff] [blame] | 91 | StringMap<unsigned> CategoryIDs; |
| 92 | std::vector<std::string> CategoryStrings; |
| 93 | public: |
Chris Lattner | 67db883 | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 94 | DiagCategoryIDMap(RecordKeeper &records) : Records(records) { |
| 95 | DiagGroupParentMap ParentInfo(Records); |
Chris Lattner | af1465b | 2010-05-04 20:44:23 +0000 | [diff] [blame] | 96 | |
| 97 | // The zero'th category is "". |
| 98 | CategoryStrings.push_back(""); |
| 99 | CategoryIDs[""] = 0; |
| 100 | |
| 101 | std::vector<Record*> Diags = |
| 102 | Records.getAllDerivedDefinitions("Diagnostic"); |
| 103 | for (unsigned i = 0, e = Diags.size(); i != e; ++i) { |
| 104 | std::string Category = getDiagnosticCategory(Diags[i], ParentInfo); |
| 105 | if (Category.empty()) continue; // Skip diags with no category. |
| 106 | |
| 107 | unsigned &ID = CategoryIDs[Category]; |
| 108 | if (ID != 0) continue; // Already seen. |
| 109 | |
| 110 | ID = CategoryStrings.size(); |
| 111 | CategoryStrings.push_back(Category); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | unsigned getID(StringRef CategoryString) { |
| 116 | return CategoryIDs[CategoryString]; |
| 117 | } |
| 118 | |
| 119 | typedef std::vector<std::string>::iterator iterator; |
| 120 | iterator begin() { return CategoryStrings.begin(); } |
| 121 | iterator end() { return CategoryStrings.end(); } |
| 122 | }; |
| 123 | } // end anonymous namespace. |
| 124 | |
| 125 | |
Chris Lattner | af1465b | 2010-05-04 20:44:23 +0000 | [diff] [blame] | 126 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 8b9d027 | 2009-03-18 21:16:16 +0000 | [diff] [blame] | 127 | // Warning Tables (.inc file) generation. |
| 128 | //===----------------------------------------------------------------------===// |
| 129 | |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 130 | void ClangDiagsDefsEmitter::run(raw_ostream &OS) { |
Ted Kremenek | 557f7f8 | 2009-03-13 22:53:41 +0000 | [diff] [blame] | 131 | // Write the #if guard |
| 132 | if (!Component.empty()) { |
Chris Lattner | 9371c33 | 2009-04-15 20:16:12 +0000 | [diff] [blame] | 133 | std::string ComponentName = UppercaseString(Component); |
| 134 | OS << "#ifdef " << ComponentName << "START\n"; |
| 135 | OS << "__" << ComponentName << "START = DIAG_START_" << ComponentName |
| 136 | << ",\n"; |
| 137 | OS << "#undef " << ComponentName << "START\n"; |
Kovarththanan Rajaratnam | e7e891f | 2010-03-18 13:16:38 +0000 | [diff] [blame] | 138 | OS << "#endif\n\n"; |
Ted Kremenek | 557f7f8 | 2009-03-13 22:53:41 +0000 | [diff] [blame] | 139 | } |
Chris Lattner | 2f8c1d5 | 2009-04-15 20:55:08 +0000 | [diff] [blame] | 140 | |
| 141 | const std::vector<Record*> &Diags = |
| 142 | Records.getAllDerivedDefinitions("Diagnostic"); |
Ted Kremenek | 557f7f8 | 2009-03-13 22:53:41 +0000 | [diff] [blame] | 143 | |
Chris Lattner | 67db883 | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 144 | DiagCategoryIDMap CategoryIDs(Records); |
| 145 | DiagGroupParentMap DGParentMap(Records); |
Chris Lattner | af1465b | 2010-05-04 20:44:23 +0000 | [diff] [blame] | 146 | |
Chris Lattner | 2f8c1d5 | 2009-04-15 20:55:08 +0000 | [diff] [blame] | 147 | for (unsigned i = 0, e = Diags.size(); i != e; ++i) { |
| 148 | const Record &R = *Diags[i]; |
Chris Lattner | f1624aa | 2009-04-15 06:26:49 +0000 | [diff] [blame] | 149 | // Filter by component. |
| 150 | if (!Component.empty() && Component != R.getValueAsString("Component")) |
| 151 | continue; |
Ted Kremenek | 557f7f8 | 2009-03-13 22:53:41 +0000 | [diff] [blame] | 152 | |
Chris Lattner | 2f8c1d5 | 2009-04-15 20:55:08 +0000 | [diff] [blame] | 153 | OS << "DIAG(" << R.getName() << ", "; |
| 154 | OS << R.getValueAsDef("Class")->getName(); |
| 155 | OS << ", diag::" << R.getValueAsDef("DefaultMapping")->getName(); |
Chris Lattner | bfc0104 | 2009-04-16 05:52:18 +0000 | [diff] [blame] | 156 | |
| 157 | // Description string. |
Chris Lattner | 2f8c1d5 | 2009-04-15 20:55:08 +0000 | [diff] [blame] | 158 | OS << ", \""; |
Daniel Dunbar | 3446cf1 | 2009-10-17 20:43:19 +0000 | [diff] [blame] | 159 | OS.write_escaped(R.getValueAsString("Text")) << '"'; |
Chris Lattner | bfc0104 | 2009-04-16 05:52:18 +0000 | [diff] [blame] | 160 | |
| 161 | // Warning associated with the diagnostic. |
| 162 | if (DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Group"))) { |
Daniel Dunbar | 3446cf1 | 2009-10-17 20:43:19 +0000 | [diff] [blame] | 163 | OS << ", \""; |
| 164 | OS.write_escaped(DI->getDef()->getValueAsString("GroupName")) << '"'; |
Chris Lattner | bfc0104 | 2009-04-16 05:52:18 +0000 | [diff] [blame] | 165 | } else { |
Argyrios Kyrtzidis | 804cb23 | 2011-05-25 05:04:22 +0000 | [diff] [blame] | 166 | OS << ", \"\""; |
Chris Lattner | bfc0104 | 2009-04-16 05:52:18 +0000 | [diff] [blame] | 167 | } |
Douglas Gregor | 956244b | 2009-06-14 07:24:49 +0000 | [diff] [blame] | 168 | |
| 169 | // SFINAE bit |
| 170 | if (R.getValueAsBit("SFINAE")) |
| 171 | OS << ", true"; |
| 172 | else |
| 173 | OS << ", false"; |
Douglas Gregor | 75f6e89 | 2011-01-27 21:06:17 +0000 | [diff] [blame] | 174 | |
| 175 | // Access control bit |
| 176 | if (R.getValueAsBit("AccessControl")) |
| 177 | OS << ", true"; |
| 178 | else |
| 179 | OS << ", false"; |
| 180 | |
Chris Lattner | af1465b | 2010-05-04 20:44:23 +0000 | [diff] [blame] | 181 | // Category number. |
| 182 | OS << ", " << CategoryIDs.getID(getDiagnosticCategory(&R, DGParentMap)); |
Douglas Gregor | 4954e9f | 2011-04-15 22:04:07 +0000 | [diff] [blame] | 183 | |
| 184 | // Brief |
| 185 | OS << ", \""; |
| 186 | OS.write_escaped(R.getValueAsString("Brief")) << '"'; |
| 187 | |
| 188 | // Explanation |
| 189 | OS << ", \""; |
| 190 | OS.write_escaped(R.getValueAsString("Explanation")) << '"'; |
Chris Lattner | bfc0104 | 2009-04-16 05:52:18 +0000 | [diff] [blame] | 191 | OS << ")\n"; |
Ted Kremenek | 04a847e | 2009-03-13 22:21:17 +0000 | [diff] [blame] | 192 | } |
Ted Kremenek | 557f7f8 | 2009-03-13 22:53:41 +0000 | [diff] [blame] | 193 | } |
Ted Kremenek | 8b9d027 | 2009-03-18 21:16:16 +0000 | [diff] [blame] | 194 | |
| 195 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f8c1d5 | 2009-04-15 20:55:08 +0000 | [diff] [blame] | 196 | // Warning Group Tables generation |
Ted Kremenek | 8b9d027 | 2009-03-18 21:16:16 +0000 | [diff] [blame] | 197 | //===----------------------------------------------------------------------===// |
| 198 | |
John McCall | f14bacc | 2011-06-15 21:43:52 +0000 | [diff] [blame^] | 199 | static std::string getDiagCategoryEnum(llvm::StringRef name) { |
| 200 | if (name.empty()) |
| 201 | return "DiagCat_None"; |
| 202 | llvm::SmallString<256> enumName = llvm::StringRef("DiagCat_"); |
| 203 | for (llvm::StringRef::iterator I = name.begin(), E = name.end(); I != E; ++I) |
| 204 | enumName += isalnum(*I) ? *I : '_'; |
| 205 | return enumName.str(); |
| 206 | } |
| 207 | |
Argyrios Kyrtzidis | d91ef79 | 2011-02-13 07:51:19 +0000 | [diff] [blame] | 208 | namespace { |
Chris Lattner | d0d3494 | 2009-04-16 00:53:25 +0000 | [diff] [blame] | 209 | struct GroupInfo { |
| 210 | std::vector<const Record*> DiagsInGroup; |
| 211 | std::vector<std::string> SubGroups; |
Chris Lattner | 9efac56 | 2009-04-16 03:16:12 +0000 | [diff] [blame] | 212 | unsigned IDNo; |
Chris Lattner | d0d3494 | 2009-04-16 00:53:25 +0000 | [diff] [blame] | 213 | }; |
Argyrios Kyrtzidis | d91ef79 | 2011-02-13 07:51:19 +0000 | [diff] [blame] | 214 | } // end anonymous namespace. |
Chris Lattner | d0d3494 | 2009-04-16 00:53:25 +0000 | [diff] [blame] | 215 | |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 216 | void ClangDiagGroupsEmitter::run(raw_ostream &OS) { |
Chris Lattner | af1465b | 2010-05-04 20:44:23 +0000 | [diff] [blame] | 217 | // Compute a mapping from a DiagGroup to all of its parents. |
Chris Lattner | 67db883 | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 218 | DiagGroupParentMap DGParentMap(Records); |
Chris Lattner | af1465b | 2010-05-04 20:44:23 +0000 | [diff] [blame] | 219 | |
Chris Lattner | 2f8c1d5 | 2009-04-15 20:55:08 +0000 | [diff] [blame] | 220 | // Invert the 1-[0/1] mapping of diags to group into a one to many mapping of |
| 221 | // groups to diags in the group. |
Chris Lattner | d0d3494 | 2009-04-16 00:53:25 +0000 | [diff] [blame] | 222 | std::map<std::string, GroupInfo> DiagsInGroup; |
Chris Lattner | 457cde4 | 2009-04-15 20:13:18 +0000 | [diff] [blame] | 223 | |
Chris Lattner | a0ed8ca | 2009-04-15 22:33:02 +0000 | [diff] [blame] | 224 | std::vector<Record*> Diags = |
Chris Lattner | 2f8c1d5 | 2009-04-15 20:55:08 +0000 | [diff] [blame] | 225 | Records.getAllDerivedDefinitions("Diagnostic"); |
Chris Lattner | 2f8c1d5 | 2009-04-15 20:55:08 +0000 | [diff] [blame] | 226 | for (unsigned i = 0, e = Diags.size(); i != e; ++i) { |
| 227 | const Record *R = Diags[i]; |
| 228 | DefInit *DI = dynamic_cast<DefInit*>(R->getValueInit("Group")); |
| 229 | if (DI == 0) continue; |
Chris Lattner | d0d3494 | 2009-04-16 00:53:25 +0000 | [diff] [blame] | 230 | std::string GroupName = DI->getDef()->getValueAsString("GroupName"); |
| 231 | DiagsInGroup[GroupName].DiagsInGroup.push_back(R); |
Ted Kremenek | 8b9d027 | 2009-03-18 21:16:16 +0000 | [diff] [blame] | 232 | } |
Chris Lattner | 2f8c1d5 | 2009-04-15 20:55:08 +0000 | [diff] [blame] | 233 | |
Chris Lattner | a0ed8ca | 2009-04-15 22:33:02 +0000 | [diff] [blame] | 234 | // Add all DiagGroup's to the DiagsInGroup list to make sure we pick up empty |
| 235 | // groups (these are warnings that GCC supports that clang never produces). |
Chris Lattner | af1465b | 2010-05-04 20:44:23 +0000 | [diff] [blame] | 236 | std::vector<Record*> DiagGroups |
| 237 | = Records.getAllDerivedDefinitions("DiagGroup"); |
| 238 | for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) { |
| 239 | Record *Group = DiagGroups[i]; |
Chris Lattner | d0d3494 | 2009-04-16 00:53:25 +0000 | [diff] [blame] | 240 | GroupInfo &GI = DiagsInGroup[Group->getValueAsString("GroupName")]; |
| 241 | |
| 242 | std::vector<Record*> SubGroups = Group->getValueAsListOfDefs("SubGroups"); |
| 243 | for (unsigned j = 0, e = SubGroups.size(); j != e; ++j) |
| 244 | GI.SubGroups.push_back(SubGroups[j]->getValueAsString("GroupName")); |
Chris Lattner | a0ed8ca | 2009-04-15 22:33:02 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Chris Lattner | 9efac56 | 2009-04-16 03:16:12 +0000 | [diff] [blame] | 247 | // Assign unique ID numbers to the groups. |
| 248 | unsigned IDNo = 0; |
| 249 | for (std::map<std::string, GroupInfo>::iterator |
| 250 | I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I, ++IDNo) |
| 251 | I->second.IDNo = IDNo; |
| 252 | |
Chris Lattner | 2f8c1d5 | 2009-04-15 20:55:08 +0000 | [diff] [blame] | 253 | // Walk through the groups emitting an array for each diagnostic of the diags |
| 254 | // that are mapped to. |
| 255 | OS << "\n#ifdef GET_DIAG_ARRAYS\n"; |
Chris Lattner | 2f8c1d5 | 2009-04-15 20:55:08 +0000 | [diff] [blame] | 256 | unsigned MaxLen = 0; |
Chris Lattner | d0d3494 | 2009-04-16 00:53:25 +0000 | [diff] [blame] | 257 | for (std::map<std::string, GroupInfo>::iterator |
Chris Lattner | 2f8c1d5 | 2009-04-15 20:55:08 +0000 | [diff] [blame] | 258 | I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) { |
| 259 | MaxLen = std::max(MaxLen, (unsigned)I->first.size()); |
Ted Kremenek | 3ac82fe | 2009-03-18 21:28:47 +0000 | [diff] [blame] | 260 | |
Chris Lattner | d0d3494 | 2009-04-16 00:53:25 +0000 | [diff] [blame] | 261 | std::vector<const Record*> &V = I->second.DiagsInGroup; |
Chris Lattner | 9efac56 | 2009-04-16 03:16:12 +0000 | [diff] [blame] | 262 | if (!V.empty()) { |
| 263 | OS << "static const short DiagArray" << I->second.IDNo << "[] = { "; |
| 264 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 265 | OS << "diag::" << V[i]->getName() << ", "; |
| 266 | OS << "-1 };\n"; |
| 267 | } |
Chris Lattner | d0d3494 | 2009-04-16 00:53:25 +0000 | [diff] [blame] | 268 | |
Chris Lattner | 9efac56 | 2009-04-16 03:16:12 +0000 | [diff] [blame] | 269 | const std::vector<std::string> &SubGroups = I->second.SubGroups; |
| 270 | if (!SubGroups.empty()) { |
Chandler Carruth | 93ca7b6 | 2010-05-13 07:43:47 +0000 | [diff] [blame] | 271 | OS << "static const short DiagSubGroup" << I->second.IDNo << "[] = { "; |
Chris Lattner | 9efac56 | 2009-04-16 03:16:12 +0000 | [diff] [blame] | 272 | for (unsigned i = 0, e = SubGroups.size(); i != e; ++i) { |
| 273 | std::map<std::string, GroupInfo>::iterator RI = |
| 274 | DiagsInGroup.find(SubGroups[i]); |
| 275 | assert(RI != DiagsInGroup.end() && "Referenced without existing?"); |
| 276 | OS << RI->second.IDNo << ", "; |
| 277 | } |
| 278 | OS << "-1 };\n"; |
| 279 | } |
Ted Kremenek | 3ac82fe | 2009-03-18 21:28:47 +0000 | [diff] [blame] | 280 | } |
Chris Lattner | 2f8c1d5 | 2009-04-15 20:55:08 +0000 | [diff] [blame] | 281 | OS << "#endif // GET_DIAG_ARRAYS\n\n"; |
| 282 | |
| 283 | // Emit the table now. |
| 284 | OS << "\n#ifdef GET_DIAG_TABLE\n"; |
Chris Lattner | d0d3494 | 2009-04-16 00:53:25 +0000 | [diff] [blame] | 285 | for (std::map<std::string, GroupInfo>::iterator |
Chris Lattner | 2f8c1d5 | 2009-04-15 20:55:08 +0000 | [diff] [blame] | 286 | I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) { |
Chris Lattner | d0d3494 | 2009-04-16 00:53:25 +0000 | [diff] [blame] | 287 | // Group option string. |
Argyrios Kyrtzidis | 804cb23 | 2011-05-25 05:04:22 +0000 | [diff] [blame] | 288 | OS << " { "; |
| 289 | OS << I->first.size() << ", "; |
| 290 | OS << "\""; |
Daniel Dunbar | 3446cf1 | 2009-10-17 20:43:19 +0000 | [diff] [blame] | 291 | OS.write_escaped(I->first) << "\"," |
| 292 | << std::string(MaxLen-I->first.size()+1, ' '); |
Chris Lattner | d0d3494 | 2009-04-16 00:53:25 +0000 | [diff] [blame] | 293 | |
| 294 | // Diagnostics in the group. |
| 295 | if (I->second.DiagsInGroup.empty()) |
| 296 | OS << "0, "; |
| 297 | else |
Chris Lattner | 9efac56 | 2009-04-16 03:16:12 +0000 | [diff] [blame] | 298 | OS << "DiagArray" << I->second.IDNo << ", "; |
Chris Lattner | d0d3494 | 2009-04-16 00:53:25 +0000 | [diff] [blame] | 299 | |
Chris Lattner | 9efac56 | 2009-04-16 03:16:12 +0000 | [diff] [blame] | 300 | // Subgroups. |
| 301 | if (I->second.SubGroups.empty()) |
| 302 | OS << 0; |
| 303 | else |
| 304 | OS << "DiagSubGroup" << I->second.IDNo; |
Chris Lattner | d0d3494 | 2009-04-16 00:53:25 +0000 | [diff] [blame] | 305 | OS << " },\n"; |
Chris Lattner | 2f8c1d5 | 2009-04-15 20:55:08 +0000 | [diff] [blame] | 306 | } |
| 307 | OS << "#endif // GET_DIAG_TABLE\n\n"; |
Chris Lattner | af1465b | 2010-05-04 20:44:23 +0000 | [diff] [blame] | 308 | |
| 309 | // Emit the category table next. |
Chris Lattner | 67db883 | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 310 | DiagCategoryIDMap CategoriesByID(Records); |
Chris Lattner | af1465b | 2010-05-04 20:44:23 +0000 | [diff] [blame] | 311 | OS << "\n#ifdef GET_CATEGORY_TABLE\n"; |
| 312 | for (DiagCategoryIDMap::iterator I = CategoriesByID.begin(), |
| 313 | E = CategoriesByID.end(); I != E; ++I) |
John McCall | f14bacc | 2011-06-15 21:43:52 +0000 | [diff] [blame^] | 314 | OS << "CATEGORY(\"" << *I << "\", " << getDiagCategoryEnum(*I) << ")\n"; |
Chris Lattner | af1465b | 2010-05-04 20:44:23 +0000 | [diff] [blame] | 315 | OS << "#endif // GET_CATEGORY_TABLE\n\n"; |
Ted Kremenek | 8b9d027 | 2009-03-18 21:16:16 +0000 | [diff] [blame] | 316 | } |
Douglas Gregor | 4954e9f | 2011-04-15 22:04:07 +0000 | [diff] [blame] | 317 | |
| 318 | //===----------------------------------------------------------------------===// |
| 319 | // Diagnostic name index generation |
| 320 | //===----------------------------------------------------------------------===// |
| 321 | |
| 322 | namespace { |
| 323 | struct RecordIndexElement |
| 324 | { |
| 325 | RecordIndexElement() {} |
| 326 | explicit RecordIndexElement(Record const &R): |
| 327 | Name(R.getName()) {} |
| 328 | |
| 329 | std::string Name; |
| 330 | }; |
| 331 | |
| 332 | struct RecordIndexElementSorter : |
| 333 | public std::binary_function<RecordIndexElement, RecordIndexElement, bool> { |
| 334 | |
| 335 | bool operator()(RecordIndexElement const &Lhs, |
| 336 | RecordIndexElement const &Rhs) const { |
| 337 | return Lhs.Name < Rhs.Name; |
| 338 | } |
| 339 | |
| 340 | }; |
| 341 | |
| 342 | } // end anonymous namespace. |
| 343 | |
| 344 | void ClangDiagsIndexNameEmitter::run(raw_ostream &OS) { |
| 345 | const std::vector<Record*> &Diags = |
| 346 | Records.getAllDerivedDefinitions("Diagnostic"); |
| 347 | |
| 348 | std::vector<RecordIndexElement> Index; |
| 349 | Index.reserve(Diags.size()); |
| 350 | for (unsigned i = 0, e = Diags.size(); i != e; ++i) { |
| 351 | const Record &R = *(Diags[i]); |
| 352 | Index.push_back(RecordIndexElement(R)); |
| 353 | } |
| 354 | |
| 355 | std::sort(Index.begin(), Index.end(), RecordIndexElementSorter()); |
| 356 | |
| 357 | for (unsigned i = 0, e = Index.size(); i != e; ++i) { |
| 358 | const RecordIndexElement &R = Index[i]; |
| 359 | |
| 360 | OS << "DIAG_NAME_INDEX(" << R.Name << ")\n"; |
| 361 | } |
| 362 | } |