rumoca 0.7.28

Modelica compiler written in RUST
Documentation
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! Lint module for Modelica code analysis.
//!
//! Provides lint rules similar to Clippy for Rust:
//! - Style checks (naming conventions, documentation)
//! - Correctness checks (unused variables, undefined references)
//! - Performance suggestions
//! - Best practices
//!
//! ## Configuration
//!
//! The linter can be configured via:
//! - A `.rumoca_lint.toml` or `rumoca_lint.toml` file in the project root
//! - Command line options (override file settings)
//!
//! Example config file:
//! ```toml
//! min_level = "warning"
//! disabled_rules = ["magic-number", "missing-documentation"]
//! ```

mod rules;

pub use rules::*;

// Re-export shared symbol analysis from ir/analysis
pub use crate::ir::analysis::symbols::{
    DefinedSymbol, collect_defined_symbols, collect_used_symbols, is_class_instance_type,
};

use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::path::Path;

use crate::ir::analysis::symbol_table::SymbolTable;
use crate::ir::ast::{ClassDefinition, ClassType, StoredDefinition, Variability};
use crate::ir::transform::flatten::flatten;

/// Severity level for lint messages
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum LintLevel {
    /// Suggestions for improvement
    Help,
    /// Style or convention issues
    Note,
    /// Potential problems
    Warning,
    /// Definite errors
    Error,
}

impl std::fmt::Display for LintLevel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            LintLevel::Help => write!(f, "help"),
            LintLevel::Note => write!(f, "note"),
            LintLevel::Warning => write!(f, "warning"),
            LintLevel::Error => write!(f, "error"),
        }
    }
}

/// A lint message
#[derive(Debug, Clone)]
pub struct LintMessage {
    /// The lint rule that generated this message
    pub rule: &'static str,
    /// Severity level
    pub level: LintLevel,
    /// Human-readable message
    pub message: String,
    /// File path
    pub file: String,
    /// Line number (1-based)
    pub line: u32,
    /// Column number (1-based)
    pub column: u32,
    /// Optional suggestion for fixing the issue
    pub suggestion: Option<String>,
}

impl LintMessage {
    pub fn new(
        rule: &'static str,
        level: LintLevel,
        message: impl Into<String>,
        file: impl Into<String>,
        line: u32,
        column: u32,
    ) -> Self {
        Self {
            rule,
            level,
            message: message.into(),
            file: file.into(),
            line,
            column,
            suggestion: None,
        }
    }

    pub fn with_suggestion(mut self, suggestion: impl Into<String>) -> Self {
        self.suggestion = Some(suggestion.into());
        self
    }
}

/// Config file names to search for (in priority order)
pub const LINT_CONFIG_FILE_NAMES: &[&str] = &[".rumoca_lint.toml", "rumoca_lint.toml"];

/// Configuration for which lints to run
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct LintConfig {
    /// Minimum severity to report
    pub min_level: LintLevel,
    /// Specific rules to disable
    pub disabled_rules: HashSet<String>,
    /// Specific rules to enable (if empty, all are enabled)
    pub enabled_rules: HashSet<String>,
    /// Whether to treat warnings as errors
    pub deny_warnings: bool,
}

impl Default for LintConfig {
    fn default() -> Self {
        Self {
            min_level: LintLevel::Help,
            disabled_rules: HashSet::new(),
            enabled_rules: HashSet::new(),
            deny_warnings: false,
        }
    }
}

impl LintConfig {
    /// Check if a rule should be run
    pub fn should_run(&self, rule: &str) -> bool {
        if self.disabled_rules.contains(rule) {
            return false;
        }
        if !self.enabled_rules.is_empty() && !self.enabled_rules.contains(rule) {
            return false;
        }
        true
    }

    /// Check if a message should be reported
    pub fn should_report(&self, msg: &LintMessage) -> bool {
        msg.level >= self.min_level && self.should_run(msg.rule)
    }

