[PATCH] D156533: [clang][DeclPrinter] Fix missing semicolon in AST print for methods that are definitions without having a body

Timo Stripf via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 28 06:23:51 PDT 2023


strimo378 created this revision.
strimo378 added a reviewer: aaron.ballman.
Herald added a project: All.
strimo378 requested review of this revision.
Herald added subscribers: cfe-commits, wangpc.
Herald added a project: clang.

DeclPrinter used FunctionDecl::isThisDeclarationADefinition to decide if the decl requires a semicolon at the end. However, there are several methods without body (that require a semicolon) that are definitions.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156533

Files:
  clang/lib/AST/DeclPrinter.cpp
  clang/test/AST/ast-print-method-decl.cpp


Index: clang/test/AST/ast-print-method-decl.cpp
===================================================================
--- clang/test/AST/ast-print-method-decl.cpp
+++ clang/test/AST/ast-print-method-decl.cpp
@@ -85,3 +85,18 @@
 
   // CHECK-NEXT: };
 };
+
+
+// CHECK: struct DefMethodsWithoutBody {
+struct DefMethodsWithoutBody {
+  // CHECK-NEXT: DefMethodsWithoutBody() = delete;
+  DefMethodsWithoutBody() = delete;
+
+  // CHECK-NEXT: DefMethodsWithoutBody() = default;
+  ~DefMethodsWithoutBody() = default;
+
+  // CHECK-NEXT: void m1() __attribute__((alias("X")));
+  void m1() __attribute__((alias("X")));
+
+  // CHECK-NEXT: };
+};
Index: clang/lib/AST/DeclPrinter.cpp
===================================================================
--- clang/lib/AST/DeclPrinter.cpp
+++ clang/lib/AST/DeclPrinter.cpp
@@ -463,12 +463,12 @@
     else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->hasBody())
       Terminator = nullptr;
     else if (auto FD = dyn_cast<FunctionDecl>(*D)) {
-      if (FD->isThisDeclarationADefinition())
+      if (FD->doesThisDeclarationHaveABody() && !FD->isDefaulted())
         Terminator = nullptr;
       else
         Terminator = ";";
     } else if (auto TD = dyn_cast<FunctionTemplateDecl>(*D)) {
-      if (TD->getTemplatedDecl()->isThisDeclarationADefinition())
+      if (TD->getTemplatedDecl()->doesThisDeclarationHaveABody())
         Terminator = nullptr;
       else
         Terminator = ";";


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156533.545130.patch
Type: text/x-patch
Size: 1451 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230728/8c0cd0b2/attachment.bin>


More information about the cfe-commits mailing list