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

154 lines
5.6 KiB
C++

// RUN: %check_clang_tidy -std=c++11,c++14 -check-suffix=,CXX14 %s bugprone-dangling-handle %t -- \
// RUN: -config="{CheckOptions: \
// RUN: {bugprone-dangling-handle.HandleClasses: \
// RUN: 'std::basic_string_view; ::llvm::StringRef;'}}"
// RUN: %check_clang_tidy -std=c++17-or-later -check-suffix=,CXX17 %s bugprone-dangling-handle %t -- \
// RUN: -config="{CheckOptions: \
// RUN: {bugprone-dangling-handle.HandleClasses: \
// RUN: 'std::basic_string_view; ::llvm::StringRef;'}}"
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
namespace llvm {
class StringRef {
public:
StringRef();
StringRef(const char*);
StringRef(const std::string&);
};
} // namespace llvm
std::string ReturnsAString();
void Positives() {
std::string_view view1 = std::string();
// CHECK-MESSAGES-CXX14: [[@LINE-1]]:20: warning: std::basic_string_view outlives its value [bugprone-dangling-handle]
// CHECK-MESSAGES-CXX17: [[@LINE-2]]:28: warning: std::basic_string_view outlives its value [bugprone-dangling-handle]
std::string_view view_2 = ReturnsAString();
// CHECK-MESSAGES-CXX14: [[@LINE-1]]:20: warning: std::basic_string_view outlives its value [bugprone-dangling-handle]
// CHECK-MESSAGES-CXX17: [[@LINE-2]]:29: warning: std::basic_string_view outlives its value [bugprone-dangling-handle]
view1 = std::string();
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: std::basic_string_view outlives
const std::string& str_ref = "";
std::string_view view3 = true ? "A" : str_ref;
// CHECK-MESSAGES-CXX14: [[@LINE-1]]:20: warning: std::basic_string_view outlives
// CHECK-MESSAGES-CXX17: [[@LINE-2]]:28: warning: std::basic_string_view outlives
view3 = true ? "A" : str_ref;
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: std::basic_string_view outlives
std::string_view view4(ReturnsAString());
// CHECK-MESSAGES-CXX14: [[@LINE-1]]:20: warning: std::basic_string_view outlives
// CHECK-MESSAGES-CXX17: [[@LINE-2]]:26: warning: std::basic_string_view outlives
std::string_view view5 = std::string("test");
// CHECK-MESSAGES-CXX14: [[@LINE-1]]:20: warning: std::basic_string_view outlives its value [bugprone-dangling-handle]
// CHECK-MESSAGES-CXX17: [[@LINE-2]]:28: warning: std::basic_string_view outlives its value [bugprone-dangling-handle]
std::string_view view6 = std::string{"test"};
// CHECK-MESSAGES-CXX14: [[@LINE-1]]:20: warning: std::basic_string_view outlives its value [bugprone-dangling-handle]
// CHECK-MESSAGES-CXX17: [[@LINE-2]]:28: warning: std::basic_string_view outlives its value [bugprone-dangling-handle]
}
void OtherTypes() {
llvm::StringRef ref = std::string();
// CHECK-MESSAGES-CXX14: [[@LINE-1]]:19: warning: llvm::StringRef outlives its value
// CHECK-MESSAGES-CXX17: [[@LINE-2]]:25: warning: llvm::StringRef outlives its value
}
const char static_array[] = "A";
std::string_view ReturnStatements(int i, std::string value_arg,
const std::string &ref_arg) {
const char array[] = "A";
const char* ptr = "A";
std::string s;
static std::string ss;
switch (i) {
// Bad cases
case 0:
return array; // refers to local
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: std::basic_string_view outliv
case 1:
return s; // refers to local
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: std::basic_string_view outliv
case 2:
return std::string(); // refers to temporary
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: std::basic_string_view outliv
case 3:
return value_arg; // refers to by-value arg
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: std::basic_string_view outliv
// Ok cases
case 100:
return ss; // refers to static
case 101:
return static_array; // refers to static
case 102:
return ptr; // pointer is ok
case 103:
return ref_arg; // refers to by-ref arg
}
struct S {
std::string_view view() { return value; }
std::string value;
};
(void)[&]()->std::string_view {
// This should not warn. The string is bound by reference.
return s;
};
(void)[=]() -> std::string_view {
// This should not warn. The reference is valid as long as the lambda.
return s;
};
(void)[=]() -> std::string_view {
// FIXME: This one should warn. We are returning a reference to a local
// lambda variable.
std::string local;
return local;
};
return "";
}
void Containers() {
std::vector<std::string_view> v;
v.assign(3, std::string());
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: std::basic_string_view outlives
v.insert(nullptr, std::string());
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: std::basic_string_view outlives
v.insert(nullptr, 3, std::string());
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: std::basic_string_view outlives
v.push_back(std::string());
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: std::basic_string_view outlives
v.resize(3, std::string());
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: std::basic_string_view outlives
std::set<std::string_view> s;
s.insert(std::string());
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: std::basic_string_view outlives
s.insert(nullptr, std::string());
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: std::basic_string_view outlives
std::map<std::string_view, int> m;
m[std::string()];
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: std::basic_string_view outlives
}
void TakesAStringView(std::string_view);
void Negatives(std::string_view default_arg = ReturnsAString()) {
std::string str;
std::string_view view = str;
TakesAStringView(std::string());
}