diff --git a/mlir/lib/Tools/PDLL/Parser/Lexer.h b/mlir/lib/Tools/PDLL/Parser/Lexer.h --- a/mlir/lib/Tools/PDLL/Parser/Lexer.h +++ b/mlir/lib/Tools/PDLL/Parser/Lexer.h @@ -206,6 +206,7 @@ /// Lex methods. void lexComment(); + LogicalResult lexCComment(); Token lexDirective(const char *tokStart); Token lexIdentifier(const char *tokStart); Token lexNumber(const char *tokStart); diff --git a/mlir/lib/Tools/PDLL/Parser/Lexer.cpp b/mlir/lib/Tools/PDLL/Parser/Lexer.cpp --- a/mlir/lib/Tools/PDLL/Parser/Lexer.cpp +++ b/mlir/lib/Tools/PDLL/Parser/Lexer.cpp @@ -235,6 +235,10 @@ if (*curPtr == '/') { lexComment(); continue; + } else if (*curPtr == '*') { + if (failed(lexCComment())) + return emitError(curPtr - 1, "unterminated comment"); + continue; } return emitError(tokStart, "unexpected character"); @@ -291,6 +295,38 @@ } } +/// Skip a c-style comment, which is in the form of /**/. +LogicalResult Lexer::lexCComment() { + // Advance over the '*'. + assert(*curPtr == '*'); + ++curPtr; + unsigned nestedDepth = 1; + + while (true) { + switch (*curPtr++) { + case 0: + return failure(); + case '*': + if (curPtr[0] == '/') { + // This is the end of comment. + ++curPtr; + if (--nestedDepth == 0) + return success(); + } + break; + case '/': + if (curPtr[0] == '*') { + // Start of a nested comment. + ++nestedDepth; + ++curPtr; + } + break; + } + } + + llvm_unreachable("EOF is handled in the switch above"); +} + Token Lexer::lexDirective(const char *tokStart) { // Match the rest with an identifier regex: [0-9a-zA-Z_]* while (isalnum(*curPtr) || *curPtr == '_') ++curPtr; diff --git a/mlir/test/mlir-pdll/Parser/comment-failure.pdll b/mlir/test/mlir-pdll/Parser/comment-failure.pdll new file mode 100644 --- /dev/null +++ b/mlir/test/mlir-pdll/Parser/comment-failure.pdll @@ -0,0 +1,8 @@ +// RUN: not mlir-pdll %s -I %S -split-input-file 2>&1 | FileCheck %s + +/* + /* This is malformed C-style comment */ +// CHECK: unterminated comment +Pattern { + erase _: Op; +} diff --git a/mlir/test/mlir-pdll/Parser/comment.pdll b/mlir/test/mlir-pdll/Parser/comment.pdll new file mode 100644 --- /dev/null +++ b/mlir/test/mlir-pdll/Parser/comment.pdll @@ -0,0 +1,33 @@ +// RUN: mlir-pdll %s -I %S -split-input-file 2>&1 | FileCheck %s + +/* This is C-style comment in single line */ +// CHECK: Module +// CHECK: EraseStmt +// CHECK: `-DeclRefExpr {{.*}} Type +Pattern { + erase _: Op; +} + +// ----- + +/* + This is C-style comment in multiple lines +*/ +// CHECK: Module +// CHECK: EraseStmt +// CHECK: `-DeclRefExpr {{.*}} Type +Pattern { + erase _: Op; +} + +// ----- + +/* + /* This is nested C-style comment in multiple lines */ +*/ +// CHECK: Module +// CHECK: EraseStmt +// CHECK: `-DeclRefExpr {{.*}} Type +Pattern { + erase _: Op; +}