Files
TheRock/compiler/amd-llvm/clang-tools-extra/test/clang-tidy/checkers/bugprone/narrowing-conversions-bitfields.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

194 lines
4.8 KiB
C++

// RUN: %check_clang_tidy %s bugprone-narrowing-conversions %t \
// RUN: -std=c++17-or-later -- -target x86_64-unknown-linux
#include <utility>
#define CHAR_BITS 8
static_assert(sizeof(unsigned int) == 32 / CHAR_BITS);
struct NoBitfield {
unsigned int id;
};
struct SmallBitfield {
unsigned int id : 4;
};
struct BigBitfield {
unsigned int id : 31;
};
struct CompleteBitfield {
unsigned int id : 32;
};
int example_warning(unsigned x) {
// CHECK-MESSAGES: :[[@LINE+1]]:10: warning: narrowing conversion from 'unsigned int' to signed type 'int' is implementation-defined [bugprone-narrowing-conversions]
return x;
}
void test_binary_and(SmallBitfield x) {
static_assert(std::is_same_v<decltype(x.id & 1), int>);
static_assert(std::is_same_v<decltype(x.id & 1u), unsigned>);
x.id & 1;
x.id & 1u;
1 & x.id;
1u & x.id;
}
void test_binary_or(SmallBitfield x) {
static_assert(std::is_same_v<decltype(x.id | 1), int>);
static_assert(std::is_same_v<decltype(x.id | 1u), unsigned>);
x.id | 1;
x.id | 1u;
1 | x.id;
1u | x.id;
}
template <typename T>
void take(T);
void test_parameter_passing(NoBitfield x) {
take<char>(x.id);
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: narrowing conversion from 'unsigned int' to signed type 'char' is implementation-defined
take<short>(x.id);
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: narrowing conversion from 'unsigned int' to signed type 'short' is implementation-defined
take<unsigned>(x.id);
take<int>(x.id);
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: narrowing conversion from 'unsigned int' to signed type 'int' is implementation-defined
take<long>(x.id);
take<long long>(x.id);
}
void test_parameter_passing(SmallBitfield x) {
take<char>(x.id);
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: narrowing conversion from 'unsigned int' to signed type 'char' is implementation-defined
take<short>(x.id);
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: narrowing conversion from 'unsigned int' to signed type 'short' is implementation-defined
take<unsigned>(x.id);
take<int>(x.id); // no-warning
take<long>(x.id);
take<long long>(x.id);
}
void test_parameter_passing(BigBitfield x) {
take<char>(x.id);
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: narrowing conversion from 'unsigned int' to signed type 'char' is implementation-defined
take<short>(x.id);
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: narrowing conversion from 'unsigned int' to signed type 'short' is implementation-defined
take<unsigned>(x.id);
take<int>(x.id); // no-warning
take<long>(x.id);
take<long long>(x.id);
}
void test_parameter_passing(CompleteBitfield x) {
take<char>(x.id);
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: narrowing conversion from 'unsigned int' to signed type 'char' is implementation-defined
take<short>(x.id);
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: narrowing conversion from 'unsigned int' to signed type 'short' is implementation-defined
take<unsigned>(x.id);
take<int>(x.id);
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: narrowing conversion from 'unsigned int' to signed type 'int' is implementation-defined
take<long>(x.id);
take<long long>(x.id);
}
void test(NoBitfield x) {
static_assert(std::is_same_v<decltype(x.id << 1), unsigned>);
static_assert(std::is_same_v<decltype(x.id << 1u), unsigned>);
static_assert(std::is_same_v<decltype(x.id + 1), unsigned>);
static_assert(std::is_same_v<decltype(x.id + 1u), unsigned>);
x.id << 1;
x.id << 1u;
x.id >> 1;
x.id >> 1u;
x.id + 1;
x.id + 1u;
1 << x.id;
1u << x.id;
1 >> x.id;
1u >> x.id;
1 + x.id;
1u + x.id;
}
void test(SmallBitfield x) {
static_assert(std::is_same_v<decltype(x.id << 1), int>);
static_assert(std::is_same_v<decltype(x.id << 1u), int>);
x.id << 1;
x.id << 1u;
x.id >> 1;
x.id >> 1u;
x.id + 1;
x.id + 1u;
1 << x.id;
1u << x.id;
1 >> x.id;
1u >> x.id;
1 + x.id;
1u + x.id;
}
void test(BigBitfield x) {
static_assert(std::is_same_v<decltype(x.id << 1), int>);
static_assert(std::is_same_v<decltype(x.id << 1u), int>);
x.id << 1;
x.id << 1u;
x.id >> 1;
x.id >> 1u;
x.id + 1;
x.id + 1u;
1 << x.id;
1u << x.id;
1 >> x.id;
1u >> x.id;
1 + x.id;
1u + x.id;
}
void test(CompleteBitfield x) {
static_assert(std::is_same_v<decltype(x.id << 1), unsigned>);
static_assert(std::is_same_v<decltype(x.id << 1u), unsigned>);
x.id << 1;
x.id << 1u;
x.id >> 1;
x.id >> 1u;
x.id + 1;
x.id + 1u;
1 << x.id;
1u << x.id;
1 >> x.id;
1u >> x.id;
1 + x.id;
1u + x.id;
}
void test_parens(SmallBitfield x) {
static_assert(std::is_same_v<decltype(x.id << (2)), int>);
static_assert(std::is_same_v<decltype(((x.id)) << (2)), int>);
x.id << (2);
((x.id)) << (2);
static_assert(std::is_same_v<decltype((2) << x.id), int>);
static_assert(std::is_same_v<decltype((2) << ((x.id))), int>);
(2) << x.id;
(2) << ((x.id));
}