[cfe-dev] Misleading macro evaluation warning

Brad Moody via cfe-dev cfe-dev at lists.llvm.org
Sat Feb 13 18:39:30 PST 2021


Hi all,

I have an implementation for a new clang compiler warning that I'd like to upstream and I'm looking for guidance on how to get the process started.

The warning reports expressions inside macro expansions that evaluate in an unexpected way due to missing parens, either around a macro argument name or around the entire macro body. This is a super common mistake that has probably been made by anyone who has written a C macro in their life, but I'm not aware of any compilers or other tools that help catch these bugs.

An example:
    #define TWO 1 + 1
    int f() {
        return TWO * 2; // Actually returns 3
    }

Clang output:
    a.c:1:15: warning: misleading evaluation of expression in expansion of macro TWO due to missing parentheses
    #define TWO 1 + 1
                  ^
    a.c:3:16: note: expression has higher precedence than expression inside macro body
        return TWO * 2;
                   ^
    a.c:3:12: note: low precedence expression is hidden by expansion of macro TWO
        return TWO * 2;
               ^
    1 warning generated.

We've been using this warning internally for about a year now, but mostly on closed code so unfortunately I can't show those results here. I'm in the process of gathering results on open-source code now for evaluation purposes.

So, what should be my next steps?

Thanks,
Brad Moody
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20210214/41ac232c/attachment.html>


More information about the cfe-dev mailing list