-
Richard Smith authored
Some grammar fixes to 'Format String Checking', and reorder the text slightly to try to make the final code block actually get rendered. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@175112 91177308-0d34-0410-b5e6-96231b3b80d8
Richard Smith authoredSome grammar fixes to 'Format String Checking', and reorder the text slightly to try to make the final code block actually get rendered. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@175112 91177308-0d34-0410-b5e6-96231b3b80d8
Clang Language Extensions
- Introduction
- Feature Checking Macros
- Include File Checking Macros
- Builtin Macros
- Vectors and Extended Vectors
- Messages on
deprecated
andunavailable
Attributes - Attributes on Enumerators
- 'User-Specified' System Frameworks
- Availability attribute
- Checks for Standard Language Features
- Checks for Type Traits
- Blocks
- Objective-C Features
- Function Overloading in C
- Initializer lists for complex numbers in C
- Builtin Functions
- Non-standard C++11 Attributes
- Target-Specific Extensions
- Extensions for Static Analysis
- Extensions for Dynamic Analysis
- Thread-Safety Annotation Checking
- Type Safety Checking
- Format String Checking
Introduction
This document describes the language extensions provided by Clang. In addition to the language extensions listed here, Clang aims to support a broad range of GCC extensions. Please see the GCC manual for more information on these extensions.
Feature Checking Macros
Language extensions can be very useful, but only if you know you can depend on them. In order to allow fine-grain features checks, we support three builtin function-like macros. This allows you to directly test for a feature in your code without having to resort to something like autoconf or fragile "compiler version checks".
__has_builtin
This function-like macro takes a single identifier argument that is the name of a builtin function. It evaluates to 1 if the builtin is supported or 0 if not. It can be used like this:
#ifndef __has_builtin // Optional of course.
#define __has_builtin(x) 0 // Compatibility with non-clang compilers.
#endif
...
#if __has_builtin(__builtin_trap)
__builtin_trap();
#else
abort();
#endif
...
__has_feature
and __has_extension
These function-like macros take a single identifier argument that is the name
of a feature. __has_feature
evaluates to 1 if the feature is both
supported by Clang and standardized in the current language standard or 0 if
not (but see :ref:`below <langext-has-feature-back-compat>`), while
__has_extension
evaluates to 1 if the feature is supported by Clang in the
current language (either as a language extension or a standard language
feature) or 0 if not. They can be used like this:
#ifndef __has_feature // Optional of course.
#define __has_feature(x) 0 // Compatibility with non-clang compilers.
#endif
#ifndef __has_extension
#define __has_extension __has_feature // Compatibility with pre-3.0 compilers.
#endif
...
#if __has_feature(cxx_rvalue_references)
// This code will only be compiled with the -std=c++11 and -std=gnu++11
// options, because rvalue references are only standardized in C++11.
#endif
#if __has_extension(cxx_rvalue_references)
// This code will be compiled with the -std=c++11, -std=gnu++11, -std=c++98
// and -std=gnu++98 options, because rvalue references are supported as a
// language extension in C++98.
#endif
For backwards compatibility reasons, __has_feature
can also be used to test
for support for non-standardized features, i.e. features not prefixed c_
,
cxx_
or objc_
.
Another use of __has_feature
is to check for compiler features not related
to the language standard, such as e.g. :doc:`AddressSanitizer
<AddressSanitizer>`.
If the -pedantic-errors
option is given, __has_extension
is equivalent
to __has_feature
.
The feature tag is described along with the language feature below.