我无法使用 cdt 插件 eclipse 从 c 源代码获取 AST
I can't get a AST from c source code using cdt plugin eclipse
我需要使用 getAST () 方法从源文件中获取 AST。我创建了一个 .cproject 因为我想如果它是丢失的那个,但它仍然犯了同样的错误。这部分代码中:
ITranslationUnit tu = (ITranslationUnit) CoreModel.getDefault (). Create (iFile);
在 tu 中返回 Null,我相信错误是因为这个,但我不明白为什么如果我正在测试带有 .cproject 的 CDT 项目,它会返回 Null。我也不明白 IFile,因为它返回一个我不知道的路径,像这样:"L/Users/raiza arteman/runtime-EclipseApplication/testeAST/src/code.c"。下面是 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);
}
正在发生这样的事情:
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)
在我可以在 CDT 项目中使用它之后,我将需要解析任何 c 代码,即使它不是来自 CDT,为此我在此处看到了建议将 .cproject 文件放在项目的根目录中的堆栈。
手动创建 .cproject
文件不是可行的方法 - 它的格式未记录并被视为实现细节。作为创建真实项目的一部分,您应该让 Eclipse 创建该文件和任何其他项目元数据文件。
实际项目的创建可以在 Eclipse UI (File | New | C++ Project
) 中手动完成,或者您可以使用 IWorkspaceRoot.getProject()
, IProject.create()
, and CoreModel.create(IProject)
.
等 API 自动完成
我需要使用 getAST () 方法从源文件中获取 AST。我创建了一个 .cproject 因为我想如果它是丢失的那个,但它仍然犯了同样的错误。这部分代码中:
ITranslationUnit tu = (ITranslationUnit) CoreModel.getDefault (). Create (iFile);
在 tu 中返回 Null,我相信错误是因为这个,但我不明白为什么如果我正在测试带有 .cproject 的 CDT 项目,它会返回 Null。我也不明白 IFile,因为它返回一个我不知道的路径,像这样:"L/Users/raiza arteman/runtime-EclipseApplication/testeAST/src/code.c"。下面是 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);
}
正在发生这样的事情:
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)
在我可以在 CDT 项目中使用它之后,我将需要解析任何 c 代码,即使它不是来自 CDT,为此我在此处看到了建议将 .cproject 文件放在项目的根目录中的堆栈。
手动创建 .cproject
文件不是可行的方法 - 它的格式未记录并被视为实现细节。作为创建真实项目的一部分,您应该让 Eclipse 创建该文件和任何其他项目元数据文件。
实际项目的创建可以在 Eclipse UI (File | New | C++ Project
) 中手动完成,或者您可以使用 IWorkspaceRoot.getProject()
, IProject.create()
, and CoreModel.create(IProject)
.