    /// Load lint config from a config file.
    ///
    /// Searches for config files in the following order:
    /// 1. `.rumoca_lint.toml` in the given directory
    /// 2. `rumoca_lint.toml` in the given directory
    /// 3. Same files in parent directories, up to the root
    ///
    /// Returns `None` if no config file is found.
    pub fn from_config_file(start_dir: &Path) -> Option<Self> {
        let mut current = start_dir.to_path_buf();
        if current.is_file() {
            current = current.parent()?.to_path_buf();
        }

        loop {
            for config_name in LINT_CONFIG_FILE_NAMES {
                let config_path = current.join(config_name);
                if config_path.exists()
                    && let Ok(contents) = std::fs::read_to_string(&config_path)
                    && let Ok(config) = toml::from_str::<LintConfig>(&contents)
                {
                    return Some(config);
                }
            }

            // Move to parent directory
            if let Some(parent) = current.parent() {
                current = parent.to_path_buf();
            } else {
                break;
            }
        }

        None
    }

    /// Merge CLI options into this config, with CLI taking precedence.
    pub fn merge_cli_options(
        &mut self,
        cli_min_level: Option<LintLevel>,
        cli_disabled_rules: &[String],
        cli_enabled_rules: &[String],
        cli_deny_warnings: Option<bool>,
    ) {
        if let Some(min_level) = cli_min_level {
            self.min_level = min_level;
        }
        // CLI disabled rules are additive
        for rule in cli_disabled_rules {
            self.disabled_rules.insert(rule.clone());
        }
        // CLI enabled rules override file config if specified
        if !cli_enabled_rules.is_empty() {
            self.enabled_rules = cli_enabled_rules.iter().cloned().collect();
        }
        if let Some(deny_warnings) = cli_deny_warnings {
            self.deny_warnings = deny_warnings;
        }
    }
}

/// Result of linting a file
#[derive(Debug, Clone)]
pub struct LintResult {
    /// File that was linted
    pub file: String,
    /// All lint messages
    pub messages: Vec<LintMessage>,
    /// Whether parsing succeeded
    pub parsed: bool,
}

impl LintResult {
    pub fn new(file: impl Into<String>) -> Self {
        Self {
            file: file.into(),
            messages: Vec::new(),
            parsed: false,
        }
    }

    /// Count messages by level
    pub fn count_by_level(&self, level: LintLevel) -> usize {
        self.messages.iter().filter(|m| m.level == level).count()
    }

    /// Check if there are any errors
    pub fn has_errors(&self) -> bool {
        self.messages.iter().any(|m| m.level == LintLevel::Error)
    }

    /// Check if there are any warnings or errors
    pub fn has_warnings(&self) -> bool {
        self.messages.iter().any(|m| m.level >= LintLevel::Warning)
    }
}

/// Lint a Modelica source string
pub fn lint_str(source: &str, file_path: &str, config: &LintConfig) -> LintResult {
    let mut result = LintResult::new(file_path);

    // Parse the file
    use crate::modelica_grammar::ModelicaGrammar;
    use crate::modelica_parser::parse;

    let mut grammar = ModelicaGrammar::new();
    match parse(source, file_path, &mut grammar) {
        Ok(_) => {
            result.parsed = true;
            if let Some(ref ast) = grammar.modelica {
                lint_ast(ast, source, file_path, config, &mut result);
            }
        }
        Err(e) => {
            result.parsed = false;
            result.messages.push(LintMessage::new(
                "parse-error",
                LintLevel::Error,
                format!("Failed to parse: {}", e),
                file_path,
                1,
                1,
            ));
        }
    }

    result
}

/// Lint a Modelica file
pub fn lint_file(path: &Path, config: &LintConfig) -> LintResult {
    let file_path = path.to_string_lossy().to_string();

    match std::fs::read_to_string(path) {
        Ok(source) => lint_str(&source, &file_path, config),
        Err(e) => {
            let mut result = LintResult::new(&file_path);
            result.messages.push(LintMessage::new(
                "io-error",
                LintLevel::Error,
                format!("Failed to read file: {}", e),
                &file_path,
                1,
                1,
            ));
            result
        }
    }
}

