[clang] [Clang] add additional tests for -Wshift-bool (PR #130339)
Oleksandr T. via cfe-commits
cfe-commits at lists.llvm.org
Wed Mar 12 04:47:09 PDT 2025
https://github.com/a-tarasyuk updated https://github.com/llvm/llvm-project/pull/130339
>From bed2cb009ae2e560aa00f86b90c57d82f97bb435 Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Fri, 7 Mar 2025 22:10:24 +0200
Subject: [PATCH 1/4] [Clang] add additional tests for -Wshift-bool
---
clang/test/Sema/shift-bool.cpp | 3 +++
1 file changed, 3 insertions(+)
diff --git a/clang/test/Sema/shift-bool.cpp b/clang/test/Sema/shift-bool.cpp
index a17a0e0ad9e7d..efaca65aacaed 100644
--- a/clang/test/Sema/shift-bool.cpp
+++ b/clang/test/Sema/shift-bool.cpp
@@ -3,6 +3,7 @@
void t() {
int x = 10;
bool y = true;
+ int z = 1;
bool a = y << x;
bool b = y >> x; // expected-warning {{right shifting a 'bool' implicitly converts it to 'int'}}
@@ -22,4 +23,6 @@ void t() {
if ((y << 1) != 0) { }
if ((y >> 1) != 0) { } // expected-warning {{right shifting a 'bool' implicitly converts it to 'int'}}
+
+ bool k = (x < z) >> 1; // expected-warning {{right shifting a 'bool' implicitly converts it to 'int'}}
}
>From 9be7657ef45482a84c571b6b3f15be833841467a Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Tue, 11 Mar 2025 17:15:52 +0200
Subject: [PATCH 2/4] add c test
---
clang/test/Sema/shift-bool.c | 24 ++++++++++++++++++++++++
clang/test/Sema/shift-bool.cpp | 3 ---
2 files changed, 24 insertions(+), 3 deletions(-)
create mode 100644 clang/test/Sema/shift-bool.c
diff --git a/clang/test/Sema/shift-bool.c b/clang/test/Sema/shift-bool.c
new file mode 100644
index 0000000000000..589ba49fa2bba
--- /dev/null
+++ b/clang/test/Sema/shift-bool.c
@@ -0,0 +1,24 @@
+// RUN: %clang_cc1 -fsyntax-only -Wshift-bool -verify %s
+
+void t() {
+ int x = 10;
+ int y = 1;
+
+ int a = y << x;
+ int b = y >> x;
+
+ int c = 0 << x;
+ int d = 0 >> x;
+
+ int e = y << 1;
+ int f = y >> 1;
+
+ int g = y << -1; // expected-warning {{shift count is negative}}
+ int h = y >> -1; // expected-warning {{shift count is negative}}
+
+ int i = y << 0;
+ int j = y >> 0;
+
+ if ((y << 1) != 0) { }
+ if ((y >> 1) != 0) { }
+}
diff --git a/clang/test/Sema/shift-bool.cpp b/clang/test/Sema/shift-bool.cpp
index efaca65aacaed..a17a0e0ad9e7d 100644
--- a/clang/test/Sema/shift-bool.cpp
+++ b/clang/test/Sema/shift-bool.cpp
@@ -3,7 +3,6 @@
void t() {
int x = 10;
bool y = true;
- int z = 1;
bool a = y << x;
bool b = y >> x; // expected-warning {{right shifting a 'bool' implicitly converts it to 'int'}}
@@ -23,6 +22,4 @@ void t() {
if ((y << 1) != 0) { }
if ((y >> 1) != 0) { } // expected-warning {{right shifting a 'bool' implicitly converts it to 'int'}}
-
- bool k = (x < z) >> 1; // expected-warning {{right shifting a 'bool' implicitly converts it to 'int'}}
}
>From ae6e763afd673f5806748d4d23ea138ac6fc23e4 Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Tue, 11 Mar 2025 22:45:01 +0200
Subject: [PATCH 3/4] update c test
---
clang/test/Sema/shift-bool.c | 29 ++++++++++++++++-------------
1 file changed, 16 insertions(+), 13 deletions(-)
diff --git a/clang/test/Sema/shift-bool.c b/clang/test/Sema/shift-bool.c
index 589ba49fa2bba..f3b00aa87e068 100644
--- a/clang/test/Sema/shift-bool.c
+++ b/clang/test/Sema/shift-bool.c
@@ -2,23 +2,26 @@
void t() {
int x = 10;
- int y = 1;
+ int y = 5;
- int a = y << x;
- int b = y >> x;
+ int a = (x < y) << 1;
+ int b = (x < y) >> 1;
- int c = 0 << x;
- int d = 0 >> x;
+ int c = (x > y) << 1;
+ int d = (x > y) >> 1;
- int e = y << 1;
- int f = y >> 1;
+ int e = (x == y) << 1;
+ int f = (x == y) >> 1;
- int g = y << -1; // expected-warning {{shift count is negative}}
- int h = y >> -1; // expected-warning {{shift count is negative}}
+ int g = (x != y) << 1;
+ int h = (x != y) >> 1;
- int i = y << 0;
- int j = y >> 0;
+ int i = (x < y) << 0;
+ int j = (x < y) >> 0;
- if ((y << 1) != 0) { }
- if ((y >> 1) != 0) { }
+ int k = (x < y) << -1; // expected-warning {{shift count is negative}}
+ int l = (x < y) >> -1; // expected-warning {{shift count is negative}}
+
+ if (((x < y) << 1) != 0) { }
+ if (((x < y) >> 1) != 0) { }
}
>From 3d5293c346f4485f3e28d1330899255c274da529 Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Tue, 11 Mar 2025 22:48:44 +0200
Subject: [PATCH 4/4] update cpp test
---
clang/test/Sema/shift-bool.cpp | 3 +++
1 file changed, 3 insertions(+)
diff --git a/clang/test/Sema/shift-bool.cpp b/clang/test/Sema/shift-bool.cpp
index a17a0e0ad9e7d..5350da91976f9 100644
--- a/clang/test/Sema/shift-bool.cpp
+++ b/clang/test/Sema/shift-bool.cpp
@@ -3,6 +3,7 @@
void t() {
int x = 10;
bool y = true;
+ int z = 1;
bool a = y << x;
bool b = y >> x; // expected-warning {{right shifting a 'bool' implicitly converts it to 'int'}}
@@ -20,6 +21,8 @@ void t() {
bool i = y << 0;
bool j = y >> 0; // expected-warning {{right shifting a 'bool' implicitly converts it to 'int'}}
+ bool k = (x < z) >> 1; // expected-warning {{right shifting a 'bool' implicitly converts it to 'int'}}
+
if ((y << 1) != 0) { }
if ((y >> 1) != 0) { } // expected-warning {{right shifting a 'bool' implicitly converts it to 'int'}}
}
More information about the cfe-commits
mailing list