Skip to content

Commit 557d2ad

Browse files
committed
[NFC] Refactor PreferMemberInitializerCheck
1 parent 3d6ca4b commit 557d2ad

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -151,20 +151,16 @@ void PreferMemberInitializerCheck::check(
151151
(!isa<RecordDecl>(Class->getDeclContext()) ||
152152
!cast<RecordDecl>(Class->getDeclContext())->isUnion()) &&
153153
shouldBeDefaultMemberInitializer(InitValue)) {
154-
auto Diag =
155-
diag(S->getBeginLoc(), "%0 should be initialized in an in-class"
156-
" default member initializer")
157-
<< Field;
158154

159155
SourceLocation FieldEnd =
160156
Lexer::getLocForEndOfToken(Field->getSourceRange().getEnd(), 0,
161157
*Result.SourceManager, getLangOpts());
162-
Diag << FixItHint::CreateInsertion(FieldEnd,
163-
UseAssignment ? " = " : "{")
164-
<< FixItHint::CreateInsertionFromRange(
165-
FieldEnd,
166-
CharSourceRange(InitValue->getSourceRange(), true))
167-
<< FixItHint::CreateInsertion(FieldEnd, UseAssignment ? "" : "}");
158+
SmallString<128> Insertion(
159+
{UseAssignment ? " = " : "{",
160+
Lexer::getSourceText(
161+
CharSourceRange(InitValue->getSourceRange(), true),
162+
*Result.SourceManager, getLangOpts()),
163+
UseAssignment ? "" : "}"});
168164

169165
SourceLocation SemiColonEnd =
170166
Lexer::findNextToken(S->getEndLoc(), *Result.SourceManager,
@@ -173,13 +169,12 @@ void PreferMemberInitializerCheck::check(
173169
CharSourceRange StmtRange =
174170
CharSourceRange::getCharRange(S->getBeginLoc(), SemiColonEnd);
175171

176-
Diag << FixItHint::CreateRemoval(StmtRange);
172+
diag(S->getBeginLoc(), "%0 should be initialized in an in-class"
173+
" default member initializer")
174+
<< Field << FixItHint::CreateInsertion(FieldEnd, Insertion)
175+
<< FixItHint::CreateRemoval(StmtRange);
177176
} else {
178-
auto Diag =
179-
diag(S->getBeginLoc(), "%0 should be initialized in a member"
180-
" initializer of the constructor")
181-
<< Field;
182-
177+
SmallString<128> Insertion;
183178
bool AddComma = false;
184179
if (!Ctor->getNumCtorInitializers() && FirstToCtorInits) {
185180
SourceLocation BodyPos = Ctor->getBody()->getBeginLoc();
@@ -193,13 +188,13 @@ void PreferMemberInitializerCheck::check(
193188
InsertPos = Lexer::getLocForEndOfToken(
194189
InsertPos, 0, *Result.SourceManager, getLangOpts());
195190

196-
Diag << FixItHint::CreateInsertion(InsertPos, " : ");
191+
Insertion = " : ";
197192
} else {
198193
bool Found = false;
194+
unsigned Index = Field->getFieldIndex();
199195
for (const auto *Init : Ctor->inits()) {
200196
if (Init->isMemberInitializer()) {
201-
if (Result.SourceManager->isBeforeInTranslationUnit(
202-
Field->getLocation(), Init->getMember()->getLocation())) {
197+
if (Index < Init->getMember()->getFieldIndex()) {
203198
InsertPos = Init->getSourceLocation();
204199
Found = true;
205200
break;
@@ -213,19 +208,17 @@ void PreferMemberInitializerCheck::check(
213208
(*Ctor->init_rbegin())->getSourceRange().getEnd(), 0,
214209
*Result.SourceManager, getLangOpts());
215210
}
216-
Diag << FixItHint::CreateInsertion(InsertPos, ", ");
211+
Insertion = ", ";
217212
} else {
218213
AddComma = true;
219214
}
220215
}
221-
Diag << FixItHint::CreateInsertion(InsertPos, Field->getName())
222-
<< FixItHint::CreateInsertion(InsertPos, "(")
223-
<< FixItHint::CreateInsertionFromRange(
224-
InsertPos,
225-
CharSourceRange(InitValue->getSourceRange(), true))
226-
<< FixItHint::CreateInsertion(InsertPos, ")");
227-
if (AddComma)
228-
Diag << FixItHint::CreateInsertion(InsertPos, ", ");
216+
Insertion.append(
217+
{Field->getName(), "(",
218+
Lexer::getSourceText(
219+
CharSourceRange(InitValue->getSourceRange(), true),
220+
*Result.SourceManager, getLangOpts()),
221+
AddComma ? "), " : ")"});
229222

230223
SourceLocation SemiColonEnd =
231224
Lexer::findNextToken(S->getEndLoc(), *Result.SourceManager,
@@ -234,7 +227,12 @@ void PreferMemberInitializerCheck::check(
234227
CharSourceRange StmtRange =
235228
CharSourceRange::getCharRange(S->getBeginLoc(), SemiColonEnd);
236229

237-
Diag << FixItHint::CreateRemoval(StmtRange);
230+
diag(S->getBeginLoc(), "%0 should be initialized in a member"
231+
" initializer of the constructor")
232+
<< Field
233+
<< FixItHint::CreateInsertion(InsertPos, Insertion,
234+
FirstToCtorInits)
235+
<< FixItHint::CreateRemoval(StmtRange);
238236
FirstToCtorInits = false;
239237
}
240238
}

0 commit comments

Comments
 (0)