Skip to content

Commit 486f90a

Browse files
committed
Add TODO checker
1 parent 66d6f7b commit 486f90a

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ jobs:
8888
- name: Check formatting
8989
run: ./y.sh fmt --check
9090

91+
- name: Check todo
92+
run: ./y.sh check-todo
93+
9194
- name: clippy
9295
run: |
9396
cargo clippy --all-targets -- -D warnings

build_system/src/main.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ mod prepare;
1212
mod rust_tools;
1313
mod rustc_info;
1414
mod test;
15+
mod todo;
1516
mod utils;
1617
const BUILD_DIR: &str = "build";
1718

@@ -45,7 +46,8 @@ Commands:
4546
clone-gcc : Clones the GCC compiler from a specified source.
4647
fmt : Runs rustfmt
4748
fuzz : Fuzzes `cg_gcc` using rustlantis
48-
abi-test : Runs the abi-cafe test suite on the codegen, checking for ABI compatibility with LLVM"
49+
abi-test : Runs the abi-cafe test suite on the codegen, checking for ABI compatibility with LLVM
50+
check-todo : Checks todo in the project"
4951
);
5052
}
5153

@@ -61,6 +63,7 @@ pub enum Command {
6163
Fmt,
6264
Fuzz,
6365
AbiTest,
66+
CheckTodo,
6467
}
6568

6669
fn main() {
@@ -80,6 +83,7 @@ fn main() {
8083
Some("info") => Command::Info,
8184
Some("clone-gcc") => Command::CloneGcc,
8285
Some("abi-test") => Command::AbiTest,
86+
Some("check-todo") => Command::CheckTodo,
8387
Some("fmt") => Command::Fmt,
8488
Some("fuzz") => Command::Fuzz,
8589
Some("--help") => {
@@ -106,6 +110,7 @@ fn main() {
106110
Command::Fmt => fmt::run(),
107111
Command::Fuzz => fuzz::run(),
108112
Command::AbiTest => abi_test::run(),
113+
Command::CheckTodo => todo::run(),
109114
} {
110115
eprintln!("Command failed to run: {e}");
111116
process::exit(1);

build_system/src/todo.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
use std::ffi::OsStr;
2+
use std::fs;
3+
use std::path::{Path, PathBuf};
4+
use std::process::Command;
5+
6+
const EXTENSIONS: &[&str] =
7+
&["rs", "py", "js", "sh", "c", "cpp", "h", "md", "css", "ftl", "toml", "yml", "yaml"];
8+
const SKIP_FILES: &[&str] = &["build_system/src/todo.rs", ".github/workflows/ci.yml"];
9+
10+
fn has_supported_extension(path: &Path) -> bool {
11+
path.extension().is_some_and(|ext| EXTENSIONS.iter().any(|e| ext == OsStr::new(e)))
12+
}
13+
14+
fn is_editor_temp(path: &Path) -> bool {
15+
path.file_name().is_some_and(|name| name.to_string_lossy().starts_with(".#"))
16+
}
17+
18+
fn list_tracked_files() -> Result<Vec<PathBuf>, String> {
19+
let output = Command::new("git")
20+
.args(["ls-files", "-z"])
21+
.output()
22+
.map_err(|e| format!("Failed to run `git ls-files`: {e}"))?;
23+
24+
if !output.status.success() {
25+
let stderr = String::from_utf8_lossy(&output.stderr);
26+
return Err(format!("`git ls-files` failed: {stderr}"));
27+
}
28+
29+
let mut files = Vec::new();
30+
for entry in output.stdout.split(|b| *b == 0) {
31+
if entry.is_empty() {
32+
continue;
33+
}
34+
let path = std::str::from_utf8(entry)
35+
.map_err(|_| "Non-utf8 path returned by `git ls-files`".to_string())?;
36+
files.push(PathBuf::from(path));
37+
}
38+
39+
Ok(files)
40+
}
41+
42+
pub(crate) fn run() -> Result<(), String> {
43+
let files = list_tracked_files()?;
44+
let mut errors = Vec::new();
45+
// Avoid embedding the task marker in source so greps only find real occurrences.
46+
let todo_marker = "todo".to_ascii_uppercase();
47+
48+
for file in files {
49+
if is_editor_temp(&file) {
50+
continue;
51+
}
52+
if SKIP_FILES.iter().any(|skip| file.ends_with(Path::new(skip))) {
53+
continue;
54+
}
55+
if !has_supported_extension(&file) {
56+
continue;
57+
}
58+
59+
let bytes =
60+
fs::read(&file).map_err(|e| format!("Failed to read `{}`: {e}", file.display()))?;
61+
let Ok(contents) = std::str::from_utf8(&bytes) else {
62+
continue;
63+
};
64+
65+
for (i, line) in contents.split('\n').enumerate() {
66+
let trimmed = line.trim();
67+
if trimmed.contains(&todo_marker) && !trimmed.contains("ignore-tidy-todo") {
68+
errors.push(format!(
69+
"{}:{}: {} is used for tasks that should be done before merging a PR; if you want to leave a message in the codebase use FIXME",
70+
file.display(),
71+
i + 1,
72+
todo_marker
73+
));
74+
}
75+
}
76+
}
77+
78+
if errors.is_empty() {
79+
return Ok(());
80+
}
81+
82+
for err in &errors {
83+
eprintln!("{err}");
84+
}
85+
86+
Err(format!("found {} {}(s)", errors.len(), todo_marker))
87+
}

0 commit comments

Comments
 (0)