| 1 |
//===-- ClangDiagnosticsEmitter.cpp - Generate Clang diagnostics tables ---===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
// |
| 9 |
// These tablegen backends emit Clang diagnostics tables. |
| 10 |
// |
| 11 |
//===----------------------------------------------------------------------===// |
| 12 |
|
| 13 |
#include "TableGenBackends.h" |
| 14 |
#include "llvm/ADT/DenseSet.h" |
| 15 |
#include "llvm/ADT/PointerUnion.h" |
| 16 |
#include "llvm/ADT/STLExtras.h" |
| 17 |
#include "llvm/ADT/SmallString.h" |
| 18 |
#include "llvm/ADT/SmallVector.h" |
| 19 |
#include "llvm/ADT/StringMap.h" |
| 20 |
#include "llvm/ADT/StringSwitch.h" |
| 21 |
#include "llvm/ADT/Twine.h" |
| 22 |
#include "llvm/Support/Casting.h" |
| 23 |
#include "llvm/Support/Format.h" |
| 24 |
#include "llvm/TableGen/Error.h" |
| 25 |
#include "llvm/TableGen/Record.h" |
| 26 |
#include "llvm/TableGen/StringToOffsetTable.h" |
| 27 |
#include "llvm/TableGen/TableGenBackend.h" |
| 28 |
#include <algorithm> |
| 29 |
#include <cctype> |
| 30 |
#include <functional> |
| 31 |
#include <map> |
| 32 |
#include <optional> |
| 33 |
#include <set> |
| 34 |
using namespace llvm; |
| 35 |
|
| 36 |
//===----------------------------------------------------------------------===// |
| 37 |
// Diagnostic category computation code. |
| 38 |
//===----------------------------------------------------------------------===// |
| 39 |
|
| 40 |
namespace { |
| 41 |
class DiagGroupParentMap { |
| 42 |
const RecordKeeper &Records; |
| 43 |
std::map<const Record *, std::vector<const Record *>> Mapping; |
| 44 |
|
| 45 |
public: |
| 46 |
DiagGroupParentMap(const RecordKeeper &records) : Records(records) { |
| 47 |
for (const Record *Group : Records.getAllDerivedDefinitions("DiagGroup")) |
| 48 |
for (const Record *SubGroup : Group->getValueAsListOfDefs("SubGroups")) |
| 49 |
Mapping[SubGroup].push_back(Group); |
| 50 |
} |
| 51 |
|
| 52 |
ArrayRef<const Record *> getParents(const Record *Group) { |
| 53 |
return Mapping[Group]; |
| 54 |
} |
| 55 |
}; |
| 56 |
} // end anonymous namespace. |
| 57 |
|
| 58 |
static StringRef |
| 59 |
getCategoryFromDiagGroup(const Record *Group, |
| 60 |
DiagGroupParentMap &DiagGroupParents) { |
| 61 |
// If the DiagGroup has a category, return it. |
| 62 |
StringRef CatName = Group->getValueAsString("CategoryName"); |
| 63 |
if (!CatName.empty()) return CatName; |
| 64 |
|
| 65 |
// The diag group may the subgroup of one or more other diagnostic groups, |
| 66 |
// check these for a category as well. |
| 67 |
for (const Record *Parent : DiagGroupParents.getParents(Group)) { |
| 68 |
CatName = getCategoryFromDiagGroup(Parent, DiagGroupParents); |
| 69 |
if (!CatName.empty()) return CatName; |
| 70 |
} |
| 71 |
return ""; |
| 72 |
} |
| 73 |
|
| 74 |
/// getDiagnosticCategory - Return the category that the specified diagnostic |
| 75 |
/// lives in. |
| 76 |
static StringRef getDiagnosticCategory(const Record *R, |
| 77 |
DiagGroupParentMap &DiagGroupParents) { |
| 78 |
// If the diagnostic is in a group, and that group has a category, use it. |
| 79 |
if (const auto *Group = dyn_cast<DefInit>(R->getValueInit("Group"))) { |
| 80 |
// Check the diagnostic's diag group for a category. |
| 81 |
StringRef CatName = |
| 82 |
getCategoryFromDiagGroup(Group->getDef(), DiagGroupParents); |
| 83 |
if (!CatName.empty()) return CatName; |
| 84 |
} |
| 85 |
|
| 86 |
// If the diagnostic itself has a category, get it. |
| 87 |
return R->getValueAsString("CategoryName"); |
| 88 |
} |
| 89 |
|
| 90 |
namespace { |
| 91 |
class DiagCategoryIDMap { |
| 92 |
const RecordKeeper &Records; |
| 93 |
StringMap<unsigned> CategoryIDs; |
| 94 |
std::vector<StringRef> CategoryStrings; |
| 95 |
|
| 96 |
public: |
| 97 |
DiagCategoryIDMap(const RecordKeeper &records) : Records(records) { |
| 98 |
DiagGroupParentMap ParentInfo(Records); |
| 99 |
|
| 100 |
// The zero'th category is "". |
| 101 |
CategoryStrings.push_back(""); |
| 102 |
CategoryIDs[""] = 0; |
| 103 |
|
| 104 |
for (const Record *Diag : |
| 105 |
Records.getAllDerivedDefinitions("Diagnostic")) { |
| 106 |
StringRef Category = getDiagnosticCategory(Diag, ParentInfo); |
| 107 |
if (Category.empty()) continue; // Skip diags with no category. |
| 108 |
|
| 109 |
unsigned &ID = CategoryIDs[Category]; |
| 110 |
if (ID != 0) continue; // Already seen. |
| 111 |
|
| 112 |
ID = CategoryStrings.size(); |
| 113 |
CategoryStrings.push_back(Category); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
unsigned getID(StringRef CategoryString) { |
| 118 |
return CategoryIDs[CategoryString]; |
| 119 |
} |
| 120 |
|
| 121 |
typedef std::vector<StringRef>::const_iterator const_iterator; |
| 122 |
const_iterator begin() const { return CategoryStrings.begin(); } |
| 123 |
const_iterator end() const { return CategoryStrings.end(); } |
| 124 |
}; |
| 125 |
|
| 126 |
struct GroupInfo { |
| 127 |
StringRef GroupName; |
| 128 |
std::vector<const Record*> DiagsInGroup; |
| 129 |
std::vector<StringRef> SubGroups; |
| 130 |
unsigned IDNo = 0; |
| 131 |
|
| 132 |
SmallVector<const Record *, 1> Defs; |
| 133 |
|
| 134 |
GroupInfo() = default; |
| 135 |
}; |
| 136 |
} // end anonymous namespace. |
| 137 |
|
| 138 |
static bool beforeThanCompare(const Record *LHS, const Record *RHS) { |
| 139 |
assert(!LHS->getLoc().empty() && !RHS->getLoc().empty()); |
| 140 |
return |
| 141 |
LHS->getLoc().front().getPointer() < RHS->getLoc().front().getPointer(); |
| 142 |
} |
| 143 |
|
| 144 |
static bool diagGroupBeforeByName(const Record *LHS, const Record *RHS) { |
| 145 |
return LHS->getValueAsString("GroupName") < |
| 146 |
RHS->getValueAsString("GroupName"); |
| 147 |
} |
| 148 |
|
| 149 |
using DiagsInGroupTy = std::map<StringRef, GroupInfo>; |
| 150 |
|
| 151 |
/// Invert the 1-[0/1] mapping of diags to group into a one to many |
| 152 |
/// mapping of groups to diags in the group. |
| 153 |
static void groupDiagnostics(ArrayRef<const Record *> Diags, |
| 154 |
ArrayRef<const Record *> DiagGroups, |
| 155 |
DiagsInGroupTy &DiagsInGroup) { |
| 156 |
for (const Record *R : Diags) { |
| 157 |
const auto *DI = dyn_cast<DefInit>(R->getValueInit("Group")); |
| 158 |
if (!DI) |
| 159 |
continue; |
| 160 |
assert(R->getValueAsDef("Class")->getName() != "CLASS_NOTE" && |
| 161 |
"Note can't be in a DiagGroup"); |
| 162 |
StringRef GroupName = DI->getDef()->getValueAsString("GroupName"); |
| 163 |
DiagsInGroup[GroupName].DiagsInGroup.push_back(R); |
| 164 |
} |
| 165 |
|
| 166 |
// Add all DiagGroup's to the DiagsInGroup list to make sure we pick up empty |
| 167 |
// groups (these are warnings that GCC supports that clang never produces). |
| 168 |
for (const Record *Group : DiagGroups) { |
| 169 |
GroupInfo &GI = DiagsInGroup[Group->getValueAsString("GroupName")]; |
| 170 |
GI.GroupName = Group->getName(); |
| 171 |
GI.Defs.push_back(Group); |
| 172 |
|
| 173 |
for (const Record *SubGroup : Group->getValueAsListOfDefs("SubGroups")) |
| 174 |
GI.SubGroups.push_back(SubGroup->getValueAsString("GroupName")); |
| 175 |
} |
| 176 |
|
| 177 |
// Assign unique ID numbers to the groups. |
| 178 |
for (auto [IdNo, Iter] : enumerate(DiagsInGroup)) |
| 179 |
Iter.second.IDNo = IdNo; |
| 180 |
|
| 181 |
// Warn if the same group is defined more than once (including implicitly). |
| 182 |
for (auto &Group : DiagsInGroup) { |
| 183 |
if (Group.second.Defs.size() == 1 && |
| 184 |
(!Group.second.Defs.front()->isAnonymous() || |
| 185 |
Group.second.DiagsInGroup.size() <= 1)) |
| 186 |
continue; |
| 187 |
|
| 188 |
bool First = true; |
| 189 |
for (const Record *Def : Group.second.Defs) { |
| 190 |
// Skip implicit definitions from diagnostics; we'll report those |
| 191 |
// separately below. |
| 192 |
bool IsImplicit = false; |
| 193 |
for (const Record *Diag : Group.second.DiagsInGroup) { |
| 194 |
if (cast<DefInit>(Diag->getValueInit("Group"))->getDef() == Def) { |
| 195 |
IsImplicit = true; |
| 196 |
break; |
| 197 |
} |
| 198 |
} |
| 199 |
if (IsImplicit) |
| 200 |
continue; |
| 201 |
|
| 202 |
SMLoc Loc = Def->getLoc().front(); |
| 203 |
if (First) { |
| 204 |
SrcMgr.PrintMessage(Loc, SourceMgr::DK_Error, |
| 205 |
Twine("group '") + Group.first + |
| 206 |
"' is defined more than once"); |
| 207 |
First = false; |
| 208 |
} else { |
| 209 |
SrcMgr.PrintMessage(Loc, SourceMgr::DK_Note, "also defined here"); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
for (const Record *Diag : Group.second.DiagsInGroup) { |
| 214 |
if (!cast<DefInit>(Diag->getValueInit("Group"))->getDef()->isAnonymous()) |
| 215 |
continue; |
| 216 |
|
| 217 |
SMLoc Loc = Diag->getLoc().front(); |
| 218 |
if (First) { |
| 219 |
SrcMgr.PrintMessage(Loc, SourceMgr::DK_Error, |
| 220 |
Twine("group '") + Group.first + |
| 221 |
"' is implicitly defined more than once"); |
| 222 |
First = false; |
| 223 |
} else { |
| 224 |
SrcMgr.PrintMessage(Loc, SourceMgr::DK_Note, |
| 225 |
"also implicitly defined here"); |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
//===----------------------------------------------------------------------===// |
| 232 |
// Infer members of -Wpedantic. |
| 233 |
//===----------------------------------------------------------------------===// |
| 234 |
|
| 235 |
typedef std::vector<const Record *> RecordVec; |
| 236 |
typedef DenseSet<const Record *> RecordSet; |
| 237 |
typedef PointerUnion<RecordVec *, RecordSet *> VecOrSet; |
| 238 |
|
| 239 |
namespace { |
| 240 |
class InferPedantic { |
| 241 |
typedef DenseMap<const Record *, std::pair<unsigned, std::optional<unsigned>>> |
| 242 |
GMap; |
| 243 |
|
| 244 |
DiagGroupParentMap &DiagGroupParents; |
| 245 |
ArrayRef<const Record *> Diags; |
| 246 |
const std::vector<const Record *> DiagGroups; |
| 247 |
DiagsInGroupTy &DiagsInGroup; |
| 248 |
DenseSet<const Record *> DiagsSet; |
| 249 |
GMap GroupCount; |
| 250 |
public: |
| 251 |
InferPedantic(DiagGroupParentMap &DiagGroupParents, |
| 252 |
ArrayRef<const Record *> Diags, |
| 253 |
ArrayRef<const Record *> DiagGroups, |
| 254 |
DiagsInGroupTy &DiagsInGroup) |
| 255 |
: DiagGroupParents(DiagGroupParents), Diags(Diags), |
| 256 |
DiagGroups(DiagGroups), DiagsInGroup(DiagsInGroup) {} |
| 257 |
|
| 258 |
/// Compute the set of diagnostics and groups that are immediately |
| 259 |
/// in -Wpedantic. |
| 260 |
void compute(VecOrSet DiagsInPedantic, |
| 261 |
VecOrSet GroupsInPedantic); |
| 262 |
|
| 263 |
private: |
| 264 |
/// Determine whether a group is a subgroup of another group. |
| 265 |
bool isSubGroupOfGroup(const Record *Group, StringRef RootGroupName); |
| 266 |
|
| 267 |
/// Determine if the diagnostic is an extension. |
| 268 |
bool isExtension(const Record *Diag); |
| 269 |
|
| 270 |
/// Determine if the diagnostic is off by default. |
| 271 |
bool isOffByDefault(const Record *Diag); |
| 272 |
|
| 273 |
/// Increment the count for a group, and transitively marked |
| 274 |
/// parent groups when appropriate. |
| 275 |
void markGroup(const Record *Group); |
| 276 |
|
| 277 |
/// Return true if the diagnostic is in a pedantic group. |
| 278 |
bool groupInPedantic(const Record *Group, bool increment = false); |
| 279 |
}; |
| 280 |
} // end anonymous namespace |
| 281 |
|
| 282 |
bool InferPedantic::isSubGroupOfGroup(const Record *Group, StringRef GName) { |
| 283 |
StringRef GroupName = Group->getValueAsString("GroupName"); |
| 284 |
if (GName == GroupName) |
| 285 |
return true; |
| 286 |
|
| 287 |
for (const Record *Parent : DiagGroupParents.getParents(Group)) |
| 288 |
if (isSubGroupOfGroup(Parent, GName)) |
| 289 |
return true; |
| 290 |
|
| 291 |
return false; |
| 292 |
} |
| 293 |
|
| 294 |
/// Determine if the diagnostic is an extension. |
| 295 |
bool InferPedantic::isExtension(const Record *Diag) { |
| 296 |
return Diag->getValueAsDef("Class")->getName() == "CLASS_EXTENSION"; |
| 297 |
} |
| 298 |
|
| 299 |
bool InferPedantic::isOffByDefault(const Record *Diag) { |
| 300 |
return Diag->getValueAsDef("DefaultSeverity")->getValueAsString("Name") == |
| 301 |
"Ignored"; |
| 302 |
} |
| 303 |
|
| 304 |
bool InferPedantic::groupInPedantic(const Record *Group, bool increment) { |
| 305 |
GMap::mapped_type &V = GroupCount[Group]; |
| 306 |
// Lazily compute the threshold value for the group count. |
| 307 |
if (!V.second) { |
| 308 |
const GroupInfo &GI = DiagsInGroup[Group->getValueAsString("GroupName")]; |
| 309 |
V.second = GI.SubGroups.size() + GI.DiagsInGroup.size(); |
| 310 |
} |
| 311 |
|
| 312 |
if (increment) |
| 313 |
++V.first; |
| 314 |
|
| 315 |
// Consider a group in -Wpendatic IFF if has at least one diagnostic |
| 316 |
// or subgroup AND all of those diagnostics and subgroups are covered |
| 317 |
// by -Wpedantic via our computation. |
| 318 |
return V.first != 0 && V.first == *V.second; |
| 319 |
} |
| 320 |
|
| 321 |
void InferPedantic::markGroup(const Record *Group) { |
| 322 |
// If all the diagnostics and subgroups have been marked as being |
| 323 |
// covered by -Wpedantic, increment the count of parent groups. Once the |
| 324 |
// group's count is equal to the number of subgroups and diagnostics in |
| 325 |
// that group, we can safely add this group to -Wpedantic. |
| 326 |
if (groupInPedantic(Group, /* increment */ true)) |
| 327 |
for (const Record *Parent : DiagGroupParents.getParents(Group)) |
| 328 |
markGroup(Parent); |
| 329 |
} |
| 330 |
|
| 331 |
void InferPedantic::compute(VecOrSet DiagsInPedantic, |
| 332 |
VecOrSet GroupsInPedantic) { |
| 333 |
// All extensions that are not on by default are implicitly in the |
| 334 |
// "pedantic" group. For those that aren't explicitly included in -Wpedantic, |
| 335 |
// mark them for consideration to be included in -Wpedantic directly. |
| 336 |
for (const Record *R : Diags) { |
| 337 |
if (!isExtension(R) || !isOffByDefault(R)) |
| 338 |
continue; |
| 339 |
DiagsSet.insert(R); |
| 340 |
if (const auto *Group = dyn_cast<DefInit>(R->getValueInit("Group"))) { |
| 341 |
const Record *GroupRec = Group->getDef(); |
| 342 |
if (!isSubGroupOfGroup(GroupRec, "pedantic")) { |
| 343 |
markGroup(GroupRec); |
| 344 |
} |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
// Compute the set of diagnostics that are directly in -Wpedantic. We |
| 349 |
// march through Diags a second time to ensure the results are emitted |
| 350 |
// in deterministic order. |
| 351 |
for (const Record *R : Diags) { |
| 352 |
if (!DiagsSet.count(R)) |
| 353 |
continue; |
| 354 |
// Check if the group is implicitly in -Wpedantic. If so, |
| 355 |
// the diagnostic should not be directly included in the -Wpedantic |
| 356 |
// diagnostic group. |
| 357 |
if (const auto *Group = dyn_cast<DefInit>(R->getValueInit("Group"))) |
| 358 |
if (groupInPedantic(Group->getDef())) |
| 359 |
continue; |
| 360 |
|
| 361 |
// The diagnostic is not included in a group that is (transitively) in |
| 362 |
// -Wpedantic. Include it in -Wpedantic directly. |
| 363 |
if (auto *V = dyn_cast<RecordVec *>(DiagsInPedantic)) |
| 364 |
V->push_back(R); |
| 365 |
else |
| 366 |
cast<RecordSet *>(DiagsInPedantic)->insert(R); |
| 367 |
} |
| 368 |
|
| 369 |
if (!GroupsInPedantic) |
| 370 |
return; |
| 371 |
|
| 372 |
// Compute the set of groups that are directly in -Wpedantic. We |
| 373 |
// march through the groups to ensure the results are emitted |
| 374 |
/// in a deterministc order. |
| 375 |
for (const Record *Group : DiagGroups) { |
| 376 |
if (!groupInPedantic(Group)) |
| 377 |
continue; |
| 378 |
|
| 379 |
const std::vector<const Record *> &Parents = |
| 380 |
DiagGroupParents.getParents(Group); |
| 381 |
bool AllParentsInPedantic = |
| 382 |
all_of(Parents, [&](const Record *R) { return groupInPedantic(R); }); |
| 383 |
// If all the parents are in -Wpedantic, this means that this diagnostic |
| 384 |
// group will be indirectly included by -Wpedantic already. In that |
| 385 |
// case, do not add it directly to -Wpedantic. If the group has no |
| 386 |
// parents, obviously it should go into -Wpedantic. |
| 387 |
if (Parents.size() > 0 && AllParentsInPedantic) |
| 388 |
continue; |
| 389 |
|
| 390 |
if (auto *V = dyn_cast<RecordVec *>(GroupsInPedantic)) |
| 391 |
V->push_back(Group); |
| 392 |
else |
| 393 |
cast<RecordSet *>(GroupsInPedantic)->insert(Group); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
namespace { |
| 398 |
enum PieceKind { |
| 399 |
MultiPieceClass, |
| 400 |
TextPieceClass, |
| 401 |
PlaceholderPieceClass, |
| 402 |
SelectPieceClass, |
| 403 |
EnumSelectPieceClass, |
| 404 |
PluralPieceClass, |
| 405 |
DiffPieceClass, |
| 406 |
SubstitutionPieceClass, |
| 407 |
}; |
| 408 |
|
| 409 |
enum ModifierType { |
| 410 |
MT_Unknown, |
| 411 |
MT_Placeholder, |
| 412 |
MT_Select, |
| 413 |
MT_EnumSelect, |
| 414 |
MT_Sub, |
| 415 |
MT_Plural, |
| 416 |
MT_Diff, |
| 417 |
MT_Ordinal, |
| 418 |
MT_Human, |
| 419 |
MT_S, |
| 420 |
MT_Q, |
| 421 |
MT_ObjCClass, |
| 422 |
MT_ObjCInstance, |
| 423 |
MT_Quoted, |
| 424 |
}; |
| 425 |
|
| 426 |
static StringRef getModifierName(ModifierType MT) { |
| 427 |
switch (MT) { |
| 428 |
case MT_EnumSelect: |
| 429 |
case MT_Select: |
| 430 |
return "select"; |
| 431 |
case MT_Sub: |
| 432 |
return "sub"; |
| 433 |
case MT_Diff: |
| 434 |
return "diff"; |
| 435 |
case MT_Plural: |
| 436 |
return "plural"; |
| 437 |
case MT_Ordinal: |
| 438 |
return "ordinal"; |
| 439 |
case MT_Human: |
| 440 |
return "human"; |
| 441 |
case MT_S: |
| 442 |
return "s"; |
| 443 |
case MT_Q: |
| 444 |
return "q"; |
| 445 |
case MT_Placeholder: |
| 446 |
return ""; |
| 447 |
case MT_ObjCClass: |
| 448 |
return "objcclass"; |
| 449 |
case MT_ObjCInstance: |
| 450 |
return "objcinstance"; |
| 451 |
case MT_Quoted: |
| 452 |
return "quoted"; |
| 453 |
case MT_Unknown: |
| 454 |
llvm_unreachable("invalid modifier type"); |
| 455 |
} |
| 456 |
// Unhandled case |
| 457 |
llvm_unreachable("invalid modifier type"); |
| 458 |
} |
| 459 |
|
| 460 |
struct Piece { |
| 461 |
// This type and its derived classes are move-only. |
| 462 |
Piece(PieceKind Kind) : ClassKind(Kind) {} |
| 463 |
Piece(Piece const &O) = delete; |
| 464 |
Piece &operator=(Piece const &) = delete; |
| 465 |
virtual ~Piece() {} |
| 466 |
|
| 467 |
PieceKind getPieceClass() const { return ClassKind; } |
| 468 |
static bool classof(const Piece *) { return true; } |
| 469 |
|
| 470 |
private: |
| 471 |
PieceKind ClassKind; |
| 472 |
}; |
| 473 |
|
| 474 |
struct MultiPiece : Piece { |
| 475 |
MultiPiece() : Piece(MultiPieceClass) {} |
| 476 |
MultiPiece(std::vector<Piece *> Pieces) |
| 477 |
: Piece(MultiPieceClass), Pieces(std::move(Pieces)) {} |
| 478 |
|
| 479 |
std::vector<Piece *> Pieces; |
| 480 |
|
| 481 |
static bool classof(const Piece *P) { |
| 482 |
return P->getPieceClass() == MultiPieceClass; |
| 483 |
} |
| 484 |
}; |
| 485 |
|
| 486 |
struct TextPiece : Piece { |
| 487 |
StringRef Role; |
| 488 |
std::string Text; |
| 489 |
TextPiece(StringRef Text, StringRef Role = "") |
| 490 |
: Piece(TextPieceClass), Role(Role), Text(Text.str()) {} |
| 491 |
|
| 492 |
static bool classof(const Piece *P) { |
| 493 |
return P->getPieceClass() == TextPieceClass; |
| 494 |
} |
| 495 |
}; |
| 496 |
|
| 497 |
struct PlaceholderPiece : Piece { |
| 498 |
ModifierType Kind; |
| 499 |
int Index; |
| 500 |
PlaceholderPiece(ModifierType Kind, int Index) |
| 501 |
: Piece(PlaceholderPieceClass), Kind(Kind), Index(Index) {} |
| 502 |
|
| 503 |
static bool classof(const Piece *P) { |
| 504 |
return P->getPieceClass() == PlaceholderPieceClass; |
| 505 |
} |
| 506 |
}; |
| 507 |
|
| 508 |
struct SelectPiece : Piece { |
| 509 |
protected: |
| 510 |
SelectPiece(PieceKind Kind, ModifierType ModKind) |
| 511 |
: Piece(Kind), ModKind(ModKind) {} |
| 512 |
|
| 513 |
public: |
| 514 |
SelectPiece(ModifierType ModKind) : SelectPiece(SelectPieceClass, ModKind) {} |
| 515 |
|
| 516 |
ModifierType ModKind; |
| 517 |
std::vector<Piece *> Options; |
| 518 |
int Index = 0; |
| 519 |
|
| 520 |
static bool classof(const Piece *P) { |
| 521 |
return P->getPieceClass() == SelectPieceClass || |
| 522 |
P->getPieceClass() == EnumSelectPieceClass || |
| 523 |
P->getPieceClass() == PluralPieceClass; |
| 524 |
} |
| 525 |
}; |
| 526 |
|
| 527 |
struct EnumSelectPiece : SelectPiece { |
| 528 |
EnumSelectPiece() : SelectPiece(EnumSelectPieceClass, MT_EnumSelect) {} |
| 529 |
|
| 530 |
StringRef EnumName; |
| 531 |
std::vector<StringRef> OptionEnumNames; |
| 532 |
|
| 533 |
static bool classof(const Piece *P) { |
| 534 |
return P->getPieceClass() == EnumSelectPieceClass; |
| 535 |
} |
| 536 |
}; |
| 537 |
|
| 538 |
struct EnumValuePiece : Piece { |
| 539 |
ModifierType Kind; |
| 540 |
}; |
| 541 |
|
| 542 |
struct PluralPiece : SelectPiece { |
| 543 |
PluralPiece() : SelectPiece(PluralPieceClass, MT_Plural) {} |
| 544 |
|
| 545 |
std::vector<Piece *> OptionPrefixes; |
| 546 |
int Index = 0; |
| 547 |
|
| 548 |
static bool classof(const Piece *P) { |
| 549 |
return P->getPieceClass() == PluralPieceClass; |
| 550 |
} |
| 551 |
}; |
| 552 |
|
| 553 |
struct DiffPiece : Piece { |
| 554 |
DiffPiece() : Piece(DiffPieceClass) {} |
| 555 |
|
| 556 |
Piece *Parts[4] = {}; |
| 557 |
int Indexes[2] = {}; |
| 558 |
|
| 559 |
static bool classof(const Piece *P) { |
| 560 |
return P->getPieceClass() == DiffPieceClass; |
| 561 |
} |
| 562 |
}; |
| 563 |
|
| 564 |
struct SubstitutionPiece : Piece { |
| 565 |
SubstitutionPiece() : Piece(SubstitutionPieceClass) {} |
| 566 |
|
| 567 |
std::string Name; |
| 568 |
std::vector<int> Modifiers; |
| 569 |
|
| 570 |
static bool classof(const Piece *P) { |
| 571 |
return P->getPieceClass() == SubstitutionPieceClass; |
| 572 |
} |
| 573 |
}; |
| 574 |
|
| 575 |
/// Diagnostic text, parsed into pieces. |
| 576 |
|
| 577 |
|
| 578 |
struct DiagnosticTextBuilder { |
| 579 |
DiagnosticTextBuilder(DiagnosticTextBuilder const &) = delete; |
| 580 |
DiagnosticTextBuilder &operator=(DiagnosticTextBuilder const &) = delete; |
| 581 |
|
| 582 |
DiagnosticTextBuilder(const RecordKeeper &Records) { |
| 583 |
// Build up the list of substitution records. |
| 584 |
for (auto *S : Records.getAllDerivedDefinitions("TextSubstitution")) { |
| 585 |
EvaluatingRecordGuard Guard(&EvaluatingRecord, S); |
| 586 |
Substitutions.try_emplace( |
| 587 |
S->getName(), DiagText(*this, S->getValueAsString("Substitution"))); |
| 588 |
} |
| 589 |
|
| 590 |
// Check that no diagnostic definitions have the same name as a |
| 591 |
// substitution. |
| 592 |
for (const Record *Diag : Records.getAllDerivedDefinitions("Diagnostic")) { |
| 593 |
StringRef Name = Diag->getName(); |
| 594 |
if (Substitutions.count(Name)) |
| 595 |
llvm::PrintFatalError( |
| 596 |
Diag->getLoc(), |
| 597 |
"Diagnostic '" + Name + |
| 598 |
"' has same name as TextSubstitution definition"); |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
std::vector<std::string> buildForDocumentation(StringRef Role, |
| 603 |
const Record *R); |
| 604 |
std::string buildForDefinition(const Record *R); |
| 605 |
llvm::SmallVector<std::pair< |
| 606 |
std::string, llvm::SmallVector<std::pair<unsigned, std::string>>>> |
| 607 |
buildForEnum(const Record *R); |
| 608 |
|
| 609 |
Piece *getSubstitution(SubstitutionPiece *S) const { |
| 610 |
auto It = Substitutions.find(S->Name); |
| 611 |
if (It == Substitutions.end()) |
| 612 |
llvm::PrintFatalError("Failed to find substitution with name: " + |
| 613 |
S->Name); |
| 614 |
return It->second.Root; |
| 615 |
} |
| 616 |
|
| 617 |
[[noreturn]] void PrintFatalError(Twine const &Msg) const { |
| 618 |
assert(EvaluatingRecord && "not evaluating a record?"); |
| 619 |
llvm::PrintFatalError(EvaluatingRecord->getLoc(), Msg); |
| 620 |
} |
| 621 |
|
| 622 |
private: |
| 623 |
struct DiagText { |
| 624 |
DiagnosticTextBuilder &Builder; |
| 625 |
std::vector<Piece *> AllocatedPieces; |
| 626 |
Piece *Root = nullptr; |
| 627 |
|
| 628 |
template <class T, class... Args> T *New(Args &&... args) { |
| 629 |
static_assert(std::is_base_of<Piece, T>::value, "must be piece"); |
| 630 |
T *Mem = new T(std::forward<Args>(args)...); |
| 631 |
AllocatedPieces.push_back(Mem); |
| 632 |
return Mem; |
| 633 |
} |
| 634 |
|
| 635 |
DiagText(DiagnosticTextBuilder &Builder, StringRef Text) |
| 636 |
: Builder(Builder), Root(parseDiagText(Text, StopAt::End)) {} |
| 637 |
|
| 638 |
enum class StopAt { |
| 639 |
// Parse until the end of the string. |
| 640 |
End, |
| 641 |
// Additionally stop if we hit a non-nested '|' or '}'. |
| 642 |
PipeOrCloseBrace, |
| 643 |
// Additionally stop if we hit a non-nested '$'. |
| 644 |
Dollar, |
| 645 |
}; |
| 646 |
|
| 647 |
Piece *parseDiagText(StringRef &Text, StopAt Stop); |
| 648 |
int parseModifier(StringRef &) const; |
| 649 |
|
| 650 |
public: |
| 651 |
DiagText(DiagText &&O) noexcept |
| 652 |
: Builder(O.Builder), AllocatedPieces(std::move(O.AllocatedPieces)), |
| 653 |
Root(O.Root) { |
| 654 |
O.Root = nullptr; |
| 655 |
} |
| 656 |
// The move assignment operator is defined as deleted pending further |
| 657 |
// motivation. |
| 658 |
DiagText &operator=(DiagText &&) = delete; |
| 659 |
|
| 660 |
// The copy constrcutor and copy assignment operator is defined as deleted |
| 661 |
// pending further motivation. |
| 662 |
DiagText(const DiagText &) = delete; |
| 663 |
DiagText &operator=(const DiagText &) = delete; |
| 664 |
|
| 665 |
~DiagText() { |
| 666 |
for (Piece *P : AllocatedPieces) |
| 667 |
delete P; |
| 668 |
} |
| 669 |
}; |
| 670 |
|
| 671 |
private: |
| 672 |
const Record *EvaluatingRecord = nullptr; |
| 673 |
struct EvaluatingRecordGuard { |
| 674 |
EvaluatingRecordGuard(const Record **Dest, const Record *New) |
| 675 |
: Dest(Dest), Old(*Dest) { |
| 676 |
*Dest = New; |
| 677 |
} |
| 678 |
~EvaluatingRecordGuard() { *Dest = Old; } |
| 679 |
const Record **Dest; |
| 680 |
const Record *Old; |
| 681 |
}; |
| 682 |
|
| 683 |
StringMap<DiagText> Substitutions; |
| 684 |
}; |
| 685 |
|
| 686 |
template <class Derived> struct DiagTextVisitor { |
| 687 |
using ModifierMappingsType = std::optional<std::vector<int>>; |
| 688 |
|
| 689 |
private: |
| 690 |
Derived &getDerived() { return static_cast<Derived &>(*this); } |
| 691 |
|
| 692 |
public: |
| 693 |
std::vector<int> |
| 694 |
getSubstitutionMappings(SubstitutionPiece *P, |
| 695 |
const ModifierMappingsType &Mappings) const { |
| 696 |
std::vector<int> NewMappings; |
| 697 |
for (int Idx : P->Modifiers) |
| 698 |
NewMappings.push_back(mapIndex(Idx, Mappings)); |
| 699 |
return NewMappings; |
| 700 |
} |
| 701 |
|
| 702 |
struct SubstitutionContext { |
| 703 |
SubstitutionContext(DiagTextVisitor &Visitor, SubstitutionPiece *P) |
| 704 |
: Visitor(Visitor) { |
| 705 |
Substitution = Visitor.Builder.getSubstitution(P); |
| 706 |
OldMappings = std::move(Visitor.ModifierMappings); |
| 707 |
std::vector<int> NewMappings = |
| 708 |
Visitor.getSubstitutionMappings(P, OldMappings); |
| 709 |
Visitor.ModifierMappings = std::move(NewMappings); |
| 710 |
} |
| 711 |
|
| 712 |
~SubstitutionContext() { |
| 713 |
Visitor.ModifierMappings = std::move(OldMappings); |
| 714 |
} |
| 715 |
|
| 716 |
private: |
| 717 |
DiagTextVisitor &Visitor; |
| 718 |
std::optional<std::vector<int>> OldMappings; |
| 719 |
|
| 720 |
public: |
| 721 |
Piece *Substitution; |
| 722 |
}; |
| 723 |
|
| 724 |
public: |
| 725 |
DiagTextVisitor(DiagnosticTextBuilder &Builder) : Builder(Builder) {} |
| 726 |
|
| 727 |
void Visit(Piece *P) { |
| 728 |
switch (P->getPieceClass()) { |
| 729 |
#define CASE(T) \ |
| 730 |
case T##PieceClass: \ |
| 731 |
return getDerived().Visit##T(static_cast<T##Piece *>(P)) |
| 732 |
CASE(Multi); |
| 733 |
CASE(Text); |
| 734 |
CASE(Placeholder); |
| 735 |
CASE(Select); |
| 736 |
CASE(EnumSelect); |
| 737 |
CASE(Plural); |
| 738 |
CASE(Diff); |
| 739 |
CASE(Substitution); |
| 740 |
#undef CASE |
| 741 |
} |
| 742 |
} |
| 743 |
|
| 744 |
void VisitSubstitution(SubstitutionPiece *P) { |
| 745 |
SubstitutionContext Guard(*this, P); |
| 746 |
Visit(Guard.Substitution); |
| 747 |
} |
| 748 |
|
| 749 |
int mapIndex(int Idx, |
| 750 |
ModifierMappingsType const &ModifierMappings) const { |
| 751 |
if (!ModifierMappings) |
| 752 |
return Idx; |
| 753 |
if (ModifierMappings->size() <= static_cast<unsigned>(Idx)) |
| 754 |
Builder.PrintFatalError("Modifier value '" + std::to_string(Idx) + |
| 755 |
"' is not valid for this mapping (has " + |
| 756 |
std::to_string(ModifierMappings->size()) + |
| 757 |
" mappings)"); |
| 758 |
return (*ModifierMappings)[Idx]; |
| 759 |
} |
| 760 |
|
| 761 |
int mapIndex(int Idx) const { |
| 762 |
return mapIndex(Idx, ModifierMappings); |
| 763 |
} |
| 764 |
|
| 765 |
protected: |
| 766 |
DiagnosticTextBuilder &Builder; |
| 767 |
ModifierMappingsType ModifierMappings; |
| 768 |
}; |
| 769 |
|
| 770 |
void escapeRST(StringRef Str, std::string &Out) { |
| 771 |
for (auto K : Str) { |
| 772 |
if (StringRef("`*|_[]\\").count(K)) |
| 773 |
Out.push_back('\\'); |
| 774 |
Out.push_back(K); |
| 775 |
} |
| 776 |
} |
| 777 |
|
| 778 |
template <typename It> void padToSameLength(It Begin, It End) { |
| 779 |
size_t Width = 0; |
| 780 |
for (It I = Begin; I != End; ++I) |
| 781 |
Width = std::max(Width, I->size()); |
| 782 |
for (It I = Begin; I != End; ++I) |
| 783 |
(*I) += std::string(Width - I->size(), ' '); |
| 784 |
} |
| 785 |
|
| 786 |
template <typename It> void makeTableRows(It Begin, It End) { |
| 787 |
if (Begin == End) |
| 788 |
return; |
| 789 |
padToSameLength(Begin, End); |
| 790 |
for (It I = Begin; I != End; ++I) |
| 791 |
*I = "|" + *I + "|"; |
| 792 |
} |
| 793 |
|
| 794 |
void makeRowSeparator(std::string &Str) { |
| 795 |
for (char &K : Str) |
| 796 |
K = (K == '|' ? '+' : '-'); |
| 797 |
} |
| 798 |
|
| 799 |
struct DiagTextDocPrinter : DiagTextVisitor<DiagTextDocPrinter> { |
| 800 |
using BaseTy = DiagTextVisitor<DiagTextDocPrinter>; |
| 801 |
DiagTextDocPrinter(DiagnosticTextBuilder &Builder, |
| 802 |
std::vector<std::string> &RST) |
| 803 |
: BaseTy(Builder), RST(RST) {} |
| 804 |
|
| 805 |
void gatherNodes( |
| 806 |
Piece *OrigP, const ModifierMappingsType &CurrentMappings, |
| 807 |
std::vector<std::pair<Piece *, ModifierMappingsType>> &Pieces) const { |
| 808 |
if (auto *Sub = dyn_cast<SubstitutionPiece>(OrigP)) { |
| 809 |
ModifierMappingsType NewMappings = |
| 810 |
getSubstitutionMappings(Sub, CurrentMappings); |
| 811 |
return gatherNodes(Builder.getSubstitution(Sub), NewMappings, Pieces); |
| 812 |
} |
| 813 |
if (auto *MD = dyn_cast<MultiPiece>(OrigP)) { |
| 814 |
for (Piece *Node : MD->Pieces) |
| 815 |
gatherNodes(Node, CurrentMappings, Pieces); |
| 816 |
return; |
| 817 |
} |
| 818 |
Pieces.push_back(std::make_pair(OrigP, CurrentMappings)); |
| 819 |
} |
| 820 |
|
| 821 |
void VisitMulti(MultiPiece *P) { |
| 822 |
if (P->Pieces.empty()) { |
| 823 |
RST.push_back(""); |
| 824 |
return; |
| 825 |
} |
| 826 |
|
| 827 |
if (P->Pieces.size() == 1) |
| 828 |
return Visit(P->Pieces[0]); |
| 829 |
|
| 830 |
// Flatten the list of nodes, replacing any substitution pieces with the |
| 831 |
// recursively flattened substituted node. |
| 832 |
std::vector<std::pair<Piece *, ModifierMappingsType>> Pieces; |
| 833 |
gatherNodes(P, ModifierMappings, Pieces); |
| 834 |
|
| 835 |
std::string EmptyLinePrefix; |
| 836 |
size_t Start = RST.size(); |
| 837 |
bool HasMultipleLines = true; |
| 838 |
for (const std::pair<Piece *, ModifierMappingsType> &NodePair : Pieces) { |
| 839 |
std::vector<std::string> Lines; |
| 840 |
DiagTextDocPrinter Visitor{Builder, Lines}; |
| 841 |
Visitor.ModifierMappings = NodePair.second; |
| 842 |
Visitor.Visit(NodePair.first); |
| 843 |
|
| 844 |
if (Lines.empty()) |
| 845 |
continue; |
| 846 |
|
| 847 |
// We need a vertical separator if either this or the previous piece is a |
| 848 |
// multi-line piece, or this is the last piece. |
| 849 |
const char *Separator = (Lines.size() > 1 || HasMultipleLines) ? "|" : ""; |
| 850 |
HasMultipleLines = Lines.size() > 1; |
| 851 |
|
| 852 |
if (Start + Lines.size() > RST.size()) |
| 853 |
RST.resize(Start + Lines.size(), EmptyLinePrefix); |
| 854 |
|
| 855 |
padToSameLength(Lines.begin(), Lines.end()); |
| 856 |
for (size_t I = 0; I != Lines.size(); ++I) |
| 857 |
RST[Start + I] += Separator + Lines[I]; |
| 858 |
std::string Empty(Lines[0].size(), ' '); |
| 859 |
for (size_t I = Start + Lines.size(); I != RST.size(); ++I) |
| 860 |
RST[I] += Separator + Empty; |
| 861 |
EmptyLinePrefix += Separator + Empty; |
| 862 |
} |
| 863 |
for (size_t I = Start; I != RST.size(); ++I) |
| 864 |
RST[I] += "|"; |
| 865 |
EmptyLinePrefix += "|"; |
| 866 |
|
| 867 |
makeRowSeparator(EmptyLinePrefix); |
| 868 |
RST.insert(RST.begin() + Start, EmptyLinePrefix); |
| 869 |
RST.insert(RST.end(), EmptyLinePrefix); |
| 870 |
} |
| 871 |
|
| 872 |
void VisitText(TextPiece *P) { |
| 873 |
RST.push_back(""); |
| 874 |
auto &S = RST.back(); |
| 875 |
|
| 876 |
StringRef T = P->Text; |
| 877 |
while (T.consume_front(" ")) |
| 878 |
RST.back() += " |nbsp| "; |
| 879 |
|
| 880 |
std::string Suffix; |
| 881 |
while (T.consume_back(" ")) |
| 882 |
Suffix += " |nbsp| "; |
| 883 |
|
| 884 |
if (!T.empty()) { |
| 885 |
S += ':'; |
| 886 |
S += P->Role; |
| 887 |
S += ":`"; |
| 888 |
escapeRST(T, S); |
| 889 |
S += '`'; |
| 890 |
} |
| 891 |
|
| 892 |
S += Suffix; |
| 893 |
} |
| 894 |
|
| 895 |
void VisitPlaceholder(PlaceholderPiece *P) { |
| 896 |
RST.push_back(std::string(":placeholder:`") + |
| 897 |
char('A' + mapIndex(P->Index)) + "`"); |
| 898 |
} |
| 899 |
|
| 900 |
void VisitSelect(SelectPiece *P) { |
| 901 |
std::vector<size_t> SeparatorIndexes; |
| 902 |
SeparatorIndexes.push_back(RST.size()); |
| 903 |
RST.emplace_back(); |
| 904 |
for (auto *O : P->Options) { |
| 905 |
Visit(O); |
| 906 |
SeparatorIndexes.push_back(RST.size()); |
| 907 |
RST.emplace_back(); |
| 908 |
} |
| 909 |
|
| 910 |
makeTableRows(RST.begin() + SeparatorIndexes.front(), |
| 911 |
RST.begin() + SeparatorIndexes.back() + 1); |
| 912 |
for (size_t I : SeparatorIndexes) |
| 913 |
makeRowSeparator(RST[I]); |
| 914 |
} |
| 915 |
|
| 916 |
void VisitEnumSelect(EnumSelectPiece *P) { |
| 917 |
// Document this as if it were a 'select', which properly prints all of the |
| 918 |
// options correctly in a readable/reasonable manner. There isn't really |
| 919 |
// anything valuable we could add to readers here. |
| 920 |
VisitSelect(P); |
| 921 |
} |
| 922 |
|
| 923 |
void VisitPlural(PluralPiece *P) { VisitSelect(P); } |
| 924 |
|
| 925 |
void VisitDiff(DiffPiece *P) { |
| 926 |
// Render %diff{a $ b $ c|d}e,f as %select{a %e b %f c|d}. |
| 927 |
PlaceholderPiece E(MT_Placeholder, P->Indexes[0]); |
| 928 |
PlaceholderPiece F(MT_Placeholder, P->Indexes[1]); |
| 929 |
|
| 930 |
MultiPiece FirstOption; |
| 931 |
FirstOption.Pieces.push_back(P->Parts[0]); |
| 932 |
FirstOption.Pieces.push_back(&E); |
| 933 |
FirstOption.Pieces.push_back(P->Parts[1]); |
| 934 |
FirstOption.Pieces.push_back(&F); |
| 935 |
FirstOption.Pieces.push_back(P->Parts[2]); |
| 936 |
|
| 937 |
SelectPiece Select(MT_Diff); |
| 938 |
Select.Options.push_back(&FirstOption); |
| 939 |
Select.Options.push_back(P->Parts[3]); |
| 940 |
|
| 941 |
VisitSelect(&Select); |
| 942 |
} |
| 943 |
|
| 944 |
std::vector<std::string> &RST; |
| 945 |
}; |
| 946 |
|
| 947 |
struct DiagEnumPrinter : DiagTextVisitor<DiagEnumPrinter> { |
| 948 |
public: |
| 949 |
using BaseTy = DiagTextVisitor<DiagEnumPrinter>; |
| 950 |
using EnumeratorItem = std::pair<unsigned, std::string>; |
| 951 |
using EnumeratorList = llvm::SmallVector<EnumeratorItem>; |
| 952 |
using ResultTy = llvm::SmallVector<std::pair<std::string, EnumeratorList>>; |
| 953 |
|
| 954 |
DiagEnumPrinter(DiagnosticTextBuilder &Builder, ResultTy &Result) |
| 955 |
: BaseTy(Builder), Result(Result) {} |
| 956 |
|
| 957 |
ResultTy &Result; |
| 958 |
|
| 959 |
void VisitMulti(MultiPiece *P) { |
| 960 |
for (auto *Child : P->Pieces) |
| 961 |
Visit(Child); |
| 962 |
} |
| 963 |
void VisitText(TextPiece *P) {} |
| 964 |
void VisitPlaceholder(PlaceholderPiece *P) {} |
| 965 |
void VisitDiff(DiffPiece *P) {} |
| 966 |
void VisitSelect(SelectPiece *P) { |
| 967 |
for (auto *D : P->Options) |
| 968 |
Visit(D); |
| 969 |
} |
| 970 |
void VisitPlural(PluralPiece *P) { VisitSelect(P); } |
| 971 |
void VisitEnumSelect(EnumSelectPiece *P) { |
| 972 |
assert(P->Options.size() == P->OptionEnumNames.size()); |
| 973 |
|
| 974 |
if (!P->EnumName.empty()) { |
| 975 |
EnumeratorList List; |
| 976 |
|
| 977 |
for (const auto &Tup : llvm::enumerate(P->OptionEnumNames)) |
| 978 |
if (!Tup.value().empty()) |
| 979 |
List.emplace_back(Tup.index(), Tup.value()); |
| 980 |
|
| 981 |
Result.emplace_back(P->EnumName, List); |
| 982 |
} |
| 983 |
|
| 984 |
VisitSelect(P); |
| 985 |
} |
| 986 |
}; |
| 987 |
|
| 988 |
struct DiagTextPrinter : DiagTextVisitor<DiagTextPrinter> { |
| 989 |
public: |
| 990 |
using BaseTy = DiagTextVisitor<DiagTextPrinter>; |
| 991 |
DiagTextPrinter(DiagnosticTextBuilder &Builder, std::string &Result) |
| 992 |
: BaseTy(Builder), Result(Result) {} |
| 993 |
|
| 994 |
void VisitMulti(MultiPiece *P) { |
| 995 |
for (auto *Child : P->Pieces) |
| 996 |
Visit(Child); |
| 997 |
} |
| 998 |
void VisitText(TextPiece *P) { Result += P->Text; } |
| 999 |
void VisitPlaceholder(PlaceholderPiece *P) { |
| 1000 |
Result += "%"; |
| 1001 |
Result += getModifierName(P->Kind); |
| 1002 |
addInt(mapIndex(P->Index)); |
| 1003 |
} |
| 1004 |
void VisitSelect(SelectPiece *P) { |
| 1005 |
Result += "%"; |
| 1006 |
Result += getModifierName(P->ModKind); |
| 1007 |
if (P->ModKind == MT_Select || P->ModKind == MT_EnumSelect) { |
| 1008 |
Result += "{"; |
| 1009 |
for (auto *D : P->Options) { |
| 1010 |
Visit(D); |
| 1011 |
Result += '|'; |
| 1012 |
} |
| 1013 |
if (!P->Options.empty()) |
| 1014 |
Result.erase(--Result.end()); |
| 1015 |
Result += '}'; |
| 1016 |
} |
| 1017 |
addInt(mapIndex(P->Index)); |
| 1018 |
} |
| 1019 |
|
| 1020 |
void VisitPlural(PluralPiece *P) { |
| 1021 |
Result += "%plural{"; |
| 1022 |
assert(P->Options.size() == P->OptionPrefixes.size()); |
| 1023 |
for (const auto [Prefix, Option] : |
| 1024 |
zip_equal(P->OptionPrefixes, P->Options)) { |
| 1025 |
if (Prefix) |
| 1026 |
Visit(Prefix); |
| 1027 |
Visit(Option); |
| 1028 |
Result += "|"; |
| 1029 |
} |
| 1030 |
if (!P->Options.empty()) |
| 1031 |
Result.erase(--Result.end()); |
| 1032 |
Result += '}'; |
| 1033 |
addInt(mapIndex(P->Index)); |
| 1034 |
} |
| 1035 |
|
| 1036 |
void VisitEnumSelect(EnumSelectPiece *P) { |
| 1037 |
// Print as if we are a 'select', which will result in the compiler just |
| 1038 |
// treating this like a normal select. This way we don't have to do any |
| 1039 |
// special work for the compiler to consume these. |
| 1040 |
VisitSelect(P); |
| 1041 |
} |
| 1042 |
|
| 1043 |
void VisitDiff(DiffPiece *P) { |
| 1044 |
Result += "%diff{"; |
| 1045 |
Visit(P->Parts[0]); |
| 1046 |
Result += "$"; |
| 1047 |
Visit(P->Parts[1]); |
| 1048 |
Result += "$"; |
| 1049 |
Visit(P->Parts[2]); |
| 1050 |
Result += "|"; |
| 1051 |
Visit(P->Parts[3]); |
| 1052 |
Result += "}"; |
| 1053 |
addInt(mapIndex(P->Indexes[0])); |
| 1054 |
Result += ","; |
| 1055 |
addInt(mapIndex(P->Indexes[1])); |
| 1056 |
} |
| 1057 |
|
| 1058 |
void addInt(int Val) { Result += std::to_string(Val); } |
| 1059 |
|
| 1060 |
std::string &Result; |
| 1061 |
}; |
| 1062 |
|
| 1063 |
int DiagnosticTextBuilder::DiagText::parseModifier(StringRef &Text) const { |
| 1064 |
if (Text.empty() || !isdigit(Text[0])) |
| 1065 |
Builder.PrintFatalError("expected modifier in diagnostic"); |
| 1066 |
int Val = 0; |
| 1067 |
do { |
| 1068 |
Val *= 10; |
| 1069 |
Val += Text[0] - '0'; |
| 1070 |
Text = Text.drop_front(); |
| 1071 |
} while (!Text.empty() && isdigit(Text[0])); |
| 1072 |
return Val; |
| 1073 |
} |
| 1074 |
|
| 1075 |
Piece *DiagnosticTextBuilder::DiagText::parseDiagText(StringRef &Text, |
| 1076 |
StopAt Stop) { |
| 1077 |
std::vector<Piece *> Parsed; |
| 1078 |
|
| 1079 |
constexpr StringLiteral StopSets[] = {"%", "%|}", "%|}$"}; |
| 1080 |
StringRef StopSet = StopSets[static_cast<int>(Stop)]; |
| 1081 |
|
| 1082 |
while (!Text.empty()) { |
| 1083 |
size_t End = (size_t)-2; |
| 1084 |
do |
| 1085 |
End = Text.find_first_of(StopSet, End + 2); |
| 1086 |
while ( |
| 1087 |
End < Text.size() - 1 && Text[End] == '%' && |
| 1088 |
(Text[End + 1] == '%' || Text[End + 1] == '|' || Text[End + 1] == '$')); |
| 1089 |
|
| 1090 |
if (End) { |
| 1091 |
Parsed.push_back(New<TextPiece>(Text.slice(0, End), "diagtext")); |
| 1092 |
Text = Text.substr(End); |
| 1093 |
if (Text.empty()) |
| 1094 |
break; |
| 1095 |
} |
| 1096 |
|
| 1097 |
if (Text[0] == '|' || Text[0] == '}' || Text[0] == '$') |
| 1098 |
break; |
| 1099 |
|
| 1100 |
// Drop the '%'. |
| 1101 |
Text = Text.drop_front(); |
| 1102 |
|
| 1103 |
// Extract the (optional) modifier. |
| 1104 |
size_t ModLength = Text.find_first_of("0123456789<{"); |
| 1105 |
StringRef Modifier = Text.slice(0, ModLength); |
| 1106 |
Text = Text.substr(ModLength); |
| 1107 |
ModifierType ModType = StringSwitch<ModifierType>{Modifier} |
| 1108 |
.Case("select", MT_Select) |
| 1109 |
.Case("enum_select", MT_EnumSelect) |
| 1110 |
.Case("sub", MT_Sub) |
| 1111 |
.Case("diff", MT_Diff) |
| 1112 |
.Case("plural", MT_Plural) |
| 1113 |
.Case("s", MT_S) |
| 1114 |
.Case("ordinal", MT_Ordinal) |
| 1115 |
.Case("human", MT_Human) |
| 1116 |
.Case("q", MT_Q) |
| 1117 |
.Case("objcclass", MT_ObjCClass) |
| 1118 |
.Case("objcinstance", MT_ObjCInstance) |
| 1119 |
.Case("quoted", MT_Quoted) |
| 1120 |
.Case("", MT_Placeholder) |
| 1121 |
.Default(MT_Unknown); |
| 1122 |
|
| 1123 |
auto ExpectAndConsume = [&](StringRef Prefix) { |
| 1124 |
if (!Text.consume_front(Prefix)) |
| 1125 |
Builder.PrintFatalError("expected '" + Prefix + "' while parsing %" + |
| 1126 |
Modifier); |
| 1127 |
}; |
| 1128 |
|
| 1129 |
if (ModType != MT_EnumSelect && Text[0] == '<') |
| 1130 |
Builder.PrintFatalError("modifier '<' syntax not valid with %" + |
| 1131 |
Modifier); |
| 1132 |
|
| 1133 |
switch (ModType) { |
| 1134 |
case MT_Unknown: |
| 1135 |
Builder.PrintFatalError("Unknown modifier type: " + Modifier); |
| 1136 |
case MT_Select: { |
| 1137 |
SelectPiece *Select = New<SelectPiece>(MT_Select); |
| 1138 |
do { |
| 1139 |
Text = Text.drop_front(); // '{' or '|' |
| 1140 |
Select->Options.push_back( |
| 1141 |
parseDiagText(Text, StopAt::PipeOrCloseBrace)); |
| 1142 |
assert(!Text.empty() && "malformed %select"); |
| 1143 |
} while (Text.front() == '|'); |
| 1144 |
ExpectAndConsume("}"); |
| 1145 |
Select->Index = parseModifier(Text); |
| 1146 |
Parsed.push_back(Select); |
| 1147 |
continue; |
| 1148 |
} |
| 1149 |
case MT_EnumSelect: { |
| 1150 |
EnumSelectPiece *EnumSelect = New<EnumSelectPiece>(); |
| 1151 |
if (Text[0] != '<') |
| 1152 |
Builder.PrintFatalError("expected '<' after " + Modifier); |
| 1153 |
|
| 1154 |
Text = Text.drop_front(); // Drop '<' |
| 1155 |
size_t EnumNameLen = Text.find_first_of('>'); |
| 1156 |
EnumSelect->EnumName = Text.slice(0, EnumNameLen); |
| 1157 |
Text = Text.substr(EnumNameLen); |
| 1158 |
ExpectAndConsume(">"); |
| 1159 |
|
| 1160 |
if (Text[0] != '{') |
| 1161 |
Builder.PrintFatalError("expected '{' after " + Modifier); |
| 1162 |
|
| 1163 |
do { |
| 1164 |
Text = Text.drop_front(); // '{' or '|' |
| 1165 |
|
| 1166 |
bool BracketsRequired = false; |
| 1167 |
if (Text[0] == '%') { |
| 1168 |
BracketsRequired = true; |
| 1169 |
Text = Text.drop_front(); // '%' |
| 1170 |
size_t OptionNameLen = Text.find_first_of("{"); |
| 1171 |
EnumSelect->OptionEnumNames.push_back(Text.slice(0, OptionNameLen)); |
| 1172 |
Text = Text.substr(OptionNameLen); |
| 1173 |
} else { |
| 1174 |
EnumSelect->OptionEnumNames.push_back({}); |
| 1175 |
} |
| 1176 |
|
| 1177 |
if (BracketsRequired) |
| 1178 |
ExpectAndConsume("{"); |
| 1179 |
else if (Text.front() == '{') { |
| 1180 |
Text = Text.drop_front(); |
| 1181 |
BracketsRequired = true; |
| 1182 |
} |
| 1183 |
|
| 1184 |
EnumSelect->Options.push_back( |
| 1185 |
parseDiagText(Text, StopAt::PipeOrCloseBrace)); |
| 1186 |
|
| 1187 |
if (BracketsRequired) |
| 1188 |
ExpectAndConsume("}"); |
| 1189 |
|
| 1190 |
assert(!Text.empty() && "malformed %select"); |
| 1191 |
} while (Text.front() == '|'); |
| 1192 |
|
| 1193 |
ExpectAndConsume("}"); |
| 1194 |
EnumSelect->Index = parseModifier(Text); |
| 1195 |
Parsed.push_back(EnumSelect); |
| 1196 |
continue; |
| 1197 |
} |
| 1198 |
case MT_Plural: { |
| 1199 |
PluralPiece *Plural = New<PluralPiece>(); |
| 1200 |
do { |
| 1201 |
Text = Text.drop_front(); // '{' or '|' |
| 1202 |
size_t End = Text.find_first_of(':'); |
| 1203 |
if (End == StringRef::npos) |
| 1204 |
Builder.PrintFatalError("expected ':' while parsing %plural"); |
| 1205 |
++End; |
| 1206 |
assert(!Text.empty()); |
| 1207 |
Plural->OptionPrefixes.push_back( |
| 1208 |
New<TextPiece>(Text.slice(0, End), "diagtext")); |
| 1209 |
Text = Text.substr(End); |
| 1210 |
Plural->Options.push_back( |
| 1211 |
parseDiagText(Text, StopAt::PipeOrCloseBrace)); |
| 1212 |
assert(!Text.empty() && "malformed %plural"); |
| 1213 |
} while (Text.front() == '|'); |
| 1214 |
ExpectAndConsume("}"); |
| 1215 |
Plural->Index = parseModifier(Text); |
| 1216 |
Parsed.push_back(Plural); |
| 1217 |
continue; |
| 1218 |
} |
| 1219 |
case MT_Sub: { |
| 1220 |
SubstitutionPiece *Sub = New<SubstitutionPiece>(); |
| 1221 |
ExpectAndConsume("{"); |
| 1222 |
size_t NameSize = Text.find_first_of('}'); |
| 1223 |
assert(NameSize != size_t(-1) && "failed to find the end of the name"); |
| 1224 |
assert(NameSize != 0 && "empty name?"); |
| 1225 |
Sub->Name = Text.substr(0, NameSize).str(); |
| 1226 |
Text = Text.drop_front(NameSize); |
| 1227 |
ExpectAndConsume("}"); |
| 1228 |
if (!Text.empty()) { |
| 1229 |
while (true) { |
| 1230 |
if (!isdigit(Text[0])) |
| 1231 |
break; |
| 1232 |
Sub->Modifiers.push_back(parseModifier(Text)); |
| 1233 |
if (!Text.consume_front(",")) |
| 1234 |
break; |
| 1235 |
assert(!Text.empty() && isdigit(Text[0]) && |
| 1236 |
"expected another modifier"); |
| 1237 |
} |
| 1238 |
} |
| 1239 |
Parsed.push_back(Sub); |
| 1240 |
continue; |
| 1241 |
} |
| 1242 |
case MT_Diff: { |
| 1243 |
DiffPiece *Diff = New<DiffPiece>(); |
| 1244 |
ExpectAndConsume("{"); |
| 1245 |
Diff->Parts[0] = parseDiagText(Text, StopAt::Dollar); |
| 1246 |
ExpectAndConsume("$"); |
| 1247 |
Diff->Parts[1] = parseDiagText(Text, StopAt::Dollar); |
| 1248 |
ExpectAndConsume("$"); |
| 1249 |
Diff->Parts[2] = parseDiagText(Text, StopAt::PipeOrCloseBrace); |
| 1250 |
ExpectAndConsume("|"); |
| 1251 |
Diff->Parts[3] = parseDiagText(Text, StopAt::PipeOrCloseBrace); |
| 1252 |
ExpectAndConsume("}"); |
| 1253 |
Diff->Indexes[0] = parseModifier(Text); |
| 1254 |
ExpectAndConsume(","); |
| 1255 |
Diff->Indexes[1] = parseModifier(Text); |
| 1256 |
Parsed.push_back(Diff); |
| 1257 |
continue; |
| 1258 |
} |
| 1259 |
case MT_S: { |
| 1260 |
SelectPiece *Select = New<SelectPiece>(ModType); |
| 1261 |
Select->Options.push_back(New<TextPiece>("")); |
| 1262 |
Select->Options.push_back(New<TextPiece>("s", "diagtext")); |
| 1263 |
Select->Index = parseModifier(Text); |
| 1264 |
Parsed.push_back(Select); |
| 1265 |
continue; |
| 1266 |
} |
| 1267 |
case MT_Q: |
| 1268 |
case MT_Placeholder: |
| 1269 |
case MT_ObjCClass: |
| 1270 |
case MT_ObjCInstance: |
| 1271 |
case MT_Quoted: |
| 1272 |
case MT_Ordinal: |
| 1273 |
case MT_Human: { |
| 1274 |
Parsed.push_back(New<PlaceholderPiece>(ModType, parseModifier(Text))); |
| 1275 |
continue; |
| 1276 |
} |
| 1277 |
} |
| 1278 |
} |
| 1279 |
|
| 1280 |
return New<MultiPiece>(Parsed); |
| 1281 |
} |
| 1282 |
|
| 1283 |
std::vector<std::string> |
| 1284 |
DiagnosticTextBuilder::buildForDocumentation(StringRef Severity, |
| 1285 |
const Record *R) { |
| 1286 |
EvaluatingRecordGuard Guard(&EvaluatingRecord, R); |
| 1287 |
StringRef Text = R->getValueAsString("Summary"); |
| 1288 |
|
| 1289 |
DiagText D(*this, Text); |
| 1290 |
TextPiece *Prefix = D.New<TextPiece>(Severity, Severity); |
| 1291 |
Prefix->Text += ": "; |
| 1292 |
auto *MP = dyn_cast<MultiPiece>(D.Root); |
| 1293 |
if (!MP) { |
| 1294 |
MP = D.New<MultiPiece>(); |
| 1295 |
MP->Pieces.push_back(D.Root); |
| 1296 |
D.Root = MP; |
| 1297 |
} |
| 1298 |
MP->Pieces.insert(MP->Pieces.begin(), Prefix); |
| 1299 |
std::vector<std::string> Result; |
| 1300 |
DiagTextDocPrinter{*this, Result}.Visit(D.Root); |
| 1301 |
return Result; |
| 1302 |
} |
| 1303 |
|
| 1304 |
DiagEnumPrinter::ResultTy DiagnosticTextBuilder::buildForEnum(const Record *R) { |
| 1305 |
EvaluatingRecordGuard Guard(&EvaluatingRecord, R); |
| 1306 |
StringRef Text = R->getValueAsString("Summary"); |
| 1307 |
DiagText D(*this, Text); |
| 1308 |
DiagEnumPrinter::ResultTy Result; |
| 1309 |
DiagEnumPrinter{*this, Result}.Visit(D.Root); |
| 1310 |
return Result; |
| 1311 |
} |
| 1312 |
|
| 1313 |
std::string DiagnosticTextBuilder::buildForDefinition(const Record *R) { |
| 1314 |
EvaluatingRecordGuard Guard(&EvaluatingRecord, R); |
| 1315 |
StringRef Text = R->getValueAsString("Summary"); |
| 1316 |
DiagText D(*this, Text); |
| 1317 |
std::string Result; |
| 1318 |
DiagTextPrinter{*this, Result}.Visit(D.Root); |
| 1319 |
return Result; |
| 1320 |
} |
| 1321 |
|
| 1322 |
} // namespace |
| 1323 |
|
| 1324 |
//===----------------------------------------------------------------------===// |
| 1325 |
// Warning Tables (.inc file) generation. |
| 1326 |
//===----------------------------------------------------------------------===// |
| 1327 |
|
| 1328 |
static bool isError(const Record &Diag) { |
| 1329 |
return Diag.getValueAsDef("Class")->getName() == "CLASS_ERROR"; |
| 1330 |
} |
| 1331 |
|
| 1332 |
static bool isRemark(const Record &Diag) { |
| 1333 |
return Diag.getValueAsDef("Class")->getName() == "CLASS_REMARK"; |
| 1334 |
} |
| 1335 |
|
| 1336 |
// Presumes the text has been split at the first whitespace or hyphen. |
| 1337 |
static bool isExemptAtStart(StringRef Text) { |
| 1338 |
// Fast path, the first character is lowercase or not alphanumeric. |
| 1339 |
if (Text.empty() || isLower(Text[0]) || !isAlnum(Text[0])) |
| 1340 |
return true; |
| 1341 |
|
| 1342 |
// If the text is all uppercase (or numbers, +, or _), then we assume it's an |
| 1343 |
// acronym and that's allowed. This covers cases like ISO, C23, C++14, and |
| 1344 |
// OBJECT_MODE. However, if there's only a single letter other than "C", we |
| 1345 |
// do not exempt it so that we catch a case like "A really bad idea" while |
| 1346 |
// still allowing a case like "C does not allow...". |
| 1347 |
if (all_of(Text, [](char C) { |
| 1348 |
return isUpper(C) || isDigit(C) || C == '+' || C == '_'; |
| 1349 |
})) |
| 1350 |
return Text.size() > 1 || Text[0] == 'C'; |
| 1351 |
|
| 1352 |
// Otherwise, there are a few other exemptions. |
| 1353 |
return StringSwitch<bool>(Text) |
| 1354 |
.Case("AddressSanitizer", true) |
| 1355 |
.Case("CFString", true) |
| 1356 |
.Case("Clang", true) |
| 1357 |
.Case("Fuchsia", true) |
| 1358 |
.Case("GNUstep", true) |
| 1359 |
.Case("IBOutletCollection", true) |
| 1360 |
.Case("Itanium", true) |
| 1361 |
.Case("Microsoft", true) |
| 1362 |
.Case("Neon", true) |
| 1363 |
.StartsWith("NSInvocation", true) // NSInvocation, NSInvocation's |
| 1364 |
.Case("Objective", true) // Objective-C (hyphen is a word boundary) |
| 1365 |
.Case("OpenACC", true) |
| 1366 |
.Case("OpenCL", true) |
| 1367 |
.Case("OpenMP", true) |
| 1368 |
.Case("Pascal", true) |
| 1369 |
.Case("Swift", true) |
| 1370 |
.Case("Unicode", true) |
| 1371 |
.Case("Vulkan", true) |
| 1372 |
.Case("WebAssembly", true) |
| 1373 |
.Default(false); |
| 1374 |
} |
| 1375 |
|
| 1376 |
// Does not presume the text has been split at all. |
| 1377 |
static bool isExemptAtEnd(StringRef Text) { |
| 1378 |
// Rather than come up with a list of characters that are allowed, we go the |
| 1379 |
// other way and look only for characters that are not allowed. |
| 1380 |
switch (Text.back()) { |
| 1381 |
default: |
| 1382 |
return true; |
| 1383 |
case '?': |
| 1384 |
// Explicitly allowed to support "; did you mean?". |
| 1385 |
return true; |
| 1386 |
case '.': |
| 1387 |
case '!': |
| 1388 |
return false; |
| 1389 |
} |
| 1390 |
} |
| 1391 |
|
| 1392 |
static void verifyDiagnosticWording(const Record &Diag) { |
| 1393 |
StringRef FullDiagText = Diag.getValueAsString("Summary"); |
| 1394 |
|
| 1395 |
auto DiagnoseStart = [&](StringRef Text) { |
| 1396 |
// Verify that the text does not start with a capital letter, except for |
| 1397 |
// special cases that are exempt like ISO and C++. Find the first word |
| 1398 |
// by looking for a word breaking character. |
| 1399 |
char Separators[] = {' ', '-', ',', '}'}; |
| 1400 |
auto Iter = std::find_first_of( |
| 1401 |
Text.begin(), Text.end(), std::begin(Separators), std::end(Separators)); |
| 1402 |
|
| 1403 |
StringRef First = Text.substr(0, Iter - Text.begin()); |
| 1404 |
if (!isExemptAtStart(First)) { |
| 1405 |
PrintError(&Diag, |
| 1406 |
"Diagnostics should not start with a capital letter; '" + |
| 1407 |
First + "' is invalid"); |
| 1408 |
} |
| 1409 |
}; |
| 1410 |
|
| 1411 |
auto DiagnoseEnd = [&](StringRef Text) { |
| 1412 |
// Verify that the text does not end with punctuation like '.' or '!'. |
| 1413 |
if (!isExemptAtEnd(Text)) { |
| 1414 |
PrintError(&Diag, "Diagnostics should not end with punctuation; '" + |
| 1415 |
Text.substr(Text.size() - 1, 1) + "' is invalid"); |
| 1416 |
} |
| 1417 |
}; |
| 1418 |
|
| 1419 |
// If the diagnostic starts with %select, look through it to see whether any |
| 1420 |
// of the options will cause a problem. |
| 1421 |
if (FullDiagText.starts_with("%select{")) { |
| 1422 |
// Do a balanced delimiter scan from the start of the text to find the |
| 1423 |
// closing '}', skipping intermediary {} pairs. |
| 1424 |
|
| 1425 |
size_t BraceCount = 1; |
| 1426 |
constexpr size_t PercentSelectBraceLen = sizeof("%select{") - 1; |
| 1427 |
auto Iter = FullDiagText.begin() + PercentSelectBraceLen; |
| 1428 |
for (auto End = FullDiagText.end(); Iter != End; ++Iter) { |
| 1429 |
char Ch = *Iter; |
| 1430 |
if (Ch == '{') |
| 1431 |
++BraceCount; |
| 1432 |
else if (Ch == '}') |
| 1433 |
--BraceCount; |
| 1434 |
if (!BraceCount) |
| 1435 |
break; |
| 1436 |
} |
| 1437 |
// Defending against a malformed diagnostic string. |
| 1438 |
if (BraceCount != 0) |
| 1439 |
return; |
| 1440 |
|
| 1441 |
StringRef SelectText = |
| 1442 |
FullDiagText.substr(PercentSelectBraceLen, Iter - FullDiagText.begin() - |
| 1443 |
PercentSelectBraceLen); |
| 1444 |
SmallVector<StringRef, 4> SelectPieces; |
| 1445 |
SelectText.split(SelectPieces, '|'); |
| 1446 |
|
| 1447 |
// Walk over all of the individual pieces of select text to see if any of |
| 1448 |
// them start with an invalid character. If any of the select pieces is |
| 1449 |
// empty, we need to look at the first word after the %select to see |
| 1450 |
// whether that is invalid or not. If all of the pieces are fine, then we |
| 1451 |
// don't need to check anything else about the start of the diagnostic. |
| 1452 |
bool CheckSecondWord = false; |
| 1453 |
for (StringRef Piece : SelectPieces) { |
| 1454 |
if (Piece.empty()) |
| 1455 |
CheckSecondWord = true; |
| 1456 |
else |
| 1457 |
DiagnoseStart(Piece); |
| 1458 |
} |
| 1459 |
|
| 1460 |
if (CheckSecondWord) { |
| 1461 |
// There was an empty select piece, so we need to check the second |
| 1462 |
// word. This catches situations like '%select{|fine}0 Not okay'. Add |
| 1463 |
// two to account for the closing curly brace and the number after it. |
| 1464 |
StringRef AfterSelect = |
| 1465 |
FullDiagText.substr(Iter - FullDiagText.begin() + 2).ltrim(); |
| 1466 |
DiagnoseStart(AfterSelect); |
| 1467 |
} |
| 1468 |
} else { |
| 1469 |
// If the start of the diagnostic is not %select, we can check the first |
| 1470 |
// word and be done with it. |
| 1471 |
DiagnoseStart(FullDiagText); |
| 1472 |
} |
| 1473 |
|
| 1474 |
// If the last character in the diagnostic is a number preceded by a }, scan |
| 1475 |
// backwards to see if this is for a %select{...}0. If it is, we need to look |
| 1476 |
// at each piece to see whether it ends in punctuation or not. |
| 1477 |
bool StillNeedToDiagEnd = true; |
| 1478 |
if (isDigit(FullDiagText.back()) && *(FullDiagText.end() - 2) == '}') { |
| 1479 |
// Scan backwards to find the opening curly brace. |
| 1480 |
size_t BraceCount = 1; |
| 1481 |
auto Iter = FullDiagText.end() - sizeof("}0"); |
| 1482 |
for (auto End = FullDiagText.begin(); Iter != End; --Iter) { |
| 1483 |
char Ch = *Iter; |
| 1484 |
if (Ch == '}') |
| 1485 |
++BraceCount; |
| 1486 |
else if (Ch == '{') |
| 1487 |
--BraceCount; |
| 1488 |
if (!BraceCount) |
| 1489 |
break; |
| 1490 |
} |
| 1491 |
// Defending against a malformed diagnostic string. |
| 1492 |
if (BraceCount != 0) |
| 1493 |
return; |
| 1494 |
|
| 1495 |
// Continue the backwards scan to find the word before the '{' to see if it |
| 1496 |
// is 'select'. |
| 1497 |
constexpr size_t SelectLen = sizeof("select") - 1; |
| 1498 |
bool IsSelect = |
| 1499 |
(FullDiagText.substr(Iter - SelectLen - FullDiagText.begin(), |
| 1500 |
SelectLen) == "select"); |
| 1501 |
if (IsSelect) { |
| 1502 |
// Gather the content between the {} for the select in question so we can |
| 1503 |
// split it into pieces. |
| 1504 |
StillNeedToDiagEnd = false; // No longer need to handle the end. |
| 1505 |
StringRef SelectText = |
| 1506 |
FullDiagText.substr(Iter - FullDiagText.begin() + /*{*/ 1, |
| 1507 |
FullDiagText.end() - Iter - /*pos before }0*/ 3); |
| 1508 |
SmallVector<StringRef, 4> SelectPieces; |
| 1509 |
SelectText.split(SelectPieces, '|'); |
| 1510 |
for (StringRef Piece : SelectPieces) { |
| 1511 |
// Not worrying about a situation like: "this is bar. %select{foo|}0". |
| 1512 |
if (!Piece.empty()) |
| 1513 |
DiagnoseEnd(Piece); |
| 1514 |
} |
| 1515 |
} |
| 1516 |
} |
| 1517 |
|
| 1518 |
// If we didn't already cover the diagnostic because of a %select, handle it |
| 1519 |
// now. |
| 1520 |
if (StillNeedToDiagEnd) |
| 1521 |
DiagnoseEnd(FullDiagText); |
| 1522 |
|
| 1523 |
// FIXME: This could also be improved by looking for instances of clang or |
| 1524 |
// gcc in the diagnostic and recommend Clang or GCC instead. However, this |
| 1525 |
// runs into odd situations like [[clang::warn_unused_result]], |
| 1526 |
// #pragma clang, or --unwindlib=libgcc. |
| 1527 |
} |
| 1528 |
|
| 1529 |
/// ClangDiagsCompatIDsEmitter - Emit a set of 'compatibility diagnostic ids' |
| 1530 |
/// that map to a set of 2 regular diagnostic ids each and which are used to |
| 1531 |
/// simplify emitting compatibility warnings. |
| 1532 |
void clang::EmitClangDiagsCompatIDs(const llvm::RecordKeeper &Records, |
| 1533 |
llvm::raw_ostream &OS, |
| 1534 |
const std::string &Component) { |
| 1535 |
ArrayRef<const Record *> Ids = |
| 1536 |
Records.getAllDerivedDefinitions("CompatWarningId"); |
| 1537 |
|
| 1538 |
StringRef PrevComponent = ""; |
| 1539 |
for (auto [I, R] : enumerate(make_pointee_range(Ids))) { |
| 1540 |
StringRef DiagComponent = R.getValueAsString("Component"); |
| 1541 |
if (!Component.empty() && Component != DiagComponent) |
| 1542 |
continue; |
| 1543 |
|
| 1544 |
StringRef CompatDiagName = R.getValueAsString("Name"); |
| 1545 |
StringRef Diag = R.getValueAsString("Diag"); |
| 1546 |
StringRef DiagPre = R.getValueAsString("DiagPre"); |
| 1547 |
int64_t CXXStdVer = R.getValueAsInt("Std"); |
| 1548 |
|
| 1549 |
// We don't want to create empty enums since some compilers (including |
| 1550 |
// Clang) warn about that, so these macros are used to avoid having to |
| 1551 |
// unconditionally write 'enum {' and '};' in the headers. |
| 1552 |
if (PrevComponent != DiagComponent) { |
| 1553 |
if (!PrevComponent.empty()) |
| 1554 |
OS << "DIAG_COMPAT_IDS_END()\n"; |
| 1555 |
OS << "DIAG_COMPAT_IDS_BEGIN()\n"; |
| 1556 |
PrevComponent = DiagComponent; |
| 1557 |
} |
| 1558 |
|
| 1559 |
// FIXME: We sometimes define multiple compat diagnostics with the same |
| 1560 |
// name, e.g. 'constexpr_body_invalid_stmt' exists for C++14/20/23. It would |
| 1561 |
// be nice if we could combine all of them into a single compatibility diag |
| 1562 |
// id. |
| 1563 |
OS << "DIAG_COMPAT_ID(" << I << ","; |
| 1564 |
OS << CompatDiagName << "," << CXXStdVer << "," << Diag << "," << DiagPre; |
| 1565 |
OS << ")\n"; |
| 1566 |
} |
| 1567 |
|
| 1568 |
if (!PrevComponent.empty()) |
| 1569 |
OS << "DIAG_COMPAT_IDS_END()\n"; |
| 1570 |
} |
| 1571 |
|
| 1572 |
/// ClangDiagsIntefaceEmitter - Emit the diagnostics interface header for |
| 1573 |
/// a Clang component. |
| 1574 |
void clang::EmitClangDiagsInterface(llvm::raw_ostream &OS, |
| 1575 |
const std::string &Component) { |
| 1576 |
if (Component.empty()) |
| 1577 |
PrintFatalError("'-gen-clang-diags-iface' requires a component name"); |
| 1578 |
|
| 1579 |
std::string ComponentUpper = StringRef(Component).upper(); |
| 1580 |
const char *Comp = Component.c_str(); |
| 1581 |
const char *Upper = ComponentUpper.c_str(); |
| 1582 |
|
| 1583 |
OS << llvm::format(R"c++( |
| 1584 |
namespace clang { |
| 1585 |
namespace diag { |
| 1586 |
enum { |
| 1587 |
#define DIAG(ENUM, FLAGS, DEFAULT_MAPPING, DESC, GROUP, SFINAE, NOWERROR, \ |
| 1588 |
SHOWINSYSHEADER, SHOWINSYSMACRO, DEFERRABLE, CATEGORY) \ |
| 1589 |
ENUM, |
| 1590 |
#define %sSTART |
| 1591 |
#include "clang/Basic/Diagnostic%sKinds.inc" |
| 1592 |
#undef DIAG |
| 1593 |
NUM_BUILTIN_%s_DIAGNOSTICS |
| 1594 |
}; |
| 1595 |
|
| 1596 |
#define DIAG_ENUM(ENUM_NAME) \ |
| 1597 |
namespace ENUM_NAME { \ |
| 1598 |
enum { |
| 1599 |
#define DIAG_ENUM_ITEM(IDX, NAME) NAME = IDX, |
| 1600 |
#define DIAG_ENUM_END() \ |
| 1601 |
} \ |
| 1602 |
; \ |
| 1603 |
} |
| 1604 |
#include "clang/Basic/Diagnostic%sEnums.inc" |
| 1605 |
#undef DIAG_ENUM_END |
| 1606 |
#undef DIAG_ENUM_ITEM |
| 1607 |
#undef DIAG_ENUM |
| 1608 |
} // end namespace diag |
| 1609 |
|
| 1610 |
namespace diag_compat { |
| 1611 |
#define DIAG_COMPAT_IDS_BEGIN() enum { |
| 1612 |
#define DIAG_COMPAT_IDS_END() \ |
| 1613 |
} \ |
| 1614 |
; |
| 1615 |
#define DIAG_COMPAT_ID(IDX, NAME, ...) NAME = IDX, |
| 1616 |
#include "clang/Basic/Diagnostic%sCompatIDs.inc" |
| 1617 |
#undef DIAG_COMPAT_ID |
| 1618 |
#undef DIAG_COMPAT_IDS_BEGIN |
| 1619 |
#undef DIAG_COMPAT_IDS_END |
| 1620 |
} // end namespace diag_compat |
| 1621 |
} // end namespace clang |
| 1622 |
)c++", |
| 1623 |
Upper, Comp, Upper, Comp, Comp); |
| 1624 |
} |
| 1625 |
|
| 1626 |
/// ClangDiagsEnumsEmitter - The top-level class emits .def files containing |
| 1627 |
/// declarations of Clang diagnostic enums for selects. |
| 1628 |
void clang::EmitClangDiagsEnums(const RecordKeeper &Records, raw_ostream &OS, |
| 1629 |
const std::string &Component) { |
| 1630 |
DiagnosticTextBuilder DiagTextBuilder(Records); |
| 1631 |
ArrayRef<const Record *> Diags = |
| 1632 |
Records.getAllDerivedDefinitions("Diagnostic"); |
| 1633 |
|
| 1634 |
llvm::SmallVector<std::pair<const Record *, std::string>> EnumerationNames; |
| 1635 |
|
| 1636 |
for (const Record &R : make_pointee_range(Diags)) { |
| 1637 |
DiagEnumPrinter::ResultTy Enums = DiagTextBuilder.buildForEnum(&R); |
| 1638 |
|
| 1639 |
for (auto &Enumeration : Enums) { |
| 1640 |
bool ShouldPrint = |
| 1641 |
Component.empty() || Component == R.getValueAsString("Component"); |
| 1642 |
|
| 1643 |
auto PreviousByName = llvm::find_if(EnumerationNames, [&](auto &Prev) { |
| 1644 |
return Prev.second == Enumeration.first; |
| 1645 |
}); |
| 1646 |
|
| 1647 |
if (PreviousByName != EnumerationNames.end()) { |
| 1648 |
PrintError(&R, |
| 1649 |
"Duplicate enumeration name '" + Enumeration.first + "'"); |
| 1650 |
PrintNote(PreviousByName->first->getLoc(), |
| 1651 |
"Previous diagnostic is here"); |
| 1652 |
} |
| 1653 |
|
| 1654 |
EnumerationNames.emplace_back(&R, Enumeration.first); |
| 1655 |
|
| 1656 |
if (ShouldPrint) |
| 1657 |
OS << "DIAG_ENUM(" << Enumeration.first << ")\n"; |
| 1658 |
|
| 1659 |
llvm::SmallVector<std::string> EnumeratorNames; |
| 1660 |
for (auto &Enumerator : Enumeration.second) { |
| 1661 |
if (llvm::is_contained(EnumeratorNames, Enumerator.second)) |
| 1662 |
PrintError(&R, |
| 1663 |
"Duplicate enumerator name '" + Enumerator.second + "'"); |
| 1664 |
EnumeratorNames.push_back(Enumerator.second); |
| 1665 |
|
| 1666 |
if (ShouldPrint) |
| 1667 |
OS << "DIAG_ENUM_ITEM(" << Enumerator.first << ", " |
| 1668 |
<< Enumerator.second << ")\n"; |
| 1669 |
} |
| 1670 |
if (ShouldPrint) |
| 1671 |
OS << "DIAG_ENUM_END()\n"; |
| 1672 |
} |
| 1673 |
} |
| 1674 |
} |
| 1675 |
|
| 1676 |
/// ClangDiagsDefsEmitter - The top-level class emits .def files containing |
| 1677 |
/// declarations of Clang diagnostics. |
| 1678 |
void clang::EmitClangDiagsDefs(const RecordKeeper &Records, raw_ostream &OS, |
| 1679 |
const std::string &Component) { |
| 1680 |
// Write the #if guard |
| 1681 |
if (!Component.empty()) { |
| 1682 |
std::string ComponentName = StringRef(Component).upper(); |
| 1683 |
OS << "#ifdef " << ComponentName << "START\n"; |
| 1684 |
OS << "__" << ComponentName << "START = DIAG_START_" << ComponentName |
| 1685 |
<< ",\n"; |
| 1686 |
OS << "#undef " << ComponentName << "START\n"; |
| 1687 |
OS << "#endif\n\n"; |
| 1688 |
} |
| 1689 |
|
| 1690 |
DiagnosticTextBuilder DiagTextBuilder(Records); |
| 1691 |
|
| 1692 |
ArrayRef<const Record *> Diags = |
| 1693 |
Records.getAllDerivedDefinitions("Diagnostic"); |
| 1694 |
|
| 1695 |
ArrayRef<const Record *> DiagGroups = |
| 1696 |
Records.getAllDerivedDefinitions("DiagGroup"); |
| 1697 |
|
| 1698 |
DiagsInGroupTy DiagsInGroup; |
| 1699 |
groupDiagnostics(Diags, DiagGroups, DiagsInGroup); |
| 1700 |
|
| 1701 |
DiagCategoryIDMap CategoryIDs(Records); |
| 1702 |
DiagGroupParentMap DGParentMap(Records); |
| 1703 |
|
| 1704 |
// Compute the set of diagnostics that are in -Wpedantic. |
| 1705 |
RecordSet DiagsInPedantic; |
| 1706 |
InferPedantic inferPedantic(DGParentMap, Diags, DiagGroups, DiagsInGroup); |
| 1707 |
inferPedantic.compute(&DiagsInPedantic, (RecordVec*)nullptr); |
| 1708 |
|
| 1709 |
for (const Record &R : make_pointee_range(Diags)) { |
| 1710 |
// Check if this is an error that is accidentally in a warning |
| 1711 |
// group. |
| 1712 |
if (isError(R)) { |
| 1713 |
if (const auto *Group = dyn_cast<DefInit>(R.getValueInit("Group"))) { |
| 1714 |
const Record *GroupRec = Group->getDef(); |
| 1715 |
StringRef GroupName = GroupRec->getValueAsString("GroupName"); |
| 1716 |
PrintFatalError(R.getLoc(), "Error " + R.getName() + |
| 1717 |
" cannot be in a warning group [" + GroupName + "]"); |
| 1718 |
} |
| 1719 |
} |
| 1720 |
|
| 1721 |
// Check that all remarks have an associated diagnostic group. |
| 1722 |
if (isRemark(R)) { |
| 1723 |
if (!isa<DefInit>(R.getValueInit("Group"))) { |
| 1724 |
PrintFatalError(R.getLoc(), "Error " + R.getName() + |
| 1725 |
" not in any diagnostic group"); |
| 1726 |
} |
| 1727 |
} |
| 1728 |
|
| 1729 |
// Filter by component. |
| 1730 |
if (!Component.empty() && Component != R.getValueAsString("Component")) |
| 1731 |
continue; |
| 1732 |
|
| 1733 |
// Validate diagnostic wording for common issues. |
| 1734 |
verifyDiagnosticWording(R); |
| 1735 |
|
| 1736 |
OS << "DIAG(" << R.getName() << ", "; |
| 1737 |
OS << R.getValueAsDef("Class")->getName(); |
| 1738 |
OS << ", (unsigned)diag::Severity::" |
| 1739 |
<< R.getValueAsDef("DefaultSeverity")->getValueAsString("Name"); |
| 1740 |
|
| 1741 |
// Description string. |
| 1742 |
OS << ", \""; |
| 1743 |
OS.write_escaped(DiagTextBuilder.buildForDefinition(&R)) << '"'; |
| 1744 |
|
| 1745 |
// Warning group associated with the diagnostic. This is stored as an index |
| 1746 |
// into the alphabetically sorted warning group table. |
| 1747 |
if (const auto *DI = dyn_cast<DefInit>(R.getValueInit("Group"))) { |
| 1748 |
auto I = DiagsInGroup.find(DI->getDef()->getValueAsString("GroupName")); |
| 1749 |
assert(I != DiagsInGroup.end()); |
| 1750 |
OS << ", " << I->second.IDNo; |
| 1751 |
} else if (DiagsInPedantic.count(&R)) { |
| 1752 |
auto I = DiagsInGroup.find("pedantic"); |
| 1753 |
assert(I != DiagsInGroup.end() && "pedantic group not defined"); |
| 1754 |
OS << ", " << I->second.IDNo; |
| 1755 |
} else { |
| 1756 |
OS << ", 0"; |
| 1757 |
} |
| 1758 |
|
| 1759 |
// SFINAE response. |
| 1760 |
OS << ", " << R.getValueAsDef("SFINAE")->getName(); |
| 1761 |
|
| 1762 |
// Default warning has no Werror bit. |
| 1763 |
if (R.getValueAsBit("WarningNoWerror")) |
| 1764 |
OS << ", true"; |
| 1765 |
else |
| 1766 |
OS << ", false"; |
| 1767 |
|
| 1768 |
if (R.getValueAsBit("ShowInSystemHeader")) |
| 1769 |
OS << ", true"; |
| 1770 |
else |
| 1771 |
OS << ", false"; |
| 1772 |
|
| 1773 |
if (R.getValueAsBit("ShowInSystemMacro")) |
| 1774 |
OS << ", true"; |
| 1775 |
else |
| 1776 |
OS << ", false"; |
| 1777 |
|
| 1778 |
if (R.getValueAsBit("Deferrable")) |
| 1779 |
OS << ", true"; |
| 1780 |
else |
| 1781 |
OS << ", false"; |
| 1782 |
|
| 1783 |
// Category number. |
| 1784 |
OS << ", " << CategoryIDs.getID(getDiagnosticCategory(&R, DGParentMap)); |
| 1785 |
OS << ")\n"; |
| 1786 |
} |
| 1787 |
} |
| 1788 |
|
| 1789 |
//===----------------------------------------------------------------------===// |
| 1790 |
// Warning Group Tables generation |
| 1791 |
//===----------------------------------------------------------------------===// |
| 1792 |
|
| 1793 |
static std::string getDiagCategoryEnum(StringRef name) { |
| 1794 |
if (name.empty()) |
| 1795 |
return "DiagCat_None"; |
| 1796 |
SmallString<256> enumName = StringRef("DiagCat_"); |
| 1797 |
for (char C : name) |
| 1798 |
enumName += isalnum(C) ? C : '_'; |
| 1799 |
return std::string(enumName); |
| 1800 |
} |
| 1801 |
|
| 1802 |
/// Emit the array of diagnostic subgroups. |
| 1803 |
/// |
| 1804 |
/// The array of diagnostic subgroups contains for each group a list of its |
| 1805 |
/// subgroups. The individual lists are separated by '-1'. Groups with no |
| 1806 |
/// subgroups are skipped. |
| 1807 |
/// |
| 1808 |
/// \code |
| 1809 |
/// static const int16_t DiagSubGroups[] = { |
| 1810 |
/// /* Empty */ -1, |
| 1811 |
/// /* DiagSubGroup0 */ 142, -1, |
| 1812 |
/// /* DiagSubGroup13 */ 265, 322, 399, -1 |
| 1813 |
/// } |
| 1814 |
/// \endcode |
| 1815 |
/// |
| 1816 |
static void emitDiagSubGroups(DiagsInGroupTy &DiagsInGroup, |
| 1817 |
RecordVec &GroupsInPedantic, raw_ostream &OS) { |
| 1818 |
OS << "static const int16_t DiagSubGroups[] = {\n" |
| 1819 |
<< " /* Empty */ -1,\n"; |
| 1820 |
for (auto const &[Name, Group] : DiagsInGroup) { |
| 1821 |
const bool IsPedantic = Name == "pedantic"; |
| 1822 |
const std::vector<StringRef> &SubGroups = Group.SubGroups; |
| 1823 |
if (!SubGroups.empty() || (IsPedantic && !GroupsInPedantic.empty())) { |
| 1824 |
OS << " /* DiagSubGroup" << Group.IDNo << " */ "; |
| 1825 |
for (StringRef SubGroup : SubGroups) { |
| 1826 |
auto RI = DiagsInGroup.find(SubGroup); |
| 1827 |
assert(RI != DiagsInGroup.end() && "Referenced without existing?"); |
| 1828 |
OS << RI->second.IDNo << ", "; |
| 1829 |
} |
| 1830 |
// Emit the groups implicitly in "pedantic". |
| 1831 |
if (IsPedantic) { |
| 1832 |
for (auto const &Group : GroupsInPedantic) { |
| 1833 |
StringRef GroupName = Group->getValueAsString("GroupName"); |
| 1834 |
auto RI = DiagsInGroup.find(GroupName); |
| 1835 |
assert(RI != DiagsInGroup.end() && "Referenced without existing?"); |
| 1836 |
OS << RI->second.IDNo << ", "; |
| 1837 |
} |
| 1838 |
} |
| 1839 |
|
| 1840 |
OS << "-1,\n"; |
| 1841 |
} |
| 1842 |
} |
| 1843 |
OS << "};\n\n"; |
| 1844 |
} |
| 1845 |
|
| 1846 |
/// Emit the list of diagnostic arrays. |
| 1847 |
/// |
| 1848 |
/// This data structure is a large array that contains itself arrays of varying |
| 1849 |
/// size. Each array represents a list of diagnostics. The different arrays are |
| 1850 |
/// separated by the value '-1'. |
| 1851 |
/// |
| 1852 |
/// \code |
| 1853 |
/// static const int16_t DiagArrays[] = { |
| 1854 |
/// /* Empty */ -1, |
| 1855 |
/// /* DiagArray1 */ diag::warn_pragma_message, |
| 1856 |
/// -1, |
| 1857 |
/// /* DiagArray2 */ diag::warn_abs_too_small, |
| 1858 |
/// diag::warn_unsigned_abs, |
| 1859 |
/// diag::warn_wrong_absolute_value_type, |
| 1860 |
/// -1 |
| 1861 |
/// }; |
| 1862 |
/// \endcode |
| 1863 |
/// |
| 1864 |
static void emitDiagArrays(DiagsInGroupTy &DiagsInGroup, |
| 1865 |
RecordVec &DiagsInPedantic, raw_ostream &OS) { |
| 1866 |
OS << "static const int16_t DiagArrays[] = {\n" |
| 1867 |
<< " /* Empty */ -1,\n"; |
| 1868 |
for (const auto &[Name, Group] : DiagsInGroup) { |
| 1869 |
const bool IsPedantic = Name == "pedantic"; |
| 1870 |
|
| 1871 |
const std::vector<const Record *> &V = Group.DiagsInGroup; |
| 1872 |
if (!V.empty() || (IsPedantic && !DiagsInPedantic.empty())) { |
| 1873 |
OS << " /* DiagArray" << Group.IDNo << " */ "; |
| 1874 |
for (auto *Record : V) |
| 1875 |
OS << "diag::" << Record->getName() << ", "; |
| 1876 |
// Emit the diagnostics implicitly in "pedantic". |
| 1877 |
if (IsPedantic) { |
| 1878 |
for (auto const &Diag : DiagsInPedantic) |
| 1879 |
OS << "diag::" << Diag->getName() << ", "; |
| 1880 |
} |
| 1881 |
OS << "-1,\n"; |
| 1882 |
} |
| 1883 |
} |
| 1884 |
OS << "};\n\n"; |
| 1885 |
} |
| 1886 |
|
| 1887 |
/// Emit a list of group names. |
| 1888 |
/// |
| 1889 |
/// This creates an `llvm::StringTable` of all the diagnostic group names. |
| 1890 |
static void emitDiagGroupNames(const StringToOffsetTable &GroupNames, |
| 1891 |
raw_ostream &OS) { |
| 1892 |
GroupNames.EmitStringTableDef(OS, "DiagGroupNames"); |
| 1893 |
OS << "\n"; |
| 1894 |
} |
| 1895 |
|
| 1896 |
/// Emit diagnostic arrays and related data structures. |
| 1897 |
/// |
| 1898 |
/// This creates the actual diagnostic array, an array of diagnostic subgroups |
| 1899 |
/// and an array of subgroup names. |
| 1900 |
/// |
| 1901 |
/// \code |
| 1902 |
/// #ifdef GET_DIAG_ARRAYS |
| 1903 |
/// static const int16_t DiagArrays[]; |
| 1904 |
/// static const int16_t DiagSubGroups[]; |
| 1905 |
/// static constexpr llvm::StringTable DiagGroupNames; |
| 1906 |
/// #endif |
| 1907 |
/// \endcode |
| 1908 |
static void emitAllDiagArrays(DiagsInGroupTy &DiagsInGroup, |
| 1909 |
RecordVec &DiagsInPedantic, |
| 1910 |
RecordVec &GroupsInPedantic, |
| 1911 |
const StringToOffsetTable &GroupNames, |
| 1912 |
raw_ostream &OS) { |
| 1913 |
OS << "\n#ifdef GET_DIAG_ARRAYS\n"; |
| 1914 |
emitDiagArrays(DiagsInGroup, DiagsInPedantic, OS); |
| 1915 |
emitDiagSubGroups(DiagsInGroup, GroupsInPedantic, OS); |
| 1916 |
emitDiagGroupNames(GroupNames, OS); |
| 1917 |
OS << "#endif // GET_DIAG_ARRAYS\n\n"; |
| 1918 |
} |
| 1919 |
|
| 1920 |
/// Emit diagnostic table. |
| 1921 |
/// |
| 1922 |
/// The table is sorted by the name of the diagnostic group. Each element |
| 1923 |
/// consists of the name of the diagnostic group (given as offset in the |
| 1924 |
/// group name table), a reference to a list of diagnostics (optional) and a |
| 1925 |
/// reference to a set of subgroups (optional). |
| 1926 |
/// |
| 1927 |
/// \code |
| 1928 |
/// #ifdef GET_DIAG_TABLE |
| 1929 |
/// {/* abi */ 159, /* DiagArray11 */ 19, /* Empty */ 0}, |
| 1930 |
/// {/* aggregate-return */ 180, /* Empty */ 0, /* Empty */ 0}, |
| 1931 |
/// {/* all */ 197, /* Empty */ 0, /* DiagSubGroup13 */ 3}, |
| 1932 |
/// {/* deprecated */ 1981,/* DiagArray1 */ 348, /* DiagSubGroup3 */ 9}, |
| 1933 |
/// #endif |
| 1934 |
/// \endcode |
| 1935 |
static void emitDiagTable(DiagsInGroupTy &DiagsInGroup, |
| 1936 |
RecordVec &DiagsInPedantic, |
| 1937 |
RecordVec &GroupsInPedantic, |
| 1938 |
const StringToOffsetTable &GroupNames, |
| 1939 |
raw_ostream &OS) { |
| 1940 |
unsigned MaxLen = 0; |
| 1941 |
|
| 1942 |
for (auto const &I: DiagsInGroup) |
| 1943 |
MaxLen = std::max(MaxLen, (unsigned)I.first.size()); |
| 1944 |
|
| 1945 |
OS << "\n#ifdef DIAG_ENTRY\n"; |
| 1946 |
unsigned SubGroupIndex = 1, DiagArrayIndex = 1; |
| 1947 |
for (auto const &[Name, GroupInfo] : DiagsInGroup) { |
| 1948 |
// Group option string. |
| 1949 |
OS << "DIAG_ENTRY("; |
| 1950 |
OS << GroupInfo.GroupName << " /* "; |
| 1951 |
|
| 1952 |
if (Name.find_first_not_of("abcdefghijklmnopqrstuvwxyz" |
| 1953 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 1954 |
"0123456789!@#$%^*-+=:?") != std::string::npos) |
| 1955 |
PrintFatalError("Invalid character in diagnostic group '" + Name + "'"); |
| 1956 |
OS << Name << " */, "; |
| 1957 |
OS << *GroupNames.GetStringOffset(Name) << ", "; |
| 1958 |
|
| 1959 |
// Special handling for 'pedantic'. |
| 1960 |
const bool IsPedantic = Name == "pedantic"; |
| 1961 |
|
| 1962 |
// Diagnostics in the group. |
| 1963 |
const std::vector<const Record *> &V = GroupInfo.DiagsInGroup; |
| 1964 |
const bool hasDiags = |
| 1965 |
!V.empty() || (IsPedantic && !DiagsInPedantic.empty()); |
| 1966 |
if (hasDiags) { |
| 1967 |
OS << "/* DiagArray" << GroupInfo.IDNo << " */ " << DiagArrayIndex |
| 1968 |
<< ", "; |
| 1969 |
if (IsPedantic) |
| 1970 |
DiagArrayIndex += DiagsInPedantic.size(); |
| 1971 |
DiagArrayIndex += V.size() + 1; |
| 1972 |
} else { |
| 1973 |
OS << "0, "; |
| 1974 |
} |
| 1975 |
|
| 1976 |
// Subgroups. |
| 1977 |
const std::vector<StringRef> &SubGroups = GroupInfo.SubGroups; |
| 1978 |
const bool hasSubGroups = |
| 1979 |
!SubGroups.empty() || (IsPedantic && !GroupsInPedantic.empty()); |
| 1980 |
if (hasSubGroups) { |
| 1981 |
OS << "/* DiagSubGroup" << GroupInfo.IDNo << " */ " << SubGroupIndex |
| 1982 |
<< ", "; |
| 1983 |
if (IsPedantic) |
| 1984 |
SubGroupIndex += GroupsInPedantic.size(); |
| 1985 |
SubGroupIndex += SubGroups.size() + 1; |
| 1986 |
} else { |
| 1987 |
OS << "0, "; |
| 1988 |
} |
| 1989 |
|
| 1990 |
std::string Documentation = GroupInfo.Defs.back() |
| 1991 |
->getValue("Documentation") |
| 1992 |
->getValue() |
| 1993 |
->getAsUnquotedString(); |
| 1994 |
|
| 1995 |
OS << "R\"(" << StringRef(Documentation).trim() << ")\""; |
| 1996 |
|
| 1997 |
OS << ")\n"; |
| 1998 |
} |
| 1999 |
OS << "#endif // DIAG_ENTRY\n\n"; |
| 2000 |
} |
| 2001 |
|
| 2002 |
/// Emit the table of diagnostic categories. |
| 2003 |
/// |
| 2004 |
/// The table has the form of macro calls that have two parameters. The |
| 2005 |
/// category's name as well as an enum that represents the category. The |
| 2006 |
/// table can be used by defining the macro 'CATEGORY' and including this |
| 2007 |
/// table right after. |
| 2008 |
/// |
| 2009 |
/// \code |
| 2010 |
/// #ifdef GET_CATEGORY_TABLE |
| 2011 |
/// CATEGORY("Semantic Issue", DiagCat_Semantic_Issue) |
| 2012 |
/// CATEGORY("Lambda Issue", DiagCat_Lambda_Issue) |
| 2013 |
/// #endif |
| 2014 |
/// \endcode |
| 2015 |
static void emitCategoryTable(const RecordKeeper &Records, raw_ostream &OS) { |
| 2016 |
DiagCategoryIDMap CategoriesByID(Records); |
| 2017 |
OS << "\n#ifdef GET_CATEGORY_TABLE\n"; |
| 2018 |
for (auto const &C : CategoriesByID) |
| 2019 |
OS << "CATEGORY(\"" << C << "\", " << getDiagCategoryEnum(C) << ")\n"; |
| 2020 |
OS << "#endif // GET_CATEGORY_TABLE\n\n"; |
| 2021 |
} |
| 2022 |
|
| 2023 |
void clang::EmitClangDiagGroups(const RecordKeeper &Records, raw_ostream &OS) { |
| 2024 |
// Compute a mapping from a DiagGroup to all of its parents. |
| 2025 |
DiagGroupParentMap DGParentMap(Records); |
| 2026 |
|
| 2027 |
ArrayRef<const Record *> Diags = |
| 2028 |
Records.getAllDerivedDefinitions("Diagnostic"); |
| 2029 |
|
| 2030 |
ArrayRef<const Record *> DiagGroups = |
| 2031 |
Records.getAllDerivedDefinitions("DiagGroup"); |
| 2032 |
|
| 2033 |
DiagsInGroupTy DiagsInGroup; |
| 2034 |
groupDiagnostics(Diags, DiagGroups, DiagsInGroup); |
| 2035 |
|
| 2036 |
// All extensions are implicitly in the "pedantic" group. Record the |
| 2037 |
// implicit set of groups in the "pedantic" group, and use this information |
| 2038 |
// later when emitting the group information for Pedantic. |
| 2039 |
RecordVec DiagsInPedantic; |
| 2040 |
RecordVec GroupsInPedantic; |
| 2041 |
InferPedantic inferPedantic(DGParentMap, Diags, DiagGroups, DiagsInGroup); |
| 2042 |
inferPedantic.compute(&DiagsInPedantic, &GroupsInPedantic); |
| 2043 |
|
| 2044 |
StringToOffsetTable GroupNames; |
| 2045 |
for (const auto &[Name, Group] : DiagsInGroup) { |
| 2046 |
GroupNames.GetOrAddStringOffset(Name); |
| 2047 |
} |
| 2048 |
|
| 2049 |
emitAllDiagArrays(DiagsInGroup, DiagsInPedantic, GroupsInPedantic, GroupNames, |
| 2050 |
OS); |
| 2051 |
emitDiagTable(DiagsInGroup, DiagsInPedantic, GroupsInPedantic, GroupNames, |
| 2052 |
OS); |
| 2053 |
emitCategoryTable(Records, OS); |
| 2054 |
} |
| 2055 |
|
| 2056 |
//===----------------------------------------------------------------------===// |
| 2057 |
// Diagnostic name index generation |
| 2058 |
//===----------------------------------------------------------------------===// |
| 2059 |
|
| 2060 |
void clang::EmitClangDiagsIndexName(const RecordKeeper &Records, |
| 2061 |
raw_ostream &OS) { |
| 2062 |
std::vector<const Record *> Diags = |
| 2063 |
Records.getAllDerivedDefinitions("Diagnostic"); |
| 2064 |
|
| 2065 |
sort(Diags, [](const Record *LHS, const Record *RHS) { |
| 2066 |
return LHS->getName() < RHS->getName(); |
| 2067 |
}); |
| 2068 |
|
| 2069 |
for (const Record *Elem : Diags) |
| 2070 |
OS << "DIAG_NAME_INDEX(" << Elem->getName() << ")\n"; |
| 2071 |
} |
| 2072 |
|
| 2073 |
//===----------------------------------------------------------------------===// |
| 2074 |
// Diagnostic documentation generation |
| 2075 |
//===----------------------------------------------------------------------===// |
| 2076 |
|
| 2077 |
namespace docs { |
| 2078 |
namespace { |
| 2079 |
|
| 2080 |
bool isRemarkGroup(const Record *DiagGroup, |
| 2081 |
const DiagsInGroupTy &DiagsInGroup) { |
| 2082 |
bool AnyRemarks = false, AnyNonRemarks = false; |
| 2083 |
|
| 2084 |
std::function<void(StringRef)> Visit = [&](StringRef GroupName) { |
| 2085 |
auto &GroupInfo = DiagsInGroup.find(GroupName)->second; |
| 2086 |
for (const Record *Diag : GroupInfo.DiagsInGroup) |
| 2087 |
(isRemark(*Diag) ? AnyRemarks : AnyNonRemarks) = true; |
| 2088 |
for (StringRef Name : GroupInfo.SubGroups) |
| 2089 |
Visit(Name); |
| 2090 |
}; |
| 2091 |
Visit(DiagGroup->getValueAsString("GroupName")); |
| 2092 |
|
| 2093 |
if (AnyRemarks && AnyNonRemarks) |
| 2094 |
PrintFatalError( |
| 2095 |
DiagGroup->getLoc(), |
| 2096 |
"Diagnostic group contains both remark and non-remark diagnostics"); |
| 2097 |
return AnyRemarks; |
| 2098 |
} |
| 2099 |
|
| 2100 |
std::string getDefaultSeverity(const Record *Diag) { |
| 2101 |
return std::string( |
| 2102 |
Diag->getValueAsDef("DefaultSeverity")->getValueAsString("Name")); |
| 2103 |
} |
| 2104 |
|
| 2105 |
std::set<std::string> getDefaultSeverities(const Record *DiagGroup, |
| 2106 |
const DiagsInGroupTy &DiagsInGroup) { |
| 2107 |
std::set<std::string> States; |
| 2108 |
|
| 2109 |
std::function<void(StringRef)> Visit = [&](StringRef GroupName) { |
| 2110 |
auto &GroupInfo = DiagsInGroup.find(GroupName)->second; |
| 2111 |
for (const Record *Diag : GroupInfo.DiagsInGroup) |
| 2112 |
States.insert(getDefaultSeverity(Diag)); |
| 2113 |
for (const auto &Name : GroupInfo.SubGroups) |
| 2114 |
Visit(Name); |
| 2115 |
}; |
| 2116 |
Visit(DiagGroup->getValueAsString("GroupName")); |
| 2117 |
return States; |
| 2118 |
} |
| 2119 |
|
| 2120 |
void writeHeader(StringRef Str, raw_ostream &OS, char Kind = '-') { |
| 2121 |
OS << Str << "\n" << std::string(Str.size(), Kind) << "\n"; |
| 2122 |
} |
| 2123 |
|
| 2124 |
void writeDiagnosticText(DiagnosticTextBuilder &Builder, const Record *R, |
| 2125 |
StringRef Role, raw_ostream &OS) { |
| 2126 |
StringRef Text = R->getValueAsString("Summary"); |
| 2127 |
if (Text == "%0") |
| 2128 |
OS << "The text of this diagnostic is not controlled by Clang.\n\n"; |
| 2129 |
else { |
| 2130 |
std::vector<std::string> Out = Builder.buildForDocumentation(Role, R); |
| 2131 |
for (auto &Line : Out) |
| 2132 |
OS << Line << "\n"; |
| 2133 |
OS << "\n"; |
| 2134 |
} |
| 2135 |
} |
| 2136 |
|
| 2137 |
} // namespace |
| 2138 |
} // namespace docs |
| 2139 |
|
| 2140 |
void clang::EmitClangDiagDocs(const RecordKeeper &Records, raw_ostream &OS) { |
| 2141 |
using namespace docs; |
| 2142 |
|
| 2143 |
// Get the documentation introduction paragraph. |
| 2144 |
const Record *Documentation = Records.getDef("GlobalDocumentation"); |
| 2145 |
if (!Documentation) { |
| 2146 |
PrintFatalError("The Documentation top-level definition is missing, " |
| 2147 |
"no documentation will be generated."); |
| 2148 |
return; |
| 2149 |
} |
| 2150 |
|
| 2151 |
OS << Documentation->getValueAsString("Intro") << "\n"; |
| 2152 |
|
| 2153 |
DiagnosticTextBuilder Builder(Records); |
| 2154 |
|
| 2155 |
ArrayRef<const Record *> Diags = |
| 2156 |
Records.getAllDerivedDefinitions("Diagnostic"); |
| 2157 |
|
| 2158 |
std::vector<const Record *> DiagGroups = |
| 2159 |
Records.getAllDerivedDefinitions("DiagGroup"); |
| 2160 |
sort(DiagGroups, diagGroupBeforeByName); |
| 2161 |
|
| 2162 |
DiagGroupParentMap DGParentMap(Records); |
| 2163 |
|
| 2164 |
DiagsInGroupTy DiagsInGroup; |
| 2165 |
groupDiagnostics(Diags, DiagGroups, DiagsInGroup); |
| 2166 |
|
| 2167 |
// Compute the set of diagnostics that are in -Wpedantic. |
| 2168 |
{ |
| 2169 |
RecordSet DiagsInPedanticSet; |
| 2170 |
RecordSet GroupsInPedanticSet; |
| 2171 |
InferPedantic inferPedantic(DGParentMap, Diags, DiagGroups, DiagsInGroup); |
| 2172 |
inferPedantic.compute(&DiagsInPedanticSet, &GroupsInPedanticSet); |
| 2173 |
auto &PedDiags = DiagsInGroup["pedantic"]; |
| 2174 |
// Put the diagnostics into a deterministic order. |
| 2175 |
RecordVec DiagsInPedantic(DiagsInPedanticSet.begin(), |
| 2176 |
DiagsInPedanticSet.end()); |
| 2177 |
RecordVec GroupsInPedantic(GroupsInPedanticSet.begin(), |
| 2178 |
GroupsInPedanticSet.end()); |
| 2179 |
sort(DiagsInPedantic, beforeThanCompare); |
| 2180 |
sort(GroupsInPedantic, beforeThanCompare); |
| 2181 |
PedDiags.DiagsInGroup.insert(PedDiags.DiagsInGroup.end(), |
| 2182 |
DiagsInPedantic.begin(), |
| 2183 |
DiagsInPedantic.end()); |
| 2184 |
for (auto *Group : GroupsInPedantic) |
| 2185 |
PedDiags.SubGroups.push_back(Group->getValueAsString("GroupName")); |
| 2186 |
} |
| 2187 |
|
| 2188 |
// FIXME: Write diagnostic categories and link to diagnostic groups in each. |
| 2189 |
|
| 2190 |
// Write out the diagnostic groups. |
| 2191 |
for (const Record *G : DiagGroups) { |
| 2192 |
bool IsRemarkGroup = isRemarkGroup(G, DiagsInGroup); |
| 2193 |
auto &GroupInfo = DiagsInGroup[G->getValueAsString("GroupName")]; |
| 2194 |
bool IsSynonym = GroupInfo.DiagsInGroup.empty() && |
| 2195 |
GroupInfo.SubGroups.size() == 1; |
| 2196 |
|
| 2197 |
writeHeader(((IsRemarkGroup ? "-R" : "-W") + |
| 2198 |
G->getValueAsString("GroupName")).str(), |
| 2199 |
OS); |
| 2200 |
|
| 2201 |
if (!IsSynonym) { |
| 2202 |
// FIXME: Ideally, all the diagnostics in a group should have the same |
| 2203 |
// default state, but that is not currently the case. |
| 2204 |
auto DefaultSeverities = getDefaultSeverities(G, DiagsInGroup); |
| 2205 |
if (!DefaultSeverities.empty() && !DefaultSeverities.count("Ignored")) { |
| 2206 |
bool AnyNonErrors = DefaultSeverities.count("Warning") || |
| 2207 |
DefaultSeverities.count("Remark"); |
| 2208 |
if (!AnyNonErrors) |
| 2209 |
OS << "This diagnostic is an error by default, but the flag ``-Wno-" |
| 2210 |
<< G->getValueAsString("GroupName") << "`` can be used to disable " |
| 2211 |
<< "the error.\n\n"; |
| 2212 |
else |
| 2213 |
OS << "This diagnostic is enabled by default.\n\n"; |
| 2214 |
} else if (DefaultSeverities.size() > 1) { |
| 2215 |
OS << "Some of the diagnostics controlled by this flag are enabled " |
| 2216 |
<< "by default.\n\n"; |
| 2217 |
} |
| 2218 |
} |
| 2219 |
|
| 2220 |
if (!GroupInfo.SubGroups.empty()) { |
| 2221 |
if (IsSynonym) |
| 2222 |
OS << "Synonym for "; |
| 2223 |
else if (GroupInfo.DiagsInGroup.empty()) |
| 2224 |
OS << "Controls "; |
| 2225 |
else |
| 2226 |
OS << "Also controls "; |
| 2227 |
|
| 2228 |
sort(GroupInfo.SubGroups); |
| 2229 |
ListSeparator LS; |
| 2230 |
for (StringRef Name : GroupInfo.SubGroups) |
| 2231 |
OS << LS << "`" << (IsRemarkGroup ? "-R" : "-W") << Name << "`_"; |
| 2232 |
OS << ".\n\n"; |
| 2233 |
} |
| 2234 |
|
| 2235 |
if (!GroupInfo.DiagsInGroup.empty()) { |
| 2236 |
OS << "**Diagnostic text:**\n\n"; |
| 2237 |
for (const Record *D : GroupInfo.DiagsInGroup) { |
| 2238 |
auto Severity = getDefaultSeverity(D); |
| 2239 |
Severity[0] = tolower(Severity[0]); |
| 2240 |
if (Severity == "ignored") |
| 2241 |
Severity = IsRemarkGroup ? "remark" : "warning"; |
| 2242 |
|
| 2243 |
writeDiagnosticText(Builder, D, Severity, OS); |
| 2244 |
} |
| 2245 |
} |
| 2246 |
|
| 2247 |
auto Doc = G->getValueAsString("Documentation"); |
| 2248 |
if (!Doc.empty()) |
| 2249 |
OS << Doc; |
| 2250 |
else if (GroupInfo.SubGroups.empty() && GroupInfo.DiagsInGroup.empty()) |
| 2251 |
OS << "This diagnostic flag exists for GCC compatibility, and has no " |
| 2252 |
"effect in Clang.\n"; |
| 2253 |
OS << "\n"; |
| 2254 |
} |
| 2255 |
} |