Skip to content

Commit

Permalink
Fix duplicate traversal in inheritance traversing for overridden nodes.
Browse files Browse the repository at this point in the history
  • Loading branch information
N-Jansen committed Oct 9, 2024
1 parent 2c52107 commit 9b223a7
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import de.se_rwth.commons.Joiners;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static de.monticore.cd.codegen.CD2JavaTemplates.ANNOTATIONS;
Expand Down Expand Up @@ -100,10 +102,10 @@ protected List<ASTCDMethod> getASTHandleMethods(ASTCDDefinition astcdDefinition,
protected ASTCDMethod getASTHandleMethod(ASTCDClass astcdClass, String languageInterfaceName, String handlerSimpleTypeName) {
ASTCDMethod handleMethod = visitorService.getVisitorMethod(HANDLE,
getMCTypeFacade().createQualifiedType(Joiners.DOT.join(visitorService.getASTPackage(), astcdClass.getName())));
List<String> superTypeList = new ArrayList<>();
Set<String> superTypeList = new LinkedHashSet<>();
// super classes
if (astcdClass.isPresentCDExtendUsage() && !astcdClass.printSuperclasses().isEmpty()) {
superTypeList= visitorService.getAllSuperClassesTransitive(astcdClass);
superTypeList.addAll(visitorService.getAllSuperClassesTransitive(astcdClass));
}
// super interfaces
superTypeList.addAll(visitorService.getAllSuperInterfacesTransitive(astcdClass.getSymbol()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ grammar Sub extends mc.feature.visitor.Sup {

E = B;

B = "test2";
B = "test2" NodeImpl?;

@Override
NodeImpl implements SomeNode = "NodeOverride";

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ component grammar Sup extends mc.common.Basics {

A = "test1" E;

interface SomeNode;

NodeImpl implements SomeNode = "NodeImpl";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* (c) https://github.com/MontiCore/monticore */

package mc.feature.visitor;

import mc.feature.visitor.sup._ast.ASTSomeNode;
import mc.feature.visitor.sup._visitor.SupVisitor2;

public class NodeCounter implements SupVisitor2 {

protected int num = 0;

@Override
public void visit(ASTSomeNode node) {
num++;
}

public int getNum() {
return num;
}

public void setNum(int num) {
this.num = num;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import de.se_rwth.commons.logging.Log;
import mc.GeneratorIntegrationsTest;
import mc.feature.visitor.sub.SubMill;
import mc.feature.visitor.sub._ast.ASTE;
import mc.feature.visitor.sub._parser.SubParser;
import mc.feature.visitor.sub._visitor.SubTraverser;
import mc.feature.visitor.sup._ast.ASTA;
Expand Down Expand Up @@ -58,4 +59,33 @@ public void testConcreteVisitor() throws IOException {
Assertions.assertTrue(Log.getFindings().isEmpty());
}


@Test
public void testInheritanceTraversal() throws IOException {
SubParser p = new SubParser();
Optional<ASTE> node = p.parse_String("test2 NodeOverride");
Assertions.assertFalse(p.hasErrors());
Assertions.assertTrue(node.isPresent());

// init with plain traverser
SubTraverser t1 = SubMill.traverser();
NodeCounter c1 = new NodeCounter();
t1.add4Sup(c1);

// plain traverser should not reach the interface implementation
node.get().accept(t1);
Assertions.assertEquals(0, c1.getNum());


// init with inheritance traverser
SubTraverser t2 = SubMill.inheritanceTraverser();
NodeCounter c2 = new NodeCounter();
t2.add4Sup(c2);

// inheritance traverser should reach the interface implementation precisely once
node.get().accept(t2);
Assertions.assertEquals(1, c2.getNum());

}

}

0 comments on commit 9b223a7

Please sign in to comment.