1 //===- ParserTest.cpp -----------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "mlir/Parser/Parser.h"
10 #include "mlir/AsmParser/AsmParser.h"
11 #include "mlir/IR/BuiltinOps.h"
12 #include "mlir/IR/Verifier.h"
14 #include "gmock/gmock.h"
19 TEST(MLIRParser
, ParseInvalidIR
) {
20 std::string moduleStr
= R
"mlir(
21 module attributes {bad} {}
25 ParserConfig
config(&context
, /*verifyAfterParse=*/false);
27 // Check that we properly parse the op, but it fails the verifier.
28 OwningOpRef
<ModuleOp
> module
= parseSourceString
<ModuleOp
>(moduleStr
, config
);
30 ASSERT_TRUE(failed(verify(*module
)));
33 TEST(MLIRParser
, ParseAtEnd
) {
34 std::string firstModuleStr
= R
"mlir(
35 "test
.first
"() : () -> ()
37 std::string secondModuleStr
= R
"mlir(
38 "test
.second
"() : () -> ()
42 context
.allowUnregisteredDialects();
45 // Parse the first module string.
46 LogicalResult firstParse
=
47 parseSourceString(firstModuleStr
, &block
, &context
);
48 EXPECT_TRUE(succeeded(firstParse
));
50 // Parse the second module string.
51 LogicalResult secondParse
=
52 parseSourceString(secondModuleStr
, &block
, &context
);
53 EXPECT_TRUE(succeeded(secondParse
));
55 // Check the we parse at the end.
56 EXPECT_EQ(block
.front().getName().getStringRef(), "test.first");
57 EXPECT_EQ(block
.back().getName().getStringRef(), "test.second");
60 TEST(MLIRParser
, ParseAttr
) {
61 using namespace testing
;
65 StringLiteral attrAsm
= "array<i64: 1, 2, 3>";
67 Attribute attr
= parseAttribute(attrAsm
, &context
, Type(), &numRead
);
68 EXPECT_EQ(attr
, b
.getDenseI64ArrayAttr({1, 2, 3}));
69 EXPECT_EQ(numRead
, attrAsm
.size());
72 std::vector
<std::string
> diagnostics
;
73 ScopedDiagnosticHandler
handler(&context
, [&](Diagnostic
&d
) {
74 llvm::raw_string_ostream(diagnostics
.emplace_back())
75 << d
.getLocation() << ": " << d
;
78 EXPECT_FALSE(parseAttribute("dense<>", &context
, Type(), &numRead
));
79 EXPECT_THAT(diagnostics
, ElementsAre("loc(\"dense<>\":1:7): expected ':'"));
80 EXPECT_EQ(numRead
, size_t(0));
82 { // Parse with trailing characters
83 std::vector
<std::string
> diagnostics
;
84 ScopedDiagnosticHandler
handler(&context
, [&](Diagnostic
&d
) {
85 llvm::raw_string_ostream(diagnostics
.emplace_back())
86 << d
.getLocation() << ": " << d
;
88 EXPECT_FALSE(parseAttribute("10 foo", &context
));
91 ElementsAre("loc(\"10 foo\":1:5): found trailing characters: 'foo'"));
94 EXPECT_EQ(parseAttribute("10 foo", &context
, Type(), &numRead
),
95 b
.getI64IntegerAttr(10));
96 EXPECT_EQ(numRead
, size_t(4)); // includes trailing whitespace
98 { // Parse without null-terminator
99 StringRef
attrAsm("999", 1);
100 Attribute attr
= parseAttribute(attrAsm
, &context
);
101 EXPECT_EQ(attr
, b
.getI64IntegerAttr(9));