Skip to content

Commit 70a2fa6

Browse files
authored
Merge pull request #13 from sinha108/var-decl-info
Information about variable declarations in callables
2 parents 6b45091 + df69040 commit 70a2fa6

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

src/main/java/com/ibm/northstar/SymbolTable.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ private static Pair<String, Callable> processCallableDeclaration(CallableDeclara
301301
callableNode.setCalledMethodDeclaringTypes(getCalledMethodDeclaringTypes(body));
302302
callableNode.setAccessedFields(getAccessedFields(body, classFields, typeName));
303303
callableNode.setCallSites(getCallSites(body));
304+
callableNode.setVariableDeclarations(getVariableDeclarations(body));
304305

305306
String callableSignature = (callableDecl instanceof MethodDeclaration) ? callableDecl.getSignature().asString() : callableDecl.getSignature().asString().replace(callableDecl.getSignature().getName(), "<init>");
306307
return Pair.of(callableSignature, callableNode);
@@ -359,7 +360,41 @@ private static List<String> getReferencedTypes(Optional<BlockStmt> blockStmt) {
359360
}
360361

361362
/**
362-
* Computes and returns the list if fields accessed in the given callable body. The returned values contain
363+
* Returns information about variable declarations in the given callable. The information includes
364+
* var name, var type, var initializer, and position.
365+
*
366+
* @param blockStmt Callable to compute var declaration information for
367+
* @return list of variable declarations
368+
*/
369+
private static List<VariableDeclaration> getVariableDeclarations(Optional<BlockStmt> blockStmt) {
370+
List<VariableDeclaration> varDeclarations = new ArrayList<>();
371+
if (blockStmt.isEmpty()) {
372+
return varDeclarations;
373+
}
374+
for (VariableDeclarator declarator : blockStmt.get().findAll(VariableDeclarator.class)) {
375+
VariableDeclaration varDeclaration = new VariableDeclaration();
376+
varDeclaration.setName(declarator.getNameAsString());
377+
varDeclaration.setType(resolveType(declarator.getType()));
378+
varDeclaration.setInitializer(declarator.getInitializer().isPresent() ?
379+
declarator.getInitializer().get().toString() : "");
380+
if (declarator.getRange().isPresent()) {
381+
varDeclaration.setStartLine(declarator.getRange().get().begin.line);
382+
varDeclaration.setStartColumn(declarator.getRange().get().begin.column);
383+
varDeclaration.setEndLine(declarator.getRange().get().end.line);
384+
varDeclaration.setEndColumn(declarator.getRange().get().end.column);
385+
} else {
386+
varDeclaration.setStartLine(-1);
387+
varDeclaration.setStartColumn(-1);
388+
varDeclaration.setEndLine(-1);
389+
varDeclaration.setEndColumn(-1);
390+
}
391+
varDeclarations.add(varDeclaration);
392+
}
393+
return varDeclarations;
394+
}
395+
396+
/**
397+
* Computes and returns the list of fields accessed in the given callable body. The returned values contain
363398
* field names qualified by names of the declaring types.
364399
*
365400
* @param callableBody Callable body to compute accessed fields for

src/main/java/com/ibm/northstar/entities/Callable.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ public class Callable {
2323
private List<String> accessedFields;
2424
private List<String> calledMethodDeclaringTypes;
2525
private List<CallSite> callSites;
26+
private List<VariableDeclaration> variableDeclarations;
2627
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.ibm.northstar.entities;
2+
3+
import lombok.Data;
4+
5+
import java.util.List;
6+
7+
@Data
8+
public class VariableDeclaration {
9+
private String name;
10+
private String type;
11+
private String initializer;
12+
private int startLine;
13+
private int startColumn;
14+
private int endLine;
15+
private int endColumn;
16+
}

0 commit comments

Comments
 (0)