/// Lint an AST
fn lint_ast(
    ast: &StoredDefinition,
    source: &str,
    file_path: &str,
    config: &LintConfig,
    result: &mut LintResult,
) {
    // Build initial scope with peer class names (for cross-class type references)
    let mut base_scope = SymbolTable::new();
    for class_name in ast.class_list.keys() {
        base_scope.add_global(class_name);
    }

    // Run lints on each top-level class
    for (class_name, class) in &ast.class_list {
        lint_class(
            class,
            class_name,
            ast,
            source,
            file_path,
            config,
            result,
            &base_scope, // Start with builtins + peer class names
        );
    }
}

/// Lint a class definition
#[allow(clippy::too_many_arguments)]
fn lint_class(
    class: &ClassDefinition,
    class_path: &str,
    ast: &StoredDefinition,
    source: &str,
    file_path: &str,
    config: &LintConfig,
    result: &mut LintResult,
    parent_scope: &SymbolTable,
) {
    // Clone parent scope to build current scope (includes builtins from SymbolTable::new())
    let mut scope = parent_scope.clone();

    // Try to flatten for inherited symbol analysis
    let flattened = match flatten(ast, Some(class_path)) {
        Ok(fc) => Some(fc),
        Err(e) => {
            // Report flatten error as a lint warning
            let error_msg = e.to_string();
            let short_msg = error_msg.lines().next().unwrap_or(&error_msg);
            result.messages.push(LintMessage {
                level: LintLevel::Warning,
                rule: "flatten-error",
                message: format!("could not flatten '{}': {}", class_path, short_msg),
                file: file_path.to_string(),
                line: class.name.location.start_line,
                column: class.name.location.start_column,
                suggestion: None,
            });
            None
        }
    };
    let analysis_class = flattened.as_ref().unwrap_or(class);

    // Add symbols from the flattened class to the scope using SymbolTable's add_symbol
    for (name, comp) in analysis_class.iter_components() {
        let is_parameter = matches!(comp.variability, Variability::Parameter(_));
        scope.add_symbol(name, name, &comp.type_name.to_string(), is_parameter);
    }
    // Add nested class names as global symbols (they're callable/usable)
    for (nested_name, _) in analysis_class.iter_classes() {
        scope.add_global(nested_name);
    }

    // Run individual lint rules
    if config.should_run("naming-convention") {
        lint_naming_conventions(class, file_path, result);
    }

    if config.should_run("missing-documentation") {
        lint_missing_documentation(class, file_path, result);
    }

    if config.should_run("unused-variable") {
        // Skip unused variable checking for records and connectors
        // since their fields are accessed externally
        if !matches!(class.class_type, ClassType::Record | ClassType::Connector) {
            lint_unused_variables(analysis_class, file_path, &scope, result);
        }
    }

    if config.should_run("undefined-reference") {
        lint_undefined_references(analysis_class, file_path, &scope, result);
    }

    if config.should_run("parameter-no-default") {
        lint_parameter_defaults(class, file_path, result);
    }

    if config.should_run("empty-section") {
        lint_empty_sections(class, file_path, result);
    }

    if config.should_run("magic-number") {
        lint_magic_numbers(class, file_path, source, result);
    }

    if config.should_run("complex-expression") {
        lint_complex_expressions(class, file_path, result);
    }

    if config.should_run("inconsistent-units") {
        lint_unit_consistency(class, file_path, result);
    }

    if config.should_run("redundant-extends") {
        lint_redundant_extends(class, file_path, result);
    }

    // Recursively lint nested classes, passing current scope
    for (nested_name, nested_class) in class.iter_classes() {
        let nested_path = format!("{}.{}", class_path, nested_name);
        lint_class(
            nested_class,
            &nested_path,
            ast,
            source,
            file_path,
            config,
            result,
            &scope,
        );
    }
}