summaryrefslogtreecommitdiffstats
path: root/clang/lib/Lex/PPMacroExpansion.cpp
diff options
context:
space:
mode:
authorKristina Brooks <notstina@gmail.com>2019-05-16 21:13:49 +0000
committerKristina Brooks <notstina@gmail.com>2019-05-16 21:13:49 +0000
commitbd9748424165c24280ec1b2ddbe4e7522b15dc96 (patch)
tree2af4356cc6139c2b8ad7cd34ad1aa5a74e936c2a /clang/lib/Lex/PPMacroExpansion.cpp
parent708afb56c125ca4f7db7070e836860076c7eafbc (diff)
downloadbcm5719-llvm-bd9748424165c24280ec1b2ddbe4e7522b15dc96.tar.gz
bcm5719-llvm-bd9748424165c24280ec1b2ddbe4e7522b15dc96.zip
Reland "[Clang][PP] Add the __FILE_NAME__ builtin macro"
This relands commit rL360833 which caused issues on Win32 bots due to path handling/normalization differences. Now this uses `sys::path::filename` which should handle additional edge cases on Win32. Original commit: "[Clang][PP] Add the __FILE_NAME__ builtin macro" This patch adds the __FILE_NAME__ macro that expands to the last component of the path, similar to __FILE__ except with a guarantee that only the last path component (without the separator) will be rendered. I intend to follow through with discussion of this with WG14 as a potential inclusion in the C standard or failing that, try to discuss this with GCC developers since this extension is desired by GCC and Clang users/developers alike. Differential Revision: https://reviews.llvm.org/D61756 llvm-svn: 360938
Diffstat (limited to 'clang/lib/Lex/PPMacroExpansion.cpp')
-rw-r--r--clang/lib/Lex/PPMacroExpansion.cpp19
1 files changed, 17 insertions, 2 deletions
diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp
index d698f0e6578..d576f331d1b 100644
--- a/clang/lib/Lex/PPMacroExpansion.cpp
+++ b/clang/lib/Lex/PPMacroExpansion.cpp
@@ -43,6 +43,7 @@
#include "llvm/Support/Casting.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Format.h"
+#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cassert>
@@ -363,6 +364,7 @@ void Preprocessor::RegisterBuiltinMacros() {
}
// Clang Extensions.
+ Ident__FILE_NAME__ = RegisterBuiltinMacro(*this, "__FILE_NAME__");
Ident__has_feature = RegisterBuiltinMacro(*this, "__has_feature");
Ident__has_extension = RegisterBuiltinMacro(*this, "__has_extension");
Ident__has_builtin = RegisterBuiltinMacro(*this, "__has_builtin");
@@ -1474,7 +1476,8 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
// __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_NAME__) {
// 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());
@@ -1495,7 +1498,19 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
// Escape this filename. Turn '\' -> '\\' '"' -> '\"'
SmallString<128> FN;
if (PLoc.isValid()) {
- FN += PLoc.getFilename();
+ // __FILE_NAME__ is a Clang-specific extension that expands to the
+ // the last part of __FILE__.
+ if (II == Ident__FILE_NAME__) {
+ // Try to get the last path component, failing that return the original
+ // presumed location.
+ StringRef PLFileName = llvm::sys::path::filename(PLoc.getFilename());
+ if (PLFileName != "")
+ FN += PLFileName;
+ else
+ FN += PLoc.getFilename();
+ } else {
+ FN += PLoc.getFilename();
+ }
Lexer::Stringify(FN);
OS << '"' << FN << '"';
}
OpenPOWER on IntegriCloud