Whole Tomato Software Forums
Whole Tomato Software Forums
Main Site | Profile | Register | Active Topics | Members | Search | FAQ
User name:
Password:
Save Password
Forgot your password?

 All Forums
 Visual Assist
 Feature Requests
 Extended enumeration highlighting
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Millianz
Junior Member

10 Posts

Posted - Oct 27 2011 :  3:31:40 PM  Show Profile  Reply with Quote
Hello,

I ran into a slight problem enums today. I'm not sure if Visual Assist can do anything about it, but if it is possible it would be great to fix this in the future:

Assume there is a header file with the following contents

REGISTER_MESSAGE(CORE_STARTUP)
REGISTER_MESSAGE(CORE_SHUTDOWN)


and is called "messagenames.h"

In another header, messages.h the following setup can be found:

#pragma once

#define REGISTER_MESSAGE(x) x,
typedef enum
{
  #include "messagenames.h"
  NUM_MESSAGES
} Messages;
#undef REGISTER_MESSAGE

#define REGISTER_MESSAGE(x) #x,
static char const gMessageNames[] = 
{
  #include "messagenames.h"
  "Invalid"
};
#undef REGISTER_MESSAGE



I now have an enum that is kept in sync automatically with the associated strings of the same name. But both, Visual Studio and Visual Assist fail to do proper syntax highglighting when I use the enum entries for more than 3 enumerations.

feline
Whole Tomato Software

United Kingdom
19024 Posts

Posted - Oct 27 2011 :  5:37:26 PM  Show Profile  Reply with Quote
I am totally confused, and have no idea what this is supposed to be doing.

If I have followed correctly, the header file "messagenames.h" contains the uses of the macros, but the macros themselves include the header file that contains the uses of the macros, which sounds like an infinite look in the making to me, which presumably the #pragma once saves you from.

Can you explain the problem you are having with syntax highlighting?

You might want to try telling VA to parse macros more deeply, as explained here:

http://docs.wholetomato.com?W363

zen is the art of being at one with the two'ness
Go to Top of Page

Millianz
Junior Member

10 Posts

Posted - Oct 28 2011 :  01:19:27 AM  Show Profile  Reply with Quote
Hi,

I tried what was suggested under the link you posted but that didn't seem to resolve the issue.
To clarify the above example:

messagenames.h only contains lines containing REGISTER_MESSAGE(messagename), no other code or include guards.

When the preprocessor parses "messages.h" it copy pastes the contents of messagenames.h into where it was included.

Since the macro REGISTER_MESSAGES is defined right above the use, it is defined once it's copy pasted into the enum / array initializer list. For the enum, the macro is defined to simply copy paste the contents of the macro into the enum followed by a comman. For the array, the macro stringizes the arguments of the macro and inserts those strings comma separated.

So after the preprocessor expands the example above the file "messages.h" would look something like this:


#define REGISTER_MESSAGE(x) x,
typedef enum
{
  CORE_STARTUP,
  CORE_SHUTDOWN,
  NUM_MESSAGES
} Messages;
#undef REGISTER_MESSAGE

#define REGISTER_MESSAGE(x) #x,
static char const gMessageNames[] = 
{
  "CORE_STARTUP",
  "CORE_SHUTDOWN",
  "Invalid"
};
#undef REGISTER_MESSAGE


The purpose of this trick: stringizing enums. Basically now it's possible to use the enum as both an int (for quick identification of a symbol, say index into a std::map using the enum value), while maintaining an array of corresponding strings.

gMessageNames[CORE_STARTUP] equals "CORE_STARTUP"

Go to Top of Page

mwb1100
Ketchup Master

82 Posts

Posted - Oct 28 2011 :  1:11:02 PM  Show Profile  Reply with Quote
quote:
Originally posted by feline

I am totally confused, and have no idea what this is supposed to be doing.


Just as an FYI: this is a not-too-common technique that is sometimes called "X-Macros": http://en.wikibooks.org/wiki/C_Programming/Preprocessor#X-Macros

Go to Top of Page

feline
Whole Tomato Software

United Kingdom
19024 Posts

Posted - Oct 28 2011 :  6:26:54 PM  Show Profile  Reply with Quote
This makes a bit more sense. You can do some very clever, but also rather opaque things with the pre-processor.

If your main problem is the enum items CORE_STARTUP and CORE_SHUTDOWN not being recognised or coloured correctly, you could simply add:

#ifdef VA_HELPER_CODE
#define CORE_STARTUP
#define CORE_SHUTDOWN
#endif

or

#ifdef VA_HELPER_CODE
enum VA_HELPER_REGISTER_MESSAGE { CORE_STARTUP, CORE_SHUTDOWN };
#endif

I know this is a work around, but it is a quick and simple one.

zen is the art of being at one with the two'ness
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
© 2023 Whole Tomato Software, LLC Go To Top Of Page
Snitz Forums 2000