roterdam / crystalsaf

Automatically exported from code.google.com/p/crystalsaf
0 stars 0 forks source link

Can't get the Variable for a SingleVariableDeclaration #15

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Steps:
In a transfer function's createEntryValue method, put the following code in:

for (int ndx = 0; ndx < method.parameters().size(); ndx++) {
   SingleVariableDeclaration decl = (SingleVariableDeclaration)
method.parameters().get(ndx);
   Variable paramVar = getAnalysisContext().getVariable(decl);
}

Example is in TutorialExamples project,
edu.cmu.cs.crystal.analysis.npe.annotations.NPEAnnotatedTransferFunction

Expected:
paramVar will be the right paramVar for this decl.

Result:
Eclipse throws an exception.

Debug info:
I traced this to the problem, just not sure what the fix should be. It does
create a TACInstruction for SingleVariableDeclaration. However, then we run
into this code in EclipseTAC.variable()

TACInstruction result = instruction(astNode);
if(result != null) {
   if(result instanceof ResultfulInstruction)
      return ((ResultfulInstruction<?>) result).getResultVariable();
   throw new IllegalArgumentException("AST node has no result: " + astNode);
}

result has type SourceVariableDeclarationImpl, which is apparently not a
ResultfulInstruction? I assume it should be, but maybe this is used for
things other that method parameters.

Original issue reported on code.google.com by ciera.christopher on 9 Jun 2009 at 1:43

GoogleCodeExporter commented 9 years ago
Try using getSourceVariable() and see if that works.  (You'll have to get the 
binding 
from the declaration.)

getVariable() is not really meant to be used in this way: it returns the 
variable 
representing the result of evaluating the given (expression) node.  A 
declaration has 
no result and is not an expression, hence getVariable() doesn't work.  (We may 
be 
able to make it work, or at least document it more clearly.  getVariable() 
doesn't 
just take an Expression AST node because it works for some things that are not 
expressions according to the Eclipse AST, notably array stuffs I believe.)

The correct way to extend getVariable() to work with declarations is probably 
to add 
two lines into EclipseTAC.variable:

TACInstruction result = instruction(astNode);
if(result != null) {
    if(result instanceof ResultfulInstruction)
        return ((ResultfulInstruction<?>) result).getResultVariable();
    if(result instanceof SourceVariableDeclaration)
        return ((SourceVariableDeclaration) result).getDeclaredVariable();
    throw ...
}

Original comment by kevin.bi...@gmail.com on 9 Jun 2009 at 3:54

GoogleCodeExporter commented 9 years ago
Using getSourceVariable fixed the problem. Thanks! I'll add documentation to
getVariable for now.

Original comment by ciera.christopher on 9 Jun 2009 at 4:36