|
| 1 | +use std::collections::HashMap; |
| 2 | +use std::path::Path; |
| 3 | + |
| 4 | +use fluent_syntax::ast::{Entry, Expression, InlineExpression, PatternElement}; |
| 5 | +use fluent_syntax::parser; |
| 6 | +use regex::Regex; |
| 7 | + |
| 8 | +use crate::diagnostics::{CheckId, DiagCtx, RunningCheck}; |
| 9 | + |
| 10 | +pub fn check(root_path: &Path, diag_ctx: DiagCtx) { |
| 11 | + let path = &root_path.join("compiler"); |
| 12 | + let mut check = diag_ctx.start_check(CheckId::new("error_messages").path(path)); |
| 13 | + let regex = Regex::new(r#"(?:(?:#\[(diag|label|suggestion|note|help|help_once|multipart_suggestion)\()|(?:fluent(_generated)?::))\s*(?<id>[a-z_][a-zA-Z0-9_]+)\s*[\n,\)};]"#).unwrap(); |
| 14 | + |
| 15 | + for dir in std::fs::read_dir(path).unwrap() { |
| 16 | + let Ok(dir) = dir else { continue }; |
| 17 | + let dir = dir.path(); |
| 18 | + let messages_file = dir.join("messages.ftl"); |
| 19 | + |
| 20 | + if messages_file.is_file() { |
| 21 | + check_if_messages_are_used(&mut check, &dir.join("src"), &messages_file, ®ex); |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +fn check_fluent_ast<'a>(ids: &mut HashMap<&'a str, bool>, elem: &PatternElement<&'a str>) { |
| 27 | + if let PatternElement::Placeable { expression } = elem { |
| 28 | + match expression { |
| 29 | + Expression::Inline(InlineExpression::MessageReference { id, .. }) => { |
| 30 | + *ids.entry(&id.name).or_default() = true; |
| 31 | + } |
| 32 | + Expression::Select { variants, .. } => { |
| 33 | + for variant in variants { |
| 34 | + for elem in &variant.value.elements { |
| 35 | + check_fluent_ast(ids, elem); |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + _ => {} |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +fn check_if_messages_are_used( |
| 45 | + check: &mut RunningCheck, |
| 46 | + src_path: &Path, |
| 47 | + messages_file: &Path, |
| 48 | + regex: &Regex, |
| 49 | +) { |
| 50 | + // First we retrieve all error messages ID. |
| 51 | + let content = std::fs::read_to_string(messages_file).expect("failed to read file"); |
| 52 | + let resource = |
| 53 | + parser::parse(content.as_str()).expect("Errors encountered while parsing a resource."); |
| 54 | + |
| 55 | + let mut ids: HashMap<&str, bool> = HashMap::new(); |
| 56 | + for entry in &resource.body { |
| 57 | + if let Entry::Message(msg) = entry { |
| 58 | + let id: &str = &msg.id.name; |
| 59 | + if !ids.contains_key(&id) { |
| 60 | + ids.insert(id, false); |
| 61 | + } |
| 62 | + if let Some(value) = &msg.value { |
| 63 | + for elem in &value.elements { |
| 64 | + check_fluent_ast(&mut ids, elem); |
| 65 | + } |
| 66 | + } |
| 67 | + for attr in &msg.attributes { |
| 68 | + for elem in &attr.value.elements { |
| 69 | + check_fluent_ast(&mut ids, elem); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + assert!(!ids.is_empty()); |
| 76 | + |
| 77 | + let skip = |f: &Path, is_dir: bool| !is_dir && !f.extension().is_some_and(|ext| ext == "rs"); |
| 78 | + crate::walk::walk(src_path, skip, &mut |_path: &_, content: &str| { |
| 79 | + for cap in regex.captures_iter(content) { |
| 80 | + let id = &cap["id"]; |
| 81 | + if let Some(found) = ids.get_mut(id) { |
| 82 | + // Error message IDs can be used more than once. |
| 83 | + *found = true; |
| 84 | + } |
| 85 | + } |
| 86 | + }); |
| 87 | + const TO_IGNORE: &[&str] = &[ |
| 88 | + // FIXME: #114050 |
| 89 | + "hir_typeck_option_result_asref", |
| 90 | + ]; |
| 91 | + for (id, found) in ids { |
| 92 | + if !found && !TO_IGNORE.iter().any(|to_ignore| *to_ignore == id) { |
| 93 | + check.error(format!("unused message ID `{id}` from `{}`", messages_file.display())); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments