blob: b6b1df4f54388978df053e0b041ced0d601e500a [file] [log] [blame]
Ryan Brown998c8a1c12015-11-02 19:30:40 +00001//===-- GoParserTest.cpp ------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#if defined(_MSC_VER) && (_HAS_EXCEPTIONS == 0)
11// Workaround for MSVC standard library bug, which fails to include <thread> when
12// exceptions are disabled.
13#include <eh.h>
14#endif
15
16#include <sstream>
17
18#include "gtest/gtest.h"
19
20#include "lldb/Core/Error.h"
21#include "lldb/Expression/GoParser.h"
22
23using namespace lldb_private;
24
25namespace
26{
27struct ASTPrinter
28{
29 ASTPrinter(GoASTNode *n) { (*this)(n); }
30
31 void
32 operator()(GoASTNode *n)
33 {
34 if (n == nullptr)
35 {
36 m_stream << "nil ";
37 return;
38 }
39 m_stream << "(" << n->GetKindName() << " ";
40 n->WalkChildren(*this);
41 if (auto *nn = llvm::dyn_cast<GoASTAssignStmt>(n))
42 m_stream << nn->GetDefine() << " ";
43 if (auto *nn = llvm::dyn_cast<GoASTBasicLit>(n))
44 m_stream << nn->GetValue().m_value.str() << " ";
45 if (auto *nn = llvm::dyn_cast<GoASTBinaryExpr>(n))
46 m_stream << GoLexer::LookupToken(nn->GetOp()).str() << " ";
47 if (auto *nn = llvm::dyn_cast<GoASTIdent>(n))
48 m_stream << nn->GetName().m_value.str() << " ";
49 if (auto *nn = llvm::dyn_cast<GoASTBranchStmt>(n))
50 m_stream << GoLexer::LookupToken(nn->GetTok()).str() << " ";
51 if (auto *nn = llvm::dyn_cast<GoASTCallExpr>(n))
52 m_stream << (nn->GetEllipsis() ? "..." : "") << " ";
53 if (auto *nn = llvm::dyn_cast<GoASTChanType>(n))
54 m_stream << nn->GetDir() << " ";
55 if (auto *nn = llvm::dyn_cast<GoASTGenDecl>(n))
56 m_stream << GoLexer::LookupToken(nn->GetTok()).str() << " ";
57 if (auto *nn = llvm::dyn_cast<GoASTIncDecStmt>(n))
58 m_stream << GoLexer::LookupToken(nn->GetTok()).str() << " ";
59 if (auto *nn = llvm::dyn_cast<GoASTRangeStmt>(n))
60 m_stream << nn->GetDefine() << " ";
61 if (auto *nn = llvm::dyn_cast<GoASTSliceExpr>(n))
62 m_stream << nn->GetSlice3() << " ";
63 if (auto *nn = llvm::dyn_cast<GoASTUnaryExpr>(n))
64 m_stream << GoLexer::LookupToken(nn->GetOp()).str() << " ";
65 m_stream << ") ";
66 }
67
68 const std::string
69 str() const
70 {
71 return m_stream.str();
72 }
73 std::stringstream m_stream;
74};
75
76testing::AssertionResult
77CheckStatement(const char *_s, const char *c_expr, const char *sexpr, const char *code)
78{
79 GoParser parser(code);
80 std::unique_ptr<GoASTStmt> stmt(parser.Statement());
81 if (parser.Failed() || !stmt)
82 {
83 Error err;
84 parser.GetError(err);
85 return testing::AssertionFailure() << "Error parsing " << c_expr << "\n\t" << err.AsCString();
86 }
87 std::string actual_sexpr = ASTPrinter(stmt.get()).str();
88 if (actual_sexpr == sexpr)
89 return testing::AssertionSuccess();
90 return testing::AssertionFailure() << "Parsing: " << c_expr << "\nExpected: " << sexpr
91 << "\nGot: " << actual_sexpr;
92}
93} // namespace
94
95#define EXPECT_PARSE(s, c) EXPECT_PRED_FORMAT2(CheckStatement, s, c)
96
97TEST(GoParserTest, ParseBasicLiterals)
98{
99 EXPECT_PARSE("(ExprStmt (BasicLit 0 ) ) ", "0");
100 EXPECT_PARSE("(ExprStmt (BasicLit 42 ) ) ", "42");
101 EXPECT_PARSE("(ExprStmt (BasicLit 0600 ) ) ", "0600");
102 EXPECT_PARSE("(ExprStmt (BasicLit 0xBadFace ) ) ", "0xBadFace");
103 EXPECT_PARSE("(ExprStmt (BasicLit 170141183460469231731687303715884105727 ) ) ",
104 "170141183460469231731687303715884105727");
105
106 EXPECT_PARSE("(ExprStmt (BasicLit 0. ) ) ", "0.");
107 EXPECT_PARSE("(ExprStmt (BasicLit 72.40 ) ) ", "72.40");
108 EXPECT_PARSE("(ExprStmt (BasicLit 072.40 ) ) ", "072.40");
109 EXPECT_PARSE("(ExprStmt (BasicLit 2.71828 ) ) ", "2.71828");
110 EXPECT_PARSE("(ExprStmt (BasicLit 1.e+0 ) ) ", "1.e+0");
111 EXPECT_PARSE("(ExprStmt (BasicLit 6.67428e-11 ) ) ", "6.67428e-11");
112 EXPECT_PARSE("(ExprStmt (BasicLit 1E6 ) ) ", "1E6");
113 EXPECT_PARSE("(ExprStmt (BasicLit .12345E+6 ) ) ", ".12345E+6");
114
115 EXPECT_PARSE("(ExprStmt (BasicLit 0i ) ) ", "0i");
116 EXPECT_PARSE("(ExprStmt (BasicLit 011i ) ) ", "011i");
117 EXPECT_PARSE("(ExprStmt (BasicLit 0.i ) ) ", "0.i");
118 EXPECT_PARSE("(ExprStmt (BasicLit 2.71828i ) ) ", "2.71828i");
119 EXPECT_PARSE("(ExprStmt (BasicLit 6.67428e-11i ) ) ", "6.67428e-11i");
120 EXPECT_PARSE("(ExprStmt (BasicLit 1E6i ) ) ", "1E6i");
121 EXPECT_PARSE("(ExprStmt (BasicLit .12345E+6i ) ) ", ".12345E+6i");
122
123 EXPECT_PARSE("(ExprStmt (BasicLit 'a' ) ) ", "'a'");
124 EXPECT_PARSE("(ExprStmt (BasicLit '本' ) ) ", "'本'");
125 EXPECT_PARSE("(ExprStmt (BasicLit \"abc\" ) ) ", "\"abc\"");
126 EXPECT_PARSE("(ExprStmt (BasicLit `abc` ) ) ", "`abc`");
127 EXPECT_PARSE("(ExprStmt (BasicLit `ab\nc` ) ) ", "`ab\nc`");
128}
129
130TEST(GoParserTest, ParseOperand)
131{
132 EXPECT_PARSE("(ExprStmt (Ident a ) ) ", "a");
133 EXPECT_PARSE("(ExprStmt (Ident _x9 ) ) ", "_x9");
134 EXPECT_PARSE("(ExprStmt (Ident ThisVariableIsExported ) ) ", "ThisVariableIsExported");
135 EXPECT_PARSE("(ExprStmt (Ident αβ ) ) ", "αβ");
136
137 EXPECT_PARSE("(ExprStmt (SelectorExpr (Ident math ) (Ident Sin ) ) ) ", "math.Sin");
138}
139
140TEST(GoParserTest, ParseCompositeLiterals)
141{
142 EXPECT_PARSE("(ExprStmt (CompositeLit (Ident Point3D ) ) ) ", "Point3D{}");
143 EXPECT_PARSE("(ExprStmt (CompositeLit (Ident Line ) (Ident origin ) (CompositeLit (Ident Point3D ) (KeyValueExpr "
144 "(Ident y ) (UnaryExpr (BasicLit 4 ) - ) ) (KeyValueExpr (Ident z ) (BasicLit 12.3 ) ) ) ) ) ",
145 "Line{origin, Point3D{y: -4, z: 12.3}}");
146 EXPECT_PARSE("(ExprStmt (CompositeLit (ArrayType (BasicLit 10 ) (Ident string ) ) ) ) ", "[10]string{}");
147 EXPECT_PARSE("(ExprStmt (CompositeLit (ArrayType (BasicLit 6 ) (Ident int ) ) (BasicLit 1 ) (BasicLit 2 ) "
148 "(BasicLit 3 ) (BasicLit 5 ) ) ) ",
149 "[6]int {1, 2, 3, 5}");
150 EXPECT_PARSE("(ExprStmt (CompositeLit (ArrayType nil (Ident int ) ) (BasicLit 2 ) (BasicLit 3 ) (BasicLit 5 ) "
151 "(BasicLit 7 ) (BasicLit 9 ) (BasicLit 2147483647 ) ) ) ",
152 "[]int{2, 3, 5, 7, 9, 2147483647}");
153 EXPECT_PARSE("(ExprStmt (CompositeLit (ArrayType (BasicLit 128 ) (Ident bool ) ) (KeyValueExpr (BasicLit 'a' ) "
154 "(Ident true ) ) (KeyValueExpr (BasicLit 'e' ) (Ident true ) ) (KeyValueExpr (BasicLit 'i' ) (Ident "
155 "true ) ) (KeyValueExpr (BasicLit 'o' ) (Ident true ) ) (KeyValueExpr (BasicLit 'u' ) (Ident true ) ) "
156 "(KeyValueExpr (BasicLit 'y' ) (Ident true ) ) ) ) ",
157 "[128]bool{'a': true, 'e': true, 'i': true, 'o': true, 'u': true, 'y': true}");
158 EXPECT_PARSE("(ExprStmt (CompositeLit (ArrayType (BasicLit 10 ) (Ident float32 ) ) (UnaryExpr (BasicLit 1 ) - ) "
159 "(KeyValueExpr (BasicLit 4 ) (UnaryExpr (BasicLit 0.1 ) - ) ) (UnaryExpr (BasicLit 0.1 ) - ) "
160 "(KeyValueExpr (BasicLit 9 ) (UnaryExpr (BasicLit 1 ) - ) ) ) ) ",
161 "[10]float32{-1, 4: -0.1, -0.1, 9: -1}");
162}
163
164TEST(GoParserTest, ParseEllipsisArray)
165{
166 EXPECT_PARSE(
167 "(ExprStmt (CompositeLit (ArrayType (Ellipsis nil ) (Ident string ) ) (BasicLit `Sat` ) (BasicLit `Sun` ) ) ) ",
168 "[...]string {`Sat`, `Sun`}");
169 EXPECT_PARSE("(ExprStmt (CompositeLit (ArrayType (Ellipsis nil ) (Ident Point ) ) (CompositeLit nil (BasicLit 1.5 "
170 ") (UnaryExpr (BasicLit 3.5 ) - ) ) (CompositeLit nil (BasicLit 0 ) (BasicLit 0 ) ) ) ) ",
171 "[...]Point{{1.5, -3.5}, {0, 0}}");
172}
173
174TEST(GoParserTest, ParseMap)
175{
176 EXPECT_PARSE("(ExprStmt (CompositeLit (MapType (Ident string ) (Ident float32 ) ) (KeyValueExpr (BasicLit `C0` ) "
177 "(BasicLit 16.35 ) ) (KeyValueExpr (BasicLit `D0` ) (BasicLit 18.35 ) ) ) ) ",
178 "map[string]float32{`C0`: 16.35, `D0`: 18.35, }");
179}
180
181TEST(GoParserTest, UnaryExpr)
182{
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");
190}
191
192TEST(GoParserTest, BinaryExpr)
193{
194 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (Ident b ) || ) ) ", "a || b");
195 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (Ident b ) && ) ) ", "a && b");
196
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");
202 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (Ident b ) >= ) ) ", "a >= b");
203
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
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");
215 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (Ident b ) &^ ) ) ", "a &^ b");
216
217 EXPECT_PARSE(
218 "(ExprStmt (BinaryExpr (BasicLit 23 ) (BinaryExpr (BasicLit 3 ) (IndexExpr (Ident x ) (Ident i ) ) * ) + ) ) ",
219 "23 + 3*x[i]");
220 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident a ) (UnaryExpr (UnaryExpr (Ident a ) + ) + ) + ) ) ", "a + + + a");
221 EXPECT_PARSE("(ExprStmt (BinaryExpr (UnaryExpr (Ident a ) ^ ) (Ident b ) >> ) ) ", "^a >> b");
222 EXPECT_PARSE("(ExprStmt (BinaryExpr (CallExpr (Ident f ) ) (CallExpr (Ident g ) ) || ) ) ", "f() || g()");
223 EXPECT_PARSE("(ExprStmt (BinaryExpr (BinaryExpr (Ident x ) (BinaryExpr (Ident y ) (BasicLit 1 ) + ) == ) "
224 "(BinaryExpr (UnaryExpr (Ident chanPtr ) <- ) (BasicLit 0 ) > ) && ) ) ",
225 "x == y+1 && <-chanPtr > 0");
226}
227
228TEST(GoParserTest, PrimaryExpr)
229{
230 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident x ) (CallExpr (Ident f ) ) <= ) ) ", "x <= f()");
231 EXPECT_PARSE("(ExprStmt (BinaryExpr (Ident s ) (BasicLit `.txt` ) + ) ) ", "(s + `.txt`)");
232 EXPECT_PARSE("(ExprStmt (CallExpr (Ident f ) (BasicLit 3.1415 ) (Ident true ) ) ) ", "f(3.1415, true)");
233 EXPECT_PARSE("(ExprStmt (CallExpr (Ident f ) (BasicLit 3.1415 ) (Ident a ) ... ) ) ", "f(3.1415, a...)");
234 EXPECT_PARSE("(ExprStmt (IndexExpr (Ident m ) (BasicLit '1' ) ) ) ", "m['1']");
235 EXPECT_PARSE("(ExprStmt (SliceExpr (Ident s ) (Ident i ) (BinaryExpr (Ident j ) (BasicLit 1 ) + ) nil 0 ) ) ",
236 "s[i : j + 1]");
237 EXPECT_PARSE("(ExprStmt (SelectorExpr (Ident obj ) (Ident color ) ) ) ", "obj.color");
238 EXPECT_PARSE("(ExprStmt (CallExpr (SelectorExpr (IndexExpr (SelectorExpr (Ident f ) (Ident p ) ) (Ident i ) ) "
239 "(Ident x ) ) ) ) ",
240 "f.p[i].x()");
241}
242
243TEST(GoParserTest, Conversions)
244{
245 EXPECT_PARSE("(ExprStmt (StarExpr (CallExpr (Ident Point ) (Ident p ) ) ) ) ", "*Point(p)");
246 EXPECT_PARSE("(ExprStmt (CallExpr (StarExpr (Ident Point ) ) (Ident p ) ) ) ", "(*Point)(p)");
247 EXPECT_PARSE("(ExprStmt (UnaryExpr (CallExpr (ChanType (Ident int ) 0 ) (Ident c ) ) <- ) ) ", "<-chan int(c)");
248 EXPECT_PARSE("(ExprStmt (TypeAssertExpr (Ident y ) (SelectorExpr (Ident io ) (Ident Reader ) ) ) ) ",
249 "y.(io.Reader)");
250}