How to make a CDT C parser include preprocessor instructions in AST?

When I parse a C file using the CDT parser, I get an AST that does not contain preprocessor instructions. That is, when I run TestASTVisitor

in AST, it doesn't find any IASTPreprocessorStatement

s.

The preprocessor operators are returned by the method ast.getAllPreprocessorStatements()

.

How can I get the preprocessor instructions to appear in the AST?

My ultimate goal is to create a piece of software that

  • converts the file to some C-like language in AST and
  • then he can use this AST as a template for generating pieces of code.

    @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");
        }
    }
    }
    
          

+3


source to share





All Articles