1

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.

1
  • Maybe playing with _Pragma for custom define depending of the compiler Demo. Didn't succeed to pass name of the region though. Commented Jul 19, 2023 at 13:03

1 Answer 1

1

Possible solutions

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")

Further research

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.

Conclusion

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.

Sign up to request clarification or add additional context in comments.

5 Comments

That would work for sure, but it kind of defeats the purpose of the convenience of having these pragmas in the first case.
This article would suggest that the region pragma has been implemented in gcc 13, but I can't find any mention of it in the update notes. gcc.gnu.org/bugzilla/show_bug.cgi?id=85487
Yeah, I found that discussion after posting this question. According to what is said there, clang already supports this. But knowing how to do this would also be neat for other pragmas, such as when targeting embedded or some other weird platform that has its own pragmas.
You're right, I didn't know Function-like macros could be empty.
Thanks for all the research. I will probably just try to setup CMAKE so that the pragma is only ignored for compilers that do not support it yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.