[all-commits] [llvm/llvm-project] 0cc3c1: Missing tautological compare warnings due to unary...

Muhammad Usman Shahid via All-commits all-commits at lists.llvm.org
Thu Jul 28 04:49:24 PDT 2022

  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 0cc3c184c784d5f0d55de8ad0a9eeee876acd149
  Author: Muhammad Usman Shahid <codesbyusman at gmail.com>
  Date:   2022-07-28 (Thu, 28 Jul 2022)

  Changed paths:
    M clang/docs/ReleaseNotes.rst
    M clang/lib/Analysis/CFG.cpp
    M clang/test/Sema/warn-bitwise-compare.c
    M clang/test/SemaCXX/warn-unreachable.cpp

  Log Message:
  Missing tautological compare warnings due to unary operators

The patch mainly focuses on the lack of warnings for
-Wtautological-compare. It works fine for positive numbers but doesn't
for negative numbers. This is because the warning explicitly checks for
an IntegerLiteral AST node, but -1 is represented by a UnaryOperator
with an IntegerLiteral sub-Expr.

For the below code we have warnings:

if (0 == (5 | x)) {}

but not for

if (0 == (-5 | x)) {}

This patch changes the analysis to not look at the AST node directly to
see if it is an IntegerLiteral, but instead attempts to evaluate the
expression to see if it is an integer constant expression. This handles
unary negation signs, but also handles all the other possible operators
as well.

Fixes #42918
Differential Revision: https://reviews.llvm.org/D130510

More information about the All-commits mailing list