2017-10-12 04:53:01 +08:00
|
|
|
// RUN: %clang_analyze_cc1 -std=c++11 -fblocks -analyzer-checker=core,debug.ExprInspection -verify %s
|
|
|
|
// RUN: %clang_analyze_cc1 -std=c++11 -fblocks -analyzer-checker=core,debug.ExprInspection -DEMULATE_LIBSTDCPP -verify %s
|
2017-09-30 08:03:22 +08:00
|
|
|
|
2017-11-03 08:36:03 +08:00
|
|
|
// We do NOT model libcxx03 implementation, but the analyzer should still
|
|
|
|
// not crash.
|
|
|
|
// RUN: %clang_analyze_cc1 -std=c++11 -fblocks -analyzer-checker=core,debug.ExprInspection -DEMULATE_LIBCXX03 -verify %s
|
|
|
|
// RUN: %clang_analyze_cc1 -std=c++11 -fblocks -analyzer-checker=core,debug.ExprInspection -DEMULATE_LIBCXX03 -DEMULATE_LIBSTDCPP -verify %s
|
|
|
|
|
2017-09-30 08:03:22 +08:00
|
|
|
void clang_analyzer_eval(bool);
|
|
|
|
|
|
|
|
// Faking std::std::call_once implementation.
|
|
|
|
namespace std {
|
2017-10-12 04:53:01 +08:00
|
|
|
|
|
|
|
#ifndef EMULATE_LIBSTDCPP
|
2017-09-30 08:03:22 +08:00
|
|
|
typedef struct once_flag_s {
|
|
|
|
unsigned long __state_ = 0;
|
|
|
|
} once_flag;
|
2017-10-12 04:53:01 +08:00
|
|
|
#else
|
|
|
|
typedef struct once_flag_s {
|
|
|
|
int _M_once = 0;
|
|
|
|
} once_flag;
|
|
|
|
#endif
|
2017-09-30 08:03:22 +08:00
|
|
|
|
2017-11-03 08:36:03 +08:00
|
|
|
#ifndef EMULATE_LIBCXX03
|
2017-09-30 08:03:22 +08:00
|
|
|
template <class Callable, class... Args>
|
2017-10-18 06:28:18 +08:00
|
|
|
void call_once(once_flag &o, Callable&& func, Args&&... args) {};
|
2017-11-03 08:36:03 +08:00
|
|
|
#else
|
|
|
|
template <class Callable, class... Args> // libcxx03 call_once
|
|
|
|
void call_once(once_flag &o, Callable func, Args&&... args) {};
|
|
|
|
#endif
|
2017-10-12 04:53:01 +08:00
|
|
|
|
2017-09-30 08:03:22 +08:00
|
|
|
} // namespace std
|
|
|
|
|
|
|
|
// Check with Lambdas.
|
|
|
|
void test_called_warning() {
|
|
|
|
std::once_flag g_initialize;
|
|
|
|
int z;
|
|
|
|
|
|
|
|
std::call_once(g_initialize, [&] {
|
|
|
|
int *x = nullptr;
|
2017-11-03 08:36:03 +08:00
|
|
|
#ifndef EMULATE_LIBCXX03
|
2017-09-30 08:03:22 +08:00
|
|
|
int y = *x; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}}
|
2017-11-03 08:36:03 +08:00
|
|
|
#endif
|
2017-09-30 08:03:22 +08:00
|
|
|
z = 200;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_called_on_path_inside_no_warning() {
|
|
|
|
std::once_flag g_initialize;
|
|
|
|
|
|
|
|
int *x = nullptr;
|
|
|
|
int y = 100;
|
|
|
|
int z;
|
|
|
|
|
|
|
|
std::call_once(g_initialize, [&] {
|
|
|
|
z = 200;
|
|
|
|
x = &z;
|
|
|
|
});
|
|
|
|
|
2017-11-03 08:36:03 +08:00
|
|
|
#ifndef EMULATE_LIBCXX03
|
2017-09-30 08:03:22 +08:00
|
|
|
*x = 100; // no-warning
|
|
|
|
clang_analyzer_eval(z == 100); // expected-warning{{TRUE}}
|
2017-11-03 08:36:03 +08:00
|
|
|
#endif
|
2017-09-30 08:03:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_called_on_path_no_warning() {
|
|
|
|
std::once_flag g_initialize;
|
|
|
|
|
|
|
|
int *x = nullptr;
|
|
|
|
int y = 100;
|
|
|
|
|
|
|
|
std::call_once(g_initialize, [&] {
|
|
|
|
x = &y;
|
|
|
|
});
|
|
|
|
|
2017-11-03 08:36:03 +08:00
|
|
|
#ifndef EMULATE_LIBCXX03
|
2017-09-30 08:03:22 +08:00
|
|
|
*x = 100; // no-warning
|
2017-11-03 08:36:03 +08:00
|
|
|
#else
|
|
|
|
*x = 100; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}}
|
|
|
|
#endif
|
2017-09-30 08:03:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_called_on_path_warning() {
|
|
|
|
std::once_flag g_initialize;
|
|
|
|
|
|
|
|
int y = 100;
|
|
|
|
int *x = &y;
|
|
|
|
|
|
|
|
std::call_once(g_initialize, [&] {
|
|
|
|
x = nullptr;
|
|
|
|
});
|
|
|
|
|
2017-11-03 08:36:03 +08:00
|
|
|
#ifndef EMULATE_LIBCXX03
|
2017-09-30 08:03:22 +08:00
|
|
|
*x = 100; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}}
|
2017-11-03 08:36:03 +08:00
|
|
|
#endif
|
2017-09-30 08:03:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_called_once_warning() {
|
|
|
|
std::once_flag g_initialize;
|
|
|
|
|
|
|
|
int *x = nullptr;
|
|
|
|
int y = 100;
|
|
|
|
|
|
|
|
std::call_once(g_initialize, [&] {
|
|
|
|
x = nullptr;
|
|
|
|
});
|
|
|
|
|
|
|
|
std::call_once(g_initialize, [&] {
|
|
|
|
x = &y;
|
|
|
|
});
|
|
|
|
|
2017-11-03 08:36:03 +08:00
|
|
|
#ifndef EMULATE_LIBCXX03
|
2017-09-30 08:03:22 +08:00
|
|
|
*x = 100; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}}
|
2017-11-03 08:36:03 +08:00
|
|
|
#endif
|
2017-09-30 08:03:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_called_once_no_warning() {
|
|
|
|
std::once_flag g_initialize;
|
|
|
|
|
|
|
|
int *x = nullptr;
|
|
|
|
int y = 100;
|
|
|
|
|
|
|
|
std::call_once(g_initialize, [&] {
|
|
|
|
x = &y;
|
|
|
|
});
|
|
|
|
|
|
|
|
std::call_once(g_initialize, [&] {
|
|
|
|
x = nullptr;
|
|
|
|
});
|
|
|
|
|
2017-11-03 08:36:03 +08:00
|
|
|
#ifndef EMULATE_LIBCXX03
|
2017-09-30 08:03:22 +08:00
|
|
|
*x = 100; // no-warning
|
2017-11-03 08:36:03 +08:00
|
|
|
#endif
|
2017-09-30 08:03:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int global = 0;
|
|
|
|
void funcPointer() {
|
|
|
|
global = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_func_pointers() {
|
|
|
|
static std::once_flag flag;
|
|
|
|
std::call_once(flag, &funcPointer);
|
2017-11-03 08:36:03 +08:00
|
|
|
#ifndef EMULATE_LIBCXX03
|
2017-09-30 08:03:22 +08:00
|
|
|
clang_analyzer_eval(global == 1); // expected-warning{{TRUE}}
|
2017-11-03 08:36:03 +08:00
|
|
|
#endif
|
2017-09-30 08:03:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Fp>
|
|
|
|
class function; // undefined
|
|
|
|
template <class _Rp, class... _ArgTypes>
|
|
|
|
struct function<_Rp(_ArgTypes...)> {
|
2017-09-30 09:15:35 +08:00
|
|
|
_Rp operator()(_ArgTypes...) const {};
|
2017-09-30 08:03:22 +08:00
|
|
|
template <class _Fp>
|
2017-09-30 09:15:35 +08:00
|
|
|
function(_Fp) {};
|
2017-09-30 08:03:22 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Note: currently we do not support calls to std::function,
|
|
|
|
// but the analyzer should not crash either.
|
|
|
|
void test_function_objects_warning() {
|
|
|
|
int x = 0;
|
|
|
|
int *y = &x;
|
|
|
|
|
|
|
|
std::once_flag flag;
|
|
|
|
|
|
|
|
function<void()> func = [&]() {
|
|
|
|
y = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::call_once(flag, func);
|
|
|
|
|
|
|
|
func();
|
|
|
|
int z = *y;
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_param_passing_lambda() {
|
|
|
|
std::once_flag flag;
|
|
|
|
int x = 120;
|
|
|
|
int y = 0;
|
|
|
|
|
|
|
|
std::call_once(flag, [&](int p) {
|
|
|
|
y = p;
|
|
|
|
},
|
|
|
|
x);
|
|
|
|
|
2017-11-03 08:36:03 +08:00
|
|
|
#ifndef EMULATE_LIBCXX03
|
2017-09-30 08:03:22 +08:00
|
|
|
clang_analyzer_eval(y == 120); // expected-warning{{TRUE}}
|
2017-11-03 08:36:03 +08:00
|
|
|
#endif
|
2017-09-30 08:03:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_param_passing_lambda_false() {
|
|
|
|
std::once_flag flag;
|
|
|
|
int x = 120;
|
|
|
|
|
|
|
|
std::call_once(flag, [&](int p) {
|
|
|
|
x = 0;
|
|
|
|
},
|
|
|
|
x);
|
|
|
|
|
2017-11-03 08:36:03 +08:00
|
|
|
#ifndef EMULATE_LIBCXX03
|
2017-09-30 08:03:22 +08:00
|
|
|
clang_analyzer_eval(x == 120); // expected-warning{{FALSE}}
|
2017-11-03 08:36:03 +08:00
|
|
|
#endif
|
2017-09-30 08:03:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_param_passing_stored_lambda() {
|
|
|
|
std::once_flag flag;
|
|
|
|
int x = 120;
|
|
|
|
int y = 0;
|
|
|
|
|
|
|
|
auto lambda = [&](int p) {
|
|
|
|
y = p;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::call_once(flag, lambda, x);
|
2017-11-03 08:36:03 +08:00
|
|
|
#ifndef EMULATE_LIBCXX03
|
2017-09-30 08:03:22 +08:00
|
|
|
clang_analyzer_eval(y == 120); // expected-warning{{TRUE}}
|
2017-11-03 08:36:03 +08:00
|
|
|
#endif
|
2017-09-30 08:03:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_multiparam_passing_lambda() {
|
|
|
|
std::once_flag flag;
|
|
|
|
int x = 120;
|
|
|
|
|
|
|
|
std::call_once(flag, [&](int a, int b, int c) {
|
|
|
|
x = a + b + c;
|
|
|
|
},
|
|
|
|
1, 2, 3);
|
|
|
|
|
2017-11-03 08:36:03 +08:00
|
|
|
#ifndef EMULATE_LIBCXX03
|
2017-09-30 08:03:22 +08:00
|
|
|
clang_analyzer_eval(x == 120); // expected-warning{{FALSE}}
|
|
|
|
clang_analyzer_eval(x == 6); // expected-warning{{TRUE}}
|
2017-11-03 08:36:03 +08:00
|
|
|
#endif
|
2017-09-30 08:03:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int global2 = 0;
|
|
|
|
void test_param_passing_lambda_global() {
|
|
|
|
std::once_flag flag;
|
|
|
|
global2 = 0;
|
|
|
|
std::call_once(flag, [&](int a, int b, int c) {
|
|
|
|
global2 = a + b + c;
|
|
|
|
},
|
|
|
|
1, 2, 3);
|
2017-11-03 08:36:03 +08:00
|
|
|
#ifndef EMULATE_LIBCXX03
|
2017-09-30 08:03:22 +08:00
|
|
|
clang_analyzer_eval(global2 == 6); // expected-warning{{TRUE}}
|
2017-11-03 08:36:03 +08:00
|
|
|
#endif
|
2017-09-30 08:03:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int global3 = 0;
|
|
|
|
void funcptr(int a, int b, int c) {
|
|
|
|
global3 = a + b + c;
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_param_passing_funcptr() {
|
|
|
|
std::once_flag flag;
|
|
|
|
global3 = 0;
|
|
|
|
|
|
|
|
std::call_once(flag, &funcptr, 1, 2, 3);
|
|
|
|
|
2017-11-03 08:36:03 +08:00
|
|
|
#ifndef EMULATE_LIBCXX03
|
2017-09-30 08:03:22 +08:00
|
|
|
clang_analyzer_eval(global3 == 6); // expected-warning{{TRUE}}
|
2017-11-03 08:36:03 +08:00
|
|
|
#endif
|
2017-09-30 08:03:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_blocks() {
|
|
|
|
global3 = 0;
|
|
|
|
std::once_flag flag;
|
|
|
|
std::call_once(flag, ^{
|
|
|
|
global3 = 120;
|
|
|
|
});
|
2017-11-03 08:36:03 +08:00
|
|
|
#ifndef EMULATE_LIBCXX03
|
2017-09-30 08:03:22 +08:00
|
|
|
clang_analyzer_eval(global3 == 120); // expected-warning{{TRUE}}
|
2017-11-03 08:36:03 +08:00
|
|
|
#endif
|
2017-09-30 08:03:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int call_once() {
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_non_std_call_once() {
|
|
|
|
int x = call_once();
|
2017-11-03 08:36:03 +08:00
|
|
|
#ifndef EMULATE_LIBCXX03
|
2017-09-30 08:03:22 +08:00
|
|
|
clang_analyzer_eval(x == 5); // expected-warning{{TRUE}}
|
2017-11-03 08:36:03 +08:00
|
|
|
#endif
|
2017-09-30 08:03:22 +08:00
|
|
|
}
|
2017-10-10 07:20:46 +08:00
|
|
|
|
|
|
|
namespace std {
|
|
|
|
template <typename d, typename e>
|
|
|
|
void call_once(d, e);
|
|
|
|
}
|
|
|
|
void g();
|
|
|
|
void test_no_segfault_on_different_impl() {
|
2017-11-03 08:36:03 +08:00
|
|
|
#ifndef EMULATE_LIBCXX03
|
2017-10-10 07:20:46 +08:00
|
|
|
std::call_once(g, false); // no-warning
|
2017-11-03 08:36:03 +08:00
|
|
|
#endif
|
2017-10-10 07:20:46 +08:00
|
|
|
}
|
2017-10-21 07:29:59 +08:00
|
|
|
|
|
|
|
void test_lambda_refcapture() {
|
|
|
|
static std::once_flag flag;
|
|
|
|
int a = 6;
|
|
|
|
std::call_once(flag, [&](int &a) { a = 42; }, a);
|
2017-11-03 08:36:03 +08:00
|
|
|
#ifndef EMULATE_LIBCXX03
|
2017-10-21 07:29:59 +08:00
|
|
|
clang_analyzer_eval(a == 42); // expected-warning{{TRUE}}
|
2017-11-03 08:36:03 +08:00
|
|
|
#endif
|
2017-10-21 07:29:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_lambda_refcapture2() {
|
|
|
|
static std::once_flag flag;
|
|
|
|
int a = 6;
|
|
|
|
std::call_once(flag, [=](int &a) { a = 42; }, a);
|
2017-11-03 08:36:03 +08:00
|
|
|
#ifndef EMULATE_LIBCXX03
|
2017-10-21 07:29:59 +08:00
|
|
|
clang_analyzer_eval(a == 42); // expected-warning{{TRUE}}
|
2017-11-03 08:36:03 +08:00
|
|
|
#endif
|
2017-10-21 07:29:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_lambda_fail_refcapture() {
|
|
|
|
static std::once_flag flag;
|
|
|
|
int a = 6;
|
|
|
|
std::call_once(flag, [=](int a) { a = 42; }, a);
|
2017-11-03 08:36:03 +08:00
|
|
|
#ifndef EMULATE_LIBCXX03
|
2017-10-21 07:29:59 +08:00
|
|
|
clang_analyzer_eval(a == 42); // expected-warning{{FALSE}}
|
2017-11-03 08:36:03 +08:00
|
|
|
#endif
|
2017-10-21 07:29:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void mutator(int ¶m) {
|
|
|
|
param = 42;
|
|
|
|
}
|
|
|
|
void test_reftypes_funcptr() {
|
|
|
|
static std::once_flag flag;
|
|
|
|
int a = 6;
|
|
|
|
std::call_once(flag, &mutator, a);
|
2017-11-03 08:36:03 +08:00
|
|
|
#ifndef EMULATE_LIBCXX03
|
2017-10-21 07:29:59 +08:00
|
|
|
clang_analyzer_eval(a == 42); // expected-warning{{TRUE}}
|
2017-11-03 08:36:03 +08:00
|
|
|
#endif
|
2017-10-21 07:29:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void fail_mutator(int param) {
|
|
|
|
param = 42;
|
|
|
|
}
|
|
|
|
void test_mutator_noref() {
|
|
|
|
static std::once_flag flag;
|
|
|
|
int a = 6;
|
|
|
|
std::call_once(flag, &fail_mutator, a);
|
2017-11-03 08:36:03 +08:00
|
|
|
#ifndef EMULATE_LIBCXX03
|
2017-10-21 07:29:59 +08:00
|
|
|
clang_analyzer_eval(a == 42); // expected-warning{{FALSE}}
|
2017-11-03 08:36:03 +08:00
|
|
|
#endif
|
2017-10-21 07:29:59 +08:00
|
|
|
}
|
2017-10-24 08:13:18 +08:00
|
|
|
|
|
|
|
// Function is implicitly treated as a function pointer
|
|
|
|
// even when an ampersand is not explicitly set.
|
|
|
|
void callbackn(int ¶m) {
|
|
|
|
param = 42;
|
2017-10-25 08:03:45 +08:00
|
|
|
}
|
2017-10-24 08:13:18 +08:00
|
|
|
void test_implicit_funcptr() {
|
|
|
|
int x = 0;
|
|
|
|
static std::once_flag flagn;
|
|
|
|
|
|
|
|
std::call_once(flagn, callbackn, x);
|
2017-11-03 08:36:03 +08:00
|
|
|
#ifndef EMULATE_LIBCXX03
|
2017-10-24 08:13:18 +08:00
|
|
|
clang_analyzer_eval(x == 42); // expected-warning{{TRUE}}
|
2017-11-03 08:36:03 +08:00
|
|
|
#endif
|
2017-10-24 08:13:18 +08:00
|
|
|
}
|