[Mlir-commits] [mlir] [MLIR] emitc: Add fmtArgs to verbatim (PR #123294)
Matthias Gehre
llvmlistbot at llvm.org
Tue Feb 18 06:52:00 PST 2025
================
@@ -167,6 +168,64 @@ static LogicalResult verifyInitializationAttribute(Operation *op,
return success();
}
+/// Parse a format string and return a list of its parts.
+/// A part is either a StringRef that has to be printed as-is, or
+/// a Placeholder which requires printing the next operand of the VerbatimOp.
+/// In the format string, all `{}` are replaced by Placeholders, except if the
+/// `{` is escaped by `{{` - then it doesn't start a placeholder.
+template <class ArgType>
+FailureOr<SmallVector<ReplacementItem>>
+parseFormatString(StringRef toParse, ArgType fmtArgs,
+ std::optional<llvm::function_ref<mlir::InFlightDiagnostic()>>
+ emitError = {}) {
+ SmallVector<ReplacementItem> items;
+
+ // If there are not operands, the format string is not interpreted.
+ if (fmtArgs.empty()) {
+ items.push_back(toParse);
+ return items;
+ }
+
+ while (!toParse.empty()) {
+ size_t idx = toParse.find('{');
+ if (idx == StringRef::npos) {
+ // No '{'
+ items.push_back(toParse);
+ break;
+ }
+ if (idx > 0) {
+ // Take all chars excluding the '{'.
+ items.push_back(toParse.take_front(idx));
+ toParse = toParse.drop_front(idx);
+ continue;
+ }
+ if (toParse.size() < 2) {
----------------
mgehre-amd wrote:
Thanks, updated!
https://github.com/llvm/llvm-project/pull/123294
More information about the Mlir-commits
mailing list