Call relation among functions/methods [message #124996] |
Mon, 25 October 2004 14:23 |
Dharma Messages: 7 Registered: July 2009 |
Junior Member |
|
|
Hi,
I'm trying to extract call relation among functions using CDT Parser. I
wrote a class that inherits StructuralParseCallback and overrides
acceptFunctionReference, enterFunctionBody and exitFunctionBody. When a
function is entered, its name is stored and all function references within
the body are functions called by currently entered function body. But,
this does not work for my small piece of C code. Can you please tell me
whether it is a problem with Eclipse CDT or my Java code that queries the
parser ?.
I commented the call graph problem within the C code itself. You can
diretly exectue "TestCallGraph", a java code attached below to see the
call graph for a given C program.
Thanks for your time.
Dharma
------------------------------------------------------------ -----------------
#include <string.h>
typedef char ECHAR;
void myFunction()
{
ECHAR * str = (ECHAR *)"X";
ECHAR * str2 = (ECHAR *)"XX";
char * cStr = "Y";
/* myFunction calling strlen - This works correctly */
/* I can see "call myFunction strlen" */
strlen(str);
/* myFunction calling strlen using ECHAR as argument is completely
missing - NOT Working*/
/* I can NOT see "call myFunction strlen" */
if(strlen(str) + strlen(str2) <= 3) { };
/* myFunction calling strlen using cStr - This works correctly */
/* I can see "call myFunction strlen */
//if (strlen(cStr) + strlen(cStr)) { };
}
------------------------------------------------------------ ------------------
Java code that access parser:
import java.io.File;
import java.util.LinkedHashMap;
import java.util.Map;
import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.IProblem;
import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.cdt.core.parser.ParserFactory;
import org.eclipse.cdt.core.parser.ParserLanguage;
import org.eclipse.cdt.core.parser.ParserMode;
import org.eclipse.cdt.core.parser.ScannerInfo;
import org.eclipse.cdt.core.parser.ast.IASTFunction;
import org.eclipse.cdt.core.parser.ast.IASTFunctionReference;
import org.eclipse.cdt.internal.core.parser.InternalParserUtil;
import org.eclipse.cdt.internal.core.parser.StructuralParseCallback ;
class CMySourceElementRequestor extends StructuralParseCallback {
private String inputFileName = null; // file to be parsed
private String currentlyEnteredFunction = null; // name of function that
is currently entered
public CMySourceElementRequestor(File inputFileName){
this.inputFileName = inputFileName.getAbsolutePath();
}
public void acceptFunctionReference( IASTFunctionReference reference ) {
// function calls other functions
System.out.println("call " + currentlyEnteredFunction + " " +
reference.getName());
}
public void enterFunctionBody(IASTFunction function) {
currentlyEnteredFunction = function.getName();
}
public void exitFunctionBody(IASTFunction function) {
System.out.println("**** Exiting function body: " + function.getName());
function.setHasFunctionBody(true);
}
public boolean parserTimeout() {
return false;
}
public boolean acceptProblem( IProblem problem ) {
System.out.println("Parsing problem: " + problem.getMessage());
System.out.println("Problem at line: " + problem.getSourceLineNumber());
String src = new String(problem.getOriginatingFileName());
System.out.println("Originating problem in file: " + src);
return true;
}
}
public class TestCallGraph {
private final static String[] incPaths = getIncludePaths();
private final static Map definedSymbols = getDefinedSymbols();
private File srcFile; // C file to be parsed
private CMySourceElementRequestor mySourceElementRequestor;
public TestCallGraph(File srcFile) {
this.srcFile = srcFile;
mySourceElementRequestor = new CMySourceElementRequestor(srcFile);
}
private static String[] getIncludePaths() {
String[] incPaths = new String[2];
incPaths[0] = "C:\\cygwin\\usr\\include";
incPaths[1] = "C:\\cygwin\\lib\\gcc-lib\\i686-pc-cygwin\\3.3.1\\include";
return incPaths;
}
private static Map getDefinedSymbols() {
Map m = new LinkedHashMap();
String key = "__IEEE_LITTLE_ENDIAN";
m.put(key, "");
return m;
}
public void printCallGraph() {
IScanner myIScanner;
IParser myIParser;
/* Create a Scanner */
myIScanner = ParserFactory.createScanner(
InternalParserUtil.createFileReader(srcFile.getAbsolutePath( )),
srcFile.getAbsolutePath(), new ScannerInfo(definedSymbols, incPaths),
ParserMode.COMPLETE_PARSE, ParserLanguage.C,
mySourceElementRequestor, null, null);
/* Create a Parser */
myIParser = ParserFactory.createParser(myIScanner,
mySourceElementRequestor, ParserMode.COMPLETE_PARSE,
ParserLanguage.C, null);
if (myIParser.parse() == true) {
System.out.println("Parse successful for file: " +
srcFile.getAbsolutePath());
} else {
System.out.println("Parse unsuccessful for file: " +
srcFile.getAbsolutePath());
}
}
/* Main program */
public static void main(String[] args) {
if(args.length < 1) {
System.out.println("Usage: java TestCallGraph <absolute path to a .c
file>");
}
TestCallGraph callGraph = new TestCallGraph(new File(args[0]));
callGraph.printCallGraph();
}
}
------------------------------------------------------------ ------------------
|
|
|
Powered by
FUDForum. Page generated in 0.03749 seconds