Skip to content

Fix override keyword being print to the left side #88453

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

Merged
merged 1 commit into from
Apr 16, 2024

Conversation

giulianobelinassi
Copy link

Previously, the override keyword in C++ was being print in the left side of a method decl, which is unsupported by C++ standard. This commit fixes that by setting the CanPrintOnLeft field to 0, forcing it to be print on the right side of the decl.

This comes from the fact that #87281 won't be backported into clang-18. However since classes with override keyword is broken in LLVM-18, I think this is still worth fixing.

@vgvassilev Please check if it works in your test cases.

Notice that this PR targets release/18.x, and not the main branch.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Apr 11, 2024
@llvmbot
Copy link
Member

llvmbot commented Apr 11, 2024

@llvm/pr-subscribers-clang

Author: Giuliano Belinassi (giulianobelinassi)

Changes

Previously, the override keyword in C++ was being print in the left side of a method decl, which is unsupported by C++ standard. This commit fixes that by setting the CanPrintOnLeft field to 0, forcing it to be print on the right side of the decl.

This comes from the fact that #87281 won't be backported into clang-18. However since classes with override keyword is broken in LLVM-18, I think this is still worth fixing.

@vgvassilev Please check if it works in your test cases.

Notice that this PR targets release/18.x, and not the main branch.


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

2 Files Affected:

  • (modified) clang/include/clang/Basic/Attr.td (+2)
  • (added) clang/test/AST/ast-dump-override-final.cpp (+20)
diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td
index 58838b01b4fd7c..dbf2dd2120fb69 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -1590,6 +1590,7 @@ def RegCall : DeclOrTypeAttr {
 }
 
 def Final : InheritableAttr {
+  let CanPrintOnLeft = 0;
   let Spellings = [CustomKeyword<"final">, CustomKeyword<"sealed">];
   let Accessors = [Accessor<"isSpelledAsSealed", [CustomKeyword<"sealed">]>];
   let SemaHandler = 0;
@@ -2472,6 +2473,7 @@ def Overloadable : Attr {
 }
 
 def Override : InheritableAttr {
+  let CanPrintOnLeft = 0;
   let Spellings = [CustomKeyword<"override">];
   let SemaHandler = 0;
   // Omitted from docs, since this is language syntax, not an attribute, as far
diff --git a/clang/test/AST/ast-dump-override-final.cpp b/clang/test/AST/ast-dump-override-final.cpp
new file mode 100644
index 00000000000000..c1cee6b01565f6
--- /dev/null
+++ b/clang/test/AST/ast-dump-override-final.cpp
@@ -0,0 +1,20 @@
+// This file contain tests to check if override and final are dumped in the
+// correct positions.
+
+// RUN: %clang_cc1 -ast-print -x c++ %s -o - | FileCheck %s
+
+// CHECK: class A {
+class A {
+  // CHECK-NEXT: virtual void f();
+  virtual void f();
+
+  // CHECK-NEXT: virtual void g() final;
+  virtual void g() final;
+} AA;
+
+// CHECK: class B : public A {
+class B : public A {
+  // CHECK-NEXT: virtual void f() override {
+  virtual void f() override {
+  };
+} B;

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

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

I don't think we should add this to the 18.x release -- we're already getting requests from users to not do that: https://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20240408/566402.html

@AaronBallman AaronBallman added this to the LLVM 18.X Release milestone Apr 12, 2024
@giulianobelinassi
Copy link
Author

I don't think we should add this to the 18.x release -- we're already getting requests from users to not do that: https://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20240408/566402.html

This doesn't affect the number of spaces and it is not based on vgvassilev's patch.

The test case in question:

~/projects/llvm-project/llvm/build/bin/clang++ a.cpp -Xclang -ast-print -S -o -
template <typename T> class FinalTemplate;

This would only change the position of the override and final keywords.

@AaronBallman
Copy link
Collaborator

I think the 18.1 behavior is already correct?

https://godbolt.org/z/v3G7h44E1

@giulianobelinassi
Copy link
Author

I think the 18.1 behavior is already correct?

https://godbolt.org/z/v3G7h44E1

It is not:
https://godbolt.org/z/96rWese3P

@AaronBallman
Copy link
Collaborator

Ah that's right, it was with definitions that we ran into the issue! Sorry for being slow today. Okay, I retract my concerns, this is a safe and targeted fix.

@AaronBallman AaronBallman requested a review from erichkeane April 12, 2024 13:49
Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

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

I think this is reasonable, but leaving it to @erichkeane to make the final call.

@giulianobelinassi
Copy link
Author

The branch is marked as Changes requested but no change was pointed in the code. Is everything correct?

@Sirraide
Copy link
Member

The branch is marked as Changes requested but no change was pointed in the code. Is everything correct?

That’s just because Aaron didn’t submit another review, so github still marks it as ‘changes requested’; seeing as it has been approved, the rest is now up to the release manager.

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

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

Sorry about leaving it in a "requests changes" state, LGTM!

Previously, the `override` keyword in C++ was being print in the left
side of a method decl, which is unsupported by C++ standard. This commit
fixes that by setting the `CanPrintOnLeft` field to 0, forcing it to be
print on the right side of the decl.

Signed-off-by: Giuliano Belinassi <[email protected]>
@tstellar tstellar merged commit e3c832b into llvm:release/18.x Apr 16, 2024
@tstellar
Copy link
Collaborator

Hi @giulianobelinassi (or anyone else). If you would like to add a note about this fix in the release notes (completely optional). Please reply to this comment with a one or two sentence description of the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
Development

Successfully merging this pull request may close these issues.

6 participants