As an experiment, I took a shot at implementing source_location for
C++20.?? This was mostly done in experimental but I wanted to try adding
column information.?? (The experimental version just returned 0).?? I
added __builtin_COLUMN in analogy to __builtin_LINE.?? The std version is
also consteval so you get different results in some cases wrt
experimental. You can diff the two 1.cc test cases in libstdc++ to see
for yourself.
As Jonathan mentioned on IRC, we probably want a single builtin and we
want to coordinate the name with clang (__builtin_source_location?).??
But this "works" and it might make useful fodder for the next round.
Ed
gcc/ChangeLog
2019-11-08 Ed Smith-Rowland <3dw...@verizon.net>
Implement C++20 P1208R6 - source_location. Implement column with a
__builtin_COLUMN for both std and experimental. The std current()
is consteval.
* builtins.c (fold_builtin_COLUMN): New function.
(fold_builtin_0): Use it.
* builtins.def: Add __builtin_COLUMN.
* doc/extend.texi: Doc __builtin_COLUMN.
* testsuite/c-c++-common/builtin_location.c: __builtin_COLUMN() tests.
* testsuite/c-c++-common/cpp/has-builtin-2.c: __builtin_COLUMN test.
libstdc++-v3/ChangeLog
2019-11-08 Ed Smith-Rowland <3dw...@verizon.net>
Implement C++20 P1208R6 - source_location. Implement column with a
__builtin_COLUMN for both std and experimental. The std current()
is consteval.
* include/experimental/source_location: Call __builtin_COLUMN
* include/std/source_location: New header.
* include/std/version: Add
* testsuite/20_util/source_location/1.cc: New test.
* libstdc++-v3/testsuite/experimental/source_location/1.cc: Test column.
Index: gcc/builtins.c
===
--- gcc/builtins.c (revision 277745)
+++ gcc/builtins.c (working copy)
@@ -9500,6 +9500,14 @@
return build_int_cst (type, LOCATION_LINE (loc));
}
+/* Fold a call to __builtin_COLUMN to an integer constant. */
+
+static inline tree
+fold_builtin_COLUMN (location_t loc, tree type)
+{
+ return build_int_cst (type, LOCATION_COLUMN (loc));
+}
+
/* Fold a call to built-in function FNDECL with 0 arguments.
This function returns NULL_TREE if no simplification was possible. */
@@ -9519,6 +9527,9 @@
case BUILT_IN_LINE:
return fold_builtin_LINE (loc, type);
+case BUILT_IN_COLUMN:
+ return fold_builtin_COLUMN (loc, type);
+
CASE_FLT_FN (BUILT_IN_INF):
CASE_FLT_FN_FLOATN_NX (BUILT_IN_INF):
case BUILT_IN_INFD32:
Index: gcc/builtins.def
===
--- gcc/builtins.def (revision 277745)
+++ gcc/builtins.def (working copy)
@@ -1048,6 +1048,7 @@
DEF_GCC_BUILTIN (BUILT_IN_FILE, "FILE", BT_FN_CONST_STRING, ATTR_NOTHROW_LEAF_LIST)
DEF_GCC_BUILTIN (BUILT_IN_FUNCTION, "FUNCTION", BT_FN_CONST_STRING, ATTR_NOTHROW_LEAF_LIST)
DEF_GCC_BUILTIN (BUILT_IN_LINE, "LINE", BT_FN_INT, ATTR_NOTHROW_LEAF_LIST)
+DEF_GCC_BUILTIN (BUILT_IN_COLUMN, "COLUMN", BT_FN_INT, ATTR_NOTHROW_LEAF_LIST)
/* Synchronization Primitives. */
#include "sync-builtins.def"
Index: gcc/doc/extend.texi
===
--- gcc/doc/extend.texi (revision 277745)
+++ gcc/doc/extend.texi (working copy)
@@ -13154,6 +13154,13 @@
of the call to @var{F}.
@end deftypefn
+@deftypefn {Built-in Function} int __builtin_COLUMN ()
+This function returns a constant integer expression that evaluates to
+the column number of the invocation of the built-in. When used as a C++
+default argument for a function @var{F}, it returns the line number
+of the call to @var{F}.
+@end deftypefn
+
@deftypefn {Built-in Function} {const char *} __builtin_FUNCTION ()
This function is the equivalent of the @code{__FUNCTION__} symbol
and returns an address constant pointing to the name of the function
Index: libstdc++-v3/include/experimental/source_location
===
--- libstdc++-v3/include/experimental/source_location (revision 277745)
+++ libstdc++-v3/include/experimental/source_location (working copy)
@@ -52,7 +52,7 @@
current(const char* __file = __builtin_FILE(),
const char* __func = __builtin_FUNCTION(),
int __line = __builtin_LINE(),
- int __col = 0) noexcept
+ int __col = __builtin_COLUMN()) noexcept
{
source_location __loc;
__loc._M_file = __file;
Index: libstdc++-v3/include/std/source_location
===
--- libstdc++-v3/include/std/source_location (nonexistent)
+++ libstdc++-v3/include/std/source_location (working copy)
@@ -0,0 +1,95 @@
+// -*- C++ -*-
+
+// Copyright (C) 2019 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistrib