replace goto inside a macro with something else - c++

I'm attempting to remove goto statement inside Mach7, because goto is not allowed in constexpr function:
#define MatchQ(s) { \
XTL_MATCH_PREAMBULA(s) \
enum { __base_counter = XTL_COUNTER }; \
typedef mch::unified_switch<source_type> switch_traits; \
XTL_PRELOADABLE_LOCAL_STATIC(XTL_CPP0X_TYPENAME switch_traits::static_data_type,static_data,match_uid_type,XTL_EMPTY()); \
XTL_CPP0X_TYPENAME switch_traits::local_data_type local_data; \
bool processed = false; \
size_t jump_target = switch_traits::choose(subject_ptr,static_data,local_data); \
XTL_CONCAT(ReMatch,__LINE__): \
switch (jump_target) \
{ \
XTL_NON_REDUNDANCY_ONLY(default:) \
{ XTL_REDUNDANCY_ONLY(try){{ \
if (switch_traits::on_default(jump_target,local_data,static_data)) \
goto XTL_CONCAT(ReMatch,__LINE__); \
XTL_SUBCLAUSE_FIRST
The codes above use goto here: goto XTL_CONCAT(ReMatch,__LINE__);, which is possible to jump to upside of switch statement.
How to replace goto here with something else?

with the help of #ozlsn and gcc -E, the replacement is done.
persuade codes:
while(true)
{
bool continue_flag = false;
switch(var)
{
default:
if(something)
continue_flag = true;
break;
// do something
OtherCases:
// do something
}
if (!contine_flag)
break;
}
full commit here: https://github.com/FirstLoveLife/Mach7/commit/3db24a337a7643018ed9e12ac95f53f9a036251c
Here is a related QA: Using continue in a switch statement

Related

TypeScript macro expansion

I need to reproduce the functionality of this C code but in typescript. The purpose of this code is mainly to simplify error checking as stated in JPL's The Power of Ten but I couldn't find a way to do it in TS.
#define ecall(retVal, l_call, format, ...) do { \
int _rv = (l_call); \
if(_rv < 0) { \
printf(format, __VA_ARGS__); \
return retVal; \
} \
else { \
check(); \
} \
} while(0)

Integration of a singly linked tail queue support in C++

I'm currently trying to integrate an ASN.1 decoder code written in C to the C++ Builder XE6 environment. I've encountered some problems while using the Singly linked tail queue support provided by the library, with the following call :
asn1p_wsyntx_chunk_t *wc;
while((wc = TQ_REMOVE(&(wx->chunks), next)))
asn1p_wsyntx_chunk_free(wc);
The error that I get is :
member reference type 'int' is not a pointer
The definitions of asn1p_wsyntx_chunk_t (wc) and asn1p_wsyntx_t (wx) are :
typedef struct asn1p_wsyntx_chunk_s {
enum {
WC_LITERAL,
WC_WHITESPACE,
WC_FIELD,
WC_OPTIONALGROUP
} type;
/*
* WC_LITERAL -> {token}
* WC_WHITESPACE -> {token}
* WC_FIELD -> {token}
* WC_OPTIONALGROUP -> {syntax}
*/
union {
char *token;
struct asn1p_wsyntx_s *syntax;
} content;
TQ_ENTRY(struct asn1p_wsyntx_chunk_s) next;
} asn1p_wsyntx_chunk_t;
typedef struct asn1p_wsyntx_s {
TQ_HEAD(struct asn1p_wsyntx_chunk_s) chunks;
} asn1p_wsyntx_t;
The code of the Singly linked tail is this one :
#define TQ_HEAD(type) \
struct { \
type *tq_head; \
type**tq_tail; \
}
#define TQ_MOVE(to, from) do { \
if(&(TQ_FIRST(from)) == (from)->tq_tail) { \
TQ_INIT(to); \
} else { \
(to)->tq_head = (from)->tq_head; \
(to)->tq_tail = (from)->tq_tail; \
} \
TQ_INIT(from); \
} while(0)
#define TQ_ENTRY(type) \
struct { \
type *tq_next; \
}
#define TQ_FIRST(headp) ((headp)->tq_head)
#define TQ_NEXT(el, field) ((el)->field.tq_next)
#define TQ_INIT(head) do { \
TQ_FIRST((head)) = 0; \
(head)->tq_tail = &TQ_FIRST((head)); \
} while(0)
#define TQ_FOR(var, head, field) \
for((var) = TQ_FIRST((head)); \
(var); (var) = TQ_NEXT((var), field))
/* MSVC does not have decltype(), cannot prevent side effects! */
#define TQ_ADD(head, xel, field) do { \
decltype(xel) __el = (xel); \
assert(TQ_NEXT((__el), field) == 0); \
*(head)->tq_tail = (__el); \
(head)->tq_tail = &TQ_NEXT((__el), field); \
} while(0)
#define TQ_CONCAT(head1, head2, field) do { \
if(TQ_FIRST(head2)) { \
*(head1)->tq_tail = (head2)->tq_head; \
(head1)->tq_tail = (head2)->tq_tail; \
TQ_INIT(head2); \
} \
} while(0)
/*
* Remove the first element and return it.
*/
#define TQ_REMOVE(head, field) ({ \
auto __fel = TQ_FIRST((head)); \
if(__fel == 0 \
|| (TQ_FIRST((head)) = TQ_NEXT(__fel, field)) \
== 0) { \
(head)->tq_tail = &TQ_FIRST((head)); \
} else { \
TQ_NEXT(__fel, field) = 0; \
} \
__fel; })
I've tried different casts but without success.
Thank you for your help !
EDIT :
Here is the code that I get for this call once preprocessed :
asn1p_wsyntx_chunk_t *wc;
while((wc = ({ auto __fel = (((&(wx->chunks)))->tq_head); if(__fel == 0 ||
((((&(wx->chunks)))->tq_head) = ((__fel)->next.tq_next)) == 0)
{ (&(wx->chunks))->tq_tail = &(((&(wx->chunks)))->tq_head); }
else { ((__fel)->next.tq_next) = 0; } __fel; })))
asn1p_wsyntx_chunk_free(wc);

