From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1652) id DB20B3858038; Thu, 20 May 2021 08:12:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DB20B3858038 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="utf-8" From: Christophe Lyon To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-938] c: Add support for __FILE_NAME__ macro (PR c/42579) X-Act-Checkin: gcc X-Git-Author: Christophe Lyon X-Git-Refname: refs/heads/master X-Git-Oldrev: 9e0a5e3ea37f9d7d2b6f2dab7c0bfbeaf08466a3 X-Git-Newrev: 1a9b3f04c11eb467a8dc504a37dad57a371a0d4c Message-Id: <20210520081258.DB20B3858038@sourceware.org> Date: Thu, 20 May 2021 08:12:58 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 20 May 2021 08:12:59 -0000 https://gcc.gnu.org/g:1a9b3f04c11eb467a8dc504a37dad57a371a0d4c commit r12-938-g1a9b3f04c11eb467a8dc504a37dad57a371a0d4c Author: Christophe Lyon Date: Thu May 20 08:10:50 2021 +0000 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 Torbjörn Svensson 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. Diff: --- gcc/doc/cpp.texi | 7 +++++++ gcc/testsuite/c-c++-common/cpp/file-name-1.c | 22 ++++++++++++++++++++++ gcc/testsuite/c-c++-common/spellcheck-reserved.c | 18 ++++++++++++++++++ libcpp/include/cpplib.h | 1 + libcpp/init.c | 1 + libcpp/macro.c | 14 ++++++++++---- 6 files changed, 59 insertions(+), 4 deletions(-) diff --git a/gcc/doc/cpp.texi b/gcc/doc/cpp.texi index 4b065b651b0..d4b3ff0a8b0 100644 --- a/gcc/doc/cpp.texi +++ b/gcc/doc/cpp.texi @@ -2005,6 +2005,13 @@ This macro expands to the name of the main input file, in the form of a C string constant. This is the source file that was specified on the command line of the preprocessor or C compiler. +@item __FILE_NAME__ +This macro expands to the basename of the current input file, in the +form of a C string constant. This is the last path component by which +the preprocessor opened the file. For example, processing +@code{"/usr/local/include/myheader.h"} would set this +macro to @code{"myheader.h"}. + @item __INCLUDE_LEVEL__ This macro expands to a decimal integer constant that represents the depth of nesting in include files. The value of this macro is diff --git a/gcc/testsuite/c-c++-common/cpp/file-name-1.c b/gcc/testsuite/c-c++-common/cpp/file-name-1.c new file mode 100644 index 00000000000..2b476e34cce --- /dev/null +++ b/gcc/testsuite/c-c++-common/cpp/file-name-1.c @@ -0,0 +1,22 @@ +/* { dg-do preprocess } */ +/* { dg-additional-options -Wno-pedantic } */ + +main-1 __FILE_NAME__ + +# 7 "inner.h" 1 +inner-1 __FILE_NAME__ +# 9 "subdir/inside.h" 1 +inside-1 __FILE_NAME__ +inside-2 __FILE__ +# 11 "" 2 +inner-2 __FILE_NAME__ +#13 "" 2 +main-2 __FILE_NAME__ + + +/* { dg-final { scan-file file-name-1.i "main-1 \"\[^\n]*file-name-1.c\"\n" } } */ +/* { dg-final { scan-file file-name-1.i "main-2 \"\[^\n]*file-name-1.c\"\n" } } */ +/* { dg-final { scan-file file-name-1.i "inner-1 \"inner.h\"\n" } } */ +/* { dg-final { scan-file file-name-1.i "inner-2 \"inner.h\"\n" } } */ +/* { dg-final { scan-file file-name-1.i "inside-1 \"inside.h\"\n" } } */ +/* { dg-final { scan-file file-name-1.i "inside-2 \"subdir/inside.h\"\n" } } */ diff --git a/gcc/testsuite/c-c++-common/spellcheck-reserved.c b/gcc/testsuite/c-c++-common/spellcheck-reserved.c index ed292f2bae0..56e59dcc00a 100644 --- a/gcc/testsuite/c-c++-common/spellcheck-reserved.c +++ b/gcc/testsuite/c-c++-common/spellcheck-reserved.c @@ -50,3 +50,21 @@ const char * test_3 (void) /* { dg-error "did you mean '__FILE__'" "" { target c } misspelled__FILE_ } */ /* { dg-error "'__FILE_' was not declared in this scope; did you mean '__FILE__'\\?" "" { target c++ } misspelled__FILE_ } */ } + +/* Verify that we can correct "__FILE_NAME_" to "__FILE_NAME__". */ + +const char * test_4 (void) +{ + return __FILE_NAME_; /* { dg-line misspelled__FILE_NAME_ } */ + /* { dg-error "did you mean '__FILE_NAME__'" "" { target c } misspelled__FILE_NAME_ } */ + /* { dg-error "'__FILE_NAME_' was not declared in this scope; did you mean '__FILE_NAME__'\\?" "" { target c++ } misspelled__FILE_NAME_ } */ +} + +/* Verify that we can correct "__FILENAME__" to "__FILE_NAME__". */ + +const char * test_5 (void) +{ + return __FILENAME__; /* { dg-line misspelled__FILENAME__ } */ + /* { dg-error "did you mean '__FILE_NAME__'" "" { target c } misspelled__FILENAME__ } */ + /* { dg-error "'__FILENAME__' was not declared in this scope; did you mean '__FILE_NAME__'\\?" "" { target c++ } misspelled__FILENAME__ } */ +} diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h index 2cdaf19362a..7e840635a38 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 d5d4e994d2a..a4504700076 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 dff7c98a4df..4fc5f838919 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);