Files
TheRock/compiler/amd-llvm/clang/test/SemaCXX/warn-tautological-compare.cpp
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

179 lines
7.4 KiB
C++

// Force x86-64 because some of our heuristics are actually based
// on integer sizes.
// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -verify -std=c++11 %s
namespace RuntimeBehavior {
// Avoid emitting tautological compare warnings when the code already has
// compile time checks on variable sizes.
const int kintmax = 2147483647;
void test0(short x) {
if (sizeof(x) < sizeof(int) || x < kintmax) {}
if (x < kintmax) {}
// expected-warning@-1{{comparison of constant 2147483647 with expression of type 'short' is always true}}
}
void test1(short x) {
if (x < kintmax) {}
// expected-warning@-1{{comparison of constant 2147483647 with expression of type 'short' is always true}}
if (sizeof(x) < sizeof(int))
return;
if (x < kintmax) {}
}
}
namespace ArrayCompare {
#define GetValue(ptr) ((ptr != 0) ? ptr[0] : 0)
extern int a[] __attribute__((weak));
int b[] = {8,13,21};
struct {
int x[10];
} c;
const char str[] = "text";
void ignore() {
if (a == 0) {}
if (a != 0) {}
(void)GetValue(b);
}
void test() {
if (b == 0) {}
// expected-warning@-1{{comparison of array 'b' equal to a null pointer is always false}}
if (b != 0) {}
// expected-warning@-1{{comparison of array 'b' not equal to a null pointer is always true}}
if (0 == b) {}
// expected-warning@-1{{comparison of array 'b' equal to a null pointer is always false}}
if (0 != b) {}
// expected-warning@-1{{comparison of array 'b' not equal to a null pointer is always true}}
if (c.x == 0) {}
// expected-warning@-1{{comparison of array 'c.x' equal to a null pointer is always false}}
if (c.x != 0) {}
// expected-warning@-1{{comparison of array 'c.x' not equal to a null pointer is always true}}
if (str == 0) {}
// expected-warning@-1{{comparison of array 'str' equal to a null pointer is always false}}
if (str != 0) {}
// expected-warning@-1{{comparison of array 'str' not equal to a null pointer is always true}}
}
}
namespace FunctionCompare {
#define CallFunction(f) ((f != 0) ? f() : 0)
extern void a() __attribute__((weak));
void fun1();
int fun2();
int* fun3();
int* fun4(int);
class S {
public:
static int foo();
};
void ignore() {
if (a == 0) {}
if (0 != a) {}
(void)CallFunction(fun2);
}
void test() {
if (fun1 == 0) {}
// expected-warning@-1{{comparison of function 'fun1' equal to a null pointer is always false}}
// expected-note@-2{{prefix with the address-of operator to silence this warning}}
if (fun2 == 0) {}
// expected-warning@-1{{comparison of function 'fun2' equal to a null pointer is always false}}
// expected-note@-2{{prefix with the address-of operator to silence this warning}}
// expected-note@-3{{suffix with parentheses to turn this into a function call}}
if (fun3 == 0) {}
// expected-warning@-1{{comparison of function 'fun3' equal to a null pointer is always false}}
// expected-note@-2{{prefix with the address-of operator to silence this warning}}
// expected-note@-3{{suffix with parentheses to turn this into a function call}}
if (fun4 == 0) {}
// expected-warning@-1{{comparison of function 'fun4' equal to a null pointer is always false}}
// expected-note@-2{{prefix with the address-of operator to silence this warning}}
if (nullptr != fun1) {}
// expected-warning@-1{{comparison of function 'fun1' not equal to a null pointer is always true}}
// expected-note@-2{{prefix with the address-of operator to silence this warning}}
if (nullptr != fun2) {}
// expected-warning@-1{{comparison of function 'fun2' not equal to a null pointer is always true}}
// expected-note@-2{{prefix with the address-of operator to silence this warning}}
if (nullptr != fun3) {}
// expected-warning@-1{{comparison of function 'fun3' not equal to a null pointer is always true}}
// expected-note@-2{{prefix with the address-of operator to silence this warning}}
// expected-note@-3{{suffix with parentheses to turn this into a function call}}
if (nullptr != fun4) {}
// expected-warning@-1{{comparison of function 'fun4' not equal to a null pointer is always true}}
// expected-note@-2{{prefix with the address-of operator to silence this warning}}
if (S::foo == 0) {}
// expected-warning@-1{{comparison of function 'S::foo' equal to a null pointer is always false}}
// expected-note@-2{{prefix with the address-of operator to silence this warning}}
// expected-note@-3{{suffix with parentheses to turn this into a function call}}
}
}
namespace PointerCompare {
extern int a __attribute__((weak));
int b;
static int c;
class S {
public:
static int a;
int b;
};
void ignored() {
if (&a == 0) {}
}
void test() {
S s;
if (&b == 0) {}
// expected-warning@-1{{comparison of address of 'b' equal to a null pointer is always false}}
if (&c == 0) {}
// expected-warning@-1{{comparison of address of 'c' equal to a null pointer is always false}}
if (&s.a == 0) {}
// expected-warning@-1{{comparison of address of 's.a' equal to a null pointer is always false}}
if (&s.b == 0) {}
// expected-warning@-1{{comparison of address of 's.b' equal to a null pointer is always false}}
if (&S::a == 0) {}
// expected-warning@-1{{comparison of address of 'S::a' equal to a null pointer is always false}}
}
}
namespace macros {
#define assert(x) if (x) {}
int array[5];
void fun();
int x;
void test() {
assert(array == 0);
// expected-warning@-1{{comparison of array 'array' equal to a null pointer is always false}}
assert(array != 0);
// expected-warning@-1{{comparison of array 'array' not equal to a null pointer is always true}}
assert(array == 0 && "expecting null pointer");
// expected-warning@-1{{comparison of array 'array' equal to a null pointer is always false}}
assert(array != 0 && "expecting non-null pointer");
// expected-warning@-1{{comparison of array 'array' not equal to a null pointer is always true}}
assert(fun == 0);
// expected-warning@-1{{comparison of function 'fun' equal to a null pointer is always false}}
// expected-note@-2{{prefix with the address-of operator to silence this warning}}
assert(fun != 0);
// expected-warning@-1{{comparison of function 'fun' not equal to a null pointer is always true}}
// expected-note@-2{{prefix with the address-of operator to silence this warning}}
assert(fun == 0 && "expecting null pointer");
// expected-warning@-1{{comparison of function 'fun' equal to a null pointer is always false}}
// expected-note@-2{{prefix with the address-of operator to silence this warning}}
assert(fun != 0 && "expecting non-null pointer");
// expected-warning@-1{{comparison of function 'fun' not equal to a null pointer is always true}}
// expected-note@-2{{prefix with the address-of operator to silence this warning}}
assert(&x == 0);
// expected-warning@-1{{comparison of address of 'x' equal to a null pointer is always false}}
assert(&x != 0);
// expected-warning@-1{{comparison of address of 'x' not equal to a null pointer is always true}}
assert(&x == 0 && "expecting null pointer");
// expected-warning@-1{{comparison of address of 'x' equal to a null pointer is always false}}
assert(&x != 0 && "expecting non-null pointer");
// expected-warning@-1{{comparison of address of 'x' not equal to a null pointer is always true}}
}
}