This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH] support for obtaining file basename
- From: Robert Millan <rmh at aybabtu dot com>
- To: gcc-patches at gcc dot gnu dot org
- Date: Sat, 2 Jan 2010 11:11:24 +0100
- Subject: [PATCH] support for obtaining file basename
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.
Also filed as: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42579
--
Robert Millan
"Be the change you want to see in the world" -- Gandhi
2010-01-02 Robert Millan <rmh.grub@aybabtu.com>
Support for obtaining file basename via __FILE_BASENAME__.
* include/cpplib.h (cpp_builtin_type): Add `BT_FILE_BASENAME'.
* init.c (builtin_array): Add entry for `__FILE_BASENAME__'.
* macro.c (_cpp_builtin_macro_text): Recognize and act upon
`BT_FILE_BASENAME'.
diff -urp libcpp.old/include/cpplib.h libcpp/include/cpplib.h
--- libcpp.old/include/cpplib.h 2010-01-02 10:53:42.000000000 +0100
+++ libcpp/include/cpplib.h 2010-01-01 21:05:56.000000000 +0100
@@ -595,6 +595,7 @@ enum cpp_builtin_type
BT_SPECLINE = 0, /* `__LINE__' */
BT_DATE, /* `__DATE__' */
BT_FILE, /* `__FILE__' */
+ BT_FILE_BASENAME, /* `__FILE_BASENAME__' */
BT_BASE_FILE, /* `__BASE_FILE__' */
BT_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
BT_TIME, /* `__TIME__' */
diff -urp libcpp.old/init.c libcpp/init.c
--- libcpp.old/init.c 2010-01-02 10:53:42.000000000 +0100
+++ libcpp/init.c 2010-01-01 21:05:56.000000000 +0100
@@ -344,6 +344,7 @@ static const struct builtin_macro builti
B("__TIME__", BT_TIME, false),
B("__DATE__", BT_DATE, false),
B("__FILE__", BT_FILE, false),
+ B("__FILE_BASENAME__", BT_FILE_BASENAME, false),
B("__BASE_FILE__", BT_BASE_FILE, false),
B("__LINE__", BT_SPECLINE, true),
B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL, true),
diff -urp libcpp.old/macro.c libcpp/macro.c
--- libcpp.old/macro.c 2010-01-02 10:53:42.000000000 +0100
+++ libcpp/macro.c 2010-01-01 21:05:56.000000000 +0100
@@ -1,7 +1,7 @@
/* Part of CPP library. (Macro and #define handling.)
Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998,
1999, 2000, 2001, 2002, 2003, 2004, 2005,
- 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
+ 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
Written by Per Bothner, 1994.
Based on CCCP program by Paul Rubin, June 1986
Adapted to ANSI C, Richard Stallman, Jan 1987
@@ -166,6 +166,7 @@ _cpp_builtin_macro_text (cpp_reader *pfi
}
break;
case BT_FILE:
+ case BT_FILE_BASENAME:
case BT_BASE_FILE:
{
unsigned int len;
@@ -178,6 +179,10 @@ _cpp_builtin_macro_text (cpp_reader *pfi
map = INCLUDED_FROM (pfile->line_table, map);
name = map->to_file;
+
+ if (node->value.builtin == BT_FILE_BASENAME)
+ name = basename (name);
+
len = strlen (name);
buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
result = buf;