Вопрос

I am familiar with #pragma mark objective-c / xcode / ios development and that it is useful for finding sections of code.

However, I am wondering if there are other keywords other than 'mark'. Like, can you do #pragma somethingelse? Thanks in advance!

Это было полезно?

Решение

First, some examples:

  1. You can control diagnostics:

    http://clang.llvm.org/docs/UsersManual.html#controlling-diagnostics-via-pragmas

  2. And from the same link:

    • clang supports the Microsoft "#pragma pack" feature for controlling record layout. GCC also contains support for this feature, however where MSVC and GCC are incompatible clang follows the MSVC definition.

    • clang supports the Microsoft #pragma comment(lib, "foo.lib") feature for automatically linking against the specified library. Currently this feature only works with the Visual C++ linker.

    • clang supports the Microsoft #pragma comment(linker, "/flag:foo") feature for adding linker flags to COFF object files. The user is responsible for ensuring that the linker understands the flags.

    The second and third from that list won't apply to your iOS code, though.

  3. Wikipedia [link] says that clang supports #pragma once, too.

And finally, here's a link to the clang API documentation for the pragma handling code. You can browse from there to see everything else. In particular, TokenKinds.def describes all the accepted tokens, so presumably it's complete:

#pragma unused
#pragma GCC visibility [push/pop]
#pragma pack [value/show/push/pop/etc/etc/etc]
#pragma clang __debug parser_crash
#pragma clang __debug captured
#pragma ms_struct [on/off]
#pragma align [native/natural/mac68k/power/reset]
#pragma weak [identifier]
#pragma weak [identifier = identifier] // alias
#pragma redefine_extname [identifier identifier]
#pragma STDC FP_CONTRACT
#pragma OPENCL EXTENSION
#pragma omp [...]
#pragma detect_mismatch
#pragma comment

The parsing code, found in ParsePragma.cpp, seems to indicate that not all of them are implemented, even if the front-end accepts them, though.

Другие советы

Yes. It's often used for implementation defined directives, but there are a few which are defined in C.

6.10.6 Pragma directive Semantics 1

  1. A preprocessing directive of the form # pragma pp-tokensopt new-line where the preprocessing token STDC does not immediately follow pragma in the directive (prior to any macro replacement) causes the implementation to behave in an implementation-defined manner. The behavior might cause translation to fail or cause the translator or the resulting program to behave in a non-conforming manner. Any such pragma that is not recognized by the implementation is ignored.

  2. If the preprocessing token STDC does immediately follow pragma in the directive (prior to any macro replacement), then no macro replacement is performed on the directive, and the directive shall have one of the following forms whose meanings are described elsewhere:

    #pragma STDC FP_CONTRACT on-off-switch

    #pragma STDC FENV_ACCESS on-off-switch

    #pragma STDC CX_LIMITED_RANGE on-off-switch

    `on-off-switch`: one of `ON OFF DEFAULT`
    

Carl Norum's answer covered examples of implementation defined directives well. For a complete list, you should refer to your compiler's documentation.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top