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
This commit is contained in:
Matthias Kramm 2021-10-21 21:41:15 +00:00 committed by Mogball
parent db0486c46f
commit 95935e8285
1 changed files with 10 additions and 1 deletions

View File

@ -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 @@ private:
// 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";