Skip to content

Commit 95935e8

Browse files
matthiaskrammMogball
authored andcommitted
Make genAttributeVerifier escape the summary.
The summary can contain references to e.g. attribute defaults, which can contain special characters. So these strings need to be C++ escaped. Reviewed By: Mogball Differential Revision: https://reviews.llvm.org/D112249
1 parent db0486c commit 95935e8

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,14 @@ static std::string replaceAllSubstrs(std::string str, const std::string &match,
151151
return str;
152152
}
153153

154+
// Escape a string using LLVM/MLIR encoding. E.g. foo"bar -> foo\22bar.
155+
static std::string escapeString(StringRef value) {
156+
std::string ret;
157+
llvm::raw_string_ostream os(ret);
158+
llvm::printEscapedString(value, os);
159+
return os.str();
160+
}
161+
154162
// Returns whether the record has a value of the given name that can be returned
155163
// via getValueAsString.
156164
static inline bool hasStringAttribute(const Record &record,
@@ -359,6 +367,7 @@ class OpEmitter {
359367
// The emitter containing all of the locally emitted verification functions.
360368
const StaticVerifierFunctionEmitter &staticVerifierEmitter;
361369
};
370+
362371
} // end anonymous namespace
363372

364373
// Populate the format context `ctx` with substitutions of attributes, operands
@@ -464,7 +473,7 @@ static void genAttributeVerifier(const Operator &op, const char *attrGet,
464473
body << tgfmt(" if (!($0)) return $1\"attribute '$2' "
465474
"failed to satisfy constraint: $3\");\n",
466475
/*ctx=*/nullptr, tgfmt(condition, &ctx.withSelf(varName)),
467-
emitErrorPrefix, attrName, attr.getSummary());
476+
emitErrorPrefix, attrName, escapeString(attr.getSummary()));
468477
if (allowMissingAttr)
469478
body << " }\n";
470479
body << " }\n";

0 commit comments

Comments
 (0)