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
Branches
No related tags found
No related merge requests found
......@@ -17,7 +17,11 @@ aspect Shadowing {
}
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 {
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);
eq ScopeTree.getElement().shadower(Declaration shadowed) {
inh Declaration Element.shadowed(Declaration shadower);
eq ScopeTree.getElement().shadowed(Declaration shadower) {
for (Declaration declaration : declarations()) {
if (declaration != shadowed && declaration.getName().equals(shadowed.getName())) {
if (declaration != shadower && declaration.getName().equals(shadower.getName())) {
return declaration;
}
}
return null;
}
eq Scope.getElement().shadower(Declaration shadowed) {
eq Scope.getElement().shadowed(Declaration shadower) {
for (Declaration declaration : declarations()) {
if (declaration != shadowed && declaration.getName().equals(shadowed.getName())) {
if (declaration != shadower && declaration.getName().equals(shadower.getName())) {
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() {
int result = 1;
for (Element element : getElementList()) {
......@@ -35,6 +42,7 @@ aspect Shadowing {
return result;
}
/** count all declarations in this subtree */
syn int Scope.numDeclarations() {
int result = 0;
for (Element element : getElementList()) {
......@@ -46,5 +54,10 @@ aspect Shadowing {
}
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 {
{
int localVarInBlockA = 2;
// this is shadowed (and forbidden)
// this is shadowing (and forbidden)
int localVarA = 3;
}
......@@ -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;
try (
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment