blob: de489e43ef22a7330dfd75809378ad18dac348d6 [file] [log] [blame]
Kate Stoneb9c1b512016-09-06 20:57:50 +00001//===-- GoParserTest.cpp ------------------------------------------*- C++
2//-*-===//
Ryan Brown998c8a1c12015-11-02 19:30:40 +00003//
4// The LLVM Compiler Infrastructure
5//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#if defined(_MSC_VER) && (_HAS_EXCEPTIONS == 0)
Kate Stoneb9c1b512016-09-06 20:57:50 +000012// Workaround for MSVC standard library bug, which fails to include <thread>
13// when
Ryan Brown998c8a1c12015-11-02 19:30:40 +000014// exceptions are disabled.
15#include <eh.h>
16#endif
17
18#include <sstream>
19
20#include "gtest/gtest.h"
21
Ryan Brown0fbd187d2015-11-03 22:46:37 +000022#include "Plugins/ExpressionParser/Go/GoParser.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000023#include "lldb/Core/Error.h"
Ryan Brown998c8a1c12015-11-02 19:30:40 +000024
25using namespace lldb_private;
26
Kate Stoneb9c1b512016-09-06 20:57:50 +000027namespace {
28struct ASTPrinter {
29 ASTPrinter(GoASTNode *n) { (*this)(n); }
Ryan Brown998c8a1c12015-11-02 19:30:40 +000030
Kate Stoneb9c1b512016-09-06 20:57:50 +000031 void operator()(GoASTNode *n) {
32 if (n == nullptr) {
33 m_stream << "nil ";
34 return;
Ryan Brown998c8a1c12015-11-02 19:30:40 +000035 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000036 m_stream << "(" << n->GetKindName() << " ";
37 n->WalkChildren(*this);
38 if (auto *nn = llvm::dyn_cast<GoASTAssignStmt>(n))
39 m_stream << nn->GetDefine() << " ";
40 if (auto *nn = llvm::dyn_cast<GoASTBasicLit>(n))
41 m_stream << nn->GetValue().m_value.str() << " ";
42 if (auto *nn = llvm::dyn_cast<GoASTBinaryExpr>(n))
43 m_stream << GoLexer::LookupToken(nn->GetOp()).str() << " ";
44 if (auto *nn = llvm::dyn_cast<GoASTIdent>(n))
45 m_stream << nn->GetName().m_value.str() << " ";
46 if (auto *nn = llvm::dyn_cast<GoASTBranchStmt>(n))
47 m_stream << GoLexer::LookupToken(nn->GetTok()).str() << " ";
48 if (auto *nn = llvm::dyn_cast<GoASTCallExpr>(n))
49 m_stream << (nn->GetEllipsis() ? "..." : "") << " ";
50 if (auto *nn = llvm::dyn_cast<GoASTChanType>(n))
51 m_stream << nn->GetDir() << " ";
52 if (auto *nn = llvm::dyn_cast<GoASTGenDecl>(n))
53 m_stream << GoLexer::LookupToken(nn->GetTok()).str() << " ";
54 if (auto *nn = llvm::dyn_cast<GoASTIncDecStmt>(n))
55 m_stream << GoLexer::LookupToken(nn->GetTok()).str() << " ";
56 if (auto *nn = llvm::dyn_cast<GoASTRangeStmt>(n))
57 m_stream << nn->GetDefine() << " ";
58 if (auto *nn = llvm::dyn_cast<GoASTSliceExpr>(n))
59 m_stream << nn->GetSlice3() << " ";
60 if (auto *nn = llvm::dyn_cast<GoASTUnaryExpr>(n))
61 m_stream << GoLexer::LookupToken(nn->GetOp()).str() << " ";
62 m_stream << ") ";
63 }
Ryan Brown998c8a1c12015-11-02 19:30:40 +000064
Kate Stoneb9c1b512016-09-06 20:57:50 +000065 const std::string str() const { return m_stream.str(); }
66 std::stringstream m_stream;
Ryan Brown998c8a1c12015-11-02 19:30:40 +000067};
68
Kate Stoneb9c1b512016-09-06 20:57:50 +000069testing::AssertionResult CheckStatement(const char *_s, const char *c_expr,
70 const char *sexpr, const char *code) {
71 GoParser parser(code);
72 std::unique_ptr<GoASTStmt> stmt(parser.Statement());
73 if (parser.Failed() || !stmt) {
74 Error err;
75 parser.GetError(err);
76 return testing::AssertionFailure() << "Error parsing " << c_expr << "\n\t"
77 << err.AsCString();
78 }
79 std::string actual_sexpr = ASTPrinter(stmt.get()).str();
80 if (actual_sexpr == sexpr)
81 return testing::AssertionSuccess();
82 return testing::AssertionFailure() << "Parsing: " << c_expr
83 << "\nExpected: " << sexpr
84 << "\nGot: " << actual_sexpr;
Ryan Brown998c8a1c12015-11-02 19:30:40 +000085}
86} // namespace
87
88#define EXPECT_PARSE(s, c) EXPECT_PRED_FORMAT2(CheckStatement, s, c)
89
Kate Stoneb9c1b512016-09-06 20:57:50 +000090TEST(GoParserTest, ParseBasicLiterals) {
91 EXPECT_PARSE("(ExprStmt (BasicLit 0 ) ) ", "0");
92 EXPECT_PARSE("(ExprStmt (BasicLit 42 ) ) ", "42");
93 EXPECT_PARSE("(ExprStmt (BasicLit 0600 ) ) ", "0600");
94 EXPECT_PARSE("(ExprStmt (BasicLit 0xBadFace ) ) ", "0xBadFace");
95 EXPECT_PARSE(
96 "(ExprStmt (BasicLit 170141183460469231731687303715884105727 ) ) ",
97 "170141183460469231731687303715884105727");
Ryan Brown998c8a1c12015-11-02 19:30:40 +000098
Kate Stoneb9c1b512016-09-06 20:57:50 +000099 EXPECT_PARSE("(ExprStmt (BasicLit 0. ) ) ", "0.");
100 EXPECT_PARSE("(ExprStmt (BasicLit 72.40 ) ) ", "72.40");
101 EXPECT_PARSE("(ExprStmt (BasicLit 072.40 ) ) ", "072.40");
102 EXPECT_PARSE("(ExprStmt (BasicLit 2.71828 ) ) ", "2.71828");
103 EXPECT_PARSE("(ExprStmt (BasicLit 1.e+0 ) ) ", "1.e+0");
104 EXPECT_PARSE("(ExprStmt (BasicLit 6.67428e-11 ) ) ", "6.67428e-11");
105 EXPECT_PARSE("(ExprStmt (BasicLit 1E6 ) ) ", "1E6");
106 EXPECT_PARSE("(ExprStmt (BasicLit .12345E+6 ) ) ", ".12345E+6");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000107
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108 EXPECT_PARSE("(ExprStmt (BasicLit 0i ) ) ", "0i");
109 EXPECT_PARSE("(ExprStmt (BasicLit 011i ) ) ", "011i");
110 EXPECT_PARSE("(ExprStmt (BasicLit 0.i ) ) ", "0.i");
111 EXPECT_PARSE("(ExprStmt (BasicLit 2.71828i ) ) ", "2.71828i");
112 EXPECT_PARSE("(ExprStmt (BasicLit 6.67428e-11i ) ) ", "6.67428e-11i");
113 EXPECT_PARSE("(ExprStmt (BasicLit 1E6i ) ) ", "1E6i");
114 EXPECT_PARSE("(ExprStmt (BasicLit .12345E+6i ) ) ", ".12345E+6i");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000115
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116 EXPECT_PARSE("(ExprStmt (BasicLit 'a' ) ) ", "'a'");
117 EXPECT_PARSE("(ExprStmt (BasicLit '本' ) ) ", "'本'");
118 EXPECT_PARSE("(ExprStmt (BasicLit \"abc\" ) ) ", "\"abc\"");
119 EXPECT_PARSE("(ExprStmt (BasicLit `abc` ) ) ", "`abc`");
120 EXPECT_PARSE("(ExprStmt (BasicLit `ab\nc` ) ) ", "`ab\nc`");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000121}
122
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123TEST(GoParserTest, ParseOperand) {
124 EXPECT_PARSE("(ExprStmt (Ident a ) ) ", "a");
125 EXPECT_PARSE("(ExprStmt (Ident _x9 ) ) ", "_x9");
126 EXPECT_PARSE("(ExprStmt (Ident ThisVariableIsExported ) ) ",
127 "ThisVariableIsExported");
128 EXPECT_PARSE("(ExprStmt (Ident αβ ) ) ", "αβ");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 EXPECT_PARSE("(ExprStmt (SelectorExpr (Ident math ) (Ident Sin ) ) ) ",
131 "math.Sin");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000132}
133
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134TEST(GoParserTest, ParseCompositeLiterals) {
135 EXPECT_PARSE("(ExprStmt (CompositeLit (Ident Point3D ) ) ) ", "Point3D{}");
136 EXPECT_PARSE("(ExprStmt (CompositeLit (Ident Line ) (Ident origin ) "
137 "(CompositeLit (Ident Point3D ) (KeyValueExpr "
138 "(Ident y ) (UnaryExpr (BasicLit 4 ) - ) ) (KeyValueExpr (Ident "
139 "z ) (BasicLit 12.3 ) ) ) ) ) ",
140 "Line{origin, Point3D{y: -4, z: 12.3}}");
141 EXPECT_PARSE("(ExprStmt (CompositeLit (ArrayType (BasicLit 10 ) (Ident "
142 "string ) ) ) ) ",
143 "[10]string{}");
144 EXPECT_PARSE("(ExprStmt (CompositeLit (ArrayType (BasicLit 6 ) (Ident int ) "
145 ") (BasicLit 1 ) (BasicLit 2 ) "
146 "(BasicLit 3 ) (BasicLit 5 ) ) ) ",
147 "[6]int {1, 2, 3, 5}");
148 EXPECT_PARSE("(ExprStmt (CompositeLit (ArrayType nil (Ident int ) ) "
149 "(BasicLit 2 ) (BasicLit 3 ) (BasicLit 5 ) "
150 "(BasicLit 7 ) (BasicLit 9 ) (BasicLit 2147483647 ) ) ) ",
151 "[]int{2, 3, 5, 7, 9, 2147483647}");
152 EXPECT_PARSE("(ExprStmt (CompositeLit (ArrayType (BasicLit 128 ) (Ident bool "
153 ") ) (KeyValueExpr (BasicLit 'a' ) "
154 "(Ident true ) ) (KeyValueExpr (BasicLit 'e' ) (Ident true ) ) "
155 "(KeyValueExpr (BasicLit 'i' ) (Ident "
156 "true ) ) (KeyValueExpr (BasicLit 'o' ) (Ident true ) ) "
157 "(KeyValueExpr (BasicLit 'u' ) (Ident true ) ) "
158 "(KeyValueExpr (BasicLit 'y' ) (Ident true ) ) ) ) ",
159 "[128]bool{'a': true, 'e': true, 'i': true, 'o': true, 'u': "
160 "true, 'y': true}");
161 EXPECT_PARSE(
162 "(ExprStmt (CompositeLit (ArrayType (BasicLit 10 ) (Ident float32 ) ) "
163 "(UnaryExpr (BasicLit 1 ) - ) "
164 "(KeyValueExpr (BasicLit 4 ) (UnaryExpr (BasicLit 0.1 ) - ) ) (UnaryExpr "
165 "(BasicLit 0.1 ) - ) "
166 "(KeyValueExpr (BasicLit 9 ) (UnaryExpr (BasicLit 1 ) - ) ) ) ) ",
167 "[10]float32{-1, 4: -0.1, -0.1, 9: -1}");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000168}
169
Kate Stoneb9c1b512016-09-06 20:57:50 +0000170TEST(GoParserTest, ParseEllipsisArray) {
171 EXPECT_PARSE("(ExprStmt (CompositeLit (ArrayType (Ellipsis nil ) (Ident "
172 "string ) ) (BasicLit `Sat` ) (BasicLit `Sun` ) ) ) ",
173 "[...]string {`Sat`, `Sun`}");
174 EXPECT_PARSE("(ExprStmt (CompositeLit (ArrayType (Ellipsis nil ) (Ident "
175 "Point ) ) (CompositeLit nil (BasicLit 1.5 "
176 ") (UnaryExpr (BasicLit 3.5 ) - ) ) (CompositeLit nil (BasicLit "
177 "0 ) (BasicLit 0 ) ) ) ) ",
178 "[...]Point{{1.5, -3.5}, {0, 0}}");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000179}
180
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181TEST(GoParserTest, ParseMap) {
182 EXPECT_PARSE("(ExprStmt (CompositeLit (MapType (Ident string ) (Ident "
183 "float32 ) ) (KeyValueExpr (BasicLit `C0` ) "
184 "(BasicLit 16.35 ) ) (KeyValueExpr (BasicLit `D0` ) (BasicLit "
185 "18.35 ) ) ) ) ",
186 "map[string]float32{`C0`: 16.35, `D0`: 18.35, }");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000187}
188
Kate Stoneb9c1b512016-09-06 20:57:50 +0000189TEST(GoParserTest, UnaryExpr) {
190 EXPECT_PARSE("(ExprStmt (UnaryExpr (Ident x ) + ) ) ", "+x");
191 EXPECT_PARSE("(ExprStmt (UnaryExpr (Ident x ) - ) ) ", "-x");
192 EXPECT_PARSE("(ExprStmt (UnaryExpr (Ident x ) ! ) ) ", "!x");
193 EXPECT_PARSE("(ExprStmt (UnaryExpr (Ident x ) ^ ) ) ", "^x");
194 EXPECT_PARSE("(ExprStmt (UnaryExpr (Ident x ) & ) ) ", "&x");
195 EXPECT_PARSE("(ExprStmt (UnaryExpr (Ident x ) <- ) ) ", "<-x");
196 EXPECT_PARSE("(ExprStmt (StarExpr (Ident x ) ) ) ", "*x");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000197}
198
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199TEST(GoParserTest, BinaryExpr) {
200 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (Ident b ) || ) ) ", "a || b");
201 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (Ident b ) && ) ) ", "a && b");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000202
Kate Stoneb9c1b512016-09-06 20:57:50 +0000203 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (Ident b ) == ) ) ", "a == b");
204 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (Ident b ) != ) ) ", "a != b");
205 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (Ident b ) < ) ) ", "a < b");
206 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (Ident b ) <= ) ) ", "a <= b");
207 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (Ident b ) > ) ) ", "a > b");
208 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (Ident b ) >= ) ) ", "a >= b");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000209
Kate Stoneb9c1b512016-09-06 20:57:50 +0000210 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (Ident b ) + ) ) ", "a + b");
211 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (Ident b ) - ) ) ", "a - b");
212 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (Ident b ) | ) ) ", "a | b");
213 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (Ident b ) ^ ) ) ", "a ^ b");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000214
Kate Stoneb9c1b512016-09-06 20:57:50 +0000215 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (Ident b ) * ) ) ", "a * b");
216 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (Ident b ) / ) ) ", "a / b");
217 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (Ident b ) % ) ) ", "a % b");
218 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (Ident b ) << ) ) ", "a << b");
219 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (Ident b ) >> ) ) ", "a >> b");
220 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (Ident b ) & ) ) ", "a & b");
221 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (Ident b ) &^ ) ) ", "a &^ b");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000222
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223 EXPECT_PARSE("(ExprStmt (BinaryExpr (BasicLit 23 ) (BinaryExpr (BasicLit 3 ) "
224 "(IndexExpr (Ident x ) (Ident i ) ) * ) + ) ) ",
225 "23 + 3*x[i]");
226 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (UnaryExpr (UnaryExpr (Ident "
227 "a ) + ) + ) + ) ) ",
228 "a + + + a");
229 EXPECT_PARSE(
230 "(ExprStmt (BinaryExpr (UnaryExpr (Ident a ) ^ ) (Ident b ) >> ) ) ",
231 "^a >> b");
232 EXPECT_PARSE("(ExprStmt (BinaryExpr (CallExpr (Ident f ) ) (CallExpr (Ident "
233 "g ) ) || ) ) ",
234 "f() || g()");
235 EXPECT_PARSE(
236 "(ExprStmt (BinaryExpr (BinaryExpr (Ident x ) (BinaryExpr (Ident y ) "
237 "(BasicLit 1 ) + ) == ) "
238 "(BinaryExpr (UnaryExpr (Ident chanPtr ) <- ) (BasicLit 0 ) > ) && ) ) ",
239 "x == y+1 && <-chanPtr > 0");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000240}
241
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242TEST(GoParserTest, PrimaryExpr) {
243 EXPECT_PARSE(
244 "(ExprStmt (BinaryExpr (Ident x ) (CallExpr (Ident f ) ) <= ) ) ",
245 "x <= f()");
246 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident s ) (BasicLit `.txt` ) + ) ) ",
247 "(s + `.txt`)");
248 EXPECT_PARSE(
249 "(ExprStmt (CallExpr (Ident f ) (BasicLit 3.1415 ) (Ident true ) ) ) ",
250 "f(3.1415, true)");
251 EXPECT_PARSE(
252 "(ExprStmt (CallExpr (Ident f ) (BasicLit 3.1415 ) (Ident a ) ... ) ) ",
253 "f(3.1415, a...)");
254 EXPECT_PARSE("(ExprStmt (IndexExpr (Ident m ) (BasicLit '1' ) ) ) ",
255 "m['1']");
256 EXPECT_PARSE("(ExprStmt (SliceExpr (Ident s ) (Ident i ) (BinaryExpr (Ident "
257 "j ) (BasicLit 1 ) + ) nil 0 ) ) ",
258 "s[i : j + 1]");
259 EXPECT_PARSE("(ExprStmt (SelectorExpr (Ident obj ) (Ident color ) ) ) ",
260 "obj.color");
261 EXPECT_PARSE("(ExprStmt (CallExpr (SelectorExpr (IndexExpr (SelectorExpr "
262 "(Ident f ) (Ident p ) ) (Ident i ) ) "
263 "(Ident x ) ) ) ) ",
264 "f.p[i].x()");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000265}
266
Kate Stoneb9c1b512016-09-06 20:57:50 +0000267TEST(GoParserTest, Conversions) {
268 EXPECT_PARSE(
269 "(ExprStmt (StarExpr (CallExpr (Ident Point ) (Ident p ) ) ) ) ",
270 "*Point(p)");
271 EXPECT_PARSE(
272 "(ExprStmt (CallExpr (StarExpr (Ident Point ) ) (Ident p ) ) ) ",
273 "(*Point)(p)");
274 EXPECT_PARSE("(ExprStmt (UnaryExpr (CallExpr (ChanType (Ident int ) 0 ) "
275 "(Ident c ) ) <- ) ) ",
276 "<-chan int(c)");
277 EXPECT_PARSE("(ExprStmt (TypeAssertExpr (Ident y ) (SelectorExpr (Ident io ) "
278 "(Ident Reader ) ) ) ) ",
279 "y.(io.Reader)");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000280}