Why native wrapped functions in Dart are such heavyweight in comparison with "DEFINE NATIVE ENTRY" functions that are very lightweight?

I cannot understand: "Why this reassurance?".
This is wrapper for custom native function from dart/runtime/vm/native_entry.cc:
It intended for the Dart programmers that want write native extensions.
void NativeEntry::NativeCallWrapper(Dart_NativeArguments args,
Dart_NativeFunction func) {
CHECK_STACK_ALIGNMENT;
VERIFY_ON_TRANSITION;
NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args);
Isolate* isolate = arguments->isolate();
ApiState* state = isolate->api_state();
ASSERT(state != NULL);
ApiLocalScope* current_top_scope = state->top_scope();
ApiLocalScope* scope = state->reusable_scope();
TRACE_NATIVE_CALL("0x%" Px "", reinterpret_cast<uintptr_t>(func));
if (scope == NULL) {
scope = new ApiLocalScope(current_top_scope,
isolate->top_exit_frame_info());
ASSERT(scope != NULL);
} else {
scope->Reinit(isolate,
current_top_scope,
isolate->top_exit_frame_info());
state->set_reusable_scope(NULL);
}
state->set_top_scope(scope); // New scope is now the top scope.
func(args);
ASSERT(current_top_scope == scope->previous());
state->set_top_scope(current_top_scope); // Reset top scope to previous.
if (state->reusable_scope() == NULL) {
scope->Reset(isolate); // Reset the old scope which we just exited.
state->set_reusable_scope(scope);
} else {
ASSERT(state->reusable_scope() != scope);
delete scope;
}
DEOPTIMIZE_ALOT;
VERIFY_ON_TRANSITION;
}
This wrapper with all unnecessary checks that it performs at every invocation of wrapped native function makes these functions uncompetitive in comparison to what uses developers for themselves.
This is MACRO for defining native function from dart/runtime/vm/native_entry.h:
#define DEFINE_NATIVE_ENTRY(name, argument_count) \
static RawObject* DN_Helper##name(Isolate* isolate, \
NativeArguments* arguments); \
void NATIVE_ENTRY_FUNCTION(name)(Dart_NativeArguments args) { \
CHECK_STACK_ALIGNMENT; \
VERIFY_ON_TRANSITION; \
NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); \
ASSERT(arguments->NativeArgCount() == argument_count); \
TRACE_NATIVE_CALL("%s", ""#name); \
{ \
StackZone zone(arguments->isolate()); \
SET_NATIVE_RETVAL(arguments, \
DN_Helper##name(arguments->isolate(), arguments)); \
DEOPTIMIZE_ALOT; \
} \
VERIFY_ON_TRANSITION; \
} \
static RawObject* DN_Helper##name(Isolate* isolate, \
NativeArguments* arguments)
I know that it works directly with RawObject. This is normal.
But I can not find in it all of these tests, which are performed in each call, as in the above wrapper.
I lose heart when I see that my functions work on the 3000% slower than the analogues defined via DEFINE_NATIVE_ENTRY.
P.S
My native function that does NOTHING and does not returns ANYTHING works on the 500% slower than (for example) this function.
#define TYPED_DATA_GETTER(getter, object, access_size) \
DEFINE_NATIVE_ENTRY(TypedData_##getter, 2) { \
GET_NON_NULL_NATIVE_ARGUMENT(Instance, instance, arguments->NativeArgAt(0)); \
GET_NON_NULL_NATIVE_ARGUMENT(Smi, offsetInBytes, arguments->NativeArgAt(1)); \
if (instance.IsTypedData()) { \
const TypedData& array = TypedData::Cast(instance); \
RangeCheck(offsetInBytes.Value(), access_size, \
array.LengthInBytes(), access_size); \
return object::New(array.getter(offsetInBytes.Value())); \
} \
if (instance.IsExternalTypedData()) { \
const ExternalTypedData& array = ExternalTypedData::Cast(instance); \
RangeCheck(offsetInBytes.Value(), access_size, \
array.LengthInBytes(), access_size); \
return object::New(array.getter(offsetInBytes.Value())); \
} \
const String& error = String::Handle(String::NewFormatted( \
"Expected a TypedData object but found %s", instance.ToCString())); \
Exceptions::ThrowArgumentError(error); \
return object::null(); \
} \
Is there any way to write lightweight native functions that not requires all of these scope?
This is an old question, but native libraries are definitely not the greatest and are pretty heavy-weight. These days we typically recommend that users look at using dart:ffi for C-interop, which is more performant than native extensions and arguably much easier to use.

