10b57cec5SDimitry Andric //=- ClangDiagnosticsEmitter.cpp - Generate Clang diagnostics tables -*- C++ -*- 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // These tablegen backends emit Clang diagnostics tables. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 13a7dea167SDimitry Andric #include "TableGenBackends.h" 140b57cec5SDimitry Andric #include "llvm/ADT/DenseSet.h" 150b57cec5SDimitry Andric #include "llvm/ADT/PointerUnion.h" 160b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 170b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 180b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h" 190b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 200b57cec5SDimitry Andric #include "llvm/ADT/StringMap.h" 215ffd83dbSDimitry Andric #include "llvm/ADT/StringSwitch.h" 220b57cec5SDimitry Andric #include "llvm/ADT/Twine.h" 230b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 240b57cec5SDimitry Andric #include "llvm/TableGen/Error.h" 250b57cec5SDimitry Andric #include "llvm/TableGen/Record.h" 260b57cec5SDimitry Andric #include "llvm/TableGen/StringToOffsetTable.h" 270b57cec5SDimitry Andric #include "llvm/TableGen/TableGenBackend.h" 280b57cec5SDimitry Andric #include <algorithm> 290b57cec5SDimitry Andric #include <cctype> 300b57cec5SDimitry Andric #include <functional> 310b57cec5SDimitry Andric #include <map> 32*bdd1243dSDimitry Andric #include <optional> 330b57cec5SDimitry Andric #include <set> 340b57cec5SDimitry Andric using namespace llvm; 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 370b57cec5SDimitry Andric // Diagnostic category computation code. 380b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric namespace { 410b57cec5SDimitry Andric class DiagGroupParentMap { 420b57cec5SDimitry Andric RecordKeeper &Records; 430b57cec5SDimitry Andric std::map<const Record*, std::vector<Record*> > Mapping; 440b57cec5SDimitry Andric public: 450b57cec5SDimitry Andric DiagGroupParentMap(RecordKeeper &records) : Records(records) { 460b57cec5SDimitry Andric std::vector<Record*> DiagGroups 470b57cec5SDimitry Andric = Records.getAllDerivedDefinitions("DiagGroup"); 480b57cec5SDimitry Andric for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) { 490b57cec5SDimitry Andric std::vector<Record*> SubGroups = 500b57cec5SDimitry Andric DiagGroups[i]->getValueAsListOfDefs("SubGroups"); 510b57cec5SDimitry Andric for (unsigned j = 0, e = SubGroups.size(); j != e; ++j) 520b57cec5SDimitry Andric Mapping[SubGroups[j]].push_back(DiagGroups[i]); 530b57cec5SDimitry Andric } 540b57cec5SDimitry Andric } 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric const std::vector<Record*> &getParents(const Record *Group) { 570b57cec5SDimitry Andric return Mapping[Group]; 580b57cec5SDimitry Andric } 590b57cec5SDimitry Andric }; 600b57cec5SDimitry Andric } // end anonymous namespace. 610b57cec5SDimitry Andric 620b57cec5SDimitry Andric static std::string 630b57cec5SDimitry Andric getCategoryFromDiagGroup(const Record *Group, 640b57cec5SDimitry Andric DiagGroupParentMap &DiagGroupParents) { 650b57cec5SDimitry Andric // If the DiagGroup has a category, return it. 665ffd83dbSDimitry Andric std::string CatName = std::string(Group->getValueAsString("CategoryName")); 670b57cec5SDimitry Andric if (!CatName.empty()) return CatName; 680b57cec5SDimitry Andric 690b57cec5SDimitry Andric // The diag group may the subgroup of one or more other diagnostic groups, 700b57cec5SDimitry Andric // check these for a category as well. 710b57cec5SDimitry Andric const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group); 720b57cec5SDimitry Andric for (unsigned i = 0, e = Parents.size(); i != e; ++i) { 730b57cec5SDimitry Andric CatName = getCategoryFromDiagGroup(Parents[i], DiagGroupParents); 740b57cec5SDimitry Andric if (!CatName.empty()) return CatName; 750b57cec5SDimitry Andric } 760b57cec5SDimitry Andric return ""; 770b57cec5SDimitry Andric } 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric /// getDiagnosticCategory - Return the category that the specified diagnostic 800b57cec5SDimitry Andric /// lives in. 810b57cec5SDimitry Andric static std::string getDiagnosticCategory(const Record *R, 820b57cec5SDimitry Andric DiagGroupParentMap &DiagGroupParents) { 830b57cec5SDimitry Andric // If the diagnostic is in a group, and that group has a category, use it. 840b57cec5SDimitry Andric if (DefInit *Group = dyn_cast<DefInit>(R->getValueInit("Group"))) { 850b57cec5SDimitry Andric // Check the diagnostic's diag group for a category. 860b57cec5SDimitry Andric std::string CatName = getCategoryFromDiagGroup(Group->getDef(), 870b57cec5SDimitry Andric DiagGroupParents); 880b57cec5SDimitry Andric if (!CatName.empty()) return CatName; 890b57cec5SDimitry Andric } 900b57cec5SDimitry Andric 910b57cec5SDimitry Andric // If the diagnostic itself has a category, get it. 925ffd83dbSDimitry Andric return std::string(R->getValueAsString("CategoryName")); 930b57cec5SDimitry Andric } 940b57cec5SDimitry Andric 950b57cec5SDimitry Andric namespace { 960b57cec5SDimitry Andric class DiagCategoryIDMap { 970b57cec5SDimitry Andric RecordKeeper &Records; 980b57cec5SDimitry Andric StringMap<unsigned> CategoryIDs; 990b57cec5SDimitry Andric std::vector<std::string> CategoryStrings; 1000b57cec5SDimitry Andric public: 1010b57cec5SDimitry Andric DiagCategoryIDMap(RecordKeeper &records) : Records(records) { 1020b57cec5SDimitry Andric DiagGroupParentMap ParentInfo(Records); 1030b57cec5SDimitry Andric 1040b57cec5SDimitry Andric // The zero'th category is "". 1050b57cec5SDimitry Andric CategoryStrings.push_back(""); 1060b57cec5SDimitry Andric CategoryIDs[""] = 0; 1070b57cec5SDimitry Andric 1080b57cec5SDimitry Andric std::vector<Record*> Diags = 1090b57cec5SDimitry Andric Records.getAllDerivedDefinitions("Diagnostic"); 1100b57cec5SDimitry Andric for (unsigned i = 0, e = Diags.size(); i != e; ++i) { 1110b57cec5SDimitry Andric std::string Category = getDiagnosticCategory(Diags[i], ParentInfo); 1120b57cec5SDimitry Andric if (Category.empty()) continue; // Skip diags with no category. 1130b57cec5SDimitry Andric 1140b57cec5SDimitry Andric unsigned &ID = CategoryIDs[Category]; 1150b57cec5SDimitry Andric if (ID != 0) continue; // Already seen. 1160b57cec5SDimitry Andric 1170b57cec5SDimitry Andric ID = CategoryStrings.size(); 1180b57cec5SDimitry Andric CategoryStrings.push_back(Category); 1190b57cec5SDimitry Andric } 1200b57cec5SDimitry Andric } 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric unsigned getID(StringRef CategoryString) { 1230b57cec5SDimitry Andric return CategoryIDs[CategoryString]; 1240b57cec5SDimitry Andric } 1250b57cec5SDimitry Andric 1260b57cec5SDimitry Andric typedef std::vector<std::string>::const_iterator const_iterator; 1270b57cec5SDimitry Andric const_iterator begin() const { return CategoryStrings.begin(); } 1280b57cec5SDimitry Andric const_iterator end() const { return CategoryStrings.end(); } 1290b57cec5SDimitry Andric }; 1300b57cec5SDimitry Andric 1310b57cec5SDimitry Andric struct GroupInfo { 132349cc55cSDimitry Andric llvm::StringRef GroupName; 1330b57cec5SDimitry Andric std::vector<const Record*> DiagsInGroup; 1340b57cec5SDimitry Andric std::vector<std::string> SubGroups; 1350b57cec5SDimitry Andric unsigned IDNo; 1360b57cec5SDimitry Andric 137fe6060f1SDimitry Andric llvm::SmallVector<const Record *, 1> Defs; 1380b57cec5SDimitry Andric 139fe6060f1SDimitry Andric GroupInfo() : IDNo(0) {} 1400b57cec5SDimitry Andric }; 1410b57cec5SDimitry Andric } // end anonymous namespace. 1420b57cec5SDimitry Andric 1430b57cec5SDimitry Andric static bool beforeThanCompare(const Record *LHS, const Record *RHS) { 1440b57cec5SDimitry Andric assert(!LHS->getLoc().empty() && !RHS->getLoc().empty()); 1450b57cec5SDimitry Andric return 1460b57cec5SDimitry Andric LHS->getLoc().front().getPointer() < RHS->getLoc().front().getPointer(); 1470b57cec5SDimitry Andric } 1480b57cec5SDimitry Andric 1490b57cec5SDimitry Andric static bool diagGroupBeforeByName(const Record *LHS, const Record *RHS) { 1500b57cec5SDimitry Andric return LHS->getValueAsString("GroupName") < 1510b57cec5SDimitry Andric RHS->getValueAsString("GroupName"); 1520b57cec5SDimitry Andric } 1530b57cec5SDimitry Andric 1540b57cec5SDimitry Andric /// Invert the 1-[0/1] mapping of diags to group into a one to many 1550b57cec5SDimitry Andric /// mapping of groups to diags in the group. 1560b57cec5SDimitry Andric static void groupDiagnostics(const std::vector<Record*> &Diags, 1570b57cec5SDimitry Andric const std::vector<Record*> &DiagGroups, 1580b57cec5SDimitry Andric std::map<std::string, GroupInfo> &DiagsInGroup) { 1590b57cec5SDimitry Andric 1600b57cec5SDimitry Andric for (unsigned i = 0, e = Diags.size(); i != e; ++i) { 1610b57cec5SDimitry Andric const Record *R = Diags[i]; 1620b57cec5SDimitry Andric DefInit *DI = dyn_cast<DefInit>(R->getValueInit("Group")); 1630b57cec5SDimitry Andric if (!DI) 1640b57cec5SDimitry Andric continue; 1650b57cec5SDimitry Andric assert(R->getValueAsDef("Class")->getName() != "CLASS_NOTE" && 1660b57cec5SDimitry Andric "Note can't be in a DiagGroup"); 1675ffd83dbSDimitry Andric std::string GroupName = 1685ffd83dbSDimitry Andric std::string(DI->getDef()->getValueAsString("GroupName")); 1690b57cec5SDimitry Andric DiagsInGroup[GroupName].DiagsInGroup.push_back(R); 1700b57cec5SDimitry Andric } 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric // Add all DiagGroup's to the DiagsInGroup list to make sure we pick up empty 1730b57cec5SDimitry Andric // groups (these are warnings that GCC supports that clang never produces). 1740b57cec5SDimitry Andric for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) { 1750b57cec5SDimitry Andric Record *Group = DiagGroups[i]; 1765ffd83dbSDimitry Andric GroupInfo &GI = 1775ffd83dbSDimitry Andric DiagsInGroup[std::string(Group->getValueAsString("GroupName"))]; 178349cc55cSDimitry Andric GI.GroupName = Group->getName(); 179fe6060f1SDimitry Andric GI.Defs.push_back(Group); 1800b57cec5SDimitry Andric 1810b57cec5SDimitry Andric std::vector<Record*> SubGroups = Group->getValueAsListOfDefs("SubGroups"); 1820b57cec5SDimitry Andric for (unsigned j = 0, e = SubGroups.size(); j != e; ++j) 1835ffd83dbSDimitry Andric GI.SubGroups.push_back( 1845ffd83dbSDimitry Andric std::string(SubGroups[j]->getValueAsString("GroupName"))); 1850b57cec5SDimitry Andric } 1860b57cec5SDimitry Andric 1870b57cec5SDimitry Andric // Assign unique ID numbers to the groups. 1880b57cec5SDimitry Andric unsigned IDNo = 0; 1890b57cec5SDimitry Andric for (std::map<std::string, GroupInfo>::iterator 1900b57cec5SDimitry Andric I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I, ++IDNo) 1910b57cec5SDimitry Andric I->second.IDNo = IDNo; 1920b57cec5SDimitry Andric 193fe6060f1SDimitry Andric // Warn if the same group is defined more than once (including implicitly). 194fe6060f1SDimitry Andric for (auto &Group : DiagsInGroup) { 195fe6060f1SDimitry Andric if (Group.second.Defs.size() == 1 && 196fe6060f1SDimitry Andric (!Group.second.Defs.front()->isAnonymous() || 197fe6060f1SDimitry Andric Group.second.DiagsInGroup.size() <= 1)) 1980b57cec5SDimitry Andric continue; 1990b57cec5SDimitry Andric 200fe6060f1SDimitry Andric bool First = true; 201fe6060f1SDimitry Andric for (const Record *Def : Group.second.Defs) { 202fe6060f1SDimitry Andric // Skip implicit definitions from diagnostics; we'll report those 203fe6060f1SDimitry Andric // separately below. 204fe6060f1SDimitry Andric bool IsImplicit = false; 205fe6060f1SDimitry Andric for (const Record *Diag : Group.second.DiagsInGroup) { 206fe6060f1SDimitry Andric if (cast<DefInit>(Diag->getValueInit("Group"))->getDef() == Def) { 207fe6060f1SDimitry Andric IsImplicit = true; 208fe6060f1SDimitry Andric break; 2090b57cec5SDimitry Andric } 210fe6060f1SDimitry Andric } 211fe6060f1SDimitry Andric if (IsImplicit) 212fe6060f1SDimitry Andric continue; 213fe6060f1SDimitry Andric 214fe6060f1SDimitry Andric llvm::SMLoc Loc = Def->getLoc().front(); 215fe6060f1SDimitry Andric if (First) { 216fe6060f1SDimitry Andric SrcMgr.PrintMessage(Loc, SourceMgr::DK_Error, 217fe6060f1SDimitry Andric Twine("group '") + Group.first + 218fe6060f1SDimitry Andric "' is defined more than once"); 219fe6060f1SDimitry Andric First = false; 2200b57cec5SDimitry Andric } else { 221fe6060f1SDimitry Andric SrcMgr.PrintMessage(Loc, SourceMgr::DK_Note, "also defined here"); 222fe6060f1SDimitry Andric } 223fe6060f1SDimitry Andric } 2240b57cec5SDimitry Andric 225fe6060f1SDimitry Andric for (const Record *Diag : Group.second.DiagsInGroup) { 226fe6060f1SDimitry Andric if (!cast<DefInit>(Diag->getValueInit("Group"))->getDef()->isAnonymous()) 227fe6060f1SDimitry Andric continue; 2280b57cec5SDimitry Andric 229fe6060f1SDimitry Andric llvm::SMLoc Loc = Diag->getLoc().front(); 230fe6060f1SDimitry Andric if (First) { 231fe6060f1SDimitry Andric SrcMgr.PrintMessage(Loc, SourceMgr::DK_Error, 232fe6060f1SDimitry Andric Twine("group '") + Group.first + 233fe6060f1SDimitry Andric "' is implicitly defined more than once"); 234fe6060f1SDimitry Andric First = false; 235fe6060f1SDimitry Andric } else { 236fe6060f1SDimitry Andric SrcMgr.PrintMessage(Loc, SourceMgr::DK_Note, 237fe6060f1SDimitry Andric "also implicitly defined here"); 2380b57cec5SDimitry Andric } 2390b57cec5SDimitry Andric } 2400b57cec5SDimitry Andric } 2410b57cec5SDimitry Andric } 2420b57cec5SDimitry Andric 2430b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 2440b57cec5SDimitry Andric // Infer members of -Wpedantic. 2450b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 2460b57cec5SDimitry Andric 2470b57cec5SDimitry Andric typedef std::vector<const Record *> RecordVec; 2480b57cec5SDimitry Andric typedef llvm::DenseSet<const Record *> RecordSet; 2490b57cec5SDimitry Andric typedef llvm::PointerUnion<RecordVec*, RecordSet*> VecOrSet; 2500b57cec5SDimitry Andric 2510b57cec5SDimitry Andric namespace { 2520b57cec5SDimitry Andric class InferPedantic { 2530b57cec5SDimitry Andric typedef llvm::DenseMap<const Record *, 254*bdd1243dSDimitry Andric std::pair<unsigned, std::optional<unsigned>>> 255*bdd1243dSDimitry Andric GMap; 2560b57cec5SDimitry Andric 2570b57cec5SDimitry Andric DiagGroupParentMap &DiagGroupParents; 2580b57cec5SDimitry Andric const std::vector<Record*> &Diags; 2590b57cec5SDimitry Andric const std::vector<Record*> DiagGroups; 2600b57cec5SDimitry Andric std::map<std::string, GroupInfo> &DiagsInGroup; 2610b57cec5SDimitry Andric llvm::DenseSet<const Record*> DiagsSet; 2620b57cec5SDimitry Andric GMap GroupCount; 2630b57cec5SDimitry Andric public: 2640b57cec5SDimitry Andric InferPedantic(DiagGroupParentMap &DiagGroupParents, 2650b57cec5SDimitry Andric const std::vector<Record*> &Diags, 2660b57cec5SDimitry Andric const std::vector<Record*> &DiagGroups, 2670b57cec5SDimitry Andric std::map<std::string, GroupInfo> &DiagsInGroup) 2680b57cec5SDimitry Andric : DiagGroupParents(DiagGroupParents), 2690b57cec5SDimitry Andric Diags(Diags), 2700b57cec5SDimitry Andric DiagGroups(DiagGroups), 2710b57cec5SDimitry Andric DiagsInGroup(DiagsInGroup) {} 2720b57cec5SDimitry Andric 2730b57cec5SDimitry Andric /// Compute the set of diagnostics and groups that are immediately 2740b57cec5SDimitry Andric /// in -Wpedantic. 2750b57cec5SDimitry Andric void compute(VecOrSet DiagsInPedantic, 2760b57cec5SDimitry Andric VecOrSet GroupsInPedantic); 2770b57cec5SDimitry Andric 2780b57cec5SDimitry Andric private: 2790b57cec5SDimitry Andric /// Determine whether a group is a subgroup of another group. 2800b57cec5SDimitry Andric bool isSubGroupOfGroup(const Record *Group, 2810b57cec5SDimitry Andric llvm::StringRef RootGroupName); 2820b57cec5SDimitry Andric 2830b57cec5SDimitry Andric /// Determine if the diagnostic is an extension. 2840b57cec5SDimitry Andric bool isExtension(const Record *Diag); 2850b57cec5SDimitry Andric 2860b57cec5SDimitry Andric /// Determine if the diagnostic is off by default. 2870b57cec5SDimitry Andric bool isOffByDefault(const Record *Diag); 2880b57cec5SDimitry Andric 2890b57cec5SDimitry Andric /// Increment the count for a group, and transitively marked 2900b57cec5SDimitry Andric /// parent groups when appropriate. 2910b57cec5SDimitry Andric void markGroup(const Record *Group); 2920b57cec5SDimitry Andric 2930b57cec5SDimitry Andric /// Return true if the diagnostic is in a pedantic group. 2940b57cec5SDimitry Andric bool groupInPedantic(const Record *Group, bool increment = false); 2950b57cec5SDimitry Andric }; 2960b57cec5SDimitry Andric } // end anonymous namespace 2970b57cec5SDimitry Andric 2980b57cec5SDimitry Andric bool InferPedantic::isSubGroupOfGroup(const Record *Group, 2990b57cec5SDimitry Andric llvm::StringRef GName) { 3005ffd83dbSDimitry Andric const std::string &GroupName = 3015ffd83dbSDimitry Andric std::string(Group->getValueAsString("GroupName")); 3020b57cec5SDimitry Andric if (GName == GroupName) 3030b57cec5SDimitry Andric return true; 3040b57cec5SDimitry Andric 3050b57cec5SDimitry Andric const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group); 3060b57cec5SDimitry Andric for (unsigned i = 0, e = Parents.size(); i != e; ++i) 3070b57cec5SDimitry Andric if (isSubGroupOfGroup(Parents[i], GName)) 3080b57cec5SDimitry Andric return true; 3090b57cec5SDimitry Andric 3100b57cec5SDimitry Andric return false; 3110b57cec5SDimitry Andric } 3120b57cec5SDimitry Andric 3130b57cec5SDimitry Andric /// Determine if the diagnostic is an extension. 3140b57cec5SDimitry Andric bool InferPedantic::isExtension(const Record *Diag) { 3155ffd83dbSDimitry Andric const std::string &ClsName = 3165ffd83dbSDimitry Andric std::string(Diag->getValueAsDef("Class")->getName()); 3170b57cec5SDimitry Andric return ClsName == "CLASS_EXTENSION"; 3180b57cec5SDimitry Andric } 3190b57cec5SDimitry Andric 3200b57cec5SDimitry Andric bool InferPedantic::isOffByDefault(const Record *Diag) { 3215ffd83dbSDimitry Andric const std::string &DefSeverity = std::string( 3225ffd83dbSDimitry Andric Diag->getValueAsDef("DefaultSeverity")->getValueAsString("Name")); 3230b57cec5SDimitry Andric return DefSeverity == "Ignored"; 3240b57cec5SDimitry Andric } 3250b57cec5SDimitry Andric 3260b57cec5SDimitry Andric bool InferPedantic::groupInPedantic(const Record *Group, bool increment) { 3270b57cec5SDimitry Andric GMap::mapped_type &V = GroupCount[Group]; 3280b57cec5SDimitry Andric // Lazily compute the threshold value for the group count. 32981ad6265SDimitry Andric if (!V.second) { 3305ffd83dbSDimitry Andric const GroupInfo &GI = 3315ffd83dbSDimitry Andric DiagsInGroup[std::string(Group->getValueAsString("GroupName"))]; 3320b57cec5SDimitry Andric V.second = GI.SubGroups.size() + GI.DiagsInGroup.size(); 3330b57cec5SDimitry Andric } 3340b57cec5SDimitry Andric 3350b57cec5SDimitry Andric if (increment) 3360b57cec5SDimitry Andric ++V.first; 3370b57cec5SDimitry Andric 3380b57cec5SDimitry Andric // Consider a group in -Wpendatic IFF if has at least one diagnostic 3390b57cec5SDimitry Andric // or subgroup AND all of those diagnostics and subgroups are covered 3400b57cec5SDimitry Andric // by -Wpedantic via our computation. 34181ad6265SDimitry Andric return V.first != 0 && V.first == *V.second; 3420b57cec5SDimitry Andric } 3430b57cec5SDimitry Andric 3440b57cec5SDimitry Andric void InferPedantic::markGroup(const Record *Group) { 3450b57cec5SDimitry Andric // If all the diagnostics and subgroups have been marked as being 3460b57cec5SDimitry Andric // covered by -Wpedantic, increment the count of parent groups. Once the 3470b57cec5SDimitry Andric // group's count is equal to the number of subgroups and diagnostics in 3480b57cec5SDimitry Andric // that group, we can safely add this group to -Wpedantic. 3490b57cec5SDimitry Andric if (groupInPedantic(Group, /* increment */ true)) { 3500b57cec5SDimitry Andric const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group); 3510b57cec5SDimitry Andric for (unsigned i = 0, e = Parents.size(); i != e; ++i) 3520b57cec5SDimitry Andric markGroup(Parents[i]); 3530b57cec5SDimitry Andric } 3540b57cec5SDimitry Andric } 3550b57cec5SDimitry Andric 3560b57cec5SDimitry Andric void InferPedantic::compute(VecOrSet DiagsInPedantic, 3570b57cec5SDimitry Andric VecOrSet GroupsInPedantic) { 3580b57cec5SDimitry Andric // All extensions that are not on by default are implicitly in the 3590b57cec5SDimitry Andric // "pedantic" group. For those that aren't explicitly included in -Wpedantic, 3600b57cec5SDimitry Andric // mark them for consideration to be included in -Wpedantic directly. 3610b57cec5SDimitry Andric for (unsigned i = 0, e = Diags.size(); i != e; ++i) { 3620b57cec5SDimitry Andric Record *R = Diags[i]; 3630b57cec5SDimitry Andric if (isExtension(R) && isOffByDefault(R)) { 3640b57cec5SDimitry Andric DiagsSet.insert(R); 3650b57cec5SDimitry Andric if (DefInit *Group = dyn_cast<DefInit>(R->getValueInit("Group"))) { 3660b57cec5SDimitry Andric const Record *GroupRec = Group->getDef(); 3670b57cec5SDimitry Andric if (!isSubGroupOfGroup(GroupRec, "pedantic")) { 3680b57cec5SDimitry Andric markGroup(GroupRec); 3690b57cec5SDimitry Andric } 3700b57cec5SDimitry Andric } 3710b57cec5SDimitry Andric } 3720b57cec5SDimitry Andric } 3730b57cec5SDimitry Andric 3740b57cec5SDimitry Andric // Compute the set of diagnostics that are directly in -Wpedantic. We 3750b57cec5SDimitry Andric // march through Diags a second time to ensure the results are emitted 3760b57cec5SDimitry Andric // in deterministic order. 3770b57cec5SDimitry Andric for (unsigned i = 0, e = Diags.size(); i != e; ++i) { 3780b57cec5SDimitry Andric Record *R = Diags[i]; 3790b57cec5SDimitry Andric if (!DiagsSet.count(R)) 3800b57cec5SDimitry Andric continue; 3810b57cec5SDimitry Andric // Check if the group is implicitly in -Wpedantic. If so, 3820b57cec5SDimitry Andric // the diagnostic should not be directly included in the -Wpedantic 3830b57cec5SDimitry Andric // diagnostic group. 3840b57cec5SDimitry Andric if (DefInit *Group = dyn_cast<DefInit>(R->getValueInit("Group"))) 3850b57cec5SDimitry Andric if (groupInPedantic(Group->getDef())) 3860b57cec5SDimitry Andric continue; 3870b57cec5SDimitry Andric 3880b57cec5SDimitry Andric // The diagnostic is not included in a group that is (transitively) in 3890b57cec5SDimitry Andric // -Wpedantic. Include it in -Wpedantic directly. 3900b57cec5SDimitry Andric if (RecordVec *V = DiagsInPedantic.dyn_cast<RecordVec*>()) 3910b57cec5SDimitry Andric V->push_back(R); 3920b57cec5SDimitry Andric else { 3930b57cec5SDimitry Andric DiagsInPedantic.get<RecordSet*>()->insert(R); 3940b57cec5SDimitry Andric } 3950b57cec5SDimitry Andric } 3960b57cec5SDimitry Andric 3970b57cec5SDimitry Andric if (!GroupsInPedantic) 3980b57cec5SDimitry Andric return; 3990b57cec5SDimitry Andric 4000b57cec5SDimitry Andric // Compute the set of groups that are directly in -Wpedantic. We 4010b57cec5SDimitry Andric // march through the groups to ensure the results are emitted 4020b57cec5SDimitry Andric /// in a deterministc order. 4030b57cec5SDimitry Andric for (unsigned i = 0, ei = DiagGroups.size(); i != ei; ++i) { 4040b57cec5SDimitry Andric Record *Group = DiagGroups[i]; 4050b57cec5SDimitry Andric if (!groupInPedantic(Group)) 4060b57cec5SDimitry Andric continue; 4070b57cec5SDimitry Andric 4080b57cec5SDimitry Andric const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group); 409*bdd1243dSDimitry Andric bool AllParentsInPedantic = 410*bdd1243dSDimitry Andric llvm::all_of(Parents, [&](Record *R) { return groupInPedantic(R); }); 4110b57cec5SDimitry Andric // If all the parents are in -Wpedantic, this means that this diagnostic 4120b57cec5SDimitry Andric // group will be indirectly included by -Wpedantic already. In that 4130b57cec5SDimitry Andric // case, do not add it directly to -Wpedantic. If the group has no 4140b57cec5SDimitry Andric // parents, obviously it should go into -Wpedantic. 415*bdd1243dSDimitry Andric if (Parents.size() > 0 && AllParentsInPedantic) 4160b57cec5SDimitry Andric continue; 4170b57cec5SDimitry Andric 4180b57cec5SDimitry Andric if (RecordVec *V = GroupsInPedantic.dyn_cast<RecordVec*>()) 4190b57cec5SDimitry Andric V->push_back(Group); 4200b57cec5SDimitry Andric else { 4210b57cec5SDimitry Andric GroupsInPedantic.get<RecordSet*>()->insert(Group); 4220b57cec5SDimitry Andric } 4230b57cec5SDimitry Andric } 4240b57cec5SDimitry Andric } 4250b57cec5SDimitry Andric 4260b57cec5SDimitry Andric namespace { 4270b57cec5SDimitry Andric enum PieceKind { 4280b57cec5SDimitry Andric MultiPieceClass, 4290b57cec5SDimitry Andric TextPieceClass, 4300b57cec5SDimitry Andric PlaceholderPieceClass, 4310b57cec5SDimitry Andric SelectPieceClass, 4320b57cec5SDimitry Andric PluralPieceClass, 4330b57cec5SDimitry Andric DiffPieceClass, 4340b57cec5SDimitry Andric SubstitutionPieceClass, 4350b57cec5SDimitry Andric }; 4360b57cec5SDimitry Andric 4370b57cec5SDimitry Andric enum ModifierType { 4380b57cec5SDimitry Andric MT_Unknown, 4390b57cec5SDimitry Andric MT_Placeholder, 4400b57cec5SDimitry Andric MT_Select, 4410b57cec5SDimitry Andric MT_Sub, 4420b57cec5SDimitry Andric MT_Plural, 4430b57cec5SDimitry Andric MT_Diff, 4440b57cec5SDimitry Andric MT_Ordinal, 4450b57cec5SDimitry Andric MT_S, 4460b57cec5SDimitry Andric MT_Q, 4470b57cec5SDimitry Andric MT_ObjCClass, 4480b57cec5SDimitry Andric MT_ObjCInstance, 4490b57cec5SDimitry Andric }; 4500b57cec5SDimitry Andric 4510b57cec5SDimitry Andric static StringRef getModifierName(ModifierType MT) { 4520b57cec5SDimitry Andric switch (MT) { 4530b57cec5SDimitry Andric case MT_Select: 4540b57cec5SDimitry Andric return "select"; 4550b57cec5SDimitry Andric case MT_Sub: 4560b57cec5SDimitry Andric return "sub"; 4570b57cec5SDimitry Andric case MT_Diff: 4580b57cec5SDimitry Andric return "diff"; 4590b57cec5SDimitry Andric case MT_Plural: 4600b57cec5SDimitry Andric return "plural"; 4610b57cec5SDimitry Andric case MT_Ordinal: 4620b57cec5SDimitry Andric return "ordinal"; 4630b57cec5SDimitry Andric case MT_S: 4640b57cec5SDimitry Andric return "s"; 4650b57cec5SDimitry Andric case MT_Q: 4660b57cec5SDimitry Andric return "q"; 4670b57cec5SDimitry Andric case MT_Placeholder: 4680b57cec5SDimitry Andric return ""; 4690b57cec5SDimitry Andric case MT_ObjCClass: 4700b57cec5SDimitry Andric return "objcclass"; 4710b57cec5SDimitry Andric case MT_ObjCInstance: 4720b57cec5SDimitry Andric return "objcinstance"; 4730b57cec5SDimitry Andric case MT_Unknown: 4740b57cec5SDimitry Andric llvm_unreachable("invalid modifier type"); 4750b57cec5SDimitry Andric } 4760b57cec5SDimitry Andric // Unhandled case 4770b57cec5SDimitry Andric llvm_unreachable("invalid modifier type"); 4780b57cec5SDimitry Andric } 4790b57cec5SDimitry Andric 4800b57cec5SDimitry Andric struct Piece { 4810b57cec5SDimitry Andric // This type and its derived classes are move-only. 4820b57cec5SDimitry Andric Piece(PieceKind Kind) : ClassKind(Kind) {} 4830b57cec5SDimitry Andric Piece(Piece const &O) = delete; 4840b57cec5SDimitry Andric Piece &operator=(Piece const &) = delete; 4850b57cec5SDimitry Andric virtual ~Piece() {} 4860b57cec5SDimitry Andric 4870b57cec5SDimitry Andric PieceKind getPieceClass() const { return ClassKind; } 4880b57cec5SDimitry Andric static bool classof(const Piece *) { return true; } 4890b57cec5SDimitry Andric 4900b57cec5SDimitry Andric private: 4910b57cec5SDimitry Andric PieceKind ClassKind; 4920b57cec5SDimitry Andric }; 4930b57cec5SDimitry Andric 4940b57cec5SDimitry Andric struct MultiPiece : Piece { 4950b57cec5SDimitry Andric MultiPiece() : Piece(MultiPieceClass) {} 4960b57cec5SDimitry Andric MultiPiece(std::vector<Piece *> Pieces) 4970b57cec5SDimitry Andric : Piece(MultiPieceClass), Pieces(std::move(Pieces)) {} 4980b57cec5SDimitry Andric 4990b57cec5SDimitry Andric std::vector<Piece *> Pieces; 5000b57cec5SDimitry Andric 5010b57cec5SDimitry Andric static bool classof(const Piece *P) { 5020b57cec5SDimitry Andric return P->getPieceClass() == MultiPieceClass; 5030b57cec5SDimitry Andric } 5040b57cec5SDimitry Andric }; 5050b57cec5SDimitry Andric 5060b57cec5SDimitry Andric struct TextPiece : Piece { 5070b57cec5SDimitry Andric StringRef Role; 5080b57cec5SDimitry Andric std::string Text; 5090b57cec5SDimitry Andric TextPiece(StringRef Text, StringRef Role = "") 5100b57cec5SDimitry Andric : Piece(TextPieceClass), Role(Role), Text(Text.str()) {} 5110b57cec5SDimitry Andric 5120b57cec5SDimitry Andric static bool classof(const Piece *P) { 5130b57cec5SDimitry Andric return P->getPieceClass() == TextPieceClass; 5140b57cec5SDimitry Andric } 5150b57cec5SDimitry Andric }; 5160b57cec5SDimitry Andric 5170b57cec5SDimitry Andric struct PlaceholderPiece : Piece { 5180b57cec5SDimitry Andric ModifierType Kind; 5190b57cec5SDimitry Andric int Index; 5200b57cec5SDimitry Andric PlaceholderPiece(ModifierType Kind, int Index) 5210b57cec5SDimitry Andric : Piece(PlaceholderPieceClass), Kind(Kind), Index(Index) {} 5220b57cec5SDimitry Andric 5230b57cec5SDimitry Andric static bool classof(const Piece *P) { 5240b57cec5SDimitry Andric return P->getPieceClass() == PlaceholderPieceClass; 5250b57cec5SDimitry Andric } 5260b57cec5SDimitry Andric }; 5270b57cec5SDimitry Andric 5280b57cec5SDimitry Andric struct SelectPiece : Piece { 5290b57cec5SDimitry Andric protected: 5300b57cec5SDimitry Andric SelectPiece(PieceKind Kind, ModifierType ModKind) 5310b57cec5SDimitry Andric : Piece(Kind), ModKind(ModKind) {} 5320b57cec5SDimitry Andric 5330b57cec5SDimitry Andric public: 5340b57cec5SDimitry Andric SelectPiece(ModifierType ModKind) : SelectPiece(SelectPieceClass, ModKind) {} 5350b57cec5SDimitry Andric 5360b57cec5SDimitry Andric ModifierType ModKind; 5370b57cec5SDimitry Andric std::vector<Piece *> Options; 538480093f4SDimitry Andric int Index = 0; 5390b57cec5SDimitry Andric 5400b57cec5SDimitry Andric static bool classof(const Piece *P) { 5410b57cec5SDimitry Andric return P->getPieceClass() == SelectPieceClass || 5420b57cec5SDimitry Andric P->getPieceClass() == PluralPieceClass; 5430b57cec5SDimitry Andric } 5440b57cec5SDimitry Andric }; 5450b57cec5SDimitry Andric 5460b57cec5SDimitry Andric struct PluralPiece : SelectPiece { 5470b57cec5SDimitry Andric PluralPiece() : SelectPiece(PluralPieceClass, MT_Plural) {} 5480b57cec5SDimitry Andric 5490b57cec5SDimitry Andric std::vector<Piece *> OptionPrefixes; 550480093f4SDimitry Andric int Index = 0; 5510b57cec5SDimitry Andric 5520b57cec5SDimitry Andric static bool classof(const Piece *P) { 5530b57cec5SDimitry Andric return P->getPieceClass() == PluralPieceClass; 5540b57cec5SDimitry Andric } 5550b57cec5SDimitry Andric }; 5560b57cec5SDimitry Andric 5570b57cec5SDimitry Andric struct DiffPiece : Piece { 5580b57cec5SDimitry Andric DiffPiece() : Piece(DiffPieceClass) {} 5590b57cec5SDimitry Andric 560fe6060f1SDimitry Andric Piece *Parts[4] = {}; 5610b57cec5SDimitry Andric int Indexes[2] = {}; 5620b57cec5SDimitry Andric 5630b57cec5SDimitry Andric static bool classof(const Piece *P) { 5640b57cec5SDimitry Andric return P->getPieceClass() == DiffPieceClass; 5650b57cec5SDimitry Andric } 5660b57cec5SDimitry Andric }; 5670b57cec5SDimitry Andric 5680b57cec5SDimitry Andric struct SubstitutionPiece : Piece { 5690b57cec5SDimitry Andric SubstitutionPiece() : Piece(SubstitutionPieceClass) {} 5700b57cec5SDimitry Andric 5710b57cec5SDimitry Andric std::string Name; 5720b57cec5SDimitry Andric std::vector<int> Modifiers; 5730b57cec5SDimitry Andric 5740b57cec5SDimitry Andric static bool classof(const Piece *P) { 5750b57cec5SDimitry Andric return P->getPieceClass() == SubstitutionPieceClass; 5760b57cec5SDimitry Andric } 5770b57cec5SDimitry Andric }; 5780b57cec5SDimitry Andric 5790b57cec5SDimitry Andric /// Diagnostic text, parsed into pieces. 5800b57cec5SDimitry Andric 5810b57cec5SDimitry Andric 5820b57cec5SDimitry Andric struct DiagnosticTextBuilder { 5830b57cec5SDimitry Andric DiagnosticTextBuilder(DiagnosticTextBuilder const &) = delete; 5840b57cec5SDimitry Andric DiagnosticTextBuilder &operator=(DiagnosticTextBuilder const &) = delete; 5850b57cec5SDimitry Andric 5860b57cec5SDimitry Andric DiagnosticTextBuilder(RecordKeeper &Records) { 5870b57cec5SDimitry Andric // Build up the list of substitution records. 5880b57cec5SDimitry Andric for (auto *S : Records.getAllDerivedDefinitions("TextSubstitution")) { 5890b57cec5SDimitry Andric EvaluatingRecordGuard Guard(&EvaluatingRecord, S); 5900b57cec5SDimitry Andric Substitutions.try_emplace( 5910b57cec5SDimitry Andric S->getName(), DiagText(*this, S->getValueAsString("Substitution"))); 5920b57cec5SDimitry Andric } 5930b57cec5SDimitry Andric 5940b57cec5SDimitry Andric // Check that no diagnostic definitions have the same name as a 5950b57cec5SDimitry Andric // substitution. 5960b57cec5SDimitry Andric for (Record *Diag : Records.getAllDerivedDefinitions("Diagnostic")) { 5970b57cec5SDimitry Andric StringRef Name = Diag->getName(); 5980b57cec5SDimitry Andric if (Substitutions.count(Name)) 5990b57cec5SDimitry Andric llvm::PrintFatalError( 6000b57cec5SDimitry Andric Diag->getLoc(), 6010b57cec5SDimitry Andric "Diagnostic '" + Name + 6020b57cec5SDimitry Andric "' has same name as TextSubstitution definition"); 6030b57cec5SDimitry Andric } 6040b57cec5SDimitry Andric } 6050b57cec5SDimitry Andric 6060b57cec5SDimitry Andric std::vector<std::string> buildForDocumentation(StringRef Role, 6070b57cec5SDimitry Andric const Record *R); 6080b57cec5SDimitry Andric std::string buildForDefinition(const Record *R); 6090b57cec5SDimitry Andric 6100b57cec5SDimitry Andric Piece *getSubstitution(SubstitutionPiece *S) const { 6110b57cec5SDimitry Andric auto It = Substitutions.find(S->Name); 6120b57cec5SDimitry Andric if (It == Substitutions.end()) 6130b57cec5SDimitry Andric PrintFatalError("Failed to find substitution with name: " + S->Name); 6140b57cec5SDimitry Andric return It->second.Root; 6150b57cec5SDimitry Andric } 6160b57cec5SDimitry Andric 617349cc55cSDimitry Andric [[noreturn]] void PrintFatalError(llvm::Twine const &Msg) const { 6180b57cec5SDimitry Andric assert(EvaluatingRecord && "not evaluating a record?"); 6190b57cec5SDimitry Andric llvm::PrintFatalError(EvaluatingRecord->getLoc(), Msg); 6200b57cec5SDimitry Andric } 6210b57cec5SDimitry Andric 6220b57cec5SDimitry Andric private: 6230b57cec5SDimitry Andric struct DiagText { 6240b57cec5SDimitry Andric DiagnosticTextBuilder &Builder; 6250b57cec5SDimitry Andric std::vector<Piece *> AllocatedPieces; 6260b57cec5SDimitry Andric Piece *Root = nullptr; 6270b57cec5SDimitry Andric 6280b57cec5SDimitry Andric template <class T, class... Args> T *New(Args &&... args) { 6290b57cec5SDimitry Andric static_assert(std::is_base_of<Piece, T>::value, "must be piece"); 6300b57cec5SDimitry Andric T *Mem = new T(std::forward<Args>(args)...); 6310b57cec5SDimitry Andric AllocatedPieces.push_back(Mem); 6320b57cec5SDimitry Andric return Mem; 6330b57cec5SDimitry Andric } 6340b57cec5SDimitry Andric 6350b57cec5SDimitry Andric DiagText(DiagnosticTextBuilder &Builder, StringRef Text) 636fe6060f1SDimitry Andric : Builder(Builder), Root(parseDiagText(Text, StopAt::End)) {} 6370b57cec5SDimitry Andric 638fe6060f1SDimitry Andric enum class StopAt { 639fe6060f1SDimitry Andric // Parse until the end of the string. 640fe6060f1SDimitry Andric End, 641fe6060f1SDimitry Andric // Additionally stop if we hit a non-nested '|' or '}'. 642fe6060f1SDimitry Andric PipeOrCloseBrace, 643fe6060f1SDimitry Andric // Additionally stop if we hit a non-nested '$'. 644fe6060f1SDimitry Andric Dollar, 645fe6060f1SDimitry Andric }; 646fe6060f1SDimitry Andric 647fe6060f1SDimitry Andric Piece *parseDiagText(StringRef &Text, StopAt Stop); 6480b57cec5SDimitry Andric int parseModifier(StringRef &) const; 6490b57cec5SDimitry Andric 6500b57cec5SDimitry Andric public: 6510b57cec5SDimitry Andric DiagText(DiagText &&O) noexcept 6520b57cec5SDimitry Andric : Builder(O.Builder), AllocatedPieces(std::move(O.AllocatedPieces)), 6530b57cec5SDimitry Andric Root(O.Root) { 6540b57cec5SDimitry Andric O.Root = nullptr; 6550b57cec5SDimitry Andric } 6560b57cec5SDimitry Andric 6570b57cec5SDimitry Andric ~DiagText() { 6580b57cec5SDimitry Andric for (Piece *P : AllocatedPieces) 6590b57cec5SDimitry Andric delete P; 6600b57cec5SDimitry Andric } 6610b57cec5SDimitry Andric }; 6620b57cec5SDimitry Andric 6630b57cec5SDimitry Andric private: 6640b57cec5SDimitry Andric const Record *EvaluatingRecord = nullptr; 6650b57cec5SDimitry Andric struct EvaluatingRecordGuard { 6660b57cec5SDimitry Andric EvaluatingRecordGuard(const Record **Dest, const Record *New) 6670b57cec5SDimitry Andric : Dest(Dest), Old(*Dest) { 6680b57cec5SDimitry Andric *Dest = New; 6690b57cec5SDimitry Andric } 6700b57cec5SDimitry Andric ~EvaluatingRecordGuard() { *Dest = Old; } 6710b57cec5SDimitry Andric const Record **Dest; 6720b57cec5SDimitry Andric const Record *Old; 6730b57cec5SDimitry Andric }; 6740b57cec5SDimitry Andric 6750b57cec5SDimitry Andric StringMap<DiagText> Substitutions; 6760b57cec5SDimitry Andric }; 6770b57cec5SDimitry Andric 6780b57cec5SDimitry Andric template <class Derived> struct DiagTextVisitor { 679*bdd1243dSDimitry Andric using ModifierMappingsType = std::optional<std::vector<int>>; 6800b57cec5SDimitry Andric 6810b57cec5SDimitry Andric private: 6820b57cec5SDimitry Andric Derived &getDerived() { return static_cast<Derived &>(*this); } 6830b57cec5SDimitry Andric 6840b57cec5SDimitry Andric public: 6850b57cec5SDimitry Andric std::vector<int> 6860b57cec5SDimitry Andric getSubstitutionMappings(SubstitutionPiece *P, 6870b57cec5SDimitry Andric const ModifierMappingsType &Mappings) const { 6880b57cec5SDimitry Andric std::vector<int> NewMappings; 6890b57cec5SDimitry Andric for (int Idx : P->Modifiers) 6900b57cec5SDimitry Andric NewMappings.push_back(mapIndex(Idx, Mappings)); 6910b57cec5SDimitry Andric return NewMappings; 6920b57cec5SDimitry Andric } 6930b57cec5SDimitry Andric 6940b57cec5SDimitry Andric struct SubstitutionContext { 6950b57cec5SDimitry Andric SubstitutionContext(DiagTextVisitor &Visitor, SubstitutionPiece *P) 6960b57cec5SDimitry Andric : Visitor(Visitor) { 6970b57cec5SDimitry Andric Substitution = Visitor.Builder.getSubstitution(P); 6980b57cec5SDimitry Andric OldMappings = std::move(Visitor.ModifierMappings); 6990b57cec5SDimitry Andric std::vector<int> NewMappings = 7000b57cec5SDimitry Andric Visitor.getSubstitutionMappings(P, OldMappings); 7010b57cec5SDimitry Andric Visitor.ModifierMappings = std::move(NewMappings); 7020b57cec5SDimitry Andric } 7030b57cec5SDimitry Andric 7040b57cec5SDimitry Andric ~SubstitutionContext() { 7050b57cec5SDimitry Andric Visitor.ModifierMappings = std::move(OldMappings); 7060b57cec5SDimitry Andric } 7070b57cec5SDimitry Andric 7080b57cec5SDimitry Andric private: 7090b57cec5SDimitry Andric DiagTextVisitor &Visitor; 710*bdd1243dSDimitry Andric std::optional<std::vector<int>> OldMappings; 7110b57cec5SDimitry Andric 7120b57cec5SDimitry Andric public: 7130b57cec5SDimitry Andric Piece *Substitution; 7140b57cec5SDimitry Andric }; 7150b57cec5SDimitry Andric 7160b57cec5SDimitry Andric public: 7170b57cec5SDimitry Andric DiagTextVisitor(DiagnosticTextBuilder &Builder) : Builder(Builder) {} 7180b57cec5SDimitry Andric 7190b57cec5SDimitry Andric void Visit(Piece *P) { 7200b57cec5SDimitry Andric switch (P->getPieceClass()) { 7210b57cec5SDimitry Andric #define CASE(T) \ 7220b57cec5SDimitry Andric case T##PieceClass: \ 7230b57cec5SDimitry Andric return getDerived().Visit##T(static_cast<T##Piece *>(P)) 7240b57cec5SDimitry Andric CASE(Multi); 7250b57cec5SDimitry Andric CASE(Text); 7260b57cec5SDimitry Andric CASE(Placeholder); 7270b57cec5SDimitry Andric CASE(Select); 7280b57cec5SDimitry Andric CASE(Plural); 7290b57cec5SDimitry Andric CASE(Diff); 7300b57cec5SDimitry Andric CASE(Substitution); 7310b57cec5SDimitry Andric #undef CASE 7320b57cec5SDimitry Andric } 7330b57cec5SDimitry Andric } 7340b57cec5SDimitry Andric 7350b57cec5SDimitry Andric void VisitSubstitution(SubstitutionPiece *P) { 7360b57cec5SDimitry Andric SubstitutionContext Guard(*this, P); 7370b57cec5SDimitry Andric Visit(Guard.Substitution); 7380b57cec5SDimitry Andric } 7390b57cec5SDimitry Andric 7400b57cec5SDimitry Andric int mapIndex(int Idx, 7410b57cec5SDimitry Andric ModifierMappingsType const &ModifierMappings) const { 7420b57cec5SDimitry Andric if (!ModifierMappings) 7430b57cec5SDimitry Andric return Idx; 7440b57cec5SDimitry Andric if (ModifierMappings->size() <= static_cast<unsigned>(Idx)) 7450b57cec5SDimitry Andric Builder.PrintFatalError("Modifier value '" + std::to_string(Idx) + 7460b57cec5SDimitry Andric "' is not valid for this mapping (has " + 7470b57cec5SDimitry Andric std::to_string(ModifierMappings->size()) + 7480b57cec5SDimitry Andric " mappings)"); 7490b57cec5SDimitry Andric return (*ModifierMappings)[Idx]; 7500b57cec5SDimitry Andric } 7510b57cec5SDimitry Andric 7520b57cec5SDimitry Andric int mapIndex(int Idx) const { 7530b57cec5SDimitry Andric return mapIndex(Idx, ModifierMappings); 7540b57cec5SDimitry Andric } 7550b57cec5SDimitry Andric 7560b57cec5SDimitry Andric protected: 7570b57cec5SDimitry Andric DiagnosticTextBuilder &Builder; 7580b57cec5SDimitry Andric ModifierMappingsType ModifierMappings; 7590b57cec5SDimitry Andric }; 7600b57cec5SDimitry Andric 7610b57cec5SDimitry Andric void escapeRST(StringRef Str, std::string &Out) { 7620b57cec5SDimitry Andric for (auto K : Str) { 7630b57cec5SDimitry Andric if (StringRef("`*|_[]\\").count(K)) 7640b57cec5SDimitry Andric Out.push_back('\\'); 7650b57cec5SDimitry Andric Out.push_back(K); 7660b57cec5SDimitry Andric } 7670b57cec5SDimitry Andric } 7680b57cec5SDimitry Andric 7690b57cec5SDimitry Andric template <typename It> void padToSameLength(It Begin, It End) { 7700b57cec5SDimitry Andric size_t Width = 0; 7710b57cec5SDimitry Andric for (It I = Begin; I != End; ++I) 7720b57cec5SDimitry Andric Width = std::max(Width, I->size()); 7730b57cec5SDimitry Andric for (It I = Begin; I != End; ++I) 7740b57cec5SDimitry Andric (*I) += std::string(Width - I->size(), ' '); 7750b57cec5SDimitry Andric } 7760b57cec5SDimitry Andric 7770b57cec5SDimitry Andric template <typename It> void makeTableRows(It Begin, It End) { 7780b57cec5SDimitry Andric if (Begin == End) 7790b57cec5SDimitry Andric return; 7800b57cec5SDimitry Andric padToSameLength(Begin, End); 7810b57cec5SDimitry Andric for (It I = Begin; I != End; ++I) 7820b57cec5SDimitry Andric *I = "|" + *I + "|"; 7830b57cec5SDimitry Andric } 7840b57cec5SDimitry Andric 7850b57cec5SDimitry Andric void makeRowSeparator(std::string &Str) { 7860b57cec5SDimitry Andric for (char &K : Str) 7870b57cec5SDimitry Andric K = (K == '|' ? '+' : '-'); 7880b57cec5SDimitry Andric } 7890b57cec5SDimitry Andric 7900b57cec5SDimitry Andric struct DiagTextDocPrinter : DiagTextVisitor<DiagTextDocPrinter> { 7910b57cec5SDimitry Andric using BaseTy = DiagTextVisitor<DiagTextDocPrinter>; 7920b57cec5SDimitry Andric DiagTextDocPrinter(DiagnosticTextBuilder &Builder, 7930b57cec5SDimitry Andric std::vector<std::string> &RST) 7940b57cec5SDimitry Andric : BaseTy(Builder), RST(RST) {} 7950b57cec5SDimitry Andric 7960b57cec5SDimitry Andric void gatherNodes( 7970b57cec5SDimitry Andric Piece *OrigP, const ModifierMappingsType &CurrentMappings, 7980b57cec5SDimitry Andric std::vector<std::pair<Piece *, ModifierMappingsType>> &Pieces) const { 7990b57cec5SDimitry Andric if (auto *Sub = dyn_cast<SubstitutionPiece>(OrigP)) { 8000b57cec5SDimitry Andric ModifierMappingsType NewMappings = 8010b57cec5SDimitry Andric getSubstitutionMappings(Sub, CurrentMappings); 8020b57cec5SDimitry Andric return gatherNodes(Builder.getSubstitution(Sub), NewMappings, Pieces); 8030b57cec5SDimitry Andric } 8040b57cec5SDimitry Andric if (auto *MD = dyn_cast<MultiPiece>(OrigP)) { 8050b57cec5SDimitry Andric for (Piece *Node : MD->Pieces) 8060b57cec5SDimitry Andric gatherNodes(Node, CurrentMappings, Pieces); 8070b57cec5SDimitry Andric return; 8080b57cec5SDimitry Andric } 8090b57cec5SDimitry Andric Pieces.push_back(std::make_pair(OrigP, CurrentMappings)); 8100b57cec5SDimitry Andric } 8110b57cec5SDimitry Andric 8120b57cec5SDimitry Andric void VisitMulti(MultiPiece *P) { 8130b57cec5SDimitry Andric if (P->Pieces.empty()) { 8140b57cec5SDimitry Andric RST.push_back(""); 8150b57cec5SDimitry Andric return; 8160b57cec5SDimitry Andric } 8170b57cec5SDimitry Andric 8180b57cec5SDimitry Andric if (P->Pieces.size() == 1) 8190b57cec5SDimitry Andric return Visit(P->Pieces[0]); 8200b57cec5SDimitry Andric 8210b57cec5SDimitry Andric // Flatten the list of nodes, replacing any substitution pieces with the 8220b57cec5SDimitry Andric // recursively flattened substituted node. 8230b57cec5SDimitry Andric std::vector<std::pair<Piece *, ModifierMappingsType>> Pieces; 8240b57cec5SDimitry Andric gatherNodes(P, ModifierMappings, Pieces); 8250b57cec5SDimitry Andric 8260b57cec5SDimitry Andric std::string EmptyLinePrefix; 8270b57cec5SDimitry Andric size_t Start = RST.size(); 8280b57cec5SDimitry Andric bool HasMultipleLines = true; 8290b57cec5SDimitry Andric for (const std::pair<Piece *, ModifierMappingsType> &NodePair : Pieces) { 8300b57cec5SDimitry Andric std::vector<std::string> Lines; 8310b57cec5SDimitry Andric DiagTextDocPrinter Visitor{Builder, Lines}; 8320b57cec5SDimitry Andric Visitor.ModifierMappings = NodePair.second; 8330b57cec5SDimitry Andric Visitor.Visit(NodePair.first); 8340b57cec5SDimitry Andric 8350b57cec5SDimitry Andric if (Lines.empty()) 8360b57cec5SDimitry Andric continue; 8370b57cec5SDimitry Andric 8380b57cec5SDimitry Andric // We need a vertical separator if either this or the previous piece is a 8390b57cec5SDimitry Andric // multi-line piece, or this is the last piece. 8400b57cec5SDimitry Andric const char *Separator = (Lines.size() > 1 || HasMultipleLines) ? "|" : ""; 8410b57cec5SDimitry Andric HasMultipleLines = Lines.size() > 1; 8420b57cec5SDimitry Andric 8430b57cec5SDimitry Andric if (Start + Lines.size() > RST.size()) 8440b57cec5SDimitry Andric RST.resize(Start + Lines.size(), EmptyLinePrefix); 8450b57cec5SDimitry Andric 8460b57cec5SDimitry Andric padToSameLength(Lines.begin(), Lines.end()); 8470b57cec5SDimitry Andric for (size_t I = 0; I != Lines.size(); ++I) 8480b57cec5SDimitry Andric RST[Start + I] += Separator + Lines[I]; 8490b57cec5SDimitry Andric std::string Empty(Lines[0].size(), ' '); 8500b57cec5SDimitry Andric for (size_t I = Start + Lines.size(); I != RST.size(); ++I) 8510b57cec5SDimitry Andric RST[I] += Separator + Empty; 8520b57cec5SDimitry Andric EmptyLinePrefix += Separator + Empty; 8530b57cec5SDimitry Andric } 8540b57cec5SDimitry Andric for (size_t I = Start; I != RST.size(); ++I) 8550b57cec5SDimitry Andric RST[I] += "|"; 8560b57cec5SDimitry Andric EmptyLinePrefix += "|"; 8570b57cec5SDimitry Andric 8580b57cec5SDimitry Andric makeRowSeparator(EmptyLinePrefix); 8590b57cec5SDimitry Andric RST.insert(RST.begin() + Start, EmptyLinePrefix); 8600b57cec5SDimitry Andric RST.insert(RST.end(), EmptyLinePrefix); 8610b57cec5SDimitry Andric } 8620b57cec5SDimitry Andric 8630b57cec5SDimitry Andric void VisitText(TextPiece *P) { 8640b57cec5SDimitry Andric RST.push_back(""); 8650b57cec5SDimitry Andric auto &S = RST.back(); 8660b57cec5SDimitry Andric 8670b57cec5SDimitry Andric StringRef T = P->Text; 8680b57cec5SDimitry Andric while (!T.empty() && T.front() == ' ') { 8690b57cec5SDimitry Andric RST.back() += " |nbsp| "; 8700b57cec5SDimitry Andric T = T.drop_front(); 8710b57cec5SDimitry Andric } 8720b57cec5SDimitry Andric 8730b57cec5SDimitry Andric std::string Suffix; 8740b57cec5SDimitry Andric while (!T.empty() && T.back() == ' ') { 8750b57cec5SDimitry Andric Suffix += " |nbsp| "; 8760b57cec5SDimitry Andric T = T.drop_back(); 8770b57cec5SDimitry Andric } 8780b57cec5SDimitry Andric 8790b57cec5SDimitry Andric if (!T.empty()) { 8800b57cec5SDimitry Andric S += ':'; 8810b57cec5SDimitry Andric S += P->Role; 8820b57cec5SDimitry Andric S += ":`"; 8830b57cec5SDimitry Andric escapeRST(T, S); 8840b57cec5SDimitry Andric S += '`'; 8850b57cec5SDimitry Andric } 8860b57cec5SDimitry Andric 8870b57cec5SDimitry Andric S += Suffix; 8880b57cec5SDimitry Andric } 8890b57cec5SDimitry Andric 8900b57cec5SDimitry Andric void VisitPlaceholder(PlaceholderPiece *P) { 8910b57cec5SDimitry Andric RST.push_back(std::string(":placeholder:`") + 8920b57cec5SDimitry Andric char('A' + mapIndex(P->Index)) + "`"); 8930b57cec5SDimitry Andric } 8940b57cec5SDimitry Andric 8950b57cec5SDimitry Andric void VisitSelect(SelectPiece *P) { 8960b57cec5SDimitry Andric std::vector<size_t> SeparatorIndexes; 8970b57cec5SDimitry Andric SeparatorIndexes.push_back(RST.size()); 8980b57cec5SDimitry Andric RST.emplace_back(); 8990b57cec5SDimitry Andric for (auto *O : P->Options) { 9000b57cec5SDimitry Andric Visit(O); 9010b57cec5SDimitry Andric SeparatorIndexes.push_back(RST.size()); 9020b57cec5SDimitry Andric RST.emplace_back(); 9030b57cec5SDimitry Andric } 9040b57cec5SDimitry Andric 9050b57cec5SDimitry Andric makeTableRows(RST.begin() + SeparatorIndexes.front(), 9060b57cec5SDimitry Andric RST.begin() + SeparatorIndexes.back() + 1); 9070b57cec5SDimitry Andric for (size_t I : SeparatorIndexes) 9080b57cec5SDimitry Andric makeRowSeparator(RST[I]); 9090b57cec5SDimitry Andric } 9100b57cec5SDimitry Andric 9110b57cec5SDimitry Andric void VisitPlural(PluralPiece *P) { VisitSelect(P); } 9120b57cec5SDimitry Andric 913fe6060f1SDimitry Andric void VisitDiff(DiffPiece *P) { 914fe6060f1SDimitry Andric // Render %diff{a $ b $ c|d}e,f as %select{a %e b %f c|d}. 915fe6060f1SDimitry Andric PlaceholderPiece E(MT_Placeholder, P->Indexes[0]); 916fe6060f1SDimitry Andric PlaceholderPiece F(MT_Placeholder, P->Indexes[1]); 917fe6060f1SDimitry Andric 918fe6060f1SDimitry Andric MultiPiece FirstOption; 919fe6060f1SDimitry Andric FirstOption.Pieces.push_back(P->Parts[0]); 920fe6060f1SDimitry Andric FirstOption.Pieces.push_back(&E); 921fe6060f1SDimitry Andric FirstOption.Pieces.push_back(P->Parts[1]); 922fe6060f1SDimitry Andric FirstOption.Pieces.push_back(&F); 923fe6060f1SDimitry Andric FirstOption.Pieces.push_back(P->Parts[2]); 924fe6060f1SDimitry Andric 925fe6060f1SDimitry Andric SelectPiece Select(MT_Diff); 926fe6060f1SDimitry Andric Select.Options.push_back(&FirstOption); 927fe6060f1SDimitry Andric Select.Options.push_back(P->Parts[3]); 928fe6060f1SDimitry Andric 929fe6060f1SDimitry Andric VisitSelect(&Select); 930fe6060f1SDimitry Andric } 9310b57cec5SDimitry Andric 9320b57cec5SDimitry Andric std::vector<std::string> &RST; 9330b57cec5SDimitry Andric }; 9340b57cec5SDimitry Andric 9350b57cec5SDimitry Andric struct DiagTextPrinter : DiagTextVisitor<DiagTextPrinter> { 9360b57cec5SDimitry Andric public: 9370b57cec5SDimitry Andric using BaseTy = DiagTextVisitor<DiagTextPrinter>; 9380b57cec5SDimitry Andric DiagTextPrinter(DiagnosticTextBuilder &Builder, std::string &Result) 9390b57cec5SDimitry Andric : BaseTy(Builder), Result(Result) {} 9400b57cec5SDimitry Andric 9410b57cec5SDimitry Andric void VisitMulti(MultiPiece *P) { 9420b57cec5SDimitry Andric for (auto *Child : P->Pieces) 9430b57cec5SDimitry Andric Visit(Child); 9440b57cec5SDimitry Andric } 9450b57cec5SDimitry Andric void VisitText(TextPiece *P) { Result += P->Text; } 9460b57cec5SDimitry Andric void VisitPlaceholder(PlaceholderPiece *P) { 9470b57cec5SDimitry Andric Result += "%"; 9480b57cec5SDimitry Andric Result += getModifierName(P->Kind); 9490b57cec5SDimitry Andric addInt(mapIndex(P->Index)); 9500b57cec5SDimitry Andric } 9510b57cec5SDimitry Andric void VisitSelect(SelectPiece *P) { 9520b57cec5SDimitry Andric Result += "%"; 9530b57cec5SDimitry Andric Result += getModifierName(P->ModKind); 9540b57cec5SDimitry Andric if (P->ModKind == MT_Select) { 9550b57cec5SDimitry Andric Result += "{"; 9560b57cec5SDimitry Andric for (auto *D : P->Options) { 9570b57cec5SDimitry Andric Visit(D); 9580b57cec5SDimitry Andric Result += '|'; 9590b57cec5SDimitry Andric } 9600b57cec5SDimitry Andric if (!P->Options.empty()) 9610b57cec5SDimitry Andric Result.erase(--Result.end()); 9620b57cec5SDimitry Andric Result += '}'; 9630b57cec5SDimitry Andric } 9640b57cec5SDimitry Andric addInt(mapIndex(P->Index)); 9650b57cec5SDimitry Andric } 9660b57cec5SDimitry Andric 9670b57cec5SDimitry Andric void VisitPlural(PluralPiece *P) { 9680b57cec5SDimitry Andric Result += "%plural{"; 9690b57cec5SDimitry Andric assert(P->Options.size() == P->OptionPrefixes.size()); 9700b57cec5SDimitry Andric for (unsigned I = 0, End = P->Options.size(); I < End; ++I) { 9710b57cec5SDimitry Andric if (P->OptionPrefixes[I]) 9720b57cec5SDimitry Andric Visit(P->OptionPrefixes[I]); 9730b57cec5SDimitry Andric Visit(P->Options[I]); 9740b57cec5SDimitry Andric Result += "|"; 9750b57cec5SDimitry Andric } 9760b57cec5SDimitry Andric if (!P->Options.empty()) 9770b57cec5SDimitry Andric Result.erase(--Result.end()); 9780b57cec5SDimitry Andric Result += '}'; 9790b57cec5SDimitry Andric addInt(mapIndex(P->Index)); 9800b57cec5SDimitry Andric } 9810b57cec5SDimitry Andric 9820b57cec5SDimitry Andric void VisitDiff(DiffPiece *P) { 9830b57cec5SDimitry Andric Result += "%diff{"; 984fe6060f1SDimitry Andric Visit(P->Parts[0]); 985fe6060f1SDimitry Andric Result += "$"; 986fe6060f1SDimitry Andric Visit(P->Parts[1]); 987fe6060f1SDimitry Andric Result += "$"; 988fe6060f1SDimitry Andric Visit(P->Parts[2]); 9890b57cec5SDimitry Andric Result += "|"; 990fe6060f1SDimitry Andric Visit(P->Parts[3]); 9910b57cec5SDimitry Andric Result += "}"; 9920b57cec5SDimitry Andric addInt(mapIndex(P->Indexes[0])); 9930b57cec5SDimitry Andric Result += ","; 9940b57cec5SDimitry Andric addInt(mapIndex(P->Indexes[1])); 9950b57cec5SDimitry Andric } 9960b57cec5SDimitry Andric 9970b57cec5SDimitry Andric void addInt(int Val) { Result += std::to_string(Val); } 9980b57cec5SDimitry Andric 9990b57cec5SDimitry Andric std::string &Result; 10000b57cec5SDimitry Andric }; 10010b57cec5SDimitry Andric 10020b57cec5SDimitry Andric int DiagnosticTextBuilder::DiagText::parseModifier(StringRef &Text) const { 10030b57cec5SDimitry Andric if (Text.empty() || !isdigit(Text[0])) 10040b57cec5SDimitry Andric Builder.PrintFatalError("expected modifier in diagnostic"); 10050b57cec5SDimitry Andric int Val = 0; 10060b57cec5SDimitry Andric do { 10070b57cec5SDimitry Andric Val *= 10; 10080b57cec5SDimitry Andric Val += Text[0] - '0'; 10090b57cec5SDimitry Andric Text = Text.drop_front(); 10100b57cec5SDimitry Andric } while (!Text.empty() && isdigit(Text[0])); 10110b57cec5SDimitry Andric return Val; 10120b57cec5SDimitry Andric } 10130b57cec5SDimitry Andric 10140b57cec5SDimitry Andric Piece *DiagnosticTextBuilder::DiagText::parseDiagText(StringRef &Text, 1015fe6060f1SDimitry Andric StopAt Stop) { 10160b57cec5SDimitry Andric std::vector<Piece *> Parsed; 10170b57cec5SDimitry Andric 1018fe6060f1SDimitry Andric constexpr llvm::StringLiteral StopSets[] = {"%", "%|}", "%|}$"}; 1019fe6060f1SDimitry Andric llvm::StringRef StopSet = StopSets[static_cast<int>(Stop)]; 1020fe6060f1SDimitry Andric 10210b57cec5SDimitry Andric while (!Text.empty()) { 10220b57cec5SDimitry Andric size_t End = (size_t)-2; 10230b57cec5SDimitry Andric do 1024fe6060f1SDimitry Andric End = Text.find_first_of(StopSet, End + 2); 1025fe6060f1SDimitry Andric while ( 1026fe6060f1SDimitry Andric End < Text.size() - 1 && Text[End] == '%' && 1027fe6060f1SDimitry Andric (Text[End + 1] == '%' || Text[End + 1] == '|' || Text[End + 1] == '$')); 10280b57cec5SDimitry Andric 10290b57cec5SDimitry Andric if (End) { 10300b57cec5SDimitry Andric Parsed.push_back(New<TextPiece>(Text.slice(0, End), "diagtext")); 10310b57cec5SDimitry Andric Text = Text.slice(End, StringRef::npos); 10320b57cec5SDimitry Andric if (Text.empty()) 10330b57cec5SDimitry Andric break; 10340b57cec5SDimitry Andric } 10350b57cec5SDimitry Andric 1036fe6060f1SDimitry Andric if (Text[0] == '|' || Text[0] == '}' || Text[0] == '$') 10370b57cec5SDimitry Andric break; 10380b57cec5SDimitry Andric 10390b57cec5SDimitry Andric // Drop the '%'. 10400b57cec5SDimitry Andric Text = Text.drop_front(); 10410b57cec5SDimitry Andric 10420b57cec5SDimitry Andric // Extract the (optional) modifier. 10430b57cec5SDimitry Andric size_t ModLength = Text.find_first_of("0123456789{"); 10440b57cec5SDimitry Andric StringRef Modifier = Text.slice(0, ModLength); 10450b57cec5SDimitry Andric Text = Text.slice(ModLength, StringRef::npos); 10460b57cec5SDimitry Andric ModifierType ModType = llvm::StringSwitch<ModifierType>{Modifier} 10470b57cec5SDimitry Andric .Case("select", MT_Select) 10480b57cec5SDimitry Andric .Case("sub", MT_Sub) 10490b57cec5SDimitry Andric .Case("diff", MT_Diff) 10500b57cec5SDimitry Andric .Case("plural", MT_Plural) 10510b57cec5SDimitry Andric .Case("s", MT_S) 10520b57cec5SDimitry Andric .Case("ordinal", MT_Ordinal) 10530b57cec5SDimitry Andric .Case("q", MT_Q) 10540b57cec5SDimitry Andric .Case("objcclass", MT_ObjCClass) 10550b57cec5SDimitry Andric .Case("objcinstance", MT_ObjCInstance) 10560b57cec5SDimitry Andric .Case("", MT_Placeholder) 10570b57cec5SDimitry Andric .Default(MT_Unknown); 10580b57cec5SDimitry Andric 1059fe6060f1SDimitry Andric auto ExpectAndConsume = [&](StringRef Prefix) { 1060fe6060f1SDimitry Andric if (!Text.consume_front(Prefix)) 1061fe6060f1SDimitry Andric Builder.PrintFatalError("expected '" + Prefix + "' while parsing %" + 1062fe6060f1SDimitry Andric Modifier); 1063fe6060f1SDimitry Andric }; 1064fe6060f1SDimitry Andric 10650b57cec5SDimitry Andric switch (ModType) { 10660b57cec5SDimitry Andric case MT_Unknown: 10670b57cec5SDimitry Andric Builder.PrintFatalError("Unknown modifier type: " + Modifier); 10680b57cec5SDimitry Andric case MT_Select: { 10690b57cec5SDimitry Andric SelectPiece *Select = New<SelectPiece>(MT_Select); 10700b57cec5SDimitry Andric do { 10710b57cec5SDimitry Andric Text = Text.drop_front(); // '{' or '|' 1072fe6060f1SDimitry Andric Select->Options.push_back( 1073fe6060f1SDimitry Andric parseDiagText(Text, StopAt::PipeOrCloseBrace)); 10740b57cec5SDimitry Andric assert(!Text.empty() && "malformed %select"); 10750b57cec5SDimitry Andric } while (Text.front() == '|'); 1076fe6060f1SDimitry Andric ExpectAndConsume("}"); 10770b57cec5SDimitry Andric Select->Index = parseModifier(Text); 10780b57cec5SDimitry Andric Parsed.push_back(Select); 10790b57cec5SDimitry Andric continue; 10800b57cec5SDimitry Andric } 10810b57cec5SDimitry Andric case MT_Plural: { 10820b57cec5SDimitry Andric PluralPiece *Plural = New<PluralPiece>(); 10830b57cec5SDimitry Andric do { 10840b57cec5SDimitry Andric Text = Text.drop_front(); // '{' or '|' 10850b57cec5SDimitry Andric size_t End = Text.find_first_of(":"); 10860b57cec5SDimitry Andric if (End == StringRef::npos) 10870b57cec5SDimitry Andric Builder.PrintFatalError("expected ':' while parsing %plural"); 10880b57cec5SDimitry Andric ++End; 10890b57cec5SDimitry Andric assert(!Text.empty()); 10900b57cec5SDimitry Andric Plural->OptionPrefixes.push_back( 10910b57cec5SDimitry Andric New<TextPiece>(Text.slice(0, End), "diagtext")); 10920b57cec5SDimitry Andric Text = Text.slice(End, StringRef::npos); 1093fe6060f1SDimitry Andric Plural->Options.push_back( 1094fe6060f1SDimitry Andric parseDiagText(Text, StopAt::PipeOrCloseBrace)); 1095fe6060f1SDimitry Andric assert(!Text.empty() && "malformed %plural"); 10960b57cec5SDimitry Andric } while (Text.front() == '|'); 1097fe6060f1SDimitry Andric ExpectAndConsume("}"); 10980b57cec5SDimitry Andric Plural->Index = parseModifier(Text); 10990b57cec5SDimitry Andric Parsed.push_back(Plural); 11000b57cec5SDimitry Andric continue; 11010b57cec5SDimitry Andric } 11020b57cec5SDimitry Andric case MT_Sub: { 11030b57cec5SDimitry Andric SubstitutionPiece *Sub = New<SubstitutionPiece>(); 1104fe6060f1SDimitry Andric ExpectAndConsume("{"); 11050b57cec5SDimitry Andric size_t NameSize = Text.find_first_of('}'); 11060b57cec5SDimitry Andric assert(NameSize != size_t(-1) && "failed to find the end of the name"); 11070b57cec5SDimitry Andric assert(NameSize != 0 && "empty name?"); 11080b57cec5SDimitry Andric Sub->Name = Text.substr(0, NameSize).str(); 11090b57cec5SDimitry Andric Text = Text.drop_front(NameSize); 1110fe6060f1SDimitry Andric ExpectAndConsume("}"); 11110b57cec5SDimitry Andric if (!Text.empty()) { 11120b57cec5SDimitry Andric while (true) { 11130b57cec5SDimitry Andric if (!isdigit(Text[0])) 11140b57cec5SDimitry Andric break; 11150b57cec5SDimitry Andric Sub->Modifiers.push_back(parseModifier(Text)); 11160b57cec5SDimitry Andric if (Text.empty() || Text[0] != ',') 11170b57cec5SDimitry Andric break; 11180b57cec5SDimitry Andric Text = Text.drop_front(); // ',' 11190b57cec5SDimitry Andric assert(!Text.empty() && isdigit(Text[0]) && 11200b57cec5SDimitry Andric "expected another modifier"); 11210b57cec5SDimitry Andric } 11220b57cec5SDimitry Andric } 11230b57cec5SDimitry Andric Parsed.push_back(Sub); 11240b57cec5SDimitry Andric continue; 11250b57cec5SDimitry Andric } 11260b57cec5SDimitry Andric case MT_Diff: { 11270b57cec5SDimitry Andric DiffPiece *Diff = New<DiffPiece>(); 1128fe6060f1SDimitry Andric ExpectAndConsume("{"); 1129fe6060f1SDimitry Andric Diff->Parts[0] = parseDiagText(Text, StopAt::Dollar); 1130fe6060f1SDimitry Andric ExpectAndConsume("$"); 1131fe6060f1SDimitry Andric Diff->Parts[1] = parseDiagText(Text, StopAt::Dollar); 1132fe6060f1SDimitry Andric ExpectAndConsume("$"); 1133fe6060f1SDimitry Andric Diff->Parts[2] = parseDiagText(Text, StopAt::PipeOrCloseBrace); 1134fe6060f1SDimitry Andric ExpectAndConsume("|"); 1135fe6060f1SDimitry Andric Diff->Parts[3] = parseDiagText(Text, StopAt::PipeOrCloseBrace); 1136fe6060f1SDimitry Andric ExpectAndConsume("}"); 11370b57cec5SDimitry Andric Diff->Indexes[0] = parseModifier(Text); 1138fe6060f1SDimitry Andric ExpectAndConsume(","); 11390b57cec5SDimitry Andric Diff->Indexes[1] = parseModifier(Text); 11400b57cec5SDimitry Andric Parsed.push_back(Diff); 11410b57cec5SDimitry Andric continue; 11420b57cec5SDimitry Andric } 11430b57cec5SDimitry Andric case MT_S: { 11440b57cec5SDimitry Andric SelectPiece *Select = New<SelectPiece>(ModType); 11450b57cec5SDimitry Andric Select->Options.push_back(New<TextPiece>("")); 11460b57cec5SDimitry Andric Select->Options.push_back(New<TextPiece>("s", "diagtext")); 11470b57cec5SDimitry Andric Select->Index = parseModifier(Text); 11480b57cec5SDimitry Andric Parsed.push_back(Select); 11490b57cec5SDimitry Andric continue; 11500b57cec5SDimitry Andric } 11510b57cec5SDimitry Andric case MT_Q: 11520b57cec5SDimitry Andric case MT_Placeholder: 11530b57cec5SDimitry Andric case MT_ObjCClass: 11540b57cec5SDimitry Andric case MT_ObjCInstance: 11550b57cec5SDimitry Andric case MT_Ordinal: { 11560b57cec5SDimitry Andric Parsed.push_back(New<PlaceholderPiece>(ModType, parseModifier(Text))); 11570b57cec5SDimitry Andric continue; 11580b57cec5SDimitry Andric } 11590b57cec5SDimitry Andric } 11600b57cec5SDimitry Andric } 11610b57cec5SDimitry Andric 11620b57cec5SDimitry Andric return New<MultiPiece>(Parsed); 11630b57cec5SDimitry Andric } 11640b57cec5SDimitry Andric 11650b57cec5SDimitry Andric std::vector<std::string> 11660b57cec5SDimitry Andric DiagnosticTextBuilder::buildForDocumentation(StringRef Severity, 11670b57cec5SDimitry Andric const Record *R) { 11680b57cec5SDimitry Andric EvaluatingRecordGuard Guard(&EvaluatingRecord, R); 1169*bdd1243dSDimitry Andric StringRef Text = R->getValueAsString("Summary"); 11700b57cec5SDimitry Andric 11710b57cec5SDimitry Andric DiagText D(*this, Text); 11720b57cec5SDimitry Andric TextPiece *Prefix = D.New<TextPiece>(Severity, Severity); 11730b57cec5SDimitry Andric Prefix->Text += ": "; 11740b57cec5SDimitry Andric auto *MP = dyn_cast<MultiPiece>(D.Root); 11750b57cec5SDimitry Andric if (!MP) { 11760b57cec5SDimitry Andric MP = D.New<MultiPiece>(); 11770b57cec5SDimitry Andric MP->Pieces.push_back(D.Root); 11780b57cec5SDimitry Andric D.Root = MP; 11790b57cec5SDimitry Andric } 11800b57cec5SDimitry Andric MP->Pieces.insert(MP->Pieces.begin(), Prefix); 11810b57cec5SDimitry Andric std::vector<std::string> Result; 11820b57cec5SDimitry Andric DiagTextDocPrinter{*this, Result}.Visit(D.Root); 11830b57cec5SDimitry Andric return Result; 11840b57cec5SDimitry Andric } 11850b57cec5SDimitry Andric 11860b57cec5SDimitry Andric std::string DiagnosticTextBuilder::buildForDefinition(const Record *R) { 11870b57cec5SDimitry Andric EvaluatingRecordGuard Guard(&EvaluatingRecord, R); 1188*bdd1243dSDimitry Andric StringRef Text = R->getValueAsString("Summary"); 11890b57cec5SDimitry Andric DiagText D(*this, Text); 11900b57cec5SDimitry Andric std::string Result; 11910b57cec5SDimitry Andric DiagTextPrinter{*this, Result}.Visit(D.Root); 11920b57cec5SDimitry Andric return Result; 11930b57cec5SDimitry Andric } 11940b57cec5SDimitry Andric 11950b57cec5SDimitry Andric } // namespace 11960b57cec5SDimitry Andric 11970b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 11980b57cec5SDimitry Andric // Warning Tables (.inc file) generation. 11990b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 12000b57cec5SDimitry Andric 12010b57cec5SDimitry Andric static bool isError(const Record &Diag) { 12025ffd83dbSDimitry Andric const std::string &ClsName = 12035ffd83dbSDimitry Andric std::string(Diag.getValueAsDef("Class")->getName()); 12040b57cec5SDimitry Andric return ClsName == "CLASS_ERROR"; 12050b57cec5SDimitry Andric } 12060b57cec5SDimitry Andric 12070b57cec5SDimitry Andric static bool isRemark(const Record &Diag) { 12085ffd83dbSDimitry Andric const std::string &ClsName = 12095ffd83dbSDimitry Andric std::string(Diag.getValueAsDef("Class")->getName()); 12100b57cec5SDimitry Andric return ClsName == "CLASS_REMARK"; 12110b57cec5SDimitry Andric } 12120b57cec5SDimitry Andric 12130b57cec5SDimitry Andric 12140b57cec5SDimitry Andric /// ClangDiagsDefsEmitter - The top-level class emits .def files containing 12150b57cec5SDimitry Andric /// declarations of Clang diagnostics. 1216a7dea167SDimitry Andric void clang::EmitClangDiagsDefs(RecordKeeper &Records, raw_ostream &OS, 12170b57cec5SDimitry Andric const std::string &Component) { 12180b57cec5SDimitry Andric // Write the #if guard 12190b57cec5SDimitry Andric if (!Component.empty()) { 12200b57cec5SDimitry Andric std::string ComponentName = StringRef(Component).upper(); 12210b57cec5SDimitry Andric OS << "#ifdef " << ComponentName << "START\n"; 12220b57cec5SDimitry Andric OS << "__" << ComponentName << "START = DIAG_START_" << ComponentName 12230b57cec5SDimitry Andric << ",\n"; 12240b57cec5SDimitry Andric OS << "#undef " << ComponentName << "START\n"; 12250b57cec5SDimitry Andric OS << "#endif\n\n"; 12260b57cec5SDimitry Andric } 12270b57cec5SDimitry Andric 12280b57cec5SDimitry Andric DiagnosticTextBuilder DiagTextBuilder(Records); 12290b57cec5SDimitry Andric 12300b57cec5SDimitry Andric std::vector<Record *> Diags = Records.getAllDerivedDefinitions("Diagnostic"); 12310b57cec5SDimitry Andric 12320b57cec5SDimitry Andric std::vector<Record*> DiagGroups 12330b57cec5SDimitry Andric = Records.getAllDerivedDefinitions("DiagGroup"); 12340b57cec5SDimitry Andric 12350b57cec5SDimitry Andric std::map<std::string, GroupInfo> DiagsInGroup; 12360b57cec5SDimitry Andric groupDiagnostics(Diags, DiagGroups, DiagsInGroup); 12370b57cec5SDimitry Andric 12380b57cec5SDimitry Andric DiagCategoryIDMap CategoryIDs(Records); 12390b57cec5SDimitry Andric DiagGroupParentMap DGParentMap(Records); 12400b57cec5SDimitry Andric 12410b57cec5SDimitry Andric // Compute the set of diagnostics that are in -Wpedantic. 12420b57cec5SDimitry Andric RecordSet DiagsInPedantic; 12430b57cec5SDimitry Andric InferPedantic inferPedantic(DGParentMap, Diags, DiagGroups, DiagsInGroup); 12440b57cec5SDimitry Andric inferPedantic.compute(&DiagsInPedantic, (RecordVec*)nullptr); 12450b57cec5SDimitry Andric 12460b57cec5SDimitry Andric for (unsigned i = 0, e = Diags.size(); i != e; ++i) { 12470b57cec5SDimitry Andric const Record &R = *Diags[i]; 12480b57cec5SDimitry Andric 12490b57cec5SDimitry Andric // Check if this is an error that is accidentally in a warning 12500b57cec5SDimitry Andric // group. 12510b57cec5SDimitry Andric if (isError(R)) { 12520b57cec5SDimitry Andric if (DefInit *Group = dyn_cast<DefInit>(R.getValueInit("Group"))) { 12530b57cec5SDimitry Andric const Record *GroupRec = Group->getDef(); 12545ffd83dbSDimitry Andric const std::string &GroupName = 12555ffd83dbSDimitry Andric std::string(GroupRec->getValueAsString("GroupName")); 12560b57cec5SDimitry Andric PrintFatalError(R.getLoc(), "Error " + R.getName() + 12570b57cec5SDimitry Andric " cannot be in a warning group [" + GroupName + "]"); 12580b57cec5SDimitry Andric } 12590b57cec5SDimitry Andric } 12600b57cec5SDimitry Andric 12610b57cec5SDimitry Andric // Check that all remarks have an associated diagnostic group. 12620b57cec5SDimitry Andric if (isRemark(R)) { 12630b57cec5SDimitry Andric if (!isa<DefInit>(R.getValueInit("Group"))) { 12640b57cec5SDimitry Andric PrintFatalError(R.getLoc(), "Error " + R.getName() + 12650b57cec5SDimitry Andric " not in any diagnostic group"); 12660b57cec5SDimitry Andric } 12670b57cec5SDimitry Andric } 12680b57cec5SDimitry Andric 12690b57cec5SDimitry Andric // Filter by component. 12700b57cec5SDimitry Andric if (!Component.empty() && Component != R.getValueAsString("Component")) 12710b57cec5SDimitry Andric continue; 12720b57cec5SDimitry Andric 12730b57cec5SDimitry Andric OS << "DIAG(" << R.getName() << ", "; 12740b57cec5SDimitry Andric OS << R.getValueAsDef("Class")->getName(); 12750b57cec5SDimitry Andric OS << ", (unsigned)diag::Severity::" 12760b57cec5SDimitry Andric << R.getValueAsDef("DefaultSeverity")->getValueAsString("Name"); 12770b57cec5SDimitry Andric 12780b57cec5SDimitry Andric // Description string. 12790b57cec5SDimitry Andric OS << ", \""; 12800b57cec5SDimitry Andric OS.write_escaped(DiagTextBuilder.buildForDefinition(&R)) << '"'; 12810b57cec5SDimitry Andric 1282349cc55cSDimitry Andric // Warning group associated with the diagnostic. This is stored as an index 1283349cc55cSDimitry Andric // into the alphabetically sorted warning group table. 12840b57cec5SDimitry Andric if (DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group"))) { 12855ffd83dbSDimitry Andric std::map<std::string, GroupInfo>::iterator I = DiagsInGroup.find( 12865ffd83dbSDimitry Andric std::string(DI->getDef()->getValueAsString("GroupName"))); 12870b57cec5SDimitry Andric assert(I != DiagsInGroup.end()); 12880b57cec5SDimitry Andric OS << ", " << I->second.IDNo; 12890b57cec5SDimitry Andric } else if (DiagsInPedantic.count(&R)) { 12900b57cec5SDimitry Andric std::map<std::string, GroupInfo>::iterator I = 12910b57cec5SDimitry Andric DiagsInGroup.find("pedantic"); 12920b57cec5SDimitry Andric assert(I != DiagsInGroup.end() && "pedantic group not defined"); 12930b57cec5SDimitry Andric OS << ", " << I->second.IDNo; 12940b57cec5SDimitry Andric } else { 12950b57cec5SDimitry Andric OS << ", 0"; 12960b57cec5SDimitry Andric } 12970b57cec5SDimitry Andric 12980b57cec5SDimitry Andric // SFINAE response. 12990b57cec5SDimitry Andric OS << ", " << R.getValueAsDef("SFINAE")->getName(); 13000b57cec5SDimitry Andric 13010b57cec5SDimitry Andric // Default warning has no Werror bit. 13020b57cec5SDimitry Andric if (R.getValueAsBit("WarningNoWerror")) 13030b57cec5SDimitry Andric OS << ", true"; 13040b57cec5SDimitry Andric else 13050b57cec5SDimitry Andric OS << ", false"; 13060b57cec5SDimitry Andric 13070b57cec5SDimitry Andric if (R.getValueAsBit("ShowInSystemHeader")) 13080b57cec5SDimitry Andric OS << ", true"; 13090b57cec5SDimitry Andric else 13100b57cec5SDimitry Andric OS << ", false"; 13110b57cec5SDimitry Andric 131204eeddc0SDimitry Andric if (R.getValueAsBit("ShowInSystemMacro")) 131304eeddc0SDimitry Andric OS << ", true"; 131404eeddc0SDimitry Andric else 131504eeddc0SDimitry Andric OS << ", false"; 131604eeddc0SDimitry Andric 1317e8d8bef9SDimitry Andric if (R.getValueAsBit("Deferrable")) 1318e8d8bef9SDimitry Andric OS << ", true"; 1319e8d8bef9SDimitry Andric else 1320e8d8bef9SDimitry Andric OS << ", false"; 1321e8d8bef9SDimitry Andric 13220b57cec5SDimitry Andric // Category number. 13230b57cec5SDimitry Andric OS << ", " << CategoryIDs.getID(getDiagnosticCategory(&R, DGParentMap)); 13240b57cec5SDimitry Andric OS << ")\n"; 13250b57cec5SDimitry Andric } 13260b57cec5SDimitry Andric } 13270b57cec5SDimitry Andric 13280b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 13290b57cec5SDimitry Andric // Warning Group Tables generation 13300b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 13310b57cec5SDimitry Andric 13320b57cec5SDimitry Andric static std::string getDiagCategoryEnum(llvm::StringRef name) { 13330b57cec5SDimitry Andric if (name.empty()) 13340b57cec5SDimitry Andric return "DiagCat_None"; 13350b57cec5SDimitry Andric SmallString<256> enumName = llvm::StringRef("DiagCat_"); 13360b57cec5SDimitry Andric for (llvm::StringRef::iterator I = name.begin(), E = name.end(); I != E; ++I) 13370b57cec5SDimitry Andric enumName += isalnum(*I) ? *I : '_'; 13385ffd83dbSDimitry Andric return std::string(enumName.str()); 13390b57cec5SDimitry Andric } 13400b57cec5SDimitry Andric 13410b57cec5SDimitry Andric /// Emit the array of diagnostic subgroups. 13420b57cec5SDimitry Andric /// 13430b57cec5SDimitry Andric /// The array of diagnostic subgroups contains for each group a list of its 13440b57cec5SDimitry Andric /// subgroups. The individual lists are separated by '-1'. Groups with no 13450b57cec5SDimitry Andric /// subgroups are skipped. 13460b57cec5SDimitry Andric /// 13470b57cec5SDimitry Andric /// \code 13480b57cec5SDimitry Andric /// static const int16_t DiagSubGroups[] = { 13490b57cec5SDimitry Andric /// /* Empty */ -1, 13500b57cec5SDimitry Andric /// /* DiagSubGroup0 */ 142, -1, 13510b57cec5SDimitry Andric /// /* DiagSubGroup13 */ 265, 322, 399, -1 13520b57cec5SDimitry Andric /// } 13530b57cec5SDimitry Andric /// \endcode 13540b57cec5SDimitry Andric /// 13550b57cec5SDimitry Andric static void emitDiagSubGroups(std::map<std::string, GroupInfo> &DiagsInGroup, 13560b57cec5SDimitry Andric RecordVec &GroupsInPedantic, raw_ostream &OS) { 13570b57cec5SDimitry Andric OS << "static const int16_t DiagSubGroups[] = {\n" 13580b57cec5SDimitry Andric << " /* Empty */ -1,\n"; 13590b57cec5SDimitry Andric for (auto const &I : DiagsInGroup) { 13600b57cec5SDimitry Andric const bool IsPedantic = I.first == "pedantic"; 13610b57cec5SDimitry Andric 13620b57cec5SDimitry Andric const std::vector<std::string> &SubGroups = I.second.SubGroups; 13630b57cec5SDimitry Andric if (!SubGroups.empty() || (IsPedantic && !GroupsInPedantic.empty())) { 13640b57cec5SDimitry Andric OS << " /* DiagSubGroup" << I.second.IDNo << " */ "; 13650b57cec5SDimitry Andric for (auto const &SubGroup : SubGroups) { 13660b57cec5SDimitry Andric std::map<std::string, GroupInfo>::const_iterator RI = 13670b57cec5SDimitry Andric DiagsInGroup.find(SubGroup); 13680b57cec5SDimitry Andric assert(RI != DiagsInGroup.end() && "Referenced without existing?"); 13690b57cec5SDimitry Andric OS << RI->second.IDNo << ", "; 13700b57cec5SDimitry Andric } 13710b57cec5SDimitry Andric // Emit the groups implicitly in "pedantic". 13720b57cec5SDimitry Andric if (IsPedantic) { 13730b57cec5SDimitry Andric for (auto const &Group : GroupsInPedantic) { 13745ffd83dbSDimitry Andric const std::string &GroupName = 13755ffd83dbSDimitry Andric std::string(Group->getValueAsString("GroupName")); 13760b57cec5SDimitry Andric std::map<std::string, GroupInfo>::const_iterator RI = 13770b57cec5SDimitry Andric DiagsInGroup.find(GroupName); 13780b57cec5SDimitry Andric assert(RI != DiagsInGroup.end() && "Referenced without existing?"); 13790b57cec5SDimitry Andric OS << RI->second.IDNo << ", "; 13800b57cec5SDimitry Andric } 13810b57cec5SDimitry Andric } 13820b57cec5SDimitry Andric 13830b57cec5SDimitry Andric OS << "-1,\n"; 13840b57cec5SDimitry Andric } 13850b57cec5SDimitry Andric } 13860b57cec5SDimitry Andric OS << "};\n\n"; 13870b57cec5SDimitry Andric } 13880b57cec5SDimitry Andric 13890b57cec5SDimitry Andric /// Emit the list of diagnostic arrays. 13900b57cec5SDimitry Andric /// 13910b57cec5SDimitry Andric /// This data structure is a large array that contains itself arrays of varying 13920b57cec5SDimitry Andric /// size. Each array represents a list of diagnostics. The different arrays are 13930b57cec5SDimitry Andric /// separated by the value '-1'. 13940b57cec5SDimitry Andric /// 13950b57cec5SDimitry Andric /// \code 13960b57cec5SDimitry Andric /// static const int16_t DiagArrays[] = { 13970b57cec5SDimitry Andric /// /* Empty */ -1, 13980b57cec5SDimitry Andric /// /* DiagArray1 */ diag::warn_pragma_message, 13990b57cec5SDimitry Andric /// -1, 14000b57cec5SDimitry Andric /// /* DiagArray2 */ diag::warn_abs_too_small, 14010b57cec5SDimitry Andric /// diag::warn_unsigned_abs, 14020b57cec5SDimitry Andric /// diag::warn_wrong_absolute_value_type, 14030b57cec5SDimitry Andric /// -1 14040b57cec5SDimitry Andric /// }; 14050b57cec5SDimitry Andric /// \endcode 14060b57cec5SDimitry Andric /// 14070b57cec5SDimitry Andric static void emitDiagArrays(std::map<std::string, GroupInfo> &DiagsInGroup, 14080b57cec5SDimitry Andric RecordVec &DiagsInPedantic, raw_ostream &OS) { 14090b57cec5SDimitry Andric OS << "static const int16_t DiagArrays[] = {\n" 14100b57cec5SDimitry Andric << " /* Empty */ -1,\n"; 14110b57cec5SDimitry Andric for (auto const &I : DiagsInGroup) { 14120b57cec5SDimitry Andric const bool IsPedantic = I.first == "pedantic"; 14130b57cec5SDimitry Andric 14140b57cec5SDimitry Andric const std::vector<const Record *> &V = I.second.DiagsInGroup; 14150b57cec5SDimitry Andric if (!V.empty() || (IsPedantic && !DiagsInPedantic.empty())) { 14160b57cec5SDimitry Andric OS << " /* DiagArray" << I.second.IDNo << " */ "; 14170b57cec5SDimitry Andric for (auto *Record : V) 14180b57cec5SDimitry Andric OS << "diag::" << Record->getName() << ", "; 14190b57cec5SDimitry Andric // Emit the diagnostics implicitly in "pedantic". 14200b57cec5SDimitry Andric if (IsPedantic) { 14210b57cec5SDimitry Andric for (auto const &Diag : DiagsInPedantic) 14220b57cec5SDimitry Andric OS << "diag::" << Diag->getName() << ", "; 14230b57cec5SDimitry Andric } 14240b57cec5SDimitry Andric OS << "-1,\n"; 14250b57cec5SDimitry Andric } 14260b57cec5SDimitry Andric } 14270b57cec5SDimitry Andric OS << "};\n\n"; 14280b57cec5SDimitry Andric } 14290b57cec5SDimitry Andric 14300b57cec5SDimitry Andric /// Emit a list of group names. 14310b57cec5SDimitry Andric /// 14320b57cec5SDimitry Andric /// This creates a long string which by itself contains a list of pascal style 14330b57cec5SDimitry Andric /// strings, which consist of a length byte directly followed by the string. 14340b57cec5SDimitry Andric /// 14350b57cec5SDimitry Andric /// \code 14360b57cec5SDimitry Andric /// static const char DiagGroupNames[] = { 14370b57cec5SDimitry Andric /// \000\020#pragma-messages\t#warnings\020CFString-literal" 14380b57cec5SDimitry Andric /// }; 14390b57cec5SDimitry Andric /// \endcode 14400b57cec5SDimitry Andric static void emitDiagGroupNames(StringToOffsetTable &GroupNames, 14410b57cec5SDimitry Andric raw_ostream &OS) { 14420b57cec5SDimitry Andric OS << "static const char DiagGroupNames[] = {\n"; 14430b57cec5SDimitry Andric GroupNames.EmitString(OS); 14440b57cec5SDimitry Andric OS << "};\n\n"; 14450b57cec5SDimitry Andric } 14460b57cec5SDimitry Andric 14470b57cec5SDimitry Andric /// Emit diagnostic arrays and related data structures. 14480b57cec5SDimitry Andric /// 14490b57cec5SDimitry Andric /// This creates the actual diagnostic array, an array of diagnostic subgroups 14500b57cec5SDimitry Andric /// and an array of subgroup names. 14510b57cec5SDimitry Andric /// 14520b57cec5SDimitry Andric /// \code 14530b57cec5SDimitry Andric /// #ifdef GET_DIAG_ARRAYS 14540b57cec5SDimitry Andric /// static const int16_t DiagArrays[]; 14550b57cec5SDimitry Andric /// static const int16_t DiagSubGroups[]; 14560b57cec5SDimitry Andric /// static const char DiagGroupNames[]; 14570b57cec5SDimitry Andric /// #endif 14580b57cec5SDimitry Andric /// \endcode 14590b57cec5SDimitry Andric static void emitAllDiagArrays(std::map<std::string, GroupInfo> &DiagsInGroup, 14600b57cec5SDimitry Andric RecordVec &DiagsInPedantic, 14610b57cec5SDimitry Andric RecordVec &GroupsInPedantic, 14620b57cec5SDimitry Andric StringToOffsetTable &GroupNames, 14630b57cec5SDimitry Andric raw_ostream &OS) { 14640b57cec5SDimitry Andric OS << "\n#ifdef GET_DIAG_ARRAYS\n"; 14650b57cec5SDimitry Andric emitDiagArrays(DiagsInGroup, DiagsInPedantic, OS); 14660b57cec5SDimitry Andric emitDiagSubGroups(DiagsInGroup, GroupsInPedantic, OS); 14670b57cec5SDimitry Andric emitDiagGroupNames(GroupNames, OS); 14680b57cec5SDimitry Andric OS << "#endif // GET_DIAG_ARRAYS\n\n"; 14690b57cec5SDimitry Andric } 14700b57cec5SDimitry Andric 14710b57cec5SDimitry Andric /// Emit diagnostic table. 14720b57cec5SDimitry Andric /// 14730b57cec5SDimitry Andric /// The table is sorted by the name of the diagnostic group. Each element 14740b57cec5SDimitry Andric /// consists of the name of the diagnostic group (given as offset in the 14750b57cec5SDimitry Andric /// group name table), a reference to a list of diagnostics (optional) and a 14760b57cec5SDimitry Andric /// reference to a set of subgroups (optional). 14770b57cec5SDimitry Andric /// 14780b57cec5SDimitry Andric /// \code 14790b57cec5SDimitry Andric /// #ifdef GET_DIAG_TABLE 14800b57cec5SDimitry Andric /// {/* abi */ 159, /* DiagArray11 */ 19, /* Empty */ 0}, 14810b57cec5SDimitry Andric /// {/* aggregate-return */ 180, /* Empty */ 0, /* Empty */ 0}, 14820b57cec5SDimitry Andric /// {/* all */ 197, /* Empty */ 0, /* DiagSubGroup13 */ 3}, 14830b57cec5SDimitry Andric /// {/* deprecated */ 1981,/* DiagArray1 */ 348, /* DiagSubGroup3 */ 9}, 14840b57cec5SDimitry Andric /// #endif 14850b57cec5SDimitry Andric /// \endcode 14860b57cec5SDimitry Andric static void emitDiagTable(std::map<std::string, GroupInfo> &DiagsInGroup, 14870b57cec5SDimitry Andric RecordVec &DiagsInPedantic, 14880b57cec5SDimitry Andric RecordVec &GroupsInPedantic, 14890b57cec5SDimitry Andric StringToOffsetTable &GroupNames, raw_ostream &OS) { 14900b57cec5SDimitry Andric unsigned MaxLen = 0; 14910b57cec5SDimitry Andric 14920b57cec5SDimitry Andric for (auto const &I: DiagsInGroup) 14930b57cec5SDimitry Andric MaxLen = std::max(MaxLen, (unsigned)I.first.size()); 14940b57cec5SDimitry Andric 1495349cc55cSDimitry Andric OS << "\n#ifdef DIAG_ENTRY\n"; 14960b57cec5SDimitry Andric unsigned SubGroupIndex = 1, DiagArrayIndex = 1; 14970b57cec5SDimitry Andric for (auto const &I: DiagsInGroup) { 14980b57cec5SDimitry Andric // Group option string. 1499349cc55cSDimitry Andric OS << "DIAG_ENTRY("; 1500349cc55cSDimitry Andric OS << I.second.GroupName << " /* "; 1501349cc55cSDimitry Andric 15020b57cec5SDimitry Andric if (I.first.find_first_not_of("abcdefghijklmnopqrstuvwxyz" 15030b57cec5SDimitry Andric "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 15040b57cec5SDimitry Andric "0123456789!@#$%^*-+=:?") != 15050b57cec5SDimitry Andric std::string::npos) 15060b57cec5SDimitry Andric PrintFatalError("Invalid character in diagnostic group '" + I.first + 15070b57cec5SDimitry Andric "'"); 1508349cc55cSDimitry Andric OS << I.first << " */, "; 15090b57cec5SDimitry Andric // Store a pascal-style length byte at the beginning of the string. 15100b57cec5SDimitry Andric std::string Name = char(I.first.size()) + I.first; 15110b57cec5SDimitry Andric OS << GroupNames.GetOrAddStringOffset(Name, false) << ", "; 15120b57cec5SDimitry Andric 15130b57cec5SDimitry Andric // Special handling for 'pedantic'. 15140b57cec5SDimitry Andric const bool IsPedantic = I.first == "pedantic"; 15150b57cec5SDimitry Andric 15160b57cec5SDimitry Andric // Diagnostics in the group. 15170b57cec5SDimitry Andric const std::vector<const Record *> &V = I.second.DiagsInGroup; 15180b57cec5SDimitry Andric const bool hasDiags = 15190b57cec5SDimitry Andric !V.empty() || (IsPedantic && !DiagsInPedantic.empty()); 15200b57cec5SDimitry Andric if (hasDiags) { 15210b57cec5SDimitry Andric OS << "/* DiagArray" << I.second.IDNo << " */ " << DiagArrayIndex 15220b57cec5SDimitry Andric << ", "; 15230b57cec5SDimitry Andric if (IsPedantic) 15240b57cec5SDimitry Andric DiagArrayIndex += DiagsInPedantic.size(); 15250b57cec5SDimitry Andric DiagArrayIndex += V.size() + 1; 15260b57cec5SDimitry Andric } else { 1527349cc55cSDimitry Andric OS << "0, "; 15280b57cec5SDimitry Andric } 15290b57cec5SDimitry Andric 15300b57cec5SDimitry Andric // Subgroups. 15310b57cec5SDimitry Andric const std::vector<std::string> &SubGroups = I.second.SubGroups; 15320b57cec5SDimitry Andric const bool hasSubGroups = 15330b57cec5SDimitry Andric !SubGroups.empty() || (IsPedantic && !GroupsInPedantic.empty()); 15340b57cec5SDimitry Andric if (hasSubGroups) { 153581ad6265SDimitry Andric OS << "/* DiagSubGroup" << I.second.IDNo << " */ " << SubGroupIndex 153681ad6265SDimitry Andric << ", "; 15370b57cec5SDimitry Andric if (IsPedantic) 15380b57cec5SDimitry Andric SubGroupIndex += GroupsInPedantic.size(); 15390b57cec5SDimitry Andric SubGroupIndex += SubGroups.size() + 1; 15400b57cec5SDimitry Andric } else { 154181ad6265SDimitry Andric OS << "0, "; 15420b57cec5SDimitry Andric } 15430b57cec5SDimitry Andric 154481ad6265SDimitry Andric std::string Documentation = I.second.Defs.back() 154581ad6265SDimitry Andric ->getValue("Documentation") 154681ad6265SDimitry Andric ->getValue() 154781ad6265SDimitry Andric ->getAsUnquotedString(); 154881ad6265SDimitry Andric 154981ad6265SDimitry Andric OS << "R\"(" << StringRef(Documentation).trim() << ")\""; 155081ad6265SDimitry Andric 1551349cc55cSDimitry Andric OS << ")\n"; 15520b57cec5SDimitry Andric } 1553349cc55cSDimitry Andric OS << "#endif // DIAG_ENTRY\n\n"; 15540b57cec5SDimitry Andric } 15550b57cec5SDimitry Andric 15560b57cec5SDimitry Andric /// Emit the table of diagnostic categories. 15570b57cec5SDimitry Andric /// 15580b57cec5SDimitry Andric /// The table has the form of macro calls that have two parameters. The 15590b57cec5SDimitry Andric /// category's name as well as an enum that represents the category. The 15600b57cec5SDimitry Andric /// table can be used by defining the macro 'CATEGORY' and including this 15610b57cec5SDimitry Andric /// table right after. 15620b57cec5SDimitry Andric /// 15630b57cec5SDimitry Andric /// \code 15640b57cec5SDimitry Andric /// #ifdef GET_CATEGORY_TABLE 15650b57cec5SDimitry Andric /// CATEGORY("Semantic Issue", DiagCat_Semantic_Issue) 15660b57cec5SDimitry Andric /// CATEGORY("Lambda Issue", DiagCat_Lambda_Issue) 15670b57cec5SDimitry Andric /// #endif 15680b57cec5SDimitry Andric /// \endcode 15690b57cec5SDimitry Andric static void emitCategoryTable(RecordKeeper &Records, raw_ostream &OS) { 15700b57cec5SDimitry Andric DiagCategoryIDMap CategoriesByID(Records); 15710b57cec5SDimitry Andric OS << "\n#ifdef GET_CATEGORY_TABLE\n"; 15720b57cec5SDimitry Andric for (auto const &C : CategoriesByID) 15730b57cec5SDimitry Andric OS << "CATEGORY(\"" << C << "\", " << getDiagCategoryEnum(C) << ")\n"; 15740b57cec5SDimitry Andric OS << "#endif // GET_CATEGORY_TABLE\n\n"; 15750b57cec5SDimitry Andric } 15760b57cec5SDimitry Andric 1577a7dea167SDimitry Andric void clang::EmitClangDiagGroups(RecordKeeper &Records, raw_ostream &OS) { 15780b57cec5SDimitry Andric // Compute a mapping from a DiagGroup to all of its parents. 15790b57cec5SDimitry Andric DiagGroupParentMap DGParentMap(Records); 15800b57cec5SDimitry Andric 15810b57cec5SDimitry Andric std::vector<Record *> Diags = Records.getAllDerivedDefinitions("Diagnostic"); 15820b57cec5SDimitry Andric 15830b57cec5SDimitry Andric std::vector<Record *> DiagGroups = 15840b57cec5SDimitry Andric Records.getAllDerivedDefinitions("DiagGroup"); 15850b57cec5SDimitry Andric 15860b57cec5SDimitry Andric std::map<std::string, GroupInfo> DiagsInGroup; 15870b57cec5SDimitry Andric groupDiagnostics(Diags, DiagGroups, DiagsInGroup); 15880b57cec5SDimitry Andric 15890b57cec5SDimitry Andric // All extensions are implicitly in the "pedantic" group. Record the 15900b57cec5SDimitry Andric // implicit set of groups in the "pedantic" group, and use this information 15910b57cec5SDimitry Andric // later when emitting the group information for Pedantic. 15920b57cec5SDimitry Andric RecordVec DiagsInPedantic; 15930b57cec5SDimitry Andric RecordVec GroupsInPedantic; 15940b57cec5SDimitry Andric InferPedantic inferPedantic(DGParentMap, Diags, DiagGroups, DiagsInGroup); 15950b57cec5SDimitry Andric inferPedantic.compute(&DiagsInPedantic, &GroupsInPedantic); 15960b57cec5SDimitry Andric 15970b57cec5SDimitry Andric StringToOffsetTable GroupNames; 15980b57cec5SDimitry Andric for (std::map<std::string, GroupInfo>::const_iterator 15990b57cec5SDimitry Andric I = DiagsInGroup.begin(), 16000b57cec5SDimitry Andric E = DiagsInGroup.end(); 16010b57cec5SDimitry Andric I != E; ++I) { 16020b57cec5SDimitry Andric // Store a pascal-style length byte at the beginning of the string. 16030b57cec5SDimitry Andric std::string Name = char(I->first.size()) + I->first; 16040b57cec5SDimitry Andric GroupNames.GetOrAddStringOffset(Name, false); 16050b57cec5SDimitry Andric } 16060b57cec5SDimitry Andric 16070b57cec5SDimitry Andric emitAllDiagArrays(DiagsInGroup, DiagsInPedantic, GroupsInPedantic, GroupNames, 16080b57cec5SDimitry Andric OS); 16090b57cec5SDimitry Andric emitDiagTable(DiagsInGroup, DiagsInPedantic, GroupsInPedantic, GroupNames, 16100b57cec5SDimitry Andric OS); 16110b57cec5SDimitry Andric emitCategoryTable(Records, OS); 16120b57cec5SDimitry Andric } 16130b57cec5SDimitry Andric 16140b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 16150b57cec5SDimitry Andric // Diagnostic name index generation 16160b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 16170b57cec5SDimitry Andric 16180b57cec5SDimitry Andric namespace { 16190b57cec5SDimitry Andric struct RecordIndexElement 16200b57cec5SDimitry Andric { 16210b57cec5SDimitry Andric RecordIndexElement() {} 16225ffd83dbSDimitry Andric explicit RecordIndexElement(Record const &R) 16235ffd83dbSDimitry Andric : Name(std::string(R.getName())) {} 16240b57cec5SDimitry Andric 16250b57cec5SDimitry Andric std::string Name; 16260b57cec5SDimitry Andric }; 16270b57cec5SDimitry Andric } // end anonymous namespace. 16280b57cec5SDimitry Andric 1629a7dea167SDimitry Andric void clang::EmitClangDiagsIndexName(RecordKeeper &Records, raw_ostream &OS) { 16300b57cec5SDimitry Andric const std::vector<Record*> &Diags = 16310b57cec5SDimitry Andric Records.getAllDerivedDefinitions("Diagnostic"); 16320b57cec5SDimitry Andric 16330b57cec5SDimitry Andric std::vector<RecordIndexElement> Index; 16340b57cec5SDimitry Andric Index.reserve(Diags.size()); 16350b57cec5SDimitry Andric for (unsigned i = 0, e = Diags.size(); i != e; ++i) { 16360b57cec5SDimitry Andric const Record &R = *(Diags[i]); 16370b57cec5SDimitry Andric Index.push_back(RecordIndexElement(R)); 16380b57cec5SDimitry Andric } 16390b57cec5SDimitry Andric 16400b57cec5SDimitry Andric llvm::sort(Index, 16410b57cec5SDimitry Andric [](const RecordIndexElement &Lhs, const RecordIndexElement &Rhs) { 16420b57cec5SDimitry Andric return Lhs.Name < Rhs.Name; 16430b57cec5SDimitry Andric }); 16440b57cec5SDimitry Andric 16450b57cec5SDimitry Andric for (unsigned i = 0, e = Index.size(); i != e; ++i) { 16460b57cec5SDimitry Andric const RecordIndexElement &R = Index[i]; 16470b57cec5SDimitry Andric 16480b57cec5SDimitry Andric OS << "DIAG_NAME_INDEX(" << R.Name << ")\n"; 16490b57cec5SDimitry Andric } 16500b57cec5SDimitry Andric } 16510b57cec5SDimitry Andric 16520b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 16530b57cec5SDimitry Andric // Diagnostic documentation generation 16540b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 16550b57cec5SDimitry Andric 16560b57cec5SDimitry Andric namespace docs { 16570b57cec5SDimitry Andric namespace { 16580b57cec5SDimitry Andric 16590b57cec5SDimitry Andric bool isRemarkGroup(const Record *DiagGroup, 16600b57cec5SDimitry Andric const std::map<std::string, GroupInfo> &DiagsInGroup) { 16610b57cec5SDimitry Andric bool AnyRemarks = false, AnyNonRemarks = false; 16620b57cec5SDimitry Andric 16630b57cec5SDimitry Andric std::function<void(StringRef)> Visit = [&](StringRef GroupName) { 16645ffd83dbSDimitry Andric auto &GroupInfo = DiagsInGroup.find(std::string(GroupName))->second; 16650b57cec5SDimitry Andric for (const Record *Diag : GroupInfo.DiagsInGroup) 16660b57cec5SDimitry Andric (isRemark(*Diag) ? AnyRemarks : AnyNonRemarks) = true; 16670b57cec5SDimitry Andric for (const auto &Name : GroupInfo.SubGroups) 16680b57cec5SDimitry Andric Visit(Name); 16690b57cec5SDimitry Andric }; 16700b57cec5SDimitry Andric Visit(DiagGroup->getValueAsString("GroupName")); 16710b57cec5SDimitry Andric 16720b57cec5SDimitry Andric if (AnyRemarks && AnyNonRemarks) 16730b57cec5SDimitry Andric PrintFatalError( 16740b57cec5SDimitry Andric DiagGroup->getLoc(), 16750b57cec5SDimitry Andric "Diagnostic group contains both remark and non-remark diagnostics"); 16760b57cec5SDimitry Andric return AnyRemarks; 16770b57cec5SDimitry Andric } 16780b57cec5SDimitry Andric 16790b57cec5SDimitry Andric std::string getDefaultSeverity(const Record *Diag) { 16805ffd83dbSDimitry Andric return std::string( 16815ffd83dbSDimitry Andric Diag->getValueAsDef("DefaultSeverity")->getValueAsString("Name")); 16820b57cec5SDimitry Andric } 16830b57cec5SDimitry Andric 16840b57cec5SDimitry Andric std::set<std::string> 16850b57cec5SDimitry Andric getDefaultSeverities(const Record *DiagGroup, 16860b57cec5SDimitry Andric const std::map<std::string, GroupInfo> &DiagsInGroup) { 16870b57cec5SDimitry Andric std::set<std::string> States; 16880b57cec5SDimitry Andric 16890b57cec5SDimitry Andric std::function<void(StringRef)> Visit = [&](StringRef GroupName) { 16905ffd83dbSDimitry Andric auto &GroupInfo = DiagsInGroup.find(std::string(GroupName))->second; 16910b57cec5SDimitry Andric for (const Record *Diag : GroupInfo.DiagsInGroup) 16920b57cec5SDimitry Andric States.insert(getDefaultSeverity(Diag)); 16930b57cec5SDimitry Andric for (const auto &Name : GroupInfo.SubGroups) 16940b57cec5SDimitry Andric Visit(Name); 16950b57cec5SDimitry Andric }; 16960b57cec5SDimitry Andric Visit(DiagGroup->getValueAsString("GroupName")); 16970b57cec5SDimitry Andric return States; 16980b57cec5SDimitry Andric } 16990b57cec5SDimitry Andric 17000b57cec5SDimitry Andric void writeHeader(StringRef Str, raw_ostream &OS, char Kind = '-') { 17010b57cec5SDimitry Andric OS << Str << "\n" << std::string(Str.size(), Kind) << "\n"; 17020b57cec5SDimitry Andric } 17030b57cec5SDimitry Andric 17040b57cec5SDimitry Andric void writeDiagnosticText(DiagnosticTextBuilder &Builder, const Record *R, 17050b57cec5SDimitry Andric StringRef Role, raw_ostream &OS) { 1706*bdd1243dSDimitry Andric StringRef Text = R->getValueAsString("Summary"); 17070b57cec5SDimitry Andric if (Text == "%0") 17080b57cec5SDimitry Andric OS << "The text of this diagnostic is not controlled by Clang.\n\n"; 17090b57cec5SDimitry Andric else { 17100b57cec5SDimitry Andric std::vector<std::string> Out = Builder.buildForDocumentation(Role, R); 17110b57cec5SDimitry Andric for (auto &Line : Out) 17120b57cec5SDimitry Andric OS << Line << "\n"; 17130b57cec5SDimitry Andric OS << "\n"; 17140b57cec5SDimitry Andric } 17150b57cec5SDimitry Andric } 17160b57cec5SDimitry Andric 17170b57cec5SDimitry Andric } // namespace 17180b57cec5SDimitry Andric } // namespace docs 17190b57cec5SDimitry Andric 1720a7dea167SDimitry Andric void clang::EmitClangDiagDocs(RecordKeeper &Records, raw_ostream &OS) { 17210b57cec5SDimitry Andric using namespace docs; 17220b57cec5SDimitry Andric 17230b57cec5SDimitry Andric // Get the documentation introduction paragraph. 17240b57cec5SDimitry Andric const Record *Documentation = Records.getDef("GlobalDocumentation"); 17250b57cec5SDimitry Andric if (!Documentation) { 17260b57cec5SDimitry Andric PrintFatalError("The Documentation top-level definition is missing, " 17270b57cec5SDimitry Andric "no documentation will be generated."); 17280b57cec5SDimitry Andric return; 17290b57cec5SDimitry Andric } 17300b57cec5SDimitry Andric 17310b57cec5SDimitry Andric OS << Documentation->getValueAsString("Intro") << "\n"; 17320b57cec5SDimitry Andric 17330b57cec5SDimitry Andric DiagnosticTextBuilder Builder(Records); 17340b57cec5SDimitry Andric 17350b57cec5SDimitry Andric std::vector<Record*> Diags = 17360b57cec5SDimitry Andric Records.getAllDerivedDefinitions("Diagnostic"); 17370b57cec5SDimitry Andric 17380b57cec5SDimitry Andric std::vector<Record*> DiagGroups = 17390b57cec5SDimitry Andric Records.getAllDerivedDefinitions("DiagGroup"); 17400b57cec5SDimitry Andric llvm::sort(DiagGroups, diagGroupBeforeByName); 17410b57cec5SDimitry Andric 17420b57cec5SDimitry Andric DiagGroupParentMap DGParentMap(Records); 17430b57cec5SDimitry Andric 17440b57cec5SDimitry Andric std::map<std::string, GroupInfo> DiagsInGroup; 17450b57cec5SDimitry Andric groupDiagnostics(Diags, DiagGroups, DiagsInGroup); 17460b57cec5SDimitry Andric 17470b57cec5SDimitry Andric // Compute the set of diagnostics that are in -Wpedantic. 17480b57cec5SDimitry Andric { 17490b57cec5SDimitry Andric RecordSet DiagsInPedanticSet; 17500b57cec5SDimitry Andric RecordSet GroupsInPedanticSet; 17510b57cec5SDimitry Andric InferPedantic inferPedantic(DGParentMap, Diags, DiagGroups, DiagsInGroup); 17520b57cec5SDimitry Andric inferPedantic.compute(&DiagsInPedanticSet, &GroupsInPedanticSet); 17530b57cec5SDimitry Andric auto &PedDiags = DiagsInGroup["pedantic"]; 17540b57cec5SDimitry Andric // Put the diagnostics into a deterministic order. 17550b57cec5SDimitry Andric RecordVec DiagsInPedantic(DiagsInPedanticSet.begin(), 17560b57cec5SDimitry Andric DiagsInPedanticSet.end()); 17570b57cec5SDimitry Andric RecordVec GroupsInPedantic(GroupsInPedanticSet.begin(), 17580b57cec5SDimitry Andric GroupsInPedanticSet.end()); 17590b57cec5SDimitry Andric llvm::sort(DiagsInPedantic, beforeThanCompare); 17600b57cec5SDimitry Andric llvm::sort(GroupsInPedantic, beforeThanCompare); 17610b57cec5SDimitry Andric PedDiags.DiagsInGroup.insert(PedDiags.DiagsInGroup.end(), 17620b57cec5SDimitry Andric DiagsInPedantic.begin(), 17630b57cec5SDimitry Andric DiagsInPedantic.end()); 17640b57cec5SDimitry Andric for (auto *Group : GroupsInPedantic) 17655ffd83dbSDimitry Andric PedDiags.SubGroups.push_back( 17665ffd83dbSDimitry Andric std::string(Group->getValueAsString("GroupName"))); 17670b57cec5SDimitry Andric } 17680b57cec5SDimitry Andric 17690b57cec5SDimitry Andric // FIXME: Write diagnostic categories and link to diagnostic groups in each. 17700b57cec5SDimitry Andric 17710b57cec5SDimitry Andric // Write out the diagnostic groups. 17720b57cec5SDimitry Andric for (const Record *G : DiagGroups) { 17730b57cec5SDimitry Andric bool IsRemarkGroup = isRemarkGroup(G, DiagsInGroup); 17745ffd83dbSDimitry Andric auto &GroupInfo = 17755ffd83dbSDimitry Andric DiagsInGroup[std::string(G->getValueAsString("GroupName"))]; 17760b57cec5SDimitry Andric bool IsSynonym = GroupInfo.DiagsInGroup.empty() && 17770b57cec5SDimitry Andric GroupInfo.SubGroups.size() == 1; 17780b57cec5SDimitry Andric 17790b57cec5SDimitry Andric writeHeader(((IsRemarkGroup ? "-R" : "-W") + 17800b57cec5SDimitry Andric G->getValueAsString("GroupName")).str(), 17810b57cec5SDimitry Andric OS); 17820b57cec5SDimitry Andric 17830b57cec5SDimitry Andric if (!IsSynonym) { 17840b57cec5SDimitry Andric // FIXME: Ideally, all the diagnostics in a group should have the same 17850b57cec5SDimitry Andric // default state, but that is not currently the case. 17860b57cec5SDimitry Andric auto DefaultSeverities = getDefaultSeverities(G, DiagsInGroup); 17870b57cec5SDimitry Andric if (!DefaultSeverities.empty() && !DefaultSeverities.count("Ignored")) { 17880b57cec5SDimitry Andric bool AnyNonErrors = DefaultSeverities.count("Warning") || 17890b57cec5SDimitry Andric DefaultSeverities.count("Remark"); 17900b57cec5SDimitry Andric if (!AnyNonErrors) 17910b57cec5SDimitry Andric OS << "This diagnostic is an error by default, but the flag ``-Wno-" 17920b57cec5SDimitry Andric << G->getValueAsString("GroupName") << "`` can be used to disable " 17930b57cec5SDimitry Andric << "the error.\n\n"; 17940b57cec5SDimitry Andric else 17950b57cec5SDimitry Andric OS << "This diagnostic is enabled by default.\n\n"; 17960b57cec5SDimitry Andric } else if (DefaultSeverities.size() > 1) { 17970b57cec5SDimitry Andric OS << "Some of the diagnostics controlled by this flag are enabled " 17980b57cec5SDimitry Andric << "by default.\n\n"; 17990b57cec5SDimitry Andric } 18000b57cec5SDimitry Andric } 18010b57cec5SDimitry Andric 18020b57cec5SDimitry Andric if (!GroupInfo.SubGroups.empty()) { 18030b57cec5SDimitry Andric if (IsSynonym) 18040b57cec5SDimitry Andric OS << "Synonym for "; 18050b57cec5SDimitry Andric else if (GroupInfo.DiagsInGroup.empty()) 18060b57cec5SDimitry Andric OS << "Controls "; 18070b57cec5SDimitry Andric else 18080b57cec5SDimitry Andric OS << "Also controls "; 18090b57cec5SDimitry Andric 18100b57cec5SDimitry Andric bool First = true; 18110b57cec5SDimitry Andric llvm::sort(GroupInfo.SubGroups); 18120b57cec5SDimitry Andric for (const auto &Name : GroupInfo.SubGroups) { 18130b57cec5SDimitry Andric if (!First) OS << ", "; 18140b57cec5SDimitry Andric OS << "`" << (IsRemarkGroup ? "-R" : "-W") << Name << "`_"; 18150b57cec5SDimitry Andric First = false; 18160b57cec5SDimitry Andric } 18170b57cec5SDimitry Andric OS << ".\n\n"; 18180b57cec5SDimitry Andric } 18190b57cec5SDimitry Andric 18200b57cec5SDimitry Andric if (!GroupInfo.DiagsInGroup.empty()) { 18210b57cec5SDimitry Andric OS << "**Diagnostic text:**\n\n"; 18220b57cec5SDimitry Andric for (const Record *D : GroupInfo.DiagsInGroup) { 18230b57cec5SDimitry Andric auto Severity = getDefaultSeverity(D); 18240b57cec5SDimitry Andric Severity[0] = tolower(Severity[0]); 18250b57cec5SDimitry Andric if (Severity == "ignored") 18260b57cec5SDimitry Andric Severity = IsRemarkGroup ? "remark" : "warning"; 18270b57cec5SDimitry Andric 18280b57cec5SDimitry Andric writeDiagnosticText(Builder, D, Severity, OS); 18290b57cec5SDimitry Andric } 18300b57cec5SDimitry Andric } 18310b57cec5SDimitry Andric 18320b57cec5SDimitry Andric auto Doc = G->getValueAsString("Documentation"); 18330b57cec5SDimitry Andric if (!Doc.empty()) 18340b57cec5SDimitry Andric OS << Doc; 18350b57cec5SDimitry Andric else if (GroupInfo.SubGroups.empty() && GroupInfo.DiagsInGroup.empty()) 18360b57cec5SDimitry Andric OS << "This diagnostic flag exists for GCC compatibility, and has no " 18370b57cec5SDimitry Andric "effect in Clang.\n"; 18380b57cec5SDimitry Andric OS << "\n"; 18390b57cec5SDimitry Andric } 18400b57cec5SDimitry Andric } 1841