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