Skip to content

Análise estática de Código #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/DeadCode.rsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module DeadCode
import lang::java::\syntax::Java18;
import ParseTree;
import IO;
import Set;

/*
Deteccao e remocao de atribuicoes que não usadas.
Base: Live variables do livro Principles of Progam Analysis

*/

bool checkExpUse(Identifier left, CompilationUnit unit) {
bool res = false;
top-down-break visit(unit) {
case (Assignment)`<LeftHandSide dst> <AssignmentOperator op> <Identifier y>`: {
res = (left == y);
}

case (Assignment)`<LeftHandSide dst> <AssignmentOperator op> <MethodInvocation m>`:{
res = checkMethodArgs(m, left);
}
}
return res;
}

bool checkMethodArgs(MethodInvocation m, Identifier left){
bool res = false;
top-down-break visit(m){
case (MethodInvocation)`<MethodName mName>(<Identifier arg>)`: {
res = (arg == left);
}
}
return res;
}


CompilationUnit removeDeadAssignment(CompilationUnit unit) = visit(unit){

case (Assignment) `<Identifier leftId> <AssignmentOperator s> <Expression rightExp>` =>
(Assignment) `<Identifier leftId> <AssignmentOperator s> eliminado` when !checkExpUse(leftId, unit)

case (PostfixExpression) `<Identifier pExp>` =>
(PostfixExpression) `eliminado` when !checkExpUse(pExp, unit)

};

41 changes: 41 additions & 0 deletions testes/DeadCodeExamples.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class DeadCodeExamples {
int teste(int var)
{
return var;
}

void casos()
{
int x,y,z;

//Teste 1 - eliminando atribuicoes
/*
y = 4;
x = 2;
z = 4;
*/

//Teste 2 - preservando alguma coisa
/*
y = 4;
x = 2;
z = y;
*/

//Teste 3 - variavel usada como parametro
/*
y = 4;
x = 2;
z = teste(y);
*/

//Teste 4 - PostfixExpression

y = 4;
x = 2;
z = y;
y++;
x++;

}
}