Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-cl] Fix value of __FUNCTION__ and __FUNC__ in MSVC mode for c++. #66120

Merged
merged 11 commits into from Sep 22, 2023

Conversation

zahiraam
Copy link
Contributor

@zahiraam zahiraam commented Sep 12, 2023

Predefined macro FUNCTION in clang is not returning the same string than MS for templated functions.

See https://godbolt.org/z/q3EKn5zq4

For the same test case MSVC is returning:

function: TestClass::TestClass
function: TestStruct::TestStruct
function: TestEnum::TestEnum

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Sep 12, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Sep 12, 2023

@llvm/pr-subscribers-clang

Changes

None

Full diff: https://github.com/llvm/llvm-project/pull/66120.diff

2 Files Affected:

  • (modified) clang/lib/AST/Expr.cpp (+20)
  • (modified) clang/lib/AST/TypePrinter.cpp (+4)
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index 4f3837371b3fc5e..55b6e2968487b86 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(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();
+      const FunctionProtoType *FT = nullptr;
+      if (FD->hasWrittenPrototype())
+        FT = dyn_cast(AFT);
+      FD->printQualifiedName(POut, Policy);
+      return std::string(POut.str());
+    }
+  }
   if (IK == PredefinedExpr::FuncDName) {
     if (const NamedDecl *ND = dyn_cast(CurrentDecl)) {
       std::unique_ptr MC;
diff --git a/clang/lib/AST/TypePrinter.cpp b/clang/lib/AST/TypePrinter.cpp
index eb69d0bb8755b48..676ce166312adf4 100644
--- a/clang/lib/AST/TypePrinter.cpp
+++ b/clang/lib/AST/TypePrinter.cpp
@@ -2218,6 +2218,10 @@ printTo(raw_ostream &OS, ArrayRef 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(

@@ -2218,6 +2218,9 @@ printTo(raw_ostream &OS, ArrayRef<TA> Args, const PrintingPolicy &Policy,
} else {
if (!FirstArg)
OS << Comma;
//if (Argument.getKind() == TemplateArgument::Type)
// OS << "class ";

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: if we go with this solution, this needs to happen here, but it should be under some conditions?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move it into printArgument. If you have to, it would be find to add a new flag to PrintingPolicy. We have lots of fine-grained flags there.

@zahiraam
Copy link
Contributor Author

There are still some debug info LIT tests that are failing, but I am not going to edit them because I am not quite sure that's correct. Probably adding the "class" keyword shouldn't happen at all times even when the MSVCPolicy is true?

@rnk rnk requested review from zmodem and removed request for rnk September 20, 2023 20:39
@zahiraam zahiraam marked this pull request as ready for review September 21, 2023 15:40
@llvmbot llvmbot added the clang:dataflow Clang Dataflow Analysis framework - https://clang.llvm.org/docs/DataFlowAnalysisIntro.html label Sep 21, 2023
Copy link
Collaborator

@rnk rnk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to make some minor naming suggestions, and I used the GitHub "edit this file" feature for the first time to commit them as new commits to your branch. I've never done this before, so I don't really know how it works, but clang-format isn't available, so now I've messed up the formatting...

I think you should be able to pull the suggested changes down, reformat the affected lines, commit that, push an updated commit, and then I can stamp that? I apologize if this isn't straightforward, it's my first time trying the tools.

@zahiraam
Copy link
Contributor Author

I wanted to make some minor naming suggestions, and I used the GitHub "edit this file" feature for the first time to commit them as new commits to your branch. I've never done this before, so I don't really know how it works, but clang-format isn't available, so now I've messed up the formatting...

I think you should be able to pull the suggested changes down, reformat the affected lines, commit that, push an updated commit, and then I can stamp that? I apologize if this isn't straightforward, it's my first time trying the tools.

No worries. I think it is synced up now. Thanks.

Copy link
Collaborator

@rnk rnk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@zahiraam zahiraam changed the title [clang-cl] Fix for __FUNCTION__ in c++. [clang-cl] Fix value of __FUNCTION__ and __FUNC__ in MSVC mode for c++. Sep 22, 2023
@zahiraam zahiraam merged commit 265568c into llvm:main Sep 22, 2023
2 checks passed
@RIscRIpt
Copy link
Member

This implementation has bugs, see #66114 (comment)

@zahiraam
Copy link
Contributor Author

This implementation has bugs, see #66114 (comment)

@RIscRIpt Thanks. Will look at them.

@Caslyn
Copy link
Contributor

Caslyn commented Sep 27, 2023

Hi @zahiraam - can we revert this change until #66114 (comment) is resolved? Unfortunately this is impacting our downstream windows-x64 builder.

@zahiraam
Copy link
Contributor Author

Hi @zahiraam - can we revert this change until #66114 (comment) is resolved? Unfortunately this is impacting our downstream windows-x64 builder.

I have a fix for it. It should be ready by eod (for me) today. But if that can't wait, sure we can revert it.

@petrhosek
Copy link
Member

Our preference would be to revert the change and reland it later since this doesn't appear to be a quick fix, which is in line with https://llvm.org/docs/DeveloperPolicy.html#patch-reversion-policy

@zahiraam
Copy link
Contributor Author

Our preference would be to revert the change and reland it later since this doesn't appear to be a quick fix, which is in line with https://llvm.org/docs/DeveloperPolicy.html#patch-reversion-policy

No problem. Go ahead and revert it. Thanks.

AaronBallman added a commit that referenced this pull request Sep 27, 2023
@AaronBallman
Copy link
Collaborator

I've reverted the changes in 9243b1b, but you should handle the revert yourself next time because you have commit privileges and it's possible the person requesting a revert does not. (If you can't do the revert immediately for whatever reason, it's fine to tell people something along the lines of "I'll revert by unless someone else is able to revert it sooner.")

@zahiraam
Copy link
Contributor Author

I've reverted the changes in 9243b1b, but you should handle the revert yourself next time because you have commit privileges and it's possible the person requesting a revert does not. (If you can't do the revert immediately for whatever reason, it's fine to tell people something along the lines of "I'll revert by unless someone else is able to revert it sooner.")

Sorry. Didn't realize that (I would have expected that the person would let me know). Thanks.

@AaronBallman
Copy link
Collaborator

I've reverted the changes in 9243b1b, but you should handle the revert yourself next time because you have commit privileges and it's possible the person requesting a revert does not. (If you can't do the revert immediately for whatever reason, it's fine to tell people something along the lines of "I'll revert by unless someone else is able to revert it sooner.")

Sorry. Didn't realize that (I would have expected that the person would let me know). Thanks.

No worries! We could probably make the policy documentation more clear about that.

legrosbuffle pushed a commit to legrosbuffle/llvm-project that referenced this pull request Sep 29, 2023
zahiraam added a commit that referenced this pull request Mar 19, 2024
Predefined macro FUNCTION in clang is not returning the same string than
MS for templated functions.

See https://godbolt.org/z/q3EKn5zq4

For the same test case MSVC is returning:

function: TestClass::TestClass
function: TestStruct::TestStruct
function: TestEnum::TestEnum

The initial work for this was in the reverted patch
(#66120). This patch solves the
issues raised in the reverted patch.
chencha3 pushed a commit to chencha3/llvm-project that referenced this pull request Mar 23, 2024
Predefined macro FUNCTION in clang is not returning the same string than
MS for templated functions.

See https://godbolt.org/z/q3EKn5zq4

For the same test case MSVC is returning:

function: TestClass::TestClass
function: TestStruct::TestStruct
function: TestEnum::TestEnum

The initial work for this was in the reverted patch
(llvm#66120). This patch solves the
issues raised in the reverted patch.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:dataflow Clang Dataflow Analysis framework - https://clang.llvm.org/docs/DataFlowAnalysisIntro.html clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants