I am not sure where I saw it first, but I got used to using #pragma region region name
and #pragma endregion
to mark sections in class definitions that belong together, like operator implementations.
In VS Code and Visual Studio, this allows you to collapse the entire section, leaving only the name visible.
However, most compilers will then raise a unknown pragma warning. I can disable that in gcc by calling -Wno-unknown-pragmas
, but had I ever needed a supported pragma and made a typo, I would not be warned.
I was wondering if it is possible to instead say "treat all pragmas XY as known but no-op", so that I can get warnings for other pragmas, but region
and endregion
are ignored.
Microsoft Visual Studio adds a _MSC_VER
macro.
You could specify only using #pragma region name
when _MSC_VER
is defined.
#ifdef _MSC_VER
#pragma region name
#endif
// Your code
#ifdef _MSC_VER
#pragma endregion
#endif
You could also use that property to create a macro that would only be defined when using the Microsoft compiler (or any other compiler if you know the macro that it sets).
However, it seems that Visual Studio doesn't recognize _Pragma("region name")
as the same thing as #pragma region name
.
#ifdef _MSC_VER
# define REGION(name) _Pragma(#name)
# define REGION_END() _Pragma("endregion")
#else
# define REGION(name)
# define REGION_END()
#endif
REGION(region name) // expands to _Pragma("region name")
// Your code
REGION_END() // expands to _Pragma("endregion")
After some thinking, I decided to check if the region pragma actually has any effect on the generated assembly instructions. I tried a couple of simple examples and it seems like it doesn't change anything. As an interesting fact, I found out that clang and GCC 13 implement their support by simply ignoring this specific pragma.
That means that the behavior is achieved by the IDE by simply analyzing the contents of the file and looking for the region pragma.
This explains why the second way I tried didn't work. It set the pragma correctly, but the IDE simply wasn't looking for macros that expand into _Pragma("region name")
but for #pragma region name
instead. As far as I know, it's not possible to create a Macro that expands into #pragma region name
.
Using a pragma is a very odd way of implementing a purely visual IDE functionality. I can't find any good reason for it to be that way. However as an interesting side note, Xcode has a #pragma mark name
which lets you mark specific lines in your Objective-C code, so I bet there is a good reason behind the pragma madness.
Luckily there are some Visual Studio plugins that achieve the same result using comments.
One that I found is #region folding for VS Code.
This specific use case can be accomplished by using a plugin. The question however was about pragmas in general and not about this specific one.
To answer that question you can use the first way if it's a one-off situation or use the second way if you know you're going to use a certain pragma often.
I can't think of another way of doing this without checking the compiler-specific macros as pragmas are by definition dependant on the compiler used, so I hope this helps.