Skip to content

Commit

Permalink
add static analyser plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Panagiotis Kourouklidis committed Jan 19, 2024
1 parent f9b82cb commit 49ac46d
Show file tree
Hide file tree
Showing 89 changed files with 3,763 additions and 0 deletions.
Empty file.
7 changes: 7 additions & 0 deletions plugins/org.eclipse.epsilon.ecl.staticanalyser/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.eclipse.epsilon</groupId>
<artifactId>plugins</artifactId>
<version>2.5.0-SNAPSHOT</version>
</parent>
<artifactId>org.eclipse.epsilon.ecl.staticanalyser</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>eclipse-plugin</packaging>
<name>EclStaticanalysis</name>
</project>
28 changes: 28 additions & 0 deletions plugins/org.eclipse.epsilon.ecl.staticanalyser/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>org.eclipse.epsilon.ecl.staticanalyser</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: EclStaticanalysis
Bundle-SymbolicName: org.eclipse.epsilon.ecl.staticanalyser
Bundle-Version: 1.0.0.qualifier
Automatic-Module-Name: org.eclipse.epsilon.ecl.staticanalysis
Bundle-RequiredExecutionEnvironment: JavaSE-11
Require-Bundle: org.eclipse.epsilon.eol.staticanalyser,
org.eclipse.epsilon.ecl.engine
Export-Package: org.eclipse.epsilon.ecl.staticanalyser
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package org.eclipse.epsilon.ecl.staticanalyser;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.epsilon.common.module.IModule;
import org.eclipse.epsilon.common.module.ModuleMarker;
import org.eclipse.epsilon.ecl.EclModule;
import org.eclipse.epsilon.ecl.dom.IEclVisitor;
import org.eclipse.epsilon.ecl.dom.MatchRule;
import org.eclipse.epsilon.eol.dom.ExecutableBlock;
import org.eclipse.epsilon.eol.dom.Expression;
import org.eclipse.epsilon.eol.dom.StatementBlock;
import org.eclipse.epsilon.eol.execute.context.FrameType;
import org.eclipse.epsilon.eol.execute.context.Variable;
import org.eclipse.epsilon.eol.staticanalyser.EolStaticAnalyser;
import org.eclipse.epsilon.eol.types.EolPrimitiveType;
import org.eclipse.epsilon.erl.dom.Post;
import org.eclipse.epsilon.erl.dom.Pre;

public class EclStaticAnalyser extends EolStaticAnalyser implements IEclVisitor {

@Override
public void visit(Post post) {
if (post != null)
post.getBody().accept(this);

}

@Override
public void visit(Pre pre) {
if (pre != null)
pre.getBody().accept(this);

}

@Override
public void visit(MatchRule matchRule) {
matchRule.getLeftParameter().accept(this);
matchRule.getRightParameter().accept(this);
if (matchRule.getGuardBlock() != null)
visit(matchRule.getGuardBlock());

if (matchRule.getDoBlock() != null)
visit(matchRule.getDoBlock());

if (matchRule.getCompareBlock() != null)
visit(matchRule.getCompareBlock());

if (matchRule.getLeftDomainBlock() != null)
visit(matchRule.getLeftDomainBlock());

if (matchRule.getRightDomainBlock() != null)
visit(matchRule.getRightDomainBlock());

}

public void visit(ExecutableBlock<?> ex) {
if(ex != null) {
if(ex.getBody() instanceof StatementBlock) {
StatementBlock guardBody = ((StatementBlock) ex.getBody());
guardBody.accept(this);
}
else
((Expression)ex.getBody()).accept(this);
}
}

@Override
public List<ModuleMarker> validate(IModule imodule) {

errors = new ArrayList<>();
if (!(imodule instanceof EclModule))
return null;
EclModule etlModule = (EclModule) imodule;
this.module = etlModule;

super.preValidate(etlModule);
for (Pre pre : etlModule.getDeclaredPre()) {
pre.accept(this);
}

super.mainValidate(etlModule);
for (MatchRule tr : etlModule.getMatchRules()) {
this.getContext().getFrameStack().enterLocal(FrameType.UNPROTECTED, tr);
tr.accept(this);
this.getContext().getFrameStack().leaveLocal(tr);

}

for (Post post : etlModule.getDeclaredPost()) {
post.accept(this);
}

super.postValidate(etlModule);

this.module.getData().put("staticAnalyser", this);

return errors;
}

}
11 changes: 11 additions & 0 deletions plugins/org.eclipse.epsilon.eol.staticanalyser.tests/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src">
<attributes>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin"/>
</classpath>
28 changes: 28 additions & 0 deletions plugins/org.eclipse.epsilon.eol.staticanalyser.tests/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>org.eclipse.epsilon.eol.staticanalyser.tests</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Tests
Bundle-SymbolicName: org.eclipse.epsilon.eol.staticanalyser.tests
Bundle-Version: 1.0.0.qualifier
Automatic-Module-Name: org.eclipse.epsilon.eol.staticanalyser.tests
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Require-Bundle: org.eclipse.epsilon.eol.engine,
org.eclipse.epsilon.eol.staticanalyser,
org.junit,
org.eclipse.emf.ecore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.
20 changes: 20 additions & 0 deletions plugins/org.eclipse.epsilon.eol.staticanalyser.tests/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.eclipse.epsilon.eol.staticanalyser.tests</groupId>
<artifactId>org.eclipse.epsilon.eol.staticanalyser.tests</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>org.eclipse.epsilon.eol.staticanalyser.tests</name>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package org.eclipse.epsilon.eol.staticanalyser.tests;

