FormatVariadic.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //===- FormatVariadic.cpp - Format string parsing and analysis ----*-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. #include "llvm/Support/FormatVariadic.h"
  8. #include <cassert>
  9. using namespace llvm;
  10. static Optional<AlignStyle> translateLocChar(char C) {
  11. switch (C) {
  12. case '-':
  13. return AlignStyle::Left;
  14. case '=':
  15. return AlignStyle::Center;
  16. case '+':
  17. return AlignStyle::Right;
  18. default:
  19. return None;
  20. }
  21. LLVM_BUILTIN_UNREACHABLE;
  22. }
  23. bool formatv_object_base::consumeFieldLayout(StringRef &Spec, AlignStyle &Where,
  24. size_t &Align, char &Pad) {
  25. Where = AlignStyle::Right;
  26. Align = 0;
  27. Pad = ' ';
  28. if (Spec.empty())
  29. return true;
  30. if (Spec.size() > 1) {
  31. // A maximum of 2 characters at the beginning can be used for something
  32. // other
  33. // than the width.
  34. // If Spec[1] is a loc char, then Spec[0] is a pad char and Spec[2:...]
  35. // contains the width.
  36. // Otherwise, if Spec[0] is a loc char, then Spec[1:...] contains the width.
  37. // Otherwise, Spec[0:...] contains the width.
  38. if (auto Loc = translateLocChar(Spec[1])) {
  39. Pad = Spec[0];
  40. Where = *Loc;
  41. Spec = Spec.drop_front(2);
  42. } else if (auto Loc = translateLocChar(Spec[0])) {
  43. Where = *Loc;
  44. Spec = Spec.drop_front(1);
  45. }
  46. }
  47. bool Failed = Spec.consumeInteger(0, Align);
  48. return !Failed;
  49. }
  50. Optional<ReplacementItem>
  51. formatv_object_base::parseReplacementItem(StringRef Spec) {
  52. StringRef RepString = Spec.trim("{}");
  53. // If the replacement sequence does not start with a non-negative integer,
  54. // this is an error.
  55. char Pad = ' ';
  56. std::size_t Align = 0;
  57. AlignStyle Where = AlignStyle::Right;
  58. StringRef Options;
  59. size_t Index = 0;
  60. RepString = RepString.trim();
  61. if (RepString.consumeInteger(0, Index)) {
  62. assert(false && "Invalid replacement sequence index!");
  63. return ReplacementItem{};
  64. }
  65. RepString = RepString.trim();
  66. if (!RepString.empty() && RepString.front() == ',') {
  67. RepString = RepString.drop_front();
  68. if (!consumeFieldLayout(RepString, Where, Align, Pad))
  69. assert(false && "Invalid replacement field layout specification!");
  70. }
  71. RepString = RepString.trim();
  72. if (!RepString.empty() && RepString.front() == ':') {
  73. Options = RepString.drop_front().trim();
  74. RepString = StringRef();
  75. }
  76. RepString = RepString.trim();
  77. if (!RepString.empty()) {
  78. assert(false && "Unexpected characters found in replacement string!");
  79. }
  80. return ReplacementItem{Spec, Index, Align, Where, Pad, Options};
  81. }
  82. std::pair<ReplacementItem, StringRef>
  83. formatv_object_base::splitLiteralAndReplacement(StringRef Fmt) {
  84. while (!Fmt.empty()) {
  85. // Everything up until the first brace is a literal.
  86. if (Fmt.front() != '{') {
  87. std::size_t BO = Fmt.find_first_of('{');
  88. return std::make_pair(ReplacementItem{Fmt.substr(0, BO)}, Fmt.substr(BO));
  89. }
  90. StringRef Braces = Fmt.take_while([](char C) { return C == '{'; });
  91. // If there is more than one brace, then some of them are escaped. Treat
  92. // these as replacements.
  93. if (Braces.size() > 1) {
  94. size_t NumEscapedBraces = Braces.size() / 2;
  95. StringRef Middle = Fmt.take_front(NumEscapedBraces);
  96. StringRef Right = Fmt.drop_front(NumEscapedBraces * 2);
  97. return std::make_pair(ReplacementItem{Middle}, Right);
  98. }
  99. // An unterminated open brace is undefined. We treat the rest of the string
  100. // as a literal replacement, but we assert to indicate that this is
  101. // undefined and that we consider it an error.
  102. std::size_t BC = Fmt.find_first_of('}');
  103. if (BC == StringRef::npos) {
  104. assert(
  105. false &&
  106. "Unterminated brace sequence. Escape with {{ for a literal brace.");
  107. return std::make_pair(ReplacementItem{Fmt}, StringRef());
  108. }
  109. // Even if there is a closing brace, if there is another open brace before
  110. // this closing brace, treat this portion as literal, and try again with the
  111. // next one.
  112. std::size_t BO2 = Fmt.find_first_of('{', 1);
  113. if (BO2 < BC)
  114. return std::make_pair(ReplacementItem{Fmt.substr(0, BO2)},
  115. Fmt.substr(BO2));
  116. StringRef Spec = Fmt.slice(1, BC);
  117. StringRef Right = Fmt.substr(BC + 1);
  118. auto RI = parseReplacementItem(Spec);
  119. if (RI.hasValue())
  120. return std::make_pair(*RI, Right);
  121. // If there was an error parsing the replacement item, treat it as an
  122. // invalid replacement spec, and just continue.
  123. Fmt = Fmt.drop_front(BC + 1);
  124. }
  125. return std::make_pair(ReplacementItem{Fmt}, StringRef());
  126. }
  127. SmallVector<ReplacementItem, 2>
  128. formatv_object_base::parseFormatString(StringRef Fmt) {
  129. SmallVector<ReplacementItem, 2> Replacements;
  130. ReplacementItem I;
  131. while (!Fmt.empty()) {
  132. std::tie(I, Fmt) = splitLiteralAndReplacement(Fmt);
  133. if (I.Type != ReplacementType::Empty)
  134. Replacements.push_back(I);
  135. }
  136. return Replacements;
  137. }
  138. void detail::format_adapter::anchor() { }