Files
TheRock/compiler/amd-llvm/clang-tools-extra/test/clang-tidy/checkers/bugprone/string-constructor.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
8.3 KiB
C++

// RUN: %check_clang_tidy %s bugprone-string-constructor %t
namespace std {
template <typename T>
class allocator {};
template <typename T>
class char_traits {};
template <typename C, typename T = std::char_traits<C>, typename A = std::allocator<C> >
struct basic_string {
basic_string();
basic_string(const C*, unsigned int size, const A &a = A());
basic_string(const C *, const A &allocator = A());
basic_string(unsigned int size, C c, const A &a = A());
basic_string(const C*, unsigned int pos, unsigned int size, const A &a = A());
};
typedef basic_string<char> string;
typedef basic_string<wchar_t> wstring;
template <typename C, typename T = std::char_traits<C>>
struct basic_string_view {
basic_string_view();
basic_string_view(const C *, unsigned int size);
basic_string_view(const C *);
};
typedef basic_string_view<char> string_view;
typedef basic_string_view<wchar_t> wstring_view;
}
const char* kText = "";
const char kText2[] = "";
extern const char kText3[];
void Test() {
std::string str('x', 4);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: string constructor parameters are probably swapped; expecting string(count, character) [bugprone-string-constructor]
// CHECK-FIXES: std::string str(4, 'x');
std::wstring wstr(L'x', 4);
// CHECK-MESSAGES: [[@LINE-1]]:16: warning: string constructor parameters are probably swapped
// CHECK-FIXES: std::wstring wstr(4, L'x');
std::string s0(0, 'x');
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: constructor creating an empty string
std::string s1(-4, 'x');
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: negative value used as length parameter
std::string s2(0x1000000, 'x');
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: suspicious large length parameter
std::string q0("test", 0);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: constructor creating an empty string
std::string q1(kText, -4);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: negative value used as length parameter
std::string q2("test", 200);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: length is bigger than string literal size
std::string t1("test", 5);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: length is bigger than string literal size
std::string q3(kText, 200);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: length is bigger than string literal size
std::string q4(kText2, 200);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: length is bigger than string literal size
std::string q5(kText3, 0x1000000);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: suspicious large length parameter
std::string q6(nullptr);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: constructing string from nullptr is undefined behaviour
std::string q7 = 0;
// CHECK-MESSAGES: [[@LINE-1]]:20: warning: constructing string from nullptr is undefined behaviour
std::string r1("test", 1, 0);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: constructor creating an empty string
std::string r2("test", 0, -4);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: negative value used as length parameter
std::string r3("test", -4, 1);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: negative value used as position of the first character parameter
std::string r4("test", 0, 0x1000000);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: suspicious large length parameter
std::string r5("test", 0, 5);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: length is bigger than string literal size
std::string r6("test", 3, 2);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: length is bigger than remaining string literal size
std::string r7("test", 4, 1);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: position of the first character parameter is bigger than string literal character range
}
void TestView() {
std::string_view q0("test", 0);
// CHECK-MESSAGES: [[@LINE-1]]:20: warning: constructor creating an empty string
std::string_view q1(kText, -4);
// CHECK-MESSAGES: [[@LINE-1]]:20: warning: negative value used as length parameter
std::string_view q2("test", 200);
// CHECK-MESSAGES: [[@LINE-1]]:20: warning: length is bigger than string literal size
std::string_view q3(kText, 200);
// CHECK-MESSAGES: [[@LINE-1]]:20: warning: length is bigger than string literal size
std::string_view q4(kText2, 200);
// CHECK-MESSAGES: [[@LINE-1]]:20: warning: length is bigger than string literal size
std::string_view q5(kText3, 0x1000000);
// CHECK-MESSAGES: [[@LINE-1]]:20: warning: suspicious large length parameter
std::string_view q6(nullptr);
// CHECK-MESSAGES: [[@LINE-1]]:20: warning: constructing string from nullptr is undefined behaviour
std::string_view q7 = 0;
// CHECK-MESSAGES: [[@LINE-1]]:25: warning: constructing string from nullptr is undefined behaviour
}
void TestUnsignedArguments() {
std::string s0("test", 0u);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: constructor creating an empty string
std::string s1(0x1000000ull, 'x');
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: suspicious large length parameter
std::string s2("test", 3ull, 2u);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: length is bigger than remaining string literal size
std::string s3("test", 0u, 5ll);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: length is bigger than string literal size
}
std::string StringFromZero() {
return 0;
// CHECK-MESSAGES: [[@LINE-1]]:10: warning: constructing string from nullptr is undefined behaviour
}
std::string_view StringViewFromZero() {
return 0;
// CHECK-MESSAGES: [[@LINE-1]]:10: warning: constructing string from nullptr is undefined behaviour
}
void TestExplicitAllocator() {
std::allocator<char> a;
std::string s1('x', 5, a);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: string constructor parameters are probably swapped; expecting string(count, character) [bugprone-string-constructor]
// CHECK-FIXES: std::string s1(5, 'x', a);
std::string s2(0, 'x', a);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: constructor creating an empty string
std::string s3(-4, 'x', a);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: negative value used as length parameter
std::string s4(0x1000000, 'x', a);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: suspicious large length parameter
std::string q0("test", 0, a);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: constructor creating an empty string
std::string q1(kText, -4, a);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: negative value used as length parameter
std::string q2("test", 200, a);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: length is bigger than string literal size
std::string q3(kText3, 0x1000000, a);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: suspicious large length parameter
std::string r1("test", 1, 0, a);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: constructor creating an empty string
std::string r2("test", 0, -4, a);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: negative value used as length parameter
std::string r3("test", -4, 1, a);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: negative value used as position of the first character parameter
std::string r4("test", 0, 0x1000000, a);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: suspicious large length parameter
std::string r5("test", 0, 5, a);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: length is bigger than string literal size
std::string r6("test", 3, 2, a);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: length is bigger than remaining string literal size
std::string r7("test", 4, 1, a);
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: position of the first character parameter is bigger than string literal character range
}
void Valid() {
std::string empty();
std::string str(4, 'x');
std::wstring wstr(4, L'x');
std::string s1("test", 4);
std::string s2("test", 3);
std::string s3("test");
std::string s4("test\000", 5);
std::string s6("te" "st", 4);
std::string s7("test", 0, 4);
std::string s8("test", 3, 1);
std::string s9("te" "st", 1, 2);
std::allocator<char> a;
std::string sa1(4, 'x', a);
std::string sa2("test", 4, a);
std::string sa3("test", 3, a);
std::string sa4("test", 0, 4, a);
std::string sa5("test", 3, 1, a);
std::string_view emptyv();
std::string_view sv1("test", 4);
std::string_view sv2("test", 3);
std::string_view sv3("test");
}
namespace instantiation_dependent_exprs {
template<typename T>
struct S {
bool x;
std::string f() { return x ? "a" : "b"; }
std::string_view g() { return x ? "a" : "b"; }
};
}