Skip to content

Commit 3bb8eb3

Browse files
committed
Handle typedefs not at the start again
1 parent 2a944c4 commit 3bb8eb3

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,27 @@ void UseUsingCheck::check(const MatchFinder::MatchResult &Result) {
7575
// consecutive TypedefDecl nodes whose SourceRanges overlap. Each range starts
7676
// at the "typedef" and then continues *across* previous definitions through
7777
// the end of the current TypedefDecl definition.
78+
const Token TokenBeforeName =
79+
*Lexer::findPreviousToken(MatchedDecl.getLocation(), SM, LO,
80+
/*IncludeComments=*/false);
7881
const SourceRange RemovalRange = {
79-
Lexer::findPreviousToken(MatchedDecl.getLocation(), SM, LO,
80-
/*IncludeComments=*/true)
81-
->getEndLoc(),
82+
TokenBeforeName.getEndLoc(),
8283
Lexer::getLocForEndOfToken(MatchedDecl.getLocation(), 1, SM, LO)};
8384
if (NextTypedefStartsANewSequence) {
84-
diag(MatchedDecl.getBeginLoc(), UseUsingWarning)
85-
<< FixItHint::CreateReplacement(
86-
{MatchedDecl.getBeginLoc(),
87-
Lexer::getLocForEndOfToken(MatchedDecl.getBeginLoc(), 0, SM,
88-
LO)},
89-
("using " + MatchedDecl.getName() + " =").str())
90-
<< FixItHint::CreateRemoval(RemovalRange);
85+
auto Diag = diag(MatchedDecl.getBeginLoc(), UseUsingWarning)
86+
<< FixItHint::CreateInsertion(
87+
MatchedDecl.getBeginLoc(),
88+
("using " + MatchedDecl.getName() + " =").str())
89+
<< FixItHint::CreateRemoval(RemovalRange);
90+
91+
Token FirstToken;
92+
Lexer::getRawToken(MatchedDecl.getBeginLoc(), FirstToken, SM, LO);
93+
if (FirstToken.getRawIdentifier() == "typedef")
94+
Diag << FixItHint::CreateRemoval(FirstToken.getLocation());
95+
96+
if (TokenBeforeName.getRawIdentifier() == "typedef")
97+
Diag << FixItHint::CreateRemoval(TokenBeforeName.getLocation());
98+
9199
FirstTypedefName = MatchedDecl.getName();
92100
} else {
93101
diag(LastCommaOrSemi, UseUsingWarning)

clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,38 @@ typedef struct Foo Bap;
137137
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
138138
// CHECK-FIXES: using Bap = struct Foo;
139139

140+
struct Foo typedef Bap2;
141+
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
142+
// CHECK-FIXES: using Bap2 =struct Foo ;
143+
144+
Foo typedef Bap3;
145+
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
146+
// CHECK-FIXES: using Bap3 =Foo ;
147+
140148
typedef struct Unknown Baq;
141149
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
142150
// CHECK-FIXES: using Baq = struct Unknown;
143151

152+
struct Unknown2 typedef Baw;
153+
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
154+
// CHECK-FIXES: using Baw =struct Unknown2 ;
155+
156+
int typedef Bax;
157+
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
158+
// CHECK-FIXES: using Bax =int ;
159+
144160
typedef struct Q1 { int a; } S1;
145161
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
146162
// CHECK-FIXES: using S1 = struct Q1 { int a; };
147163
typedef struct { int b; } S2;
148164
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
149165
// CHECK-FIXES: using S2 = struct { int b; };
166+
struct Q2 { int c; } typedef S3;
167+
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
168+
// CHECK-FIXES: using S3 =struct Q2 { int c; } ;
169+
struct { int d; } typedef S4;
170+
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
171+
// CHECK-FIXES: using S4 =struct { int d; } ;
150172

151173
namespace my_space {
152174
class my_cclass {};

0 commit comments

Comments
 (0)