[PATCH] D55159: Lex: Fix bug in __BASE_FILE__ implementation.

Peter Collingbourne via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 30 21:39:22 PST 2018


pcc created this revision.
pcc added a reviewer: rsmith.
Herald added a subscriber: llvm-commits.

__BASE_FILE__ previously expanded to "<built-in>" instead of the name
of the main source file in files included using -include.

Also added test coverage for __BASE_FILE__ since there doesn't seem
to be any.


Repository:
  rL LLVM

https://reviews.llvm.org/D55159

Files:
  clang/lib/Lex/PPMacroExpansion.cpp
  clang/test/Preprocessor/Inputs/base_file.h
  clang/test/Preprocessor/base_file.c


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__
Index: clang/test/Preprocessor/Inputs/base_file.h
===================================================================
--- /dev/null
+++ clang/test/Preprocessor/Inputs/base_file.h
@@ -0,0 +1 @@
+__BASE_FILE__
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())


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D55159.176243.patch
Type: text/x-patch
Size: 2294 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181201/6fc4d5dc/attachment-0001.bin>


More information about the cfe-commits mailing list