River Riddle | 1ae60e0 | 2022-09-29 01:39:26 | [diff] [blame] | 1 | //===- ParserTest.cpp -----------------------------------------------------===// |
| 2 | // |
| 3 | // This file is licensed under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "mlir/Parser/Parser.h" |
| 10 | #include "mlir/IR/BuiltinOps.h" |
| 11 | #include "mlir/IR/Verifier.h" |
| 12 | |
| 13 | #include "gmock/gmock.h" |
| 14 | |
| 15 | using namespace mlir; |
| 16 | |
| 17 | namespace { |
| 18 | TEST(MLIRParser, ParseInvalidIR) { |
| 19 | std::string moduleStr = R"mlir( |
| 20 | module attributes {bad} {} |
| 21 | )mlir"; |
| 22 | |
| 23 | MLIRContext context; |
| 24 | ParserConfig config(&context, /*verifyAfterParse=*/false); |
| 25 | |
| 26 | // Check that we properly parse the op, but it fails the verifier. |
| 27 | OwningOpRef<ModuleOp> module = parseSourceString<ModuleOp>(moduleStr, config); |
| 28 | ASSERT_TRUE(module); |
| 29 | ASSERT_TRUE(failed(verify(*module))); |
| 30 | } |
River Riddle | 54cdc03 | 2022-10-03 19:26:12 | [diff] [blame] | 31 | |
| 32 | TEST(MLIRParser, ParseAtEnd) { |
| 33 | std::string firstModuleStr = R"mlir( |
| 34 | "test.first"() : () -> () |
| 35 | )mlir"; |
| 36 | std::string secondModuleStr = R"mlir( |
| 37 | "test.second"() : () -> () |
| 38 | )mlir"; |
| 39 | |
| 40 | MLIRContext context; |
| 41 | context.allowUnregisteredDialects(); |
| 42 | Block block; |
| 43 | |
| 44 | // Parse the first module string. |
| 45 | LogicalResult firstParse = |
| 46 | parseSourceString(firstModuleStr, &block, &context); |
| 47 | EXPECT_TRUE(succeeded(firstParse)); |
| 48 | |
| 49 | // Parse the second module string. |
| 50 | LogicalResult secondParse = |
| 51 | parseSourceString(secondModuleStr, &block, &context); |
| 52 | EXPECT_TRUE(succeeded(secondParse)); |
| 53 | |
| 54 | // Check the we parse at the end. |
| 55 | EXPECT_EQ(block.front().getName().getStringRef(), "test.first"); |
| 56 | EXPECT_EQ(block.back().getName().getStringRef(), "test.second"); |
| 57 | } |
River Riddle | 1ae60e0 | 2022-09-29 01:39:26 | [diff] [blame] | 58 | } // namespace |