ClangDiagnosticsEmitter.cpp 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  1. //=- ClangDiagnosticsEmitter.cpp - Generate Clang diagnostics tables -*- C++ -*-
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // These tablegen backends emit Clang diagnostics tables.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "TableGenBackends.h"
  13. #include "llvm/ADT/DenseSet.h"
  14. #include "llvm/ADT/PointerUnion.h"
  15. #include "llvm/ADT/STLExtras.h"
  16. #include "llvm/ADT/SmallPtrSet.h"
  17. #include "llvm/ADT/SmallString.h"
  18. #include "llvm/ADT/SmallVector.h"
  19. #include "llvm/ADT/StringMap.h"
  20. #include "llvm/ADT/StringSwitch.h"
  21. #include "llvm/ADT/Twine.h"
  22. #include "llvm/Support/Casting.h"
  23. #include "llvm/TableGen/Error.h"
  24. #include "llvm/TableGen/Record.h"
  25. #include "llvm/TableGen/StringToOffsetTable.h"
  26. #include "llvm/TableGen/TableGenBackend.h"
  27. #include <algorithm>
  28. #include <cctype>
  29. #include <functional>
  30. #include <map>
  31. #include <optional>
  32. #include <set>
  33. using namespace llvm;
  34. //===----------------------------------------------------------------------===//
  35. // Diagnostic category computation code.
  36. //===----------------------------------------------------------------------===//
  37. namespace {
  38. class DiagGroupParentMap {
  39. RecordKeeper &Records;
  40. std::map<const Record*, std::vector<Record*> > Mapping;
  41. public:
  42. DiagGroupParentMap(RecordKeeper &records) : Records(records) {
  43. std::vector<Record*> DiagGroups
  44. = Records.getAllDerivedDefinitions("DiagGroup");
  45. for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
  46. std::vector<Record*> SubGroups =
  47. DiagGroups[i]->getValueAsListOfDefs("SubGroups");
  48. for (unsigned j = 0, e = SubGroups.size(); j != e; ++j)
  49. Mapping[SubGroups[j]].push_back(DiagGroups[i]);
  50. }
  51. }
  52. const std::vector<Record*> &getParents(const Record *Group) {
  53. return Mapping[Group];
  54. }
  55. };
  56. } // end anonymous namespace.
  57. static std::string
  58. getCategoryFromDiagGroup(const Record *Group,
  59. DiagGroupParentMap &DiagGroupParents) {
  60. // If the DiagGroup has a category, return it.
  61. std::string CatName = std::string(Group->getValueAsString("CategoryName"));
  62. if (!CatName.empty()) return CatName;
  63. // The diag group may the subgroup of one or more other diagnostic groups,
  64. // check these for a category as well.
  65. const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
  66. for (unsigned i = 0, e = Parents.size(); i != e; ++i) {
  67. CatName = getCategoryFromDiagGroup(Parents[i], DiagGroupParents);
  68. if (!CatName.empty()) return CatName;
  69. }
  70. return "";
  71. }
  72. /// getDiagnosticCategory - Return the category that the specified diagnostic
  73. /// lives in.
  74. static std::string getDiagnosticCategory(const Record *R,
  75. DiagGroupParentMap &DiagGroupParents) {
  76. // If the diagnostic is in a group, and that group has a category, use it.
  77. if (DefInit *Group = dyn_cast<DefInit>(R->getValueInit("Group"))) {
  78. // Check the diagnostic's diag group for a category.
  79. std::string CatName = getCategoryFromDiagGroup(Group->getDef(),
  80. DiagGroupParents);
  81. if (!CatName.empty()) return CatName;
  82. }
  83. // If the diagnostic itself has a category, get it.
  84. return std::string(R->getValueAsString("CategoryName"));
  85. }
  86. namespace {
  87. class DiagCategoryIDMap {
  88. RecordKeeper &Records;
  89. StringMap<unsigned> CategoryIDs;
  90. std::vector<std::string> CategoryStrings;
  91. public:
  92. DiagCategoryIDMap(RecordKeeper &records) : Records(records) {
  93. DiagGroupParentMap ParentInfo(Records);
  94. // The zero'th category is "".
  95. CategoryStrings.push_back("");
  96. CategoryIDs[""] = 0;
  97. std::vector<Record*> Diags =
  98. Records.getAllDerivedDefinitions("Diagnostic");
  99. for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
  100. std::string Category = getDiagnosticCategory(Diags[i], ParentInfo);
  101. if (Category.empty()) continue; // Skip diags with no category.
  102. unsigned &ID = CategoryIDs[Category];
  103. if (ID != 0) continue; // Already seen.
  104. ID = CategoryStrings.size();
  105. CategoryStrings.push_back(Category);
  106. }
  107. }
  108. unsigned getID(StringRef CategoryString) {
  109. return CategoryIDs[CategoryString];
  110. }
  111. typedef std::vector<std::string>::const_iterator const_iterator;
  112. const_iterator begin() const { return CategoryStrings.begin(); }
  113. const_iterator end() const { return CategoryStrings.end(); }
  114. };
  115. struct GroupInfo {
  116. llvm::StringRef GroupName;
  117. std::vector<const Record*> DiagsInGroup;
  118. std::vector<std::string> SubGroups;
  119. unsigned IDNo;
  120. llvm::SmallVector<const Record *, 1> Defs;
  121. GroupInfo() : IDNo(0) {}
  122. };
  123. } // end anonymous namespace.
  124. static bool beforeThanCompare(const Record *LHS, const Record *RHS) {
  125. assert(!LHS->getLoc().empty() && !RHS->getLoc().empty());
  126. return
  127. LHS->getLoc().front().getPointer() < RHS->getLoc().front().getPointer();
  128. }
  129. static bool diagGroupBeforeByName(const Record *LHS, const Record *RHS) {
  130. return LHS->getValueAsString("GroupName") <
  131. RHS->getValueAsString("GroupName");
  132. }
  133. /// Invert the 1-[0/1] mapping of diags to group into a one to many
  134. /// mapping of groups to diags in the group.
  135. static void groupDiagnostics(const std::vector<Record*> &Diags,
  136. const std::vector<Record*> &DiagGroups,
  137. std::map<std::string, GroupInfo> &DiagsInGroup) {
  138. for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
  139. const Record *R = Diags[i];
  140. DefInit *DI = dyn_cast<DefInit>(R->getValueInit("Group"));
  141. if (!DI)
  142. continue;
  143. assert(R->getValueAsDef("Class")->getName() != "CLASS_NOTE" &&
  144. "Note can't be in a DiagGroup");
  145. std::string GroupName =
  146. std::string(DI->getDef()->getValueAsString("GroupName"));
  147. DiagsInGroup[GroupName].DiagsInGroup.push_back(R);
  148. }
  149. // Add all DiagGroup's to the DiagsInGroup list to make sure we pick up empty
  150. // groups (these are warnings that GCC supports that clang never produces).
  151. for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
  152. Record *Group = DiagGroups[i];
  153. GroupInfo &GI =
  154. DiagsInGroup[std::string(Group->getValueAsString("GroupName"))];
  155. GI.GroupName = Group->getName();
  156. GI.Defs.push_back(Group);
  157. std::vector<Record*> SubGroups = Group->getValueAsListOfDefs("SubGroups");
  158. for (unsigned j = 0, e = SubGroups.size(); j != e; ++j)
  159. GI.SubGroups.push_back(
  160. std::string(SubGroups[j]->getValueAsString("GroupName")));
  161. }
  162. // Assign unique ID numbers to the groups.
  163. unsigned IDNo = 0;
  164. for (std::map<std::string, GroupInfo>::iterator
  165. I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I, ++IDNo)
  166. I->second.IDNo = IDNo;
  167. // Warn if the same group is defined more than once (including implicitly).
  168. for (auto &Group : DiagsInGroup) {
  169. if (Group.second.Defs.size() == 1 &&
  170. (!Group.second.Defs.front()->isAnonymous() ||
  171. Group.second.DiagsInGroup.size() <= 1))
  172. continue;
  173. bool First = true;
  174. for (const Record *Def : Group.second.Defs) {
  175. // Skip implicit definitions from diagnostics; we'll report those
  176. // separately below.
  177. bool IsImplicit = false;
  178. for (const Record *Diag : Group.second.DiagsInGroup) {
  179. if (cast<DefInit>(Diag->getValueInit("Group"))->getDef() == Def) {
  180. IsImplicit = true;
  181. break;
  182. }
  183. }
  184. if (IsImplicit)
  185. continue;
  186. llvm::SMLoc Loc = Def->getLoc().front();
  187. if (First) {
  188. SrcMgr.PrintMessage(Loc, SourceMgr::DK_Error,
  189. Twine("group '") + Group.first +
  190. "' is defined more than once");
  191. First = false;
  192. } else {
  193. SrcMgr.PrintMessage(Loc, SourceMgr::DK_Note, "also defined here");
  194. }
  195. }
  196. for (const Record *Diag : Group.second.DiagsInGroup) {
  197. if (!cast<DefInit>(Diag->getValueInit("Group"))->getDef()->isAnonymous())
  198. continue;
  199. llvm::SMLoc Loc = Diag->getLoc().front();
  200. if (First) {
  201. SrcMgr.PrintMessage(Loc, SourceMgr::DK_Error,
  202. Twine("group '") + Group.first +
  203. "' is implicitly defined more than once");
  204. First = false;
  205. } else {
  206. SrcMgr.PrintMessage(Loc, SourceMgr::DK_Note,
  207. "also implicitly defined here");
  208. }
  209. }
  210. }
  211. }
  212. //===----------------------------------------------------------------------===//
  213. // Infer members of -Wpedantic.
  214. //===----------------------------------------------------------------------===//
  215. typedef std::vector<const Record *> RecordVec;
  216. typedef llvm::DenseSet<const Record *> RecordSet;
  217. typedef llvm::PointerUnion<RecordVec*, RecordSet*> VecOrSet;
  218. namespace {
  219. class InferPedantic {
  220. typedef llvm::DenseMap<const Record *,
  221. std::pair<unsigned, std::optional<unsigned>>>
  222. GMap;
  223. DiagGroupParentMap &DiagGroupParents;
  224. const std::vector<Record*> &Diags;
  225. const std::vector<Record*> DiagGroups;
  226. std::map<std::string, GroupInfo> &DiagsInGroup;
  227. llvm::DenseSet<const Record*> DiagsSet;
  228. GMap GroupCount;
  229. public:
  230. InferPedantic(DiagGroupParentMap &DiagGroupParents,
  231. const std::vector<Record*> &Diags,
  232. const std::vector<Record*> &DiagGroups,
  233. std::map<std::string, GroupInfo> &DiagsInGroup)
  234. : DiagGroupParents(DiagGroupParents),
  235. Diags(Diags),
  236. DiagGroups(DiagGroups),
  237. DiagsInGroup(DiagsInGroup) {}
  238. /// Compute the set of diagnostics and groups that are immediately
  239. /// in -Wpedantic.
  240. void compute(VecOrSet DiagsInPedantic,
  241. VecOrSet GroupsInPedantic);
  242. private:
  243. /// Determine whether a group is a subgroup of another group.
  244. bool isSubGroupOfGroup(const Record *Group,
  245. llvm::StringRef RootGroupName);
  246. /// Determine if the diagnostic is an extension.
  247. bool isExtension(const Record *Diag);
  248. /// Determine if the diagnostic is off by default.
  249. bool isOffByDefault(const Record *Diag);
  250. /// Increment the count for a group, and transitively marked
  251. /// parent groups when appropriate.
  252. void markGroup(const Record *Group);
  253. /// Return true if the diagnostic is in a pedantic group.
  254. bool groupInPedantic(const Record *Group, bool increment = false);
  255. };
  256. } // end anonymous namespace
  257. bool InferPedantic::isSubGroupOfGroup(const Record *Group,
  258. llvm::StringRef GName) {
  259. const std::string &GroupName =
  260. std::string(Group->getValueAsString("GroupName"));
  261. if (GName == GroupName)
  262. return true;
  263. const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
  264. for (unsigned i = 0, e = Parents.size(); i != e; ++i)
  265. if (isSubGroupOfGroup(Parents[i], GName))
  266. return true;
  267. return false;
  268. }
  269. /// Determine if the diagnostic is an extension.
  270. bool InferPedantic::isExtension(const Record *Diag) {
  271. const std::string &ClsName =
  272. std::string(Diag->getValueAsDef("Class")->getName());
  273. return ClsName == "CLASS_EXTENSION";
  274. }
  275. bool InferPedantic::isOffByDefault(const Record *Diag) {
  276. const std::string &DefSeverity = std::string(
  277. Diag->getValueAsDef("DefaultSeverity")->getValueAsString("Name"));
  278. return DefSeverity == "Ignored";
  279. }
  280. bool InferPedantic::groupInPedantic(const Record *Group, bool increment) {
  281. GMap::mapped_type &V = GroupCount[Group];
  282. // Lazily compute the threshold value for the group count.
  283. if (!V.second) {
  284. const GroupInfo &GI =
  285. DiagsInGroup[std::string(Group->getValueAsString("GroupName"))];
  286. V.second = GI.SubGroups.size() + GI.DiagsInGroup.size();
  287. }
  288. if (increment)
  289. ++V.first;
  290. // Consider a group in -Wpendatic IFF if has at least one diagnostic
  291. // or subgroup AND all of those diagnostics and subgroups are covered
  292. // by -Wpedantic via our computation.
  293. return V.first != 0 && V.first == *V.second;
  294. }
  295. void InferPedantic::markGroup(const Record *Group) {
  296. // If all the diagnostics and subgroups have been marked as being
  297. // covered by -Wpedantic, increment the count of parent groups. Once the
  298. // group's count is equal to the number of subgroups and diagnostics in
  299. // that group, we can safely add this group to -Wpedantic.
  300. if (groupInPedantic(Group, /* increment */ true)) {
  301. const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
  302. for (unsigned i = 0, e = Parents.size(); i != e; ++i)
  303. markGroup(Parents[i]);
  304. }
  305. }
  306. void InferPedantic::compute(VecOrSet DiagsInPedantic,
  307. VecOrSet GroupsInPedantic) {
  308. // All extensions that are not on by default are implicitly in the
  309. // "pedantic" group. For those that aren't explicitly included in -Wpedantic,
  310. // mark them for consideration to be included in -Wpedantic directly.
  311. for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
  312. Record *R = Diags[i];
  313. if (isExtension(R) && isOffByDefault(R)) {
  314. DiagsSet.insert(R);
  315. if (DefInit *Group = dyn_cast<DefInit>(R->getValueInit("Group"))) {
  316. const Record *GroupRec = Group->getDef();
  317. if (!isSubGroupOfGroup(GroupRec, "pedantic")) {
  318. markGroup(GroupRec);
  319. }
  320. }
  321. }
  322. }
  323. // Compute the set of diagnostics that are directly in -Wpedantic. We
  324. // march through Diags a second time to ensure the results are emitted
  325. // in deterministic order.
  326. for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
  327. Record *R = Diags[i];
  328. if (!DiagsSet.count(R))
  329. continue;
  330. // Check if the group is implicitly in -Wpedantic. If so,
  331. // the diagnostic should not be directly included in the -Wpedantic
  332. // diagnostic group.
  333. if (DefInit *Group = dyn_cast<DefInit>(R->getValueInit("Group")))
  334. if (groupInPedantic(Group->getDef()))
  335. continue;
  336. // The diagnostic is not included in a group that is (transitively) in
  337. // -Wpedantic. Include it in -Wpedantic directly.
  338. if (RecordVec *V = DiagsInPedantic.dyn_cast<RecordVec*>())
  339. V->push_back(R);
  340. else {
  341. DiagsInPedantic.get<RecordSet*>()->insert(R);
  342. }
  343. }
  344. if (!GroupsInPedantic)
  345. return;
  346. // Compute the set of groups that are directly in -Wpedantic. We
  347. // march through the groups to ensure the results are emitted
  348. /// in a deterministc order.
  349. for (unsigned i = 0, ei = DiagGroups.size(); i != ei; ++i) {
  350. Record *Group = DiagGroups[i];
  351. if (!groupInPedantic(Group))
  352. continue;
  353. const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
  354. bool AllParentsInPedantic =
  355. llvm::all_of(Parents, [&](Record *R) { return groupInPedantic(R); });
  356. // If all the parents are in -Wpedantic, this means that this diagnostic
  357. // group will be indirectly included by -Wpedantic already. In that
  358. // case, do not add it directly to -Wpedantic. If the group has no
  359. // parents, obviously it should go into -Wpedantic.
  360. if (Parents.size() > 0 && AllParentsInPedantic)
  361. continue;
  362. if (RecordVec *V = GroupsInPedantic.dyn_cast<RecordVec*>())
  363. V->push_back(Group);
  364. else {
  365. GroupsInPedantic.get<RecordSet*>()->insert(Group);
  366. }
  367. }
  368. }
  369. namespace {
  370. enum PieceKind {
  371. MultiPieceClass,
  372. TextPieceClass,
  373. PlaceholderPieceClass,
  374. SelectPieceClass,
  375. PluralPieceClass,
  376. DiffPieceClass,
  377. SubstitutionPieceClass,
  378. };
  379. enum ModifierType {
  380. MT_Unknown,
  381. MT_Placeholder,
  382. MT_Select,
  383. MT_Sub,
  384. MT_Plural,
  385. MT_Diff,
  386. MT_Ordinal,
  387. MT_S,
  388. MT_Q,
  389. MT_ObjCClass,
  390. MT_ObjCInstance,
  391. };
  392. static StringRef getModifierName(ModifierType MT) {
  393. switch (MT) {
  394. case MT_Select:
  395. return "select";
  396. case MT_Sub:
  397. return "sub";
  398. case MT_Diff:
  399. return "diff";
  400. case MT_Plural:
  401. return "plural";
  402. case MT_Ordinal:
  403. return "ordinal";
  404. case MT_S:
  405. return "s";
  406. case MT_Q:
  407. return "q";
  408. case MT_Placeholder:
  409. return "";
  410. case MT_ObjCClass:
  411. return "objcclass";
  412. case MT_ObjCInstance:
  413. return "objcinstance";
  414. case MT_Unknown:
  415. llvm_unreachable("invalid modifier type");
  416. }
  417. // Unhandled case
  418. llvm_unreachable("invalid modifier type");
  419. }
  420. struct Piece {
  421. // This type and its derived classes are move-only.
  422. Piece(PieceKind Kind) : ClassKind(Kind) {}
  423. Piece(Piece const &O) = delete;
  424. Piece &operator=(Piece const &) = delete;
  425. virtual ~Piece() {}
  426. PieceKind getPieceClass() const { return ClassKind; }
  427. static bool classof(const Piece *) { return true; }
  428. private:
  429. PieceKind ClassKind;
  430. };
  431. struct MultiPiece : Piece {
  432. MultiPiece() : Piece(MultiPieceClass) {}
  433. MultiPiece(std::vector<Piece *> Pieces)
  434. : Piece(MultiPieceClass), Pieces(std::move(Pieces)) {}
  435. std::vector<Piece *> Pieces;
  436. static bool classof(const Piece *P) {
  437. return P->getPieceClass() == MultiPieceClass;
  438. }
  439. };
  440. struct TextPiece : Piece {
  441. StringRef Role;
  442. std::string Text;
  443. TextPiece(StringRef Text, StringRef Role = "")
  444. : Piece(TextPieceClass), Role(Role), Text(Text.str()) {}
  445. static bool classof(const Piece *P) {
  446. return P->getPieceClass() == TextPieceClass;
  447. }
  448. };
  449. struct PlaceholderPiece : Piece {
  450. ModifierType Kind;
  451. int Index;
  452. PlaceholderPiece(ModifierType Kind, int Index)
  453. : Piece(PlaceholderPieceClass), Kind(Kind), Index(Index) {}
  454. static bool classof(const Piece *P) {
  455. return P->getPieceClass() == PlaceholderPieceClass;
  456. }
  457. };
  458. struct SelectPiece : Piece {
  459. protected:
  460. SelectPiece(PieceKind Kind, ModifierType ModKind)
  461. : Piece(Kind), ModKind(ModKind) {}
  462. public:
  463. SelectPiece(ModifierType ModKind) : SelectPiece(SelectPieceClass, ModKind) {}
  464. ModifierType ModKind;
  465. std::vector<Piece *> Options;
  466. int Index = 0;
  467. static bool classof(const Piece *P) {
  468. return P->getPieceClass() == SelectPieceClass ||
  469. P->getPieceClass() == PluralPieceClass;
  470. }
  471. };
  472. struct PluralPiece : SelectPiece {
  473. PluralPiece() : SelectPiece(PluralPieceClass, MT_Plural) {}
  474. std::vector<Piece *> OptionPrefixes;
  475. int Index = 0;
  476. static bool classof(const Piece *P) {
  477. return P->getPieceClass() == PluralPieceClass;
  478. }
  479. };
  480. struct DiffPiece : Piece {
  481. DiffPiece() : Piece(DiffPieceClass) {}
  482. Piece *Parts[4] = {};
  483. int Indexes[2] = {};
  484. static bool classof(const Piece *P) {
  485. return P->getPieceClass() == DiffPieceClass;
  486. }
  487. };
  488. struct SubstitutionPiece : Piece {
  489. SubstitutionPiece() : Piece(SubstitutionPieceClass) {}
  490. std::string Name;
  491. std::vector<int> Modifiers;
  492. static bool classof(const Piece *P) {
  493. return P->getPieceClass() == SubstitutionPieceClass;
  494. }
  495. };
  496. /// Diagnostic text, parsed into pieces.
  497. struct DiagnosticTextBuilder {
  498. DiagnosticTextBuilder(DiagnosticTextBuilder const &) = delete;
  499. DiagnosticTextBuilder &operator=(DiagnosticTextBuilder const &) = delete;
  500. DiagnosticTextBuilder(RecordKeeper &Records) {
  501. // Build up the list of substitution records.
  502. for (auto *S : Records.getAllDerivedDefinitions("TextSubstitution")) {
  503. EvaluatingRecordGuard Guard(&EvaluatingRecord, S);
  504. Substitutions.try_emplace(
  505. S->getName(), DiagText(*this, S->getValueAsString("Substitution")));
  506. }
  507. // Check that no diagnostic definitions have the same name as a
  508. // substitution.
  509. for (Record *Diag : Records.getAllDerivedDefinitions("Diagnostic")) {
  510. StringRef Name = Diag->getName();
  511. if (Substitutions.count(Name))
  512. llvm::PrintFatalError(
  513. Diag->getLoc(),
  514. "Diagnostic '" + Name +
  515. "' has same name as TextSubstitution definition");
  516. }
  517. }
  518. std::vector<std::string> buildForDocumentation(StringRef Role,
  519. const Record *R);
  520. std::string buildForDefinition(const Record *R);
  521. Piece *getSubstitution(SubstitutionPiece *S) const {
  522. auto It = Substitutions.find(S->Name);
  523. if (It == Substitutions.end())
  524. PrintFatalError("Failed to find substitution with name: " + S->Name);
  525. return It->second.Root;
  526. }
  527. [[noreturn]] void PrintFatalError(llvm::Twine const &Msg) const {
  528. assert(EvaluatingRecord && "not evaluating a record?");
  529. llvm::PrintFatalError(EvaluatingRecord->getLoc(), Msg);
  530. }
  531. private:
  532. struct DiagText {
  533. DiagnosticTextBuilder &Builder;
  534. std::vector<Piece *> AllocatedPieces;
  535. Piece *Root = nullptr;
  536. template <class T, class... Args> T *New(Args &&... args) {
  537. static_assert(std::is_base_of<Piece, T>::value, "must be piece");
  538. T *Mem = new T(std::forward<Args>(args)...);
  539. AllocatedPieces.push_back(Mem);
  540. return Mem;
  541. }
  542. DiagText(DiagnosticTextBuilder &Builder, StringRef Text)
  543. : Builder(Builder), Root(parseDiagText(Text, StopAt::End)) {}
  544. enum class StopAt {
  545. // Parse until the end of the string.
  546. End,
  547. // Additionally stop if we hit a non-nested '|' or '}'.
  548. PipeOrCloseBrace,
  549. // Additionally stop if we hit a non-nested '$'.
  550. Dollar,
  551. };
  552. Piece *parseDiagText(StringRef &Text, StopAt Stop);
  553. int parseModifier(StringRef &) const;
  554. public:
  555. DiagText(DiagText &&O) noexcept
  556. : Builder(O.Builder), AllocatedPieces(std::move(O.AllocatedPieces)),
  557. Root(O.Root) {
  558. O.Root = nullptr;
  559. }
  560. ~DiagText() {
  561. for (Piece *P : AllocatedPieces)
  562. delete P;
  563. }
  564. };
  565. private:
  566. const Record *EvaluatingRecord = nullptr;
  567. struct EvaluatingRecordGuard {
  568. EvaluatingRecordGuard(const Record **Dest, const Record *New)
  569. : Dest(Dest), Old(*Dest) {
  570. *Dest = New;
  571. }
  572. ~EvaluatingRecordGuard() { *Dest = Old; }
  573. const Record **Dest;
  574. const Record *Old;
  575. };
  576. StringMap<DiagText> Substitutions;
  577. };
  578. template <class Derived> struct DiagTextVisitor {
  579. using ModifierMappingsType = std::optional<std::vector<int>>;
  580. private:
  581. Derived &getDerived() { return static_cast<Derived &>(*this); }
  582. public:
  583. std::vector<int>
  584. getSubstitutionMappings(SubstitutionPiece *P,
  585. const ModifierMappingsType &Mappings) const {
  586. std::vector<int> NewMappings;
  587. for (int Idx : P->Modifiers)
  588. NewMappings.push_back(mapIndex(Idx, Mappings));
  589. return NewMappings;
  590. }
  591. struct SubstitutionContext {
  592. SubstitutionContext(DiagTextVisitor &Visitor, SubstitutionPiece *P)
  593. : Visitor(Visitor) {
  594. Substitution = Visitor.Builder.getSubstitution(P);
  595. OldMappings = std::move(Visitor.ModifierMappings);
  596. std::vector<int> NewMappings =
  597. Visitor.getSubstitutionMappings(P, OldMappings);
  598. Visitor.ModifierMappings = std::move(NewMappings);
  599. }
  600. ~SubstitutionContext() {
  601. Visitor.ModifierMappings = std::move(OldMappings);
  602. }
  603. private:
  604. DiagTextVisitor &Visitor;
  605. std::optional<std::vector<int>> OldMappings;
  606. public:
  607. Piece *Substitution;
  608. };
  609. public:
  610. DiagTextVisitor(DiagnosticTextBuilder &Builder) : Builder(Builder) {}
  611. void Visit(Piece *P) {
  612. switch (P->getPieceClass()) {
  613. #define CASE(T) \
  614. case T##PieceClass: \
  615. return getDerived().Visit##T(static_cast<T##Piece *>(P))
  616. CASE(Multi);
  617. CASE(Text);
  618. CASE(Placeholder);
  619. CASE(Select);
  620. CASE(Plural);
  621. CASE(Diff);
  622. CASE(Substitution);
  623. #undef CASE
  624. }
  625. }
  626. void VisitSubstitution(SubstitutionPiece *P) {
  627. SubstitutionContext Guard(*this, P);
  628. Visit(Guard.Substitution);
  629. }
  630. int mapIndex(int Idx,
  631. ModifierMappingsType const &ModifierMappings) const {
  632. if (!ModifierMappings)
  633. return Idx;
  634. if (ModifierMappings->size() <= static_cast<unsigned>(Idx))
  635. Builder.PrintFatalError("Modifier value '" + std::to_string(Idx) +
  636. "' is not valid for this mapping (has " +
  637. std::to_string(ModifierMappings->size()) +
  638. " mappings)");
  639. return (*ModifierMappings)[Idx];
  640. }
  641. int mapIndex(int Idx) const {
  642. return mapIndex(Idx, ModifierMappings);
  643. }
  644. protected:
  645. DiagnosticTextBuilder &Builder;
  646. ModifierMappingsType ModifierMappings;
  647. };
  648. void escapeRST(StringRef Str, std::string &Out) {
  649. for (auto K : Str) {
  650. if (StringRef("`*|_[]\\").count(K))
  651. Out.push_back('\\');
  652. Out.push_back(K);
  653. }
  654. }
  655. template <typename It> void padToSameLength(It Begin, It End) {
  656. size_t Width = 0;
  657. for (It I = Begin; I != End; ++I)
  658. Width = std::max(Width, I->size());
  659. for (It I = Begin; I != End; ++I)
  660. (*I) += std::string(Width - I->size(), ' ');
  661. }
  662. template <typename It> void makeTableRows(It Begin, It End) {
  663. if (Begin == End)
  664. return;
  665. padToSameLength(Begin, End);
  666. for (It I = Begin; I != End; ++I)
  667. *I = "|" + *I + "|";
  668. }
  669. void makeRowSeparator(std::string &Str) {
  670. for (char &K : Str)
  671. K = (K == '|' ? '+' : '-');
  672. }
  673. struct DiagTextDocPrinter : DiagTextVisitor<DiagTextDocPrinter> {
  674. using BaseTy = DiagTextVisitor<DiagTextDocPrinter>;
  675. DiagTextDocPrinter(DiagnosticTextBuilder &Builder,
  676. std::vector<std::string> &RST)
  677. : BaseTy(Builder), RST(RST) {}
  678. void gatherNodes(
  679. Piece *OrigP, const ModifierMappingsType &CurrentMappings,
  680. std::vector<std::pair<Piece *, ModifierMappingsType>> &Pieces) const {
  681. if (auto *Sub = dyn_cast<SubstitutionPiece>(OrigP)) {
  682. ModifierMappingsType NewMappings =
  683. getSubstitutionMappings(Sub, CurrentMappings);
  684. return gatherNodes(Builder.getSubstitution(Sub), NewMappings, Pieces);
  685. }
  686. if (auto *MD = dyn_cast<MultiPiece>(OrigP)) {
  687. for (Piece *Node : MD->Pieces)
  688. gatherNodes(Node, CurrentMappings, Pieces);
  689. return;
  690. }
  691. Pieces.push_back(std::make_pair(OrigP, CurrentMappings));
  692. }
  693. void VisitMulti(MultiPiece *P) {
  694. if (P->Pieces.empty()) {
  695. RST.push_back("");
  696. return;
  697. }
  698. if (P->Pieces.size() == 1)
  699. return Visit(P->Pieces[0]);
  700. // Flatten the list of nodes, replacing any substitution pieces with the
  701. // recursively flattened substituted node.
  702. std::vector<std::pair<Piece *, ModifierMappingsType>> Pieces;
  703. gatherNodes(P, ModifierMappings, Pieces);
  704. std::string EmptyLinePrefix;
  705. size_t Start = RST.size();
  706. bool HasMultipleLines = true;
  707. for (const std::pair<Piece *, ModifierMappingsType> &NodePair : Pieces) {
  708. std::vector<std::string> Lines;
  709. DiagTextDocPrinter Visitor{Builder, Lines};
  710. Visitor.ModifierMappings = NodePair.second;
  711. Visitor.Visit(NodePair.first);
  712. if (Lines.empty())
  713. continue;
  714. // We need a vertical separator if either this or the previous piece is a
  715. // multi-line piece, or this is the last piece.
  716. const char *Separator = (Lines.size() > 1 || HasMultipleLines) ? "|" : "";
  717. HasMultipleLines = Lines.size() > 1;
  718. if (Start + Lines.size() > RST.size())
  719. RST.resize(Start + Lines.size(), EmptyLinePrefix);
  720. padToSameLength(Lines.begin(), Lines.end());
  721. for (size_t I = 0; I != Lines.size(); ++I)
  722. RST[Start + I] += Separator + Lines[I];
  723. std::string Empty(Lines[0].size(), ' ');
  724. for (size_t I = Start + Lines.size(); I != RST.size(); ++I)
  725. RST[I] += Separator + Empty;
  726. EmptyLinePrefix += Separator + Empty;
  727. }
  728. for (size_t I = Start; I != RST.size(); ++I)
  729. RST[I] += "|";
  730. EmptyLinePrefix += "|";
  731. makeRowSeparator(EmptyLinePrefix);
  732. RST.insert(RST.begin() + Start, EmptyLinePrefix);
  733. RST.insert(RST.end(), EmptyLinePrefix);
  734. }
  735. void VisitText(TextPiece *P) {
  736. RST.push_back("");
  737. auto &S = RST.back();
  738. StringRef T = P->Text;
  739. while (!T.empty() && T.front() == ' ') {
  740. RST.back() += " |nbsp| ";
  741. T = T.drop_front();
  742. }
  743. std::string Suffix;
  744. while (!T.empty() && T.back() == ' ') {
  745. Suffix += " |nbsp| ";
  746. T = T.drop_back();
  747. }
  748. if (!T.empty()) {
  749. S += ':';
  750. S += P->Role;
  751. S += ":`";
  752. escapeRST(T, S);
  753. S += '`';
  754. }
  755. S += Suffix;
  756. }
  757. void VisitPlaceholder(PlaceholderPiece *P) {
  758. RST.push_back(std::string(":placeholder:`") +
  759. char('A' + mapIndex(P->Index)) + "`");
  760. }
  761. void VisitSelect(SelectPiece *P) {
  762. std::vector<size_t> SeparatorIndexes;
  763. SeparatorIndexes.push_back(RST.size());
  764. RST.emplace_back();
  765. for (auto *O : P->Options) {
  766. Visit(O);
  767. SeparatorIndexes.push_back(RST.size());
  768. RST.emplace_back();
  769. }
  770. makeTableRows(RST.begin() + SeparatorIndexes.front(),
  771. RST.begin() + SeparatorIndexes.back() + 1);
  772. for (size_t I : SeparatorIndexes)
  773. makeRowSeparator(RST[I]);
  774. }
  775. void VisitPlural(PluralPiece *P) { VisitSelect(P); }
  776. void VisitDiff(DiffPiece *P) {
  777. // Render %diff{a $ b $ c|d}e,f as %select{a %e b %f c|d}.
  778. PlaceholderPiece E(MT_Placeholder, P->Indexes[0]);
  779. PlaceholderPiece F(MT_Placeholder, P->Indexes[1]);
  780. MultiPiece FirstOption;
  781. FirstOption.Pieces.push_back(P->Parts[0]);
  782. FirstOption.Pieces.push_back(&E);
  783. FirstOption.Pieces.push_back(P->Parts[1]);
  784. FirstOption.Pieces.push_back(&F);
  785. FirstOption.Pieces.push_back(P->Parts[2]);
  786. SelectPiece Select(MT_Diff);
  787. Select.Options.push_back(&FirstOption);
  788. Select.Options.push_back(P->Parts[3]);
  789. VisitSelect(&Select);
  790. }
  791. std::vector<std::string> &RST;
  792. };
  793. struct DiagTextPrinter : DiagTextVisitor<DiagTextPrinter> {
  794. public:
  795. using BaseTy = DiagTextVisitor<DiagTextPrinter>;
  796. DiagTextPrinter(DiagnosticTextBuilder &Builder, std::string &Result)
  797. : BaseTy(Builder), Result(Result) {}
  798. void VisitMulti(MultiPiece *P) {
  799. for (auto *Child : P->Pieces)
  800. Visit(Child);
  801. }
  802. void VisitText(TextPiece *P) { Result += P->Text; }
  803. void VisitPlaceholder(PlaceholderPiece *P) {
  804. Result += "%";
  805. Result += getModifierName(P->Kind);
  806. addInt(mapIndex(P->Index));
  807. }
  808. void VisitSelect(SelectPiece *P) {
  809. Result += "%";
  810. Result += getModifierName(P->ModKind);
  811. if (P->ModKind == MT_Select) {
  812. Result += "{";
  813. for (auto *D : P->Options) {
  814. Visit(D);
  815. Result += '|';
  816. }
  817. if (!P->Options.empty())
  818. Result.pop_back();
  819. Result += '}';
  820. }
  821. addInt(mapIndex(P->Index));
  822. }
  823. void VisitPlural(PluralPiece *P) {
  824. Result += "%plural{";
  825. assert(P->Options.size() == P->OptionPrefixes.size());
  826. for (unsigned I = 0, End = P->Options.size(); I < End; ++I) {
  827. if (P->OptionPrefixes[I])
  828. Visit(P->OptionPrefixes[I]);
  829. Visit(P->Options[I]);
  830. Result += "|";
  831. }
  832. if (!P->Options.empty())
  833. Result.pop_back();
  834. Result += '}';
  835. addInt(mapIndex(P->Index));
  836. }
  837. void VisitDiff(DiffPiece *P) {
  838. Result += "%diff{";
  839. Visit(P->Parts[0]);
  840. Result += "$";
  841. Visit(P->Parts[1]);
  842. Result += "$";
  843. Visit(P->Parts[2]);
  844. Result += "|";
  845. Visit(P->Parts[3]);
  846. Result += "}";
  847. addInt(mapIndex(P->Indexes[0]));
  848. Result += ",";
  849. addInt(mapIndex(P->Indexes[1]));
  850. }
  851. void addInt(int Val) { Result += std::to_string(Val); }
  852. std::string &Result;
  853. };
  854. int DiagnosticTextBuilder::DiagText::parseModifier(StringRef &Text) const {
  855. if (Text.empty() || !isdigit(Text[0]))
  856. Builder.PrintFatalError("expected modifier in diagnostic");
  857. int Val = 0;
  858. do {
  859. Val *= 10;
  860. Val += Text[0] - '0';
  861. Text = Text.drop_front();
  862. } while (!Text.empty() && isdigit(Text[0]));
  863. return Val;
  864. }
  865. Piece *DiagnosticTextBuilder::DiagText::parseDiagText(StringRef &Text,
  866. StopAt Stop) {
  867. std::vector<Piece *> Parsed;
  868. constexpr llvm::StringLiteral StopSets[] = {"%", "%|}", "%|}$"};
  869. llvm::StringRef StopSet = StopSets[static_cast<int>(Stop)];
  870. while (!Text.empty()) {
  871. size_t End = (size_t)-2;
  872. do
  873. End = Text.find_first_of(StopSet, End + 2);
  874. while (
  875. End < Text.size() - 1 && Text[End] == '%' &&
  876. (Text[End + 1] == '%' || Text[End + 1] == '|' || Text[End + 1] == '$'));
  877. if (End) {
  878. Parsed.push_back(New<TextPiece>(Text.slice(0, End), "diagtext"));
  879. Text = Text.slice(End, StringRef::npos);
  880. if (Text.empty())
  881. break;
  882. }
  883. if (Text[0] == '|' || Text[0] == '}' || Text[0] == '$')
  884. break;
  885. // Drop the '%'.
  886. Text = Text.drop_front();
  887. // Extract the (optional) modifier.
  888. size_t ModLength = Text.find_first_of("0123456789{");
  889. StringRef Modifier = Text.slice(0, ModLength);
  890. Text = Text.slice(ModLength, StringRef::npos);
  891. ModifierType ModType = llvm::StringSwitch<ModifierType>{Modifier}
  892. .Case("select", MT_Select)
  893. .Case("sub", MT_Sub)
  894. .Case("diff", MT_Diff)
  895. .Case("plural", MT_Plural)
  896. .Case("s", MT_S)
  897. .Case("ordinal", MT_Ordinal)
  898. .Case("q", MT_Q)
  899. .Case("objcclass", MT_ObjCClass)
  900. .Case("objcinstance", MT_ObjCInstance)
  901. .Case("", MT_Placeholder)
  902. .Default(MT_Unknown);
  903. auto ExpectAndConsume = [&](StringRef Prefix) {
  904. if (!Text.consume_front(Prefix))
  905. Builder.PrintFatalError("expected '" + Prefix + "' while parsing %" +
  906. Modifier);
  907. };
  908. switch (ModType) {
  909. case MT_Unknown:
  910. Builder.PrintFatalError("Unknown modifier type: " + Modifier);
  911. case MT_Select: {
  912. SelectPiece *Select = New<SelectPiece>(MT_Select);
  913. do {
  914. Text = Text.drop_front(); // '{' or '|'
  915. Select->Options.push_back(
  916. parseDiagText(Text, StopAt::PipeOrCloseBrace));
  917. assert(!Text.empty() && "malformed %select");
  918. } while (Text.front() == '|');
  919. ExpectAndConsume("}");
  920. Select->Index = parseModifier(Text);
  921. Parsed.push_back(Select);
  922. continue;
  923. }
  924. case MT_Plural: {
  925. PluralPiece *Plural = New<PluralPiece>();
  926. do {
  927. Text = Text.drop_front(); // '{' or '|'
  928. size_t End = Text.find_first_of(":");
  929. if (End == StringRef::npos)
  930. Builder.PrintFatalError("expected ':' while parsing %plural");
  931. ++End;
  932. assert(!Text.empty());
  933. Plural->OptionPrefixes.push_back(
  934. New<TextPiece>(Text.slice(0, End), "diagtext"));
  935. Text = Text.slice(End, StringRef::npos);
  936. Plural->Options.push_back(
  937. parseDiagText(Text, StopAt::PipeOrCloseBrace));
  938. assert(!Text.empty() && "malformed %plural");
  939. } while (Text.front() == '|');
  940. ExpectAndConsume("}");
  941. Plural->Index = parseModifier(Text);
  942. Parsed.push_back(Plural);
  943. continue;
  944. }
  945. case MT_Sub: {
  946. SubstitutionPiece *Sub = New<SubstitutionPiece>();
  947. ExpectAndConsume("{");
  948. size_t NameSize = Text.find_first_of('}');
  949. assert(NameSize != size_t(-1) && "failed to find the end of the name");
  950. assert(NameSize != 0 && "empty name?");
  951. Sub->Name = Text.substr(0, NameSize).str();
  952. Text = Text.drop_front(NameSize);
  953. ExpectAndConsume("}");
  954. if (!Text.empty()) {
  955. while (true) {
  956. if (!isdigit(Text[0]))
  957. break;
  958. Sub->Modifiers.push_back(parseModifier(Text));
  959. if (Text.empty() || Text[0] != ',')
  960. break;
  961. Text = Text.drop_front(); // ','
  962. assert(!Text.empty() && isdigit(Text[0]) &&
  963. "expected another modifier");
  964. }
  965. }
  966. Parsed.push_back(Sub);
  967. continue;
  968. }
  969. case MT_Diff: {
  970. DiffPiece *Diff = New<DiffPiece>();
  971. ExpectAndConsume("{");
  972. Diff->Parts[0] = parseDiagText(Text, StopAt::Dollar);
  973. ExpectAndConsume("$");
  974. Diff->Parts[1] = parseDiagText(Text, StopAt::Dollar);
  975. ExpectAndConsume("$");
  976. Diff->Parts[2] = parseDiagText(Text, StopAt::PipeOrCloseBrace);
  977. ExpectAndConsume("|");
  978. Diff->Parts[3] = parseDiagText(Text, StopAt::PipeOrCloseBrace);
  979. ExpectAndConsume("}");
  980. Diff->Indexes[0] = parseModifier(Text);
  981. ExpectAndConsume(",");
  982. Diff->Indexes[1] = parseModifier(Text);
  983. Parsed.push_back(Diff);
  984. continue;
  985. }
  986. case MT_S: {
  987. SelectPiece *Select = New<SelectPiece>(ModType);
  988. Select->Options.push_back(New<TextPiece>(""));
  989. Select->Options.push_back(New<TextPiece>("s", "diagtext"));
  990. Select->Index = parseModifier(Text);
  991. Parsed.push_back(Select);
  992. continue;
  993. }
  994. case MT_Q:
  995. case MT_Placeholder:
  996. case MT_ObjCClass:
  997. case MT_ObjCInstance:
  998. case MT_Ordinal: {
  999. Parsed.push_back(New<PlaceholderPiece>(ModType, parseModifier(Text)));
  1000. continue;
  1001. }
  1002. }
  1003. }
  1004. return New<MultiPiece>(Parsed);
  1005. }
  1006. std::vector<std::string>
  1007. DiagnosticTextBuilder::buildForDocumentation(StringRef Severity,
  1008. const Record *R) {
  1009. EvaluatingRecordGuard Guard(&EvaluatingRecord, R);
  1010. StringRef Text = R->getValueAsString("Summary");
  1011. DiagText D(*this, Text);
  1012. TextPiece *Prefix = D.New<TextPiece>(Severity, Severity);
  1013. Prefix->Text += ": ";
  1014. auto *MP = dyn_cast<MultiPiece>(D.Root);
  1015. if (!MP) {
  1016. MP = D.New<MultiPiece>();
  1017. MP->Pieces.push_back(D.Root);
  1018. D.Root = MP;
  1019. }
  1020. MP->Pieces.insert(MP->Pieces.begin(), Prefix);
  1021. std::vector<std::string> Result;
  1022. DiagTextDocPrinter{*this, Result}.Visit(D.Root);
  1023. return Result;
  1024. }
  1025. std::string DiagnosticTextBuilder::buildForDefinition(const Record *R) {
  1026. EvaluatingRecordGuard Guard(&EvaluatingRecord, R);
  1027. StringRef Text = R->getValueAsString("Summary");
  1028. DiagText D(*this, Text);
  1029. std::string Result;
  1030. DiagTextPrinter{*this, Result}.Visit(D.Root);
  1031. return Result;
  1032. }
  1033. } // namespace
  1034. //===----------------------------------------------------------------------===//
  1035. // Warning Tables (.inc file) generation.
  1036. //===----------------------------------------------------------------------===//
  1037. static bool isError(const Record &Diag) {
  1038. const std::string &ClsName =
  1039. std::string(Diag.getValueAsDef("Class")->getName());
  1040. return ClsName == "CLASS_ERROR";
  1041. }
  1042. static bool isRemark(const Record &Diag) {
  1043. const std::string &ClsName =
  1044. std::string(Diag.getValueAsDef("Class")->getName());
  1045. return ClsName == "CLASS_REMARK";
  1046. }
  1047. /// ClangDiagsDefsEmitter - The top-level class emits .def files containing
  1048. /// declarations of Clang diagnostics.
  1049. void clang::EmitClangDiagsDefs(RecordKeeper &Records, raw_ostream &OS,
  1050. const std::string &Component) {
  1051. // Write the #if guard
  1052. if (!Component.empty()) {
  1053. std::string ComponentName = StringRef(Component).upper();
  1054. OS << "#ifdef " << ComponentName << "START\n";
  1055. OS << "__" << ComponentName << "START = DIAG_START_" << ComponentName
  1056. << ",\n";
  1057. OS << "#undef " << ComponentName << "START\n";
  1058. OS << "#endif\n\n";
  1059. }
  1060. DiagnosticTextBuilder DiagTextBuilder(Records);
  1061. std::vector<Record *> Diags = Records.getAllDerivedDefinitions("Diagnostic");
  1062. std::vector<Record*> DiagGroups
  1063. = Records.getAllDerivedDefinitions("DiagGroup");
  1064. std::map<std::string, GroupInfo> DiagsInGroup;
  1065. groupDiagnostics(Diags, DiagGroups, DiagsInGroup);
  1066. DiagCategoryIDMap CategoryIDs(Records);
  1067. DiagGroupParentMap DGParentMap(Records);
  1068. // Compute the set of diagnostics that are in -Wpedantic.
  1069. RecordSet DiagsInPedantic;
  1070. InferPedantic inferPedantic(DGParentMap, Diags, DiagGroups, DiagsInGroup);
  1071. inferPedantic.compute(&DiagsInPedantic, (RecordVec*)nullptr);
  1072. for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
  1073. const Record &R = *Diags[i];
  1074. // Check if this is an error that is accidentally in a warning
  1075. // group.
  1076. if (isError(R)) {
  1077. if (DefInit *Group = dyn_cast<DefInit>(R.getValueInit("Group"))) {
  1078. const Record *GroupRec = Group->getDef();
  1079. const std::string &GroupName =
  1080. std::string(GroupRec->getValueAsString("GroupName"));
  1081. PrintFatalError(R.getLoc(), "Error " + R.getName() +
  1082. " cannot be in a warning group [" + GroupName + "]");
  1083. }
  1084. }
  1085. // Check that all remarks have an associated diagnostic group.
  1086. if (isRemark(R)) {
  1087. if (!isa<DefInit>(R.getValueInit("Group"))) {
  1088. PrintFatalError(R.getLoc(), "Error " + R.getName() +
  1089. " not in any diagnostic group");
  1090. }
  1091. }
  1092. // Filter by component.
  1093. if (!Component.empty() && Component != R.getValueAsString("Component"))
  1094. continue;
  1095. OS << "DIAG(" << R.getName() << ", ";
  1096. OS << R.getValueAsDef("Class")->getName();
  1097. OS << ", (unsigned)diag::Severity::"
  1098. << R.getValueAsDef("DefaultSeverity")->getValueAsString("Name");
  1099. // Description string.
  1100. OS << ", \"";
  1101. OS.write_escaped(DiagTextBuilder.buildForDefinition(&R)) << '"';
  1102. // Warning group associated with the diagnostic. This is stored as an index
  1103. // into the alphabetically sorted warning group table.
  1104. if (DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group"))) {
  1105. std::map<std::string, GroupInfo>::iterator I = DiagsInGroup.find(
  1106. std::string(DI->getDef()->getValueAsString("GroupName")));
  1107. assert(I != DiagsInGroup.end());
  1108. OS << ", " << I->second.IDNo;
  1109. } else if (DiagsInPedantic.count(&R)) {
  1110. std::map<std::string, GroupInfo>::iterator I =
  1111. DiagsInGroup.find("pedantic");
  1112. assert(I != DiagsInGroup.end() && "pedantic group not defined");
  1113. OS << ", " << I->second.IDNo;
  1114. } else {
  1115. OS << ", 0";
  1116. }
  1117. // SFINAE response.
  1118. OS << ", " << R.getValueAsDef("SFINAE")->getName();
  1119. // Default warning has no Werror bit.
  1120. if (R.getValueAsBit("WarningNoWerror"))
  1121. OS << ", true";
  1122. else
  1123. OS << ", false";
  1124. if (R.getValueAsBit("ShowInSystemHeader"))
  1125. OS << ", true";
  1126. else
  1127. OS << ", false";
  1128. if (R.getValueAsBit("ShowInSystemMacro"))
  1129. OS << ", true";
  1130. else
  1131. OS << ", false";
  1132. if (R.getValueAsBit("Deferrable"))
  1133. OS << ", true";
  1134. else
  1135. OS << ", false";
  1136. // Category number.
  1137. OS << ", " << CategoryIDs.getID(getDiagnosticCategory(&R, DGParentMap));
  1138. OS << ")\n";
  1139. }
  1140. }
  1141. //===----------------------------------------------------------------------===//
  1142. // Warning Group Tables generation
  1143. //===----------------------------------------------------------------------===//
  1144. static std::string getDiagCategoryEnum(llvm::StringRef name) {
  1145. if (name.empty())
  1146. return "DiagCat_None";
  1147. SmallString<256> enumName = llvm::StringRef("DiagCat_");
  1148. for (llvm::StringRef::iterator I = name.begin(), E = name.end(); I != E; ++I)
  1149. enumName += isalnum(*I) ? *I : '_';
  1150. return std::string(enumName.str());
  1151. }
  1152. /// Emit the array of diagnostic subgroups.
  1153. ///
  1154. /// The array of diagnostic subgroups contains for each group a list of its
  1155. /// subgroups. The individual lists are separated by '-1'. Groups with no
  1156. /// subgroups are skipped.
  1157. ///
  1158. /// \code
  1159. /// static const int16_t DiagSubGroups[] = {
  1160. /// /* Empty */ -1,
  1161. /// /* DiagSubGroup0 */ 142, -1,
  1162. /// /* DiagSubGroup13 */ 265, 322, 399, -1
  1163. /// }
  1164. /// \endcode
  1165. ///
  1166. static void emitDiagSubGroups(std::map<std::string, GroupInfo> &DiagsInGroup,
  1167. RecordVec &GroupsInPedantic, raw_ostream &OS) {
  1168. OS << "static const int16_t DiagSubGroups[] = {\n"
  1169. << " /* Empty */ -1,\n";
  1170. for (auto const &I : DiagsInGroup) {
  1171. const bool IsPedantic = I.first == "pedantic";
  1172. const std::vector<std::string> &SubGroups = I.second.SubGroups;
  1173. if (!SubGroups.empty() || (IsPedantic && !GroupsInPedantic.empty())) {
  1174. OS << " /* DiagSubGroup" << I.second.IDNo << " */ ";
  1175. for (auto const &SubGroup : SubGroups) {
  1176. std::map<std::string, GroupInfo>::const_iterator RI =
  1177. DiagsInGroup.find(SubGroup);
  1178. assert(RI != DiagsInGroup.end() && "Referenced without existing?");
  1179. OS << RI->second.IDNo << ", ";
  1180. }
  1181. // Emit the groups implicitly in "pedantic".
  1182. if (IsPedantic) {
  1183. for (auto const &Group : GroupsInPedantic) {
  1184. const std::string &GroupName =
  1185. std::string(Group->getValueAsString("GroupName"));
  1186. std::map<std::string, GroupInfo>::const_iterator RI =
  1187. DiagsInGroup.find(GroupName);
  1188. assert(RI != DiagsInGroup.end() && "Referenced without existing?");
  1189. OS << RI->second.IDNo << ", ";
  1190. }
  1191. }
  1192. OS << "-1,\n";
  1193. }
  1194. }
  1195. OS << "};\n\n";
  1196. }
  1197. /// Emit the list of diagnostic arrays.
  1198. ///
  1199. /// This data structure is a large array that contains itself arrays of varying
  1200. /// size. Each array represents a list of diagnostics. The different arrays are
  1201. /// separated by the value '-1'.
  1202. ///
  1203. /// \code
  1204. /// static const int16_t DiagArrays[] = {
  1205. /// /* Empty */ -1,
  1206. /// /* DiagArray1 */ diag::warn_pragma_message,
  1207. /// -1,
  1208. /// /* DiagArray2 */ diag::warn_abs_too_small,
  1209. /// diag::warn_unsigned_abs,
  1210. /// diag::warn_wrong_absolute_value_type,
  1211. /// -1
  1212. /// };
  1213. /// \endcode
  1214. ///
  1215. static void emitDiagArrays(std::map<std::string, GroupInfo> &DiagsInGroup,
  1216. RecordVec &DiagsInPedantic, raw_ostream &OS) {
  1217. OS << "static const int16_t DiagArrays[] = {\n"
  1218. << " /* Empty */ -1,\n";
  1219. for (auto const &I : DiagsInGroup) {
  1220. const bool IsPedantic = I.first == "pedantic";
  1221. const std::vector<const Record *> &V = I.second.DiagsInGroup;
  1222. if (!V.empty() || (IsPedantic && !DiagsInPedantic.empty())) {
  1223. OS << " /* DiagArray" << I.second.IDNo << " */ ";
  1224. for (auto *Record : V)
  1225. OS << "diag::" << Record->getName() << ", ";
  1226. // Emit the diagnostics implicitly in "pedantic".
  1227. if (IsPedantic) {
  1228. for (auto const &Diag : DiagsInPedantic)
  1229. OS << "diag::" << Diag->getName() << ", ";
  1230. }
  1231. OS << "-1,\n";
  1232. }
  1233. }
  1234. OS << "};\n\n";
  1235. }
  1236. /// Emit a list of group names.
  1237. ///
  1238. /// This creates a long string which by itself contains a list of pascal style
  1239. /// strings, which consist of a length byte directly followed by the string.
  1240. ///
  1241. /// \code
  1242. /// static const char DiagGroupNames[] = {
  1243. /// \000\020#pragma-messages\t#warnings\020CFString-literal"
  1244. /// };
  1245. /// \endcode
  1246. static void emitDiagGroupNames(StringToOffsetTable &GroupNames,
  1247. raw_ostream &OS) {
  1248. OS << "static const char DiagGroupNames[] = {\n";
  1249. GroupNames.EmitString(OS);
  1250. OS << "};\n\n";
  1251. }
  1252. /// Emit diagnostic arrays and related data structures.
  1253. ///
  1254. /// This creates the actual diagnostic array, an array of diagnostic subgroups
  1255. /// and an array of subgroup names.
  1256. ///
  1257. /// \code
  1258. /// #ifdef GET_DIAG_ARRAYS
  1259. /// static const int16_t DiagArrays[];
  1260. /// static const int16_t DiagSubGroups[];
  1261. /// static const char DiagGroupNames[];
  1262. /// #endif
  1263. /// \endcode
  1264. static void emitAllDiagArrays(std::map<std::string, GroupInfo> &DiagsInGroup,
  1265. RecordVec &DiagsInPedantic,
  1266. RecordVec &GroupsInPedantic,
  1267. StringToOffsetTable &GroupNames,
  1268. raw_ostream &OS) {
  1269. OS << "\n#ifdef GET_DIAG_ARRAYS\n";
  1270. emitDiagArrays(DiagsInGroup, DiagsInPedantic, OS);
  1271. emitDiagSubGroups(DiagsInGroup, GroupsInPedantic, OS);
  1272. emitDiagGroupNames(GroupNames, OS);
  1273. OS << "#endif // GET_DIAG_ARRAYS\n\n";
  1274. }
  1275. /// Emit diagnostic table.
  1276. ///
  1277. /// The table is sorted by the name of the diagnostic group. Each element
  1278. /// consists of the name of the diagnostic group (given as offset in the
  1279. /// group name table), a reference to a list of diagnostics (optional) and a
  1280. /// reference to a set of subgroups (optional).
  1281. ///
  1282. /// \code
  1283. /// #ifdef GET_DIAG_TABLE
  1284. /// {/* abi */ 159, /* DiagArray11 */ 19, /* Empty */ 0},
  1285. /// {/* aggregate-return */ 180, /* Empty */ 0, /* Empty */ 0},
  1286. /// {/* all */ 197, /* Empty */ 0, /* DiagSubGroup13 */ 3},
  1287. /// {/* deprecated */ 1981,/* DiagArray1 */ 348, /* DiagSubGroup3 */ 9},
  1288. /// #endif
  1289. /// \endcode
  1290. static void emitDiagTable(std::map<std::string, GroupInfo> &DiagsInGroup,
  1291. RecordVec &DiagsInPedantic,
  1292. RecordVec &GroupsInPedantic,
  1293. StringToOffsetTable &GroupNames, raw_ostream &OS) {
  1294. unsigned MaxLen = 0;
  1295. for (auto const &I: DiagsInGroup)
  1296. MaxLen = std::max(MaxLen, (unsigned)I.first.size());
  1297. OS << "\n#ifdef DIAG_ENTRY\n";
  1298. unsigned SubGroupIndex = 1, DiagArrayIndex = 1;
  1299. for (auto const &I: DiagsInGroup) {
  1300. // Group option string.
  1301. OS << "DIAG_ENTRY(";
  1302. OS << I.second.GroupName << " /* ";
  1303. if (I.first.find_first_not_of("abcdefghijklmnopqrstuvwxyz"
  1304. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1305. "0123456789!@#$%^*-+=:?") !=
  1306. std::string::npos)
  1307. PrintFatalError("Invalid character in diagnostic group '" + I.first +
  1308. "'");
  1309. OS << I.first << " */, ";
  1310. // Store a pascal-style length byte at the beginning of the string.
  1311. std::string Name = char(I.first.size()) + I.first;
  1312. OS << GroupNames.GetOrAddStringOffset(Name, false) << ", ";
  1313. // Special handling for 'pedantic'.
  1314. const bool IsPedantic = I.first == "pedantic";
  1315. // Diagnostics in the group.
  1316. const std::vector<const Record *> &V = I.second.DiagsInGroup;
  1317. const bool hasDiags =
  1318. !V.empty() || (IsPedantic && !DiagsInPedantic.empty());
  1319. if (hasDiags) {
  1320. OS << "/* DiagArray" << I.second.IDNo << " */ " << DiagArrayIndex
  1321. << ", ";
  1322. if (IsPedantic)
  1323. DiagArrayIndex += DiagsInPedantic.size();
  1324. DiagArrayIndex += V.size() + 1;
  1325. } else {
  1326. OS << "0, ";
  1327. }
  1328. // Subgroups.
  1329. const std::vector<std::string> &SubGroups = I.second.SubGroups;
  1330. const bool hasSubGroups =
  1331. !SubGroups.empty() || (IsPedantic && !GroupsInPedantic.empty());
  1332. if (hasSubGroups) {
  1333. OS << "/* DiagSubGroup" << I.second.IDNo << " */ " << SubGroupIndex
  1334. << ", ";
  1335. if (IsPedantic)
  1336. SubGroupIndex += GroupsInPedantic.size();
  1337. SubGroupIndex += SubGroups.size() + 1;
  1338. } else {
  1339. OS << "0, ";
  1340. }
  1341. std::string Documentation = I.second.Defs.back()
  1342. ->getValue("Documentation")
  1343. ->getValue()
  1344. ->getAsUnquotedString();
  1345. OS << "R\"(" << StringRef(Documentation).trim() << ")\"";
  1346. OS << ")\n";
  1347. }
  1348. OS << "#endif // DIAG_ENTRY\n\n";
  1349. }
  1350. /// Emit the table of diagnostic categories.
  1351. ///
  1352. /// The table has the form of macro calls that have two parameters. The
  1353. /// category's name as well as an enum that represents the category. The
  1354. /// table can be used by defining the macro 'CATEGORY' and including this
  1355. /// table right after.
  1356. ///
  1357. /// \code
  1358. /// #ifdef GET_CATEGORY_TABLE
  1359. /// CATEGORY("Semantic Issue", DiagCat_Semantic_Issue)
  1360. /// CATEGORY("Lambda Issue", DiagCat_Lambda_Issue)
  1361. /// #endif
  1362. /// \endcode
  1363. static void emitCategoryTable(RecordKeeper &Records, raw_ostream &OS) {
  1364. DiagCategoryIDMap CategoriesByID(Records);
  1365. OS << "\n#ifdef GET_CATEGORY_TABLE\n";
  1366. for (auto const &C : CategoriesByID)
  1367. OS << "CATEGORY(\"" << C << "\", " << getDiagCategoryEnum(C) << ")\n";
  1368. OS << "#endif // GET_CATEGORY_TABLE\n\n";
  1369. }
  1370. void clang::EmitClangDiagGroups(RecordKeeper &Records, raw_ostream &OS) {
  1371. // Compute a mapping from a DiagGroup to all of its parents.
  1372. DiagGroupParentMap DGParentMap(Records);
  1373. std::vector<Record *> Diags = Records.getAllDerivedDefinitions("Diagnostic");
  1374. std::vector<Record *> DiagGroups =
  1375. Records.getAllDerivedDefinitions("DiagGroup");
  1376. std::map<std::string, GroupInfo> DiagsInGroup;
  1377. groupDiagnostics(Diags, DiagGroups, DiagsInGroup);
  1378. // All extensions are implicitly in the "pedantic" group. Record the
  1379. // implicit set of groups in the "pedantic" group, and use this information
  1380. // later when emitting the group information for Pedantic.
  1381. RecordVec DiagsInPedantic;
  1382. RecordVec GroupsInPedantic;
  1383. InferPedantic inferPedantic(DGParentMap, Diags, DiagGroups, DiagsInGroup);
  1384. inferPedantic.compute(&DiagsInPedantic, &GroupsInPedantic);
  1385. StringToOffsetTable GroupNames;
  1386. for (std::map<std::string, GroupInfo>::const_iterator
  1387. I = DiagsInGroup.begin(),
  1388. E = DiagsInGroup.end();
  1389. I != E; ++I) {
  1390. // Store a pascal-style length byte at the beginning of the string.
  1391. std::string Name = char(I->first.size()) + I->first;
  1392. GroupNames.GetOrAddStringOffset(Name, false);
  1393. }
  1394. emitAllDiagArrays(DiagsInGroup, DiagsInPedantic, GroupsInPedantic, GroupNames,
  1395. OS);
  1396. emitDiagTable(DiagsInGroup, DiagsInPedantic, GroupsInPedantic, GroupNames,
  1397. OS);
  1398. emitCategoryTable(Records, OS);
  1399. }
  1400. //===----------------------------------------------------------------------===//
  1401. // Diagnostic name index generation
  1402. //===----------------------------------------------------------------------===//
  1403. namespace {
  1404. struct RecordIndexElement
  1405. {
  1406. RecordIndexElement() {}
  1407. explicit RecordIndexElement(Record const &R)
  1408. : Name(std::string(R.getName())) {}
  1409. std::string Name;
  1410. };
  1411. } // end anonymous namespace.
  1412. void clang::EmitClangDiagsIndexName(RecordKeeper &Records, raw_ostream &OS) {
  1413. const std::vector<Record*> &Diags =
  1414. Records.getAllDerivedDefinitions("Diagnostic");
  1415. std::vector<RecordIndexElement> Index;
  1416. Index.reserve(Diags.size());
  1417. for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
  1418. const Record &R = *(Diags[i]);
  1419. Index.push_back(RecordIndexElement(R));
  1420. }
  1421. llvm::sort(Index,
  1422. [](const RecordIndexElement &Lhs, const RecordIndexElement &Rhs) {
  1423. return Lhs.Name < Rhs.Name;
  1424. });
  1425. for (unsigned i = 0, e = Index.size(); i != e; ++i) {
  1426. const RecordIndexElement &R = Index[i];
  1427. OS << "DIAG_NAME_INDEX(" << R.Name << ")\n";
  1428. }
  1429. }
  1430. //===----------------------------------------------------------------------===//
  1431. // Diagnostic documentation generation
  1432. //===----------------------------------------------------------------------===//
  1433. namespace docs {
  1434. namespace {
  1435. bool isRemarkGroup(const Record *DiagGroup,
  1436. const std::map<std::string, GroupInfo> &DiagsInGroup) {
  1437. bool AnyRemarks = false, AnyNonRemarks = false;
  1438. std::function<void(StringRef)> Visit = [&](StringRef GroupName) {
  1439. auto &GroupInfo = DiagsInGroup.find(std::string(GroupName))->second;
  1440. for (const Record *Diag : GroupInfo.DiagsInGroup)
  1441. (isRemark(*Diag) ? AnyRemarks : AnyNonRemarks) = true;
  1442. for (const auto &Name : GroupInfo.SubGroups)
  1443. Visit(Name);
  1444. };
  1445. Visit(DiagGroup->getValueAsString("GroupName"));
  1446. if (AnyRemarks && AnyNonRemarks)
  1447. PrintFatalError(
  1448. DiagGroup->getLoc(),
  1449. "Diagnostic group contains both remark and non-remark diagnostics");
  1450. return AnyRemarks;
  1451. }
  1452. std::string getDefaultSeverity(const Record *Diag) {
  1453. return std::string(
  1454. Diag->getValueAsDef("DefaultSeverity")->getValueAsString("Name"));
  1455. }
  1456. std::set<std::string>
  1457. getDefaultSeverities(const Record *DiagGroup,
  1458. const std::map<std::string, GroupInfo> &DiagsInGroup) {
  1459. std::set<std::string> States;
  1460. std::function<void(StringRef)> Visit = [&](StringRef GroupName) {
  1461. auto &GroupInfo = DiagsInGroup.find(std::string(GroupName))->second;
  1462. for (const Record *Diag : GroupInfo.DiagsInGroup)
  1463. States.insert(getDefaultSeverity(Diag));
  1464. for (const auto &Name : GroupInfo.SubGroups)
  1465. Visit(Name);
  1466. };
  1467. Visit(DiagGroup->getValueAsString("GroupName"));
  1468. return States;
  1469. }
  1470. void writeHeader(StringRef Str, raw_ostream &OS, char Kind = '-') {
  1471. OS << Str << "\n" << std::string(Str.size(), Kind) << "\n";
  1472. }
  1473. void writeDiagnosticText(DiagnosticTextBuilder &Builder, const Record *R,
  1474. StringRef Role, raw_ostream &OS) {
  1475. StringRef Text = R->getValueAsString("Summary");
  1476. if (Text == "%0")
  1477. OS << "The text of this diagnostic is not controlled by Clang.\n\n";
  1478. else {
  1479. std::vector<std::string> Out = Builder.buildForDocumentation(Role, R);
  1480. for (auto &Line : Out)
  1481. OS << Line << "\n";
  1482. OS << "\n";
  1483. }
  1484. }
  1485. } // namespace
  1486. } // namespace docs
  1487. void clang::EmitClangDiagDocs(RecordKeeper &Records, raw_ostream &OS) {
  1488. using namespace docs;
  1489. // Get the documentation introduction paragraph.
  1490. const Record *Documentation = Records.getDef("GlobalDocumentation");
  1491. if (!Documentation) {
  1492. PrintFatalError("The Documentation top-level definition is missing, "
  1493. "no documentation will be generated.");
  1494. return;
  1495. }
  1496. OS << Documentation->getValueAsString("Intro") << "\n";
  1497. DiagnosticTextBuilder Builder(Records);
  1498. std::vector<Record*> Diags =
  1499. Records.getAllDerivedDefinitions("Diagnostic");
  1500. std::vector<Record*> DiagGroups =
  1501. Records.getAllDerivedDefinitions("DiagGroup");
  1502. llvm::sort(DiagGroups, diagGroupBeforeByName);
  1503. DiagGroupParentMap DGParentMap(Records);
  1504. std::map<std::string, GroupInfo> DiagsInGroup;
  1505. groupDiagnostics(Diags, DiagGroups, DiagsInGroup);
  1506. // Compute the set of diagnostics that are in -Wpedantic.
  1507. {
  1508. RecordSet DiagsInPedanticSet;
  1509. RecordSet GroupsInPedanticSet;
  1510. InferPedantic inferPedantic(DGParentMap, Diags, DiagGroups, DiagsInGroup);
  1511. inferPedantic.compute(&DiagsInPedanticSet, &GroupsInPedanticSet);
  1512. auto &PedDiags = DiagsInGroup["pedantic"];
  1513. // Put the diagnostics into a deterministic order.
  1514. RecordVec DiagsInPedantic(DiagsInPedanticSet.begin(),
  1515. DiagsInPedanticSet.end());
  1516. RecordVec GroupsInPedantic(GroupsInPedanticSet.begin(),
  1517. GroupsInPedanticSet.end());
  1518. llvm::sort(DiagsInPedantic, beforeThanCompare);
  1519. llvm::sort(GroupsInPedantic, beforeThanCompare);
  1520. PedDiags.DiagsInGroup.insert(PedDiags.DiagsInGroup.end(),
  1521. DiagsInPedantic.begin(),
  1522. DiagsInPedantic.end());
  1523. for (auto *Group : GroupsInPedantic)
  1524. PedDiags.SubGroups.push_back(
  1525. std::string(Group->getValueAsString("GroupName")));
  1526. }
  1527. // FIXME: Write diagnostic categories and link to diagnostic groups in each.
  1528. // Write out the diagnostic groups.
  1529. for (const Record *G : DiagGroups) {
  1530. bool IsRemarkGroup = isRemarkGroup(G, DiagsInGroup);
  1531. auto &GroupInfo =
  1532. DiagsInGroup[std::string(G->getValueAsString("GroupName"))];
  1533. bool IsSynonym = GroupInfo.DiagsInGroup.empty() &&
  1534. GroupInfo.SubGroups.size() == 1;
  1535. writeHeader(((IsRemarkGroup ? "-R" : "-W") +
  1536. G->getValueAsString("GroupName")).str(),
  1537. OS);
  1538. if (!IsSynonym) {
  1539. // FIXME: Ideally, all the diagnostics in a group should have the same
  1540. // default state, but that is not currently the case.
  1541. auto DefaultSeverities = getDefaultSeverities(G, DiagsInGroup);
  1542. if (!DefaultSeverities.empty() && !DefaultSeverities.count("Ignored")) {
  1543. bool AnyNonErrors = DefaultSeverities.count("Warning") ||
  1544. DefaultSeverities.count("Remark");
  1545. if (!AnyNonErrors)
  1546. OS << "This diagnostic is an error by default, but the flag ``-Wno-"
  1547. << G->getValueAsString("GroupName") << "`` can be used to disable "
  1548. << "the error.\n\n";
  1549. else
  1550. OS << "This diagnostic is enabled by default.\n\n";
  1551. } else if (DefaultSeverities.size() > 1) {
  1552. OS << "Some of the diagnostics controlled by this flag are enabled "
  1553. << "by default.\n\n";
  1554. }
  1555. }
  1556. if (!GroupInfo.SubGroups.empty()) {
  1557. if (IsSynonym)
  1558. OS << "Synonym for ";
  1559. else if (GroupInfo.DiagsInGroup.empty())
  1560. OS << "Controls ";
  1561. else
  1562. OS << "Also controls ";
  1563. bool First = true;
  1564. llvm::sort(GroupInfo.SubGroups);
  1565. for (const auto &Name : GroupInfo.SubGroups) {
  1566. if (!First) OS << ", ";
  1567. OS << "`" << (IsRemarkGroup ? "-R" : "-W") << Name << "`_";
  1568. First = false;
  1569. }
  1570. OS << ".\n\n";
  1571. }
  1572. if (!GroupInfo.DiagsInGroup.empty()) {
  1573. OS << "**Diagnostic text:**\n\n";
  1574. for (const Record *D : GroupInfo.DiagsInGroup) {
  1575. auto Severity = getDefaultSeverity(D);
  1576. Severity[0] = tolower(Severity[0]);
  1577. if (Severity == "ignored")
  1578. Severity = IsRemarkGroup ? "remark" : "warning";
  1579. writeDiagnosticText(Builder, D, Severity, OS);
  1580. }
  1581. }
  1582. auto Doc = G->getValueAsString("Documentation");
  1583. if (!Doc.empty())
  1584. OS << Doc;
  1585. else if (GroupInfo.SubGroups.empty() && GroupInfo.DiagsInGroup.empty())
  1586. OS << "This diagnostic flag exists for GCC compatibility, and has no "
  1587. "effect in Clang.\n";
  1588. OS << "\n";
  1589. }
  1590. }