Author Topic: using macros wxCHECK_MSG(..,..,..)  (Read 2374 times)

Offline omlk

  • Multiple posting newcomer
  • *
  • Posts: 110
using macros wxCHECK_MSG(..,..,..)
« on: November 12, 2023, 10:17:45 pm »
I was looking through the code and came across these edits, and I want to know if everything was done correctly with regards to the macro?
modified: 2022-05-21    ollydbg
Code
    
m_RE_Unix.Compile(_T("([^$]|^)(\\$[({]?(#?[A-Za-z_0-9.]+)[\\)} /\\\\]?)"), wxRE_EXTENDED | wxRE_NEWLINE);     wxCHECK_MSG(m_RE_Unix.IsValid(), false, "Invalid regex (m_RE_Unix) in macros manager");
https://sourceforge.net/p/codeblocks/code/12880/tree/trunk/src/sdk/macrosmanager.cpp
macrosmanager.cpp: row 93

My test example:
Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define wxCHECK2_MSG(cond, op, msg)                                       \
    if ( cond )                                                           \
    {                                                                     \
        printf("cond:%d",(int)cond);                                      \
    }                                                                     \
    else                                                                  \
    {                                                                     \
        printf("op:%s \"%s\" %s:%d",#op,msg,__FILE__,__LINE__);           \
        op;                                                               \
    }
    //struct wxMAKE_UNIQUE_NAME(wxDummyCheckStruct) /* to force a semicolon */

// check which returns with the specified return code if the condition fails
#define wxCHECK_MSG(cond, rc, msg)   wxCHECK2_MSG(cond, return rc, msg)

int test() {
    return -55550; // test 1
//    return 0; // test 2
}

int main(int argc, char** argv) {

    // MACROS, not need ";" in end line
    wxCHECK_MSG(test(), 1212121, "Error msg string.")

    printf("\n%s:%d",__FILE__,__LINE__);
    return 0;

}
« Last Edit: November 13, 2023, 12:44:06 am by omlk »

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5916
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: using macros wxCHECK_MSG(..,..,..)
« Reply #1 on: November 13, 2023, 02:55:11 am »
modified: 2022-05-21 ollydbg

Is that code changed by me?

Code::Blocks / SVN Repo / Commit [r12880]

r12880 does not contains your mentioned changes.

Any way, I don't know what's issue you see.
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline omlk

  • Multiple posting newcomer
  • *
  • Posts: 110
Re: using macros wxCHECK_MSG(..,..,..)
« Reply #2 on: November 13, 2023, 04:19:40 am »
modified: 2022-05-21 ollydbg
I saw this information here
https://sourceforge.net/p/codeblocks/code/12880/tree/trunk/src/sdk/
 and did not mean that you added the use of a macro. I wanted to show that in this revision [r12880] there is a use of maros.
If specifically, then it seems here [r12513] (31.9 kB) by  fuscated 2021-08-15 11:51:16

The point is, does the author of the changes really know how this macro works and the behavior will be correct in the event of a compilation error of the expression?
« Last Edit: November 13, 2023, 04:28:47 am by omlk »

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1563
Re: using macros wxCHECK_MSG(..,..,..)
« Reply #3 on: November 13, 2023, 09:06:22 am »
Quote
Does the author of the changes really know how this macro works and the behavior will be correct in the event of a compilation error of the expression

You can bet he knows. MacrosManager::CompileRegexes() will return false if any of the IsValid() calls return false.

Offline omlk

  • Multiple posting newcomer
  • *
  • Posts: 110
Re: using macros wxCHECK_MSG(..,..,..)
« Reply #4 on: November 13, 2023, 01:26:16 pm »
You can bet he knows. MacrosManager::CompileRegexes() will return false if any of the IsValid() calls return false.
I'm not talking about the  MacrosManager::CompileRegexes(), but about the
Code
wxCHECK_MSG(m_RE_Unix.IsValid(), false, "Invalid regex (m_RE_Unix) in macros manager");
wxCHECK_MSG(m_RE_DOS.IsValid(), false, "Invalid regex (m_RE_DOS) in macros manager");
wxCHECK_MSG(m_RE_IfSp.IsValid(), false, "Invalid regex (m_RE_IfSp) in macros manager");
processing and output of information where there is an incorrect regular expression. If there is an error in the 1st reg.expression, will the following reg.expressions, which are in the code below, be checked?
What does the second argument as "FALSE" mean?
Code
wxCHECK_MSG(.., false,..);
« Last Edit: November 13, 2023, 01:28:43 pm by omlk »

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1563
Re: using macros wxCHECK_MSG(..,..,..)
« Reply #5 on: November 13, 2023, 01:37:33 pm »
The first error makes the method return false, read the documentation of wxCHECK_MSG.

Offline omlk

  • Multiple posting newcomer
  • *
  • Posts: 110
Re: using macros wxCHECK_MSG(..,..,..)
« Reply #6 on: November 13, 2023, 01:53:57 pm »
The first error makes the method return false, read the documentation of wxCHECK_MSG.

Code
bool MacrosManager::CompileRegexes() {
  m_RE_Unix.Compile(_T("([^$]|^)(\\$[({]?(#?[A-Za-z_0-9.]+)[\\)} /\\\\]?)"),wxRE_EXTENDED | wxRE_NEWLINE);            wxCHECK_MSG(m_RE_Unix.IsValid(), false, "Invalid regex (m_RE_Unix) in macros manager");
  m_RE_DOS.Compile(_T("([^%]|^)(%(#?[A-Za-z_0-9.]+)%)"), wxRE_EXTENDED | wxRE_NEWLINE);         wxCHECK_MSG(m_RE_DOS.IsValid(), false, "Invalid regex (m_RE_DOS) in macros manager");
  m_RE_IfSp.Compile(_T("(([^=!<>]+)[ ]*(=|==|!=|>|<|>=|<=)[ ]*([^=!<>]+))"),wxRE_EXTENDED | wxRE_NEWLINE);
  wxCHECK_MSG(m_RE_IfSp.IsValid(), false, "Invalid regex (m_RE_IfSp) in macros manager");
  m_RE_Script.Compile(_T("(\\[\\[(.*)\\]\\])"), wxRE_EXTENDED | wxRE_NEWLINE);  wxCHECK_MSG(m_RE_Script.IsValid(), false, "Invalid regex (m_RE_Script) in macros manager");
#ifndef __WXMAC__  const int flagsForMac = wxRE_ADVANCED; #else     const int flagsForMac = wxRE_EXTENDED;#endif
  m_RE_ToAbsolutePath.Compile(_T("\\$TO_ABSOLUTE_PATH{([^}]*)}"), flagsForMac);           wxCHECK_MSG(m_RE_ToAbsolutePath.IsValid(), false,"Invalid regex (m_RE_ToAbsolutePath) in macros manager");     
  m_RE_To83Path.Compile(_T("\\$TO_83_PATH{([^}]*)}"), flagsForMac);
  wxCHECK_MSG(m_RE_To83Path.IsValid(), false, "Invalid regex (m_RE_To83Path) in macros manager");     
  m_RE_RemoveQuotes.Compile(_T("\\$REMOVE_QUOTES{([^}]*)}"), flagsForMac);      wxCHECK_MSG(m_RE_RemoveQuotes.IsValid(), false,"Invalid regex (m_RE_RemoveQuotes) in macros manager");

  return true;
}
 this execute?
Code
wxCHECK_MSG(m_RE_RemoveQuotes.IsValid(), false,"Invalid regex (m_RE_RemoveQuotes) in macros manager");
if in first
Code
    wxCHECK_MSG(m_RE_Unix.IsValid(), false, "Invalid regex (m_RE_Unix) in macros manager");
retur false   

You can specify which method will return false?
« Last Edit: November 13, 2023, 01:57:49 pm by omlk »

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1563
Re: using macros wxCHECK_MSG(..,..,..)
« Reply #7 on: November 13, 2023, 01:57:44 pm »
Quote
You can specify which method will return false?
MacrosManager::CompileRegexes()

Offline omlk

  • Multiple posting newcomer
  • *
  • Posts: 110
Re: using macros wxCHECK_MSG(..,..,..)
« Reply #8 on: November 13, 2023, 01:59:14 pm »
Quote
You can specify which method will return false?
MacrosManager::CompileRegexes()
Do other regular expressions not need to be checked?

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5493
Re: using macros wxCHECK_MSG(..,..,..)
« Reply #9 on: November 13, 2023, 02:22:01 pm »
what is the goal of your question ?

the macro is documented on what the second parameter does.

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1563
Re: using macros wxCHECK_MSG(..,..,..)
« Reply #10 on: November 13, 2023, 02:22:53 pm »
They will be checked after the failing one has been fixed.

Offline omlk

  • Multiple posting newcomer
  • *
  • Posts: 110
Re: using macros wxCHECK_MSG(..,..,..)
« Reply #11 on: November 13, 2023, 02:26:04 pm »
They will be checked after the failing one has been fixed.
OK, is that what you wanted? replacing assert?  :)

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1563
Re: using macros wxCHECK_MSG(..,..,..)
« Reply #12 on: November 13, 2023, 02:32:59 pm »
assert generates an exception only in debug mode, wx debugging macros are a lot more informative in release and debug mode.

End trans.

Offline omlk

  • Multiple posting newcomer
  • *
  • Posts: 110
Re: using macros wxCHECK_MSG(..,..,..)
« Reply #13 on: November 13, 2023, 02:37:19 pm »
assert generates an exception only in debug mode, wx debugging macros are a lot more informative in release and debug mode.

End trans.
That is, there is no option in the widgets to see all the problems at once? just correct one at a time and watch the next one after fix?

Offline blauzahn

  • Almost regular
  • **
  • Posts: 161
Re: using macros wxCHECK_MSG(..,..,..)
« Reply #14 on: November 19, 2023, 10:19:58 am »
Yes. That is exactly what they are intended for. Fail fast on an eventually corrupted state.

according to https://en.cppreference.com/w/cpp/error/assert:
Quote
...outputs implementation-specific diagnostic information on the standard error output and calls std::abort.

At least as important: An assert serves as executable note to the next reader.

If on the other hand an assert actually threw an exception it would indeed be possible to proceed and see subsequent behaviour. If you really want to, you could redefine the macro. Good luck with that. Instead of that I would rather use unittests.