当我使用CDT的解析器解析C文件时,我得到一个AST,它不包含预处理器语句。也就是说,当我在AST上运行TestASTVisitor
时,它找不到任何IASTPreprocessorStatement
。
预处理程序语句由方法ast.getAllPreprocessorStatements()
返回。
如何让预处理器语句出现在AST中?
我的最终目标是创建一个软件,
然后可以使用该AST作为模板来生成代码部分。
@Test
public void test3() throws CoreException, IOException
{
final IASTTranslationUnit ast = createAst();
final TestASTVisitor visitor = new TestASTVisitor();
ast.accept(visitor);
for (final IASTNode curNode : ast.getChildren())
{
curNode.accept(visitor);
}
}
public class TestASTVisitor extends ASTVisitor {
public TestASTVisitor() {
super(true);
this.shouldVisitAmbiguousNodes = true;
this.includeInactiveNodes = true;
this.shouldVisitImplicitNames = true;
this.shouldVisitTokens = true;
}
@Override
public int visit(IASTTranslationUnit tu) {
printMessageIfPreprocessorStatement(tu);
return super.visit(tu);
}
@Override
public int visit(IASTName name) {
printMessageIfPreprocessorStatement(name);
return super.visit(name);
}
@Override
public int visit(IASTDeclaration declaration) {
printMessageIfPreprocessorStatement(declaration);
return super.visit(declaration);
}
@Override
public int visit(IASTInitializer initializer) {
printMessageIfPreprocessorStatement(initializer);
return super.visit(initializer);
}
@Override
public int visit(IASTParameterDeclaration parameterDeclaration) {
printMessageIfPreprocessorStatement(parameterDeclaration);
return super.visit(parameterDeclaration);
}
@Override
public int visit(IASTDeclarator declarator) {
printMessageIfPreprocessorStatement(declarator);
return super.visit(declarator);
}
@Override
public int visit(IASTDeclSpecifier declSpec) {
printMessageIfPreprocessorStatement(declSpec);
return super.visit(declSpec);
}
@Override
public int visit(IASTArrayModifier arrayModifier) {
printMessageIfPreprocessorStatement(arrayModifier);
return super.visit(arrayModifier);
}
@Override
public int visit(IASTPointerOperator ptrOperator) {
printMessageIfPreprocessorStatement(ptrOperator);
return super.visit(ptrOperator);
}
@Override
public int visit(IASTAttribute attribute) {
printMessageIfPreprocessorStatement(attribute);
return super.visit(attribute);
}
@Override
public int visit(IASTAttributeSpecifier specifier) {
printMessageIfPreprocessorStatement(specifier);
return super.visit(specifier);
}
@Override
public int visit(IASTToken token) {
printMessageIfPreprocessorStatement(token);
return super.visit(token);
}
@Override
public int visit(IASTExpression expression) {
printMessageIfPreprocessorStatement(expression);
return super.visit(expression);
}
@Override
public int visit(IASTStatement statement) {
printMessageIfPreprocessorStatement(statement);
return super.visit(statement);
}
@Override
public int visit(IASTTypeId typeId) {
printMessageIfPreprocessorStatement(typeId);
return super.visit(typeId);
}
@Override
public int visit(IASTEnumerator enumerator) {
printMessageIfPreprocessorStatement(enumerator);
return super.visit(enumerator);
}
@Override
public int visit(IASTProblem problem) {
printMessageIfPreprocessorStatement(problem);
return super.visit(problem);
}
@Override
public int visit(ICPPASTBaseSpecifier baseSpecifier) {
printMessageIfPreprocessorStatement(baseSpecifier);
return super.visit(baseSpecifier);
}
@Override
public int visit(ICPPASTNamespaceDefinition namespaceDefinition) {
printMessageIfPreprocessorStatement(namespaceDefinition);
return super.visit(namespaceDefinition);
}
@Override
public int visit(ICPPASTTemplateParameter templateParameter) {
printMessageIfPreprocessorStatement(templateParameter);
return super.visit(templateParameter);
}
@Override
public int visit(ICPPASTCapture capture) {
printMessageIfPreprocessorStatement(capture);
return super.visit(capture);
}
@Override
public int visit(ICASTDesignator designator) {
printMessageIfPreprocessorStatement(designator);
return super.visit(designator);
}
@Override
public int visit(ICPPASTVirtSpecifier virtSpecifier) {
printMessageIfPreprocessorStatement(virtSpecifier);
return super.visit(virtSpecifier);
}
@Override
public int visit(ICPPASTClassVirtSpecifier classVirtSpecifier) {
printMessageIfPreprocessorStatement(classVirtSpecifier);
return super.visit(classVirtSpecifier);
}
@Override
public int visit(ICPPASTDecltypeSpecifier decltypeSpecifier) {
printMessageIfPreprocessorStatement(decltypeSpecifier);
return super.visit(decltypeSpecifier);
}
@Override
public int visit(IASTComment comment) {
printMessageIfPreprocessorStatement(comment);
return super.visit(comment);
}
@Override
public int visit(ASTAmbiguousNode astAmbiguousNode) {
printMessageIfPreprocessorStatement(astAmbiguousNode);
return super.visit(astAmbiguousNode);
}
private void printMessageIfPreprocessorStatement(Object node)
{
if (node instanceof IASTPreprocessorStatement)
{
System.out.println("Found preprocessor statement");
}
}
}