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 8 #include "llvm/Support/FormatVariadic.h" 9 #include <cassert> 10 #include <optional> 11 12 using namespace llvm; 13 14 static std::optional<AlignStyle> translateLocChar(char C) { 15 switch (C) { 16 case '-': 17 return AlignStyle::Left; 18 case '=': 19 return AlignStyle::Center; 20 case '+': 21 return AlignStyle::Right; 22 default: 23 return std::nullopt; 24 } 25 LLVM_BUILTIN_UNREACHABLE; 26 } 27 28 static bool consumeFieldLayout(StringRef &Spec, AlignStyle &Where, 29 size_t &Align, char &Pad) { 30 Where = AlignStyle::Right; 31 Align = 0; 32 Pad = ' '; 33 if (Spec.empty()) 34 return true; 35 36 if (Spec.size() > 1) { 37 // A maximum of 2 characters at the beginning can be used for something 38 // other than the width. 39 // If Spec[1] is a loc char, then Spec[0] is a pad char and Spec[2:...] 40 // contains the width. 41 // Otherwise, if Spec[0] is a loc char, then Spec[1:...] contains the width. 42 // Otherwise, Spec[0:...] contains the width. 43 if (auto Loc = translateLocChar(Spec[1])) { 44 Pad = Spec[0]; 45 Where = *Loc; 46 Spec = Spec.drop_front(2); 47 } else if (auto Loc = translateLocChar(Spec[0])) { 48 Where = *Loc; 49 Spec = Spec.drop_front(1); 50 } 51 } 52 53 bool Failed = Spec.consumeInteger(0, Align); 54 return !Failed; 55 } 56 57 static std::optional<ReplacementItem> parseReplacementItem(StringRef Spec) { 58 StringRef RepString = Spec.trim("{}"); 59 60 // If the replacement sequence does not start with a non-negative integer, 61 // this is an error. 62 char Pad = ' '; 63 std::size_t Align = 0; 64 AlignStyle Where = AlignStyle::Right; 65 StringRef Options; 66 size_t Index = 0; 67 RepString = RepString.trim(); 68 if (RepString.consumeInteger(0, Index)) { 69 assert(false && "Invalid replacement sequence index!"); 70 return ReplacementItem{}; 71 } 72 RepString = RepString.trim(); 73 if (RepString.consume_front(",")) { 74 if (!consumeFieldLayout(RepString, Where, Align, Pad)) 75 assert(false && "Invalid replacement field layout specification!"); 76 } 77 RepString = RepString.trim(); 78 if (RepString.consume_front(":")) { 79 Options = RepString.trim(); 80 RepString = StringRef(); 81 } 82 RepString = RepString.trim(); 83 assert(RepString.empty() && 84 "Unexpected characters found in replacement string!"); 85 86 return ReplacementItem{Spec, Index, Align, Where, Pad, Options}; 87 } 88 89 static std::pair<ReplacementItem, StringRef> 90 splitLiteralAndReplacement(StringRef Fmt) { 91 while (!Fmt.empty()) { 92 // Everything up until the first brace is a literal. 93 if (Fmt.front() != '{') { 94 std::size_t BO = Fmt.find_first_of('{'); 95 return std::make_pair(ReplacementItem{Fmt.substr(0, BO)}, Fmt.substr(BO)); 96 } 97 98 StringRef Braces = Fmt.take_while([](char C) { return C == '{'; }); 99 // If there is more than one brace, then some of them are escaped. Treat 100 // these as replacements. 101 if (Braces.size() > 1) { 102 size_t NumEscapedBraces = Braces.size() / 2; 103 StringRef Middle = Fmt.take_front(NumEscapedBraces); 104 StringRef Right = Fmt.drop_front(NumEscapedBraces * 2); 105 return std::make_pair(ReplacementItem{Middle}, Right); 106 } 107 // An unterminated open brace is undefined. Assert to indicate that this is 108 // undefined and that we consider it an error. When asserts are disabled, 109 // build a replacement item with an error message. 110 std::size_t BC = Fmt.find_first_of('}'); 111 if (BC == StringRef::npos) { 112 assert( 113 false && 114 "Unterminated brace sequence. Escape with {{ for a literal brace."); 115 return std::make_pair( 116 ReplacementItem{"Unterminated brace sequence. Escape with {{ for a " 117 "literal brace."}, 118 StringRef()); 119 } 120 121 // Even if there is a closing brace, if there is another open brace before 122 // this closing brace, treat this portion as literal, and try again with the 123 // next one. 124 std::size_t BO2 = Fmt.find_first_of('{', 1); 125 if (BO2 < BC) 126 return std::make_pair(ReplacementItem{Fmt.substr(0, BO2)}, 127 Fmt.substr(BO2)); 128 129 StringRef Spec = Fmt.slice(1, BC); 130 StringRef Right = Fmt.substr(BC + 1); 131 132 auto RI = parseReplacementItem(Spec); 133 if (RI) 134 return std::make_pair(*RI, Right); 135 136 // If there was an error parsing the replacement item, treat it as an 137 // invalid replacement spec, and just continue. 138 Fmt = Fmt.drop_front(BC + 1); 139 } 140 return std::make_pair(ReplacementItem{Fmt}, StringRef()); 141 } 142 143 #ifndef NDEBUG 144 #define ENABLE_VALIDATION 1 145 #else 146 #define ENABLE_VALIDATION 0 // Conveniently enable validation in release mode. 147 #endif 148 149 SmallVector<ReplacementItem, 2> 150 formatv_object_base::parseFormatString(StringRef Fmt, size_t NumArgs, 151 bool Validate) { 152 SmallVector<ReplacementItem, 2> Replacements; 153 154 #if ENABLE_VALIDATION 155 const StringRef SavedFmtStr = Fmt; 156 size_t NumExpectedArgs = 0; 157 #endif 158 159 while (!Fmt.empty()) { 160 ReplacementItem I; 161 std::tie(I, Fmt) = splitLiteralAndReplacement(Fmt); 162 if (I.Type != ReplacementType::Empty) 163 Replacements.push_back(I); 164 #if ENABLE_VALIDATION 165 if (I.Type == ReplacementType::Format) 166 NumExpectedArgs = std::max(NumExpectedArgs, I.Index + 1); 167 #endif 168 } 169 170 #if ENABLE_VALIDATION 171 if (!Validate) 172 return Replacements; 173 174 // Perform additional validation. Verify that the number of arguments matches 175 // the number of replacement indices and that there are no holes in the 176 // replacement indices. 177 178 // When validation fails, return an array of replacement items that 179 // will print an error message as the outout of this formatv() (used when 180 // validation is enabled in release mode). 181 auto getErrorReplacements = [SavedFmtStr](StringLiteral ErrorMsg) { 182 return SmallVector<ReplacementItem, 2>{ 183 ReplacementItem("Invalid formatv() call: "), ReplacementItem(ErrorMsg), 184 ReplacementItem(" for format string: "), ReplacementItem(SavedFmtStr)}; 185 }; 186 187 if (NumExpectedArgs != NumArgs) { 188 errs() << formatv( 189 "Expected {0} Args, but got {1} for format string '{2}'\n", 190 NumExpectedArgs, NumArgs, SavedFmtStr); 191 assert(0 && "Invalid formatv() call"); 192 return getErrorReplacements("Unexpected number of arguments"); 193 } 194 195 // Find the number of unique indices seen. All replacement indices 196 // are < NumExpectedArgs. 197 SmallVector<bool> Indices(NumExpectedArgs); 198 size_t Count = 0; 199 for (const ReplacementItem &I : Replacements) { 200 if (I.Type != ReplacementType::Format || Indices[I.Index]) 201 continue; 202 Indices[I.Index] = true; 203 ++Count; 204 } 205 206 if (Count != NumExpectedArgs) { 207 errs() << formatv( 208 "Replacement field indices cannot have holes for format string '{0}'\n", 209 SavedFmtStr); 210 assert(0 && "Invalid format string"); 211 return getErrorReplacements("Replacement indices have holes"); 212 } 213 #endif // ENABLE_VALIDATION 214 return Replacements; 215 } 216 217 void support::detail::format_adapter::anchor() {} 218