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

MSVC makes these string literals [1][2].

[1] https://godbolt.org/z/6vnTzbExx
[2] 
https://learn.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=msvc-170

Fixes #114


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D146764

Files:
  clang/include/clang/Testing/TestClangConfig.h
  clang/lib/Sema/SemaExpr.cpp
  clang/test/CodeGen/predefined-expr.c
  clang/test/Sema/ms_predefined_expr.cpp
  clang/unittests/AST/ASTImporterTest.cpp
  clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp

Index: clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
===================================================================
--- clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
@@ -1058,6 +1058,10 @@
 }
 
 TEST_P(ASTMatchersTest, PredefinedExpr) {
+  // Predefined expressions are string literals under Microsoft extensions.
+  if (GetParam().isWin32())
+    return;
+
   // __func__ expands as StringLiteral("foo")
   EXPECT_TRUE(matches("void foo() { __func__; }",
                       predefinedExpr(hasType(asString("const char[4]")),
Index: clang/unittests/AST/ASTImporterTest.cpp
===================================================================
--- clang/unittests/AST/ASTImporterTest.cpp
+++ clang/unittests/AST/ASTImporterTest.cpp
@@ -509,6 +509,11 @@
 }
 
 TEST_P(ImportExpr, ImportPredefinedExpr) {
+  // Predefined expressions are string literals under Microsoft extensions.
+  if (std::find(GetParam().begin(), GetParam().end(), "-fms-compatibility") !=
+      GetParam().end())
+    return;
+
   MatchVerifier<Decl> Verifier;
   // __func__ expands as StringLiteral("declToImport")
   testImport("void declToImport() { (void)__func__; }", Lang_CXX03, "",
Index: clang/test/Sema/ms_predefined_expr.cpp
===================================================================
--- /dev/null
+++ clang/test/Sema/ms_predefined_expr.cpp
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 %s -fsyntax-only -Wmicrosoft -verify -fms-extensions
+// expected-no-diagnostics
+
+void f(void) {
+ const char a[] = __FUNCTION__;
+ const char b[] = __FUNCDNAME__;
+ const char c[] = __FUNCSIG__;
+ const char d[] = __func__;
+ const char e[] = __PRETTY_FUNCTION__;
+}
Index: clang/test/CodeGen/predefined-expr.c
===================================================================
--- clang/test/CodeGen/predefined-expr.c
+++ clang/test/CodeGen/predefined-expr.c
@@ -1,16 +1,27 @@
-// RUN: %clang_cc1 %s -triple %itanium_abi_triple -emit-llvm -o - | FileCheck %s
-// RUN: %clang_cc1 -fms-extensions %s -triple %itanium_abi_triple -emit-llvm -o - | FileCheck %s
-
-// CHECK: @__func__.plainFunction = private unnamed_addr constant [14 x i8] c"plainFunction\00"
-// CHECK: @__PRETTY_FUNCTION__.plainFunction = private unnamed_addr constant [25 x i8] c"void plainFunction(void)\00"
-// CHECK: @__func__.externFunction = private unnamed_addr constant [15 x i8] c"externFunction\00"
-// CHECK: @__PRETTY_FUNCTION__.externFunction = private unnamed_addr constant [26 x i8] c"void externFunction(void)\00"
-// CHECK: @__func__.privateExternFunction = private unnamed_addr constant [22 x i8] c"privateExternFunction\00"
-// CHECK: @__PRETTY_FUNCTION__.privateExternFunction = private unnamed_addr constant [33 x i8] c"void privateExternFunction(void)\00"
-// CHECK: @__func__.__captured_stmt = private unnamed_addr constant [25 x i8] c"functionWithCapturedStmt\00"
-// CHECK: @__PRETTY_FUNCTION__.__captured_stmt = private unnamed_addr constant [36 x i8] c"void functionWithCapturedStmt(void)\00"
-// CHECK: @__func__.staticFunction = private unnamed_addr constant [15 x i8] c"staticFunction\00"
-// CHECK: @__PRETTY_FUNCTION__.staticFunction = private unnamed_addr constant [26 x i8] c"void staticFunction(void)\00"
+// RUN: %clang_cc1 %s -triple %itanium_abi_triple -emit-llvm -o - | FileCheck %s --check-prefix=ITANIUM
+// RUN: %clang_cc1 -fms-extensions %s -triple %itanium_abi_triple -emit-llvm -o - | FileCheck %s --check-prefix=MS
+
+// ITANIUM: @__func__.plainFunction = private unnamed_addr constant [14 x i8] c"plainFunction\00"
+// ITANIUM: @__PRETTY_FUNCTION__.plainFunction = private unnamed_addr constant [25 x i8] c"void plainFunction(void)\00"
+// ITANIUM: @__func__.externFunction = private unnamed_addr constant [15 x i8] c"externFunction\00"
+// ITANIUM: @__PRETTY_FUNCTION__.externFunction = private unnamed_addr constant [26 x i8] c"void externFunction(void)\00"
+// ITANIUM: @__func__.privateExternFunction = private unnamed_addr constant [22 x i8] c"privateExternFunction\00"
+// ITANIUM: @__PRETTY_FUNCTION__.privateExternFunction = private unnamed_addr constant [33 x i8] c"void privateExternFunction(void)\00"
+// ITANIUM: @__func__.__captured_stmt = private unnamed_addr constant [25 x i8] c"functionWithCapturedStmt\00"
+// ITANIUM: @__PRETTY_FUNCTION__.__captured_stmt = private unnamed_addr constant [36 x i8] c"void functionWithCapturedStmt(void)\00"
+// ITANIUM: @__func__.staticFunction = private unnamed_addr constant [15 x i8] c"staticFunction\00"
+// ITANIUM: @__PRETTY_FUNCTION__.staticFunction = private unnamed_addr constant [26 x i8] c"void staticFunction(void)\00"
+
+// MS: @.str.{{[0-9]+}} = private unnamed_addr constant [14 x i8] c"plainFunction\00"
+// MS: @.str.{{[0-9]+}} = private unnamed_addr constant [25 x i8] c"void plainFunction(void)\00"
+// MS: @.str.{{[0-9]+}} = private unnamed_addr constant [15 x i8] c"externFunction\00"
+// MS: @.str.{{[0-9]+}} = private unnamed_addr constant [26 x i8] c"void externFunction(void)\00"
+// MS: @.str.{{[0-9]+}} = private unnamed_addr constant [22 x i8] c"privateExternFunction\00"
+// MS: @.str.{{[0-9]+}} = private unnamed_addr constant [33 x i8] c"void privateExternFunction(void)\00"
+// MS: @.str.{{[0-9]+}} = private unnamed_addr constant [25 x i8] c"functionWithCapturedStmt\00"
+// MS: @.str.{{[0-9]+}} = private unnamed_addr constant [36 x i8] c"void functionWithCapturedStmt(void)\00"
+// MS: @.str.{{[0-9]+}} = private unnamed_addr constant [15 x i8] c"staticFunction\00"
+// MS: @.str.{{[0-9]+}} = private unnamed_addr constant [26 x i8] c"void staticFunction(void)\00"
 
 int printf(const char *, ...);
 
Index: clang/lib/Sema/SemaExpr.cpp
===================================================================
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -3552,9 +3552,10 @@
 
   QualType ResTy;
   StringLiteral *SL = nullptr;
-  if (cast<DeclContext>(currentDecl)->isDependentContext())
+  if (cast<DeclContext>(currentDecl)->isDependentContext()) {
     ResTy = Context.DependentTy;
-  else {
+    return PredefinedExpr::Create(Context, Loc, ResTy, IK, SL);
+  } else {
     // Pre-defined identifiers are of type char[x], where x is the length of
     // the string.
     auto Str = PredefinedExpr::ComputeName(IK, currentDecl);
@@ -3582,6 +3583,11 @@
     }
   }
 
+  // MSVC treats all predefined expressions as string literals rather than char
+  // arrays.
+  if (LangOpts.MicrosoftExt)
+    return SL;
+
   return PredefinedExpr::Create(Context, Loc, ResTy, IK, SL);
 }
 
Index: clang/include/clang/Testing/TestClangConfig.h
===================================================================
--- clang/include/clang/Testing/TestClangConfig.h
+++ clang/include/clang/Testing/TestClangConfig.h
@@ -58,9 +58,9 @@
            Language == Lang_CXX14;
   }
 
-  bool hasDelayedTemplateParsing() const {
-    return Target == "x86_64-pc-win32-msvc";
-  }
+  bool isWin32() const { return Target == "x86_64-pc-win32-msvc"; }
+
+  bool hasDelayedTemplateParsing() const { return isWin32(); }
 
   std::vector<std::string> getCommandLineArgs() const {
     std::vector<std::string> Result = getCommandLineArgsForTesting(Language);
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATC... Arthur Eubanks via Phabricator via cfe-commits
    • ... Hans Wennborg via Phabricator via cfe-commits
    • ... Aaron Ballman via Phabricator via cfe-commits
    • ... Martin Storsj√∂ via Phabricator via cfe-commits
    • ... Arthur Eubanks via Phabricator via cfe-commits
    • ... Aaron Ballman via Phabricator via cfe-commits
    • ... Corentin Jabot via Phabricator via cfe-commits
    • ... Arthur Eubanks (out until mid-April) via Phabricator via cfe-commits
    • ... Richard Smith - zygoloid via Phabricator via cfe-commits
    • ... Aaron Ballman via Phabricator via cfe-commits

Reply via email to