llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[clang] warn on operator in macro expansion whose operand doesn't lie entirely within the same macro expansion

Open zygoloid opened this issue 9 months ago • 4 comments

Testcase:

#define FOO 2+3
// 2+(3*4) == 14, not (2+3)*4 == 20
int n = FOO*4;

Clang doesn't warn on this, but I think it should. In general, I think a good heuristic would be: if the location of an operator is within a macro expansion, and an operand of that operator is not entirely within that same macro expansion, then warn. In this case, the + operator within FOO has an operand that extends to the 4 token, so we should warn.

This seems like something we could quickly and easily check for with our existing source location representation.

zygoloid avatar Mar 04 '26 19:03 zygoloid

Hi!

This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:

  1. Check that no other contributor is working on this issue. If someone is assigned to the issue or claimed to be working on it, ping the person. After one week without a response, the assignee may be changed.
  2. Leave a comment indicating that you are working on the issue, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
  3. Fix the issue locally.
  4. Run the test suite locally. Remember that the subdirectories under test/ create fine-grained testing targets, so you can e.g. use make check-clang-ast to only run Clang's AST tests.
  5. Create a Git commit.
  6. Run git clang-format HEAD~1 to format your changes.
  7. Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation. Mention this issue in the description of the pull request.

If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.

llvmbot avatar Mar 04 '26 19:03 llvmbot

@llvm/issue-subscribers-good-first-issue

Author: Richard Smith (zygoloid)

Testcase: ```c++ #define FOO 2+3 // 2+(3*4) == 14, not (2+3)*4 == 20 int n = FOO*4; ```

Clang doesn't warn on this, but I think it should. In general, I think a good heuristic would be: if the location of an operator is within a macro expansion, and an operand of that operator is not entirely within that same macro expansion, then warn. In this case, the + operator within FOO has an operand that extends to the 4 token, so we should warn.

This seems like something we could quickly and easily check for with our existing source location representation.

llvmbot avatar Mar 04 '26 20:03 llvmbot

could you please assign this to me?

ArfiH avatar Mar 04 '26 21:03 ArfiH

could you please assign this to me?

We don't assign until there is a PR and we check it is going in the right direction but announcing you are working on it is a good idea to prevent duplicate effort.

shafik avatar Mar 04 '26 22:03 shafik