diff options
author | Christophe Lyon <christophe.lyon@linaro.org> | 2021-05-20 08:10:50 +0000 |
---|---|---|
committer | Christophe Lyon <christophe.lyon@linaro.org> | 2021-05-20 08:12:41 +0000 |
commit | 1a9b3f04c11eb467a8dc504a37dad57a371a0d4c (patch) | |
tree | 29f41364435007ef3df83429bface6b67343d303 /libcpp | |
parent | 9e0a5e3ea37f9d7d2b6f2dab7c0bfbeaf08466a3 (diff) | |
download | gcc-1a9b3f04c11eb467a8dc504a37dad57a371a0d4c.zip gcc-1a9b3f04c11eb467a8dc504a37dad57a371a0d4c.tar.gz gcc-1a9b3f04c11eb467a8dc504a37dad57a371a0d4c.tar.bz2 |
c: Add support for __FILE_NAME__ macro (PR c/42579)
The toolchain provided by ST for stm32 has had support for
__FILENAME__ for a while, but clang/llvm has recently implemented
support for __FILE_NAME__, so it seems better to use the same macro
name in GCC.
It happens that the ST patch is similar to the one proposed in PR
c/42579.
Given these input files:
::::::::::::::
mydir/myinc.h
::::::::::::::
char* mystringh_file = __FILE__;
char* mystringh_filename = __FILE_NAME__;
char* mystringh_base_file = __BASE_FILE__;
::::::::::::::
mydir/mysrc.c
::::::::::::::
char* mystring_file = __FILE__;
char* mystring_filename = __FILE_NAME__;
char* mystring_base_file = __BASE_FILE__;
we produce:
$ gcc mydir/mysrc.c -I . -E
char* mystringh_file = "./mydir/myinc.h";
char* mystringh_filename = "myinc.h";
char* mystringh_base_file = "mydir/mysrc.c";
char* mystring_file = "mydir/mysrc.c";
char* mystring_filename = "mysrc.c";
char* mystring_base_file = "mydir/mysrc.c";
2021-05-20 Christophe Lyon <christophe.lyon@linaro.org>
Torbjörn Svensson <torbjorn.svensson@st.com>
PR c/42579
libcpp/
* include/cpplib.h (cpp_builtin_type): Add BT_FILE_NAME entry.
* init.c (builtin_array): Likewise.
* macro.c (_cpp_builtin_macro_text): Add support for BT_FILE_NAME.
gcc/
* doc/cpp.texi (Common Predefined Macros): Document __FILE_NAME__.
gcc/testsuite/
* c-c++-common/spellcheck-reserved.c: Add tests for __FILE_NAME__.
* c-c++-common/cpp/file-name-1.c: New test.
Diffstat (limited to 'libcpp')
-rw-r--r-- | libcpp/include/cpplib.h | 1 | ||||
-rw-r--r-- | libcpp/init.c | 1 | ||||
-rw-r--r-- | libcpp/macro.c | 14 |
3 files changed, 12 insertions, 4 deletions
diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h index 2cdaf19..7e84063 100644 --- a/libcpp/include/cpplib.h +++ b/libcpp/include/cpplib.h @@ -886,6 +886,7 @@ enum cpp_builtin_type BT_SPECLINE = 0, /* `__LINE__' */ BT_DATE, /* `__DATE__' */ BT_FILE, /* `__FILE__' */ + BT_FILE_NAME, /* `__FILE_NAME__' */ BT_BASE_FILE, /* `__BASE_FILE__' */ BT_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */ BT_TIME, /* `__TIME__' */ diff --git a/libcpp/init.c b/libcpp/init.c index d5d4e99..a450470 100644 --- a/libcpp/init.c +++ b/libcpp/init.c @@ -405,6 +405,7 @@ static const struct builtin_macro builtin_array[] = B("__TIME__", BT_TIME, false), B("__DATE__", BT_DATE, false), B("__FILE__", BT_FILE, false), + B("__FILE_NAME__", BT_FILE_NAME, false), B("__BASE_FILE__", BT_BASE_FILE, false), B("__LINE__", BT_SPECLINE, true), B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL, true), diff --git a/libcpp/macro.c b/libcpp/macro.c index dff7c98..4fc5f83 100644 --- a/libcpp/macro.c +++ b/libcpp/macro.c @@ -531,15 +531,21 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node, } break; case BT_FILE: + case BT_FILE_NAME: case BT_BASE_FILE: { unsigned int len; const char *name; uchar *buf; - - if (node->value.builtin == BT_FILE) - name = linemap_get_expansion_filename (pfile->line_table, - pfile->line_table->highest_line); + + if (node->value.builtin == BT_FILE + || node->value.builtin == BT_FILE_NAME) + { + name = linemap_get_expansion_filename (pfile->line_table, + pfile->line_table->highest_line); + if ((node->value.builtin == BT_FILE_NAME) && name) + name = lbasename (name); + } else { name = _cpp_get_file_name (pfile->main_file); |