[clang] [clang-tools-extra] [clang] Fix CXXNewExpr end source location for 'new struct S' (PR #92266)

Arseniy Zaostrovnykh via cfe-commits cfe-commits at lists.llvm.org
Thu May 16 00:29:22 PDT 2024


https://github.com/necto updated https://github.com/llvm/llvm-project/pull/92266

>From eeb24ddbf261fd7667dd05feee14637bc379d182 Mon Sep 17 00:00:00 2001
From: Arseniy Zaostrovnykh <necto.ne at gmail.com>
Date: Wed, 15 May 2024 16:02:07 +0200
Subject: [PATCH 1/3] Fix CXXNewExpr end source location for 'new struct S'

---
 clang/lib/Parse/ParseDeclCXX.cpp      | 1 +
 clang/test/Misc/ast-source-ranges.cpp | 5 +++++
 2 files changed, 6 insertions(+)
 create mode 100644 clang/test/Misc/ast-source-ranges.cpp

diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index 65ddebca49bc6..32c4e923243a9 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -1883,6 +1883,7 @@ void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
   if (Tok.is(tok::identifier)) {
     Name = Tok.getIdentifierInfo();
     NameLoc = ConsumeToken();
+    DS.SetRangeEnd(NameLoc);
 
     if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
       // The name was supposed to refer to a template, but didn't.
diff --git a/clang/test/Misc/ast-source-ranges.cpp b/clang/test/Misc/ast-source-ranges.cpp
new file mode 100644
index 0000000000000..9c0ab9449a6f5
--- /dev/null
+++ b/clang/test/Misc/ast-source-ranges.cpp
@@ -0,0 +1,5 @@
+// RUN: %clang_cc1 -ast-dump %s  2>&1 | FileCheck %s
+
+struct Sock {};
+void leakNewFn() { new struct Sock; }
+// CHECK: CXXNewExpr {{.*}} <col:20, col:31> 'struct Sock *'

>From 28d5f458542d1fed2b3d82543c93e61a2768637c Mon Sep 17 00:00:00 2001
From: Arseniy Zaostrovnykh <necto.ne at gmail.com>
Date: Wed, 15 May 2024 16:53:53 +0200
Subject: [PATCH 2/3] Fix clang-tidy:make-unique

---
 .../test/clang-tidy/checkers/modernize/make-unique.cpp     | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/make-unique.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/make-unique.cpp
index 7934c6e93ffbd..fe512a8f3bf32 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/make-unique.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/make-unique.cpp
@@ -606,11 +606,8 @@ void invoke_template() {
   template_fun(foo);
 }
 
-void no_fix_for_invalid_new_loc() {
-  // FIXME: Although the code is valid, the end location of `new struct Base` is
-  // invalid. Correct it once https://bugs.llvm.org/show_bug.cgi?id=35952 is
-  // fixed.
+void fix_for_c_style_struct() {
   auto T = std::unique_ptr<Base>(new struct Base);
   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use std::make_unique instead
-  // CHECK-FIXES: auto T = std::unique_ptr<Base>(new struct Base);
+  // CHECK-FIXES: auto T = std::make_unique<Base>();
 }

>From 21077adfefd5a255735cb7f6f0bb690a1e880e62 Mon Sep 17 00:00:00 2001
From: Arseniy Zaostrovnykh <necto.ne at gmail.com>
Date: Thu, 16 May 2024 09:27:25 +0200
Subject: [PATCH 3/3] Move the AST dump test to the conventional file

---
 clang/test/AST/ast-dump-expr.cpp      | 5 +++++
 clang/test/Misc/ast-source-ranges.cpp | 5 -----
 2 files changed, 5 insertions(+), 5 deletions(-)
 delete mode 100644 clang/test/Misc/ast-source-ranges.cpp

diff --git a/clang/test/AST/ast-dump-expr.cpp b/clang/test/AST/ast-dump-expr.cpp
index 69e65e22d61d0..9b29ab1bbb228 100644
--- a/clang/test/AST/ast-dump-expr.cpp
+++ b/clang/test/AST/ast-dump-expr.cpp
@@ -583,3 +583,8 @@ void NonADLCall3() {
   f(x);
 }
 } // namespace test_adl_call_three
+
+struct Sock {};
+void leakNewFn() { new struct Sock; }
+// CHECK: CXXNewExpr {{.*}} <col:20, col:31> 'struct Sock *'
+
diff --git a/clang/test/Misc/ast-source-ranges.cpp b/clang/test/Misc/ast-source-ranges.cpp
deleted file mode 100644
index 9c0ab9449a6f5..0000000000000
--- a/clang/test/Misc/ast-source-ranges.cpp
+++ /dev/null
@@ -1,5 +0,0 @@
-// RUN: %clang_cc1 -ast-dump %s  2>&1 | FileCheck %s
-
-struct Sock {};
-void leakNewFn() { new struct Sock; }
-// CHECK: CXXNewExpr {{.*}} <col:20, col:31> 'struct Sock *'



More information about the cfe-commits mailing list