From fe79aa81a500e078ec29fb8aaa03c3badac5fd18 Mon Sep 17 00:00:00 2001 From: Derek Bankieris Date: Wed, 17 May 2017 09:37:22 -0500 Subject: [PATCH] Improve printable field detection Renamed determinePrintAttr to isPrintable. Different callers need to filter on different IO specs, so I added an ioMask parameter. Additional refactoring. Refs #427 --- .../PrintFileContents10.cpp | 80 +++++++++---------- .../Interface_Code_Gen/PrintFileContents10.hh | 2 +- .../PrintFileContentsBase.cpp | 12 +-- .../PrintFileContentsBase.hh | 4 +- 4 files changed, 46 insertions(+), 52 deletions(-) diff --git a/trick_source/codegen/Interface_Code_Gen/PrintFileContents10.cpp b/trick_source/codegen/Interface_Code_Gen/PrintFileContents10.cpp index 4cb79352..d2192a67 100644 --- a/trick_source/codegen/Interface_Code_Gen/PrintFileContents10.cpp +++ b/trick_source/codegen/Interface_Code_Gen/PrintFileContents10.cpp @@ -1,4 +1,4 @@ - +#include #include #include "PrintFileContents10.hh" @@ -54,51 +54,51 @@ void PrintFileContents10::print_enum_attr(std::ostream & ostream , EnumValues * } /** Prints attributes for a field */ -void PrintFileContents10::print_field_attr(std::ostream & ostream , FieldDescription * fdes ) { +void PrintFileContents10::print_field_attr(std::ostream & ostream , FieldDescription & fdes ) { int array_dim ; - ostream << "{\"" << fdes->getName() << "\"" // name - << ", \"" << fdes->getFullyQualifiedMangledTypeName("__") << "\"" // type_name - << ", \"" << fdes->getUnits() << "\"" // units + ostream << "{\"" << fdes.getName() << "\"" // name + << ", \"" << fdes.getFullyQualifiedMangledTypeName("__") << "\"" // type_name + << ", \"" << fdes.getUnits() << "\"" // units << ", \"\", \"\"," << std::endl // alias, user_defined - << " \"" << fdes->getDescription() << "\"," << std::endl // description - << " " << fdes->getIO() // io - << "," << fdes->getEnumString() ; // type + << " \"" << fdes.getDescription() << "\"," << std::endl // description + << " " << fdes.getIO() // io + << "," << fdes.getEnumString() ; // type // There are several cases when printing the size of a variable. - if ( fdes->isBitField() ) { + if ( fdes.isBitField() ) { // bitfields are handled in 4 byte (32 bit) chunks ostream << ", 4" ; - } else if ( fdes->isRecord() or fdes->isEnum() or fdes->getTypeName().empty() ) { + } else if ( fdes.isRecord() or fdes.isEnum() or fdes.getTypeName().empty() ) { // records enums use io_src_get_size. The sentinel has no typename ostream << ", 0" ; } else { // print size of the underlying type - ostream << ", sizeof(" << fdes->getTypeName() << ")" ; + ostream << ", sizeof(" << fdes.getTypeName() << ")" ; } ostream << ", 0, 0, Language_CPP" ; // range_min, range_max, language - ostream << ", " << (fdes->isStatic() << 1) + (fdes->isDashDashUnits() << 2) << "," << std::endl ; // mods - if ( fdes->isBitField() ) { + ostream << ", " << (fdes.isStatic() << 1) + (fdes.isDashDashUnits() << 2) << "," << std::endl ; // mods + if ( fdes.isBitField() ) { // For bitfields we need the offset to start on 4 byte boundaries because that is what our // insert and extract bitfield routines work with. - ostream << " " << (fdes->getFieldOffset() - (fdes->getFieldOffset() % 32)) / 8 ; // offset + ostream << " " << (fdes.getFieldOffset() - (fdes.getFieldOffset() % 32)) / 8 ; // offset } else { - ostream << " " << (fdes->getFieldOffset() / 8) ; // offset + ostream << " " << (fdes.getFieldOffset() / 8) ; // offset } ostream << ", NULL" ; // attr - ostream << ", " << fdes->getNumDims() ; // num_index + ostream << ", " << fdes.getNumDims() ; // num_index ostream << ", {" ; - if ( fdes->isBitField() ) { - ostream << "{" << fdes->getBitFieldWidth() ; // size of bitfield - ostream << ", " << 32 - (fdes->getFieldOffset() % 32) - fdes->getBitFieldWidth() << "}" ; // start bit + if ( fdes.isBitField() ) { + ostream << "{" << fdes.getBitFieldWidth() ; // size of bitfield + ostream << ", " << 32 - (fdes.getFieldOffset() % 32) - fdes.getBitFieldWidth() << "}" ; // start bit } else { - array_dim = fdes->getArrayDim(0) ; + array_dim = fdes.getArrayDim(0) ; if ( array_dim < 0 ) array_dim = 0 ; ostream << "{" << array_dim << ", 0}" ; // index 0 } unsigned int ii ; for ( ii = 1 ; ii < 8 ; ii++ ) { - array_dim = fdes->getArrayDim(ii) ; + array_dim = fdes.getArrayDim(ii) ; if ( array_dim < 0 ) array_dim = 0 ; ostream << ", {" << array_dim << ", 0}" ; // indexes 1 through 7 } @@ -113,17 +113,14 @@ void PrintFileContents10::print_class_attr(std::ostream & ostream , ClassValues print_open_extern_c(ostream) ; ostream << "ATTRIBUTES attr" << c->getFullyQualifiedMangledTypeName("__") << "[] = {" << std::endl ; - for (auto& fieldDescription : c->getFieldDescriptions()) { - if ( determinePrintAttr(c , fieldDescription) ) { - print_field_attr(ostream, fieldDescription) ; + for (FieldDescription* fieldDescription : getPrintableFields(*c)) { + print_field_attr(ostream, *fieldDescription) ; ostream << "," << std::endl ; - } } // Print an empty sentinel attribute at the end of the class. - FieldDescription * new_fdes = new FieldDescription(std::string("")) ; + FieldDescription new_fdes(std::string("")) ; print_field_attr(ostream, new_fdes) ; ostream << " };" << std::endl ; - delete new_fdes ; print_close_extern_c(ostream) ; } @@ -206,10 +203,8 @@ void PrintFileContents10::print_init_attr_func( std::ostream & ostream , ClassVa << " initialized = 1;\n\n" ; unsigned int ii = 0; - for ( auto field : cv->getFieldDescriptions() ) { - if ( determinePrintAttr(cv, field) ) { - print_field_init_attr_stmts(ostream, field, cv, ii++) ; - } + for ( FieldDescription* field : getPrintableFields(*cv) ) { + print_field_init_attr_stmts(ostream, field, cv, ii++) ; } print_inherited_add_attr_info(ostream, cv ) ; ostream << "}\n" ; @@ -320,7 +315,8 @@ void PrintFileContents10::print_clear_stl(std::ostream & ostream , FieldDescript } void PrintFileContents10::print_stl_helper(std::ostream & ostream , ClassValues * cv ) { - auto& fieldDescriptions = cv->getFieldDescriptions(); + std::vector fieldDescriptions = getPrintableFields(*cv, 0x3 << 2); + fieldDescriptions.erase(std::remove_if(fieldDescriptions.begin(), fieldDescriptions.end(), [](FieldDescription* field) {return !field->isSTL();}), fieldDescriptions.end()); if (!fieldDescriptions.size()) { return; @@ -328,17 +324,15 @@ void PrintFileContents10::print_stl_helper(std::ostream & ostream , ClassValues print_open_extern_c(ostream) ; - for (auto& field : fieldDescriptions) { - if ( field->isSTL() and determinePrintAttr(cv, field) ) { - if (field->isCheckpointable()) { - print_checkpoint_stl(ostream, field, cv) ; - print_post_checkpoint_stl(ostream, field, cv) ; - } - if (field->isRestorable()) { - print_restore_stl(ostream, field, cv) ; - if (field->hasSTLClear()) { - print_clear_stl(ostream, field, cv) ; - } + for (FieldDescription* field : fieldDescriptions) { + if (field->isCheckpointable()) { + print_checkpoint_stl(ostream, field, cv) ; + print_post_checkpoint_stl(ostream, field, cv) ; + } + if (field->isRestorable()) { + print_restore_stl(ostream, field, cv) ; + if (field->hasSTLClear()) { + print_clear_stl(ostream, field, cv) ; } } } diff --git a/trick_source/codegen/Interface_Code_Gen/PrintFileContents10.hh b/trick_source/codegen/Interface_Code_Gen/PrintFileContents10.hh index c1a4f428..6802f43d 100644 --- a/trick_source/codegen/Interface_Code_Gen/PrintFileContents10.hh +++ b/trick_source/codegen/Interface_Code_Gen/PrintFileContents10.hh @@ -53,7 +53,7 @@ class PrintFileContents10 : public PrintFileContentsBase { void print_enum_attr(std::ostream & outfile , EnumValues * in_enum) ; /** Prints attributes for a field */ - void print_field_attr(std::ostream & outfile , FieldDescription * fdes ) ; + void print_field_attr(std::ostream & outfile , FieldDescription & fdes ) ; /** Prints class attributes */ void print_class_attr(std::ostream & outfile , ClassValues * in_class) ; diff --git a/trick_source/codegen/Interface_Code_Gen/PrintFileContentsBase.cpp b/trick_source/codegen/Interface_Code_Gen/PrintFileContentsBase.cpp index d647cd47..258b54e8 100644 --- a/trick_source/codegen/Interface_Code_Gen/PrintFileContentsBase.cpp +++ b/trick_source/codegen/Interface_Code_Gen/PrintFileContentsBase.cpp @@ -41,7 +41,7 @@ void PrintFileContentsBase::print_units_map(std::ostream & ostream, ClassValues ostream << " }\n" ; } - ostream << "} um" << name << ";\n" ; + ostream << "} um" << name << ";\n\n" ; } /* Utility routines for printing */ @@ -56,20 +56,20 @@ void PrintFileContentsBase::print_close_extern_c(std::ostream & ostream) { /* internal function determines if a particular field is printable based on access to the field and the presense of init_attr friends. */ -bool PrintFileContentsBase::determinePrintAttr( ClassValues * c , FieldDescription * fdes ) { - if ( !fdes->getTypeName().compare("void") or !fdes->getChkpntIO() or !fdes->getEnumString().compare("TRICK_VOID")) { +bool PrintFileContentsBase::isPrintable( ClassValues * c , FieldDescription * fdes , unsigned int ioMask) { + if ( !(fdes->getIO() & ioMask) || !fdes->getTypeName().compare("void") || !fdes->getEnumString().compare("TRICK_VOID")) { return false; } - if ( fdes->getAccess() == clang::AS_public or (!fdes->isStatic() and !global_compat15 and !c->isCompat15())) { + if ( fdes->getAccess() == clang::AS_public || (!fdes->isStatic() && !global_compat15 && !c->isCompat15())) { return true; } return c->getHasInitAttrFriend() && ( !fdes->isInherited() || fdes->getAccess() == clang::AS_protected ) ; } -std::vector PrintFileContentsBase::getPrintableFields(ClassValues& classValues) { +std::vector PrintFileContentsBase::getPrintableFields(ClassValues& classValues, unsigned int ioMask) { std::vector results; for (auto& field : classValues.getFieldDescriptions()) { - if (determinePrintAttr(&classValues, field) and field->getUnits().compare("1")) { + if (isPrintable(&classValues, field)) { results.push_back(field); } } diff --git a/trick_source/codegen/Interface_Code_Gen/PrintFileContentsBase.hh b/trick_source/codegen/Interface_Code_Gen/PrintFileContentsBase.hh index b985f8d3..14f25dac 100644 --- a/trick_source/codegen/Interface_Code_Gen/PrintFileContentsBase.hh +++ b/trick_source/codegen/Interface_Code_Gen/PrintFileContentsBase.hh @@ -53,10 +53,10 @@ class PrintFileContentsBase { void print_close_extern_c(std::ostream & ostream) ; /* internal function determines if a particular field is printable */ - bool determinePrintAttr(ClassValues * c , FieldDescription *fdes) ; + bool isPrintable(ClassValues * c , FieldDescription *fdes , unsigned int ioMask = 0xFFFFFFF) ; /* gets a vector of fields that can be printed */ - std::vector getPrintableFields(ClassValues& classValues); + std::vector getPrintableFields(ClassValues& classValues, unsigned int ioMask = 0xFFFFFFF); } ;