Skip to content
Snippets Groups Projects
Commit aadf220e authored by Johannes Mey's avatar Johannes Mey
Browse files

add subclasses into the scope of the correct visibility of the superclass, adapt tests accordingly

parent bb2b83c5
No related branches found
No related tags found
No related merge requests found
......@@ -6,7 +6,11 @@ aspect ProgramToScopeTree {
for (CompilationUnit compilationUnit : getCompilationUnitList())
for (TypeDecl typeDecl : compilationUnit.getTypeDeclList()) {
if (typeDecl.isClassDecl()) {
// do not add the TypeDecl, if it has a superclass from source
ClassDecl classDecl = (ClassDecl)typeDecl;
if (!classDecl.superclass().compilationUnit().fromSource()) {
tree.addElement(((ClassDecl)typeDecl).publicScope());
}
} else {
tree.addElement(typeDecl.scope());
}
......@@ -23,7 +27,15 @@ aspect ProgramToScopeTree {
// collect all scopes
TypeDecl contributes scope() when !isClassDecl() to ASTNode.scope() for containingScope();
ClassDecl contributes publicScope() to ASTNode.scope() for containingScope();
ClassDecl contributes publicScope() when isInnerClass() to ASTNode.scope() for containingScope();
ClassDecl contributes publicScope()
when superclass().compilationUnit().fromSource() && !isInnerClass() && accessibleFromPackage(containingScope().hostPackage())
to ClassDecl.packageScope()
for containingScope();
ClassDecl contributes publicScope()
when superclass().compilationUnit().fromSource() && !isInnerClass() && !accessibleFromPackage(containingScope().hostPackage())
to ClassDecl.protectedScope()
for containingScope();
Block contributes scope() to ASTNode.scope() for containingScope();
ForStmt contributes scope() when !(getStmt() instanceof Block) to ASTNode.scope() for containingScope();
EnhancedForStmt contributes scope() when !(getStmt() instanceof Block) to ASTNode.scope() for containingScope();
......@@ -128,4 +140,16 @@ aspect ScopeGenerationAttributes {
eq EnhancedForStmt.getVariableDecl().containingScope() = (getStmt() instanceof Block) ? getStmt() : this;
eq ForStmt.getInitStmt().containingScope() = (getStmt() instanceof Block) ? getStmt() : this;
eq ForStmt.getUpdateStmt().containingScope() = (getStmt() instanceof Block) ? getStmt() : this;
// belonging to visibility scope
eq CompilationUnit.getTypeDecl(int i).containingScope() {
if (getTypeDecl(i).isClassDecl()) {
TypeDecl superclass = ((ClassDecl)getTypeDecl(i)).superclass();
return superclass.compilationUnit().fromSource() ? superclass : this;
}
return this;
}
// allow host package to be called from all AST nodes
inh String ASTNode.hostPackage();
eq Program.getCompilationUnit(int i).hostPackage() = getCompilationUnit(i).getPackageDecl();
}
......@@ -14,14 +14,6 @@ public class FieldsTest extends ScopeAnalysisTest {
ScopeAnalysis scopeAnalysis = new ScopeAnalysis();
Set<AbstractFinding> findings = scopeAnalysis.analyze("src/test/resources/fields", true, false);
// assertShadow(findings, "fieldC", 19, 3);
// assertShadow(findings, "fieldB", 21, 4);
// assertRedefinition(findings, "fieldB", 2);
// assertRedefinition(findings, "fieldB", 4);
// Assertions.assertEquals(4, findings.size());
}
}
......@@ -13,15 +13,14 @@ public class SuperclassFieldsTest extends ScopeAnalysisTest {
void test() {
ScopeAnalysis scopeAnalysis = new ScopeAnalysis();
Set<AbstractFinding> findings = scopeAnalysis.analyze("src/test/resources/superclassFields", true, false);
Set<AbstractFinding> findings = scopeAnalysis.analyze("src/test/resources/superclassFields", false, false);
assertShadow(findings, "fieldC", 19, 3);
assertShadow(findings, "fieldB", 21, 4);
assertRedefinition(findings, "fieldB", 2);
assertRedefinition(findings, "fieldB", 4);
assertShadow(findings, "fieldB", 21, 2);
assertShadow(findings, "fieldB", 2, 4);
Assertions.assertEquals(4, findings.size());
Assertions.assertEquals(3, findings.size());
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment