Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix false positive no return warnings in msvc #2132

Merged
merged 1 commit into from
Oct 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/cata_unreachable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once
#ifndef CATA_SRC_CATA_UNREACHABLE_H
#define CATA_SRC_CATA_UNREACHABLE_H

namespace cata
{

/**
* @brief Marks unreachable code.
*
* Utility function to mark unreachable code to help compiler with optimizations.
*
* Usage:
* void ( bool always_true )
* {
* if ( always_true ) {
* return;
* } else {
* // If always_true happens to be false, this will cause Undefined Behavior.
* cata::unreachable();
* }
* }
*
* Source: https://stackoverflow.com/a/65258501
*/
#ifdef __GNUC__ // GCC 4.8+, Clang, Intel and other compilers compatible with GCC (-std=c++0x or above)
[[noreturn]] inline __attribute__( ( always_inline ) ) void unreachable()
{
__builtin_unreachable();
}
#elif defined(_MSC_VER) // MSVC
[[noreturn]] __forceinline void unreachable()
{
__assume( false );
}
#else // ???
inline void unreachable() {}
#endif

} // namespace cata

#endif // CATA_SRC_CATA_UNREACHABLE_H
3 changes: 3 additions & 0 deletions src/lightmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "avatar.h"
#include "calendar.h"
#include "cata_unreachable.h"
#include "cata_utility.h"
#include "character.h"
#include "colony.h"
Expand Down Expand Up @@ -846,6 +847,7 @@ void cast_zlight_segment(
( *blocked_caches[p.z + OVERMAP_DEPTH] )[p.x - 1][p.y + 1].ne );
break;
}
cata::unreachable();
};

float radius = 60.0f - offset_distance;
Expand Down Expand Up @@ -1145,6 +1147,7 @@ void castLight( Out( &output_cache )[MAPSIZE_X][MAPSIZE_Y],
return ( p.x > 1 && p.y < MAPSIZE_Y - 1 && blocked_array[p.x - 1][p.y + 1].ne );
break;
}
cata::unreachable();
};

float newStart = 0.0f;
Expand Down