forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck.rs
More file actions
223 lines (199 loc) · 6.69 KB
/
Copy pathcheck.rs
File metadata and controls
223 lines (199 loc) · 6.69 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
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
use camino::Utf8PathBuf;
use codegen::emit_module_yul;
use common::InputDb;
use driver::DriverDataBase;
use hir::hir_def::{HirIngot, TopLevelMod};
use mir::lower_module;
use url::Url;
pub fn check(path: &Utf8PathBuf, dump_mir: bool, emit_yul_min: bool) {
let mut db = DriverDataBase::default();
// Determine if we're dealing with a single file or an ingot directory
let has_errors = if path.is_file() && path.extension() == Some("fe") {
check_single_file(&mut db, path, dump_mir, emit_yul_min)
} else if path.is_dir() {
check_ingot(&mut db, path, dump_mir, emit_yul_min)
} else {
eprintln!("❌ Error: Path must be either a .fe file or a directory containing fe.toml");
std::process::exit(1);
};
if has_errors {
std::process::exit(1);
}
}
fn check_single_file(
db: &mut DriverDataBase,
file_path: &Utf8PathBuf,
dump_mir: bool,
emit_yul_min: bool,
) -> bool {
// Create a file URL for the single .fe file
let file_url = match Url::from_file_path(file_path.canonicalize_utf8().unwrap()) {
Ok(url) => url,
Err(_) => {
eprintln!("❌ Error: Invalid file path: {file_path}");
return true;
}
};
// Read the file content
let content = match std::fs::read_to_string(file_path) {
Ok(content) => content,
Err(err) => {
eprintln!("Error reading file {file_path}: {err}");
return true;
}
};
// Add the file to the workspace
db.workspace().touch(db, file_url.clone(), Some(content));
// Try to get the file and check it for errors
if let Some(file) = db.workspace().get(db, &file_url) {
let top_mod = db.top_mod(file);
let diags = db.run_on_top_mod(top_mod);
if !diags.is_empty() {
eprintln!("errors in {file_url}");
eprintln!();
diags.emit(db);
return true;
}
if dump_mir {
dump_module_mir(db, top_mod);
}
if emit_yul_min {
emit_yul(db, top_mod);
}
} else {
eprintln!("❌ Error: Could not process file {file_path}");
return true;
}
false
}
fn check_ingot(
db: &mut DriverDataBase,
dir_path: &Utf8PathBuf,
dump_mir: bool,
emit_yul_min: bool,
) -> bool {
let canonical_path = match dir_path.canonicalize_utf8() {
Ok(path) => path,
Err(_) => {
eprintln!("Error: Invalid or non-existent directory path: {dir_path}");
eprintln!(" Make sure the directory exists and is accessible");
return true;
}
};
let ingot_url = match Url::from_directory_path(canonical_path.as_str()) {
Ok(url) => url,
Err(_) => {
eprintln!("❌ Error: Invalid directory path: {dir_path}");
return true;
}
};
let had_init_diagnostics = driver::init_ingot(db, &ingot_url);
if had_init_diagnostics {
return true;
}
let Some(ingot) = db.workspace().containing_ingot(db, ingot_url.clone()) else {
// Check if the issue is a missing fe.toml file
let config_url = match ingot_url.join("fe.toml") {
Ok(url) => url,
Err(_) => {
eprintln!("❌ Error: Invalid ingot directory path");
return true;
}
};
if db.workspace().get(db, &config_url).is_none() {
eprintln!("❌ Error: No fe.toml file found in the root directory");
eprintln!(" Expected fe.toml at: {config_url}");
eprintln!(
" Make sure you're in an fe project directory or create a fe.toml file"
);
} else {
eprintln!("❌ Error: Could not resolve ingot from directory");
}
return true;
};
// Check if the ingot has source files before trying to analyze
if ingot.root_file(db).is_err() {
eprintln!(
"source files resolution error: `src` folder does not exist in the ingot directory"
);
return true;
}
let diags = db.run_on_ingot(ingot);
let mut has_errors = false;
if !diags.is_empty() {
diags.emit(db);
has_errors = true;
} else {
let root_mod = ingot.root_mod(db);
if dump_mir {
dump_module_mir(db, root_mod);
}
if emit_yul_min {
emit_yul(db, root_mod);
}
}
// Collect all dependencies with errors
let mut dependency_errors = Vec::new();
for dependency_url in db.dependency_graph().dependency_urls(db, &ingot_url) {
let Some(ingot) = db.workspace().containing_ingot(db, dependency_url.clone()) else {
// Skip dependencies that can't be resolved
continue;
};
let diags = db.run_on_ingot(ingot);
if !diags.is_empty() {
dependency_errors.push((dependency_url, diags));
}
}
// Print dependency errors if any exist
if !dependency_errors.is_empty() {
has_errors = true;
if dependency_errors.len() == 1 {
eprintln!("❌ Error in downstream ingot");
} else {
eprintln!("❌ Errors in downstream ingots");
}
for (dependency_url, diags) in dependency_errors {
print_dependency_info(db, &dependency_url);
diags.emit(db);
}
}
has_errors
}
fn print_dependency_info(db: &DriverDataBase, dependency_url: &Url) {
eprintln!();
// Get the ingot for this dependency URL to access its config
if let Some(ingot) = db.workspace().containing_ingot(db, dependency_url.clone()) {
if let Some(config) = ingot.config(db) {
let name = config.metadata.name.as_deref().unwrap_or("unknown");
if let Some(version) = &config.metadata.version {
eprintln!("➖ {name} (version: {version})");
} else {
eprintln!("➖ {name}");
}
} else {
eprintln!("➖ Unknown dependency");
}
} else {
eprintln!("➖ Unknown dependency");
}
eprintln!("🔗 {dependency_url}");
eprintln!();
}
fn emit_yul(db: &DriverDataBase, top_mod: TopLevelMod<'_>) {
match emit_module_yul(db, top_mod) {
Ok(yul) => {
println!("=== Yul ===");
println!("{yul}");
}
Err(err) => eprintln!("⚠️ failed to emit Yul: {err}"),
}
}
fn dump_module_mir(db: &DriverDataBase, top_mod: TopLevelMod<'_>) {
match lower_module(db, top_mod) {
Ok(mir_module) => {
println!("=== MIR for module ===");
print!("{}", mir::fmt::format_module(db, &mir_module));
}
Err(err) => eprintln!("failed to lower MIR: {err}"),
}
}