GumTreeDiff / tree-sitter-parser

A tree generated based upon the amazing tree-sitter library.
GNU Affero General Public License v3.0
5 stars 6 forks source link

Errors parsing #if/#else statements #4

Closed bdemick closed 2 years ago

bdemick commented 2 years ago

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: https://github.com/tree-sitter/tree-sitter-c/issues/70

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