diff --git a/src/Bookkeeper.cpp b/src/Bookkeeper.cpp old mode 100644 new mode 100755 index b4a541a15..80d706581 --- a/src/Bookkeeper.cpp +++ b/src/Bookkeeper.cpp @@ -3,6 +3,9 @@ // Copyright (c) 2007, 2008, 2010, 2011, 2013 The University of Utah // All rights reserved. // +// Copyright (c) 2015-2016 Huawei Technologies Co., Ltd +// All rights reserved. +// // This file is part of `csmith', a random generator of C programs. // // Redistribution and use in source and binary forms, with or without @@ -278,7 +281,7 @@ Bookkeeper::output_pointer_statistics(std::ostream &out) total_has_null_ptr++; } const Variable* var = ptrs[i]; - const Type* t = var->type; + const PointerType * t = dynamic_cast(var->type); assert(t->eType == ePointer); if (t->get_indirect_level() > 1) { point_to_pointer++; @@ -487,10 +490,10 @@ Bookkeeper::record_vars_with_bitfields(const Type *type) } void -Bookkeeper::record_type_with_bitfields(const Type *typ) +Bookkeeper::record_type_with_bitfields(const Type *type) { - if (!typ->is_aggregate()) return; - + if (!type->is_aggregate()) return; + const AggregateType * typ = dynamic_cast(type); if (typ->has_bitfields()) { Bookkeeper::structs_with_bitfields++; size_t len = typ->bitfields_length_.size(); diff --git a/src/CVQualifiers.cpp b/src/CVQualifiers.cpp old mode 100644 new mode 100755 index f5a20ee65..607fd8c37 --- a/src/CVQualifiers.cpp +++ b/src/CVQualifiers.cpp @@ -3,6 +3,9 @@ // Copyright (c) 2007, 2008, 2009, 2010, 2011, 2013 The University of Utah // All rights reserved. // +// Copyright (c) 2015-2016 Huawei Technologies Co., Ltd +// All rights reserved. +// // This file is part of `csmith', a random generator of C programs. // // Redistribution and use in source and binary forms, with or without @@ -31,6 +34,7 @@ #include #include "CVQualifiers.h" #include "Type.h" +#include "PointerType.h" #include "Effect.h" #include "CGContext.h" #include "CGOptions.h" @@ -290,7 +294,7 @@ CVQualifiers::random_qualifiers(const Type* t, Effect::Access access, const CGCo const Effect &effect_context = cg_context.get_effect_context(); // set random volatile/const properties for each level of indirection for pointers - const Type* tmp = t->ptr_type; + const Type* tmp = (t->eType == ePointer) ? (dynamic_cast(t))->ptr_type : NULL; while (tmp) { isVolatile = rnd_flipcoin(volatile_prob); isConst = rnd_flipcoin(const_prob); @@ -299,7 +303,7 @@ CVQualifiers::random_qualifiers(const Type* t, Effect::Access access, const CGCo } is_consts.push_back(isConst); is_volatiles.push_back(isVolatile); - tmp = tmp->ptr_type; + tmp = (tmp->eType == ePointer) ? (dynamic_cast(tmp))->ptr_type : NULL; } // set random volatile/const properties for variable itself bool volatile_ok = effect_context.is_side_effect_free(); diff --git a/src/Constant.cpp b/src/Constant.cpp old mode 100644 new mode 100755 index c580cc51a..479677f51 --- a/src/Constant.cpp +++ b/src/Constant.cpp @@ -3,6 +3,9 @@ // Copyright (c) 2007, 2008, 2009, 2010, 2011, 2013, 2014 The University of Utah // All rights reserved. // +// Copyright (c) 2015-2016 Huawei Technologies Co., Ltd +// All rights reserved. +// // This file is part of `csmith', a random generator of C programs. // // Redistribution and use in source and binary forms, with or without @@ -241,11 +244,12 @@ GenerateRandomConstantInRange(const Type* type, int bound) * "{2, 4, {2, 4}, 6.0}" *************************************************************/ static string -GenerateRandomStructConstant(const Type* type) +GenerateRandomStructConstant(const Type* t) { string value = "{"; size_t i; - assert(type->eType == eStruct); + assert(t->eType == eStruct); + const AggregateType * type = dynamic_cast(t); assert(type->fields.size() == type->bitfields_length_.size()); for (i = 0; i < type->fields.size(); i++) { @@ -277,10 +281,12 @@ GenerateRandomStructConstant(const Type* type) the first field is enough *************************************************************/ static string -GenerateRandomUnionConstant(const Type* type) +GenerateRandomUnionConstant(const Type* t) { string value = "{"; - assert(type->eType == eUnion && type->fields.size() == type->bitfields_length_.size()); + assert (t->eType == eUnion); + const AggregateType * type = dynamic_cast(t); + assert(type->fields.size() == type->bitfields_length_.size()); value += GenerateRandomConstant(type->fields[0]); value += "}"; return value; diff --git a/src/FactUnion.cpp b/src/FactUnion.cpp old mode 100644 new mode 100755 index 5f6518e56..d84051777 --- a/src/FactUnion.cpp +++ b/src/FactUnion.cpp @@ -3,6 +3,9 @@ // Copyright (c) 2007, 2008, 2009, 2010, 2011 The University of Utah // All rights reserved. // +// Copyright (c) 2015-2016 Huawei Technologies Co., Ltd +// All rights reserved. +// // This file is part of `csmith', a random generator of C programs. // // Redistribution and use in source and binary forms, with or without @@ -32,6 +35,7 @@ #include "CGOptions.h" #include "Fact.h" #include "Type.h" +#include "AggregateType.h" #include "Common.h" #include "Function.h" #include "FactMgr.h" @@ -289,7 +293,7 @@ FactUnion::imply(const Fact& f) const bool FactUnion::is_field_readable(const Variable* v, int fid, const vector& facts) { - assert(v->type->eType == eUnion && fid >=0 && fid < (int)(v->type->fields.size())); + assert(v->type->eType == eUnion && fid >=0 && fid < (int)((dynamic_cast(v->type))->fields.size())); FactUnion tmp(v, fid); const FactUnion* fu = dynamic_cast(find_related_fact(facts, &tmp)); return (fu && tmp.imply(*fu)) ; diff --git a/src/Function.cpp b/src/Function.cpp old mode 100644 new mode 100755 index 6c9f49f0d..bb45a0c38 --- a/src/Function.cpp +++ b/src/Function.cpp @@ -3,6 +3,9 @@ // Copyright (c) 2007, 2008, 2010, 2011, 2013, 2014 The University of Utah // All rights reserved. // +// Copyright (c) 2015-2016 Huawei Technologies Co., Ltd +// All rights reserved. +// // This file is part of `csmith', a random generator of C programs. // // Redistribution and use in source and binary forms, with or without @@ -575,7 +578,7 @@ Function::GenerateBody(const CGContext &prev_context) cg_context.extend_call_chain(prev_context); FactMgr* fm = get_fact_mgr_for_func(this); for (size_t i=0; itype->ptr_type != 0) { + if (params[i]->type->eType == ePointer) { fm->global_facts.push_back(FactPointTo::make_fact(params[i], FactPointTo::tbd_ptr)); } } diff --git a/src/FunctionInvocation.cpp b/src/FunctionInvocation.cpp old mode 100644 new mode 100755 index cfe93c62d..5cbe41071 --- a/src/FunctionInvocation.cpp +++ b/src/FunctionInvocation.cpp @@ -3,6 +3,9 @@ // Copyright (c) 2007, 2008, 2009, 2010, 2011, 2013, 2014 The University of Utah // All rights reserved. // +// Copyright (c) 2015-2016 Huawei Technologies Co., Ltd +// All rights reserved. +// // This file is part of `csmith', a random generator of C programs. // // Redistribution and use in source and binary forms, with or without @@ -160,7 +163,7 @@ FunctionInvocation::make_random_unary(CGContext &cg_context, const Type* type) FunctionInvocation * FunctionInvocation::make_random_binary(CGContext &cg_context, const Type* type) { - if (rnd_flipcoin(10) && Type::has_pointer_type()) { + if (rnd_flipcoin(10) && PointerType::has_pointer_type()) { return make_random_binary_ptr_comparison(cg_context); } @@ -270,7 +273,7 @@ FunctionInvocation::make_random_binary_ptr_comparison(CGContext &cg_context) SafeOpFlags *flags = SafeOpFlags::make_random_binary(get_int_type(), NULL, NULL, sOpBinary, op); FunctionInvocation *fi = FunctionInvocationBinary::CreateFunctionInvocationBinary(cg_context, op, flags); - const Type* type = Type::choose_random_pointer_type(); + const Type* type = PointerType::choose_random_pointer_type(); Effect lhs_eff_accum; CGContext lhs_cg_context(cg_context, cg_context.get_effect_context(), &lhs_eff_accum); diff --git a/src/Makefile.in b/src/Makefile.in old mode 100644 new mode 100755 index b79489d52..0caf830e3 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -97,6 +97,7 @@ am_csmith_OBJECTS = csmith-AbsExtension.$(OBJEXT) \ csmith-LinearSequence.$(OBJEXT) csmith-MspFilters.$(OBJEXT) \ csmith-OutputMgr.$(OBJEXT) csmith-PartialExpander.$(OBJEXT) \ csmith-Probabilities.$(OBJEXT) csmith-RandomNumber.$(OBJEXT) \ + csmith-Parameter.$(OBJEXT) \ csmith-RandomProgramGenerator.$(OBJEXT) \ csmith-SafeOpFlags.$(OBJEXT) csmith-Sequence.$(OBJEXT) \ csmith-SequenceFactory.$(OBJEXT) \ @@ -108,7 +109,8 @@ am_csmith_OBJECTS = csmith-AbsExtension.$(OBJEXT) \ csmith-StatementExpr.$(OBJEXT) csmith-StatementFor.$(OBJEXT) \ csmith-StatementGoto.$(OBJEXT) csmith-StatementIf.$(OBJEXT) \ csmith-StatementReturn.$(OBJEXT) csmith-StringUtils.$(OBJEXT) \ - csmith-Type.$(OBJEXT) csmith-Variable.$(OBJEXT) \ + csmith-Type.$(OBJEXT) csmith-PointerType.$(OBJEXT) \ + csmith-AggregateType.$(OBJEXT) csmith-Variable.$(OBJEXT) \ csmith-VariableSelector.$(OBJEXT) \ csmith-VectorFilter.$(OBJEXT) csmith-platform.$(OBJEXT) \ csmith-random.$(OBJEXT) csmith-util.$(OBJEXT) @@ -361,6 +363,8 @@ csmith_SOURCES = \ MspFilters.h \ OutputMgr.cpp \ OutputMgr.h \ + Parameter.cpp \ + Parameter.h \ PartialExpander.cpp \ PartialExpander.h \ Probabilities.cpp \ @@ -402,6 +406,10 @@ csmith_SOURCES = \ StringUtils.h \ Type.cpp \ Type.h \ + PointerType.cpp \ + PointerType.h \ + AggregateType.cpp \ + AggregateType.h \ Variable.cpp \ Variable.h \ VariableSelector.cpp \ @@ -562,6 +570,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/csmith-MspFilters.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/csmith-OutputMgr.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/csmith-PartialExpander.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/csmith-Parameter.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/csmith-Probabilities.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/csmith-RandomNumber.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/csmith-RandomProgramGenerator.Po@am__quote@ @@ -581,6 +590,8 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/csmith-StatementReturn.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/csmith-StringUtils.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/csmith-Type.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/csmith-PointerType.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/csmith-AggregateType.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/csmith-Variable.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/csmith-VariableSelector.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/csmith-VectorFilter.Po@am__quote@ @@ -1463,6 +1474,48 @@ csmith-Type.obj: Type.cpp @AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(csmith_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o csmith-Type.obj `if test -f 'Type.cpp'; then $(CYGPATH_W) 'Type.cpp'; else $(CYGPATH_W) '$(srcdir)/Type.cpp'; fi` +csmith-Parameter.o: Parameter.cpp +@am__fastdepCXX_TRUE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(csmith_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT csmith-Parameter.o -MD -MP -MF $(DEPDIR)/csmith-Parameter.Tpo -c -o csmith-Parameter.o `test -f 'Parameter.cpp' || echo '$(srcdir)/'`Parameter.cpp +@am__fastdepCXX_TRUE@ $(am__mv) $(DEPDIR)/csmith-Parameter.Tpo $(DEPDIR)/csmith-Parameter.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='Parameter.cpp' object='csmith-Parameter.o' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(csmith_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o csmith-Parameter.o `test -f 'Parameter.cpp' || echo '$(srcdir)/'`Parameter.cpp + +csmith-Parameter.obj: Parameter.cpp +@am__fastdepCXX_TRUE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(csmith_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT csmith-Parameter.obj -MD -MP -MF $(DEPDIR)/csmith-Parameter.Tpo -c -o csmith-Parameter.obj `if test -f 'Parameter.cpp'; then $(CYGPATH_W) 'Parameter.cpp'; else $(CYGPATH_W) '$(srcdir)/Parameter.cpp'; fi` +@am__fastdepCXX_TRUE@ $(am__mv) $(DEPDIR)/csmith-Parameter.Tpo $(DEPDIR)/csmith-Parameter.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='Parameter.cpp' object='csmith-Parameter.obj' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(csmith_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o csmith-Parameter.obj `if test -f 'Parameter.cpp'; then $(CYGPATH_W) 'Parameter.cpp'; else $(CYGPATH_W) '$(srcdir)/Parameter.cpp'; fi` + +csmith-PointerType.o: PointerType.cpp +@am__fastdepCXX_TRUE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(csmith_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT csmith-PointerType.o -MD -MP -MF $(DEPDIR)/csmith-PointerType.Tpo -c -o csmith-PointerType.o `test -f 'PointerType.cpp' || echo '$(srcdir)/'`PointerType.cpp +@am__fastdepCXX_TRUE@ $(am__mv) $(DEPDIR)/csmith-PointerType.Tpo $(DEPDIR)/csmith-PointerType.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='PointerType.cpp' object='csmith-PointerType.o' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(csmith_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o csmith-PointerType.o `test -f 'PointerType.cpp' || echo '$(srcdir)/'`PointerType.cpp + +csmith-PointerType.obj: PointerType.cpp +@am__fastdepCXX_TRUE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(csmith_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT csmith-PointerType.obj -MD -MP -MF $(DEPDIR)/csmith-PointerType.Tpo -c -o csmith-PointerType.obj `if test -f 'PointerType.cpp'; then $(CYGPATH_W) 'PointerType.cpp'; else $(CYGPATH_W) '$(srcdir)/PointerType.cpp'; fi` +@am__fastdepCXX_TRUE@ $(am__mv) $(DEPDIR)/csmith-PointerType.Tpo $(DEPDIR)/csmith-PointerType.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='PointerType.cpp' object='csmith-PointerType.obj' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(csmith_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o csmith-PointerType.obj `if test -f 'PointerType.cpp'; then $(CYGPATH_W) 'PointerType.cpp'; else $(CYGPATH_W) '$(srcdir)/PointerType.cpp'; fi` + +csmith-AggregateType.o: AggregateType.cpp +@am__fastdepCXX_TRUE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(csmith_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT csmith-AggregateType.o -MD -MP -MF $(DEPDIR)/csmith-AggregateType.Tpo -c -o csmith-AggregateType.o `test -f 'AggregateType.cpp' || echo '$(srcdir)/'`AggregateType.cpp +@am__fastdepCXX_TRUE@ $(am__mv) $(DEPDIR)/csmith-AggregateType.Tpo $(DEPDIR)/csmith-AggregateType.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='AggregateType.cpp' object='csmith-AggregateType.o' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(csmith_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o csmith-AggregateType.o `test -f 'AggregateType.cpp' || echo '$(srcdir)/'`AggregateType.cpp + +csmith-AggregateType.obj: AggregateType.cpp +@am__fastdepCXX_TRUE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(csmith_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT csmith-AggregateType.obj -MD -MP -MF $(DEPDIR)/csmith-AggregateType.Tpo -c -o csmith-AggregateType.obj `if test -f 'AggregateType.cpp'; then $(CYGPATH_W) 'AggregateType.cpp'; else $(CYGPATH_W) '$(srcdir)/AggregateType.cpp'; fi` +@am__fastdepCXX_TRUE@ $(am__mv) $(DEPDIR)/csmith-AggregateType.Tpo $(DEPDIR)/csmith-AggregateType.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='AggregateType.cpp' object='csmith-AggregateType.obj' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(csmith_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o csmith-AggregateType.obj `if test -f 'AggregateType.cpp'; then $(CYGPATH_W) 'AggregateType.cpp'; else $(CYGPATH_W) '$(srcdir)/AggregateType.cpp'; fi` + csmith-Variable.o: Variable.cpp @am__fastdepCXX_TRUE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(csmith_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT csmith-Variable.o -MD -MP -MF $(DEPDIR)/csmith-Variable.Tpo -c -o csmith-Variable.o `test -f 'Variable.cpp' || echo '$(srcdir)/'`Variable.cpp @am__fastdepCXX_TRUE@ $(am__mv) $(DEPDIR)/csmith-Variable.Tpo $(DEPDIR)/csmith-Variable.Po diff --git a/src/Parameter.cpp b/src/Parameter.cpp old mode 100644 new mode 100755 index ebbc1b919..1c57e1cba --- a/src/Parameter.cpp +++ b/src/Parameter.cpp @@ -3,6 +3,9 @@ // Copyright (c) 2015-2016 Xuejun Yang // All rights reserved. // +// Copyright (c) 2015-2016 Huawei Technologies Co., Ltd +// All rights reserved. +// // This file is part of `csmith', a random generator of C programs. // // Redistribution and use in source and binary forms, with or without @@ -139,8 +142,8 @@ Parameter::GenerateParameter(Function &curFunc) // Add this type to our parameter list. const Type* t = 0; bool rnd = rnd_flipcoin(40); - if (Type::has_pointer_type() && rnd) { - t= Type::choose_random_pointer_type(); + if (PointerType::has_pointer_type() && rnd) { + t= PointerType::choose_random_pointer_type(); } else { t = Type::choose_random_nonvoid_nonvolatile(); diff --git a/src/Type.cpp b/src/Type.cpp old mode 100644 new mode 100755 index 6e09d105a..2ebca42ba --- a/src/Type.cpp +++ b/src/Type.cpp @@ -3,6 +3,9 @@ // Copyright (c) 2007, 2008, 2010, 2011, 2013, 2014 The University of Utah // All rights reserved. // +// Copyright (c) 2015-2016 Huawei Technologies Co., Ltd +// All rights reserved. +// // This file is part of `csmith', a random generator of C programs. // // Redistribution and use in source and binary forms, with or without @@ -37,6 +40,8 @@ // #include "Type.h" +#include "PointerType.h" +#include "AggregateType.h" #include #include #include @@ -62,186 +67,7 @@ Type *Type::void_type = NULL; // --------------------------------------------------------------------- // List of all types used in the program -static vector AllTypes; -static vector derived_types; - -////////////////////////////////////////////////////////////////////// -class NonVoidTypeFilter : public Filter -{ -public: - NonVoidTypeFilter(); - - virtual ~NonVoidTypeFilter(); - - virtual bool filter(int v) const; - - Type *get_type(); - -private: - mutable Type *typ_; - -}; - -NonVoidTypeFilter::NonVoidTypeFilter() - : typ_(NULL) -{ - -} - -NonVoidTypeFilter::~NonVoidTypeFilter() -{ - -} - -bool -NonVoidTypeFilter::filter(int v) const -{ - assert(static_cast(v) < AllTypes.size()); - Type *type = AllTypes[v]; - if (type->eType == eSimple && type->simple_type == eVoid) - return true; - - if (!type->used) { - Bookkeeper::record_type_with_bitfields(type); - type->used = true; - } - - typ_ = type; - if (type->eType == eSimple) { - Filter *filter = SIMPLE_TYPES_PROB_FILTER; - return filter->filter(typ_->simple_type); - } - - return false; -} - -Type * -NonVoidTypeFilter::get_type() -{ - assert(typ_); - return typ_; -} - -class NonVoidNonVolatileTypeFilter : public Filter -{ -public: - NonVoidNonVolatileTypeFilter(); - - virtual ~NonVoidNonVolatileTypeFilter(); - - virtual bool filter(int v) const; - - Type *get_type(); - -private: - mutable Type *typ_; - -}; - -NonVoidNonVolatileTypeFilter::NonVoidNonVolatileTypeFilter() - : typ_(NULL) -{ - -} - -NonVoidNonVolatileTypeFilter::~NonVoidNonVolatileTypeFilter() -{ - -} - -bool -NonVoidNonVolatileTypeFilter::filter(int v) const -{ - assert(static_cast(v) < AllTypes.size()); - Type *type = AllTypes[v]; - if (type->eType == eSimple && type->simple_type == eVoid) - return true; - - if (type->is_aggregate() && type->is_volatile_struct_union()) - return true; - - if ((type->eType == eStruct) && (!CGOptions::arg_structs())) { - return true; - } - - if ((type->eType == eUnion) && (!CGOptions::arg_unions())) { - return true; - } - - if (!type->used) { - Bookkeeper::record_type_with_bitfields(type); - type->used = true; - } - - typ_ = type; - if (type->eType == eSimple) { - Filter *filter = SIMPLE_TYPES_PROB_FILTER; - return filter->filter(typ_->simple_type); - } - - return false; -} - -Type * -NonVoidNonVolatileTypeFilter::get_type() -{ - assert(typ_); - return typ_; -} - -class ChooseRandomTypeFilter : public Filter -{ -public: - ChooseRandomTypeFilter(bool for_field_var); - - virtual ~ChooseRandomTypeFilter(); - - virtual bool filter(int v) const; - - Type *get_type(); - - bool for_field_var_; - -private: - mutable Type *typ_; -}; - -ChooseRandomTypeFilter::ChooseRandomTypeFilter(bool for_field_var) - : for_field_var_(for_field_var) -{ -} - -ChooseRandomTypeFilter::~ChooseRandomTypeFilter() -{ - -} - -bool -ChooseRandomTypeFilter::filter(int v) const -{ - assert((v >= 0) && (static_cast(v) < AllTypes.size())); - typ_ = AllTypes[v]; - assert(typ_); - if (typ_->eType == eSimple) { - Filter *filter = SIMPLE_TYPES_PROB_FILTER; - return filter->filter(typ_->simple_type); - } - else if ((typ_->eType == eStruct) && (!CGOptions::return_structs())) { - return true; - } - - if (for_field_var_ && typ_->get_struct_depth() >= CGOptions::max_nested_struct_level()) { - return true; - } - return false; -} - -Type * -ChooseRandomTypeFilter::get_type() -{ - assert(typ_); - return typ_; -} +vector Type::AllTypes; /////////////////////////////////////////////////////////////////////////////// @@ -250,64 +76,18 @@ ChooseRandomTypeFilter::get_type() ********************************************************/ Type::Type(eSimpleType simple_type) : eType(eSimple), - ptr_type(0), simple_type(simple_type), - used(false), - printed(false), - packed_(false) -{ - // Nothing else to do. -} - -// -------------------------------------------------------------- - /* copy constructor - *******************************************************/ -#if 0 -Type::Type(const Type &t) : - eType(t.eType), - ptr_type(t.ptr_type), - simple_type(t.simple_type), - dimensions(t.dimensions), - fields(t.fields), - used(t.used), - printed(t.printed) + used(false) { // Nothing else to do. } -#endif // -------------------------------------------------------------- - /* constructor for struct or union types - *******************************************************/ -Type::Type(vector& struct_fields, bool isStruct, bool packed, - vector &qfers, vector &fields_length) : - ptr_type(0), - fields(struct_fields), - used(false), - printed(false), - packed_(packed), - qfers_(qfers), - bitfields_length_(fields_length) +Type::Type(void) : + simple_type(MAX_SIMPLE_TYPES), + used(false) { - static unsigned int sequence = 0; - if (isStruct) - eType = eStruct; - else - eType = eUnion; - sid = sequence++; -} - -// -------------------------------------------------------------- - /* constructor for pointers - *******************************************************/ -Type::Type(const Type* t) : - eType(ePointer), - ptr_type(t), - used(false), - printed(false), - packed_(false) -{ - // Nothing else to do. + // Nothing to do. } // -------------------------------------------------------------- @@ -316,24 +96,6 @@ Type::~Type(void) // Nothing to do. } -// -------------------------------------------------------------- -#if 0 -Type & -Type::operator=(const Type& t) -{ - if (this == &t) { - return *this; - } - - eType = t.eType; - simple_type = t.simple_type; - dimensions = t.dimensions; - fields = t.fields; - - return *this; -} -#endif - // --------------------------------------------------------------------- const Type & Type::get_simple_type(eSimpleType st) @@ -428,69 +190,28 @@ Type::find_type(const Type* t) return 0; } -// --------------------------------------------------------------------- -/* find the pointer type to the given type in existing types, - * return 0 if not found - *************************************************************/ -Type* -Type::find_pointer_type(const Type* t, bool add) -{ - for (size_t i=0; iptr_type == t) { - return derived_types[i]; - } - } - if (add) { - Type* ptr_type = new Type(t); - derived_types.push_back(ptr_type); - return ptr_type; - } - return 0; -} - bool Type::is_const_struct_union() const { - if (!is_aggregate()) return false; - assert(fields.size() == qfers_.size()); - - for (size_t i = 0; i < fields.size(); ++i) { - const Type *field = fields[i]; - if (field->is_const_struct_union()) { - return true; - } - const CVQualifiers& cf = qfers_[i]; - if (cf.is_const()) return true; - } return false; } bool Type::is_volatile_struct_union() const { - if (!is_aggregate()) return false; - assert(fields.size() == qfers_.size()); - - for (size_t i = 0; i < fields.size(); ++i) { - const Type *field = fields[i]; - if (field->is_volatile_struct_union()) { - return true; - } - const CVQualifiers& cf = qfers_[i]; - if (cf.is_volatile()) - return true; - } return false; } +bool +Type::is_bitfield(size_t index) const +{ + return false; +} + bool Type::has_int_field() const { if (is_int()) return true; - for (size_t i=0; ihas_int_field()) return true; - } return false; } @@ -500,68 +221,6 @@ Type::signed_overflow_possible() const return eType == eSimple && is_signed() && ((int)SizeInBytes()) >= CGOptions::int_size(); } -void -Type::get_all_ok_struct_union_types(vector &ok_types, bool no_const, bool no_volatile, bool need_int_field, bool bStruct) -{ - vector::iterator i; - for(i = AllTypes.begin(); i != AllTypes.end(); ++i) { - Type* t = (*i); - if (bStruct && t->eType != eStruct) continue; - if (!bStruct && t->eType != eUnion) continue; - if ((no_const && t->is_const_struct_union()) || - (no_volatile && t->is_volatile_struct_union()) || - (need_int_field && (!t->has_int_field()))) { - continue; - } - ok_types.push_back(t); - } -} - -const Type* -Type::choose_random_struct_union_type(vector &ok_types) -{ - size_t sz = ok_types.size(); - assert(sz > 0); - - int index = rnd_upto(ok_types.size()); - assert(index >= 0); - Type *rv_type = ok_types[index]; - if (!rv_type->used) { - Bookkeeper::record_type_with_bitfields(rv_type); - rv_type->used = true; - } - return rv_type; -} - -const Type* -Type::choose_random_pointer_type(void) -{ - unsigned int index = rnd_upto(derived_types.size()); - return derived_types[index]; -} - -bool -Type::has_pointer_type(void) -{ - return derived_types.size() > 0; -} - -const Type* -Type::choose_random_struct_from_type(const Type* type, bool no_volatile) -{ - if (!type) - return NULL; - - const Type* t = type; - vector ok_struct_types; - get_all_ok_struct_union_types(ok_struct_types, no_volatile, false, true, true); - - if (ok_struct_types.size() > 0) { - t = Type::choose_random_struct_union_type(ok_struct_types); - } - return t; -} - const Type* Type::random_type_from_type(const Type* type, bool no_volatile, bool strict_simple_type) { @@ -583,7 +242,7 @@ static bool MoreTypesProbability(void) { // Always have at least 10 types in the program. - if (AllTypes.size() < 10) + if (Type::AllTypes.size() < 10) return true; // by default 50% probability for each additional struct or union type. return rnd_flipcoin(MoreStructUnionTypeProb); @@ -594,516 +253,12 @@ eSimpleType Type::choose_random_nonvoid_simple(void) { eSimpleType simple_type; -#if 0 - vector vs; - vs.push_back(eVoid); - - if (!CGOptions::allow_int64()) { - vs.push_back(eLongLong); - vs.push_back(eULongLong); - } - - VectorFilter filter(vs); -#endif simple_type = (eSimpleType) rnd_upto(MAX_SIMPLE_TYPES, SIMPLE_TYPES_PROB_FILTER); return simple_type; } -void -Type::make_one_bitfield(vector &random_fields, vector &qualifiers, - vector &fields_length) -{ - int max_length = CGOptions::int_size() * 8; - bool sign = rnd_flipcoin(BitFieldsSignedProb); - - const Type *type = sign ? &Type::get_simple_type(eInt) : &Type::get_simple_type(eUInt); - random_fields.push_back(type); - CVQualifiers qual = CVQualifiers::random_qualifiers(type, FieldConstProb, FieldVolatileProb); - - qualifiers.push_back(qual); - int length = rnd_upto(max_length); - - bool no_zero_len = fields_length.empty() || (fields_length.back() == 0); - // force length to be non-zero is required - if (length == 0 && no_zero_len) { - if (max_length <= 2) length = 1; - else length = rnd_upto(max_length - 1) + 1; - } - fields_length.push_back(length); -} - -// --------------------------------------------------------------------- -void -Type::make_full_bitfields_struct_fields(size_t field_cnt, vector &random_fields, - vector &qualifiers, - vector &fields_length) -{ - for (size_t i=0; i &random_fields, - vector &qualifiers, - vector &fields_length) -{ - ChooseRandomTypeFilter f(/*for_field_var*/true); - unsigned int i = rnd_upto(AllTypes.size(), &f); - const Type* type = AllTypes[i]; - random_fields.push_back(type); - CVQualifiers qual = CVQualifiers::random_qualifiers(type, FieldConstProb, FieldVolatileProb); - qualifiers.push_back(qual); - fields_length.push_back(-1); -} - -void -Type::make_one_union_field(vector &fields, vector &qfers, vector &lens) -{ - bool is_bitfield = CGOptions::bitfields() && !CGOptions::ccomp() && rnd_flipcoin(BitFieldInNormalStructProb); - if (is_bitfield) { - make_one_bitfield(fields, qfers, lens); - } - else { - size_t i; - vector ok_types; - // filter out struct types containing bit-fields. Their layout is implementation - // defined, we don't want to mess with them in unions for now - for (i=0; ihas_bitfields()) { - ok_types.push_back(AllTypes[i]); - } - } - - // find of struct types - vector struct_types; - for (size_t i=0; ieType == eStruct) { - struct_types.push_back(ok_types[i]); - } - } - const Type* type = NULL; - do { - // 10% chance to be struct field - if (!struct_types.empty() && pure_rnd_flipcoin(10)) { - type = struct_types[pure_rnd_upto(struct_types.size())]; - assert(type->eType == eStruct); - } - // 10% chance to be char* if pointer is allowed - else if (CGOptions::pointers() && CGOptions::int8() && pure_rnd_flipcoin(10)) { - type = find_pointer_type(&get_simple_type(eChar), true); - } - else { - unsigned int i = pure_rnd_upto(ok_types.size()); - const Type* t = ok_types[i]; - // no union in union? - if (t->eType == eUnion || - (t->eType == eSimple && SIMPLE_TYPES_PROB_FILTER->filter(t->simple_type))) { - continue; - } - type = t; - } - } while (type == NULL); - - fields.push_back(type); - CVQualifiers qual = CVQualifiers::random_qualifiers(type, FieldConstProb, FieldVolatileProb); - qfers.push_back(qual); - lens.push_back(-1); - } -} - -void -Type::make_normal_struct_fields(size_t field_cnt, vector &random_fields, - vector &qualifiers, - vector &fields_length) -{ - for (size_t i=0; i &enumerator, int bitfield_prob) -{ - int field_cnt = CGOptions::max_struct_fields(); - for (int i = 0; i < field_cnt; ++i) { - std::ostringstream ss; - ss << "bitfield" << i; - - if (CGOptions::bitfields()) { - enumerator.add_bool_elem(ss.str(), bitfield_prob); - } - else { - enumerator.add_bool_elem(ss.str(), 0); - } - } -} - -void -Type::init_fields_enumerator(Enumerator &enumerator, - Enumerator &bitfield_enumerator, - int type_bound, int qual_bound, - int bitfield_qual_bound) -{ - int field_cnt = CGOptions::max_struct_fields(); - for (int i = 0; i < field_cnt; ++i) { - std::ostringstream ss; - ss << "bitfield" << i; - bool is_bitfield = bitfield_enumerator.get_elem(ss.str()); - if (is_bitfield) { - std::ostringstream ss1, ss2, ss3; - ss1 << "bitfield_sign" << i; - ss2 << "bitfield_qualifier" << i; - ss3 << "bitfield_length" << i; - enumerator.add_bool_elem_of_bool(ss1.str(), false); - enumerator.add_elem(ss2.str(), bitfield_qual_bound); - enumerator.add_elem(ss3.str(), ENUM_BITFIELD_SIZE); - } - else { - std::ostringstream ss1, ss2; - ss1 << "field" << i; - ss2 << "qualifier" << i; - enumerator.add_elem(ss1.str(), type_bound); - enumerator.add_elem(ss2.str(), qual_bound); - } - } - enumerator.add_bool_elem_of_bool("packed", CGOptions::packed_struct()); -} - -int -Type::get_bitfield_length(int length_flag) -{ - int max_length = CGOptions::int_size() * 8; - assert(max_length > 0); - int length = 0; - switch (length_flag) { - case ZERO_BITFIELD: - length = 0; - break; -#if 0 - case MAX_BITFIELD: - length = max_length; - break; -#endif - case RANDOM_BITFIELD: - length = pure_rnd_upto(max_length); - break; - default: - assert(0); - break; - } - return length; -} - -bool -Type::make_one_bitfield_by_enum(Enumerator &enumerator, - vector &all_bitfield_quals, - vector &random_fields, - vector &qualifiers, - vector &fields_length, - int index, bool &last_is_zero) -{ - - std::ostringstream ss1, ss2, ss3; - ss1 << "bitfield_sign" << index; - ss2 << "bitfield_qualifier" << index; - ss3 << "bitfield_length" << index; - - bool sign = enumerator.get_elem(ss1.str()); - // we cannot allow too many structs, - // so randomly choose the sign of fields. - if (pure_rnd_flipcoin(50)) - sign = true; - const Type *type = sign ? &Type::get_simple_type(eInt) : &Type::get_simple_type(eUInt); - random_fields.push_back(type); - int qual_index = enumerator.get_elem(ss2.str()); - assert((qual_index >= 0) && ((static_cast(qual_index)) < all_bitfield_quals.size())); - CVQualifiers qual = all_bitfield_quals[qual_index]; - qualifiers.push_back(qual); - - int length_flag = enumerator.get_elem(ss3.str()); - - int length = get_bitfield_length(length_flag); - if ((index==0 || last_is_zero) && (length == 0)) { - return false; - } - last_is_zero = (length == 0) ? true : false; - fields_length.push_back(length); - return true; -} - -bool -Type::make_one_normal_field_by_enum(Enumerator &enumerator, vector &all_types, - vector &all_quals, vector &fields, - vector &quals, vector &fields_length, int i) -{ - int types_size = all_types.size(); - int quals_size = all_quals.size(); - Filter *filter = SIMPLE_TYPES_PROB_FILTER; - - std::ostringstream ss1, ss2; - ss1 << "field" << i; - int typ_index = enumerator.get_elem(ss1.str()); - assert(typ_index >= 0 && typ_index < types_size); - Type *typ = const_cast(all_types[typ_index]); - if (typ->eType == eSimple) { - assert(typ->simple_type != eVoid); - if (filter->filter(typ->simple_type)) - return false; - } - - assert(typ != NULL); - fields.push_back(typ); - - ss2 << "qualifier" << i; - int qual_index = enumerator.get_elem(ss2.str()); - assert(qual_index >= 0 && qual_index < quals_size); - CVQualifiers qual = all_quals[qual_index]; - quals.push_back(qual); - - fields_length.push_back(-1); - return true; -} - -void -Type::make_all_struct_types_(Enumerator &bitfields_enumerator, vector &accum_types, - vector &all_types, vector &all_quals, - vector &all_bitfield_quals) -{ - Enumerator fields_enumerator; - init_fields_enumerator(fields_enumerator, bitfields_enumerator, all_types.size(), - all_quals.size(), all_bitfield_quals.size()); - - Enumerator *i; - for (i = fields_enumerator.begin(); i != fields_enumerator.end(); i = i->next()) { - make_all_struct_types_with_bitfields(*i, bitfields_enumerator, accum_types, all_types, all_quals, all_bitfield_quals); - } -} - -void -Type::make_all_struct_types_with_bitfields(Enumerator &enumerator, - Enumerator &bitfields_enumerator, vector &accum_types, - vector &all_types, vector &all_quals, - vector &all_bitfield_quals) -{ - vector fields; - vector quals; - vector fields_length; - int field_cnt = CGOptions::max_struct_fields(); - bool last_is_zero = false; - - int bitfields_cnt = 0; - int normal_fields_cnt = 0; - for (int i = 0; i < field_cnt; ++i) { - std::ostringstream ss; - ss << "bitfield" << i; - bool is_bitfield = bitfields_enumerator.get_elem(ss.str()); - bool rv = false; - if (is_bitfield) { - rv = make_one_bitfield_by_enum(enumerator, all_bitfield_quals, fields, quals, fields_length, i, last_is_zero); - bitfields_cnt++; - } - else { - rv = make_one_normal_field_by_enum(enumerator, all_types, all_quals, fields, quals, fields_length, i); - last_is_zero = rv ? false : last_is_zero; - normal_fields_cnt++; - } - if (!rv) - return; - } - if ((ExhaustiveBitFieldsProb > 0) && (ExhaustiveBitFieldsProb < 100) && - ((bitfields_cnt == field_cnt) || (normal_fields_cnt == field_cnt))) - return; - - bool packed = enumerator.get_elem("packed"); - Type* new_type = new Type(fields, true, packed, quals, fields_length); - new_type->used = true; - accum_types.push_back(new_type); -} - -/* - * level control's the nested level of struct - */ -void -Type::copy_all_fields_types(vector &dest_types, vector &src_types) -{ - vector::const_iterator i; - for (i = src_types.begin(); i != src_types.end(); ++i) - dest_types.push_back(*i); -} - -void -Type::reset_accum_types(vector &accum_types) -{ - accum_types.clear(); - vector::const_iterator i; - for (i = AllTypes.begin(); i != AllTypes.end(); ++i) - accum_types.push_back(*i); -} - -void -Type::delete_useless_structs(vector &all_types, vector &accum_types) -{ - assert(all_types.size() <= accum_types.size()); - for (size_t i = 0; i < all_types.size(); ++i) { - const Type *t = all_types[i]; - if (t->eType == eStruct) { - const Type *t1 = accum_types[i]; - delete t1; - accum_types[i] = t; - } - } -} - -void -Type::make_all_struct_types(int level, vector &accum_types) -{ - - if (level > 0) { - make_all_struct_types(level - 1, accum_types); - } - vector all_types; - copy_all_fields_types(all_types, accum_types); - reset_accum_types(accum_types); - - vector all_quals; - CVQualifiers::get_all_qualifiers(all_quals, RegularConstProb, RegularVolatileProb); - - vector all_bitfield_quals; - CVQualifiers::get_all_qualifiers(all_bitfield_quals, FieldConstProb, FieldVolatileProb); - - Enumerator fields_enumerator; - init_is_bitfield_enumerator(fields_enumerator, ExhaustiveBitFieldsProb); - - Enumerator *i; - for (i = fields_enumerator.begin(); i != fields_enumerator.end(); i = i->next()) { - make_all_struct_types_(*i, accum_types, all_types, all_quals, all_bitfield_quals); - } - delete_useless_structs(all_types, accum_types); -} - -bool -Type::has_aggregate_field(const vector &fields) -{ - for (vector::const_iterator iter = fields.begin(), - iter_end = fields.end(); iter != iter_end; ++iter) { - if ((*iter)->is_aggregate()) - return true; - } - return false; -} - -bool -Type::has_longlong_field(const vector &fields) -{ - for (vector::const_iterator iter = fields.begin(), - iter_end = fields.end(); iter != iter_end; ++iter) { - if ((*iter)->is_long_long()) - return true; - } - return false; -} - -Type* -Type::make_random_struct_type(void) -{ - size_t field_cnt = 0; - size_t max_cnt = CGOptions::max_struct_fields(); - if (CGOptions::fixed_struct_fields()) - field_cnt = max_cnt; - else - field_cnt = rnd_upto(max_cnt) + 1; - vector random_fields; - vector qualifiers; - vector fields_length; - bool is_bitfields = CGOptions::bitfields() && rnd_flipcoin(BitFieldsCreationProb); - - //if (CGOptions::bitfields()) - if (is_bitfields) - make_full_bitfields_struct_fields(field_cnt, random_fields, qualifiers, fields_length); - else - make_normal_struct_fields(field_cnt, random_fields, qualifiers, fields_length); - - // for now, no union type - bool packed = false; - if (CGOptions::packed_struct()) { - if (CGOptions::ccomp() && (has_aggregate_field(random_fields) || has_longlong_field(random_fields))) { - // Nothing to do - } - else { - packed = rnd_flipcoin(50); - } - } - - Type* new_type = new Type(random_fields, true, packed, qualifiers, fields_length); - return new_type; -} - -Type* -Type::make_random_union_type(void) -{ - size_t max_cnt = CGOptions::max_union_fields(); - size_t field_cnt = rnd_upto(max_cnt) + 1; - - vector fields; - vector qfers; - vector lens; - - for (size_t i=0; ihas_bitfields()); - } - Type* new_type = new Type(fields, false, false, qfers, lens); - return new_type; -} - -// --------------------------------------------------------------------- -Type* -Type::make_random_pointer_type(void) -{ - //Type* new_type = 0; - //Type* ptr_type = 0; - // occasionally choose pointer to pointers - if (rnd_flipcoin(20)) { - if (derived_types.size() > 0) { - unsigned int rnd_num = rnd_upto(derived_types.size()); - const Type* t = derived_types[rnd_num]; - if (t->get_indirect_level() < CGOptions::max_indirect_level()) { - return find_pointer_type(t, true); - } - } - } - - // choose a pointer to basic/aggregate types - const Type* t = choose_random(); - // consolidate all integer pointer types into "int*", hopefully this increase - // chance of pointer assignments and dereferences - if (t->eType == eSimple) { - t = get_int_type(); - } - return find_pointer_type(t, true); -} - // --------------------------------------------------------------------- void Type::GenerateSimpleTypes(void) @@ -1123,14 +278,14 @@ GenerateAllTypes(void) Type::GenerateSimpleTypes(); if (CGOptions::use_struct()) { while (MoreTypesProbability()) { - Type *ty = Type::make_random_struct_type(); - AllTypes.push_back(ty); + Type *ty = AggregateType::make_random_struct_type(); + Type::AllTypes.push_back(ty); } } if (CGOptions::use_union()) { while (MoreTypesProbability()) { - Type *ty = Type::make_random_union_type(); - AllTypes.push_back(ty); + Type *ty = AggregateType::make_random_union_type(); + Type::AllTypes.push_back(ty); } } } @@ -1139,23 +294,62 @@ GenerateAllTypes(void) const Type * Type::choose_random() { - ChooseRandomTypeFilter f(/*for_field_var*/false); - rnd_upto(AllTypes.size(), &f); - Type *rv_type = f.get_type(); - if (!rv_type->used) { - Bookkeeper::record_type_with_bitfields(rv_type); - rv_type->used = true; + VectorFilter f; + unsigned int type_index = 0; + Type *type = 0; + for( type_index = 0; type_index < AllTypes.size(); type_index++) { + type = AllTypes[type_index]; + assert(type); + if (type->eType == eSimple) { + Filter *filter = SIMPLE_TYPES_PROB_FILTER; + if(filter->filter(type->simple_type)) + f.add(type_index); + } + else if ((type->eType == eStruct) && (!CGOptions::return_structs())) { + f.add(type_index); + } + } + + type_index = rnd_upto(AllTypes.size(), &f); + + Type *typ = AllTypes[type_index]; + + if (!typ->used) { + Bookkeeper::record_type_with_bitfields(typ); + typ->used = true; } - return rv_type; + assert(typ); + return typ; } const Type * Type::choose_random_nonvoid(void) { - NonVoidTypeFilter f; - rnd_upto(AllTypes.size(), &f); + VectorFilter f; + unsigned int type_index = 0; + Type *type = 0; + + for( type_index = 0; type_index < AllTypes.size(); type_index++) { + type = AllTypes[type_index]; + + if (type->simple_type == eVoid) + f.add(type_index); + + if (type->eType == eSimple) { + Filter *filter = SIMPLE_TYPES_PROB_FILTER; + if(filter->filter(type->simple_type)) + f.add(type_index); + } + } + + type_index = rnd_upto(AllTypes.size(), &f); - Type *typ = f.get_type(); + Type *typ = AllTypes[type_index]; + + if (!typ->used) { + Bookkeeper::record_type_with_bitfields(typ); + typ->used = true; + } assert(typ); return typ; } @@ -1163,10 +357,42 @@ Type::choose_random_nonvoid(void) const Type * Type::choose_random_nonvoid_nonvolatile(void) { - NonVoidNonVolatileTypeFilter f; - rnd_upto(AllTypes.size(), &f); + VectorFilter f; + unsigned int type_index = 0; + Type *type = 0; + + for( type_index = 0; type_index < AllTypes.size(); type_index++) { + type = AllTypes[type_index]; + + if (type->simple_type == eVoid) + f.add(type_index); + + if (type->is_aggregate() && type->is_volatile_struct_union()) + f.add(type_index); + + if ((type->eType == eStruct) && (!CGOptions::arg_structs())) { + f.add(type_index); + } - Type *typ = f.get_type(); + if ((type->eType == eUnion) && (!CGOptions::arg_unions())) { + f.add(type_index); + } + + if (type->eType == eSimple) { + Filter *filter = SIMPLE_TYPES_PROB_FILTER; + if(filter->filter(type->simple_type)) + f.add(type_index); + } + } + + type_index = rnd_upto(AllTypes.size(), &f); + + Type *typ = AllTypes[type_index]; + + if (!typ->used) { + Bookkeeper::record_type_with_bitfields(typ); + typ->used = true; + } assert(typ); return typ; } @@ -1180,67 +406,15 @@ Type::choose_random_simple(void) return &get_simple_type(ty); } -// --------------------------------------------------------------------- -int -Type::get_indirect_level() const -{ - int level = 0; - const Type* pt = ptr_type; - while (pt != 0) { - level++; - pt = pt->ptr_type; - } - return level; -} - -// --------------------------------------------------------------------- -int -Type::get_struct_depth() const -{ - int depth = 0; - if (eType == eStruct) { - depth++; - int max_depth = 0; - for (size_t i=0; iget_struct_depth(); - if(field_depth > max_depth) { - max_depth = field_depth; - } - } - depth += max_depth; - } - return depth; -} - bool Type::is_unamed_padding(size_t index) const { - size_t sz = bitfields_length_.size(); - if (sz == 0) - return false; - - assert(index < sz); - return (bitfields_length_[index] == 0); -} - -bool -Type::is_bitfield(size_t index) const -{ - assert(index < bitfields_length_.size()); - return (bitfields_length_[index] >= 0); + return false; } bool Type::has_bitfields() const { - for (size_t i=0; i= 0) { - return true; - } - if (fields[i]->eType == eStruct && fields[i]->has_bitfields()) { - return true; - } - } return false; } @@ -1249,25 +423,13 @@ Type::has_bitfields() const bool Type::has_padding(void) const { - if (eType == eStruct && !packed_) return true; - for (size_t i=0; ihas_padding()) { - return true; - } - } return false; } bool Type::is_full_bitfields_struct() const { - if (eType != eStruct) return false; - size_t i; - for (i = 0; i < bitfields_length_.size(); ++i) { - if (bitfields_length_[i] < 0) - return false; - } - return true; + return false; } bool @@ -1317,16 +479,6 @@ Type::to_unsigned(void) const return NULL; } -const Type* -Type::get_base_type(void) const -{ - const Type* tmp = this; - while (tmp->ptr_type != 0) { - tmp = tmp->ptr_type; - } - return tmp; -} - bool Type::is_promotable(const Type* t) const { @@ -1361,26 +513,13 @@ Type::is_convertable(const Type* t) const if (this == t) return true; if (eType == eSimple && t->eType == eSimple) { - // forbiden conversion from float to int - if (t->is_float() && !is_float()) - return false; + // forbiden conversion from float to int + if (t->is_float() && !is_float()) + return false; if ((simple_type != eVoid && t->simple_type != eVoid) || simple_type == t->simple_type) return true; } - else if (eType == ePointer && t->eType == ePointer) { - if (ptr_type == t->ptr_type) { - return true; - } - if (ptr_type->eType == eSimple && t->ptr_type->eType == eSimple) { - if(ptr_type->simple_type == eFloat && t->ptr_type->simple_type == eFloat) - return true; - else - return ptr_type->SizeInBytes() == t->ptr_type->SizeInBytes(); - } - //return ptr_type->is_convertable(t->ptr_type); - //return t->ptr_type->is_promotable(ptr_type); - } return false; } @@ -1426,12 +565,12 @@ bool Type::is_dereferenced_from(const Type* t) const { if (t->eType == ePointer) { - const Type* pt = t->ptr_type; + const Type* pt = (dynamic_cast(t))->ptr_type; while (pt) { if (pt == this) { return true; } - pt = pt->ptr_type; + pt = (pt->eType == ePointer) ? (dynamic_cast(pt))->ptr_type : NULL; } } return false; @@ -1448,17 +587,13 @@ Type::is_derivable(const Type* t) const if (this == t) { return true; } - return is_convertable(t) || is_dereferenced_from(t) || (ptr_type==t); + return is_convertable(t) || is_dereferenced_from(t); } unsigned long Type::SizeInBytes(void) const { - size_t i; - switch (eType) { - default: break; - case eSimple: - switch (simple_type) { + switch (simple_type) { case eVoid: return 0; case eInt: return 4; case eShort: return 2; @@ -1472,40 +607,6 @@ Type::SizeInBytes(void) const case eULongLong:return 8; case eFloat: return 4; // case eDouble: return 8; - } - break; - case eUnion: { - unsigned int max_size = 0; - for (i=0; iSizeInBytes(); - } - if (sz == SIZE_UNKNOWN) return sz; - if (sz > max_size) { - max_size = sz; - } - } - return max_size; - } - case eStruct: { - if (!this->packed_) return SIZE_UNKNOWN; - // give up if there are bitfields, too much compiler-dependence and machine-dependence - if (this->has_bitfields()) return SIZE_UNKNOWN; - unsigned int total_size = 0; - for (i=0; iSizeInBytes(); - if (sz == SIZE_UNKNOWN) return sz; - total_size += sz; - } - return total_size; - } - case ePointer: - CGOptions::pointer_size(); - break; } return 0; } @@ -1522,15 +623,15 @@ Type::SelectLType(bool no_volatile, eAssignOps op) // so choose pointer types iff we create simple assignment // (see Statement::make_random) if (op == eSimpleAssign && rnd_flipcoin(PointerAsLTypeProb)) { - type = Type::make_random_pointer_type(); + type = PointerType::make_random_pointer_type(); } // choose a struct type as LHS type if (!type) { - vector ok_struct_types; - get_all_ok_struct_union_types(ok_struct_types, true, no_volatile, false, true); + vector ok_struct_types; + AggregateType::get_all_ok_struct_union_types(ok_struct_types, true, no_volatile, false, true); if ((ok_struct_types.size() > 0) && (op == eSimpleAssign) && rnd_flipcoin(StructAsLTypeProb)) { - type = Type::choose_random_struct_union_type(ok_struct_types); + type = AggregateType::choose_random_struct_union_type(ok_struct_types); } } @@ -1554,35 +655,12 @@ Type::get_int_subfield_names(string prefix, vector& names, const vector< if (eType == eSimple) { names.push_back(prefix); } - else if (is_aggregate()) { - size_t i; - size_t j = 0; - for (i=0; i empty; - fields[i]->get_int_subfield_names(oss.str(), names, empty); - } - } } bool Type::contain_pointer_field(void) const { if (eType == ePointer) return true; - if (eType == eStruct || eType == eUnion) { - for (size_t i=0; icontain_pointer_field()) { - return true; - } - } - } return false; } @@ -1590,21 +668,14 @@ Type::contain_pointer_field(void) const void Type::Output(std::ostream &out) const { - switch (eType) { - case eSimple: - if (this->simple_type == eVoid) { - out << "void"; - } else if (this->simple_type == eFloat) { - out << "float"; - } else { - out << (is_signed() ? "int" : "uint"); - out << (SizeInBytes() * 8); - out << "_t"; - } - break; - case ePointer: ptr_type->Output( out ); out << "*"; break; - case eUnion: out << "union U" << sid; break; - case eStruct: out << "struct S" << sid; break; + if (this->simple_type == eVoid) { + out << "void"; + } else if (this->simple_type == eFloat) { + out << "float"; + } else { + out << (is_signed() ? "int" : "uint"); + out << (SizeInBytes() * 8); + out << "_t"; } } @@ -1618,97 +689,6 @@ Type::get_type_sizeof_string(std::string &s) const s = ss.str(); } -// --------------------------------------------------------------------- -/* print struct definition (fields etc) - *************************************************************/ -void OutputStructUnion(Type* type, std::ostream &out) -{ - size_t i; - // sanity check - assert (type->is_aggregate()); - - if (!type->printed) { - // output dependent structs, if any - for (i=0; ifields.size(); i++) { - if (type->fields[i]->is_aggregate()) { - OutputStructUnion((Type*)type->fields[i], out); - } - } - // output myself - if (type->packed_) { - if (!CGOptions::ccomp()) { - out << "#pragma pack(push)"; - really_outputln(out); - } - out << "#pragma pack(1)"; - really_outputln(out); - } - type->Output(out); - out << " {"; - really_outputln(out); - - assert(type->fields.size() == type->qfers_.size()); - unsigned int j = 0; - for (i=0; ifields.size(); i++) { - out << " "; - const Type *field = type->fields[i]; - bool is_bitfield = type->is_bitfield(i); - if (is_bitfield) { - assert(field->eType == eSimple); - type->qfers_[i].OutputFirstQuals(out); - if (field->simple_type == eInt) - out << "signed"; - else if (field->simple_type == eUInt) - out << "unsigned"; - else - assert(0); - int length = type->bitfields_length_[i]; - assert(length >= 0); - if (length == 0) - out << " : "; - else - out << " f" << j++ << " : "; - out << length << ";"; - } - else { - type->qfers_[i].output_qualified_type(field, out); - out << " f" << j++ << ";"; - } - really_outputln(out); - } - out << "};"; - really_outputln(out); - if (type->packed_) { - if (CGOptions::ccomp()) { - out << "#pragma pack()"; - } - else { - out << "#pragma pack(pop)"; - } - really_outputln(out); - } - type->printed = true; - really_outputln(out); - } -} - -// --------------------------------------------------------------------- -/* print all struct definitions (fields etc) - *************************************************************/ -void -OutputStructUnionDeclarations(std::ostream &out) -{ - size_t i; - output_comment_line(out, "--- Struct/Union Declarations ---"); - for (i=0; iused && (t->eType == eStruct || t->eType == eUnion)) { - OutputStructUnion(AllTypes[i], out); - } - } -} - /* * return the printf directive string for the type. for example, int -> "%d" */ @@ -1716,27 +696,10 @@ std::string Type::printf_directive(void) const { string ret; - size_t i; - switch (eType) { - case eSimple: - if (SizeInBytes() >= 8) { - ret = is_signed() ? "%lld" : "%llu"; - } else { - ret = is_signed() ? "%d" : "%u"; - } - break; - case ePointer: - ret = "0x%0x"; - break; - case eUnion: - case eStruct: - ret = "{"; - for (i=0; i 0) ret += ", "; - ret += fields[i]->printf_directive(); - } - ret += "}"; - break; + if (SizeInBytes() >= 8) { + ret = is_signed() ? "%lld" : "%llu"; + } else { + ret = is_signed() ? "%d" : "%u"; } return ret; } @@ -1751,10 +714,7 @@ Type::doFinalization(void) for(j = AllTypes.begin(); j != AllTypes.end(); ++j) delete (*j); AllTypes.clear(); - - for(j = derived_types.begin(); j != derived_types.end(); ++j) - delete (*j); - derived_types.clear(); + PointerType::doFinalization(); } diff --git a/src/Type.h b/src/Type.h old mode 100644 new mode 100755 index f5632f53e..d85cc666d --- a/src/Type.h +++ b/src/Type.h @@ -3,6 +3,9 @@ // Copyright (c) 2007, 2008, 2010, 2011, 2013, 2014 The University of Utah // All rights reserved. // +// Copyright (c) 2015-2016 Huawei Technologies Co., Ltd +// All rights reserved. +// // This file is part of `csmith', a random generator of C programs. // // Redistribution and use in source and binary forms, with or without @@ -48,7 +51,6 @@ #include "StatementAssign.h" #include "CVQualifiers.h" -template class Enumerator; using namespace std; #define SIZE_UNKNOWN 0xFFFF @@ -95,6 +97,8 @@ enum eMatchType eFlexible, }; +////////////////////////////////////////////////////////////////////// + /* * */ @@ -110,86 +114,14 @@ class Type // choose a random integer type static const Type *choose_random_simple(void); - // choose a random pointer type - static const Type* choose_random_pointer_type(void); - - static const Type *choose_random_struct_from_type(const Type* type, bool no_volatile); - - static void get_all_ok_struct_union_types(vector &ok_types, bool no_const, bool no_volatile, bool need_int_field, bool bStruct); - - bool has_int_field() const; + virtual bool has_int_field() const; bool signed_overflow_possible() const; - static const Type* choose_random_struct_union_type(vector &ok_types); - static const Type * choose_random_nonvoid_nonvolatile(void); - static bool has_pointer_type(void); - static const Type* random_type_from_type(const Type* type, bool no_volatile=false, bool strict_simple_type=false); - static void copy_all_fields_types(vector &dest_types, vector &src_types); - - static void reset_accum_types(vector &accum_types); - - static void delete_useless_structs(vector &all_types, vector &accum_types); - - static void init_is_bitfield_enumerator(Enumerator &enumerator, int bitfield_prob); - - static void init_fields_enumerator(Enumerator &enumerator, - Enumerator &bitfield_enumerator, - int type_bound, int qual_bound, int bitfield_qual_bound); - - static bool make_one_normal_field_by_enum(Enumerator &enumerator, vector &all_types, - vector &all_quals, vector &fields, - vector &quals, vector &fields_length, int i); - - static bool make_one_bitfield_by_enum(Enumerator &enumerator, - vector &all_bitfield_quals, - vector &random_fields, - vector &qualifiers, - vector &fields_length, - int index, bool &last_is_zero); - - static int get_bitfield_length(int length_flag); - - static void make_all_struct_types_(Enumerator &bitfields_enumerator, vector &accum_types, - vector &all_types, vector &all_quals, - vector &all_bitfield_quals); - - static void make_all_struct_types_with_bitfields(Enumerator &enumerator, - Enumerator &bitfields_enumerator, - vector &accum_types, vector &all_types, - vector &all_quals, vector &all_bitfield_quals); - - static void make_all_struct_types(int level, vector &accum_types); - - // make a random struct or union type - static Type* make_random_struct_type(void); - static Type* make_random_union_type(void); - - static void make_one_bitfield(vector &random_fields, - vector &qualifiers, - vector &fields_length); - - static void make_one_struct_field(vector &random_fields, - vector &qualifiers, - vector &fields_length); - - static void make_one_union_field(vector &fields, vector &qfers, vector &lens); - - static void make_full_bitfields_struct_fields(size_t field_cnt, vector &random_fields, - vector &qualifiers, - vector &fields_length); - - static void make_normal_struct_fields(size_t field_cnt, vector &random_fields, - vector &qualifiers, - vector &fields_length); - - // make a random pointer type - static Type* make_random_pointer_type(void); - static const Type *get_type_from_string(const string &type_string); // generate all simple types except void and long long(if it is not allowed) @@ -203,31 +135,24 @@ class Type // select a type for LHS static const Type *SelectLType(bool no_volatile, eAssignOps op); - static bool has_aggregate_field(const vector &fields); - - static bool has_longlong_field(const vector &fields); - explicit Type(eSimpleType simple_type); - Type(vector& fields, bool isStruct, bool packed, - vector &qfers, vector &fields_length); - Type(vector& array_dims, eSimpleType st); - explicit Type(const Type* t); + Type(void); ~Type(void); static const Type &get_simple_type(eSimpleType simple_type); static void doFinalization(void); - const Type* get_base_type(void) const; - int get_indirect_level(void) const; - int get_struct_depth(void) const; - void get_int_subfield_names(string prefix, vector& names, const vector& excluded_fields) const; + virtual const Type* get_base_type(void) const { return this; } + virtual int get_indirect_level(void) const { return 0; } + virtual int get_struct_depth(void) const { return 0; } + virtual void get_int_subfield_names(string prefix, vector& names, const vector& excluded_fields) const; bool is_signed(void) const; bool is_long_long(void) const { return ((eType == eSimple) && (simple_type == eLongLong || simple_type == eULongLong)); } const Type* to_unsigned(void) const; - bool is_pointer_to_char(void) const { return ptr_type && ptr_type->eType == eSimple && (ptr_type->simple_type==eChar || ptr_type->simple_type==eUChar);} + virtual bool is_pointer_to_char(void) const { return false; } bool is_signed_char() const { return ((eType == eSimple) && (simple_type == eChar)); } @@ -235,46 +160,39 @@ class Type return ((eType == eSimple) && (simple_type == eFloat)); } bool is_promotable(const Type* t) const; - bool is_convertable(const Type* t) const; - bool is_derivable(const Type* t) const; + virtual bool is_convertable(const Type* t) const; + virtual bool is_derivable(const Type* t) const; bool is_dereferenced_from(const Type* t) const; bool is_equivalent(const Type* t) const; bool needs_cast(const Type* t) const; - bool is_unamed_padding(size_t index) const; - bool is_full_bitfields_struct() const; - bool is_bitfield(size_t index) const ; - bool has_bitfields() const; - bool has_padding(void) const; - bool contain_pointer_field(void) const; - bool is_const_struct_union() const; - bool is_volatile_struct_union() const; + virtual bool is_unamed_padding(size_t index) const; + virtual bool is_full_bitfields_struct() const; + virtual bool is_bitfield(size_t index) const; + virtual bool has_bitfields() const; + virtual bool has_padding(void) const; + virtual bool contain_pointer_field(void) const; + virtual bool is_const_struct_union() const; + virtual bool is_volatile_struct_union() const; bool is_int(void) const { return eType == eSimple && simple_type != eVoid;} bool is_void(void) const { return eType == eSimple && simple_type == eVoid;} bool is_aggregate(void) const { return eType == eStruct || eType == eUnion;} bool match(const Type* t, enum eMatchType mt) const; - unsigned long SizeInBytes(void) const; - void Output(std::ostream &) const; - std::string printf_directive(void) const; - static Type* find_pointer_type(const Type* t, bool add); + virtual unsigned long SizeInBytes(void) const; + virtual void Output(std::ostream &) const; + virtual std::string printf_directive(void) const; static Type* find_type(const Type* t); // private: eTypeDesc eType; - const Type *ptr_type; eSimpleType simple_type; - vector dimensions; // for array types - vector fields; // for struct/union types - unsigned int sid; // sequence id, for struct/union types + bool used; // whether any variable declared with this type - bool printed; // whether this struct/union has been printed in the random program - const bool packed_; // whether this struct/union should be packed - vector qfers_; // conresponds to each element of fields - // It's a tradeoff between the current implementation and the - // need of struct's level type qualifiers. - vector bitfields_length_; // -1 means it's a regular field static Type *void_type; + + static vector AllTypes; + private: DISALLOW_COPY_AND_ASSIGN(Type); @@ -286,8 +204,6 @@ class Type void GenerateAllTypes(void); const Type * get_int_type(void); -void OutputStructUnionDeclarations(std::ostream &); -void OutputStructUnion(Type* type, std::ostream &out); /////////////////////////////////////////////////////////////////////////////// diff --git a/src/Variable.cpp b/src/Variable.cpp old mode 100644 new mode 100755 index 77e5bc673..179cc0aac --- a/src/Variable.cpp +++ b/src/Variable.cpp @@ -3,6 +3,9 @@ // Copyright (c) 2007, 2008, 2009, 2010, 2011, 2013, 2014 The University of Utah // All rights reserved. // +// Copyright (c) 2015-2016 Huawei Technologies Co., Ltd +// All rights reserved. +// // This file is part of `csmith', a random generator of C programs. // // Redistribution and use in source and binary forms, with or without @@ -304,7 +307,8 @@ Variable::is_packed_aggregate_field_var() const { if (!field_var_of) return false; - if (field_var_of->type->packed_) + const AggregateType * aggregate_type = dynamic_cast(field_var_of->type); + if (aggregate_type && aggregate_type->packed_) return true; return field_var_of->is_packed_aggregate_field_var(); } @@ -335,9 +339,10 @@ Variable::get_field_id(void) const /////////////////////////////////////////////////////////////////////////////// /* expand field variables of struct, assigned names, f0, f1, etc, to them ************************************************************************/ -void Variable::create_field_vars(const Type *type) +void Variable::create_field_vars(const Type *aggregate_type) { assert(type->is_aggregate()); + const AggregateType * type = dynamic_cast(aggregate_type); size_t i, j; assert(type->fields.size() == type->qfers_.size()); j = 0; @@ -550,7 +555,7 @@ Variable::is_const_after_deref(int deref_level) const int i; const Type* t = type; for (i=0; iptr_type; + t = (dynamic_cast(t))->ptr_type; } assert(t); return t->is_const_struct_union(); @@ -573,7 +578,7 @@ Variable::is_volatile_after_deref(int deref_level) const int i; const Type* t = type; for (i=0; iptr_type; + t = (t->eType == ePointer) ? (dynamic_cast(t))->ptr_type : NULL; } assert(t); return t->is_volatile_struct_union(); @@ -1425,7 +1430,7 @@ Variable::is_packed_after_bitfield(void) const { const Variable* parent = this->field_var_of; if (parent == NULL) return false; - if (parent->type->eType == eStruct && parent->type->packed_) { + if (parent->type->eType == eStruct && (dynamic_cast(parent->type))->packed_) { for (size_t i=0; ifield_vars.size(); i++) { if (parent->field_vars[i] == this) { break; diff --git a/src/Variable.h b/src/Variable.h old mode 100644 new mode 100755 index 8b404a0dc..a12b82bd3 --- a/src/Variable.h +++ b/src/Variable.h @@ -3,6 +3,9 @@ // Copyright (c) 2007, 2008, 2009, 2010, 2011, 2013 The University of Utah // All rights reserved. // +// Copyright (c) 2015-2016 Huawei Technologies Co., Ltd +// All rights reserved. +// // This file is part of `csmith', a random generator of C programs. // // Redistribution and use in source and binary forms, with or without @@ -49,6 +52,8 @@ using namespace std; #include "Effect.h" #include "Type.h" +#include "PointerType.h" +#include "AggregateType.h" #include "CVQualifiers.h" #include "StringUtils.h" diff --git a/src/VariableSelector.cpp b/src/VariableSelector.cpp old mode 100644 new mode 100755 index e28086e4b..e908e69ac --- a/src/VariableSelector.cpp +++ b/src/VariableSelector.cpp @@ -3,6 +3,9 @@ // Copyright (c) 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 The University of Utah // All rights reserved. // +// Copyright (c) 2015-2016 Huawei Technologies Co., Ltd +// All rights reserved. +// // This file is part of `csmith', a random generator of C programs. // // Redistribution and use in source and binary forms, with or without @@ -564,11 +567,11 @@ VariableSelector::eager_create_global_struct(Effect::Access access, const CGCont int level = type->get_indirect_level(); const Type *t = 0; if (level == 0) { - t = Type::choose_random_struct_from_type(type, false); + t = AggregateType::choose_random_struct_from_type(type, false); GenerateNewGlobal(access, cg_context, t, qfer); } else if (level == 1) { - t = Type::choose_random_struct_from_type(t->ptr_type, false); + t = AggregateType::choose_random_struct_from_type(((t->eType == ePointer) ? (dynamic_cast(t))->ptr_type : NULL), false); if (qfer) { CVQualifiers qfer1 = qfer->indirect_qualifiers(level); GenerateNewGlobal(access, cg_context, t, &qfer1); @@ -594,11 +597,11 @@ VariableSelector::eager_create_local_struct(Block &block, Effect::Access access, int level = type->get_indirect_level(); const Type *t = 0; if (level == 0) { - t = Type::choose_random_struct_from_type(type, true); + t = AggregateType::choose_random_struct_from_type(type, true); GenerateNewParentLocal(block, access, cg_context, t, qfer); } else if (level == 1) { - t = Type::choose_random_struct_from_type(t->ptr_type, true); + t = AggregateType::choose_random_struct_from_type(((t->eType == ePointer) ? (dynamic_cast(t))->ptr_type : NULL), true); if (qfer) { CVQualifiers qfer1 = qfer->indirect_qualifiers(level); GenerateNewParentLocal(block, access, cg_context, t, &qfer1); @@ -792,7 +795,7 @@ VariableSelector::make_init_value(Effect::Access access, const CGContext &cg_con } // for pointers, take address of a random visible local variable - const Type* type = t->ptr_type; + const Type* type =(dynamic_cast(t))->ptr_type; assert(type); vector vars = find_all_visible_vars(b); @@ -1126,7 +1129,7 @@ VariableSelector::select_deref_pointer(Effect::Access access, const CGContext &c Variable* var = choose_var(vars, access, cg_context, type, qfer, eDereference, invalid_vars); if (var == 0) { - Type* ptr_type = Type::find_pointer_type(type, true); + PointerType* ptr_type = PointerType::find_pointer_type(type, true); assert(ptr_type); CVQualifiers ptr_qfer = (!qfer || qfer->wildcard) ? CVQualifiers::random_qualifiers(ptr_type, access, cg_context, true) diff --git a/src/csmith.vcxproj b/src/csmith.vcxproj old mode 100644 new mode 100755 index 90cdf808a..d4628b1fd --- a/src/csmith.vcxproj +++ b/src/csmith.vcxproj @@ -134,6 +134,7 @@ + @@ -176,6 +177,7 @@ + @@ -206,6 +208,7 @@ + @@ -249,6 +252,7 @@ +