blob: 710dc5e4e86f2c64f33d2ca5f0f755536441d759 [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
Ryan Brown998c8a1c12015-11-02 19:30:40 +000011#include <sstream>
12
13#include "gtest/gtest.h"
14
Ryan Brown0fbd187d2015-11-03 22:46:37 +000015#include "Plugins/ExpressionParser/Go/GoParser.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000016#include "lldb/Utility/Error.h"
Ryan Brown998c8a1c12015-11-02 19:30:40 +000017
18using namespace lldb_private;
19
Kate Stoneb9c1b512016-09-06 20:57:50 +000020namespace {
21struct ASTPrinter {
22 ASTPrinter(GoASTNode *n) { (*this)(n); }
Ryan Brown998c8a1c12015-11-02 19:30:40 +000023
Kate Stoneb9c1b512016-09-06 20:57:50 +000024 void operator()(GoASTNode *n) {
25 if (n == nullptr) {
26 m_stream << "nil ";
27 return;
Ryan Brown998c8a1c12015-11-02 19:30:40 +000028 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000029 m_stream << "(" << n->GetKindName() << " ";
30 n->WalkChildren(*this);
31 if (auto *nn = llvm::dyn_cast<GoASTAssignStmt>(n))
32 m_stream << nn->GetDefine() << " ";
33 if (auto *nn = llvm::dyn_cast<GoASTBasicLit>(n))
34 m_stream << nn->GetValue().m_value.str() << " ";
35 if (auto *nn = llvm::dyn_cast<GoASTBinaryExpr>(n))
36 m_stream << GoLexer::LookupToken(nn->GetOp()).str() << " ";
37 if (auto *nn = llvm::dyn_cast<GoASTIdent>(n))
38 m_stream << nn->GetName().m_value.str() << " ";
39 if (auto *nn = llvm::dyn_cast<GoASTBranchStmt>(n))
40 m_stream << GoLexer::LookupToken(nn->GetTok()).str() << " ";
41 if (auto *nn = llvm::dyn_cast<GoASTCallExpr>(n))
42 m_stream << (nn->GetEllipsis() ? "..." : "") << " ";
43 if (auto *nn = llvm::dyn_cast<GoASTChanType>(n))
44 m_stream << nn->GetDir() << " ";
45 if (auto *nn = llvm::dyn_cast<GoASTGenDecl>(n))
46 m_stream << GoLexer::LookupToken(nn->GetTok()).str() << " ";
47 if (auto *nn = llvm::dyn_cast<GoASTIncDecStmt>(n))
48 m_stream << GoLexer::LookupToken(nn->GetTok()).str() << " ";
49 if (auto *nn = llvm::dyn_cast<GoASTRangeStmt>(n))
50 m_stream << nn->GetDefine() << " ";
51 if (auto *nn = llvm::dyn_cast<GoASTSliceExpr>(n))
52 m_stream << nn->GetSlice3() << " ";
53 if (auto *nn = llvm::dyn_cast<GoASTUnaryExpr>(n))
54 m_stream << GoLexer::LookupToken(nn->GetOp()).str() << " ";
55 m_stream << ") ";
56 }
Ryan Brown998c8a1c12015-11-02 19:30:40 +000057
Kate Stoneb9c1b512016-09-06 20:57:50 +000058 const std::string str() const { return m_stream.str(); }
59 std::stringstream m_stream;
Ryan Brown998c8a1c12015-11-02 19:30:40 +000060};
61
Kate Stoneb9c1b512016-09-06 20:57:50 +000062testing::AssertionResult CheckStatement(const char *_s, const char *c_expr,
63 const char *sexpr, const char *code) {
64 GoParser parser(code);
65 std::unique_ptr<GoASTStmt> stmt(parser.Statement());
66 if (parser.Failed() || !stmt) {
67 Error err;
68 parser.GetError(err);
69 return testing::AssertionFailure() << "Error parsing " << c_expr << "\n\t"
70 << err.AsCString();
71 }
72 std::string actual_sexpr = ASTPrinter(stmt.get()).str();
73 if (actual_sexpr == sexpr)
74 return testing::AssertionSuccess();
75 return testing::AssertionFailure() << "Parsing: " << c_expr
76 << "\nExpected: " << sexpr
77 << "\nGot: " << actual_sexpr;
Ryan Brown998c8a1c12015-11-02 19:30:40 +000078}
79} // namespace
80
81#define EXPECT_PARSE(s, c) EXPECT_PRED_FORMAT2(CheckStatement, s, c)
82
Kate Stoneb9c1b512016-09-06 20:57:50 +000083TEST(GoParserTest, ParseBasicLiterals) {
84 EXPECT_PARSE("(ExprStmt (BasicLit 0 ) ) ", "0");
85 EXPECT_PARSE("(ExprStmt (BasicLit 42 ) ) ", "42");
86 EXPECT_PARSE("(ExprStmt (BasicLit 0600 ) ) ", "0600");
87 EXPECT_PARSE("(ExprStmt (BasicLit 0xBadFace ) ) ", "0xBadFace");
88 EXPECT_PARSE(
89 "(ExprStmt (BasicLit 170141183460469231731687303715884105727 ) ) ",
90 "170141183460469231731687303715884105727");
Ryan Brown998c8a1c12015-11-02 19:30:40 +000091
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 EXPECT_PARSE("(ExprStmt (BasicLit 0. ) ) ", "0.");
93 EXPECT_PARSE("(ExprStmt (BasicLit 72.40 ) ) ", "72.40");
94 EXPECT_PARSE("(ExprStmt (BasicLit 072.40 ) ) ", "072.40");
95 EXPECT_PARSE("(ExprStmt (BasicLit 2.71828 ) ) ", "2.71828");
96 EXPECT_PARSE("(ExprStmt (BasicLit 1.e+0 ) ) ", "1.e+0");
97 EXPECT_PARSE("(ExprStmt (BasicLit 6.67428e-11 ) ) ", "6.67428e-11");
98 EXPECT_PARSE("(ExprStmt (BasicLit 1E6 ) ) ", "1E6");
99 EXPECT_PARSE("(ExprStmt (BasicLit .12345E+6 ) ) ", ".12345E+6");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000100
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101 EXPECT_PARSE("(ExprStmt (BasicLit 0i ) ) ", "0i");
102 EXPECT_PARSE("(ExprStmt (BasicLit 011i ) ) ", "011i");
103 EXPECT_PARSE("(ExprStmt (BasicLit 0.i ) ) ", "0.i");
104 EXPECT_PARSE("(ExprStmt (BasicLit 2.71828i ) ) ", "2.71828i");
105 EXPECT_PARSE("(ExprStmt (BasicLit 6.67428e-11i ) ) ", "6.67428e-11i");
106 EXPECT_PARSE("(ExprStmt (BasicLit 1E6i ) ) ", "1E6i");
107 EXPECT_PARSE("(ExprStmt (BasicLit .12345E+6i ) ) ", ".12345E+6i");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000108
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109 EXPECT_PARSE("(ExprStmt (BasicLit 'a' ) ) ", "'a'");
110 EXPECT_PARSE("(ExprStmt (BasicLit '本' ) ) ", "'本'");
111 EXPECT_PARSE("(ExprStmt (BasicLit \"abc\" ) ) ", "\"abc\"");
112 EXPECT_PARSE("(ExprStmt (BasicLit `abc` ) ) ", "`abc`");
113 EXPECT_PARSE("(ExprStmt (BasicLit `ab\nc` ) ) ", "`ab\nc`");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000114}
115
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116TEST(GoParserTest, ParseOperand) {
117 EXPECT_PARSE("(ExprStmt (Ident a ) ) ", "a");
118 EXPECT_PARSE("(ExprStmt (Ident _x9 ) ) ", "_x9");
119 EXPECT_PARSE("(ExprStmt (Ident ThisVariableIsExported ) ) ",
120 "ThisVariableIsExported");
121 EXPECT_PARSE("(ExprStmt (Ident αβ ) ) ", "αβ");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000122
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 EXPECT_PARSE("(ExprStmt (SelectorExpr (Ident math ) (Ident Sin ) ) ) ",
124 "math.Sin");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000125}
126
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127TEST(GoParserTest, ParseCompositeLiterals) {
128 EXPECT_PARSE("(ExprStmt (CompositeLit (Ident Point3D ) ) ) ", "Point3D{}");
129 EXPECT_PARSE("(ExprStmt (CompositeLit (Ident Line ) (Ident origin ) "
130 "(CompositeLit (Ident Point3D ) (KeyValueExpr "
131 "(Ident y ) (UnaryExpr (BasicLit 4 ) - ) ) (KeyValueExpr (Ident "
132 "z ) (BasicLit 12.3 ) ) ) ) ) ",
133 "Line{origin, Point3D{y: -4, z: 12.3}}");
134 EXPECT_PARSE("(ExprStmt (CompositeLit (ArrayType (BasicLit 10 ) (Ident "
135 "string ) ) ) ) ",
136 "[10]string{}");
137 EXPECT_PARSE("(ExprStmt (CompositeLit (ArrayType (BasicLit 6 ) (Ident int ) "
138 ") (BasicLit 1 ) (BasicLit 2 ) "
139 "(BasicLit 3 ) (BasicLit 5 ) ) ) ",
140 "[6]int {1, 2, 3, 5}");
141 EXPECT_PARSE("(ExprStmt (CompositeLit (ArrayType nil (Ident int ) ) "
142 "(BasicLit 2 ) (BasicLit 3 ) (BasicLit 5 ) "
143 "(BasicLit 7 ) (BasicLit 9 ) (BasicLit 2147483647 ) ) ) ",
144 "[]int{2, 3, 5, 7, 9, 2147483647}");
145 EXPECT_PARSE("(ExprStmt (CompositeLit (ArrayType (BasicLit 128 ) (Ident bool "
146 ") ) (KeyValueExpr (BasicLit 'a' ) "
147 "(Ident true ) ) (KeyValueExpr (BasicLit 'e' ) (Ident true ) ) "
148 "(KeyValueExpr (BasicLit 'i' ) (Ident "
149 "true ) ) (KeyValueExpr (BasicLit 'o' ) (Ident true ) ) "
150 "(KeyValueExpr (BasicLit 'u' ) (Ident true ) ) "
151 "(KeyValueExpr (BasicLit 'y' ) (Ident true ) ) ) ) ",
152 "[128]bool{'a': true, 'e': true, 'i': true, 'o': true, 'u': "
153 "true, 'y': true}");
154 EXPECT_PARSE(
155 "(ExprStmt (CompositeLit (ArrayType (BasicLit 10 ) (Ident float32 ) ) "
156 "(UnaryExpr (BasicLit 1 ) - ) "
157 "(KeyValueExpr (BasicLit 4 ) (UnaryExpr (BasicLit 0.1 ) - ) ) (UnaryExpr "
158 "(BasicLit 0.1 ) - ) "
159 "(KeyValueExpr (BasicLit 9 ) (UnaryExpr (BasicLit 1 ) - ) ) ) ) ",
160 "[10]float32{-1, 4: -0.1, -0.1, 9: -1}");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000161}
162
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163TEST(GoParserTest, ParseEllipsisArray) {
164 EXPECT_PARSE("(ExprStmt (CompositeLit (ArrayType (Ellipsis nil ) (Ident "
165 "string ) ) (BasicLit `Sat` ) (BasicLit `Sun` ) ) ) ",
166 "[...]string {`Sat`, `Sun`}");
167 EXPECT_PARSE("(ExprStmt (CompositeLit (ArrayType (Ellipsis nil ) (Ident "
168 "Point ) ) (CompositeLit nil (BasicLit 1.5 "
169 ") (UnaryExpr (BasicLit 3.5 ) - ) ) (CompositeLit nil (BasicLit "
170 "0 ) (BasicLit 0 ) ) ) ) ",
171 "[...]Point{{1.5, -3.5}, {0, 0}}");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000172}
173
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174TEST(GoParserTest, ParseMap) {
175 EXPECT_PARSE("(ExprStmt (CompositeLit (MapType (Ident string ) (Ident "
176 "float32 ) ) (KeyValueExpr (BasicLit `C0` ) "
177 "(BasicLit 16.35 ) ) (KeyValueExpr (BasicLit `D0` ) (BasicLit "
178 "18.35 ) ) ) ) ",
179 "map[string]float32{`C0`: 16.35, `D0`: 18.35, }");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000180}
181
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182TEST(GoParserTest, UnaryExpr) {
183 EXPECT_PARSE("(ExprStmt (UnaryExpr (Ident x ) + ) ) ", "+x");
184 EXPECT_PARSE("(ExprStmt (UnaryExpr (Ident x ) - ) ) ", "-x");
185 EXPECT_PARSE("(ExprStmt (UnaryExpr (Ident x ) ! ) ) ", "!x");
186 EXPECT_PARSE("(ExprStmt (UnaryExpr (Ident x ) ^ ) ) ", "^x");
187 EXPECT_PARSE("(ExprStmt (UnaryExpr (Ident x ) & ) ) ", "&x");
188 EXPECT_PARSE("(ExprStmt (UnaryExpr (Ident x ) <- ) ) ", "<-x");
189 EXPECT_PARSE("(ExprStmt (StarExpr (Ident x ) ) ) ", "*x");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000190}
191
Kate Stoneb9c1b512016-09-06 20:57:50 +0000192TEST(GoParserTest, BinaryExpr) {
193 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (Ident b ) || ) ) ", "a || b");
194 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (Ident b ) && ) ) ", "a && b");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000195
Kate Stoneb9c1b512016-09-06 20:57:50 +0000196 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (Ident b ) == ) ) ", "a == b");
197 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (Ident b ) != ) ) ", "a != b");
198 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (Ident b ) < ) ) ", "a < b");
199 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (Ident b ) <= ) ) ", "a <= b");
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");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000207
Kate Stoneb9c1b512016-09-06 20:57:50 +0000208 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (Ident b ) * ) ) ", "a * b");
209 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (Ident b ) / ) ) ", "a / b");
210 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");
214 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (Ident b ) &^ ) ) ", "a &^ b");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000215
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216 EXPECT_PARSE("(ExprStmt (BinaryExpr (BasicLit 23 ) (BinaryExpr (BasicLit 3 ) "
217 "(IndexExpr (Ident x ) (Ident i ) ) * ) + ) ) ",
218 "23 + 3*x[i]");
219 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (UnaryExpr (UnaryExpr (Ident "
220 "a ) + ) + ) + ) ) ",
221 "a + + + a");
222 EXPECT_PARSE(
223 "(ExprStmt (BinaryExpr (UnaryExpr (Ident a ) ^ ) (Ident b ) >> ) ) ",
224 "^a >> b");
225 EXPECT_PARSE("(ExprStmt (BinaryExpr (CallExpr (Ident f ) ) (CallExpr (Ident "
226 "g ) ) || ) ) ",
227 "f() || g()");
228 EXPECT_PARSE(
229 "(ExprStmt (BinaryExpr (BinaryExpr (Ident x ) (BinaryExpr (Ident y ) "
230 "(BasicLit 1 ) + ) == ) "
231 "(BinaryExpr (UnaryExpr (Ident chanPtr ) <- ) (BasicLit 0 ) > ) && ) ) ",
232 "x == y+1 && <-chanPtr > 0");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000233}
234
Kate Stoneb9c1b512016-09-06 20:57:50 +0000235TEST(GoParserTest, PrimaryExpr) {
236 EXPECT_PARSE(
237 "(ExprStmt (BinaryExpr (Ident x ) (CallExpr (Ident f ) ) <= ) ) ",
238 "x <= f()");
239 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident s ) (BasicLit `.txt` ) + ) ) ",
240 "(s + `.txt`)");
241 EXPECT_PARSE(
242 "(ExprStmt (CallExpr (Ident f ) (BasicLit 3.1415 ) (Ident true ) ) ) ",
243 "f(3.1415, true)");
244 EXPECT_PARSE(
245 "(ExprStmt (CallExpr (Ident f ) (BasicLit 3.1415 ) (Ident a ) ... ) ) ",
246 "f(3.1415, a...)");
247 EXPECT_PARSE("(ExprStmt (IndexExpr (Ident m ) (BasicLit '1' ) ) ) ",
248 "m['1']");
249 EXPECT_PARSE("(ExprStmt (SliceExpr (Ident s ) (Ident i ) (BinaryExpr (Ident "
250 "j ) (BasicLit 1 ) + ) nil 0 ) ) ",
251 "s[i : j + 1]");
252 EXPECT_PARSE("(ExprStmt (SelectorExpr (Ident obj ) (Ident color ) ) ) ",
253 "obj.color");
254 EXPECT_PARSE("(ExprStmt (CallExpr (SelectorExpr (IndexExpr (SelectorExpr "
255 "(Ident f ) (Ident p ) ) (Ident i ) ) "
256 "(Ident x ) ) ) ) ",
257 "f.p[i].x()");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000258}
259
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260TEST(GoParserTest, Conversions) {
261 EXPECT_PARSE(
262 "(ExprStmt (StarExpr (CallExpr (Ident Point ) (Ident p ) ) ) ) ",
263 "*Point(p)");
264 EXPECT_PARSE(
265 "(ExprStmt (CallExpr (StarExpr (Ident Point ) ) (Ident p ) ) ) ",
266 "(*Point)(p)");
267 EXPECT_PARSE("(ExprStmt (UnaryExpr (CallExpr (ChanType (Ident int ) 0 ) "
268 "(Ident c ) ) <- ) ) ",
269 "<-chan int(c)");
270 EXPECT_PARSE("(ExprStmt (TypeAssertExpr (Ident y ) (SelectorExpr (Ident io ) "
271 "(Ident Reader ) ) ) ) ",
272 "y.(io.Reader)");
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000273}