[clang] 2cc5846 - [clang][sema] Ignore xor-used-as-pow if both sides are macros
Timm Bäder via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 25 07:31:44 PST 2021
Author: Timm Bäder
Date: 2021-02-25T16:31:07+01:00
New Revision: 2cc58463caf4c8a43c2954e4206d3647c762ba30
URL: https://github.com/llvm/llvm-project/commit/2cc58463caf4c8a43c2954e4206d3647c762ba30
DIFF: https://github.com/llvm/llvm-project/commit/2cc58463caf4c8a43c2954e4206d3647c762ba30.diff
LOG: [clang][sema] Ignore xor-used-as-pow if both sides are macros
This happens in codebases a lot, which use xor where both sides are
macros. Using xor in that case is not the common error-prone 2^6 code
that the warning was introduced for.
Don't diagnose such a use of xor.
Differential Revision: https://reviews.llvm.org/D97445
Added:
Modified:
clang/lib/Sema/SemaExpr.cpp
clang/test/SemaCXX/warn-xor-as-pow.cpp
Removed:
################################################################################
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index e03183c1a42f..98097b6fb381 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -12102,6 +12102,11 @@ static void diagnoseXorMisusedAsPow(Sema &S, const ExprResult &XorLHS,
if (Loc.isMacroID())
return;
+ // Do not diagnose if both LHS and RHS are macros.
+ if (XorLHS.get()->getExprLoc().isMacroID() &&
+ XorRHS.get()->getExprLoc().isMacroID())
+ return;
+
bool Negative = false;
bool ExplicitPlus = false;
const auto *LHSInt = dyn_cast<IntegerLiteral>(XorLHS.get());
diff --git a/clang/test/SemaCXX/warn-xor-as-pow.cpp b/clang/test/SemaCXX/warn-xor-as-pow.cpp
index 123d0ac5e031..88719bd1a9d7 100644
--- a/clang/test/SemaCXX/warn-xor-as-pow.cpp
+++ b/clang/test/SemaCXX/warn-xor-as-pow.cpp
@@ -65,6 +65,7 @@ void test(unsigned a, unsigned b) {
res = 2 ^ 0x4;
res = 2 ^ 04;
+ res = TWO ^ TEN;
res = 0x2 ^ 10;
res = 0X2 ^ 10;
res = 02 ^ 10;
More information about the cfe-commits
mailing list