调用EclipseCDT解析器的结果实现对C++代码的解析

调用EclipseCDT解析器的结果实现对C++代码的解析,第1张

调用EclipseCDT解析器的结果实现对C++代码的解析,第2张

org.eclipse.cdt.core的插件包引入后,可以通过下面的代码
public static last translation unit getast(ifile file){
itranslationunit tu = coremodelutil . find translation unit(file)得到一个C++源文件的解析结果;
ICProject project = tu . getc project();
IASTTranslationUnit ast = null;
try {
IIndex index = ccorplugin . getindexmanager()。getIndex(project,
IIndexManager。ADD_DEPENDENCIES | IIndexManager。ADD _ DEPENDENT);
index . acquired lock();
try {
ast = tu . getast(index,0);
} finally {
index . releaseredlock();
}
} catch(interrupted exception e){
//log(e);
} catch(core exception e){
//log(e);
}
return ast;
}
然后调用IASTTranslatonUnit ast上的getDeclarations()方法来获取所有声明信息。CCorePlugin和CoreModelUtil是CDT Core提供的最重要的条目。
translator unit表示一个编译单元。可以是一个项目。一份文件。文件的代码块ASTTranslatonUnit是编译单元的AST表示形式。它包含了所有的信息。

位律师回复
DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
白度搜_经验知识百科全书 » 调用EclipseCDT解析器的结果实现对C++代码的解析

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情