Index: clang/lib/Lex/PPMacroExpansion.cpp =================================================================== --- clang/lib/Lex/PPMacroExpansion.cpp +++ clang/lib/Lex/PPMacroExpansion.cpp @@ -1505,24 +1505,11 @@ // __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__) { // 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()); - // __BASE_FILE__ is a GNU extension that returns the top of the presumed - // #include stack instead of the current file. - if (II == Ident__BASE_FILE__ && PLoc.isValid()) { - SourceLocation NextLoc = PLoc.getIncludeLoc(); - while (NextLoc.isValid()) { - PLoc = SourceMgr.getPresumedLoc(NextLoc); - if (PLoc.isInvalid()) - break; - - NextLoc = PLoc.getIncludeLoc(); - } - } - // Escape this filename. Turn '\' -> '\\' '"' -> '\"' SmallString<128> FN; if (PLoc.isValid()) { @@ -1531,6 +1518,12 @@ OS << '"' << FN << '"'; } Tok.setKind(tok::string_literal); + } else if (II == Ident__BASE_FILE__) { + SmallString<128> FN = + SourceMgr.getFileEntryForID(SourceMgr.getMainFileID())->getName(); + Lexer::Stringify(FN); + OS << '"' << FN << '"'; + Tok.setKind(tok::string_literal); } else if (II == Ident__DATE__) { Diag(Tok.getLocation(), diag::warn_pp_date_time); if (!DATELoc.isValid()) Index: clang/test/Preprocessor/Inputs/base_file.h =================================================================== --- /dev/null +++ clang/test/Preprocessor/Inputs/base_file.h @@ -0,0 +1 @@ +__BASE_FILE__ Index: clang/test/Preprocessor/base_file.c =================================================================== --- /dev/null +++ clang/test/Preprocessor/base_file.c @@ -0,0 +1,9 @@ +// RUN: %clang_cc1 -include %S/Inputs/base_file.h -E %s | FileCheck %s + +// CHECK: {{[\\/]}}base_file.c" + +// CHECK: {{[\\/]}}base_file.c" +#include "Inputs/base_file.h" + +// CHECK: {{[\\/]}}base_file.c" +__BASE_FILE__