Peter Collingbourne | bee583f | 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 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/DenseSet.h" |
Chandler Carruth | 59ff16c | 2012-12-04 09:53:39 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/Optional.h" |
| 16 | #include "llvm/ADT/PointerUnion.h" |
Jordan Rose | c3b23aa | 2013-01-10 18:50:46 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SetVector.h" |
| 18 | #include "llvm/ADT/SmallPtrSet.h" |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallString.h" |
Jordan Rose | c3b23aa | 2013-01-10 18:50:46 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallVector.h" |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringMap.h" |
Jordan Rose | c3b23aa | 2013-01-10 18:50:46 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Twine.h" |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Compiler.h" |
| 24 | #include "llvm/Support/Debug.h" |
Joerg Sonnenberger | 691a16b | 2012-10-25 16:37:08 +0000 | [diff] [blame] | 25 | #include "llvm/TableGen/Error.h" |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 26 | #include "llvm/TableGen/Record.h" |
Craig Topper | da7cf8a | 2013-08-29 05:18:04 +0000 | [diff] [blame] | 27 | #include "llvm/TableGen/StringToOffsetTable.h" |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 28 | #include "llvm/TableGen/TableGenBackend.h" |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 29 | #include <algorithm> |
Joerg Sonnenberger | 42cf268 | 2012-08-10 10:58:18 +0000 | [diff] [blame] | 30 | #include <cctype> |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 31 | #include <functional> |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 32 | #include <map> |
Benjamin Kramer | a8cafe2 | 2012-02-15 20:57:03 +0000 | [diff] [blame] | 33 | #include <set> |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 34 | using namespace llvm; |
| 35 | |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | // Diagnostic category computation code. |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | |
| 40 | namespace { |
| 41 | class DiagGroupParentMap { |
| 42 | RecordKeeper &Records; |
| 43 | std::map<const Record*, std::vector<Record*> > Mapping; |
| 44 | public: |
| 45 | DiagGroupParentMap(RecordKeeper &records) : Records(records) { |
| 46 | std::vector<Record*> DiagGroups |
| 47 | = Records.getAllDerivedDefinitions("DiagGroup"); |
| 48 | for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) { |
| 49 | std::vector<Record*> SubGroups = |
| 50 | DiagGroups[i]->getValueAsListOfDefs("SubGroups"); |
| 51 | for (unsigned j = 0, e = SubGroups.size(); j != e; ++j) |
| 52 | Mapping[SubGroups[j]].push_back(DiagGroups[i]); |
| 53 | } |
| 54 | } |
Craig Topper | f3932e3 | 2013-07-19 21:43:59 +0000 | [diff] [blame] | 55 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 56 | const std::vector<Record*> &getParents(const Record *Group) { |
| 57 | return Mapping[Group]; |
| 58 | } |
| 59 | }; |
| 60 | } // end anonymous namespace. |
| 61 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 62 | static std::string |
| 63 | getCategoryFromDiagGroup(const Record *Group, |
| 64 | DiagGroupParentMap &DiagGroupParents) { |
| 65 | // If the DiagGroup has a category, return it. |
| 66 | std::string CatName = Group->getValueAsString("CategoryName"); |
| 67 | if (!CatName.empty()) return CatName; |
Craig Topper | f3932e3 | 2013-07-19 21:43:59 +0000 | [diff] [blame] | 68 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 69 | // The diag group may the subgroup of one or more other diagnostic groups, |
| 70 | // check these for a category as well. |
| 71 | const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group); |
| 72 | for (unsigned i = 0, e = Parents.size(); i != e; ++i) { |
| 73 | CatName = getCategoryFromDiagGroup(Parents[i], DiagGroupParents); |
| 74 | if (!CatName.empty()) return CatName; |
| 75 | } |
| 76 | return ""; |
| 77 | } |
| 78 | |
| 79 | /// getDiagnosticCategory - Return the category that the specified diagnostic |
| 80 | /// lives in. |
| 81 | static std::string getDiagnosticCategory(const Record *R, |
| 82 | DiagGroupParentMap &DiagGroupParents) { |
| 83 | // If the diagnostic is in a group, and that group has a category, use it. |
Sean Silva | 1c4aaa8 | 2012-10-10 20:25:43 +0000 | [diff] [blame] | 84 | if (DefInit *Group = dyn_cast<DefInit>(R->getValueInit("Group"))) { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 85 | // Check the diagnostic's diag group for a category. |
| 86 | std::string CatName = getCategoryFromDiagGroup(Group->getDef(), |
| 87 | DiagGroupParents); |
| 88 | if (!CatName.empty()) return CatName; |
| 89 | } |
Ted Kremenek | b22ea2a | 2012-07-07 05:53:30 +0000 | [diff] [blame] | 90 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 91 | // If the diagnostic itself has a category, get it. |
| 92 | return R->getValueAsString("CategoryName"); |
| 93 | } |
| 94 | |
| 95 | namespace { |
| 96 | class DiagCategoryIDMap { |
| 97 | RecordKeeper &Records; |
| 98 | StringMap<unsigned> CategoryIDs; |
| 99 | std::vector<std::string> CategoryStrings; |
| 100 | public: |
| 101 | DiagCategoryIDMap(RecordKeeper &records) : Records(records) { |
| 102 | DiagGroupParentMap ParentInfo(Records); |
Craig Topper | f3932e3 | 2013-07-19 21:43:59 +0000 | [diff] [blame] | 103 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 104 | // The zero'th category is "". |
| 105 | CategoryStrings.push_back(""); |
| 106 | CategoryIDs[""] = 0; |
Craig Topper | f3932e3 | 2013-07-19 21:43:59 +0000 | [diff] [blame] | 107 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 108 | std::vector<Record*> Diags = |
| 109 | Records.getAllDerivedDefinitions("Diagnostic"); |
| 110 | for (unsigned i = 0, e = Diags.size(); i != e; ++i) { |
| 111 | std::string Category = getDiagnosticCategory(Diags[i], ParentInfo); |
| 112 | if (Category.empty()) continue; // Skip diags with no category. |
Craig Topper | f3932e3 | 2013-07-19 21:43:59 +0000 | [diff] [blame] | 113 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 114 | unsigned &ID = CategoryIDs[Category]; |
| 115 | if (ID != 0) continue; // Already seen. |
Craig Topper | f3932e3 | 2013-07-19 21:43:59 +0000 | [diff] [blame] | 116 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 117 | ID = CategoryStrings.size(); |
| 118 | CategoryStrings.push_back(Category); |
| 119 | } |
| 120 | } |
Craig Topper | f3932e3 | 2013-07-19 21:43:59 +0000 | [diff] [blame] | 121 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 122 | unsigned getID(StringRef CategoryString) { |
| 123 | return CategoryIDs[CategoryString]; |
| 124 | } |
Craig Topper | f3932e3 | 2013-07-19 21:43:59 +0000 | [diff] [blame] | 125 | |
Craig Topper | a9bcac5 | 2013-07-21 22:20:10 +0000 | [diff] [blame] | 126 | typedef std::vector<std::string>::const_iterator const_iterator; |
| 127 | const_iterator begin() const { return CategoryStrings.begin(); } |
| 128 | const_iterator end() const { return CategoryStrings.end(); } |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 129 | }; |
Argyrios Kyrtzidis | 87acf19 | 2012-03-06 00:00:38 +0000 | [diff] [blame] | 130 | |
| 131 | struct GroupInfo { |
| 132 | std::vector<const Record*> DiagsInGroup; |
| 133 | std::vector<std::string> SubGroups; |
| 134 | unsigned IDNo; |
Jordan Rose | c3b23aa | 2013-01-10 18:50:46 +0000 | [diff] [blame] | 135 | |
| 136 | const Record *ExplicitDef; |
| 137 | |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 138 | GroupInfo() : ExplicitDef(nullptr) {} |
Argyrios Kyrtzidis | 87acf19 | 2012-03-06 00:00:38 +0000 | [diff] [blame] | 139 | }; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 140 | } // end anonymous namespace. |
| 141 | |
Jordan Rose | c3b23aa | 2013-01-10 18:50:46 +0000 | [diff] [blame] | 142 | static bool beforeThanCompare(const Record *LHS, const Record *RHS) { |
| 143 | assert(!LHS->getLoc().empty() && !RHS->getLoc().empty()); |
| 144 | return |
| 145 | LHS->getLoc().front().getPointer() < RHS->getLoc().front().getPointer(); |
| 146 | } |
| 147 | |
| 148 | static bool beforeThanCompareGroups(const GroupInfo *LHS, const GroupInfo *RHS){ |
| 149 | assert(!LHS->DiagsInGroup.empty() && !RHS->DiagsInGroup.empty()); |
| 150 | return beforeThanCompare(LHS->DiagsInGroup.front(), |
| 151 | RHS->DiagsInGroup.front()); |
| 152 | } |
| 153 | |
| 154 | static SMRange findSuperClassRange(const Record *R, StringRef SuperName) { |
| 155 | ArrayRef<Record *> Supers = R->getSuperClasses(); |
| 156 | |
| 157 | for (size_t i = 0, e = Supers.size(); i < e; ++i) |
| 158 | if (Supers[i]->getName() == SuperName) |
| 159 | return R->getSuperClassRanges()[i]; |
| 160 | |
| 161 | return SMRange(); |
| 162 | } |
| 163 | |
Argyrios Kyrtzidis | 87acf19 | 2012-03-06 00:00:38 +0000 | [diff] [blame] | 164 | /// \brief Invert the 1-[0/1] mapping of diags to group into a one to many |
| 165 | /// mapping of groups to diags in the group. |
| 166 | static void groupDiagnostics(const std::vector<Record*> &Diags, |
| 167 | const std::vector<Record*> &DiagGroups, |
| 168 | std::map<std::string, GroupInfo> &DiagsInGroup) { |
Jordan Rose | c3b23aa | 2013-01-10 18:50:46 +0000 | [diff] [blame] | 169 | |
Argyrios Kyrtzidis | 87acf19 | 2012-03-06 00:00:38 +0000 | [diff] [blame] | 170 | for (unsigned i = 0, e = Diags.size(); i != e; ++i) { |
| 171 | const Record *R = Diags[i]; |
Sean Silva | 1c4aaa8 | 2012-10-10 20:25:43 +0000 | [diff] [blame] | 172 | DefInit *DI = dyn_cast<DefInit>(R->getValueInit("Group")); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 173 | if (!DI) |
| 174 | continue; |
Richard Smith | ce52ca1 | 2012-05-04 19:05:50 +0000 | [diff] [blame] | 175 | assert(R->getValueAsDef("Class")->getName() != "CLASS_NOTE" && |
| 176 | "Note can't be in a DiagGroup"); |
Argyrios Kyrtzidis | 87acf19 | 2012-03-06 00:00:38 +0000 | [diff] [blame] | 177 | std::string GroupName = DI->getDef()->getValueAsString("GroupName"); |
| 178 | DiagsInGroup[GroupName].DiagsInGroup.push_back(R); |
| 179 | } |
Jordan Rose | c3b23aa | 2013-01-10 18:50:46 +0000 | [diff] [blame] | 180 | |
| 181 | typedef SmallPtrSet<GroupInfo *, 16> GroupSetTy; |
| 182 | GroupSetTy ImplicitGroups; |
| 183 | |
Argyrios Kyrtzidis | 87acf19 | 2012-03-06 00:00:38 +0000 | [diff] [blame] | 184 | // Add all DiagGroup's to the DiagsInGroup list to make sure we pick up empty |
| 185 | // groups (these are warnings that GCC supports that clang never produces). |
| 186 | for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) { |
| 187 | Record *Group = DiagGroups[i]; |
| 188 | GroupInfo &GI = DiagsInGroup[Group->getValueAsString("GroupName")]; |
Jordan Rose | c3b23aa | 2013-01-10 18:50:46 +0000 | [diff] [blame] | 189 | if (Group->isAnonymous()) { |
| 190 | if (GI.DiagsInGroup.size() > 1) |
| 191 | ImplicitGroups.insert(&GI); |
| 192 | } else { |
| 193 | if (GI.ExplicitDef) |
| 194 | assert(GI.ExplicitDef == Group); |
| 195 | else |
| 196 | GI.ExplicitDef = Group; |
| 197 | } |
| 198 | |
Argyrios Kyrtzidis | 87acf19 | 2012-03-06 00:00:38 +0000 | [diff] [blame] | 199 | std::vector<Record*> SubGroups = Group->getValueAsListOfDefs("SubGroups"); |
| 200 | for (unsigned j = 0, e = SubGroups.size(); j != e; ++j) |
| 201 | GI.SubGroups.push_back(SubGroups[j]->getValueAsString("GroupName")); |
| 202 | } |
Craig Topper | f3932e3 | 2013-07-19 21:43:59 +0000 | [diff] [blame] | 203 | |
Argyrios Kyrtzidis | 87acf19 | 2012-03-06 00:00:38 +0000 | [diff] [blame] | 204 | // Assign unique ID numbers to the groups. |
| 205 | unsigned IDNo = 0; |
| 206 | for (std::map<std::string, GroupInfo>::iterator |
| 207 | I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I, ++IDNo) |
| 208 | I->second.IDNo = IDNo; |
Jordan Rose | c3b23aa | 2013-01-10 18:50:46 +0000 | [diff] [blame] | 209 | |
| 210 | // Sort the implicit groups, so we can warn about them deterministically. |
| 211 | SmallVector<GroupInfo *, 16> SortedGroups(ImplicitGroups.begin(), |
| 212 | ImplicitGroups.end()); |
| 213 | for (SmallVectorImpl<GroupInfo *>::iterator I = SortedGroups.begin(), |
| 214 | E = SortedGroups.end(); |
| 215 | I != E; ++I) { |
| 216 | MutableArrayRef<const Record *> GroupDiags = (*I)->DiagsInGroup; |
| 217 | std::sort(GroupDiags.begin(), GroupDiags.end(), beforeThanCompare); |
| 218 | } |
| 219 | std::sort(SortedGroups.begin(), SortedGroups.end(), beforeThanCompareGroups); |
| 220 | |
| 221 | // Warn about the same group being used anonymously in multiple places. |
| 222 | for (SmallVectorImpl<GroupInfo *>::const_iterator I = SortedGroups.begin(), |
| 223 | E = SortedGroups.end(); |
| 224 | I != E; ++I) { |
| 225 | ArrayRef<const Record *> GroupDiags = (*I)->DiagsInGroup; |
| 226 | |
| 227 | if ((*I)->ExplicitDef) { |
| 228 | std::string Name = (*I)->ExplicitDef->getValueAsString("GroupName"); |
| 229 | for (ArrayRef<const Record *>::const_iterator DI = GroupDiags.begin(), |
| 230 | DE = GroupDiags.end(); |
| 231 | DI != DE; ++DI) { |
| 232 | const DefInit *GroupInit = cast<DefInit>((*DI)->getValueInit("Group")); |
| 233 | const Record *NextDiagGroup = GroupInit->getDef(); |
| 234 | if (NextDiagGroup == (*I)->ExplicitDef) |
| 235 | continue; |
| 236 | |
| 237 | SMRange InGroupRange = findSuperClassRange(*DI, "InGroup"); |
| 238 | SmallString<64> Replacement; |
| 239 | if (InGroupRange.isValid()) { |
| 240 | Replacement += "InGroup<"; |
| 241 | Replacement += (*I)->ExplicitDef->getName(); |
| 242 | Replacement += ">"; |
| 243 | } |
| 244 | SMFixIt FixIt(InGroupRange, Replacement.str()); |
| 245 | |
| 246 | SrcMgr.PrintMessage(NextDiagGroup->getLoc().front(), |
| 247 | SourceMgr::DK_Error, |
| 248 | Twine("group '") + Name + |
| 249 | "' is referred to anonymously", |
Dmitri Gribenko | 010316c | 2013-05-05 01:03:47 +0000 | [diff] [blame] | 250 | None, |
Jordan Rose | c3b23aa | 2013-01-10 18:50:46 +0000 | [diff] [blame] | 251 | InGroupRange.isValid() ? FixIt |
| 252 | : ArrayRef<SMFixIt>()); |
| 253 | SrcMgr.PrintMessage((*I)->ExplicitDef->getLoc().front(), |
| 254 | SourceMgr::DK_Note, "group defined here"); |
| 255 | } |
| 256 | } else { |
| 257 | // If there's no existing named group, we should just warn once and use |
| 258 | // notes to list all the other cases. |
| 259 | ArrayRef<const Record *>::const_iterator DI = GroupDiags.begin(), |
| 260 | DE = GroupDiags.end(); |
| 261 | assert(DI != DE && "We only care about groups with multiple uses!"); |
| 262 | |
| 263 | const DefInit *GroupInit = cast<DefInit>((*DI)->getValueInit("Group")); |
| 264 | const Record *NextDiagGroup = GroupInit->getDef(); |
| 265 | std::string Name = NextDiagGroup->getValueAsString("GroupName"); |
| 266 | |
| 267 | SMRange InGroupRange = findSuperClassRange(*DI, "InGroup"); |
| 268 | SrcMgr.PrintMessage(NextDiagGroup->getLoc().front(), |
| 269 | SourceMgr::DK_Error, |
| 270 | Twine("group '") + Name + |
| 271 | "' is referred to anonymously", |
| 272 | InGroupRange); |
| 273 | |
| 274 | for (++DI; DI != DE; ++DI) { |
| 275 | GroupInit = cast<DefInit>((*DI)->getValueInit("Group")); |
| 276 | InGroupRange = findSuperClassRange(*DI, "InGroup"); |
| 277 | SrcMgr.PrintMessage(GroupInit->getDef()->getLoc().front(), |
| 278 | SourceMgr::DK_Note, "also referenced here", |
| 279 | InGroupRange); |
| 280 | } |
| 281 | } |
| 282 | } |
Argyrios Kyrtzidis | 87acf19 | 2012-03-06 00:00:38 +0000 | [diff] [blame] | 283 | } |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 284 | |
| 285 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b22ea2a | 2012-07-07 05:53:30 +0000 | [diff] [blame] | 286 | // Infer members of -Wpedantic. |
| 287 | //===----------------------------------------------------------------------===// |
| 288 | |
| 289 | typedef std::vector<const Record *> RecordVec; |
| 290 | typedef llvm::DenseSet<const Record *> RecordSet; |
| 291 | typedef llvm::PointerUnion<RecordVec*, RecordSet*> VecOrSet; |
| 292 | |
| 293 | namespace { |
| 294 | class InferPedantic { |
| 295 | typedef llvm::DenseMap<const Record*, |
Ted Kremenek | 0332558 | 2013-02-21 01:29:01 +0000 | [diff] [blame] | 296 | std::pair<unsigned, Optional<unsigned> > > GMap; |
Ted Kremenek | b22ea2a | 2012-07-07 05:53:30 +0000 | [diff] [blame] | 297 | |
| 298 | DiagGroupParentMap &DiagGroupParents; |
| 299 | const std::vector<Record*> &Diags; |
| 300 | const std::vector<Record*> DiagGroups; |
| 301 | std::map<std::string, GroupInfo> &DiagsInGroup; |
| 302 | llvm::DenseSet<const Record*> DiagsSet; |
| 303 | GMap GroupCount; |
| 304 | public: |
| 305 | InferPedantic(DiagGroupParentMap &DiagGroupParents, |
| 306 | const std::vector<Record*> &Diags, |
| 307 | const std::vector<Record*> &DiagGroups, |
| 308 | std::map<std::string, GroupInfo> &DiagsInGroup) |
| 309 | : DiagGroupParents(DiagGroupParents), |
| 310 | Diags(Diags), |
| 311 | DiagGroups(DiagGroups), |
| 312 | DiagsInGroup(DiagsInGroup) {} |
| 313 | |
| 314 | /// Compute the set of diagnostics and groups that are immediately |
| 315 | /// in -Wpedantic. |
| 316 | void compute(VecOrSet DiagsInPedantic, |
| 317 | VecOrSet GroupsInPedantic); |
| 318 | |
| 319 | private: |
| 320 | /// Determine whether a group is a subgroup of another group. |
| 321 | bool isSubGroupOfGroup(const Record *Group, |
| 322 | llvm::StringRef RootGroupName); |
| 323 | |
| 324 | /// Determine if the diagnostic is an extension. |
| 325 | bool isExtension(const Record *Diag); |
| 326 | |
Ted Kremenek | 5d858cb | 2012-08-10 20:50:00 +0000 | [diff] [blame] | 327 | /// Determine if the diagnostic is off by default. |
| 328 | bool isOffByDefault(const Record *Diag); |
| 329 | |
Ted Kremenek | b22ea2a | 2012-07-07 05:53:30 +0000 | [diff] [blame] | 330 | /// Increment the count for a group, and transitively marked |
| 331 | /// parent groups when appropriate. |
| 332 | void markGroup(const Record *Group); |
| 333 | |
| 334 | /// Return true if the diagnostic is in a pedantic group. |
| 335 | bool groupInPedantic(const Record *Group, bool increment = false); |
| 336 | }; |
| 337 | } // end anonymous namespace |
| 338 | |
| 339 | bool InferPedantic::isSubGroupOfGroup(const Record *Group, |
| 340 | llvm::StringRef GName) { |
| 341 | |
| 342 | const std::string &GroupName = Group->getValueAsString("GroupName"); |
| 343 | if (GName == GroupName) |
| 344 | return true; |
| 345 | |
| 346 | const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group); |
| 347 | for (unsigned i = 0, e = Parents.size(); i != e; ++i) |
| 348 | if (isSubGroupOfGroup(Parents[i], GName)) |
| 349 | return true; |
| 350 | |
| 351 | return false; |
| 352 | } |
| 353 | |
| 354 | /// Determine if the diagnostic is an extension. |
| 355 | bool InferPedantic::isExtension(const Record *Diag) { |
| 356 | const std::string &ClsName = Diag->getValueAsDef("Class")->getName(); |
| 357 | return ClsName == "CLASS_EXTENSION"; |
| 358 | } |
| 359 | |
Ted Kremenek | 5d858cb | 2012-08-10 20:50:00 +0000 | [diff] [blame] | 360 | bool InferPedantic::isOffByDefault(const Record *Diag) { |
Alp Toker | 46df1c0 | 2014-06-12 10:15:20 +0000 | [diff] [blame^] | 361 | const std::string &DefSeverity = |
| 362 | Diag->getValueAsDef("DefaultSeverity")->getValueAsString("Name"); |
| 363 | return DefSeverity == "Ignored"; |
Ted Kremenek | 5d858cb | 2012-08-10 20:50:00 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Ted Kremenek | b22ea2a | 2012-07-07 05:53:30 +0000 | [diff] [blame] | 366 | bool InferPedantic::groupInPedantic(const Record *Group, bool increment) { |
| 367 | GMap::mapped_type &V = GroupCount[Group]; |
| 368 | // Lazily compute the threshold value for the group count. |
| 369 | if (!V.second.hasValue()) { |
| 370 | const GroupInfo &GI = DiagsInGroup[Group->getValueAsString("GroupName")]; |
| 371 | V.second = GI.SubGroups.size() + GI.DiagsInGroup.size(); |
| 372 | } |
| 373 | |
| 374 | if (increment) |
| 375 | ++V.first; |
| 376 | |
| 377 | // Consider a group in -Wpendatic IFF if has at least one diagnostic |
| 378 | // or subgroup AND all of those diagnostics and subgroups are covered |
| 379 | // by -Wpedantic via our computation. |
| 380 | return V.first != 0 && V.first == V.second.getValue(); |
| 381 | } |
| 382 | |
| 383 | void InferPedantic::markGroup(const Record *Group) { |
| 384 | // If all the diagnostics and subgroups have been marked as being |
| 385 | // covered by -Wpedantic, increment the count of parent groups. Once the |
| 386 | // group's count is equal to the number of subgroups and diagnostics in |
| 387 | // that group, we can safely add this group to -Wpedantic. |
| 388 | if (groupInPedantic(Group, /* increment */ true)) { |
| 389 | const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group); |
| 390 | for (unsigned i = 0, e = Parents.size(); i != e; ++i) |
| 391 | markGroup(Parents[i]); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | void InferPedantic::compute(VecOrSet DiagsInPedantic, |
| 396 | VecOrSet GroupsInPedantic) { |
Ted Kremenek | 5d858cb | 2012-08-10 20:50:00 +0000 | [diff] [blame] | 397 | // All extensions that are not on by default are implicitly in the |
| 398 | // "pedantic" group. For those that aren't explicitly included in -Wpedantic, |
| 399 | // mark them for consideration to be included in -Wpedantic directly. |
Ted Kremenek | b22ea2a | 2012-07-07 05:53:30 +0000 | [diff] [blame] | 400 | for (unsigned i = 0, e = Diags.size(); i != e; ++i) { |
| 401 | Record *R = Diags[i]; |
Ted Kremenek | 5d858cb | 2012-08-10 20:50:00 +0000 | [diff] [blame] | 402 | if (isExtension(R) && isOffByDefault(R)) { |
Ted Kremenek | b22ea2a | 2012-07-07 05:53:30 +0000 | [diff] [blame] | 403 | DiagsSet.insert(R); |
Sean Silva | 1c4aaa8 | 2012-10-10 20:25:43 +0000 | [diff] [blame] | 404 | if (DefInit *Group = dyn_cast<DefInit>(R->getValueInit("Group"))) { |
Ted Kremenek | b22ea2a | 2012-07-07 05:53:30 +0000 | [diff] [blame] | 405 | const Record *GroupRec = Group->getDef(); |
| 406 | if (!isSubGroupOfGroup(GroupRec, "pedantic")) { |
| 407 | markGroup(GroupRec); |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | // Compute the set of diagnostics that are directly in -Wpedantic. We |
| 414 | // march through Diags a second time to ensure the results are emitted |
| 415 | // in deterministic order. |
| 416 | for (unsigned i = 0, e = Diags.size(); i != e; ++i) { |
| 417 | Record *R = Diags[i]; |
| 418 | if (!DiagsSet.count(R)) |
| 419 | continue; |
| 420 | // Check if the group is implicitly in -Wpedantic. If so, |
| 421 | // the diagnostic should not be directly included in the -Wpedantic |
| 422 | // diagnostic group. |
Sean Silva | 1c4aaa8 | 2012-10-10 20:25:43 +0000 | [diff] [blame] | 423 | if (DefInit *Group = dyn_cast<DefInit>(R->getValueInit("Group"))) |
Ted Kremenek | b22ea2a | 2012-07-07 05:53:30 +0000 | [diff] [blame] | 424 | if (groupInPedantic(Group->getDef())) |
| 425 | continue; |
| 426 | |
| 427 | // The diagnostic is not included in a group that is (transitively) in |
| 428 | // -Wpedantic. Include it in -Wpedantic directly. |
| 429 | if (RecordVec *V = DiagsInPedantic.dyn_cast<RecordVec*>()) |
| 430 | V->push_back(R); |
| 431 | else { |
| 432 | DiagsInPedantic.get<RecordSet*>()->insert(R); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | if (!GroupsInPedantic) |
| 437 | return; |
| 438 | |
| 439 | // Compute the set of groups that are directly in -Wpedantic. We |
| 440 | // march through the groups to ensure the results are emitted |
| 441 | /// in a deterministc order. |
| 442 | for (unsigned i = 0, ei = DiagGroups.size(); i != ei; ++i) { |
| 443 | Record *Group = DiagGroups[i]; |
| 444 | if (!groupInPedantic(Group)) |
| 445 | continue; |
| 446 | |
| 447 | unsigned ParentsInPedantic = 0; |
| 448 | const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group); |
| 449 | for (unsigned j = 0, ej = Parents.size(); j != ej; ++j) { |
| 450 | if (groupInPedantic(Parents[j])) |
| 451 | ++ParentsInPedantic; |
| 452 | } |
| 453 | // If all the parents are in -Wpedantic, this means that this diagnostic |
| 454 | // group will be indirectly included by -Wpedantic already. In that |
| 455 | // case, do not add it directly to -Wpedantic. If the group has no |
| 456 | // parents, obviously it should go into -Wpedantic. |
| 457 | if (Parents.size() > 0 && ParentsInPedantic == Parents.size()) |
| 458 | continue; |
| 459 | |
| 460 | if (RecordVec *V = GroupsInPedantic.dyn_cast<RecordVec*>()) |
| 461 | V->push_back(Group); |
| 462 | else { |
| 463 | GroupsInPedantic.get<RecordSet*>()->insert(Group); |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 469 | // Warning Tables (.inc file) generation. |
| 470 | //===----------------------------------------------------------------------===// |
| 471 | |
Ted Kremenek | 7249239 | 2012-08-07 05:01:49 +0000 | [diff] [blame] | 472 | static bool isError(const Record &Diag) { |
| 473 | const std::string &ClsName = Diag.getValueAsDef("Class")->getName(); |
| 474 | return ClsName == "CLASS_ERROR"; |
| 475 | } |
| 476 | |
Tobias Grosser | 7416024 | 2014-02-28 09:11:08 +0000 | [diff] [blame] | 477 | static bool isRemark(const Record &Diag) { |
| 478 | const std::string &ClsName = Diag.getValueAsDef("Class")->getName(); |
| 479 | return ClsName == "CLASS_REMARK"; |
| 480 | } |
| 481 | |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 482 | /// ClangDiagsDefsEmitter - The top-level class emits .def files containing |
| 483 | /// declarations of Clang diagnostics. |
| 484 | namespace clang { |
| 485 | void EmitClangDiagsDefs(RecordKeeper &Records, raw_ostream &OS, |
| 486 | const std::string &Component) { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 487 | // Write the #if guard |
| 488 | if (!Component.empty()) { |
Benjamin Kramer | 44f91da | 2011-11-06 20:36:48 +0000 | [diff] [blame] | 489 | std::string ComponentName = StringRef(Component).upper(); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 490 | OS << "#ifdef " << ComponentName << "START\n"; |
| 491 | OS << "__" << ComponentName << "START = DIAG_START_" << ComponentName |
| 492 | << ",\n"; |
| 493 | OS << "#undef " << ComponentName << "START\n"; |
| 494 | OS << "#endif\n\n"; |
| 495 | } |
| 496 | |
| 497 | const std::vector<Record*> &Diags = |
| 498 | Records.getAllDerivedDefinitions("Diagnostic"); |
Benjamin Kramer | a8cafe2 | 2012-02-15 20:57:03 +0000 | [diff] [blame] | 499 | |
Benjamin Kramer | a8cafe2 | 2012-02-15 20:57:03 +0000 | [diff] [blame] | 500 | std::vector<Record*> DiagGroups |
| 501 | = Records.getAllDerivedDefinitions("DiagGroup"); |
Argyrios Kyrtzidis | 87acf19 | 2012-03-06 00:00:38 +0000 | [diff] [blame] | 502 | |
| 503 | std::map<std::string, GroupInfo> DiagsInGroup; |
| 504 | groupDiagnostics(Diags, DiagGroups, DiagsInGroup); |
Benjamin Kramer | a8cafe2 | 2012-02-15 20:57:03 +0000 | [diff] [blame] | 505 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 506 | DiagCategoryIDMap CategoryIDs(Records); |
| 507 | DiagGroupParentMap DGParentMap(Records); |
| 508 | |
Ted Kremenek | b22ea2a | 2012-07-07 05:53:30 +0000 | [diff] [blame] | 509 | // Compute the set of diagnostics that are in -Wpedantic. |
| 510 | RecordSet DiagsInPedantic; |
| 511 | InferPedantic inferPedantic(DGParentMap, Diags, DiagGroups, DiagsInGroup); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 512 | inferPedantic.compute(&DiagsInPedantic, (RecordVec*)nullptr); |
Ted Kremenek | b22ea2a | 2012-07-07 05:53:30 +0000 | [diff] [blame] | 513 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 514 | for (unsigned i = 0, e = Diags.size(); i != e; ++i) { |
| 515 | const Record &R = *Diags[i]; |
Craig Topper | f3932e3 | 2013-07-19 21:43:59 +0000 | [diff] [blame] | 516 | |
Ted Kremenek | 7249239 | 2012-08-07 05:01:49 +0000 | [diff] [blame] | 517 | // Check if this is an error that is accidentally in a warning |
| 518 | // group. |
| 519 | if (isError(R)) { |
Sean Silva | 1c4aaa8 | 2012-10-10 20:25:43 +0000 | [diff] [blame] | 520 | if (DefInit *Group = dyn_cast<DefInit>(R.getValueInit("Group"))) { |
Ted Kremenek | 7249239 | 2012-08-07 05:01:49 +0000 | [diff] [blame] | 521 | const Record *GroupRec = Group->getDef(); |
| 522 | const std::string &GroupName = GroupRec->getValueAsString("GroupName"); |
Joerg Sonnenberger | 691a16b | 2012-10-25 16:37:08 +0000 | [diff] [blame] | 523 | PrintFatalError(R.getLoc(), "Error " + R.getName() + |
| 524 | " cannot be in a warning group [" + GroupName + "]"); |
Ted Kremenek | 7249239 | 2012-08-07 05:01:49 +0000 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | |
Tobias Grosser | 7416024 | 2014-02-28 09:11:08 +0000 | [diff] [blame] | 528 | // Check that all remarks have an associated diagnostic group. |
| 529 | if (isRemark(R)) { |
| 530 | if (!isa<DefInit>(R.getValueInit("Group"))) { |
| 531 | PrintFatalError(R.getLoc(), "Error " + R.getName() + |
| 532 | " not in any diagnostic group"); |
| 533 | } |
| 534 | } |
| 535 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 536 | // Filter by component. |
| 537 | if (!Component.empty() && Component != R.getValueAsString("Component")) |
| 538 | continue; |
| 539 | |
| 540 | OS << "DIAG(" << R.getName() << ", "; |
| 541 | OS << R.getValueAsDef("Class")->getName(); |
Alp Toker | 46df1c0 | 2014-06-12 10:15:20 +0000 | [diff] [blame^] | 542 | OS << ", (unsigned)diag::Severity::" |
| 543 | << R.getValueAsDef("DefaultSeverity")->getValueAsString("Name"); |
Craig Topper | f3932e3 | 2013-07-19 21:43:59 +0000 | [diff] [blame] | 544 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 545 | // Description string. |
| 546 | OS << ", \""; |
| 547 | OS.write_escaped(R.getValueAsString("Text")) << '"'; |
Craig Topper | f3932e3 | 2013-07-19 21:43:59 +0000 | [diff] [blame] | 548 | |
Benjamin Kramer | a8cafe2 | 2012-02-15 20:57:03 +0000 | [diff] [blame] | 549 | // Warning associated with the diagnostic. This is stored as an index into |
| 550 | // the alphabetically sorted warning table. |
Sean Silva | 1c4aaa8 | 2012-10-10 20:25:43 +0000 | [diff] [blame] | 551 | if (DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group"))) { |
Argyrios Kyrtzidis | 87acf19 | 2012-03-06 00:00:38 +0000 | [diff] [blame] | 552 | std::map<std::string, GroupInfo>::iterator I = |
| 553 | DiagsInGroup.find(DI->getDef()->getValueAsString("GroupName")); |
| 554 | assert(I != DiagsInGroup.end()); |
| 555 | OS << ", " << I->second.IDNo; |
Ted Kremenek | b22ea2a | 2012-07-07 05:53:30 +0000 | [diff] [blame] | 556 | } else if (DiagsInPedantic.count(&R)) { |
| 557 | std::map<std::string, GroupInfo>::iterator I = |
| 558 | DiagsInGroup.find("pedantic"); |
| 559 | assert(I != DiagsInGroup.end() && "pedantic group not defined"); |
| 560 | OS << ", " << I->second.IDNo; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 561 | } else { |
Benjamin Kramer | a8cafe2 | 2012-02-15 20:57:03 +0000 | [diff] [blame] | 562 | OS << ", 0"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Richard Smith | 16e1b07 | 2013-11-12 02:41:45 +0000 | [diff] [blame] | 565 | // SFINAE response. |
| 566 | OS << ", " << R.getValueAsDef("SFINAE")->getName(); |
| 567 | |
| 568 | // Default warning has no Werror bit. |
| 569 | if (R.getValueAsBit("WarningNoWerror")) |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 570 | OS << ", true"; |
| 571 | else |
| 572 | OS << ", false"; |
| 573 | |
Richard Smith | 16e1b07 | 2013-11-12 02:41:45 +0000 | [diff] [blame] | 574 | // Default warning show in system header bit. |
| 575 | if (R.getValueAsBit("WarningShowInSystemHeader")) |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 576 | OS << ", true"; |
| 577 | else |
| 578 | OS << ", false"; |
| 579 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 580 | // Category number. |
| 581 | OS << ", " << CategoryIDs.getID(getDiagnosticCategory(&R, DGParentMap)); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 582 | OS << ")\n"; |
| 583 | } |
| 584 | } |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 585 | } // end namespace clang |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 586 | |
| 587 | //===----------------------------------------------------------------------===// |
| 588 | // Warning Group Tables generation |
| 589 | //===----------------------------------------------------------------------===// |
| 590 | |
| 591 | static std::string getDiagCategoryEnum(llvm::StringRef name) { |
| 592 | if (name.empty()) |
| 593 | return "DiagCat_None"; |
Dylan Noblesmith | f1a13f2 | 2012-02-13 12:32:26 +0000 | [diff] [blame] | 594 | SmallString<256> enumName = llvm::StringRef("DiagCat_"); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 595 | for (llvm::StringRef::iterator I = name.begin(), E = name.end(); I != E; ++I) |
| 596 | enumName += isalnum(*I) ? *I : '_'; |
| 597 | return enumName.str(); |
| 598 | } |
Craig Topper | f3932e3 | 2013-07-19 21:43:59 +0000 | [diff] [blame] | 599 | |
Tobias Grosser | cfc57bb | 2014-05-06 22:06:56 +0000 | [diff] [blame] | 600 | /// \brief Emit the array of diagnostic subgroups. |
| 601 | /// |
| 602 | /// The array of diagnostic subgroups contains for each group a list of its |
| 603 | /// subgroups. The individual lists are separated by '-1'. Groups with no |
| 604 | /// subgroups are skipped. |
| 605 | /// |
| 606 | /// \code |
| 607 | /// static const int16_t DiagSubGroups[] = { |
| 608 | /// /* Empty */ -1, |
| 609 | /// /* DiagSubGroup0 */ 142, -1, |
| 610 | /// /* DiagSubGroup13 */ 265, 322, 399, -1 |
| 611 | /// } |
| 612 | /// \endcode |
| 613 | /// |
| 614 | static void emitDiagSubGroups(std::map<std::string, GroupInfo> &DiagsInGroup, |
| 615 | RecordVec &GroupsInPedantic, raw_ostream &OS) { |
| 616 | OS << "static const int16_t DiagSubGroups[] = {\n" |
| 617 | << " /* Empty */ -1,\n"; |
| 618 | for (auto const &I : DiagsInGroup) { |
| 619 | const bool IsPedantic = I.first == "pedantic"; |
| 620 | |
| 621 | const std::vector<std::string> &SubGroups = I.second.SubGroups; |
| 622 | if (!SubGroups.empty() || (IsPedantic && !GroupsInPedantic.empty())) { |
| 623 | OS << " /* DiagSubGroup" << I.second.IDNo << " */ "; |
| 624 | for (auto const &SubGroup : SubGroups) { |
| 625 | std::map<std::string, GroupInfo>::const_iterator RI = |
| 626 | DiagsInGroup.find(SubGroup); |
| 627 | assert(RI != DiagsInGroup.end() && "Referenced without existing?"); |
| 628 | OS << RI->second.IDNo << ", "; |
| 629 | } |
| 630 | // Emit the groups implicitly in "pedantic". |
| 631 | if (IsPedantic) { |
| 632 | for (auto const &Group : GroupsInPedantic) { |
| 633 | const std::string &GroupName = Group->getValueAsString("GroupName"); |
| 634 | std::map<std::string, GroupInfo>::const_iterator RI = |
| 635 | DiagsInGroup.find(GroupName); |
| 636 | assert(RI != DiagsInGroup.end() && "Referenced without existing?"); |
| 637 | OS << RI->second.IDNo << ", "; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | OS << "-1,\n"; |
| 642 | } |
| 643 | } |
| 644 | OS << "};\n\n"; |
| 645 | } |
| 646 | |
| 647 | /// \brief Emit the list of diagnostic arrays. |
| 648 | /// |
| 649 | /// This data structure is a large array that contains itself arrays of varying |
| 650 | /// size. Each array represents a list of diagnostics. The different arrays are |
| 651 | /// separated by the value '-1'. |
| 652 | /// |
| 653 | /// \code |
| 654 | /// static const int16_t DiagArrays[] = { |
| 655 | /// /* Empty */ -1, |
| 656 | /// /* DiagArray1 */ diag::warn_pragma_message, |
| 657 | /// -1, |
| 658 | /// /* DiagArray2 */ diag::warn_abs_too_small, |
| 659 | /// diag::warn_unsigned_abs, |
| 660 | /// diag::warn_wrong_absolute_value_type, |
| 661 | /// -1 |
| 662 | /// }; |
| 663 | /// \endcode |
| 664 | /// |
| 665 | static void emitDiagArrays(std::map<std::string, GroupInfo> &DiagsInGroup, |
| 666 | RecordVec &DiagsInPedantic, raw_ostream &OS) { |
| 667 | OS << "static const int16_t DiagArrays[] = {\n" |
| 668 | << " /* Empty */ -1,\n"; |
| 669 | for (auto const &I : DiagsInGroup) { |
| 670 | const bool IsPedantic = I.first == "pedantic"; |
| 671 | |
| 672 | const std::vector<const Record *> &V = I.second.DiagsInGroup; |
| 673 | if (!V.empty() || (IsPedantic && !DiagsInPedantic.empty())) { |
| 674 | OS << " /* DiagArray" << I.second.IDNo << " */ "; |
| 675 | for (auto *Record : V) |
| 676 | OS << "diag::" << Record->getName() << ", "; |
| 677 | // Emit the diagnostics implicitly in "pedantic". |
| 678 | if (IsPedantic) { |
| 679 | for (auto const &Diag : DiagsInPedantic) |
| 680 | OS << "diag::" << Diag->getName() << ", "; |
| 681 | } |
| 682 | OS << "-1,\n"; |
| 683 | } |
| 684 | } |
| 685 | OS << "};\n\n"; |
| 686 | } |
| 687 | |
| 688 | /// \brief Emit a list of group names. |
| 689 | /// |
| 690 | /// This creates a long string which by itself contains a list of pascal style |
| 691 | /// strings, which consist of a length byte directly followed by the string. |
| 692 | /// |
| 693 | /// \code |
| 694 | /// static const char DiagGroupNames[] = { |
| 695 | /// \000\020#pragma-messages\t#warnings\020CFString-literal" |
| 696 | /// }; |
| 697 | /// \endcode |
| 698 | static void emitDiagGroupNames(StringToOffsetTable &GroupNames, |
| 699 | raw_ostream &OS) { |
| 700 | OS << "static const char DiagGroupNames[] = {\n"; |
| 701 | GroupNames.EmitString(OS); |
| 702 | OS << "};\n\n"; |
| 703 | } |
| 704 | |
| 705 | /// \brief Emit diagnostic arrays and related data structures. |
| 706 | /// |
| 707 | /// This creates the actual diagnostic array, an array of diagnostic subgroups |
| 708 | /// and an array of subgroup names. |
| 709 | /// |
| 710 | /// \code |
| 711 | /// #ifdef GET_DIAG_ARRAYS |
| 712 | /// static const int16_t DiagArrays[]; |
| 713 | /// static const int16_t DiagSubGroups[]; |
| 714 | /// static const char DiagGroupNames[]; |
| 715 | /// #endif |
| 716 | /// \endcode |
| 717 | static void emitAllDiagArrays(std::map<std::string, GroupInfo> &DiagsInGroup, |
| 718 | RecordVec &DiagsInPedantic, |
| 719 | RecordVec &GroupsInPedantic, |
| 720 | StringToOffsetTable &GroupNames, |
| 721 | raw_ostream &OS) { |
| 722 | OS << "\n#ifdef GET_DIAG_ARRAYS\n"; |
| 723 | emitDiagArrays(DiagsInGroup, DiagsInPedantic, OS); |
| 724 | emitDiagSubGroups(DiagsInGroup, GroupsInPedantic, OS); |
| 725 | emitDiagGroupNames(GroupNames, OS); |
| 726 | OS << "#endif // GET_DIAG_ARRAYS\n\n"; |
| 727 | } |
| 728 | |
| 729 | /// \brief Emit diagnostic table. |
| 730 | /// |
| 731 | /// The table is sorted by the name of the diagnostic group. Each element |
| 732 | /// consists of the name of the diagnostic group (given as offset in the |
| 733 | /// group name table), a reference to a list of diagnostics (optional) and a |
| 734 | /// reference to a set of subgroups (optional). |
| 735 | /// |
| 736 | /// \code |
| 737 | /// #ifdef GET_DIAG_TABLE |
| 738 | /// {/* abi */ 159, /* DiagArray11 */ 19, /* Empty */ 0}, |
| 739 | /// {/* aggregate-return */ 180, /* Empty */ 0, /* Empty */ 0}, |
| 740 | /// {/* all */ 197, /* Empty */ 0, /* DiagSubGroup13 */ 3}, |
| 741 | /// {/* deprecated */ 1981,/* DiagArray1 */ 348, /* DiagSubGroup3 */ 9}, |
| 742 | /// #endif |
| 743 | /// \endcode |
| 744 | static void emitDiagTable(std::map<std::string, GroupInfo> &DiagsInGroup, |
| 745 | RecordVec &DiagsInPedantic, |
| 746 | RecordVec &GroupsInPedantic, |
| 747 | StringToOffsetTable &GroupNames, raw_ostream &OS) { |
| 748 | unsigned MaxLen = 0; |
| 749 | |
| 750 | for (auto const &I: DiagsInGroup) |
| 751 | MaxLen = std::max(MaxLen, (unsigned)I.first.size()); |
| 752 | |
| 753 | OS << "\n#ifdef GET_DIAG_TABLE\n"; |
| 754 | unsigned SubGroupIndex = 1, DiagArrayIndex = 1; |
| 755 | for (auto const &I: DiagsInGroup) { |
| 756 | // Group option string. |
| 757 | OS << " { /* "; |
| 758 | if (I.first.find_first_not_of("abcdefghijklmnopqrstuvwxyz" |
| 759 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 760 | "0123456789!@#$%^*-+=:?") != |
| 761 | std::string::npos) |
| 762 | PrintFatalError("Invalid character in diagnostic group '" + I.first + |
| 763 | "'"); |
| 764 | OS << I.first << " */ " << std::string(MaxLen - I.first.size(), ' '); |
| 765 | // Store a pascal-style length byte at the beginning of the string. |
| 766 | std::string Name = char(I.first.size()) + I.first; |
| 767 | OS << GroupNames.GetOrAddStringOffset(Name, false) << ", "; |
| 768 | |
| 769 | // Special handling for 'pedantic'. |
| 770 | const bool IsPedantic = I.first == "pedantic"; |
| 771 | |
| 772 | // Diagnostics in the group. |
| 773 | const std::vector<const Record *> &V = I.second.DiagsInGroup; |
| 774 | const bool hasDiags = |
| 775 | !V.empty() || (IsPedantic && !DiagsInPedantic.empty()); |
| 776 | if (hasDiags) { |
| 777 | OS << "/* DiagArray" << I.second.IDNo << " */ " << DiagArrayIndex |
| 778 | << ", "; |
| 779 | if (IsPedantic) |
| 780 | DiagArrayIndex += DiagsInPedantic.size(); |
| 781 | DiagArrayIndex += V.size() + 1; |
| 782 | } else { |
| 783 | OS << "/* Empty */ 0, "; |
| 784 | } |
| 785 | |
| 786 | // Subgroups. |
| 787 | const std::vector<std::string> &SubGroups = I.second.SubGroups; |
| 788 | const bool hasSubGroups = |
| 789 | !SubGroups.empty() || (IsPedantic && !GroupsInPedantic.empty()); |
| 790 | if (hasSubGroups) { |
| 791 | OS << "/* DiagSubGroup" << I.second.IDNo << " */ " << SubGroupIndex; |
| 792 | if (IsPedantic) |
| 793 | SubGroupIndex += GroupsInPedantic.size(); |
| 794 | SubGroupIndex += SubGroups.size() + 1; |
| 795 | } else { |
| 796 | OS << "/* Empty */ 0"; |
| 797 | } |
| 798 | |
| 799 | OS << " },\n"; |
| 800 | } |
| 801 | OS << "#endif // GET_DIAG_TABLE\n\n"; |
| 802 | } |
| 803 | |
| 804 | /// \brief Emit the table of diagnostic categories. |
| 805 | /// |
| 806 | /// The table has the form of macro calls that have two parameters. The |
| 807 | /// category's name as well as an enum that represents the category. The |
| 808 | /// table can be used by defining the macro 'CATEGORY' and including this |
| 809 | /// table right after. |
| 810 | /// |
| 811 | /// \code |
| 812 | /// #ifdef GET_CATEGORY_TABLE |
| 813 | /// CATEGORY("Semantic Issue", DiagCat_Semantic_Issue) |
| 814 | /// CATEGORY("Lambda Issue", DiagCat_Lambda_Issue) |
| 815 | /// #endif |
| 816 | /// \endcode |
| 817 | static void emitCategoryTable(RecordKeeper &Records, raw_ostream &OS) { |
| 818 | DiagCategoryIDMap CategoriesByID(Records); |
| 819 | OS << "\n#ifdef GET_CATEGORY_TABLE\n"; |
| 820 | for (auto const &C : CategoriesByID) |
| 821 | OS << "CATEGORY(\"" << C << "\", " << getDiagCategoryEnum(C) << ")\n"; |
| 822 | OS << "#endif // GET_CATEGORY_TABLE\n\n"; |
| 823 | } |
| 824 | |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 825 | namespace clang { |
| 826 | void EmitClangDiagGroups(RecordKeeper &Records, raw_ostream &OS) { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 827 | // Compute a mapping from a DiagGroup to all of its parents. |
| 828 | DiagGroupParentMap DGParentMap(Records); |
Ted Kremenek | b22ea2a | 2012-07-07 05:53:30 +0000 | [diff] [blame] | 829 | |
Tobias Grosser | cfc57bb | 2014-05-06 22:06:56 +0000 | [diff] [blame] | 830 | std::vector<Record *> Diags = Records.getAllDerivedDefinitions("Diagnostic"); |
Craig Topper | f3932e3 | 2013-07-19 21:43:59 +0000 | [diff] [blame] | 831 | |
Tobias Grosser | cfc57bb | 2014-05-06 22:06:56 +0000 | [diff] [blame] | 832 | std::vector<Record *> DiagGroups = |
| 833 | Records.getAllDerivedDefinitions("DiagGroup"); |
Argyrios Kyrtzidis | 87acf19 | 2012-03-06 00:00:38 +0000 | [diff] [blame] | 834 | |
| 835 | std::map<std::string, GroupInfo> DiagsInGroup; |
| 836 | groupDiagnostics(Diags, DiagGroups, DiagsInGroup); |
Ted Kremenek | b22ea2a | 2012-07-07 05:53:30 +0000 | [diff] [blame] | 837 | |
| 838 | // All extensions are implicitly in the "pedantic" group. Record the |
| 839 | // implicit set of groups in the "pedantic" group, and use this information |
| 840 | // later when emitting the group information for Pedantic. |
| 841 | RecordVec DiagsInPedantic; |
| 842 | RecordVec GroupsInPedantic; |
| 843 | InferPedantic inferPedantic(DGParentMap, Diags, DiagGroups, DiagsInGroup); |
| 844 | inferPedantic.compute(&DiagsInPedantic, &GroupsInPedantic); |
| 845 | |
Craig Topper | da7cf8a | 2013-08-29 05:18:04 +0000 | [diff] [blame] | 846 | StringToOffsetTable GroupNames; |
| 847 | for (std::map<std::string, GroupInfo>::const_iterator |
Tobias Grosser | cfc57bb | 2014-05-06 22:06:56 +0000 | [diff] [blame] | 848 | I = DiagsInGroup.begin(), |
| 849 | E = DiagsInGroup.end(); |
| 850 | I != E; ++I) { |
Craig Topper | da7cf8a | 2013-08-29 05:18:04 +0000 | [diff] [blame] | 851 | // Store a pascal-style length byte at the beginning of the string. |
| 852 | std::string Name = char(I->first.size()) + I->first; |
| 853 | GroupNames.GetOrAddStringOffset(Name, false); |
| 854 | } |
| 855 | |
Tobias Grosser | cfc57bb | 2014-05-06 22:06:56 +0000 | [diff] [blame] | 856 | emitAllDiagArrays(DiagsInGroup, DiagsInPedantic, GroupsInPedantic, GroupNames, |
| 857 | OS); |
| 858 | emitDiagTable(DiagsInGroup, DiagsInPedantic, GroupsInPedantic, GroupNames, |
| 859 | OS); |
| 860 | emitCategoryTable(Records, OS); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 861 | } |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 862 | } // end namespace clang |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 863 | |
| 864 | //===----------------------------------------------------------------------===// |
| 865 | // Diagnostic name index generation |
| 866 | //===----------------------------------------------------------------------===// |
| 867 | |
| 868 | namespace { |
| 869 | struct RecordIndexElement |
| 870 | { |
| 871 | RecordIndexElement() {} |
| 872 | explicit RecordIndexElement(Record const &R): |
| 873 | Name(R.getName()) {} |
Craig Topper | f3932e3 | 2013-07-19 21:43:59 +0000 | [diff] [blame] | 874 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 875 | std::string Name; |
| 876 | }; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 877 | } // end anonymous namespace. |
| 878 | |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 879 | namespace clang { |
| 880 | void EmitClangDiagsIndexName(RecordKeeper &Records, raw_ostream &OS) { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 881 | const std::vector<Record*> &Diags = |
| 882 | Records.getAllDerivedDefinitions("Diagnostic"); |
Craig Topper | f3932e3 | 2013-07-19 21:43:59 +0000 | [diff] [blame] | 883 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 884 | std::vector<RecordIndexElement> Index; |
| 885 | Index.reserve(Diags.size()); |
| 886 | for (unsigned i = 0, e = Diags.size(); i != e; ++i) { |
Craig Topper | f3932e3 | 2013-07-19 21:43:59 +0000 | [diff] [blame] | 887 | const Record &R = *(Diags[i]); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 888 | Index.push_back(RecordIndexElement(R)); |
| 889 | } |
Craig Topper | f3932e3 | 2013-07-19 21:43:59 +0000 | [diff] [blame] | 890 | |
Benjamin Kramer | bbdd764 | 2014-03-01 14:48:57 +0000 | [diff] [blame] | 891 | std::sort(Index.begin(), Index.end(), |
| 892 | [](const RecordIndexElement &Lhs, |
| 893 | const RecordIndexElement &Rhs) { return Lhs.Name < Rhs.Name; }); |
Craig Topper | f3932e3 | 2013-07-19 21:43:59 +0000 | [diff] [blame] | 894 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 895 | for (unsigned i = 0, e = Index.size(); i != e; ++i) { |
| 896 | const RecordIndexElement &R = Index[i]; |
Craig Topper | f3932e3 | 2013-07-19 21:43:59 +0000 | [diff] [blame] | 897 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 898 | OS << "DIAG_NAME_INDEX(" << R.Name << ")\n"; |
| 899 | } |
| 900 | } |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 901 | } // end namespace clang |