Skip to content

Commit

Permalink
config: Check if any classes are duplicated (#712)
Browse files Browse the repository at this point in the history
  • Loading branch information
PabstMirror authored Jun 30, 2024
1 parent 63504c3 commit 659a53b
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ use hemtt_workspace::reporting::{Code, Diagnostic, Label, Processed};

use crate::Class;

pub struct DuplicateExternal {
pub struct DuplicateClasses {
classes: Vec<Class>,
diagnostic: Option<Diagnostic>,
}

impl Code for DuplicateExternal {
impl Code for DuplicateClasses {
fn ident(&self) -> &'static str {
"CE8"
"CE9"
}

fn message(&self) -> String {
"external class defined multiple times".to_string()
"class defined multiple times".to_string()
}

fn label_message(&self) -> String {
Expand All @@ -38,7 +38,7 @@ impl Code for DuplicateExternal {
}
}

impl DuplicateExternal {
impl DuplicateClasses {
pub fn new(classes: Vec<Class>, processed: &Processed) -> Self {
Self {
classes,
Expand All @@ -49,7 +49,7 @@ impl DuplicateExternal {

fn generate_processed(mut self, processed: &Processed) -> Self {
let Some(name) = self.classes[0].name() else {
panic!("DuplicateExternal::generate_processed called on class without name");
panic!("DuplicateClasses::generate_processed called on class without name");
};
self.diagnostic = Diagnostic::new_for_processed(&self, name.span.clone(), processed);
if let Some(diag) = &mut self.diagnostic {
Expand Down
3 changes: 2 additions & 1 deletion libs/config/src/analyze/codes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ pub mod ce4_missing_semicolon;
pub mod ce5_unexpected_array;
pub mod ce6_expected_array;
pub mod ce7_missing_parent;
pub mod ce8_duplicate_external;
// pub mod ce8_duplicate_external //* superseded by ce9_duplicate_classes
pub mod ce9_duplicate_classes;

pub mod cw1_parent_case;
pub mod cw2_magwell_missing_magazine;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ use std::{collections::HashMap, sync::Arc};

use hemtt_workspace::reporting::{Code, Processed};

use crate::{analyze::codes::ce8_duplicate_external::DuplicateExternal, Class, Property};
use crate::{analyze::codes::ce9_duplicate_classes::DuplicateClasses, Class, Property};

pub fn error(properties: &[Property], processed: &Processed) -> Vec<Arc<dyn Code>> {
let mut defined: HashMap<String, Vec<Class>> = HashMap::new();
let mut errors = Vec::new();
for property in properties {
if let Property::Class(c) = property {
match c {
Class::Root { properties } | Class::Local { properties, .. } => {
Class::Root { properties } => {
errors.extend(error(properties, processed));
}
Class::External { name } => {
Expand All @@ -19,12 +19,23 @@ pub fn error(properties: &[Property], processed: &Processed) -> Vec<Arc<dyn Code
.or_default()
.push(c.clone());
}
Class::Local {
name,
parent: _,
properties,
} => {
errors.extend(error(properties, processed));
defined
.entry(name.value.to_lowercase())
.or_default()
.push(c.clone());
}
}
}
}
errors.extend(defined.into_iter().filter_map(|(_, classes)| {
if classes.len() > 1 {
Some(Arc::new(DuplicateExternal::new(classes, processed)) as Arc<dyn Code>)
Some(Arc::new(DuplicateClasses::new(classes, processed)) as Arc<dyn Code>)
} else {
None
}
Expand Down
4 changes: 2 additions & 2 deletions libs/config/src/analyze/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::Config;

use super::Analyze;

mod duplicate_classes;
mod duplicate_properties;
mod external_duplicate;
mod external_missing;
mod external_parent_case;
mod magwells;
Expand Down Expand Up @@ -37,7 +37,7 @@ impl Analyze for Config {
.iter()
.flat_map(|p| p.errors(project, processed))
.collect::<Vec<_>>();
errors.extend(external_duplicate::error(&self.0, processed));
errors.extend(duplicate_classes::error(&self.0, processed));
errors.extend(external_missing::error(&self.0, processed));
errors.extend(duplicate_properties::duplicate_properties(
&self.0, processed,
Expand Down
4 changes: 2 additions & 2 deletions libs/config/tests/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ fn check(dir: &str) {

bootstrap!(ce1_invalid_value);
bootstrap!(ce2_invalid_value_macro);
bootstrap!(ce3_duplicate_property_separate);
bootstrap!(ce3_duplicate_property_shadow_property);
bootstrap!(ce4_missing_semicolon);
bootstrap!(ce5_unexpected_array);
bootstrap!(ce6_expected_array);
bootstrap!(ce7_missing_parent);
bootstrap!(ce8_duplicate_external);
bootstrap!(ce9_duplicate_class);
bootstrap!(ce9_duplicate_external);

This file was deleted.

This file was deleted.

5 changes: 5 additions & 0 deletions libs/config/tests/errors/ce9_duplicate_class/source.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class CfgAmmo {
class BulletBase;
class MissileBase;
class MissileBase: BulletBase {};
};
10 changes: 10 additions & 0 deletions libs/config/tests/errors/ce9_duplicate_class/stdout.ansi
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error[CE9]: class defined multiple times
┌─ source.hpp:3:11
│
3 │ class MissileBase;
│ ^^^^^^^^^^^ defined multiple times
4 │ class MissileBase: BulletBase {};
│ ----------- also defined here
│
= help: remove all but the first definition of `class MissileBase;`

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[CE8]: external class defined multiple times
error[CE9]: class defined multiple times
┌─ source.hpp:2:11
│
2 │ class BulletBase;
Expand Down

0 comments on commit 659a53b

Please sign in to comment.