Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +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 | |
Ted Kremenek | e8cf7d1 | 2012-07-07 05:53:30 +0000 | [diff] [blame^] | 14 | #include "llvm/ADT/PointerUnion.h" |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/DenseSet.h" |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallString.h" |
Jakob Stoklund Olesen | 3cc509b | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringMap.h" |
Ted Kremenek | e8cf7d1 | 2012-07-07 05:53:30 +0000 | [diff] [blame^] | 18 | #include "llvm/ADT/Optional.h" |
Jakob Stoklund Olesen | 3cc509b | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Compiler.h" |
| 20 | #include "llvm/Support/Debug.h" |
| 21 | #include "llvm/TableGen/Record.h" |
| 22 | #include "llvm/TableGen/TableGenBackend.h" |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 23 | #include <algorithm> |
| 24 | #include <functional> |
Jakob Stoklund Olesen | 3cc509b | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 25 | #include <map> |
Benjamin Kramer | d49cb20 | 2012-02-15 20:57:03 +0000 | [diff] [blame] | 26 | #include <set> |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | // Diagnostic category computation code. |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
| 33 | namespace { |
| 34 | class DiagGroupParentMap { |
| 35 | RecordKeeper &Records; |
| 36 | std::map<const Record*, std::vector<Record*> > Mapping; |
| 37 | public: |
| 38 | DiagGroupParentMap(RecordKeeper &records) : Records(records) { |
| 39 | std::vector<Record*> DiagGroups |
| 40 | = Records.getAllDerivedDefinitions("DiagGroup"); |
| 41 | for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) { |
| 42 | std::vector<Record*> SubGroups = |
| 43 | DiagGroups[i]->getValueAsListOfDefs("SubGroups"); |
| 44 | for (unsigned j = 0, e = SubGroups.size(); j != e; ++j) |
| 45 | Mapping[SubGroups[j]].push_back(DiagGroups[i]); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | const std::vector<Record*> &getParents(const Record *Group) { |
| 50 | return Mapping[Group]; |
| 51 | } |
| 52 | }; |
| 53 | } // end anonymous namespace. |
| 54 | |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 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) { |
| 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 | } |
Ted Kremenek | e8cf7d1 | 2012-07-07 05:53:30 +0000 | [diff] [blame^] | 83 | |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 84 | // If the diagnostic itself has a category, get it. |
| 85 | return R->getValueAsString("CategoryName"); |
| 86 | } |
| 87 | |
| 88 | namespace { |
| 89 | class DiagCategoryIDMap { |
| 90 | RecordKeeper &Records; |
| 91 | StringMap<unsigned> CategoryIDs; |
| 92 | std::vector<std::string> CategoryStrings; |
| 93 | public: |
| 94 | DiagCategoryIDMap(RecordKeeper &records) : Records(records) { |
| 95 | DiagGroupParentMap ParentInfo(Records); |
| 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 | }; |
Argyrios Kyrtzidis | d42236e | 2012-03-06 00:00:38 +0000 | [diff] [blame] | 123 | |
| 124 | struct GroupInfo { |
| 125 | std::vector<const Record*> DiagsInGroup; |
| 126 | std::vector<std::string> SubGroups; |
| 127 | unsigned IDNo; |
| 128 | }; |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 129 | } // end anonymous namespace. |
| 130 | |
Argyrios Kyrtzidis | d42236e | 2012-03-06 00:00:38 +0000 | [diff] [blame] | 131 | /// \brief Invert the 1-[0/1] mapping of diags to group into a one to many |
| 132 | /// mapping of groups to diags in the group. |
| 133 | static void groupDiagnostics(const std::vector<Record*> &Diags, |
| 134 | const std::vector<Record*> &DiagGroups, |
| 135 | std::map<std::string, GroupInfo> &DiagsInGroup) { |
| 136 | for (unsigned i = 0, e = Diags.size(); i != e; ++i) { |
| 137 | const Record *R = Diags[i]; |
| 138 | DefInit *DI = dynamic_cast<DefInit*>(R->getValueInit("Group")); |
| 139 | if (DI == 0) continue; |
Richard Smith | 4648477 | 2012-05-04 19:05:50 +0000 | [diff] [blame] | 140 | assert(R->getValueAsDef("Class")->getName() != "CLASS_NOTE" && |
| 141 | "Note can't be in a DiagGroup"); |
Argyrios Kyrtzidis | d42236e | 2012-03-06 00:00:38 +0000 | [diff] [blame] | 142 | std::string GroupName = DI->getDef()->getValueAsString("GroupName"); |
| 143 | DiagsInGroup[GroupName].DiagsInGroup.push_back(R); |
| 144 | } |
| 145 | |
| 146 | // Add all DiagGroup's to the DiagsInGroup list to make sure we pick up empty |
| 147 | // groups (these are warnings that GCC supports that clang never produces). |
| 148 | for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) { |
| 149 | Record *Group = DiagGroups[i]; |
| 150 | GroupInfo &GI = DiagsInGroup[Group->getValueAsString("GroupName")]; |
| 151 | |
| 152 | std::vector<Record*> SubGroups = Group->getValueAsListOfDefs("SubGroups"); |
| 153 | for (unsigned j = 0, e = SubGroups.size(); j != e; ++j) |
| 154 | GI.SubGroups.push_back(SubGroups[j]->getValueAsString("GroupName")); |
| 155 | } |
| 156 | |
| 157 | // Assign unique ID numbers to the groups. |
| 158 | unsigned IDNo = 0; |
| 159 | for (std::map<std::string, GroupInfo>::iterator |
| 160 | I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I, ++IDNo) |
| 161 | I->second.IDNo = IDNo; |
| 162 | } |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 163 | |
| 164 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e8cf7d1 | 2012-07-07 05:53:30 +0000 | [diff] [blame^] | 165 | // Infer members of -Wpedantic. |
| 166 | //===----------------------------------------------------------------------===// |
| 167 | |
| 168 | typedef std::vector<const Record *> RecordVec; |
| 169 | typedef llvm::DenseSet<const Record *> RecordSet; |
| 170 | typedef llvm::PointerUnion<RecordVec*, RecordSet*> VecOrSet; |
| 171 | |
| 172 | namespace { |
| 173 | class InferPedantic { |
| 174 | typedef llvm::DenseMap<const Record*, |
| 175 | std::pair<unsigned, llvm::Optional<unsigned> > > GMap; |
| 176 | |
| 177 | DiagGroupParentMap &DiagGroupParents; |
| 178 | const std::vector<Record*> &Diags; |
| 179 | const std::vector<Record*> DiagGroups; |
| 180 | std::map<std::string, GroupInfo> &DiagsInGroup; |
| 181 | llvm::DenseSet<const Record*> DiagsSet; |
| 182 | GMap GroupCount; |
| 183 | public: |
| 184 | InferPedantic(DiagGroupParentMap &DiagGroupParents, |
| 185 | const std::vector<Record*> &Diags, |
| 186 | const std::vector<Record*> &DiagGroups, |
| 187 | std::map<std::string, GroupInfo> &DiagsInGroup) |
| 188 | : DiagGroupParents(DiagGroupParents), |
| 189 | Diags(Diags), |
| 190 | DiagGroups(DiagGroups), |
| 191 | DiagsInGroup(DiagsInGroup) {} |
| 192 | |
| 193 | /// Compute the set of diagnostics and groups that are immediately |
| 194 | /// in -Wpedantic. |
| 195 | void compute(VecOrSet DiagsInPedantic, |
| 196 | VecOrSet GroupsInPedantic); |
| 197 | |
| 198 | private: |
| 199 | /// Determine whether a group is a subgroup of another group. |
| 200 | bool isSubGroupOfGroup(const Record *Group, |
| 201 | llvm::StringRef RootGroupName); |
| 202 | |
| 203 | /// Determine if the diagnostic is an extension. |
| 204 | bool isExtension(const Record *Diag); |
| 205 | |
| 206 | /// Increment the count for a group, and transitively marked |
| 207 | /// parent groups when appropriate. |
| 208 | void markGroup(const Record *Group); |
| 209 | |
| 210 | /// Return true if the diagnostic is in a pedantic group. |
| 211 | bool groupInPedantic(const Record *Group, bool increment = false); |
| 212 | }; |
| 213 | } // end anonymous namespace |
| 214 | |
| 215 | bool InferPedantic::isSubGroupOfGroup(const Record *Group, |
| 216 | llvm::StringRef GName) { |
| 217 | |
| 218 | const std::string &GroupName = Group->getValueAsString("GroupName"); |
| 219 | if (GName == GroupName) |
| 220 | return true; |
| 221 | |
| 222 | const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group); |
| 223 | for (unsigned i = 0, e = Parents.size(); i != e; ++i) |
| 224 | if (isSubGroupOfGroup(Parents[i], GName)) |
| 225 | return true; |
| 226 | |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | /// Determine if the diagnostic is an extension. |
| 231 | bool InferPedantic::isExtension(const Record *Diag) { |
| 232 | const std::string &ClsName = Diag->getValueAsDef("Class")->getName(); |
| 233 | return ClsName == "CLASS_EXTENSION"; |
| 234 | } |
| 235 | |
| 236 | bool InferPedantic::groupInPedantic(const Record *Group, bool increment) { |
| 237 | GMap::mapped_type &V = GroupCount[Group]; |
| 238 | // Lazily compute the threshold value for the group count. |
| 239 | if (!V.second.hasValue()) { |
| 240 | const GroupInfo &GI = DiagsInGroup[Group->getValueAsString("GroupName")]; |
| 241 | V.second = GI.SubGroups.size() + GI.DiagsInGroup.size(); |
| 242 | } |
| 243 | |
| 244 | if (increment) |
| 245 | ++V.first; |
| 246 | |
| 247 | // Consider a group in -Wpendatic IFF if has at least one diagnostic |
| 248 | // or subgroup AND all of those diagnostics and subgroups are covered |
| 249 | // by -Wpedantic via our computation. |
| 250 | return V.first != 0 && V.first == V.second.getValue(); |
| 251 | } |
| 252 | |
| 253 | void InferPedantic::markGroup(const Record *Group) { |
| 254 | // If all the diagnostics and subgroups have been marked as being |
| 255 | // covered by -Wpedantic, increment the count of parent groups. Once the |
| 256 | // group's count is equal to the number of subgroups and diagnostics in |
| 257 | // that group, we can safely add this group to -Wpedantic. |
| 258 | if (groupInPedantic(Group, /* increment */ true)) { |
| 259 | const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group); |
| 260 | for (unsigned i = 0, e = Parents.size(); i != e; ++i) |
| 261 | markGroup(Parents[i]); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | void InferPedantic::compute(VecOrSet DiagsInPedantic, |
| 266 | VecOrSet GroupsInPedantic) { |
| 267 | // All extensions are implicitly in the "pedantic" group. For those that |
| 268 | // aren't explicitly included in -Wpedantic, mark them for consideration |
| 269 | // to be included in -Wpedantic directly. |
| 270 | for (unsigned i = 0, e = Diags.size(); i != e; ++i) { |
| 271 | Record *R = Diags[i]; |
| 272 | if (isExtension(R)) { |
| 273 | DiagsSet.insert(R); |
| 274 | if (DefInit *Group = dynamic_cast<DefInit*>(R->getValueInit("Group"))) { |
| 275 | const Record *GroupRec = Group->getDef(); |
| 276 | if (!isSubGroupOfGroup(GroupRec, "pedantic")) { |
| 277 | markGroup(GroupRec); |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | // Compute the set of diagnostics that are directly in -Wpedantic. We |
| 284 | // march through Diags a second time to ensure the results are emitted |
| 285 | // in deterministic order. |
| 286 | for (unsigned i = 0, e = Diags.size(); i != e; ++i) { |
| 287 | Record *R = Diags[i]; |
| 288 | if (!DiagsSet.count(R)) |
| 289 | continue; |
| 290 | // Check if the group is implicitly in -Wpedantic. If so, |
| 291 | // the diagnostic should not be directly included in the -Wpedantic |
| 292 | // diagnostic group. |
| 293 | if (DefInit *Group = dynamic_cast<DefInit*>(R->getValueInit("Group"))) |
| 294 | if (groupInPedantic(Group->getDef())) |
| 295 | continue; |
| 296 | |
| 297 | // The diagnostic is not included in a group that is (transitively) in |
| 298 | // -Wpedantic. Include it in -Wpedantic directly. |
| 299 | if (RecordVec *V = DiagsInPedantic.dyn_cast<RecordVec*>()) |
| 300 | V->push_back(R); |
| 301 | else { |
| 302 | DiagsInPedantic.get<RecordSet*>()->insert(R); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | if (!GroupsInPedantic) |
| 307 | return; |
| 308 | |
| 309 | // Compute the set of groups that are directly in -Wpedantic. We |
| 310 | // march through the groups to ensure the results are emitted |
| 311 | /// in a deterministc order. |
| 312 | for (unsigned i = 0, ei = DiagGroups.size(); i != ei; ++i) { |
| 313 | Record *Group = DiagGroups[i]; |
| 314 | if (!groupInPedantic(Group)) |
| 315 | continue; |
| 316 | |
| 317 | unsigned ParentsInPedantic = 0; |
| 318 | const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group); |
| 319 | for (unsigned j = 0, ej = Parents.size(); j != ej; ++j) { |
| 320 | if (groupInPedantic(Parents[j])) |
| 321 | ++ParentsInPedantic; |
| 322 | } |
| 323 | // If all the parents are in -Wpedantic, this means that this diagnostic |
| 324 | // group will be indirectly included by -Wpedantic already. In that |
| 325 | // case, do not add it directly to -Wpedantic. If the group has no |
| 326 | // parents, obviously it should go into -Wpedantic. |
| 327 | if (Parents.size() > 0 && ParentsInPedantic == Parents.size()) |
| 328 | continue; |
| 329 | |
| 330 | if (RecordVec *V = GroupsInPedantic.dyn_cast<RecordVec*>()) |
| 331 | V->push_back(Group); |
| 332 | else { |
| 333 | GroupsInPedantic.get<RecordSet*>()->insert(Group); |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 339 | // Warning Tables (.inc file) generation. |
| 340 | //===----------------------------------------------------------------------===// |
| 341 | |
Jakob Stoklund Olesen | 3cc509b | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 342 | /// ClangDiagsDefsEmitter - The top-level class emits .def files containing |
| 343 | /// declarations of Clang diagnostics. |
| 344 | namespace clang { |
| 345 | void EmitClangDiagsDefs(RecordKeeper &Records, raw_ostream &OS, |
| 346 | const std::string &Component) { |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 347 | // Write the #if guard |
| 348 | if (!Component.empty()) { |
Benjamin Kramer | 90c7892 | 2011-11-06 20:36:48 +0000 | [diff] [blame] | 349 | std::string ComponentName = StringRef(Component).upper(); |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 350 | OS << "#ifdef " << ComponentName << "START\n"; |
| 351 | OS << "__" << ComponentName << "START = DIAG_START_" << ComponentName |
| 352 | << ",\n"; |
| 353 | OS << "#undef " << ComponentName << "START\n"; |
| 354 | OS << "#endif\n\n"; |
| 355 | } |
| 356 | |
| 357 | const std::vector<Record*> &Diags = |
| 358 | Records.getAllDerivedDefinitions("Diagnostic"); |
Benjamin Kramer | d49cb20 | 2012-02-15 20:57:03 +0000 | [diff] [blame] | 359 | |
Benjamin Kramer | d49cb20 | 2012-02-15 20:57:03 +0000 | [diff] [blame] | 360 | std::vector<Record*> DiagGroups |
| 361 | = Records.getAllDerivedDefinitions("DiagGroup"); |
Argyrios Kyrtzidis | d42236e | 2012-03-06 00:00:38 +0000 | [diff] [blame] | 362 | |
| 363 | std::map<std::string, GroupInfo> DiagsInGroup; |
| 364 | groupDiagnostics(Diags, DiagGroups, DiagsInGroup); |
Benjamin Kramer | d49cb20 | 2012-02-15 20:57:03 +0000 | [diff] [blame] | 365 | |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 366 | DiagCategoryIDMap CategoryIDs(Records); |
| 367 | DiagGroupParentMap DGParentMap(Records); |
| 368 | |
Ted Kremenek | e8cf7d1 | 2012-07-07 05:53:30 +0000 | [diff] [blame^] | 369 | // Compute the set of diagnostics that are in -Wpedantic. |
| 370 | RecordSet DiagsInPedantic; |
| 371 | InferPedantic inferPedantic(DGParentMap, Diags, DiagGroups, DiagsInGroup); |
| 372 | inferPedantic.compute(&DiagsInPedantic, (RecordVec*)0); |
| 373 | |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 374 | for (unsigned i = 0, e = Diags.size(); i != e; ++i) { |
| 375 | const Record &R = *Diags[i]; |
| 376 | // Filter by component. |
| 377 | if (!Component.empty() && Component != R.getValueAsString("Component")) |
| 378 | continue; |
| 379 | |
| 380 | OS << "DIAG(" << R.getName() << ", "; |
| 381 | OS << R.getValueAsDef("Class")->getName(); |
| 382 | OS << ", diag::" << R.getValueAsDef("DefaultMapping")->getName(); |
| 383 | |
| 384 | // Description string. |
| 385 | OS << ", \""; |
| 386 | OS.write_escaped(R.getValueAsString("Text")) << '"'; |
| 387 | |
Benjamin Kramer | d49cb20 | 2012-02-15 20:57:03 +0000 | [diff] [blame] | 388 | // Warning associated with the diagnostic. This is stored as an index into |
| 389 | // the alphabetically sorted warning table. |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 390 | if (DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Group"))) { |
Argyrios Kyrtzidis | d42236e | 2012-03-06 00:00:38 +0000 | [diff] [blame] | 391 | std::map<std::string, GroupInfo>::iterator I = |
| 392 | DiagsInGroup.find(DI->getDef()->getValueAsString("GroupName")); |
| 393 | assert(I != DiagsInGroup.end()); |
| 394 | OS << ", " << I->second.IDNo; |
Ted Kremenek | e8cf7d1 | 2012-07-07 05:53:30 +0000 | [diff] [blame^] | 395 | } else if (DiagsInPedantic.count(&R)) { |
| 396 | std::map<std::string, GroupInfo>::iterator I = |
| 397 | DiagsInGroup.find("pedantic"); |
| 398 | assert(I != DiagsInGroup.end() && "pedantic group not defined"); |
| 399 | OS << ", " << I->second.IDNo; |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 400 | } else { |
Benjamin Kramer | d49cb20 | 2012-02-15 20:57:03 +0000 | [diff] [blame] | 401 | OS << ", 0"; |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | // SFINAE bit |
| 405 | if (R.getValueAsBit("SFINAE")) |
| 406 | OS << ", true"; |
| 407 | else |
| 408 | OS << ", false"; |
| 409 | |
| 410 | // Access control bit |
| 411 | if (R.getValueAsBit("AccessControl")) |
| 412 | OS << ", true"; |
| 413 | else |
| 414 | OS << ", false"; |
| 415 | |
| 416 | // FIXME: This condition is just to avoid temporary revlock, it can be |
| 417 | // removed. |
| 418 | if (R.getValue("WarningNoWerror")) { |
| 419 | // Default warning has no Werror bit. |
| 420 | if (R.getValueAsBit("WarningNoWerror")) |
| 421 | OS << ", true"; |
| 422 | else |
| 423 | OS << ", false"; |
| 424 | |
| 425 | // Default warning show in system header bit. |
| 426 | if (R.getValueAsBit("WarningShowInSystemHeader")) |
| 427 | OS << ", true"; |
| 428 | else |
| 429 | OS << ", false"; |
| 430 | } |
| 431 | |
| 432 | // Category number. |
| 433 | OS << ", " << CategoryIDs.getID(getDiagnosticCategory(&R, DGParentMap)); |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 434 | OS << ")\n"; |
| 435 | } |
| 436 | } |
Jakob Stoklund Olesen | 3cc509b | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 437 | } // end namespace clang |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 438 | |
| 439 | //===----------------------------------------------------------------------===// |
| 440 | // Warning Group Tables generation |
| 441 | //===----------------------------------------------------------------------===// |
| 442 | |
| 443 | static std::string getDiagCategoryEnum(llvm::StringRef name) { |
| 444 | if (name.empty()) |
| 445 | return "DiagCat_None"; |
Dylan Noblesmith | 36d5927 | 2012-02-13 12:32:26 +0000 | [diff] [blame] | 446 | SmallString<256> enumName = llvm::StringRef("DiagCat_"); |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 447 | for (llvm::StringRef::iterator I = name.begin(), E = name.end(); I != E; ++I) |
| 448 | enumName += isalnum(*I) ? *I : '_'; |
| 449 | return enumName.str(); |
| 450 | } |
Ted Kremenek | e8cf7d1 | 2012-07-07 05:53:30 +0000 | [diff] [blame^] | 451 | |
Jakob Stoklund Olesen | 3cc509b | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 452 | namespace clang { |
| 453 | void EmitClangDiagGroups(RecordKeeper &Records, raw_ostream &OS) { |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 454 | // Compute a mapping from a DiagGroup to all of its parents. |
| 455 | DiagGroupParentMap DGParentMap(Records); |
Ted Kremenek | e8cf7d1 | 2012-07-07 05:53:30 +0000 | [diff] [blame^] | 456 | |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 457 | std::vector<Record*> Diags = |
| 458 | Records.getAllDerivedDefinitions("Diagnostic"); |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 459 | |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 460 | std::vector<Record*> DiagGroups |
| 461 | = Records.getAllDerivedDefinitions("DiagGroup"); |
Argyrios Kyrtzidis | d42236e | 2012-03-06 00:00:38 +0000 | [diff] [blame] | 462 | |
| 463 | std::map<std::string, GroupInfo> DiagsInGroup; |
| 464 | groupDiagnostics(Diags, DiagGroups, DiagsInGroup); |
Ted Kremenek | e8cf7d1 | 2012-07-07 05:53:30 +0000 | [diff] [blame^] | 465 | |
| 466 | // All extensions are implicitly in the "pedantic" group. Record the |
| 467 | // implicit set of groups in the "pedantic" group, and use this information |
| 468 | // later when emitting the group information for Pedantic. |
| 469 | RecordVec DiagsInPedantic; |
| 470 | RecordVec GroupsInPedantic; |
| 471 | InferPedantic inferPedantic(DGParentMap, Diags, DiagGroups, DiagsInGroup); |
| 472 | inferPedantic.compute(&DiagsInPedantic, &GroupsInPedantic); |
| 473 | |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 474 | // Walk through the groups emitting an array for each diagnostic of the diags |
| 475 | // that are mapped to. |
| 476 | OS << "\n#ifdef GET_DIAG_ARRAYS\n"; |
| 477 | unsigned MaxLen = 0; |
| 478 | for (std::map<std::string, GroupInfo>::iterator |
| 479 | I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) { |
| 480 | MaxLen = std::max(MaxLen, (unsigned)I->first.size()); |
Ted Kremenek | e8cf7d1 | 2012-07-07 05:53:30 +0000 | [diff] [blame^] | 481 | const bool IsPedantic = I->first == "pedantic"; |
| 482 | |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 483 | std::vector<const Record*> &V = I->second.DiagsInGroup; |
Ted Kremenek | e8cf7d1 | 2012-07-07 05:53:30 +0000 | [diff] [blame^] | 484 | if (!V.empty() || (IsPedantic && !DiagsInPedantic.empty())) { |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 485 | OS << "static const short DiagArray" << I->second.IDNo << "[] = { "; |
| 486 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 487 | OS << "diag::" << V[i]->getName() << ", "; |
Ted Kremenek | e8cf7d1 | 2012-07-07 05:53:30 +0000 | [diff] [blame^] | 488 | // Emit the diagnostics implicitly in "pedantic". |
| 489 | if (IsPedantic) { |
| 490 | for (unsigned i = 0, e = DiagsInPedantic.size(); i != e; ++i) |
| 491 | OS << "diag::" << DiagsInPedantic[i]->getName() << ", "; |
| 492 | } |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 493 | OS << "-1 };\n"; |
| 494 | } |
| 495 | |
| 496 | const std::vector<std::string> &SubGroups = I->second.SubGroups; |
Ted Kremenek | e8cf7d1 | 2012-07-07 05:53:30 +0000 | [diff] [blame^] | 497 | if (!SubGroups.empty() || (IsPedantic && !GroupsInPedantic.empty())) { |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 498 | OS << "static const short DiagSubGroup" << I->second.IDNo << "[] = { "; |
| 499 | for (unsigned i = 0, e = SubGroups.size(); i != e; ++i) { |
| 500 | std::map<std::string, GroupInfo>::iterator RI = |
| 501 | DiagsInGroup.find(SubGroups[i]); |
| 502 | assert(RI != DiagsInGroup.end() && "Referenced without existing?"); |
| 503 | OS << RI->second.IDNo << ", "; |
| 504 | } |
Ted Kremenek | e8cf7d1 | 2012-07-07 05:53:30 +0000 | [diff] [blame^] | 505 | // Emit the groups implicitly in "pedantic". |
| 506 | if (IsPedantic) { |
| 507 | for (unsigned i = 0, e = GroupsInPedantic.size(); i != e; ++i) { |
| 508 | const std::string &GroupName = |
| 509 | GroupsInPedantic[i]->getValueAsString("GroupName"); |
| 510 | std::map<std::string, GroupInfo>::iterator RI = |
| 511 | DiagsInGroup.find(GroupName); |
| 512 | assert(RI != DiagsInGroup.end() && "Referenced without existing?"); |
| 513 | OS << RI->second.IDNo << ", "; |
| 514 | } |
| 515 | } |
| 516 | |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 517 | OS << "-1 };\n"; |
| 518 | } |
| 519 | } |
| 520 | OS << "#endif // GET_DIAG_ARRAYS\n\n"; |
| 521 | |
| 522 | // Emit the table now. |
| 523 | OS << "\n#ifdef GET_DIAG_TABLE\n"; |
| 524 | for (std::map<std::string, GroupInfo>::iterator |
| 525 | I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) { |
| 526 | // Group option string. |
| 527 | OS << " { "; |
| 528 | OS << I->first.size() << ", "; |
| 529 | OS << "\""; |
Benjamin Kramer | 037ad1b | 2011-11-15 12:54:53 +0000 | [diff] [blame] | 530 | if (I->first.find_first_not_of("abcdefghijklmnopqrstuvwxyz" |
| 531 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 532 | "0123456789!@#$%^*-+=:?")!=std::string::npos) |
| 533 | throw "Invalid character in diagnostic group '" + I->first + "'"; |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 534 | OS.write_escaped(I->first) << "\"," |
| 535 | << std::string(MaxLen-I->first.size()+1, ' '); |
Ted Kremenek | e8cf7d1 | 2012-07-07 05:53:30 +0000 | [diff] [blame^] | 536 | |
| 537 | // Special handling for 'pedantic'. |
| 538 | const bool IsPedantic = I->first == "pedantic"; |
| 539 | |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 540 | // Diagnostics in the group. |
Ted Kremenek | e8cf7d1 | 2012-07-07 05:53:30 +0000 | [diff] [blame^] | 541 | const bool hasDiags = !I->second.DiagsInGroup.empty() || |
| 542 | (IsPedantic && !DiagsInPedantic.empty()); |
| 543 | if (!hasDiags) |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 544 | OS << "0, "; |
| 545 | else |
| 546 | OS << "DiagArray" << I->second.IDNo << ", "; |
| 547 | |
| 548 | // Subgroups. |
Ted Kremenek | e8cf7d1 | 2012-07-07 05:53:30 +0000 | [diff] [blame^] | 549 | const bool hasSubGroups = !I->second.SubGroups.empty() || |
| 550 | (IsPedantic && !GroupsInPedantic.empty()); |
| 551 | if (!hasSubGroups) |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 552 | OS << 0; |
| 553 | else |
| 554 | OS << "DiagSubGroup" << I->second.IDNo; |
| 555 | OS << " },\n"; |
| 556 | } |
| 557 | OS << "#endif // GET_DIAG_TABLE\n\n"; |
| 558 | |
| 559 | // Emit the category table next. |
| 560 | DiagCategoryIDMap CategoriesByID(Records); |
| 561 | OS << "\n#ifdef GET_CATEGORY_TABLE\n"; |
| 562 | for (DiagCategoryIDMap::iterator I = CategoriesByID.begin(), |
| 563 | E = CategoriesByID.end(); I != E; ++I) |
| 564 | OS << "CATEGORY(\"" << *I << "\", " << getDiagCategoryEnum(*I) << ")\n"; |
| 565 | OS << "#endif // GET_CATEGORY_TABLE\n\n"; |
| 566 | } |
Jakob Stoklund Olesen | 3cc509b | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 567 | } // end namespace clang |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 568 | |
| 569 | //===----------------------------------------------------------------------===// |
| 570 | // Diagnostic name index generation |
| 571 | //===----------------------------------------------------------------------===// |
| 572 | |
| 573 | namespace { |
| 574 | struct RecordIndexElement |
| 575 | { |
| 576 | RecordIndexElement() {} |
| 577 | explicit RecordIndexElement(Record const &R): |
| 578 | Name(R.getName()) {} |
| 579 | |
| 580 | std::string Name; |
| 581 | }; |
| 582 | |
| 583 | struct RecordIndexElementSorter : |
| 584 | public std::binary_function<RecordIndexElement, RecordIndexElement, bool> { |
| 585 | |
| 586 | bool operator()(RecordIndexElement const &Lhs, |
| 587 | RecordIndexElement const &Rhs) const { |
| 588 | return Lhs.Name < Rhs.Name; |
| 589 | } |
| 590 | |
| 591 | }; |
| 592 | |
| 593 | } // end anonymous namespace. |
| 594 | |
Jakob Stoklund Olesen | 3cc509b | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 595 | namespace clang { |
| 596 | void EmitClangDiagsIndexName(RecordKeeper &Records, raw_ostream &OS) { |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 597 | const std::vector<Record*> &Diags = |
| 598 | Records.getAllDerivedDefinitions("Diagnostic"); |
| 599 | |
| 600 | std::vector<RecordIndexElement> Index; |
| 601 | Index.reserve(Diags.size()); |
| 602 | for (unsigned i = 0, e = Diags.size(); i != e; ++i) { |
| 603 | const Record &R = *(Diags[i]); |
| 604 | Index.push_back(RecordIndexElement(R)); |
| 605 | } |
| 606 | |
| 607 | std::sort(Index.begin(), Index.end(), RecordIndexElementSorter()); |
| 608 | |
| 609 | for (unsigned i = 0, e = Index.size(); i != e; ++i) { |
| 610 | const RecordIndexElement &R = Index[i]; |
| 611 | |
| 612 | OS << "DIAG_NAME_INDEX(" << R.Name << ")\n"; |
| 613 | } |
| 614 | } |
Jakob Stoklund Olesen | 3cc509b | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 615 | } // end namespace clang |