Page MenuHomePhabricator

File Metadata

Author
rnkovacs
Created
May 21 2018, 10:01 AM

tests.cpp

#include <iostream>
#include <string>
#include <string_view>
#include <vector>
void deref_after_scope() {
const char *p;
{
std::string str;
// ...
p = str.c_str();
}
std::cout << p << std::endl;
}
void wstring_deref_after_scope() {
const wchar_t *p;
{
std::wstring str;
// ...
p = str.c_str();
}
std::cout << p << std::endl;
}
void deref_after_scope2() {
std::string_view p;
{
std::string str;
// ...
p = str;
}
std::cout << p << std::endl;
}
void deref_after_scope3() {
std::string_view p;
{
std::string str;
// ...
p = std::string_view(str.c_str()+1, 5);
}
std::cout << p << std::endl;
}
const char *escape_via_return() {
std::string str;
return str.c_str();
}
std::string_view escape_via_return2() {
std::string str;
return str;
}
const char *escape_via_return_ok() {
static std::string str;
return str.c_str();
}
std::string h();
void deref_after_temporary() {
std::string_view p = h();
std::cout << p << std::endl;
}
void lifetime_extension_ok() {
const std::string &r = h();
std::string_view p = r;
std::cout << p << std::endl;
}
void consume(std::string s);
void deref_after_move() {
std::string_view p;
std::string str;
// ...
p = str;
consume(std::move(str)); // What if small string optimization takes place?
std::cout << p << std::endl;
}
const char *global;
std::string_view global2;
void escape_to_global() {
std::string str;
global = str.c_str();
}
void escape_to_global2() {
std::string str;
global2 = str.c_str();
}
void escape_to_static() {
static const char *stat;
std::string str;
stat = str.c_str();
}
void deref_after_clear() {
const char *p;
std::string str;
p = str.c_str();
str.clear();
std::cout << p << std::endl;
}
void deref_after_clear2() {
std::string_view p;
std::string str;
p = str;
str.clear();
std::cout << p << std::endl;
}
void deref_after_reassign() {
const char *p;
std::string str;
p = str.c_str();
str = "";
std::cout << p << std::endl;
}
void deref_after_reassign2() {
std::string_view p;
std::string str;
p = str;
str = "";
std::cout << p << std::endl;
}
void deref_after_reassign3() {
const char *p;
std::string str;
p = str.c_str();
str.assign(3, 'c');
std::cout << p << std::endl;
}
void deref_after_buffer_realloc() {
const char *p;
std::string str;
p = str.c_str();
str += "foobar";
std::cout << p << std::endl;
}
void deref_after_buffer_realloc2() {
std::string_view p;
std::string str;
p = str;
str += "foobar";
std::cout << p << std::endl;
}
void deref_after_buffer_realloc3() {
std::string_view p;
std::string str;
p = str;
str += ""; // Should we warn if this is noop?
std::cout << p << std::endl;
}
void deref_after_shrink_to_fit() {
const char *p;
std::string str;
p = str.c_str();
str.shrink_to_fit();
std::cout << p << std::endl;
}
void deref_after_reserve() {
const char *p;
std::string str;
p = str.c_str();
str.reserve(50);
std::cout << p << std::endl;
}
void deref_after_insert() {
const char *p;
std::string str;
p = str.c_str();
str.insert(0, 2, 'c');
std::cout << p << std::endl;
}
void deref_after_insert2() {
const char *p;
std::string str;
p = str.c_str();
str.insert(0, 0, 'c'); // Should we warn if this is noop?
std::cout << p << std::endl;
}
void deref_after_push_back() {
const char *p;
std::string str;
p = str.c_str();
str.push_back('c');
std::cout << p << std::endl;
}
void deref_after_replace() {
const char *p;
std::string str;
p = str.c_str();
str.replace(0, 2, "foobar");
std::cout << p << std::endl;
}
void deref_after_resize() {
const char *p;
std::string str;
p = str.c_str();
str.resize(5);
std::cout << p << std::endl;
}
void deref_after_swap() {
const char *p;
std::string str;
std::string str2;
p = str.c_str();
str.swap(str2); // Should we warn?
std::cout << p << std::endl;
}
void deref_after_overwritten() {
const char *p;
std::string str;
p = str.c_str();
std::cin >> str;
std::cout << p << std::endl;
}
void propagate_views() {
std::string_view p;
std::string_view p2;
std::string str;
p = str;
p2 = p.substr(2);
str.clear();
std::cout << p2 << std::endl;
}
void propagate_views2() {
std::string_view p;
std::string_view p2;
std::string str;
p = str;
p2 = p;
str.clear();
std::cout << p2 << std::endl;
}
void propagate_views3() {
std::string_view p;
const char *p2;
std::string str;
p = str;
p2 = p.data();
str.clear();
std::cout << p2 << std::endl;
}
struct EscapeIntoMember {
const char *p;
static const char *stat;
void escape() {
std::string str;
p = str.c_str();
}
void escape_static() {
std::string str;
stat = str.c_str();
}
};
void unknown(std::string &);
void unknown(const char *p);
void deref_after_unknown_ok() {
const char *p;
std::string str;
p = str.c_str();
unknown(str);
std::cout << p << std::endl;
}
void escape_into_unknown() {
const char *p;
std::string str;
p = str.c_str();
str.clear();
unknown(p);
}
void string_object_changed_ok() {
std::string ok;
const char *p;
{
std::string bad;
p = bad.c_str();
// ...
p = ok.c_str();
}
}
void string_object_changed2_ok() {
std::string ok;
std::string_view p;
{
std::string bad;
p = bad;
// ...
p = ok;
}
}
void string_object_changed3_ok() {
std::string_view p;
{
std::string bad;
p = bad;
// ...
p = "literal";
}
}
void string_object_changed4_ok() {
const char *p;
{
std::string bad;
p = bad.c_str();
// ...
p = "literal";
}
}
void consume(std::string_view &);
void consume(const char *&);
void string_object_potentially_changed_ok() {
std::string_view p;
{
std::string bad;
p = bad;
consume(p);
}
}
void string_object_potentially_changed2_ok() {
const char *p;
{
std::string bad;
p = bad.c_str();
consume(p);
}
}
void vector_deref_after_scope() {
const char *p;
{
std::vector<char> v;
// ...
p = v.data();
}
std::cout << p << std::endl;
}

Event Timeline