2017-07-14 116 views
1

我需要使用getAST()方法从源文件中获取AST。我创建了一个.cproject,因为我认为它是一个缺失的,但它仍然犯了同样的错误。在这部分代码:我无法从c源代码使用cdt plugin eclipse获取AST

ITranslationUnit tu = (ITranslationUnit) CoreModel.getDefault(). Create (iFile); 

是返回null的恩,我认为错误是因为这一点,但我不明白为什么它返回NULL,如果我用一个.cproject CDT项目测试。我也不明白IFile,因为它返回了我不知道的路径,如下所示:“L/Users/raiza arteman/runtime -EclipseApplication/testeAST/src/code.c”。下面是类:

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文件放入项目根目录的建议。

回答

0

手动创建.cproject文件不是要走的路 - 它的格式没有记录,并且被视为实现细节。您应该让Eclipse创建该文件以及任何其他项目元数据文件,作为创建实际项目的一部分。

真实项目的创建可以手动在Eclipse UI(File | New | C++ Project)来完成,也可以使用API​​,如IWorkspaceRoot.getProject()IProject.create()CoreModel.create(IProject)它自动化。