From ba2e4fe4e7f79e49fcac54ea20f5b899dc687cfc Mon Sep 17 00:00:00 2001 From: Arseniy Zaostrovnykh Date: Thu, 16 May 2024 12:14:53 +0200 Subject: [PATCH] [clang] Fix CXXNewExpr end source location for 'new struct S' (#92266) Currently, `new struct S` fails to set any valid end source location because the token corresponding to `S` is consumed in `ParseClassSpecifier` and is not accessible in the `ParseDeclarationSpecifiers` that normally sets the end source location. Fixes #35300 --- .../test/clang-tidy/checkers/modernize/make-unique.cpp | 7 ++----- clang/lib/Parse/ParseDeclCXX.cpp | 1 + clang/test/AST/ast-dump-expr.cpp | 7 +++++++ 3 files changed, 10 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 7934c6e93ffbd3..fe512a8f3bf321 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(new struct Base); // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use std::make_unique instead - // CHECK-FIXES: auto T = std::unique_ptr(new struct Base); + // CHECK-FIXES: auto T = std::make_unique(); } diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp index 65ddebca49bc6d..32c4e923243a92 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/AST/ast-dump-expr.cpp b/clang/test/AST/ast-dump-expr.cpp index 4df5ba4276abd2..604868103dab8b 100644 --- a/clang/test/AST/ast-dump-expr.cpp +++ b/clang/test/AST/ast-dump-expr.cpp @@ -583,3 +583,10 @@ void NonADLCall3() { f(x); } } // namespace test_adl_call_three + +namespace GH35300 { +struct Sock {}; +void leakNewFn() { new struct Sock; } +// CHECK: CXXNewExpr {{.*}} 'struct Sock *' +} +