diff --git a/src/DeadCode.rsc b/src/DeadCode.rsc new file mode 100644 index 0000000..01008ae --- /dev/null +++ b/src/DeadCode.rsc @@ -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)` `: { + res = (left == y); + } + + case (Assignment)` `:{ + res = checkMethodArgs(m, left); + } + } + return res; +} + +bool checkMethodArgs(MethodInvocation m, Identifier left){ + bool res = false; + top-down-break visit(m){ + case (MethodInvocation)`()`: { + res = (arg == left); + } + } + return res; +} + + +CompilationUnit removeDeadAssignment(CompilationUnit unit) = visit(unit){ + + case (Assignment) ` ` => + (Assignment) ` eliminado` when !checkExpUse(leftId, unit) + + case (PostfixExpression) `` => + (PostfixExpression) `eliminado` when !checkExpUse(pExp, unit) + +}; + diff --git a/testes/DeadCodeExamples.java b/testes/DeadCodeExamples.java new file mode 100644 index 0000000..0a17296 --- /dev/null +++ b/testes/DeadCodeExamples.java @@ -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++; + + } +} \ No newline at end of file