Bug 42579

Summary: [PATCH] support for obtaining file basename
Product: gcc Reporter: Robert Millan <rmh.gcc>
Component: cAssignee: Christophe Lyon <clyon>
Status: RESOLVED FIXED    
Severity: enhancement CC: asierllano, clyon, egallager, fuchedzhy, gcc-bugs, rdiezmail-gcc
Priority: P3 Keywords: patch
Version: 4.5.0   
Target Milestone: 12.0   
See Also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82176
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:
Attachments: __FILE_BASENAME__

Description Robert Millan 2010-01-02 10:09:33 UTC
Please consider this patch.  It adds support for obtaining file basename via __FILE_BASENAME__.

This would be used by GNU GRUB.  I'll provide some explanation on why this is very necessary for us:

  - Bootloader code is very critical; a bug can render system unbootable.  Additionally, it needs to support a wide range of hardware and firmware.  Because of this, we put extensive debugging support into GRUB.  Debug statements currently rely on __FILE__.

  - We want to support $srcdir != $objdir build setups, and continue supporting them.

  - Some parts of bootstrap code tend to be very size constrained.  Every byte counts, and each debug statement is taking unnecessary space because accessing a file in $srcdir requires either an absolute path or at least a number of '../' components (in Debian, that would be two of them).

Because of this, I would find it very useful if GCC provided __FILE_BASENAME__, or at least an equivalent facility.
Comment 1 Robert Millan 2010-01-02 10:12:20 UTC
Created attachment 19445 [details]
__FILE_BASENAME__

Attaching the patch.  Also sent to gcc-patches.
Comment 2 Richard Biener 2010-01-02 11:06:18 UTC
__BASE_FILE__ could be enough for what you want.
Comment 3 Robert Millan 2010-01-02 11:39:40 UTC
(In reply to comment #2)
> __BASE_FILE__ could be enough for what you want.

Hi,

I tried already, but __BASE_FILE__ seems to be something unrelated.  It's
like __FILE__ but when used in an #include'd source it picks the includer
rather than the included file.
Comment 4 Christopher Friedt 2010-04-30 09:54:37 UTC
(In reply to comment #0)
> Please consider this patch.  It adds support for obtaining file basename via
> __FILE_BASENAME__.

Please do! 

What would also be really nice would be to have similar functionality for C++, with __CLASS__ and __METHOD__ macros.
Comment 5 Asier Llano 2012-08-31 08:00:09 UTC
We have a similar issue for embedded devices with debugging. We even implemented it with the same exact name __FILE_BASENAME__ and when I was going to submit a it in bugzilla we realized that it was already reported.
Comment 6 Asier Llano 2012-08-31 08:03:58 UTC
We tried it in 4.7 and it works perfectly. By now we will have to apply this patch for our development with 4.7.1.
Comment 7 Jonathan Wakely 2017-09-11 17:14:48 UTC
Please send patches to the gcc-patches mailing list for review. Patches in bugzilla are easily overlooked or forgotten.

https://gcc.gnu.org/contribute.html
Comment 8 Eric Gallager 2019-10-27 04:47:51 UTC
*** Bug 91998 has been marked as a duplicate of this bug. ***
Comment 9 GCC Commits 2021-05-20 08:13:00 UTC
The master branch has been updated by Christophe Lyon <clyon@gcc.gnu.org>:

https://gcc.gnu.org/g:1a9b3f04c11eb467a8dc504a37dad57a371a0d4c

commit r12-938-g1a9b3f04c11eb467a8dc504a37dad57a371a0d4c
Author: Christophe Lyon <christophe.lyon@linaro.org>
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  <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.
Comment 10 Christophe Lyon 2021-05-20 08:14:27 UTC
Fixed on trunk: we now support __FILE_NAME__, like clang.
Comment 11 R. Diez 2021-05-31 16:17:02 UTC
What is the target GCC version for __FILE_NAME__? GCC 12.1?
Comment 12 R. Diez 2021-05-31 16:20:53 UTC
*** Bug 77488 has been marked as a duplicate of this bug. ***
Comment 13 Christophe Lyon 2021-06-01 07:44:10 UTC
Yes, this will be part of gcc-12.