Skip to content

Commit

Permalink
[lang] Adding tests on field read accesses for prepare Xtext PR.
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Feb 18, 2018
1 parent fb41b2d commit f4708c5
Show file tree
Hide file tree
Showing 5 changed files with 725 additions and 46 deletions.
Expand Up @@ -162,7 +162,6 @@
import org.eclipse.xtext.xbase.XExpression;
import org.eclipse.xtext.xbase.XFeatureCall;
import org.eclipse.xtext.xbase.XForLoopExpression;
import org.eclipse.xtext.xbase.XMemberFeatureCall;
import org.eclipse.xtext.xbase.XSynchronizedExpression;
import org.eclipse.xtext.xbase.XTypeLiteral;
import org.eclipse.xtext.xbase.XVariableDeclaration;
Expand Down Expand Up @@ -2570,38 +2569,6 @@ private static EObject getOutermostType(XtendMember member) {
return result;
}

/** Replies if the given object is locally assigned.
*
* <p>An object is locally assigned when it is the left operand of an assignment operation.
*
* @param target the object to test.
* @param containerToFindUsage the container in which the usages should be find.
* @return {@code true} if the given object is assigned.
* @since 0.7
*/
protected boolean isLocallyAssigned(EObject target, EObject containerToFindUsage) {
if (this.readAndWriteTracking.isAssigned(target)) {
return true;
}
final Collection<Setting> usages = XbaseUsageCrossReferencer.find(target, containerToFindUsage);
// field are assigned when they are not used as the left operand of an assignment operator.
for (final Setting usage : usages) {
EObject object = usage.getEObject();
while (object instanceof XMemberFeatureCall) {
object = ((XMemberFeatureCall) object).eContainer();
}
if (object instanceof XAssignment) {
final XAssignment assignment = (XAssignment) object;
if (assignment.getFeature() == target) {
// Mark the field as assigned in order to be faster during the next assignment test.
this.readAndWriteTracking.markAssignmentAccess(target);
return true;
}
}
}
return false;
}

@Override
protected boolean isInitialized(JvmField input) {
if (super.isInitialized(input)) {
Expand Down Expand Up @@ -2631,6 +2598,35 @@ protected boolean isInitialized(JvmField input) {
return false;
}

/** Replies if the given object is locally assigned.
*
* <p>An object is locally assigned when it is the left operand of an assignment operation.
*
* @param target the object to test.
* @param containerToFindUsage the container in which the usages should be find.
* @return {@code true} if the given object is assigned.
* @since 0.7
*/
protected boolean isLocallyAssigned(EObject target, EObject containerToFindUsage) {
if (this.readAndWriteTracking.isAssigned(target)) {
return true;
}
final Collection<Setting> usages = XbaseUsageCrossReferencer.find(target, containerToFindUsage);
// field are assigned when they are not used as the left operand of an assignment operator.
for (final Setting usage : usages) {
final EObject object = usage.getEObject();
if (object instanceof XAssignment) {
final XAssignment assignment = (XAssignment) object;
if (assignment.getFeature() == target) {
// Mark the field as assigned in order to be faster during the next assignment test.
this.readAndWriteTracking.markAssignmentAccess(target);
return true;
}
}
}
return false;
}

@SuppressWarnings("checkstyle:npathcomplexity")
@Override
protected boolean isLocallyUsed(EObject target, EObject containerToFindUsage) {
Expand All @@ -2639,13 +2635,10 @@ protected boolean isLocallyUsed(EObject target, EObject containerToFindUsage) {
return true;
}
final Collection<Setting> usages = XbaseUsageCrossReferencer.find(target, containerToFindUsage);
// field are used when they are not used as the left operand of an assignment operator.
if (target instanceof JvmField) {
// field and local variables are used when they are not used as the left operand of an assignment operator.
if (target instanceof XVariableDeclaration || target instanceof JvmField) {
for (final Setting usage : usages) {
EObject object = usage.getEObject();
while (object instanceof XMemberFeatureCall) {
object = ((XMemberFeatureCall) object).eContainer();
}
final EObject object = usage.getEObject();
if (object instanceof XAssignment) {
final XAssignment assignment = (XAssignment) object;
if (assignment.getFeature() != target) {
Expand Down
Expand Up @@ -33,14 +33,15 @@
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
* @see "https://github.com/sarl/sarl/issues/208"
* @see "https://github.com/sarl/sarl/issues/810"
*/
@RunWith(Suite.class)
@SuiteClasses({
Bug208.WithProblem.class,
Bug208.WithoutProblem.class,
Bug208And810.WithProblem.class,
Bug208And810.WithoutProblem.class,
})
@SuppressWarnings("all")
public class Bug208 {
public class Bug208And810 {

public static class WithProblem extends AbstractSarlTest {

Expand Down
Expand Up @@ -421,7 +421,9 @@ public void copyInLocalVariable_02() throws Exception {
" x = 1",
" }",
"}"));
validate(mas).assertNoIssues();
validate(mas).assertWarning(
XbasePackage.eINSTANCE.getXVariableDeclaration(),
org.eclipse.xtext.xbase.validation.IssueCodes.UNUSED_LOCAL_VARIABLE);
}

@Test
Expand Down
Expand Up @@ -16,19 +16,23 @@
package io.sarl.lang.tests.bugs.to00699;

import com.google.inject.Inject;
import org.eclipse.xtext.xbase.XbasePackage;
import org.eclipse.xtext.xbase.testing.CompilationTestHelper;
import org.junit.Test;

import io.sarl.lang.SARLVersion;
import io.sarl.lang.sarl.SarlPackage;
import io.sarl.lang.sarl.SarlScript;
import io.sarl.lang.validation.IssueCodes;
import io.sarl.tests.api.AbstractSarlTest;

/**
/** Text for issue 505: Reference instance methods or fields in abstract class.
*
* @author $Author: sgalland$
* @version $Name$ $Revision$ $Date$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
* @see "https://github.com/sarl/sarl/issues/505"
*/
@SuppressWarnings("all")
public class Bug505 extends AbstractSarlTest {
Expand Down Expand Up @@ -94,7 +98,7 @@ public class Bug505 extends AbstractSarlTest {
@Test
public void snipset1() throws Exception {
SarlScript mas = file(snippet1);
validate(mas).assertNoIssues();
validate(mas).assertNoErrors();
}

@Test
Expand Down Expand Up @@ -288,7 +292,9 @@ public void snipset4Compilation() throws Exception {
@Test
public void snipset5() throws Exception {
SarlScript mas = file(snippet5);
validate(mas).assertNoIssues();
validate(mas).assertWarning(
XbasePackage.eINSTANCE.getXAssignment(),
IssueCodes.POTENTIAL_FIELD_SYNCHRONIZATION_PROBLEM);
}

@Test
Expand Down

0 comments on commit f4708c5

Please sign in to comment.