[PATCH] D17741: adds __FILE_BASENAME__ builtin macro
Weiming Zhao via cfe-commits
cfe-commits at lists.llvm.org
Mon Feb 29 13:58:54 PST 2016
weimingz created this revision.
weimingz added a subscriber: cfe-commits.
__FILE__ will be expanded to the full path. In some embedded system scenarios, the final images is linked from many objs and the image size is a very important factor.
The full filenames can occupy a lot space in the string pool and most of the time, knowing the base name is sufficient.
__FILE_BASENAME__ can be used in this scenario.
http://reviews.llvm.org/D17741
Files:
include/clang/Lex/Preprocessor.h
lib/Lex/PPMacroExpansion.cpp
test/Lexer/file_basename.c
Index: test/Lexer/file_basename.c
===================================================================
--- /dev/null
+++ test/Lexer/file_basename.c
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -E %s | FileCheck %s
+
+const char *filename (const char *name) {
+ // CHECK: static const char this_file_basename[] = "file_basename.c";
+ static const char this_file_basename[] = __FILE_BASENAME__;
+ // CHECK: static const char this_file[] = "{{.*}}/Lexer/file_basename.c";
+ static const char this_file[] = __FILE__;
+ return this_file;
+}
Index: lib/Lex/PPMacroExpansion.cpp
===================================================================
--- lib/Lex/PPMacroExpansion.cpp
+++ lib/Lex/PPMacroExpansion.cpp
@@ -28,6 +28,7 @@
#include "llvm/Config/llvm-config.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Format.h"
+#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include <cstdio>
#include <ctime>
@@ -292,6 +293,7 @@
void Preprocessor::RegisterBuiltinMacros() {
Ident__LINE__ = RegisterBuiltinMacro(*this, "__LINE__");
Ident__FILE__ = RegisterBuiltinMacro(*this, "__FILE__");
+ Ident__FILE_BASENAME__ = RegisterBuiltinMacro(*this, "__FILE_BASENAME__");
Ident__DATE__ = RegisterBuiltinMacro(*this, "__DATE__");
Ident__TIME__ = RegisterBuiltinMacro(*this, "__TIME__");
Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__");
@@ -1509,7 +1511,8 @@
// __LINE__ expands to a simple numeric value.
OS << (PLoc.isValid()? PLoc.getLine() : 1);
Tok.setKind(tok::numeric_constant);
- } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
+ } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__ ||
+ II == Ident__FILE_BASENAME__) {
// C99 6.10.8: "__FILE__: The presumed name of the current source file (a
// character string literal)". This can be affected by #line.
PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
@@ -1530,7 +1533,9 @@
// Escape this filename. Turn '\' -> '\\' '"' -> '\"'
SmallString<128> FN;
if (PLoc.isValid()) {
- FN += PLoc.getFilename();
+ FN += II == Ident__FILE_BASENAME__
+ ? llvm::sys::path::filename(PLoc.getFilename())
+ : PLoc.getFilename();
Lexer::Stringify(FN);
OS << '"' << FN << '"';
}
Index: include/clang/Lex/Preprocessor.h
===================================================================
--- include/clang/Lex/Preprocessor.h
+++ include/clang/Lex/Preprocessor.h
@@ -119,6 +119,7 @@
/// Identifiers for builtin macros and other builtins.
IdentifierInfo *Ident__LINE__, *Ident__FILE__; // __LINE__, __FILE__
+ IdentifierInfo *Ident__FILE_BASENAME__; // __FILE_BASENAME__
IdentifierInfo *Ident__DATE__, *Ident__TIME__; // __DATE__, __TIME__
IdentifierInfo *Ident__INCLUDE_LEVEL__; // __INCLUDE_LEVEL__
IdentifierInfo *Ident__BASE_FILE__; // __BASE_FILE__
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D17741.49414.patch
Type: text/x-patch
Size: 2942 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160229/397183ad/attachment.bin>
More information about the cfe-commits
mailing list