Skip to content

Commit 801c98f

Browse files
authored
fix: generated globals are not duplicates (#1301)
1 parent 77c791d commit 801c98f

File tree

4 files changed

+61
-28
lines changed

4 files changed

+61
-28
lines changed

compiler/plc_ast/src/ast.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use std::{
44
fmt::{Debug, Display, Formatter},
5+
hash::Hash,
56
ops::Range,
67
};
78

@@ -398,7 +399,7 @@ impl Debug for VariableBlock {
398399
}
399400
}
400401

401-
#[derive(Clone, PartialEq)]
402+
#[derive(Clone)]
402403
pub struct Variable {
403404
pub name: String,
404405
pub data_type_declaration: DataTypeDeclaration,
@@ -407,6 +408,12 @@ pub struct Variable {
407408
pub location: SourceLocation,
408409
}
409410

411+
impl PartialEq for Variable {
412+
fn eq(&self, other: &Self) -> bool {
413+
self.name == other.name && self.location == other.location
414+
}
415+
}
416+
410417
impl Debug for Variable {
411418
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
412419
let mut var = f.debug_struct("Variable");

compiler/plc_ast/src/pre_processor.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
ast::{
99
flatten_expression_list, Assignment, AstFactory, AstNode, AstStatement, CompilationUnit, DataType,
1010
DataTypeDeclaration, DirectAccessType, HardwareAccess, HardwareAccessType, Operator, Pou,
11-
UserTypeDeclaration, Variable, VariableBlockType,
11+
UserTypeDeclaration, Variable, VariableBlock, VariableBlockType,
1212
},
1313
literals::AstLiteral,
1414
provider::IdProvider,
@@ -189,20 +189,26 @@ fn process_global_variables(unit: &mut CompilationUnit, id_provider: &mut IdProv
189189
data_type_declaration: ref_ty.unwrap_or(global_var.data_type_declaration.clone()),
190190
initializer: None,
191191
address: None,
192-
location: global_var.location.clone(),
192+
location: node.location.clone(),
193193
};
194194
mangled_globals.push(internal_mangled_var);
195195
}
196196
}
197197
}
198198

199-
if let Some(block) =
200-
unit.global_vars.iter_mut().find(|block| block.variable_block_type == VariableBlockType::Global)
201-
{
202-
for new_var in mangled_globals {
203-
block.variables.push(new_var);
199+
let mut block = if let Some(index) = unit.global_vars.iter().position(|block| {
200+
block.variable_block_type == VariableBlockType::Global && block.location.is_internal()
201+
}) {
202+
unit.global_vars.remove(index)
203+
} else {
204+
VariableBlock::default().with_block_type(VariableBlockType::Global)
205+
};
206+
for var in mangled_globals {
207+
if !block.variables.contains(&var) {
208+
block.variables.push(var);
204209
}
205210
}
211+
unit.global_vars.push(block);
206212
}
207213

208214
fn build_enum_initializer(

src/index/tests/index_tests.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,12 +1798,12 @@ fn aliased_hardware_access_variable_creates_global_var_for_address() {
17981798
span: Range(
17991799
TextLocation {
18001800
line: 2,
1801-
column: 12,
1802-
offset: 36,
1801+
column: 16,
1802+
offset: 40,
18031803
}..TextLocation {
18041804
line: 2,
1805-
column: 15,
1806-
offset: 39,
1805+
column: 29,
1806+
offset: 53,
18071807
},
18081808
),
18091809
},
@@ -1949,12 +1949,12 @@ fn address_used_in_2_aliases_only_created_once() {
19491949
span: Range(
19501950
TextLocation {
19511951
line: 2,
1952-
column: 12,
1953-
offset: 36,
1952+
column: 16,
1953+
offset: 40,
19541954
}..TextLocation {
19551955
line: 2,
1956-
column: 15,
1957-
offset: 39,
1956+
column: 29,
1957+
offset: 53,
19581958
},
19591959
),
19601960
},
@@ -1998,12 +1998,12 @@ fn aliased_variable_with_in_or_out_directions_create_the_same_variable() {
19981998
span: Range(
19991999
TextLocation {
20002000
line: 2,
2001-
column: 12,
2002-
offset: 36,
2001+
column: 16,
2002+
offset: 40,
20032003
}..TextLocation {
20042004
line: 2,
2005-
column: 15,
2006-
offset: 39,
2005+
column: 29,
2006+
offset: 53,
20072007
},
20082008
),
20092009
},
@@ -2029,12 +2029,12 @@ fn aliased_variable_with_in_or_out_directions_create_the_same_variable() {
20292029
span: Range(
20302030
TextLocation {
20312031
line: 4,
2032-
column: 12,
2033-
offset: 112,
2032+
column: 17,
2033+
offset: 117,
20342034
}..TextLocation {
20352035
line: 4,
2036-
column: 16,
2037-
offset: 116,
2036+
column: 30,
2037+
offset: 130,
20382038
},
20392039
),
20402040
},
@@ -2076,12 +2076,12 @@ fn if_two_aliased_var_of_different_types_use_the_same_address_the_first_wins() {
20762076
span: Range(
20772077
TextLocation {
20782078
line: 2,
2079-
column: 12,
2080-
offset: 36,
2079+
column: 16,
2080+
offset: 40,
20812081
}..TextLocation {
20822082
line: 2,
2083-
column: 15,
2084-
offset: 39,
2083+
column: 29,
2084+
offset: 53,
20852085
},
20862086
),
20872087
},
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: (%COMPILE %s && %RUN) | %CHECK %s
2+
VAR_GLOBAL
3+
a AT %IX1.2.1 : BOOL;
4+
b AT %QX1.2.2 : BOOL;
5+
c AT %ID1.2.3 : DWORD;
6+
d AT %ID1.2.4 : DWORD;
7+
END_VAR
8+
9+
FUNCTION main : DINT
10+
__init___hardware_reference_used_st();
11+
12+
a := TRUE;
13+
printf('%u$N', a); //CHECK: 1
14+
b := TRUE;
15+
printf('%u$N', b); //CHECK: 1
16+
c := 1337;
17+
printf('%d$N', c); //CHECK: 1337
18+
d := 98765;
19+
printf('%d$N', d); //CHECK: 98765
20+
END_FUNCTION

0 commit comments

Comments
 (0)