From 9f40674d91afafe9525d0b936c40c117fe713fa4 Mon Sep 17 00:00:00 2001 From: Dirk Olbrich Date: Tue, 24 Oct 2023 12:21:32 +0200 Subject: [PATCH] ifcparse: fix clang-tidy warning readability-else-after-return --- src/ifcparse/IfcCharacterDecoder.cpp | 32 ++++---- src/ifcparse/IfcEntityInstanceData.h | 3 +- src/ifcparse/IfcFile.h | 6 +- src/ifcparse/IfcHierarchyHelper.cpp | 25 +++---- src/ifcparse/IfcParse.cpp | 108 +++++++++++++-------------- src/ifcparse/IfcSIPrefix.cpp | 78 +++++++++++-------- src/ifcparse/IfcSchema.cpp | 9 ++- src/ifcparse/IfcSchema.h | 3 +- src/ifcparse/IfcSpfHeader.cpp | 18 ++--- src/ifcparse/IfcUtil.cpp | 33 ++++---- src/ifcparse/IfcWrite.cpp | 3 +- src/ifcparse/IfcWrite.h | 3 +- 12 files changed, 160 insertions(+), 161 deletions(-) diff --git a/src/ifcparse/IfcCharacterDecoder.cpp b/src/ifcparse/IfcCharacterDecoder.cpp index ec9ad2db4c..bd0a101df9 100644 --- a/src/ifcparse/IfcCharacterDecoder.cpp +++ b/src/ifcparse/IfcCharacterDecoder.cpp @@ -92,17 +92,15 @@ class pure_impure_helper { char peek() { if (pure_) { return stream_->peek_at(pointer_); - } else { - return stream_->Peek(); } + return stream_->Peek(); } unsigned int tell() { if (pure_) { return pointer_; - } else { - return stream_->Tell(); } + return stream_->Tell(); } void increment() { @@ -206,27 +204,26 @@ class pure_impure_helper { if (builder_.empty()) { static std::string empty; return empty; - } else { - auto it = std::max_element(builder_.begin(), builder_.end()); - if (*it <= 0x7e) { - std::string r(builder_.begin(), builder_.end()); - return r; - } else { - return IfcUtil::convert_utf8(builder_); - } } - } else if (mode == IfcParse::IfcCharacterDecoder::SUBSTITUTE) { + auto it = std::max_element(builder_.begin(), builder_.end()); + if (*it <= 0x7e) { + std::string r(builder_.begin(), builder_.end()); + return r; + } + return IfcUtil::convert_utf8(builder_); + } + if (mode == IfcParse::IfcCharacterDecoder::SUBSTITUTE) { std::string r; r.reserve(builder_.size()); std::transform(builder_.begin(), builder_.end(), std::back_inserter(r), [&substitution_character](wchar_t c) { if (c >= 0x20 && c <= 0x7e) { return (char)c; - } else { - return substitution_character; } + return substitution_character; }); return r; - } else if (mode == IfcParse::IfcCharacterDecoder::ESCAPE) { + } + if (mode == IfcParse::IfcCharacterDecoder::ESCAPE) { std::stringstream str; str << std::hex << std::setw(4) << std::setfill('0'); std::for_each(builder_.begin(), builder_.end(), [&str](wchar_t c) { @@ -237,9 +234,8 @@ class pure_impure_helper { } }); return str.str(); - } else { - throw IfcParse::IfcException("Invalid conversion mode"); } + throw IfcParse::IfcException("Invalid conversion mode"); } }; } // namespace diff --git a/src/ifcparse/IfcEntityInstanceData.h b/src/ifcparse/IfcEntityInstanceData.h index 095f702191..2b0543be64 100644 --- a/src/ifcparse/IfcEntityInstanceData.h +++ b/src/ifcparse/IfcEntityInstanceData.h @@ -86,9 +86,8 @@ class IFC_PARSE_API IfcEntityInstanceData { } if (type_->as_entity()) { return type_->as_entity()->attribute_count(); - } else { - return 1; } + return 1; } void clearArguments(); diff --git a/src/ifcparse/IfcFile.h b/src/ifcparse/IfcFile.h index 680d5bc615..39883a8590 100644 --- a/src/ifcparse/IfcFile.h +++ b/src/ifcparse/IfcFile.h @@ -206,9 +206,8 @@ class IFC_PARSE_API IfcFile { aggregate_of_instance::ptr untyped_list = instances_by_type(&T::Class()); if (untyped_list) { return untyped_list->as(); - } else { - return typename T::list::ptr(new typename T::list); } + return typename T::list::ptr(new typename T::list); } template @@ -216,9 +215,8 @@ class IFC_PARSE_API IfcFile { aggregate_of_instance::ptr untyped_list = instances_by_type_excl_subtypes(&T::Class()); if (untyped_list) { return untyped_list->as(); - } else { - return typename T::list::ptr(new typename T::list); } + return typename T::list::ptr(new typename T::list); } /// Returns all entities in the file that match the positional argument. diff --git a/src/ifcparse/IfcHierarchyHelper.cpp b/src/ifcparse/IfcHierarchyHelper.cpp index 04f1be90f1..763abb1eef 100644 --- a/src/ifcparse/IfcHierarchyHelper.cpp +++ b/src/ifcparse/IfcHierarchyHelper.cpp @@ -998,20 +998,19 @@ typename Schema::IfcGeometricRepresentationContext* IfcHierarchyHelper:: typename std::map::const_iterator it = contexts.find(s); if (it != contexts.end()) { return it->second; - } else { - typename Schema::IfcProject* project = getSingle(); - if (!project) { - project = addProject(); - } - auto project_contexts = project->RepresentationContexts(); - typename Schema::IfcGeometricRepresentationContext* context = new typename Schema::IfcGeometricRepresentationContext( - boost::none, s, 3, 1e-5, addPlacement3d(), addDoublet(0, 1)); - addEntity(context); - push_back_to_maybe_optional(project_contexts, context); - - project->setRepresentationContexts(project_contexts); - return contexts[s] = context; } + typename Schema::IfcProject* project = getSingle(); + if (!project) { + project = addProject(); + } + auto project_contexts = project->RepresentationContexts(); + typename Schema::IfcGeometricRepresentationContext* context = new typename Schema::IfcGeometricRepresentationContext( + boost::none, s, 3, 1e-5, addPlacement3d(), addDoublet(0, 1)); + addEntity(context); + push_back_to_maybe_optional(project_contexts, context); + + project->setRepresentationContexts(project_contexts); + return contexts[s] = context; } #ifdef HAS_SCHEMA_2x3 diff --git a/src/ifcparse/IfcParse.cpp b/src/ifcparse/IfcParse.cpp index b71c499fa9..10109a6baf 100644 --- a/src/ifcparse/IfcParse.cpp +++ b/src/ifcparse/IfcParse.cpp @@ -355,9 +355,8 @@ Token IfcSpfLexer::Next() { } if (len) { return GeneralTokenPtr(this, pos, stream->Tell()); - } else { - return NoneTokenPtr(); } + return NoneTokenPtr(); } bool IfcSpfStream::is_eof_at(unsigned int local_ptr) { @@ -392,13 +391,13 @@ void IfcSpfLexer::TokenString(unsigned int offset, std::string& buffer) { stream->increment_at(offset); if (c == ' ' || c == '\r' || c == '\n' || c == '\t') { continue; - } else if (c == '\'') { + } + if (c == '\'') { // todo, make decoder use local offset ptr buffer = decoder->get(offset); break; - } else { - buffer.push_back(c); } + buffer.push_back(c); } } @@ -574,14 +573,13 @@ boost::logic::tribool TokenFunc::asLogical(const Token& t) { if (t.type != Token_BOOL) { throw IfcInvalidTokenException(t.startPos, toString(t), "boolean"); } - if (t.value_int == 0) { return false; - } else if (t.value_int == 1) { - return true; - } else { - return boost::logic::indeterminate; } + if (t.value_int == 1) { + return true; + } + return boost::logic::indeterminate; } double TokenFunc::asFloat(const Token& t) { @@ -589,13 +587,12 @@ double TokenFunc::asFloat(const Token& t) { if (t.type == Token_INT) { /// NB: We are being more permissive here then allowed by the standard return t.value_int; - } else // ----> continues beyond preprocessor directive + } // ----> continues beyond preprocessor directive #endif - if (t.type == Token_FLOAT) { + if (t.type == Token_FLOAT) { return t.value_double; - } else { - throw IfcInvalidTokenException(t.startPos, toString(t), "real"); } + throw IfcInvalidTokenException(t.startPos, toString(t), "real"); } const std::string& TokenFunc::asStringRef(const Token& t) { @@ -615,9 +612,8 @@ const std::string& TokenFunc::asStringRef(const Token& t) { std::string TokenFunc::asString(const Token& t) { if (isString(t) || isEnumeration(t) || isBinary(t)) { return asStringRef(t); - } else { - throw IfcInvalidTokenException(t.startPos, toString(t), "string"); } + throw IfcInvalidTokenException(t.startPos, toString(t), "string"); } boost::dynamic_bitset<> TokenFunc::asBinary(const Token& t) { @@ -703,9 +699,8 @@ class vector_or_array { size_t index() const { if (vector_) { return vector_->size(); - } else { - return index_; } + return index_; } }; } // namespace @@ -935,27 +930,35 @@ ArgumentList::~ArgumentList() { IfcUtil::ArgumentType TokenArgument::type() const { if (TokenFunc::isInt(token)) { return IfcUtil::Argument_INT; - } else if (TokenFunc::isBool(token)) { - return IfcUtil::Argument_BOOL; - } else if (TokenFunc::isLogical(token)) { - return IfcUtil::Argument_LOGICAL; - } else if (TokenFunc::isFloat(token)) { - return IfcUtil::Argument_DOUBLE; - } else if (TokenFunc::isString(token)) { - return IfcUtil::Argument_STRING; - } else if (TokenFunc::isEnumeration(token)) { - return IfcUtil::Argument_ENUMERATION; - } else if (TokenFunc::isIdentifier(token)) { - return IfcUtil::Argument_ENTITY_INSTANCE; - } else if (TokenFunc::isBinary(token)) { - return IfcUtil::Argument_BINARY; - } else if (TokenFunc::isOperator(token, '$')) { - return IfcUtil::Argument_NULL; - } else if (TokenFunc::isOperator(token, '*')) { - return IfcUtil::Argument_DERIVED; - } else { - return IfcUtil::Argument_UNKNOWN; } + if (TokenFunc::isBool(token)) { + return IfcUtil::Argument_BOOL; + } + if (TokenFunc::isLogical(token)) { + return IfcUtil::Argument_LOGICAL; + } + if (TokenFunc::isFloat(token)) { + return IfcUtil::Argument_DOUBLE; + } + if (TokenFunc::isString(token)) { + return IfcUtil::Argument_STRING; + } + if (TokenFunc::isEnumeration(token)) { + return IfcUtil::Argument_ENUMERATION; + } + if (TokenFunc::isIdentifier(token)) { + return IfcUtil::Argument_ENTITY_INSTANCE; + } + if (TokenFunc::isBinary(token)) { + return IfcUtil::Argument_BINARY; + } + if (TokenFunc::isOperator(token, '$')) { + return IfcUtil::Argument_NULL; + } + if (TokenFunc::isOperator(token, '*')) { + return IfcUtil::Argument_DERIVED; + } + return IfcUtil::Argument_UNKNOWN; } // @@ -973,9 +976,8 @@ Argument* TokenArgument::operator[](unsigned int /*i*/) const { throw IfcExcepti std::string TokenArgument::toString(bool upper) const { if (upper && TokenFunc::isString(token)) { return IfcWrite::IfcCharacterEncoder(TokenFunc::asString(token)); - } else { - return TokenFunc::toString(token); } + return TokenFunc::toString(token); } bool TokenArgument::isNull() const { return TokenFunc::isOperator(token, '$'); } @@ -1148,9 +1150,8 @@ IfcEntityInstanceData::~IfcEntityInstanceData() { unsigned IfcEntityInstanceData::set_id(boost::optional i) { if (i) { return id_ = *i; - } else { - return id_ = file->FreshId(); } + return id_ = file->FreshId(); } // @@ -1215,9 +1216,8 @@ IfcUtil::ArgumentType get_argument_type(const IfcParse::declaration* decl, size_ if (pt == 0) { return IfcUtil::Argument_UNKNOWN; - } else { - return IfcUtil::from_parameter_type(pt); } + return IfcUtil::from_parameter_type(pt); } } // namespace @@ -1246,12 +1246,10 @@ Argument* IfcEntityInstanceData::getArgument(size_t i) const { if (i < getArgumentCount()) { if (attributes_[i] == nullptr) { return &static_null_attribute; - } else { - return attributes_[i]; } - } else { - throw IfcParse::IfcException("Attribute index out of range"); + return attributes_[i]; } + throw IfcParse::IfcException("Attribute index out of range"); } class unregister_inverse_visitor { @@ -1799,13 +1797,12 @@ class traversal_recorder { aggregate_of_instance::ptr get_list() const { if (mode_ == 0) { return list_; - } else { - aggregate_of_instance::ptr l(new aggregate_of_instance); - for (auto& p : instances_by_level_) { - l->push(p.second); - } - return l; } + aggregate_of_instance::ptr l(new aggregate_of_instance); + for (auto& p : instances_by_level_) { + l->push(p.second); + } + return l; } }; @@ -2415,9 +2412,8 @@ IfcUtil::IfcBaseClass* IfcFile::instance_by_guid(const std::string& guid) { entity_by_guid_t::const_iterator it = byguid.find(guid); if (it == byguid.end()) { throw IfcException("Instance with GlobalId '" + guid + "' not found"); - } else { - return it->second; } + return it->second; } // FIXME: Test destructor to delete entity and arg allocations diff --git a/src/ifcparse/IfcSIPrefix.cpp b/src/ifcparse/IfcSIPrefix.cpp index 3f80a054de..856be793f8 100644 --- a/src/ifcparse/IfcSIPrefix.cpp +++ b/src/ifcparse/IfcSIPrefix.cpp @@ -59,39 +59,53 @@ double IfcParse::IfcSIPrefixToValue(const std::string& v) { if (v == "EXA") { return 1.e18; - } else if (v == "PETA") { - return 1.e15; - } else if (v == "TERA") { - return 1.e12; - } else if (v == "GIGA") { - return 1.e9; - } else if (v == "MEGA") { - return 1.e6; - } else if (v == "KILO") { - return 1.e3; - } else if (v == "HECTO") { - return 1.e2; - } else if (v == "DECA") { - return 1.e1; - } else if (v == "DECI") { - return 1.e-1; - } else if (v == "CENTI") { - return 1.e-2; - } else if (v == "MILLI") { - return 1.e-3; - } else if (v == "MICRO") { - return 1.e-6; - } else if (v == "NANO") { - return 1.e-9; - } else if (v == "PICO") { - return 1.e-12; - } else if (v == "FEMTO") { - return 1.e-15; - } else if (v == "ATTO") { - return 1.e-18; - } else { - return 1.; } + if (v == "PETA") { + return 1.e15; + } + if (v == "TERA") { + return 1.e12; + } + if (v == "GIGA") { + return 1.e9; + } + if (v == "MEGA") { + return 1.e6; + } + if (v == "KILO") { + return 1.e3; + } + if (v == "HECTO") { + return 1.e2; + } + if (v == "DECA") { + return 1.e1; + } + if (v == "DECI") { + return 1.e-1; + } + if (v == "CENTI") { + return 1.e-2; + } + if (v == "MILLI") { + return 1.e-3; + } + if (v == "MICRO") { + return 1.e-6; + } + if (v == "NANO") { + return 1.e-9; + } + if (v == "PICO") { + return 1.e-12; + } + if (v == "FEMTO") { + return 1.e-15; + } + if (v == "ATTO") { + return 1.e-18; + } + return 1.; } template diff --git a/src/ifcparse/IfcSchema.cpp b/src/ifcparse/IfcSchema.cpp index 40c46bbf3d..1a929974f1 100644 --- a/src/ifcparse/IfcSchema.cpp +++ b/src/ifcparse/IfcSchema.cpp @@ -74,7 +74,8 @@ bool IfcParse::declaration::is(const std::string& name) const { if (this->as_entity() && this->as_entity()->supertype()) { return this->as_entity()->supertype()->is(name); - } else if (this->as_type_declaration()) { + } + if (this->as_type_declaration()) { const IfcParse::named_type* nt = this->as_type_declaration()->declared_type()->as_named_type(); if (nt) { return nt->is(name); @@ -91,7 +92,8 @@ bool IfcParse::declaration::is(const IfcParse::declaration& decl) const { if (this->as_entity() && this->as_entity()->supertype()) { return this->as_entity()->supertype()->is(decl); - } else if (this->as_type_declaration()) { + } + if (this->as_type_declaration()) { const IfcParse::named_type* nt = this->as_type_declaration()->declared_type()->as_named_type(); if (nt) { return nt->is(decl); @@ -153,9 +155,8 @@ IfcParse::schema_definition::~schema_definition() { IfcUtil::IfcBaseClass* IfcParse::schema_definition::instantiate(IfcEntityInstanceData* data) const { if (factory_) { return (*factory_)(data); - } else { - return new IfcUtil::IfcLateBoundEntity(data->type(), data); } + return new IfcUtil::IfcLateBoundEntity(data->type(), data); } void IfcParse::register_schema(schema_definition* s) { diff --git a/src/ifcparse/IfcSchema.h b/src/ifcparse/IfcSchema.h index 7c2966b38f..aad5e1db03 100644 --- a/src/ifcparse/IfcSchema.h +++ b/src/ifcparse/IfcSchema.h @@ -489,9 +489,8 @@ class IFC_PARSE_API schema_definition { std::vector::const_iterator it = std::lower_bound(declarations_.begin(), declarations_.end(), *name_ptr, declaration_by_name_cmp()); if (it == declarations_.end() || (**it).name_uc() != *name_ptr) { throw IfcParse::IfcException("Entity with name '" + name + "' not found in schema '" + name_ + "'"); - } else { - return *it; } + return *it; } const declaration* declaration_by_name(int name) const { diff --git a/src/ifcparse/IfcSpfHeader.cpp b/src/ifcparse/IfcSpfHeader.cpp index 019bfc539e..588e125048 100644 --- a/src/ifcparse/IfcSpfHeader.cpp +++ b/src/ifcparse/IfcSpfHeader.cpp @@ -133,49 +133,43 @@ void IfcSpfHeader::write(std::ostream& os) const { const FileDescription& IfcSpfHeader::file_description() const { if (_file_description) { return *_file_description; - } else { - throw IfcException("File description not set"); } + throw IfcException("File description not set"); } const FileName& IfcSpfHeader::file_name() const { if (_file_name) { return *_file_name; - } else { - throw IfcException("File name not set"); } + throw IfcException("File name not set"); } const FileSchema& IfcSpfHeader::file_schema() const { if (_file_schema) { return *_file_schema; - } else { - throw IfcException("File schema not set"); } + throw IfcException("File schema not set"); } FileDescription& IfcSpfHeader::file_description() { if (_file_description) { return *_file_description; - } else { - throw IfcException("File description not set"); } + throw IfcException("File description not set"); } FileName& IfcSpfHeader::file_name() { if (_file_name) { return *_file_name; - } else { - throw IfcException("File name not set"); } + throw IfcException("File name not set"); } FileSchema& IfcSpfHeader::file_schema() { if (_file_schema) { return *_file_schema; - } else { - throw IfcException("File schema not set"); } + throw IfcException("File schema not set"); } FileDescription::FileDescription(IfcFile* file) : HeaderEntity(FILE_DESCRIPTION, 2, file) {} diff --git a/src/ifcparse/IfcUtil.cpp b/src/ifcparse/IfcUtil.cpp index 41fdae812c..81c944e233 100644 --- a/src/ifcparse/IfcUtil.cpp +++ b/src/ifcparse/IfcUtil.cpp @@ -222,25 +222,26 @@ void IfcUtil::IfcBaseClass::data(IfcEntityInstanceData* d) { } IfcUtil::ArgumentType IfcUtil::make_aggregate(IfcUtil::ArgumentType elem_type) { - if (elem_type == IfcUtil::Argument_INT) { + switch (elem_type) { + case IfcUtil::Argument_INT: return IfcUtil::Argument_AGGREGATE_OF_INT; - } else if (elem_type == IfcUtil::Argument_DOUBLE) { + case IfcUtil::Argument_DOUBLE: return IfcUtil::Argument_AGGREGATE_OF_DOUBLE; - } else if (elem_type == IfcUtil::Argument_STRING) { + case IfcUtil::Argument_STRING: return IfcUtil::Argument_AGGREGATE_OF_STRING; - } else if (elem_type == IfcUtil::Argument_BINARY) { + case IfcUtil::Argument_BINARY: return IfcUtil::Argument_AGGREGATE_OF_BINARY; - } else if (elem_type == IfcUtil::Argument_ENTITY_INSTANCE) { + case IfcUtil::Argument_ENTITY_INSTANCE: return IfcUtil::Argument_AGGREGATE_OF_ENTITY_INSTANCE; - } else if (elem_type == IfcUtil::Argument_AGGREGATE_OF_INT) { + case IfcUtil::Argument_AGGREGATE_OF_INT: return IfcUtil::Argument_AGGREGATE_OF_AGGREGATE_OF_INT; - } else if (elem_type == IfcUtil::Argument_AGGREGATE_OF_DOUBLE) { + case IfcUtil::Argument_AGGREGATE_OF_DOUBLE: return IfcUtil::Argument_AGGREGATE_OF_AGGREGATE_OF_DOUBLE; - } else if (elem_type == IfcUtil::Argument_AGGREGATE_OF_ENTITY_INSTANCE) { + case IfcUtil::Argument_AGGREGATE_OF_ENTITY_INSTANCE: return IfcUtil::Argument_AGGREGATE_OF_AGGREGATE_OF_ENTITY_INSTANCE; - } else if (elem_type == IfcUtil::Argument_EMPTY_AGGREGATE) { + case IfcUtil::Argument_EMPTY_AGGREGATE: return IfcUtil::Argument_AGGREGATE_OF_EMPTY_AGGREGATE; - } else { + default: return IfcUtil::Argument_UNKNOWN; } } @@ -254,14 +255,18 @@ IfcUtil::ArgumentType IfcUtil::from_parameter_type(const IfcParse::parameter_typ if (at) { return make_aggregate(from_parameter_type(at->type_of_element())); - } else if (nt) { + } + if (nt) { if (nt->declared_type()->as_entity()) { return IfcUtil::Argument_ENTITY_INSTANCE; - } else if (nt->declared_type()->as_enumeration_type()) { + } + if (nt->declared_type()->as_enumeration_type()) { return IfcUtil::Argument_ENUMERATION; - } else if (nt->declared_type()->as_select_type()) { + } + if (nt->declared_type()->as_select_type()) { return IfcUtil::Argument_ENTITY_INSTANCE; - } else if (nt->declared_type()->as_type_declaration()) { + } + if (nt->declared_type()->as_type_declaration()) { return from_parameter_type(nt->declared_type()->as_type_declaration()->declared_type()); } } else if (st) { diff --git a/src/ifcparse/IfcWrite.cpp b/src/ifcparse/IfcWrite.cpp index 3796edf8ad..c660ac4d76 100644 --- a/src/ifcparse/IfcWrite.cpp +++ b/src/ifcparse/IfcWrite.cpp @@ -281,9 +281,8 @@ unsigned int IfcWriteArgument::size() const { const int size = container.apply_visitor(v); if (size == -1) { throw IfcParse::IfcException("Invalid cast"); - } else { - return size; } + return size; } IfcUtil::ArgumentType IfcWriteArgument::type() const { diff --git a/src/ifcparse/IfcWrite.h b/src/ifcparse/IfcWrite.h index b25f2eb5b2..496ce987db 100644 --- a/src/ifcparse/IfcWrite.h +++ b/src/ifcparse/IfcWrite.h @@ -122,9 +122,8 @@ class IFC_PARSE_API IfcWriteArgument : public Argument { const T& as() const { if (const T* val = boost::get(&container)) { return *val; - } else { - throw IfcParse::IfcException("Invalid cast"); } + throw IfcParse::IfcException("Invalid cast"); } template