@@ -151,20 +151,16 @@ void PreferMemberInitializerCheck::check(
151
151
(!isa<RecordDecl>(Class->getDeclContext ()) ||
152
152
!cast<RecordDecl>(Class->getDeclContext ())->isUnion ()) &&
153
153
shouldBeDefaultMemberInitializer (InitValue)) {
154
- auto Diag =
155
- diag (S->getBeginLoc (), " %0 should be initialized in an in-class"
156
- " default member initializer" )
157
- << Field;
158
154
159
155
SourceLocation FieldEnd =
160
156
Lexer::getLocForEndOfToken (Field->getSourceRange ().getEnd (), 0 ,
161
157
*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 ? " " : " }" } );
168
164
169
165
SourceLocation SemiColonEnd =
170
166
Lexer::findNextToken (S->getEndLoc (), *Result.SourceManager ,
@@ -173,13 +169,12 @@ void PreferMemberInitializerCheck::check(
173
169
CharSourceRange StmtRange =
174
170
CharSourceRange::getCharRange (S->getBeginLoc (), SemiColonEnd);
175
171
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);
177
176
} 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;
183
178
bool AddComma = false ;
184
179
if (!Ctor->getNumCtorInitializers () && FirstToCtorInits) {
185
180
SourceLocation BodyPos = Ctor->getBody ()->getBeginLoc ();
@@ -193,13 +188,13 @@ void PreferMemberInitializerCheck::check(
193
188
InsertPos = Lexer::getLocForEndOfToken (
194
189
InsertPos, 0 , *Result.SourceManager , getLangOpts ());
195
190
196
- Diag << FixItHint::CreateInsertion (InsertPos, " : " ) ;
191
+ Insertion = " : " ;
197
192
} else {
198
193
bool Found = false ;
194
+ unsigned Index = Field->getFieldIndex ();
199
195
for (const auto *Init : Ctor->inits ()) {
200
196
if (Init->isMemberInitializer ()) {
201
- if (Result.SourceManager ->isBeforeInTranslationUnit (
202
- Field->getLocation (), Init->getMember ()->getLocation ())) {
197
+ if (Index < Init->getMember ()->getFieldIndex ()) {
203
198
InsertPos = Init->getSourceLocation ();
204
199
Found = true ;
205
200
break ;
@@ -213,19 +208,17 @@ void PreferMemberInitializerCheck::check(
213
208
(*Ctor->init_rbegin ())->getSourceRange ().getEnd (), 0 ,
214
209
*Result.SourceManager , getLangOpts ());
215
210
}
216
- Diag << FixItHint::CreateInsertion (InsertPos, " , " ) ;
211
+ Insertion = " , " ;
217
212
} else {
218
213
AddComma = true ;
219
214
}
220
215
}
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 ? " ), " : " )" });
229
222
230
223
SourceLocation SemiColonEnd =
231
224
Lexer::findNextToken (S->getEndLoc (), *Result.SourceManager ,
@@ -234,7 +227,12 @@ void PreferMemberInitializerCheck::check(
234
227
CharSourceRange StmtRange =
235
228
CharSourceRange::getCharRange (S->getBeginLoc (), SemiColonEnd);
236
229
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);
238
236
FirstToCtorInits = false ;
239
237
}
240
238
}
0 commit comments