skvadrik / re2c

Lexer generator for C, C++, Go and Rust.

Home Page:https://re2c.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

clang-tidy warnning value never used.

krishna116 opened this issue · comments

Give a minimal code:

bool isValidHexNumber(const char* str, int length)
{
    const char *YYCURSOR = str;
    const char *YYLIMIT = str + length;
    const char *YYMARKER;
    for(;;)
    {
    /*!local:re2c:isValidHexNumber
        re2c:define:YYCTYPE = char;
        re2c:yyfill:enable = 0;
        re2c:eof = 0;

        HexNum = ("+"|"-")? "0" [xX][0-9A-Fa-f]{1,31}; 

        HexNum  { return true; }
        $       { return false; }
        *       { return false; }
    */
    }

    return false;
}

Run re2c lex.txt -o lex.cpp and build with the code "lex.cpp", I get the clang-tidy warnning:

lex.cpp:52:2: warning: Value stored to 'YYCURSOR' is never read [clang-analyzer-deadcode.DeadStores]
        YYCURSOR = YYMARKER;
        ^          ~~~~~~~~

$ clang-tidy --version
LLVM (http://llvm.org/):
LLVM version 14.0.0
Optimized build.
Default target: x86_64-w64-windows-gnu
Host CPU: znver1

$ re2c --version
re2c 3.0

Thanks.

Thanks for reporting! Unfortunately this cannot be fixed because re2c doesn't know if the semantic action uses YYCURSOR or not. It happens to be { return false; } in this case, but it could be something like { return YYCURSOR - str > 42; }. Even if re2c parsed C++ code (which it doesn't), it would need to implement preprocessor and perform static analysis to determine if YYCURSOR escapes from the function, and that may be impossible to determine with static analysis in the general case. So the only sensible thing to do for re2c is to set YYCURSOR to the correct value.

Thanks for reply. So it means I cannot return from the middle of the code if I want to pass this case?
I change the code logic to return from the last line, now it works fine.

bool isValidHexNumber(const char* str, int length)
{
    const char *YYCURSOR = str;
    const char *YYLIMIT = str + length;
    const char *YYMARKER;
    bool good = false;

    for(;;)
    {
    /*!local:re2c:isValidHexNumber
        re2c:define:YYCTYPE = char;
        re2c:yyfill:enable = 0;
        re2c:eof = 0;

        HexNum = ("+"|"-")? "0" [xX][0-9A-Fa-f]{1,31}; 

        HexNum  { if((YYCURSOR - str) == length) good = true; break; }
        $       { break; }
        *       { break; }
    */
    }

    return good;
}

Thank you very much.

No, it is totally fine to return from semantic actions, you don't need to rewrite the code to break and then return.

If you want to check that HexNum is the only lexeme in str (in other words, that the string ends after HexNum), then you can either compare (YYCURSOR - str) == length as you did, or better if the string is NULL-terminated, you can change HexNum definition to HexNum = ("+"|"-")? "0" [xX][0-9A-Fa-f]{1,31} [\x00];, adding the terminating NULL at the end.

Your original problem was a warning from clang-tidy. It is not a correctness issue, just a warning that cannot be avoided (unless you use YYCURSOR in a semantic action) but it does no harm otherwise. You can add (void) YYCURSOR; before return in semantic action, or use other ways to silence the warning.

The explanation is good, thank you again, so it is closed.