I need to get an AST from the source file using the getAST () method. I created a .cproject because I thought if it was the one that was missing, but it still made the same mistake. In this part of the code:
ITranslationUnit tu = (ITranslationUnit) CoreModel.getDefault (). Create (iFile);
Is returning Null in tu, I believe the error is because of this, but I do not understand why it is returning Null if I am testing with a CDT project with .cproject. I also do not understand IFile, because it is returning a path that I do not know, like this: "L/Users/raiza arteman/runtime-EclipseApplication/testeAST/src/code.c". Below is the class:
public void analyzeFilesInSrc() throws Exception{
ICProject project = CoreModel.getDefault().getCModel().getCProject(SampleHandler.PROJECT);//project to analyse
IIndex index = CCorePlugin.getIndexManager().getIndex(project);
System.out.println("iindex "+index);
// It gets all C files from the SRC path to analyze.
this.filesInSrc = new ArrayList<String>();
this.setSrcFiles(SampleHandler.RUNTIME_WORKSPACE_PATH + SampleHandler.PROJECT + File.separator + "src");
// For each C file in the SRC folder..
for (String file : this.filesInSrc){
String completeFilePath = file.replace(SampleHandler.RUNTIME_WORKSPACE_PATH, "");
System.out.println(completeFilePath.replace(PROJECT + File.separator + "src" + File.separator, ""));
this.editDirectives(file);
IPath iFilePath = new Path(completeFilePath);
IFile iFile = ResourcesPlugin.getWorkspace().getRoot().getFile(iFilePath);
System.out.println("ifile "+iFile);
System.out.println("Itranslatiotounit "+(ITranslationUnit) CoreModel.getDefault().create(iFile));
ITranslationUnit tu = (ITranslationUnit) CoreModel.getDefault().create(iFile);
System.out.println("tu "+tu);
try {
// We need a read-lock on the index.
index.acquireReadLock();
IASTTranslationUnit ast= tu.getAST(index, ITranslationUnit.AST_PARSE_INACTIVE_CODE);
this.setTypes(ast);
this.setMacros(ast);
}
is happening this:
java.lang.NullPointerException
at analysis.handlers.SampleHandler.analyzeFilesInSrc(SampleHandler.java:249)
at analysis.handlers.SampleHandler.execute(SampleHandler.java:103)
at org.eclipse.ui.internal.handlers.HandlerProxy.execute(HandlerProxy.java:295)
at org.eclipse.ui.internal.handlers.E4HandlerProxy.execute(E4HandlerProxy.java:90)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
After I can use this in a CDT project, I will need to parse any c code, even if it is not from the CDT, for this I have seen a suggestion here in the stack of putting a .cproject file in the root in the project.
Manually creating a .cproject
file is not the way to go - its format is undocumented and considered an implementation detail. You should let Eclipse create that file, and any other project metadata files, as part of creating a real project.
The creation of the real project can be done manually in the Eclipse UI (File | New | C++ Project
) or you can automate it using APIs like IWorkspaceRoot.getProject()
, IProject.create()
, and CoreModel.create(IProject)
.