[PATCH] D136343: [Lex] Add compatibility with MSVC

Qfrost via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 20 05:38:34 PDT 2022


Qfrost911 created this revision.
Qfrost911 added a reviewer: shafik.
Herald added a project: All.
Qfrost911 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

The code below is valid in MSVC, but it is not allowed in clang

  printf(__FUNCTION__ "Hello World");

This patch fixes the compatibility with MSVC.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D136343

Files:
  clang/include/clang/Lex/Preprocessor.h
  clang/lib/Lex/PPMacroExpansion.cpp


Index: clang/lib/Lex/PPMacroExpansion.cpp
===================================================================
--- clang/lib/Lex/PPMacroExpansion.cpp
+++ clang/lib/Lex/PPMacroExpansion.cpp
@@ -337,6 +337,11 @@
 /// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
 /// identifier table.
 void Preprocessor::RegisterBuiltinMacros() {
+#ifdef _WIN32
+  Ident__FUNCTION__ = RegisterBuiltinMacro(*this, "__FUNCTION__");
+#else
+  Ident__FUNCTION__ = Ident__LINE__ = RegisterBuiltinMacro(*this, "__LINE__");
+#endif
   Ident__LINE__ = RegisterBuiltinMacro(*this, "__LINE__");
   Ident__FILE__ = RegisterBuiltinMacro(*this, "__FILE__");
   Ident__DATE__ = RegisterBuiltinMacro(*this, "__DATE__");
@@ -1561,6 +1566,13 @@
           FN += PLoc.getFilename();
       } else {
         FN += PLoc.getFilename();
+#ifdef _WIN32
+        if (II == Ident__FUNCTION__) {
+          FN += "(";
+          FN += Twine(PLoc.getLine()).str();
+          FN += ") ";
+        }
+#endif
       }
       processPathForFileMacro(FN, getLangOpts(), getTargetInfo());
       Lexer::Stringify(FN);
Index: clang/include/clang/Lex/Preprocessor.h
===================================================================
--- clang/include/clang/Lex/Preprocessor.h
+++ clang/include/clang/Lex/Preprocessor.h
@@ -150,7 +150,7 @@
   llvm::BumpPtrAllocator BP;
 
   /// Identifiers for builtin macros and other builtins.
-  IdentifierInfo *Ident__LINE__, *Ident__FILE__;   // __LINE__, __FILE__
+  IdentifierInfo *Ident__LINE__, *Ident__FILE__, *Ident__FUNCTION__;   // __LINE__, __FILE__, __FUNCTION__
   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: D136343.469182.patch
Type: text/x-patch
Size: 1806 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221020/a185c91b/attachment.bin>


More information about the cfe-commits mailing list