import static org.junit.Assert.assertEquals;

import java.util.List;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.epsilon.common.module.ModuleElement;
import org.eclipse.epsilon.common.module.ModuleMarker;
import org.eclipse.epsilon.eol.EolModule;
import org.eclipse.epsilon.eol.staticanalyser.EolStaticAnalyser;
import org.eclipse.epsilon.eol.staticanalyser.StaticModelFactory;
import org.eclipse.epsilon.eol.types.EolType;
import org.junit.Test;

public class EolStaticAnalyserTests {
static int ident = 0;
@Test
public void testModelTypesVariableDeclaration() throws Exception {
EolModule module = new EolModule();
module.parse(EolStaticAnalyserTests.class.getResource("variableDeclaration.eol").toURI());

printTree(module);
EPackage.Registry.INSTANCE.put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);

EolStaticAnalyser staticAnalyser = new EolStaticAnalyser(new StaticModelFactory());
List<ModuleMarker> errors = staticAnalyser.validate(module);
System.out.println(errors.get(0).getMessage());
assertEquals(1, errors.size());
}

public void printTree(ModuleElement parent) {
System.out.println(" ".repeat(ident) + parent.getClass().getSimpleName());
ident += 1;
for (ModuleElement m : parent.getChildren()) {
printTree(m);
}
ident -= 1;
}

@Test
public void testPrimitiveTypesVariableDeclaration() throws Exception {
assertValid("var i : Integer; (/*Integer*/i).println();");
assertValid("var b : Boolean; (/*Boolean*/b).println();");
assertValid("var s : String; (/*String*/s).println();");
assertValid("var r : Real; (/*Real*/r).println();");
}

@Test
public void testPrimitiveTypesAssignmentExpression() throws Exception {
StringBuffer st = new StringBuffer();
st.append("var i : Integer = 4;");
st.append("var s : String = \"test\";");
st.append("(/*Integer*/i) = (/*String*/s);");
st.append("var a : Any = true;");
st.append("(/*Any*/ a) = (/*String*/s);");
assertValid(st.toString());
}

@Test
public void testCollectionTypesAssignmentExpression() throws Exception {
StringBuffer st = new StringBuffer();
st.append("var col : Collection<Integer> = Collection{0..9};\n");
st.append("var seq : Sequence<Integer> = Sequence{0..9};\n");
st.append("var bg : Bag<Integer> = Bag{0..9};\n");
st.append("(/*Bag<Integer>*/bg) = (/*Collection<Integer>*/col);\n");
assertValid(st.toString());
}

@Test
public void testPrimitiveTypesAssignmentExpressionErrorMessage() throws Exception {
StringBuffer st = new StringBuffer();
st.append("var i : Integer = 4;");
st.append("var s : String = \"test\";");
String expectedMessage = "String cannot be assigned to Integer";
st.append("i = s;");
assertValidMessage(st.toString(),expectedMessage);
}

public void assertValid(String eol) throws Exception {
EolModule module = new EolModule();
module.parse(eol);
EolStaticAnalyser staticAnalyser = new EolStaticAnalyser(new StaticModelFactory());
staticAnalyser.validate(module);
visit(module.getChildren());
}

public void assertValidMessage(String eol, String message) throws Exception {
EolModule module = new EolModule();
module.parse(eol);
EolStaticAnalyser staticAnalyser = new EolStaticAnalyser(new StaticModelFactory());
List<ModuleMarker> errors = staticAnalyser.validate(module);
assertEquals(message, errors.get(0).getMessage());
}

protected void visit(List<ModuleElement> elements) {
for (ModuleElement element : elements) {
if (!element.getComments().isEmpty()) {
assertEquals(element.getComments().get(0).toString(), getResolvedType(element).toString());
}
visit(element.getChildren());
}
}

protected EolType getResolvedType(ModuleElement element) {
return (EolType) element.getData().get("resolvedType");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var j;
1+1;
j = new Person;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<ecore:EPackage xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="test" nsURI="test" nsPrefix="">
<eClassifiers xsi:type="ecore:EClass" name="Family">
<eStructuralFeatures xsi:type="ecore:EReference" name="p" lowerBound="1" upperBound="-1"
eType="#//Person" containment="true"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Person"/>
</ecore:EPackage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
model M alias X driver EMF {nsuri="http://www.eclipse.org/emf/2002/Ecore"};

var i:M!EClass;
var j:X!EClass;
7 changes: 7 additions & 0 deletions plugins/org.eclipse.epsilon.eol.staticanalyser/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions plugins/org.eclipse.epsilon.eol.staticanalyser/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
Loading

0 comments on commit 49ac46d

Please sign in to comment.