Skip to main content



      Home
Home » Language IDEs » C / C++ IDE (CDT) » Call relation among functions/methods
Call relation among functions/methods [message #124996] Mon, 25 October 2004 10:23
Eclipse UserFriend
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();
}

}

------------------------------------------------------------ ------------------
Previous Topic:Library search path
Next Topic:Call relation among functions/methods
Goto Forum:
  


Current Time: Mon Jun 02 21:51:42 EDT 2025

Powered by FUDForum. Page generated in 0.03880 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top