Skip to content

Commit b2b35fc

Browse files
committed
check on self-derive in expand
1 parent 8194739 commit b2b35fc

File tree

1 file changed

+36
-15
lines changed

1 file changed

+36
-15
lines changed

svd-parser/src/expand.rs

+36-15
Original file line numberDiff line numberDiff line change
@@ -391,16 +391,20 @@ pub fn derive_cluster(
391391
let (dparent, dname) = BlockPath::parse_str(dpath);
392392
let rdpath;
393393
let cluster_path;
394-
let d = (if let Some(dparent) = dparent {
394+
if let Some(dparent) = dparent {
395395
cluster_path = dparent.new_cluster(dname);
396396
rdpath = dparent;
397-
index.clusters.get(&cluster_path)
398397
} else {
399398
cluster_path = path.new_cluster(dname);
400399
rdpath = path.clone();
401-
index.clusters.get(&cluster_path)
402-
})
403-
.ok_or_else(|| anyhow!("cluster {} not found", dpath))?;
400+
}
401+
if &rdpath == path && dname == &c.name {
402+
return Err(anyhow!("Cluster {cluster_path} is self-derived"));
403+
}
404+
let d = index
405+
.clusters
406+
.get(&cluster_path)
407+
.ok_or_else(|| anyhow!("cluster {} not found", dpath))?;
404408

405409
let mut cpath = None;
406410
if c.children.is_empty() {
@@ -422,16 +426,20 @@ pub fn derive_register(
422426
let (dblock, dname) = RegisterPath::parse_str(dpath);
423427
let rdpath;
424428
let reg_path;
425-
let d = (if let Some(dblock) = dblock {
429+
if let Some(dblock) = dblock {
426430
reg_path = dblock.new_register(dname);
427431
rdpath = dblock;
428-
index.registers.get(&reg_path)
429432
} else {
430433
reg_path = path.new_register(dname);
431434
rdpath = path.clone();
432-
index.registers.get(&reg_path)
433-
})
434-
.ok_or_else(|| anyhow!("register {} not found", dpath))?;
435+
}
436+
if &rdpath == path && dname == &r.name {
437+
return Err(anyhow!("Register {reg_path} is self-derived"));
438+
}
439+
let d = index
440+
.registers
441+
.get(&reg_path)
442+
.ok_or_else(|| anyhow!("register {} not found", dpath))?;
435443

436444
let mut rpath = None;
437445
if r.fields.is_none() {
@@ -453,16 +461,20 @@ pub fn derive_field(
453461
let (dregister, dname) = FieldPath::parse_str(dpath);
454462
let rdpath;
455463
let field_path;
456-
let d = (if let Some(dregister) = dregister {
464+
if let Some(dregister) = dregister {
457465
field_path = dregister.new_field(dname);
458466
rdpath = dregister;
459-
index.fields.get(&field_path)
460467
} else {
461468
field_path = rpath.new_field(dname);
462469
rdpath = rpath.clone();
463-
index.fields.get(&field_path)
464-
})
465-
.ok_or_else(|| anyhow!("field {} not found", dpath))?;
470+
}
471+
if &rdpath == rpath && dname == &f.name {
472+
return Err(anyhow!("Field {field_path} is self-derived"));
473+
}
474+
let d = index
475+
.fields
476+
.get(&field_path)
477+
.ok_or_else(|| anyhow!("field {} not found", dpath))?;
466478

467479
let mut fpath = None;
468480
if f.enumerated_values.is_empty() {
@@ -556,6 +568,9 @@ pub fn derive_enumerated_values(
556568
fpath: &FieldPath,
557569
index: &Index,
558570
) -> Result<EnumPath> {
571+
if Some(dpath) == ev.name.as_deref() {
572+
return Err(anyhow!("EnumeratedValues {fpath}.{dpath} is self-derived"));
573+
}
559574
let mut v: Vec<&str> = dpath.split('.').collect();
560575
let dname = v.pop().unwrap();
561576
let d = if v.is_empty() {
@@ -590,6 +605,9 @@ pub fn derive_enumerated_values(
590605
};
591606
FieldPath::new(rdpath, fdname)
592607
};
608+
if &fdpath == fpath && Some(dname) == ev.name.as_deref() {
609+
return Err(anyhow!("EnumeratedValues {fpath}.{dname} is self-derived"));
610+
}
593611
let epath = EnumPath::new(fdpath, dname);
594612
index.evs.get(&epath).map(|d| (d, epath))
595613
};
@@ -615,6 +633,9 @@ pub fn derive_peripheral(
615633
dpath: &str,
616634
index: &Index,
617635
) -> Result<Option<BlockPath>> {
636+
if dpath == &p.name {
637+
return Err(anyhow!("Peripheral {dpath} is self-derived"));
638+
}
618639
let mut path = None;
619640
let derpath = BlockPath::new(dpath);
620641
let d = index

0 commit comments

Comments
 (0)