MCPcopy Create free account
hub / github.com/csmith-project/csmith / CreateArrayVariable

Method CreateArrayVariable

src/ArrayVariable.cpp:125–188  ·  view source on GitHub ↗

=======================================================================================

Source from the content-addressed store, hash-verified

123
124//=======================================================================================
125ArrayVariable *
126ArrayVariable::CreateArrayVariable(const CGContext& cg_context, Block* blk, const std::string &name, const Type *type, const Expression* init, const CVQualifiers* qfer, const Variable* isFieldVarOf)
127{
128 assert(type);
129 if (type->eType == eSimple)
130 assert(type->simple_type != eVoid);
131
132 // quick way to choose a random array dimension: 1d 60%, 2d 30%, and son on
133 int num = rnd_upto(99)+1;
134 ERROR_GUARD(NULL);
135 int dimension = 0;
136 int step = 100;
137 for (; num > 0; num -= step) {
138 dimension++;
139 step /= 2;
140 if (step == 0) step = 1;
141 }
142 if (dimension > CGOptions::max_array_dimensions()) {
143 dimension = CGOptions::max_array_dimensions();
144 }
145 vector<unsigned int> sizes;
146 int total_size = 1;
147 for (int i=0; i<dimension; i++) {
148 unsigned int dimen_size = rnd_upto(CGOptions::max_array_length_per_dimension()) + 1;
149 ERROR_GUARD(NULL);
150 if (total_size * dimen_size > (unsigned int)CGOptions::max_array_length()) {
151 dimen_size = CGOptions::max_array_length() / total_size;
152 }
153 if (dimen_size) {
154 total_size *= dimen_size;
155 sizes.push_back(dimen_size);
156 }
157 }
158 ArrayVariable *var = new ArrayVariable(blk, name, type, init, qfer, sizes, isFieldVarOf);
159 ERROR_GUARD_AND_DEL1(NULL, var);
160 if (type->is_aggregate()) {
161 var->create_field_vars(type);
162 }
163 // create a list of alternative initial values
164 unsigned int init_num = pure_rnd_upto(total_size / 2);
165 if (0) { // keep the code for comparing the bug finding power with the else branch
166 if (type->eType == eSimple || type->eType == eStruct) {
167 unsigned int init_num = pure_rnd_upto(total_size - 1);
168 for (size_t i=0; i<init_num; i++) {
169 Expression* e = Constant::make_random(type);
170 var->add_init_value(e);
171 }
172 }
173 } else {
174 for (size_t i=0; i<init_num; i++) {
175 Expression* e = NULL;
176 if (type->eType != ePointer || CGOptions::strict_const_arrays()) {
177 e = Constant::make_random(type);
178 } else {
179 e = VariableSelector::make_init_value(Effect::READ, cg_context, type, qfer, blk);
180 }
181 var->add_init_value(e);
182 }

Callers

nothing calls this directly

Calls 5

rnd_uptoFunction · 0.85
pure_rnd_uptoFunction · 0.85
create_field_varsMethod · 0.80
add_init_valueMethod · 0.80
is_aggregateMethod · 0.45

Tested by

no test coverage detected