Skip to content

Errors parsing #if/#else statements #4

Closed
@bdemick

Description

@bdemick

I'm attempting to use tree-sitter as the parser for a project, and running into some issues where #if/#elif/#else/#endif directives exist. One of the particular code patterns that seems to throw things off is (ignore the nonsensical logic, this is syntactically correct):

char global_buf[256] = {};

int test_func(int sz, int* dst)
{
    int eReturn;
#if( USE_INSECURE == 1 )
	if( *global_buf != dst )
	{
		memcpy( dst, global_buf, sz );
		eReturn = -1;
	}
	else
#else
    if( *global_buf != dst )
	{
		memcpy_s( dst, global_buf, sz );
		eReturn = -4;
	}
	else
#endif
	if( dst != '\0' )
	{
		memcpy( dst, global_buf, sz );
		eReturn = 0;
	}
	else if( *(global_buf + 4) == 0UL )
	{
		eReturn = -3;
	}
	else
	{
		eReturn = -2;
        }
    return eReturn;
}

The XML output for tree-sitter-parser.py has an ERROR node in the resulting AST at the two else nodes within the #if directives, which causes the subsequent GumTreeDiff tree generation to fail. The cgum and srcML parsers seem to handle these just fine so I'm assuming there's something not handled correctly in the tree-sitter-to-XML conversion.

Update: Didn't think to look in the tree-sitter-c repo first, and it looks like this may be a known issue: tree-sitter/tree-sitter-c#70

Closing since it's clearly not an issue with the XML generation wrapper.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions