Skip to content

Commit ebb1e90

Browse files
committed
[mlir:PDLL] Fix error handling of eof within a string literal
We currently aren't handling this properly, and in the case of a string block just crash. This commit adds proper error handling and detection for eof. Differential Revision: https://reviews.llvm.org/D124585
1 parent 32bf1f1 commit ebb1e90

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

mlir/lib/Tools/PDLL/Parser/Lexer.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -355,19 +355,28 @@ Token Lexer::lexString(const char *tokStart, bool isStringBlock) {
355355
case '"':
356356
// If this is a string block, we only end the string when we encounter a
357357
// `}]`.
358-
if (!isStringBlock) return formToken(Token::string, tokStart);
358+
if (!isStringBlock)
359+
return formToken(Token::string, tokStart);
359360
continue;
360361
case '}':
361362
// If this is a string block, we only end the string when we encounter a
362363
// `}]`.
363-
if (!isStringBlock || *curPtr != ']') continue;
364+
if (!isStringBlock || *curPtr != ']')
365+
continue;
364366
++curPtr;
365367
return formToken(Token::string_block, tokStart);
366-
case 0:
368+
case 0: {
367369
// If this is a random nul character in the middle of a string, just
368-
// include it. If it is the end of file, then it is an error.
369-
if (curPtr - 1 != curBuffer.end()) continue;
370-
LLVM_FALLTHROUGH;
370+
// include it. If it is the end of file, then it is an error.
371+
if (curPtr - 1 != curBuffer.end())
372+
continue;
373+
--curPtr;
374+
375+
StringRef expectedEndStr = isStringBlock ? "}]" : "\"";
376+
return emitError(curPtr - 1,
377+
"expected '" + expectedEndStr + "' in string literal");
378+
}
379+
371380
case '\n':
372381
case '\v':
373382
case '\f':

mlir/lib/Tools/PDLL/Parser/Parser.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,8 @@ FailureOr<T *> Parser::parseUserNativeConstraintOrRewriteDecl(
12451245
} else if (isInline) {
12461246
return emitError(name.getLoc(),
12471247
"external declarations must be declared in global scope");
1248+
} else if (curToken.is(Token::error)) {
1249+
return failure();
12481250
}
12491251
if (failed(parseToken(Token::semicolon,
12501252
"expected `;` after native declaration")))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: not mlir-pdll %s -I %S -split-input-file 2>&1 | FileCheck %s
2+
3+
// CHECK: expected '}]' in string literal
4+
Constraint Cst() [{
5+
6+
// -----
7+
8+
// CHECK: expected '"' in string literal
9+
Constraint Cst() "

0 commit comments

Comments
 (0)