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__ 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 #include @@ -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: 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; +}