Skip to main content

ifc_validate/express/
coverage.rs

1//! What the evaluator can run of one release, found by reading the rules.
2//!
3//! A declaration is evaluable when neither it nor any FUNCTION it calls,
4//! transitively, uses a construct the evaluator refuses: an unimplemented
5//! built-in (`FORMAT`, `HIBOUND`, `LOBOUND`, `ROLESOF`), a procedure call,
6//! `LIKE`, or `||` over something other than entity constructors. The
7//! count is static: a rule can still be stopped at run time by a budget or
8//! by data it cannot read, and that is reported per instance.
9
10use std::collections::BTreeSet;
11
12use super::ast::{BinOp, Builtin, Callee, Expr, Stmt};
13use super::tables::Tables;
14
15/// Evaluable declarations of one kind.
16#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
17#[non_exhaustive]
18pub struct RuleCount {
19    /// Declared in the schema.
20    pub total: usize,
21    /// Of those, evaluable.
22    pub evaluable: usize,
23}
24
25/// The evaluator's coverage of one release.
26#[derive(Debug, Clone, Default, PartialEq, Eq)]
27#[non_exhaustive]
28pub struct Coverage {
29    /// The schema name.
30    pub release: String,
31    /// Entity WHERE rules.
32    pub entity_rules: RuleCount,
33    /// Defined-type WHERE rules.
34    pub type_rules: RuleCount,
35    /// Global RULEs (counted once each, whatever their WHERE clauses).
36    pub global_rules: RuleCount,
37    /// FUNCTIONs.
38    pub functions: RuleCount,
39    /// DERIVE attributes.
40    pub derived: RuleCount,
41    /// Every declaration that is not evaluable, with the construct that
42    /// stops it, sorted.
43    pub not_evaluable: Vec<(String, String)>,
44}
45
46pub(crate) fn measure(tables: &Tables) -> Coverage {
47    let schema = &tables.schema;
48    // Direct refusals per function, then closed over calls.
49    let direct: Vec<BTreeSet<String>> = schema
50        .functions
51        .iter()
52        .map(|f| {
53            let mut found = Found::default();
54            f.locals
55                .iter()
56                .filter_map(|l| l.init.as_ref())
57                .for_each(|e| found.expr(e));
58            f.body.iter().for_each(|s| found.stmt(s));
59            found.refused
60        })
61        .collect();
62    let calls: Vec<BTreeSet<usize>> = schema
63        .functions
64        .iter()
65        .map(|f| {
66            let mut found = Found::default();
67            f.body.iter().for_each(|s| found.stmt(s));
68            f.locals
69                .iter()
70                .filter_map(|l| l.init.as_ref())
71                .for_each(|e| found.expr(e));
72            found.calls
73        })
74        .collect();
75    // A function is refused for its own first refusal, or for the first
76    // refusal of a function it calls; only `None` turns `Some`, so this
77    // settles.
78    let mut closed: Vec<Option<String>> = direct.iter().map(|d| d.iter().next().cloned()).collect();
79    loop {
80        let mut changed = false;
81        for (index, callees) in calls.iter().enumerate() {
82            if closed[index].is_some() {
83                continue;
84            }
85            if let Some((callee, why)) = callees
86                .iter()
87                .find_map(|&c| closed[c].as_ref().map(|why| (c, why.clone())))
88            {
89                closed[index] = Some(format!("{why} (via {})", schema.functions[callee].spelling));
90                changed = true;
91            }
92        }
93        if !changed {
94            break;
95        }
96    }
97    let mut coverage = Coverage {
98        release: schema.name.to_string(),
99        ..Coverage::default()
100    };
101    let mut judge = |name: String, exprs: &[&Expr], stmts: &[Stmt], count: &mut RuleCount| {
102        let mut found = Found::default();
103        exprs.iter().for_each(|e| found.expr(e));
104        stmts.iter().for_each(|s| found.stmt(s));
105        let mut refused = found.refused;
106        for callee in found.calls {
107            if let Some(why) = &closed[callee] {
108                refused.insert(format!("{why} (via {})", schema.functions[callee].spelling));
109            }
110        }
111        count.total += 1;
112        match refused.into_iter().next() {
113            None => count.evaluable += 1,
114            Some(why) => coverage.not_evaluable.push((name, why)),
115        }
116    };
117    let (mut entity_rules, mut type_rules, mut global_rules, mut derived) = Default::default();
118    for entity in &schema.entities {
119        for rule in &entity.rules {
120            let name = format!("{}.{}", entity.spelling, rule.label);
121            judge(name, &[&rule.expr], &[], &mut entity_rules);
122        }
123        for attr in &entity.derived {
124            let name = format!("{}.{}", entity.spelling, attr.spelling);
125            judge(name, &[&attr.expr], &[], &mut derived);
126        }
127    }
128    for decl in &schema.types {
129        for rule in &decl.rules {
130            let name = format!("{}.{}", decl.spelling, rule.label);
131            judge(name, &[&rule.expr], &[], &mut type_rules);
132        }
133    }
134    for rule in &schema.rules {
135        let mut exprs: Vec<&Expr> = rule.rules.iter().map(|r| &r.expr).collect();
136        exprs.extend(rule.locals.iter().filter_map(|l| l.init.as_ref()));
137        judge(
138            format!("global.{}", rule.spelling),
139            &exprs,
140            &rule.body,
141            &mut global_rules,
142        );
143    }
144    let mut functions = RuleCount::default();
145    for (index, function) in schema.functions.iter().enumerate() {
146        functions.total += 1;
147        match &closed[index] {
148            None => functions.evaluable += 1,
149            Some(why) => coverage
150                .not_evaluable
151                .push((function.spelling.to_string(), why.clone())),
152        }
153    }
154    coverage.entity_rules = entity_rules;
155    coverage.type_rules = type_rules;
156    coverage.global_rules = global_rules;
157    coverage.derived = derived;
158    coverage.functions = functions;
159    coverage.not_evaluable.sort();
160    coverage
161}
162
163/// The refused constructs and called functions found in some code.
164#[derive(Default)]
165struct Found {
166    refused: BTreeSet<String>,
167    calls: BTreeSet<usize>,
168}
169
170impl Found {
171    fn stmt(&mut self, stmt: &Stmt) {
172        match stmt {
173            Stmt::Null | Stmt::Escape | Stmt::Skip => {}
174            Stmt::Assign(target, value) => {
175                for access in &target.path {
176                    if let super::ast::Access::Index(index) = access {
177                        self.expr(index);
178                    }
179                }
180                self.expr(value);
181            }
182            Stmt::Case {
183                selector,
184                arms,
185                otherwise,
186            } => {
187                self.expr(selector);
188                for (labels, body) in arms {
189                    labels.iter().for_each(|l| self.expr(l));
190                    self.stmt(body);
191                }
192                if let Some(body) = otherwise {
193                    self.stmt(body);
194                }
195            }
196            Stmt::Compound(body) => body.iter().for_each(|s| self.stmt(s)),
197            Stmt::If {
198                condition,
199                then,
200                otherwise,
201            } => {
202                self.expr(condition);
203                then.iter().chain(otherwise).for_each(|s| self.stmt(s));
204            }
205            Stmt::Repeat {
206                increment,
207                while_,
208                until,
209                body,
210            } => {
211                if let Some(increment) = increment {
212                    self.expr(&increment.from);
213                    self.expr(&increment.to);
214                    if let Some(by) = &increment.by {
215                        self.expr(by);
216                    }
217                }
218                while_.iter().chain(until).for_each(|e| self.expr(e));
219                body.iter().for_each(|s| self.stmt(s));
220            }
221            Stmt::Return(value) => value.iter().for_each(|e| self.expr(e)),
222            Stmt::Call(name, args) => {
223                self.refused.insert(format!("the procedure {name}"));
224                args.iter().for_each(|e| self.expr(e));
225            }
226        }
227    }
228
229    fn expr(&mut self, expr: &Expr) {
230        match expr {
231            Expr::Call(callee, args) => {
232                match callee {
233                    Callee::Builtin(builtin) if builtin.unsupported() => {
234                        self.refused
235                            .insert(format!("the built-in {}", builtin_name(*builtin)));
236                    }
237                    Callee::Function(index) => {
238                        self.calls.insert(*index);
239                    }
240                    Callee::Type(_) if args.len() != 1 => {
241                        self.refused
242                            .insert("a type constructor with other than one argument".into());
243                    }
244                    Callee::Unresolved(name) => {
245                        self.refused.insert(format!("the unresolved call {name}"));
246                    }
247                    _ => {}
248                }
249                args.iter().for_each(|e| self.expr(e));
250            }
251            Expr::Binary(BinOp::Like, left, right) => {
252                self.refused.insert("the LIKE operator".into());
253                self.expr(left);
254                self.expr(right);
255            }
256            Expr::Binary(BinOp::Complex, left, right) => {
257                for side in [left, right] {
258                    if !matches!(
259                        **side,
260                        Expr::Call(Callee::Entity(_), _) | Expr::Binary(BinOp::Complex, ..)
261                    ) {
262                        self.refused
263                            .insert("`||` over something other than entity constructors".into());
264                    }
265                    self.expr(side);
266                }
267            }
268            Expr::Binary(_, left, right) => {
269                self.expr(left);
270                self.expr(right);
271            }
272            Expr::Unary(_, operand) => self.expr(operand),
273            Expr::Attribute(base, _) | Expr::Group(base, _) => self.expr(base),
274            Expr::Index(base, index, high) => {
275                self.expr(base);
276                self.expr(index);
277                if let Some(high) = high {
278                    self.expr(high);
279                }
280            }
281            Expr::Aggregate(elems) => {
282                for elem in elems {
283                    self.expr(&elem.value);
284                    if let Some(repeat) = &elem.repeat {
285                        self.expr(repeat);
286                    }
287                }
288            }
289            Expr::Interval {
290                low, item, high, ..
291            } => {
292                self.expr(low);
293                self.expr(item);
294                self.expr(high);
295            }
296            Expr::Query {
297                source, condition, ..
298            } => {
299                self.expr(source);
300                self.expr(condition);
301            }
302            Expr::Name(name) => {
303                self.refused.insert(format!("the unresolved name {name}"));
304            }
305            _ => {}
306        }
307    }
308}
309
310fn builtin_name(builtin: Builtin) -> String {
311    format!("{builtin:?}").to_ascii_uppercase()
312}