diff options
| author | Eric Fiselier <eric@efcs.ca> | 2019-05-16 21:04:15 +0000 |
|---|---|---|
| committer | Eric Fiselier <eric@efcs.ca> | 2019-05-16 21:04:15 +0000 |
| commit | 708afb56c125ca4f7db7070e836860076c7eafbc (patch) | |
| tree | 3dee8820f6b797ab01e7621f7eab44a615fced41 /clang/docs/LanguageExtensions.rst | |
| parent | e7b680478c8e15e92af6706da0edaeab8708e49e (diff) | |
| download | bcm5719-llvm-708afb56c125ca4f7db7070e836860076c7eafbc.tar.gz bcm5719-llvm-708afb56c125ca4f7db7070e836860076c7eafbc.zip | |
Implement __builtin_LINE() et. al. to support source location capture.
Summary:
This patch implements the source location builtins `__builtin_LINE(), `__builtin_FUNCTION()`, `__builtin_FILE()` and `__builtin_COLUMN()`. These builtins are needed to implement [`std::experimental::source_location`](https://rawgit.com/cplusplus/fundamentals-ts/v2/main.html#reflection.src_loc.creation).
With the exception of `__builtin_COLUMN`, GCC also implements these builtins, and Clangs behavior is intended to match as closely as possible.
Reviewers: rsmith, joerg, aaron.ballman, bogner, majnemer, shafik, martong
Reviewed By: rsmith
Subscribers: rnkovacs, loskutov, riccibruno, mgorny, kunitoki, alexr, majnemer, hfinkel, cfe-commits
Differential Revision: https://reviews.llvm.org/D37035
llvm-svn: 360937
Diffstat (limited to 'clang/docs/LanguageExtensions.rst')
| -rw-r--r-- | clang/docs/LanguageExtensions.rst | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst index 7772bf53e8e..9871c23d9b4 100644 --- a/clang/docs/LanguageExtensions.rst +++ b/clang/docs/LanguageExtensions.rst @@ -2300,6 +2300,61 @@ automatically will insert one if the first argument to `llvm.coro.suspend` is token `none`. If a user calls `__builin_suspend`, clang will insert `token none` as the first argument to the intrinsic. +Source location builtins +------------------------ + +Clang provides experimental builtins to support C++ standard library implementation +of ``std::experimental::source_location`` as specified in http://wg21.link/N4600. +With the exception of ``__builtin_COLUMN``, these builtins are also implemented by +GCC. + +**Syntax**: + +.. code-block:: c + + const char *__builtin_FILE(); + const char *__builtin_FUNCTION(); + unsigned __builtin_LINE(); + unsigned __builtin_COLUMN(); // Clang only + +**Example of use**: + +.. code-block:: c++ + + void my_assert(bool pred, int line = __builtin_LINE(), // Captures line of caller + const char* file = __builtin_FILE(), + const char* function = __builtin_FUNCTION()) { + if (pred) return; + printf("%s:%d assertion failed in function %s\n", file, line, function); + std::abort(); + } + + struct MyAggregateType { + int x; + int line = __builtin_LINE(); // captures line where aggregate initialization occurs + }; + static_assert(MyAggregateType{42}.line == __LINE__); + + struct MyClassType { + int line = __builtin_LINE(); // captures line of the constructor used during initialization + constexpr MyClassType(int) { assert(line == __LINE__); } + }; + +**Description**: + +The builtins ``__builtin_LINE``, ``__builtin_FUNCTION``, and ``__builtin_FILE`` return +the values, at the "invocation point", for ``__LINE__``, ``__FUNCTION__``, and +``__FILE__`` respectively. These builtins are constant expressions. + +When the builtins appear as part of a default function argument the invocation +point is the location of the caller. When the builtins appear as part of a +default member initializer, the invocation point is the location of the +constructor or aggregate initialization used to create the object. Otherwise +the invocation point is the same as the location of the builtin. + +When the invocation point of ``__builtin_FUNCTION`` is not a function scope the +empty string is returned. + Non-standard C++11 Attributes ============================= |

