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.
Created attachment 19445 [details]
Attaching the patch. Also sent to gcc-patches.
__BASE_FILE__ could be enough for what you want.
(In reply to comment #2)
> __BASE_FILE__ could be enough for what you want.
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.
(In reply to comment #0)
> Please consider this patch. It adds support for obtaining file basename via
What would also be really nice would be to have similar functionality for C++, with __CLASS__ and __METHOD__ macros.
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.
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.
Please send patches to the gcc-patches mailing list for review. Patches in bugzilla are easily overlooked or forgotten.
*** Bug 91998 has been marked as a duplicate of this bug. ***
The master branch has been updated by Christophe Lyon <firstname.lastname@example.org>:
Author: Christophe Lyon <email@example.com>
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
Given these input files:
char* mystringh_file = __FILE__;
char* mystringh_filename = __FILE_NAME__;
char* mystringh_base_file = __BASE_FILE__;
char* mystring_file = __FILE__;
char* mystring_filename = __FILE_NAME__;
char* mystring_base_file = __BASE_FILE__;
$ 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 <firstname.lastname@example.org>
Torbjörn Svensson <email@example.com>
* 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.
* doc/cpp.texi (Common Predefined Macros): Document __FILE_NAME__.
* c-c++-common/spellcheck-reserved.c: Add tests for __FILE_NAME__.
* c-c++-common/cpp/file-name-1.c: New test.
Fixed on trunk: we now support __FILE_NAME__, like clang.
What is the target GCC version for __FILE_NAME__? GCC 12.1?
*** Bug 77488 has been marked as a duplicate of this bug. ***
Yes, this will be part of gcc-12.