Author Topic: X-Macro support - redux  (Read 1319 times)

pkusbel

  • Community Member
  • Posts: 22
  • Hero Points: 4
X-Macro support - redux
« on: August 05, 2023, 02:45:50 PM »
The last topic I saw on this was in 2012 https://community.slickedit.com/index.php?topic=7797.0 , hoping there has been improvement on ways to support this.

I have a code base with extensive use of macro's for meta-programming spread across multiple files.   Same as shown in the above reference thread from 2012:  the macro's define multiple constructs, but ultimately the big thing I need that is missing is the enum definitions.  Simple example below:
Code: [Select]
#define FOO_SIG_LIST(x)\
    x(sig_foo_a) x(sig_foo_b)

#define BAR_SIG_LIST(x)\
    x(sig_bar_a) x(sig_bar_b)

#define GLOBAL_SIG_LIST(x)\
    FOO_SIG_LIST(x) BAR_SIG_LIST(x)

#define AS_ENUM(x,...)  x,

enum {
    GLOBAL_SIG_LIST(AS_ENUM)
};

and then later on used in the code primarily with 
Code: [Select]
...
swith (sig){
   case sig_foo_a:
   case sig_bar_b:
}

As it is right now, SE can't recognize the enum expansion so I cannot use auto-completion (ctrl-space) or navigation (ctrl-.).   

Following the advice from Dennis in the referenced thread from 2012, I added the #defines exactly as above to slickedit's workspace preprocessor file <workspace>_cpp.h, and this worked. So now I have the #defines in both the code as well as the <workspace>_cpp.h, and SE expanded the macro's as expected and correctly picked up the signal enum's.

But this is extremely clunky.  Every list needs to be defined twice: one in the project file, and then again in slickedits <workspace>_cpp.h file.  Every time a signal is added removed from one of the lists in the actual code, the corresponding #define in the SE <workspace>_cpp.h must also be modified, which makes it basically unmaintainable.   

I've already use the <workspace>_cpp.h to solve all the other macro recognition issues, but this last one with variadic expansion I've not been able so solve.  SE actually already has all the information from the source code, is there any way to get SE to actually use the #define in the projects actual source code instead of having to redefine it for SE explicitly in <workspace>_cpp.h? 


pkusbel

  • Community Member
  • Posts: 22
  • Hero Points: 4
Re: X-Macro support - redux
« Reply #1 on: August 29, 2023, 01:49:43 AM »
Any answers to this question?  Or is a support ticket the preferred way for this discussion?

thanks

Graeme

  • Senior Community Member
  • Posts: 2816
  • Hero Points: 347
Re: X-Macro support - redux
« Reply #2 on: August 29, 2023, 06:16:15 AM »
You might be able to #include a file into the workspace_cpp.h file, I'm not sure.  If the actual project header file gives errors when you do that, it's probably not hard to write a macro that extracts the #defines into another file and #include that into _cpp.h instead.  You might also be able to find a way to tell slick to reprocess the _cpp.h file from a macro, in which case, when you save the actual project header file, you could automatically run a macro that did all the above - or you could just do that manually. 

If you have maintenance and support, there's no harm in asking slick support about it.

pkusbel

  • Community Member
  • Posts: 22
  • Hero Points: 4
Re: X-Macro support - redux
« Reply #3 on: September 05, 2023, 02:55:15 PM »
Quote
You might be able to #include a file into the workspace_cpp.h file, I'm not sure.  If the actual project header file gives errors when you do that, it's probably not hard to write a macro that extracts the #defines into another file and #include that into _cpp.h instead.  You might also be able to find a way to tell slick to reprocess the _cpp.h file from a macro, in which case, when you save the actual project header file, you could automatically run a macro that did all the above - or you could just do that manually.

If you have maintenance and support, there's no harm in asking slick support about it.

Tried to include a header from <workspace>_cpp.h, that doesn't work.      Opened a formal request for this with support....

Dennis

  • Senior Community Member
  • Posts: 3992
  • Hero Points: 520
Re: X-Macro support - redux
« Reply #4 on: September 06, 2023, 07:02:07 PM »
If you go to Project > Workspace Properties... > C/C++ Preprocessing, from there you can click on "Import..." to pull in a set of #defines from a selected header file.  You might find that more convenient than editing workspace_cpp.h.

Another option, which appears to work with those enumerated types at least, is to enable the following option:  Document > C/C++ Options... > C/C++ Advanced Options > Dynamically expand local defines = ON

You will want to rebuild your workspace tag file after doing this.

Note, this feature has some side-effects that are not lovely, for example:

#define MYMAGICNUM 42
extern void fffff(int arg1=MYMAGICNUM);

The signature will be tagged as if it were:

    extern void fffff(int arg1=42);

You see this when you get function argument help, the side-effect being may not remember the meaning of 42, and that could take millions of years to recalculate.
« Last Edit: September 06, 2023, 09:01:08 PM by Dennis »

pkusbel

  • Community Member
  • Posts: 22
  • Hero Points: 4
Re: X-Macro support - redux
« Reply #5 on: September 06, 2023, 11:04:35 PM »

If you go to Project > Workspace Properties... > C/C++ Preprocessing, from there you can click on "Import..." to pull in a set of #defines from a selected header file.  You might find that more convenient than editing workspace_cpp.h.

this is a great tip, been using SE for over 20 years and I've never noticed this!    The downside of using a tool over and over:  you sometimes don't notice when the tool changes..

Quote

Another option, which appears to work with those enumerated types at least, is to enable the following option:  Document > C/C++ Options... > C/C++ Advanced Options > Dynamically expand local defines = ON

You will want to rebuild your workspace tag file after doing this.

Note, this feature has some side-effects that are not lovely, for example:

#define MYMAGICNUM 42
extern void fffff(int arg1=MYMAGICNUM);

The signature will be tagged as if it were:

    extern void fffff(int arg1=42);

You see this when you get function argument help, the side-effect being may not remember the meaning of 42, and that could take millions of years to recalculate.


The current codebase is all C, and while it is macro-rich, i haven't run into any oddities yet.  This is working really, well, thank you for this!   For the expansions:  how deep will pick up nestings and recursions?   Will it also expand definitions picked up from other files?   I'm wondering now where to draw the lines between what I put in <workspace>_cpp.h and this feature.

Dennis

  • Senior Community Member
  • Posts: 3992
  • Hero Points: 520
Re: X-Macro support - redux
« Reply #6 on: September 07, 2023, 12:04:09 PM »
Glad that is working for you.  The option is called "expand local defines" because it only works within the scope of the current file being parsed.

If you have a #define in a header that is used outside of that header, then you'll need to import that one into workspace_cpp.h