Skip to content

Commit

Permalink
Rewrite switch statements to avoid compiler warnings.
Browse files Browse the repository at this point in the history
We have a problem with switch statements: Some compiler versions warn
when a switch doesn't handle all options, so we need them to be
complete. But if they are complete, there is never a case where the
code falls out of the switch, so the return at the end of the function
is never called (which some compilers don't like), but if there is no
return at the end of the function other compiler versions complain. The
only way to handle this is to pick one of the cases, make it into a
default case that drops out of the switch and then do the return
outside the switch.
  • Loading branch information
joto committed Mar 7, 2017
1 parent 9db7541 commit 985ef06
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 29 deletions.
2 changes: 1 addition & 1 deletion include/osmium/io/detail/string_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ namespace osmium {

inline void append_xml_encoded_string(std::string& out, const char* data) {
for (; *data != '\0'; ++data) {
switch(*data) {
switch (*data) {
case '&': out += "&"; break;
case '\"': out += """; break;
case '\'': out += "'"; break;
Expand Down
9 changes: 3 additions & 6 deletions include/osmium/io/file_compression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,17 @@ namespace osmium {
bzip2 = 2
};

// avoid g++ false positive
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wreturn-type"
inline const char* as_string(file_compression compression) {
switch (compression) {
case file_compression::none:
return "none";
case file_compression::gzip:
return "gzip";
case file_compression::bzip2:
return "bzip2";
default: // file_compression::none:
break;
}
return "none";
}
#pragma GCC diagnostic pop

template <typename TChar, typename TTraits>
inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, const file_compression compression) {
Expand Down
9 changes: 3 additions & 6 deletions include/osmium/io/file_format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,8 @@ namespace osmium {
yes = 1
};

// avoid g++ false positive
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wreturn-type"
inline const char* as_string(file_format format) {
switch (format) {
case file_format::unknown:
return "unknown";
case file_format::xml:
return "XML";
case file_format::pbf:
Expand All @@ -73,9 +68,11 @@ namespace osmium {
return "O5M";
case file_format::debug:
return "DEBUG";
default: // file_format::unknown
break;
}
return "unknown";
}
#pragma GCC diagnostic pop

template <typename TChar, typename TTraits>
inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, const file_format format) {
Expand Down
21 changes: 9 additions & 12 deletions include/osmium/osm/item_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ namespace osmium {

inline item_type char_to_item_type(const char c) noexcept {
switch (c) {
case 'X':
return item_type::undefined;
case 'n':
return item_type::node;
case 'w':
Expand All @@ -113,18 +111,14 @@ namespace osmium {
return item_type::inner_ring;
case 'D':
return item_type::changeset_discussion;
default:
return item_type::undefined;
default: // 'X'
break;
}
return item_type::undefined;
}

// avoid g++ false positive
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wreturn-type"
inline char item_type_to_char(const item_type type) noexcept {
switch (type) {
case item_type::undefined:
return 'X';
case item_type::node:
return 'n';
case item_type::way:
Expand All @@ -149,13 +143,14 @@ namespace osmium {
return 'I';
case item_type::changeset_discussion:
return 'D';
default: // item_type::undefined
break;
}
return 'X';
}

inline const char* item_type_to_name(const item_type type) noexcept {
switch (type) {
case item_type::undefined:
return "undefined";
case item_type::node:
return "node";
case item_type::way:
Expand All @@ -180,9 +175,11 @@ namespace osmium {
return "inner_ring";
case item_type::changeset_discussion:
return "changeset_discussion";
default: // item_type::undefined
break;
}
return "undefined";
}
#pragma GCC diagnostic pop

template <typename TChar, typename TTraits>
inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, const item_type item_type) {
Expand Down
10 changes: 6 additions & 4 deletions include/osmium/util/memory_mapping.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -644,9 +644,10 @@ inline DWORD osmium::util::MemoryMapping::get_protection() const noexcept {
return PAGE_READONLY;
case mapping_mode::write_private:
return PAGE_WRITECOPY;
case mapping_mode::write_shared:
return PAGE_READWRITE;
default: // mapping_mode::write_shared
break;
}
return PAGE_READWRITE;
}

inline DWORD osmium::util::MemoryMapping::get_flags() const noexcept {
Expand All @@ -655,9 +656,10 @@ inline DWORD osmium::util::MemoryMapping::get_flags() const noexcept {
return FILE_MAP_READ;
case mapping_mode::write_private:
return FILE_MAP_COPY;
case mapping_mode::write_shared:
return FILE_MAP_WRITE;
default: // mapping_mode::write_shared
break;
}
return FILE_MAP_WRITE;
}

inline HANDLE osmium::util::MemoryMapping::get_handle() const noexcept {
Expand Down

0 comments on commit 985ef06

Please sign in to comment.