| 1 |
// RUN: %clang_analyze_cc1 %s -verify -analyzer-checker=core |
| 2 |
|
| 3 |
#include "Inputs/system-header-simulator-cxx.h" |
| 4 |
|
| 5 |
|
| 6 |
namespace GH94193 { |
| 7 |
template<typename T> class optional { |
| 8 |
union { |
| 9 |
char x; |
| 10 |
T uvalue; |
| 11 |
}; |
| 12 |
bool holds_value = false; |
| 13 |
public: |
| 14 |
optional() = default; |
| 15 |
optional(const optional&) = delete; |
| 16 |
optional(optional&&) = delete; |
| 17 |
template <typename U = T> explicit optional(U&& value) : holds_value(true) { |
| 18 |
new (static_cast<void*>(std::addressof(uvalue))) T(std::forward<U>(value)); |
| 19 |
} |
| 20 |
optional& operator=(const optional&) = delete; |
| 21 |
optional& operator=(optional&&) = delete; |
| 22 |
explicit operator bool() const { |
| 23 |
return holds_value; |
| 24 |
} |
| 25 |
T& unwrap() & { |
| 26 |
return uvalue; // no-warning: returns a valid value |
| 27 |
} |
| 28 |
}; |
| 29 |
|
| 30 |
int top1(int x) { |
| 31 |
optional<int> opt{x}; // note: Ctor was inlined. |
| 32 |
return opt.unwrap(); // no-warning: returns a valid value |
| 33 |
} |
| 34 |
|
| 35 |
std::string *top2() { |
| 36 |
std::string a = "123"; |
| 37 |
// expected-warning@+2 {{address of stack memory associated with local variable 'a' returned}} diagnosed by -Wreturn-stack-address |
| 38 |
// expected-warning@+1 {{Address of stack memory associated with local variable 'a' returned to caller [core.StackAddressEscape]}} |
| 39 |
return std::addressof(a); |
| 40 |
} |
| 41 |
} // namespace GH94193 |