Skip to content

Commit

Permalink
Fix argument count check for ops with only a variadic argument list.
Browse files Browse the repository at this point in the history
We don't count variadic arguments in the `getNumFullArguments()` method
since they can be zero. Not properly checking the return value of this
function can however lead to a `arg_size() < 0` check in the generated
verifier method. This happens if we only have a single variadic argument
list argument.
  • Loading branch information
Thomas Symalla committed May 22, 2024
1 parent 28eac73 commit 35c47a3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
19 changes: 16 additions & 3 deletions lib/TableGen/Operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,9 +460,17 @@ void Operation::emitVerifierMethod(llvm::raw_ostream &out,
fmt.withContext(symbols.chooseName("context"));
fmt.addSubst("_errs", symbols.chooseName("errs"));

bool emitArgCountVerifier = true;
if (m_hasVariadicArgument) {
fmt.addSubst("_comparator", "<");
fmt.addSubst("_at_least", "at least ");
if (getNumFullArguments() == 0) {
// If the only argument in an operation is a variadic argument list,
// getNumFullArguments() will return zero. Thus, prevent us from emitting
// arg_size() < 0 verifier checks.
emitArgCountVerifier = false;
} else {
fmt.addSubst("_comparator", "<");
fmt.addSubst("_at_least", "at least ");
}
} else {
fmt.addSubst("_comparator", "!=");
fmt.addSubst("_at_least", "");
Expand All @@ -474,14 +482,19 @@ void Operation::emitVerifierMethod(llvm::raw_ostream &out,
(void)$_context;
using ::llvm_dialects::printable;
)",
&fmt);

if (emitArgCountVerifier) {
out << tgfmt(R"(
if (arg_size() $_comparator $0) {
$_errs << " wrong number of arguments: " << arg_size()
<< ", expected $_at_least$0\n";
return false;
}
)",
&fmt, getNumFullArguments());
&fmt, getNumFullArguments());
}

Assignment assignment;
Evaluator eval(symbols, assignment, m_system, out, fmt);
Expand Down
44 changes: 19 additions & 25 deletions test/example/generated/ExampleDialect.cpp.inc
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ rhs,
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 3) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 3\n";
Expand Down Expand Up @@ -482,7 +482,7 @@ rhs
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 2) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 2\n";
Expand Down Expand Up @@ -577,7 +577,7 @@ index
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 2) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 2\n";
Expand Down Expand Up @@ -680,7 +680,7 @@ source
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 1) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 1\n";
Expand Down Expand Up @@ -844,7 +844,7 @@ source
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 0) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 0\n";
Expand Down Expand Up @@ -912,7 +912,7 @@ source
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 1) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 1\n";
Expand Down Expand Up @@ -1010,7 +1010,7 @@ source
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 1) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 1\n";
Expand Down Expand Up @@ -1110,7 +1110,7 @@ index
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 3) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 3\n";
Expand Down Expand Up @@ -1228,7 +1228,7 @@ instName_0
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 2) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 2\n";
Expand Down Expand Up @@ -1311,7 +1311,7 @@ instName
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 1) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 1\n";
Expand Down Expand Up @@ -1387,12 +1387,6 @@ instName
(void)context;

using ::llvm_dialects::printable;

if (arg_size() < 0) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected at least 0\n";
return false;
}
::llvm::Type * const resultType = getResult()->getType();
(void)resultType;

Expand Down Expand Up @@ -1457,7 +1451,7 @@ instName
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 0) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 0\n";
Expand Down Expand Up @@ -1514,7 +1508,7 @@ instName
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 0) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 0\n";
Expand Down Expand Up @@ -1571,7 +1565,7 @@ data
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 1) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 1\n";
Expand Down Expand Up @@ -1634,7 +1628,7 @@ data
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 1) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 1\n";
Expand Down Expand Up @@ -1713,7 +1707,7 @@ initial
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 3) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 3\n";
Expand Down Expand Up @@ -1805,7 +1799,7 @@ initial
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 3) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 3\n";
Expand Down Expand Up @@ -1897,7 +1891,7 @@ initial
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 3) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 3\n";
Expand Down Expand Up @@ -1984,7 +1978,7 @@ data
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 1) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 1\n";
Expand Down Expand Up @@ -2049,7 +2043,7 @@ data
(void)context;

using ::llvm_dialects::printable;

if (arg_size() < 1) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected at least 1\n";
Expand Down

0 comments on commit 35c47a3

Please sign in to comment.