Skip to content

Commit 3e1a5cf

Browse files
Throw an exception for unknown table type in GetValue() (JSBSim-Team#1454)
Co-authored-by: Sean McLeod <sean@seanmcleod.com>
1 parent 9de5fe4 commit 3e1a5cf

1 file changed

Lines changed: 32 additions & 3 deletions

File tree

src/math/FGTable.cpp

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,35 @@ INCLUDES
4545
#include "input_output/FGXMLElement.h"
4646
#include "input_output/string_utilities.h"
4747

48+
/******************************************************************************
49+
UNREACHABLE(msg) macro is designed to optimize impossible code branches away
50+
in release optimized builds and to assert in debug builds with a custom message.
51+
52+
Often used for unreachable default in fully covered switches.
53+
54+
Only use for invariants that can be proven from the structure of the code, not
55+
for assumptions about external inputs like user input, deserialized data etc.
56+
57+
c++23 defines std::unreachable(), however JSBSim currently assumes c++17 as
58+
the minimum, so if being compiled with c++ < 23 the macro makes use of a
59+
compiler specific intrinsic.
60+
******************************************************************************/
61+
62+
#ifdef NDEBUG
63+
#if __cplusplus >= 202302L
64+
#include <utility>
65+
#define UNREACHABLE(msg) std::unreachable();
66+
#elif defined(__GNUC__) || defined(__clang__)
67+
#define UNREACHABLE(msg) __builtin_unreachable();
68+
#elif defined(_MSC_VER)
69+
#define UNREACHABLE(msg) __assume(false);
70+
#else
71+
#define UNREACHABLE(msg) do { } while(0); // fallback: no-op
72+
#endif
73+
#else
74+
#define UNREACHABLE(msg) assert(false && msg); std::abort();
75+
#endif
76+
4877
using namespace std;
4978

5079
namespace JSBSim {
@@ -377,7 +406,7 @@ FGTable::FGTable(std::shared_ptr<FGPropertyManager> pm, Element* el,
377406
*this << buf;
378407
break;
379408
default:
380-
assert(false); // Should never be called
409+
UNREACHABLE("invalid table type") // Should never be called
381410
break;
382411
}
383412
}
@@ -461,7 +490,7 @@ FGTable::FGTable(std::shared_ptr<FGPropertyManager> pm, Element* el,
461490
if (Data.size() != nRows+1) missingData(el, nRows, Data.size()-1);
462491
break;
463492
default:
464-
assert(false); // Should never be called
493+
UNREACHABLE("invalid table type") // Should never be called
465494
break;
466495
}
467496

@@ -541,7 +570,7 @@ double FGTable::GetValue(void) const
541570
return GetValue(lookupPropertyValues.data());
542571
}
543572
default:
544-
assert(false);
573+
UNREACHABLE("invalid table type") // Should never be called
545574
}
546575
}
547576

0 commit comments

Comments
 (0)