PostfixExpressionTest.cpp
6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//===-- PostfixExpressionTest.cpp -------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "lldb/Symbol/PostfixExpression.h"
#include "lldb/Expression/DWARFExpression.h"
#include "lldb/Utility/DataExtractor.h"
#include "lldb/Utility/StreamString.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/raw_ostream.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using namespace lldb_private;
using namespace lldb_private::postfix;
static std::string ToString(BinaryOpNode::OpType type) {
switch (type) {
case BinaryOpNode::Align:
return "@";
case BinaryOpNode::Minus:
return "-";
case BinaryOpNode::Plus:
return "+";
}
llvm_unreachable("Fully covered switch!");
}
static std::string ToString(UnaryOpNode::OpType type) {
switch (type) {
case UnaryOpNode::Deref:
return "^";
}
llvm_unreachable("Fully covered switch!");
}
struct ASTPrinter : public Visitor<std::string> {
protected:
std::string Visit(BinaryOpNode &binary, Node *&) override {
return llvm::formatv("{0}({1}, {2})", ToString(binary.GetOpType()),
Dispatch(binary.Left()), Dispatch(binary.Right()));
}
std::string Visit(InitialValueNode &, Node *&) override { return "InitialValue"; }
std::string Visit(IntegerNode &integer, Node *&) override {
return llvm::formatv("int({0})", integer.GetValue());
}
std::string Visit(RegisterNode ®, Node *&) override {
return llvm::formatv("reg({0})", reg.GetRegNum());
}
std::string Visit(SymbolNode &symbol, Node *&) override {
return symbol.GetName();
}
std::string Visit(UnaryOpNode &unary, Node *&) override {
return llvm::formatv("{0}({1})", ToString(unary.GetOpType()),
Dispatch(unary.Operand()));
}
public:
static std::string Print(Node *node) {
if (node)
return ASTPrinter().Dispatch(node);
return "nullptr";
}
};
static std::string ParseOneAndStringify(llvm::StringRef expr) {
llvm::BumpPtrAllocator alloc;
return ASTPrinter::Print(ParseOneExpression(expr, alloc));
}
TEST(PostfixExpression, ParseOneExpression) {
EXPECT_EQ("int(47)", ParseOneAndStringify("47"));
EXPECT_EQ("$foo", ParseOneAndStringify("$foo"));
EXPECT_EQ("+(int(1), int(2))", ParseOneAndStringify("1 2 +"));
EXPECT_EQ("-(int(1), int(2))", ParseOneAndStringify("1 2 -"));
EXPECT_EQ("@(int(1), int(2))", ParseOneAndStringify("1 2 @"));
EXPECT_EQ("+(int(1), +(int(2), int(3)))", ParseOneAndStringify("1 2 3 + +"));
EXPECT_EQ("+(+(int(1), int(2)), int(3))", ParseOneAndStringify("1 2 + 3 +"));
EXPECT_EQ("^(int(1))", ParseOneAndStringify("1 ^"));
EXPECT_EQ("^(^(int(1)))", ParseOneAndStringify("1 ^ ^"));
EXPECT_EQ("^(+(int(1), ^(int(2))))", ParseOneAndStringify("1 2 ^ + ^"));
EXPECT_EQ("-($foo, int(47))", ParseOneAndStringify("$foo 47 -"));
EXPECT_EQ("+(int(47), int(-42))", ParseOneAndStringify("47 -42 +"));
EXPECT_EQ("nullptr", ParseOneAndStringify("+"));
EXPECT_EQ("nullptr", ParseOneAndStringify("^"));
EXPECT_EQ("nullptr", ParseOneAndStringify("1 +"));
EXPECT_EQ("nullptr", ParseOneAndStringify("1 2 ^"));
EXPECT_EQ("nullptr", ParseOneAndStringify("1 2 3 +"));
EXPECT_EQ("nullptr", ParseOneAndStringify("^ 1"));
EXPECT_EQ("nullptr", ParseOneAndStringify("+ 1 2"));
EXPECT_EQ("nullptr", ParseOneAndStringify("1 + 2"));
EXPECT_EQ("nullptr", ParseOneAndStringify("1 2"));
EXPECT_EQ("nullptr", ParseOneAndStringify(""));
}
static std::vector<std::pair<std::string, std::string>>
ParseFPOAndStringify(llvm::StringRef prog) {
llvm::BumpPtrAllocator alloc;
std::vector<std::pair<llvm::StringRef, Node *>> parsed =
ParseFPOProgram(prog, alloc);
std::vector<std::pair<std::string, std::string>> result;
for (const auto &p : parsed)
result.emplace_back(p.first, ASTPrinter::Print(p.second));
return result;
}
TEST(PostfixExpression, ParseFPOProgram) {
EXPECT_THAT(ParseFPOAndStringify("a 1 ="),
testing::ElementsAre(std::make_pair("a", "int(1)")));
EXPECT_THAT(ParseFPOAndStringify("a 1 = b 2 3 + ="),
testing::ElementsAre(std::make_pair("a", "int(1)"),
std::make_pair("b", "+(int(2), int(3))")));
EXPECT_THAT(ParseFPOAndStringify(""), testing::IsEmpty());
EXPECT_THAT(ParseFPOAndStringify("="), testing::IsEmpty());
EXPECT_THAT(ParseFPOAndStringify("a 1"), testing::IsEmpty());
EXPECT_THAT(ParseFPOAndStringify("a 1 = ="), testing::IsEmpty());
EXPECT_THAT(ParseFPOAndStringify("a 1 + ="), testing::IsEmpty());
EXPECT_THAT(ParseFPOAndStringify("= a 1 ="), testing::IsEmpty());
}
static std::string ParseAndGenerateDWARF(llvm::StringRef expr) {
llvm::BumpPtrAllocator alloc;
Node *ast = ParseOneExpression(expr, alloc);
if (!ast)
return "Parse failed.";
if (!ResolveSymbols(ast, [&](SymbolNode &symbol) -> Node * {
if (symbol.GetName() == "INIT")
return MakeNode<InitialValueNode>(alloc);
uint32_t num;
if (to_integer(symbol.GetName().drop_front(), num))
return MakeNode<RegisterNode>(alloc, num);
return nullptr;
})) {
return "Resolution failed.";
}
const size_t addr_size = 4;
StreamString dwarf(Stream::eBinary, addr_size, lldb::eByteOrderLittle);
ToDWARF(*ast, dwarf);
// print dwarf expression to comparable textual representation
DataExtractor extractor(dwarf.GetData(), dwarf.GetSize(),
lldb::eByteOrderLittle, addr_size);
StreamString result;
if (!DWARFExpression::PrintDWARFExpression(result, extractor, addr_size,
/*dwarf_ref_size*/ 4,
/*location_expression*/ false)) {
return "DWARF printing failed.";
}
return result.GetString();
}
TEST(PostfixExpression, ToDWARF) {
EXPECT_EQ("DW_OP_consts +0", ParseAndGenerateDWARF("0"));
EXPECT_EQ("DW_OP_breg1 +0", ParseAndGenerateDWARF("R1"));
EXPECT_EQ("DW_OP_bregx 65 0", ParseAndGenerateDWARF("R65"));
EXPECT_EQ("DW_OP_pick 0x00", ParseAndGenerateDWARF("INIT"));
EXPECT_EQ("DW_OP_pick 0x00, DW_OP_pick 0x01, DW_OP_plus ",
ParseAndGenerateDWARF("INIT INIT +"));
EXPECT_EQ("DW_OP_breg1 +0, DW_OP_pick 0x01, DW_OP_plus ",
ParseAndGenerateDWARF("R1 INIT +"));
EXPECT_EQ("DW_OP_consts +1, DW_OP_pick 0x01, DW_OP_deref , DW_OP_plus ",
ParseAndGenerateDWARF("1 INIT ^ +"));
EXPECT_EQ("DW_OP_consts +4, DW_OP_consts +5, DW_OP_plus ",
ParseAndGenerateDWARF("4 5 +"));
EXPECT_EQ("DW_OP_consts +4, DW_OP_consts +5, DW_OP_minus ",
ParseAndGenerateDWARF("4 5 -"));
EXPECT_EQ("DW_OP_consts +4, DW_OP_deref ", ParseAndGenerateDWARF("4 ^"));
EXPECT_EQ("DW_OP_breg6 +0, DW_OP_consts +128, DW_OP_lit1 "
", DW_OP_minus , DW_OP_not , DW_OP_and ",
ParseAndGenerateDWARF("R6 128 @"));
}