-
Notifications
You must be signed in to change notification settings - Fork 159
/
ast.go
268 lines (254 loc) · 6.96 KB
/
ast.go
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Package ast parses the clang AST output into AST structures.
package ast
import (
"bytes"
"encoding/json"
"regexp"
"strconv"
"strings"
)
// Node represents any node in the AST.
type Node interface {
Address() Address
Children() []Node
AddChild(node Node)
Position() Position
}
// Address contains the memory address (originally outputted as a hexadecimal
// string) from the clang AST. The address are not predictable between run and
// are only useful for identifying nodes in a single AST.
//
// The Address is used like a primary key when storing the tree as a flat
// structure.
type Address uint64
// ParseAddress returns the integer representation of the hexadecimal address
// (like 0x7f8a1d8ccfd0). If the address cannot be parsed, 0 is returned.
func ParseAddress(address string) Address {
addr, _ := strconv.ParseUint(address, 0, 64)
return Address(addr)
}
// Parse takes the coloured output of the clang AST command and returns a root
// node for the AST.
func Parse(line string) Node {
// This is a special case. I'm not sure if it's a bug in the clang AST
// dumper. It should have children.
if line == "array filler" {
return parseArrayFiller(line)
}
nodeName := strings.SplitN(line, " ", 2)[0]
switch nodeName {
case "AlignedAttr":
return parseAlignedAttr(line)
case "AllocSizeAttr":
return parseAllocSizeAttr(line)
case "AlwaysInlineAttr":
return parseAlwaysInlineAttr(line)
case "ArraySubscriptExpr":
return parseArraySubscriptExpr(line)
case "AsmLabelAttr":
return parseAsmLabelAttr(line)
case "AvailabilityAttr":
return parseAvailabilityAttr(line)
case "BinaryOperator":
return parseBinaryOperator(line)
case "BreakStmt":
return parseBreakStmt(line)
case "BuiltinType":
return parseBuiltinType(line)
case "CallExpr":
return parseCallExpr(line)
case "CaseStmt":
return parseCaseStmt(line)
case "CharacterLiteral":
return parseCharacterLiteral(line)
case "CompoundStmt":
return parseCompoundStmt(line)
case "ConditionalOperator":
return parseConditionalOperator(line)
case "ConstAttr":
return parseConstAttr(line)
case "ConstantArrayType":
return parseConstantArrayType(line)
case "ContinueStmt":
return parseContinueStmt(line)
case "CompoundAssignOperator":
return parseCompoundAssignOperator(line)
case "CStyleCastExpr":
return parseCStyleCastExpr(line)
case "DeclRefExpr":
return parseDeclRefExpr(line)
case "DeclStmt":
return parseDeclStmt(line)
case "DefaultStmt":
return parseDefaultStmt(line)
case "DisableTailCallsAttr":
return parseDisableTailCallsAttr(line)
case "DeprecatedAttr":
return parseDeprecatedAttr(line)
case "DoStmt":
return parseDoStmt(line)
case "ElaboratedType":
return parseElaboratedType(line)
case "Enum":
return parseEnum(line)
case "EnumConstantDecl":
return parseEnumConstantDecl(line)
case "EnumDecl":
return parseEnumDecl(line)
case "EnumType":
return parseEnumType(line)
case "Field":
return parseField(line)
case "FieldDecl":
return parseFieldDecl(line)
case "FloatingLiteral":
return parseFloatingLiteral(line)
case "FormatAttr":
return parseFormatAttr(line)
case "FunctionDecl":
return parseFunctionDecl(line)
case "FunctionProtoType":
return parseFunctionProtoType(line)
case "ForStmt":
return parseForStmt(line)
case "GCCAsmStmt":
return parseGCCAsmStmt(line)
case "GotoStmt":
return parseGotoStmt(line)
case "IfStmt":
return parseIfStmt(line)
case "ImplicitCastExpr":
return parseImplicitCastExpr(line)
case "ImplicitValueInitExpr":
return parseImplicitValueInitExpr(line)
case "IncompleteArrayType":
return parseIncompleteArrayType(line)
case "IndirectFieldDecl":
return parseIndirectFieldDecl(line)
case "InitListExpr":
return parseInitListExpr(line)
case "IntegerLiteral":
return parseIntegerLiteral(line)
case "LabelStmt":
return parseLabelStmt(line)
case "MallocAttr":
return parseMallocAttr(line)
case "MaxFieldAlignmentAttr":
return parseMaxFieldAlignmentAttr(line)
case "MemberExpr":
return parseMemberExpr(line)
case "ModeAttr":
return parseModeAttr(line)
case "NoInlineAttr":
return parseNoInlineAttr(line)
case "NoThrowAttr":
return parseNoThrowAttr(line)
case "NonNullAttr":
return parseNonNullAttr(line)
case "OffsetOfExpr":
return parseOffsetOfExpr(line)
case "PackedAttr":
return parsePackedAttr(line)
case "ParenExpr":
return parseParenExpr(line)
case "ParenType":
return parseParenType(line)
case "ParmVarDecl":
return parseParmVarDecl(line)
case "PointerType":
return parsePointerType(line)
case "PredefinedExpr":
return parsePredefinedExpr(line)
case "PureAttr":
return parsePureAttr(line)
case "QualType":
return parseQualType(line)
case "Record":
return parseRecord(line)
case "RecordDecl":
return parseRecordDecl(line)
case "RecordType":
return parseRecordType(line)
case "RestrictAttr":
return parseRestrictAttr(line)
case "ReturnStmt":
return parseReturnStmt(line)
case "ReturnsTwiceAttr":
return parseReturnsTwiceAttr(line)
case "StmtExpr":
return parseStmtExpr(line)
case "StringLiteral":
return parseStringLiteral(line)
case "SwitchStmt":
return parseSwitchStmt(line)
case "TranslationUnitDecl":
return parseTranslationUnitDecl(line)
case "TransparentUnionAttr":
return parseTransparentUnionAttr(line)
case "Typedef":
return parseTypedef(line)
case "TypedefDecl":
return parseTypedefDecl(line)
case "TypedefType":
return parseTypedefType(line)
case "UnaryExprOrTypeTraitExpr":
return parseUnaryExprOrTypeTraitExpr(line)
case "UnaryOperator":
return parseUnaryOperator(line)
case "UnusedAttr":
return parseUnusedAttr(line)
case "VAArgExpr":
return parseVAArgExpr(line)
case "VarDecl":
return parseVarDecl(line)
case "WarnUnusedResultAttr":
return parseWarnUnusedResultAttr(line)
case "WeakAttr":
return parseWeakAttr(line)
case "WhileStmt":
return parseWhileStmt(line)
case "NullStmt":
return nil
default:
panic("unknown node type: '" + line + "'")
}
}
// Global cache of regexp
var cachedRegex = map[string]*regexp.Regexp{}
func groupsFromRegex(rx, line string) map[string]string {
// We remove tabs and newlines from the regex. This is purely cosmetic,
// as the regex input can be quite long and it's nice for the caller to
// be able to format it in a more readable way.
if _, ok := cachedRegex[rx]; !ok {
fullRegexp := "(?P<address>[0-9a-fx]+) " +
strings.Replace(strings.Replace(rx, "\n", "", -1), "\t", "", -1)
cachedRegex[rx] = regexp.MustCompile(fullRegexp)
}
re := cachedRegex[rx]
match := re.FindStringSubmatch(line)
if len(match) == 0 {
panic("could not match regexp '" + rx +
"' with string '" + line + "'")
}
result := make(map[string]string)
for i, name := range re.SubexpNames() {
if i != 0 {
result[name] = match[i]
}
}
return result
}
// Atos - ASTree to string
// Typically using for debug
func Atos(node Node) string {
j, err := json.Marshal(node)
if err != nil {
panic(err)
}
var out bytes.Buffer
err = json.Indent(&out, j, "", "\t")
if err != nil {
panic(err)
}
return out.String()
}