[clang] [clang-cl] Fix for __FUNCTION__ in c++. (PR #66120)

Zahira Ammarguellat via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 12 11:27:14 PDT 2023


https://github.com/zahiraam created https://github.com/llvm/llvm-project/pull/66120:

None

>From 3fcfa303bd211f9a3382657012968cd3f7269db8 Mon Sep 17 00:00:00 2001
From: Ammarguellat <zahira.ammarguellat at intel.com>
Date: Tue, 12 Sep 2023 11:25:19 -0700
Subject: [PATCH] [clang-cl] Fix for __FUNCTION__ in c++.

---
 clang/lib/AST/Expr.cpp        | 20 ++++++++++++++++++++
 clang/lib/AST/TypePrinter.cpp |  4 ++++
 2 files changed, 24 insertions(+)

diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index 4f3837371b3fc5..55b6e2968487b8 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -727,6 +727,26 @@ StringRef PredefinedExpr::getIdentKindName(PredefinedExpr::IdentKind IK) {
 std::string PredefinedExpr::ComputeName(IdentKind IK, const Decl *CurrentDecl) {
   ASTContext &Context = CurrentDecl->getASTContext();
 
+  if (CurrentDecl->getASTContext().getTargetInfo().getCXXABI().isMicrosoft() &&
+      IK == PredefinedExpr::Function) {
+    if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
+      SmallString<256> Name;
+      llvm::raw_svector_ostream Out(Name);
+      PrintingPolicy Policy(Context.getLangOpts());
+      Policy.AlwaysIncludeTypeForTemplateArgument = true;
+      std::string Proto;
+      llvm::raw_string_ostream POut(Proto);
+      const FunctionDecl *Decl = FD;
+      if (const FunctionDecl *Pattern = FD->getTemplateInstantiationPattern())
+        Decl = Pattern;
+      const FunctionType *AFT = Decl->getType()->getAs<FunctionType>();
+      const FunctionProtoType *FT = nullptr;
+      if (FD->hasWrittenPrototype())
+        FT = dyn_cast<FunctionProtoType>(AFT);
+      FD->printQualifiedName(POut, Policy);
+      return std::string(POut.str());
+    }
+  }
   if (IK == PredefinedExpr::FuncDName) {
     if (const NamedDecl *ND = dyn_cast<NamedDecl>(CurrentDecl)) {
       std::unique_ptr<MangleContext> MC;
diff --git a/clang/lib/AST/TypePrinter.cpp b/clang/lib/AST/TypePrinter.cpp
index eb69d0bb8755b4..676ce166312adf 100644
--- a/clang/lib/AST/TypePrinter.cpp
+++ b/clang/lib/AST/TypePrinter.cpp
@@ -2218,6 +2218,10 @@ printTo(raw_ostream &OS, ArrayRef<TA> Args, const PrintingPolicy &Policy,
     } else {
       if (!FirstArg)
         OS << Comma;
+      // zahira
+      //if (Argument.getKind() == TemplateArgument::Type)
+      //  OS << "class ";
+
       // Tries to print the argument with location info if exists.
       printArgument(Arg, Policy, ArgOS,
                     TemplateParameterList::shouldIncludeTypeForArgument(



More information about the cfe-commits mailing list