diff --git a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp index e5d243952a53f7..12cb505d2a656b 100644 --- a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp +++ b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp @@ -151,6 +151,14 @@ static std::string replaceAllSubstrs(std::string str, const std::string &match, return str; } +// Escape a string using LLVM/MLIR encoding. E.g. foo"bar -> foo\22bar. +static std::string escapeString(StringRef value) { + std::string ret; + llvm::raw_string_ostream os(ret); + llvm::printEscapedString(value, os); + return os.str(); +} + // Returns whether the record has a value of the given name that can be returned // via getValueAsString. static inline bool hasStringAttribute(const Record &record, @@ -359,6 +367,7 @@ class OpEmitter { // The emitter containing all of the locally emitted verification functions. const StaticVerifierFunctionEmitter &staticVerifierEmitter; }; + } // end anonymous namespace // Populate the format context `ctx` with substitutions of attributes, operands @@ -464,7 +473,7 @@ static void genAttributeVerifier(const Operator &op, const char *attrGet, body << tgfmt(" if (!($0)) return $1\"attribute '$2' " "failed to satisfy constraint: $3\");\n", /*ctx=*/nullptr, tgfmt(condition, &ctx.withSelf(varName)), - emitErrorPrefix, attrName, attr.getSummary()); + emitErrorPrefix, attrName, escapeString(attr.getSummary())); if (allowMissingAttr) body << " }\n"; body << " }\n";