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

fix wrong naming of shadower/shadowed, add printing with line numbers.

parent c8c03836
No related branches found
No related tags found
No related merge requests found
...@@ -17,7 +17,11 @@ aspect Shadowing { ...@@ -17,7 +17,11 @@ aspect Shadowing {
} }
public String toString() { public String toString() {
return "declaration '" + shadowed.getName() + "' is shadowed by '" + shadower.getName() + "'!"; if (shadower.sourceFile().equals(shadowed.sourceFile())) {
return shadower.sourceFile() + ": declaration '" + shadower + "' (l." + shadower.lineNumber() + ") is shadowing '" + shadowed + "' (l." + shadowed.lineNumber() + ")!";
} else {
return shadower.sourceFile() + ": declaration '" + shadower + "' (l." + shadower.lineNumber() + ") is shadowing '" + shadowed + "(' " + shadowed.sourceLocation() + ")!";
}
} }
} }
} }
aspect Shadowing { aspect Shadowing {
coll Set<VariableShadowFinding> ScopeTree.variableShadowings() [new HashSet<>()] with add root ScopeTree; coll Set<VariableShadowFinding> ScopeTree.variableShadowings() [new HashSet<>()] with add root ScopeTree;
Declaration contributes new VariableShadowFinding(this, shadower()) when isShadowed() to ScopeTree.variableShadowings(); Declaration contributes new VariableShadowFinding(shadowed(), this) when isShadowing() to ScopeTree.variableShadowings();
syn Declaration Declaration.shadower() = shadower(this); syn Declaration Declaration.shadowed() = shadowed(this);
inh Declaration Element.shadower(Declaration shadowed); inh Declaration Element.shadowed(Declaration shadower);
eq ScopeTree.getElement().shadower(Declaration shadowed) { eq ScopeTree.getElement().shadowed(Declaration shadower) {
for (Declaration declaration : declarations()) { for (Declaration declaration : declarations()) {
if (declaration != shadowed && declaration.getName().equals(shadowed.getName())) { if (declaration != shadower && declaration.getName().equals(shadower.getName())) {
return declaration; return declaration;
} }
} }
return null; return null;
} }
eq Scope.getElement().shadower(Declaration shadowed) { eq Scope.getElement().shadowed(Declaration shadower) {
for (Declaration declaration : declarations()) { for (Declaration declaration : declarations()) {
if (declaration != shadowed && declaration.getName().equals(shadowed.getName())) { if (declaration != shadower && declaration.getName().equals(shadower.getName())) {
return declaration; return declaration;
} }
} }
return shadower(shadowed); return shadowed(shadower);
} }
syn boolean Declaration.isShadowed() = shadower() != null; syn boolean Declaration.isShadowing() = shadowed() != null;
}
aspect Statictics {
/** count all scopes in this subtree */
syn int Scope.numScopes() { syn int Scope.numScopes() {
int result = 1; int result = 1;
for (Element element : getElementList()) { for (Element element : getElementList()) {
...@@ -35,6 +42,7 @@ aspect Shadowing { ...@@ -35,6 +42,7 @@ aspect Shadowing {
return result; return result;
} }
/** count all declarations in this subtree */
syn int Scope.numDeclarations() { syn int Scope.numDeclarations() {
int result = 0; int result = 0;
for (Element element : getElementList()) { for (Element element : getElementList()) {
...@@ -46,5 +54,10 @@ aspect Shadowing { ...@@ -46,5 +54,10 @@ aspect Shadowing {
} }
return result; return result;
} }
}
aspect Printing {
syn String Element.toString();
eq Scope.toString() = "Scope";
eq Declaration.toString() = getName();
} }
aspect Shadow{
eq TypeDeclScope.toString() = getTypeDecl().getClass().getSimpleName() + "-Scope";
eq BlockScope.toString() = getBlock().getClass().getSimpleName() + "-Scope";
eq ForStmtScope.toString() = getForStmt().getClass().getSimpleName() + "-Scope";
eq EnhancedForStmtScope.toString() = getEnhancedForStmt().getClass().getSimpleName() + "-Scope";
eq JavaDeclaration.toString() = getDeclarator().getClass().getSimpleName() + ":" + super.toString();
eq JavaParameterDeclaration.toString() = getParameterDeclaration().getClass().getSimpleName() + ":" + super.toString();
eq JavaInferredLambdaParameterDeclaration.toString() = getInferredParameterDeclaration().getClass().getSimpleName() + ":" + super.toString();
public int TypeDeclScope.lineNumber(){
return getTypeDecl().lineNumber();
}
public int BlockScope.lineNumber(){
return getBlock().lineNumber();
}
public int ForStmtScope.lineNumber(){
return getForStmt().lineNumber();
}
public int EnhancedForStmtScope.lineNumber(){
return getEnhancedForStmt().lineNumber();
}
public int JavaDeclaration.lineNumber() {
return getDeclarator().lineNumber();
}
public int JavaParameterDeclaration.lineNumber() {
return getParameterDeclaration().lineNumber();
}
public int JavaInferredLambdaParameterDeclaration.lineNumber() {
return getInferredParameterDeclaration().lineNumber();
}
public String TypeDeclScope.sourceFile() {
return getTypeDecl().sourceFile();
}
public String BlockScope.sourceFile() {
return getBlock().sourceFile();
}
public String ForStmtScope.sourceFile() {
return getForStmt().sourceFile();
}
public String EnhancedForStmtScope.sourceFile() {
return getEnhancedForStmt().sourceFile();
}
public String JavaDeclaration.sourceFile() {
return getDeclarator().sourceFile();
}
public String JavaParameterDeclaration.sourceFile() {
return getParameterDeclaration().sourceFile();
}
public String JavaInferredLambdaParameterDeclaration.sourceFile() {
return getInferredParameterDeclaration().sourceFile();
}
}
aspect Shadow {
}
...@@ -17,7 +17,7 @@ public abstract class ClassA { ...@@ -17,7 +17,7 @@ public abstract class ClassA {
{ {
int localVarInBlockA = 2; int localVarInBlockA = 2;
// this is shadowed (and forbidden) // this is shadowing (and forbidden)
int localVarA = 3; int localVarA = 3;
} }
...@@ -27,7 +27,7 @@ public abstract class ClassA { ...@@ -27,7 +27,7 @@ public abstract class ClassA {
} }
} }
// this is shadowed (over two levels, not forbidden) // this is shadowing (over two levels, not forbidden)
int fieldA; int fieldA;
try ( try (
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment