aboutsummaryrefslogtreecommitdiff
path: root/src/server/check.odin
blob: a3804dfa71b3e2863bf3db9ba3c793d9c4e501d1 (plain)
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package server

import "core:encoding/json"
import "core:fmt"
import "core:intrinsics"
import "core:log"
import "core:mem"
import "core:os"
import "core:path/filepath"
import path "core:path/slashpath"
import "core:runtime"
import "core:slice"
import "core:strconv"
import "core:strings"
import "core:sync"
import "core:text/scanner"
import "core:thread"

import "src:common"

//Store uris we have reported on since last save. We use this to clear them on next save.
uris_reported := make([dynamic]string)


//If the user does not specify where to call odin check, it'll just find all directory with odin, and call them seperately.
fallback_find_odin_directories :: proc(config: ^common.Config) -> []string {
	walk_proc :: proc(
		info: os.File_Info,
		in_err: os.Errno,
		user_data: rawptr,
	) -> (
		err: os.Errno,
		skip_dir: bool,
	) {
		data := cast(^[dynamic]string)user_data

		if !info.is_dir && filepath.ext(info.name) == ".odin" {
			dir := filepath.dir(info.fullpath, context.temp_allocator)
			if !slice.contains(data[:], dir) {
				append(data, dir)
			}
		}

		return in_err, false
	}

	data := make([dynamic]string, context.temp_allocator)

	if len(config.workspace_folders) > 0 {
		if uri, ok := common.parse_uri(
			config.workspace_folders[0].uri,
			context.temp_allocator,
		); ok {
			filepath.walk(uri.path, walk_proc, &data)
		}
	}

	return data[:]
}

check :: proc(paths: []string, writer: ^Writer, config: ^common.Config) {
	paths := paths

	if len(paths) == 0 {
		paths = fallback_find_odin_directories(config)
	}

	data := make([]byte, mem.Kilobyte * 200, context.temp_allocator)

	buffer: []byte
	code: u32
	ok: bool

	collection_builder := strings.builder_make(context.temp_allocator)

	for k, v in common.config.collections {
		if k == "" || k == "core" || k == "vendor" || k == "base" {
			continue
		}
		strings.write_string(
			&collection_builder,
			fmt.aprintf("-collection:%v=%v ", k, v),
		)
	}

	errors := make(map[string][dynamic]Diagnostic, 0, context.temp_allocator)

	for path in paths {
		command: string

		if config.odin_command != "" {
			command = config.odin_command
		} else {
			command = "odin"
		}

		entry_point_opt :=
			filepath.ext(path) == ".odin" ? "-file" : "-no-entry-point"

		slice.zero(data)

		if code, ok, buffer = common.run_executable(
			fmt.tprintf(
				"%v check %s %s %s %s %s",
				command,
				path,
				strings.to_string(collection_builder),
				entry_point_opt,
				config.checker_args,
				ODIN_OS == .Linux || ODIN_OS == .Darwin ? "2>&1" : "",
			),
			&data,
		); !ok {
			log.errorf(
				"Odin check failed with code %v for file %v",
				code,
				path,
			)
			return
		}

		s: scanner.Scanner

		scanner.init(&s, string(buffer))

		s.whitespace = {'\t', ' '}

		current: rune

		ErrorSeperator :: struct {
			message: string,
			line:    int,
			column:  int,
			uri:     string,
		}

		error_seperators := make(
			[dynamic]ErrorSeperator,
			context.temp_allocator,
		)

		//find all the signatures string(digit:digit)
		loop: for scanner.peek(&s) != scanner.EOF {

			scan_line: {
				error: ErrorSeperator

				source_pos := s.src_pos

				if source_pos == 1 {
					source_pos = 0
				}

				for scanner.peek(&s) != '(' {
					n := scanner.scan(&s)

					if n == scanner.EOF {
						break loop
					}
					if n == '\n' {
						source_pos = s.src_pos - 1
					}
				}

				error.uri = strings.clone(
					string(buffer[source_pos:s.src_pos - 1]),
					context.temp_allocator,
				)

				left_paren := scanner.scan(&s)

				if left_paren != '(' {
					break scan_line
				}

				lhs_digit := scanner.scan(&s)

				if lhs_digit != scanner.Int {
					break scan_line
				}

				line, column: int
				ok: bool

				line, ok = strconv.parse_int(scanner.token_text(&s))

				if !ok {
					break scan_line
				}

				seperator := scanner.scan(&s)

				if seperator != ':' {
					break scan_line
				}

				rhs_digit := scanner.scan(&s)

				if rhs_digit != scanner.Int {
					break scan_line
				}

				column, ok = strconv.parse_int(scanner.token_text(&s))

				if !ok {
					break scan_line
				}

				right_paren := scanner.scan(&s)

				if right_paren != ')' {
					break scan_line
				}

				source_pos = s.src_pos

				for scanner.peek(&s) != '\n' {
					n := scanner.scan(&s)

					if n == scanner.EOF {
						break
					}
				}

				if source_pos == s.src_pos {
					continue
				}

				error.message = strings.clone(
					string(buffer[source_pos:s.src_pos - 1]),
					context.temp_allocator,
				)
				error.column = column
				error.line = line

				append(&error_seperators, error)
				continue loop
			}

			// line scan failed, skip to the next line
			for scanner.peek(&s) != '\n' {
				n := scanner.scan(&s)
				if n == scanner.EOF {
					break
				}
			}
		}

		for error in error_seperators {
			if strings.contains(
				error.message,
				"Redeclaration of 'main' in this scope",
			) {
				continue
			}

			if error.uri not_in errors {
				errors[error.uri] = make(
					[dynamic]Diagnostic,
					context.temp_allocator,
				)
			}

			append(
				&errors[error.uri],
				Diagnostic {
					code = "checker",
					severity = .Error,
					range =  {
						start =  {
							character = error.column,
							line = error.line - 1,
						},
						end = {character = 0, line = error.line},
					},
					message = error.message,
				},
			)
		}
	}

	for uri in uris_reported {
		params := NotificationPublishDiagnosticsParams {
			uri         = uri,
			diagnostics = {},
		}

		notifaction := Notification {
			jsonrpc = "2.0",
			method  = "textDocument/publishDiagnostics",
			params  = params,
		}

		if writer != nil {
			send_notification(notifaction, writer)
		}

		delete(uri)
	}

	clear(&uris_reported)

	for k, v in errors {
		uri := common.create_uri(k, context.temp_allocator)

		params := NotificationPublishDiagnosticsParams {
			uri         = uri.uri,
			diagnostics = v[:],
		}

		notifaction := Notification {
			jsonrpc = "2.0",
			method  = "textDocument/publishDiagnostics",
			params  = params,
		}

		append(&uris_reported, strings.clone(uri.uri))

		if writer != nil {
			send_notification(notifaction, writer)
		}
	}
}