CppUnit expect exception with Assert Throw compiles with warning C4127 [duplicate]

This question already has answers here:
do while(false) pattern [duplicate]
(7 answers)
Closed 9 years ago.
Currently I am writing unit tests in C++ with CppUnit. Recently I needed to check that an exception is thrown in a specific case using CppUnits macro:
CPPUNIT_ASSERT_THROW(
boost::get<FooClassInBoostVariant>(m_boostVariantFooOrBar),
boost::bad_get);
the warning during the compilation of the test surprised me (on VS2010, but will be a warning on other compilers as well...):
warning C4127: conditional expression is constant
I looked into the macro definition of CppUnit and found the following:
do { \
bool cpputExceptionThrown_ = false; \
try { \
expression; \
} catch ( const ExceptionType & ) { \
cpputExceptionThrown_ = true; \
} \
\
if ( cpputExceptionThrown_ ) \
break; \
\
CPPUNIT_NS::Asserter::fail( \
"Expected exception: " #ExceptionType \
" not thrown.", \
CPPUNIT_SOURCELINE() ); \
} while ( false )
Well, I totally understand how this works, the do while loop is executed only once, because of the false, and the break is used to not execute the Asserter::fail() part. But why are they doing it like this? It - of course - triggers the compiler warning, as the break condition for the while loop is obviously always "false". But isn't there a more elegant way to do this? I usually adhere to the no-warning-compilation principle, so this really bugs me.
So my question really is, why didn't they implement it like this:
{ \
bool cpputExceptionThrown_ = false; \
try { \
expression; \
} catch ( const ExceptionType & ) { \
cpputExceptionThrown_ = true; \
} \
\
if ( !cpputExceptionThrown_ ) { \
CPPUNIT_NS::Asserter::fail( \
"Expected exception: " #ExceptionType \
" not thrown.", \
CPPUNIT_SOURCELINE() ); \
} \
}
Thanks in advance!
-Hannes
The reason is to make the assertion one statement. Consider these two uses of the macro:
CPPUNIT_ASSERT_THROW(foo(), MyException); // a
CPPUNIT_ASSERT_THROW(foo(), MyException) // b - without trailing `;`!
doSomething();
With their code, you'd get an error with //b, since the code expands to do { ... } while (false) doSomething(); - you'd be missing the ; after the condition.
With your code, //b would happily compile, but //a could give you an "empty statement" warning, since that line would expand to { ... };, with the superfluos ; after the block.
Why they force you to use //a I don't know - but I like //b way more because it's just consistent to have a ; after each line. One does not have to distinguish lines with assertions from normal statements.
PS:
I am not sure but there might be more differences between { ... } blocks and do {...} while(false) statements that will allow to put an assertion macro in places where simple blocks are not allowed.
Edit: with C++11, you could use a lambda (define and call it in one place):
#define CPPUNIT_ASSERT_THROW(expression, ExceptionType) \
[&]() -> void { \
bool cpputExceptionThrown_ = false; \
try { \
expression; \
} catch ( const ExceptionType & ) { \
cpputExceptionThrown_ = true; \
} \
\
if ( cpputExceptionThrown_ ) \
return; \
\
CPPUNIT_NS::Asserter::fail( \
"Expected exception: " #ExceptionType \
" not thrown.", \
CPPUNIT_SOURCELINE() ); \
}()
However, there might be caveats, e.g. due to the lambda capturing the variables you use in the expression.
OK, I guess I found the answer by myself:
http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert/ gives the explanation.
It is actually a common practice to wrap multi-line macros in do { } while (false);. This is a workaround to allow for using those macros, e.g., in un-braced if else.
if (condition_a)
MULTI_LINE_MACRO();
else
MULTI_LINE_MACRO_2();
The result would be that unexpectedly only the first line gets executed, which definitely leads to unexpected behaviour. So they were not entirely incompetent I guess...
http://kernelnewbies.org/FAQ/DoWhile0 also explains why my solution would not work. The MULTI_LINE_MACRO(); within the if would expand, e.g., to
if (condition_a)
{ /* macro stuff */ }
;
else // << never executed because of the ; above.
So I guess I have to disable the warning. GCC has a workaroud for this ( ({ MACRO }) ), called a Statement Expression, but I don't think this works on VS2010.

