Skip to content

Commit 16e530d

Browse files
matthiaskrammMogball
authored andcommitted
When generating C++ code, use C++ string escaping.
Reviewed By: Mogball Differential Revision: https://reviews.llvm.org/D112468
1 parent 07bed3a commit 16e530d

File tree

5 files changed

+22
-17
lines changed

5 files changed

+22
-17
lines changed

mlir/include/mlir/TableGen/CodeGenHelpers.h

+3
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ class StaticVerifierFunctionEmitter {
146146
llvm::DenseMap<const void *, std::string> localTypeConstraints;
147147
};
148148

149+
// Escape a string using C++ encoding. E.g. foo"bar -> foo\x22bar.
150+
std::string escapeString(StringRef value);
151+
149152
} // namespace tblgen
150153
} // namespace mlir
151154

mlir/test/mlir-tblgen/predicate.td

+11
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,14 @@ def OpK : NS_Op<"op_for_AnyTensorOf", []> {
111111
// CHECK: auto valueGroup0 = getODSOperands(0);
112112
// CHECK: for (::mlir::Value v : valueGroup0) {
113113
// CHECK: if (::mlir::failed([[$TENSOR_INTEGER_FLOAT_CONSTRAINT]]
114+
115+
def OpL : NS_Op<"op_for_StringEscaping", []> {
116+
let arguments = (ins
117+
StringBasedAttr<CPred<"$_self.cast<StringAttr>().getValue() == \"foo\"">,
118+
"only value \"foo\" is allowed">:$s
119+
);
120+
}
121+
122+
// CHECK-LABEL: OpLAdaptor::verify
123+
// CHECK: getValue() == "foo"
124+
// CHECK-SAME: only value \"foo\" is allowed

mlir/tools/mlir-tblgen/CodeGenHelpers.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,10 @@ void StaticVerifierFunctionEmitter::emitTypeConstraintMethods(
137137
os.unindent() << "}\n\n";
138138
}
139139
}
140+
141+
std::string mlir::tblgen::escapeString(StringRef value) {
142+
std::string ret;
143+
llvm::raw_string_ostream os(ret);
144+
os.write_escaped(value);
145+
return os.str();
146+
}

mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp

-8
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,6 @@ 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-
162154
// Returns whether the record has a value of the given name that can be returned
163155
// via getValueAsString.
164156
static inline bool hasStringAttribute(const Record &record,

mlir/tools/mlir-tblgen/RewriterGen.cpp

+1-9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "mlir/Support/IndentedOstream.h"
1414
#include "mlir/TableGen/Attribute.h"
15+
#include "mlir/TableGen/CodeGenHelpers.h"
1516
#include "mlir/TableGen/Format.h"
1617
#include "mlir/TableGen/GenInfo.h"
1718
#include "mlir/TableGen/Operator.h"
@@ -50,15 +51,6 @@ struct format_provider<mlir::tblgen::Pattern::IdentifierLine> {
5051
};
5152
} // end namespace llvm
5253

53-
// Escape a string for use inside a C++ literal.
54-
// E.g. foo"bar -> foo\x22bar.
55-
static std::string escapeString(StringRef value) {
56-
std::string ret;
57-
llvm::raw_string_ostream os(ret);
58-
os.write_escaped(value, /*use_hex_escapes=*/true);
59-
return os.str();
60-
}
61-
6254
//===----------------------------------------------------------------------===//
6355
// PatternEmitter
6456
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)