-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathllvm-ast.ts
202 lines (180 loc) · 8.43 KB
/
llvm-ast.ts
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
// Copyright (c) 2021, Compiler Explorer Authors
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import type {CompilationResult} from '../types/compilation/compilation.interfaces.js';
import type {ResultLine} from '../types/resultline/resultline.interfaces.js';
import type {PropertyGetter} from './properties.interfaces.js';
type Point = {
line: number | null;
col: number | null;
};
export class LlvmAstParser {
maxAstLines: number;
// Almost every line of AST includes a span of related source lines:
// In different forms like <line:a:b, line:c:d>
static readonly locTypes = {
NONE: 'none', // No location specified
POINT: 'point', // A single location: beginning of a token
SPAN: 'span', // Two locations: first token to last token (beginning)
} as const;
constructor(compilerProps: PropertyGetter) {
this.maxAstLines = 500;
if (compilerProps) {
this.maxAstLines = compilerProps('maxLinesOfAst', this.maxAstLines);
}
}
// Accepts "line:a:b" and "col:b"
parsePoint(ptLine: string, lastLineNo: number | null): Point {
const lineRegex = /line:(\d+):/;
const colRegex = /(?:col|\d):(\d+)(?::|$)/;
const lineMatch = ptLine.match(lineRegex);
const colMatch = ptLine.match(colRegex);
const line = lineMatch ? Number(lineMatch[1]) : lastLineNo;
const col = colMatch ? Number(colMatch[1]) : null; // Does not happen for well-formed strings
return {line, col};
}
// Accepts "<X, X>" and "<X>", where
// X can be "col:a" or "line:a:b"
CBF6
// lastLineNo - the line number of the previous node,
// reused when only a column specified.
parseSpan(
line: string,
lastLineNo: number | null,
):
| {type: typeof LlvmAstParser.locTypes.SPAN; begin: Point; end: Point}
| {type: typeof LlvmAstParser.locTypes.POINT; loc: Point}
| {type: typeof LlvmAstParser.locTypes.NONE} {
const spanRegex = /<((?:line|col)[\d ,:ceilno]+)>/;
const m = line.match(spanRegex);
if (m) {
const span = m[1];
const beginEnd = span.split(',');
if (beginEnd.length === 2) {
const begin = this.parsePoint(beginEnd[0], lastLineNo);
const end = this.parsePoint(beginEnd[1], begin.line);
return {type: LlvmAstParser.locTypes.SPAN, begin, end};
}
return {type: LlvmAstParser.locTypes.POINT, loc: this.parsePoint(span, lastLineNo)};
}
return {type: LlvmAstParser.locTypes.NONE};
}
// Link the AST lines with spans of source locations (lines+columns)
parseAndSetSourceLines(astDump: ResultLine[]) {
let lfrom: any = {line: null, loc: null};
let lto: any = {line: null, loc: null};
for (const line of astDump) {
const span = this.parseSpan(line.text, lfrom.line);
switch (span.type) {
case LlvmAstParser.locTypes.NONE: {
break;
}
case LlvmAstParser.locTypes.POINT: {
lfrom = span.loc;
lto = span.loc;
break;
}
case LlvmAstParser.locTypes.SPAN: {
lfrom = span.begin;
lto = span.end;
break;
}
}
if (span.type !== LlvmAstParser.locTypes.NONE) {
// TODO: ResultLineSource doesn't have to/from
(line.source as any) = {from: lfrom, to: lto};
}
}
}
processAst(result: CompilationResult): ResultLine[] {
const output = result.stdout;
// Top level decls start with |- or `-
const topLevelRegex = /^([`|])-/;
// Refers to the user's source file rather than a system header
const sourceRegex = /<source>/g;
// <<invalid sloc>, /app/hell.hpp:5:1>
const userSource = /<<invalid sloc>, \/app\/.*:\d+:\d+>/;
// </usr/include/x86_64-linux-gnu/bits/types.h:31:1, col:23>
// <line:229:1, /usr/include/x86_64-linux-gnu/sys/cdefs.h:61:27> /usr/include/time.h:229:12
// </usr/include/x86_64-linux-gnu/sys/cdefs.h:293:44, /usr/include/time.h:258:27> 1
// </opt/compiler-explorer/gcc-11.2.0/lib/gcc/x86_64-linux-gnu/11.2.0/etc...
const systemSource = /(<\/usr\/|,\s\/usr\/|<\/opt\/)/;
// Refers to whatever the most recent file specified was
const lineRegex = /<(col|line):/;
// Intra-line filters
const addressRegex = /^([^A-Za-z]*[A-Za-z]+) 0x[\da-z]+/gm;
const slocRegex2 = / ?<?<invalid sloc>>?/g;
let mostRecentIsSource = false;
const isBlockUserSource = (output: ResultLine[], start: number, mostRecentIsSource: boolean) => {
for (let i = start + 1; i < output.length; ++i) {
if (topLevelRegex.test(output[i].text)) {
// Scanned through the block without encountering new info
return mostRecentIsSource;
}
if (systemSource.test(output[i].text)) {
return false;
}
if (userSource.test(output[i].text)) {
return true;
}
}
// Reached the end with no new info
return mostRecentIsSource;
};
// Remove all AST nodes which aren't directly from the user's source code
for (let i = 0; i < output.length; ++i) {
if (topLevelRegex.test(output[i].text)) {
if (lineRegex.test(output[i].text) && mostRecentIsSource) {
// do nothing
} else if (sourceRegex.test(output[i].text)) {
mostRecentIsSource = true;
} else {
// This is a system header or implicit definition,
// remove everything up to the next top level decl
// Top level decls with invalid sloc as the file don't change the most recent file
if (systemSource.test(output[i].text)) {
// skip ast from this source
} else if (userSource.test(output[i].text)) {
continue;
} else {
mostRecentIsSource = isBlockUserSource(output, i, mostRecentIsSource);
if (mostRecentIsSource) continue;
}
let spliceMax = i + 1;
while (output[spliceMax] && !topLevelRegex.test(output[spliceMax].text)) {
spliceMax++;
}
output.splice(i, spliceMax - i);
--i;
}
}
// Filter out the symbol addresses
output[i].text = output[i].text.replaceAll(addressRegex, '$1');
// Filter out <invalid sloc> and <<invalid sloc>>
output[i].text = output[i].text.replaceAll(slocRegex2, '');
// Unify file references
output[i].text = output[i].text.replaceAll(sourceRegex, 'line');
}
this.parseAndSetSourceLines(output);
return output;
}
}