Multi-statement Macros in C++

In C++, is it possible to make a multi-statement macro with nested if statements inside of it like the one below? I've been attempting it for a while now and I'm getting a scope issue for the second if statement not being able to see 'symbol'. Maybe I need to understand macros further.
#define MATCH_SYMBOL( symbol, token)
if(something == symbol){
if( symbol == '-'){
}else if (symbol != '-'){
}
other steps;
}
For a multi-line macro you need to add a \ character to the end of all but the last line to tell the macro processor to continue parsing the macro on the next line, like so:
#define MATCH_SYMBOL( symbol, token) \
if(something == symbol){ \
if( symbol == '-'){ \
}else if (symbol != '-'){ \
} \
other steps; \
}
Right now, it's trying to interpret it as a 1-line macro and then some actual code at the top of your file, which isn't what you want:
#define MATCH_SYMBOL( symbol, token)
// and then... wrongly thinking this is separate...
if(something == symbol){ // symbol was never defined, because the macro was never used here!
if( symbol == '-'){
}else if (symbol != '-'){
}
other steps;
}
If you're using C++ you should avoid using macros altogether. They are not type-safe, they're not namespace-aware, they're hard to debug and just they're plain messy.
If you need a type-independent function, use templates:
template <typename T>
bool match_symbol(T symbol, T token) {
if(something == symbol){
if( symbol == '-'){
}else if (symbol != '-'){
}
...
or if the parameters can be different types:
template <typename T, typename V>
bool match_symbol(T symbol, V token) {
if(something == symbol){
if( symbol == '-'){
}else if (symbol != '-'){
}
...
Note that some of the answers here have a problem.
For example, for a normal statement you can do this:
if (foo)
function();
else
otherstuff();
If you followed some of the suggestions here, but if replace function with a macro, it might expand to:
if (foo)
if (something) { /* ... */ }
else { /* ... */ }; // <-- note evil semicolon!
else
otherstuff();
So a common (ugly) hack that people do to avoid this is:
#define MATCH_SYMBOL(symbol, token) \
do \
{ \
if(something == symbol) \
{ \
if( symbol == '-') \
{ \
} \
else if (symbol != '-') \
{ \
} \
other steps; \
} \
} \
while (0) // no semicolon here
This is so that the "statement" MATCH_SYMBOL(a, b) can end with a semicolon just like a normal statement. You also have braces around the multiple statements.
If you think nobody's crazy enough to use this technique, think again. It's very common in the Linux kernel, for example.
You need to have a backslash (\) at the end of all the lines in the macro but the last one.
The way of the C++:
inline void MATCH_SYMBOL(const Symbol& symbol, const Token& token) {
/* ... */
if (something == symbol) {
if ('-' == symbol) {
/* ... */
}
else if ('-' != symbol) {
/* ... */
}
}
/* ...other steps... */
}
also, see if you can replace the macro with a function.
?
MATCH_SYMBOL(Sym const & symbol, Tok const & token)
{
...
}
One can also define macro function and implement the function than
#define MATCH_SYMBOL( symbol, token) match_symbol(symbol,token)