You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
2.6 KiB

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <string>
struct SimpleRuleKey {
std::string value;
int i;
};
struct s {
int n;
int b;
std::string s;
};
struct s t4; // global struc are init by default
std::string struct_init_ok() {
SimpleRuleKey srk{};
std::string s1;
s1 = srk.value; // srk has been initialized
return s1;
}
std::string struct_uninit_ok() {
SimpleRuleKey srk;
std::string s1;
s1 = srk.value; // srk has not been initialized, but value has type string and
// therefore it is initialized by default
return s1;
}
int struct_uninit_bad() {
int k;
SimpleRuleKey srk;
k = srk.i; // Should reports here: srk was not initialized and i has type int
// so it's not initialized by default
return k;
}
int struct_partially_init_ok() {
struct s t1 = {0}; // partially initialized
int j;
j = t1.b; // when partially initialized, automatically other fields get
// initilized
return j;
}
int global_struct_ok() {
int j;
j = t4.n; // global struct are initilized by default
return j;
}
void init_struct(struct s* z) { z->n = 10; };
int call_init_struct_ok() {
struct s t;
init_struct(&t);
return t.n;
}
int FN_call_init_struct() {
struct s t;
init_struct(&t);
return t.b;
}
struct s init_all_fields(void);
int init_field_via_function_ok() {
struct s t;
t = init_all_fields();
return t.n;
}
int init_field_via_function_ptr_ok() {
struct s* t;
*t = init_all_fields();
return t->n;
}
int get_a_int_pointer(int* x) { return *x; };
void pass_pointer_of_field_OK() {
struct s my_t;
get_a_int_pointer(&my_t.n);
}
int get_an_int(int x){};
void pass_basic_type_field_bad() {
struct s my_t;
get_an_int(my_t.n); // pass an uninit int
}
enum class FieldType : uint8_t {
Stop = 0x0,
True = 0x1,
False = 0x2,
Int8 = 0x3,
Int16 = 0x4,
Int32 = 0x5,
Int64 = 0x6,
Double = 0x7,
Binary = 0x8,
List = 0x9,
Set = 0xa,
Map = 0xb,
Struct = 0xc,
Float = 0xd
};
class C {
int a, b;
public:
std::pair<FieldType, int> read_values();
};
int use_C(C& c) {
const auto pr = c.read_values();
return pr.second;
}
struct s not_a_constructor_but_returning_T(void);
int foo() {
struct s t = not_a_constructor_but_returning_T();
return t.n;
}
short struct_partial_init_bad() {
struct {
int* a;
short* b;
} s;
s.a = 0;
short* p = s.b;
return *p;
}
[clang] fix bad interaction between ConditionalOperator and initializers Summary: This is several inter-connected changes together to keep the tests happy. The ConditionalOperator `b?t:e` is translated by first creating a placeholder variable to temporarily store the result of the evaluation in each branch, then the real thing we want to assign to reads that variable. But, there are situations where that changes the semantics of the expression, namely when the value created is a struct on the stack (eg, a C++ temporary). This is because in SIL we cannot assign the *address* of a program variable, only its contents, so by the time we're out of the conditional operator we cannot set the struct value correctly anymore: we can only set its content, which we did, but that results in a "shifted" struct value that is one dereference away from where it should be. So a batch of changes concern `conditionalOperator_trans`: - instead of systematically creating a temporary for the conditional, use the `trans_state.var_exp_typ` provided from above if available when translating `ConditionalOperator` - don't even set anything if that variable was already initialized by merely translating the branch expression, eg when it's a constructor - fix long-standing TODO to propagate these initialization facts accurately for ConditionalOperator (used by `init_expr_trans` to also figure out if it should insert a store to the variable being initialised or not) The rest of the changes adapt some relevant other constructs to deal with conditionalOperator properly now that it can set the current variable itself, instead of storing stuff inside a temp variable. This change was a problem because some constructs, eg a variable declaration, will insert nodes that set up the variable before calling its initialization, and now the initialization happens *before* that setup, in the translation of the inner conditional operator, which naturally creates nodes above the current one. - add a generic helper to force a sequential order between two translation results, forcing node creation if necessary - use that in `init_expr_trans` and `cxxNewExpr_trans` - adjust many places where `var_exp_typ` was incorrectly not reset when translating sub-expressions The sequentiality business creates more nodes when used, and the conditionalOperator business uses fewer temporary variables, so the frontend results change quite a bit. Note that biabduction tests were invaluable in debugging this. There could be other constructs to adjust similarly to cxxNewExpr that were not covered by the tests though. Added tests in pulse that exercises the previous bug. Reviewed By: da319 Differential Revision: D24796282 fbshipit-source-id: 0790c8d17
4 years ago
struct Header {
uint8_t flags{};
};
int test_switch1(bool b) {
Header h;
h.flags |= b ? 1 : 0;
}