@@ -178,140 +178,140 @@ void PreferMemberInitializerCheck::check(
178
178
const FieldDecl *Field = nullptr ;
179
179
const Expr *InitValue = nullptr ;
180
180
std::tie (Field, InitValue) = isAssignmentToMemberOf (Class, S, Ctor);
181
- if (Field) {
182
- if (IsUseDefaultMemberInitEnabled && getLangOpts ().CPlusPlus11 &&
183
- Ctor->isDefaultConstructor () &&
184
- (getLangOpts ().CPlusPlus20 || !Field->isBitField ()) &&
185
- !Field->hasInClassInitializer () &&
186
- (!isa<RecordDecl>(Class->getDeclContext ()) ||
187
- !cast<RecordDecl>(Class->getDeclContext ())->isUnion ()) &&
188
- shouldBeDefaultMemberInitializer (InitValue)) {
189
-
190
- bool InvalidFix = false ;
191
- SourceLocation FieldEnd =
192
- Lexer::getLocForEndOfToken (Field->getSourceRange ().getEnd (), 0 ,
193
- *Result.SourceManager , getLangOpts ());
194
- InvalidFix |= FieldEnd.isInvalid () || FieldEnd.isMacroID ();
195
- SourceLocation SemiColonEnd;
196
- if (auto NextToken = Lexer::findNextToken (
197
- S->getEndLoc (), *Result.SourceManager , getLangOpts ()))
198
- SemiColonEnd = NextToken->getEndLoc ();
199
- else
200
- InvalidFix = true ;
201
- auto Diag =
202
- diag (S->getBeginLoc (), " %0 should be initialized in an in-class"
203
- " default member initializer" )
204
- << Field;
205
- if (InvalidFix)
181
+ if (!Field)
182
+ continue ;
183
+ const bool IsInDefaultMemberInitializer =
184
+ IsUseDefaultMemberInitEnabled && getLangOpts ().CPlusPlus11 &&
185
+ Ctor->isDefaultConstructor () &&
186
+ (getLangOpts ().CPlusPlus20 || !Field->isBitField ()) &&
187
+ !Field->hasInClassInitializer () &&
188
+ (!isa<RecordDecl>(Class->getDeclContext ()) ||
189
+ !cast<RecordDecl>(Class->getDeclContext ())->isUnion ()) &&
190
+ shouldBeDefaultMemberInitializer (InitValue);
191
+ if (IsInDefaultMemberInitializer) {
192
+ bool InvalidFix = false ;
193
+ SourceLocation FieldEnd =
194
+ Lexer::getLocForEndOfToken (Field->getSourceRange ().getEnd (), 0 ,
195
+ *Result.SourceManager , getLangOpts ());
196
+ InvalidFix |= FieldEnd.isInvalid () || FieldEnd.isMacroID ();
197
+ SourceLocation SemiColonEnd;
198
+ if (auto NextToken = Lexer::findNextToken (
199
+ S->getEndLoc (), *Result.SourceManager , getLangOpts ()))
200
+ SemiColonEnd = NextToken->getEndLoc ();
201
+ else
202
+ InvalidFix = true ;
203
+ auto Diag =
204
+ diag (S->getBeginLoc (), " %0 should be initialized in an in-class"
205
+ " default member initializer" )
206
+ << Field;
207
+ if (InvalidFix)
208
+ continue ;
209
+ CharSourceRange StmtRange =
210
+ CharSourceRange::getCharRange (S->getBeginLoc (), SemiColonEnd);
211
+
212
+ SmallString<128 > Insertion (
213
+ {UseAssignment ? " = " : " {" ,
214
+ Lexer::getSourceText (
215
+ CharSourceRange (InitValue->getSourceRange (), true ),
216
+ *Result.SourceManager , getLangOpts ()),
217
+ UseAssignment ? " " : " }" });
218
+
219
+ Diag << FixItHint::CreateInsertion (FieldEnd, Insertion)
220
+ << FixItHint::CreateRemoval (StmtRange);
221
+
222
+ } else {
223
+ StringRef InsertPrefix = " " ;
224
+ bool HasInitAlready = false ;
225
+ SourceLocation InsertPos;
226
+ SourceRange ReplaceRange;
227
+ bool AddComma = false ;
228
+ bool InvalidFix = false ;
229
+ unsigned Index = Field->getFieldIndex ();
230
+ const CXXCtorInitializer *LastInListInit = nullptr ;
231
+ for (const CXXCtorInitializer *Init : Ctor->inits ()) {
232
+ if (!Init->isWritten () || Init->isInClassMemberInitializer ())
206
233
continue ;
207
- CharSourceRange StmtRange =
208
- CharSourceRange::getCharRange (S->getBeginLoc (), SemiColonEnd);
209
-
210
- SmallString<128 > Insertion (
211
- {UseAssignment ? " = " : " {" ,
212
- Lexer::getSourceText (
213
- CharSourceRange (InitValue->getSourceRange (), true ),
214
- *Result.SourceManager , getLangOpts ()),
215
- UseAssignment ? " " : " }" });
216
-
217
- Diag << FixItHint::CreateInsertion (FieldEnd, Insertion)
218
- << FixItHint::CreateRemoval (StmtRange);
219
-
220
- } else {
221
- StringRef InsertPrefix = " " ;
222
- bool HasInitAlready = false ;
223
- SourceLocation InsertPos;
224
- SourceRange ReplaceRange;
225
- bool AddComma = false ;
226
- bool InvalidFix = false ;
227
- unsigned Index = Field->getFieldIndex ();
228
- const CXXCtorInitializer *LastInListInit = nullptr ;
229
- for (const CXXCtorInitializer *Init : Ctor->inits ()) {
230
- if (!Init->isWritten () || Init->isInClassMemberInitializer ())
231
- continue ;
232
- if (Init->getMember () == Field) {
233
- HasInitAlready = true ;
234
- if (isa<ImplicitValueInitExpr>(Init->getInit ()))
235
- InsertPos = Init->getRParenLoc ();
236
- else {
237
- ReplaceRange = Init->getInit ()->getSourceRange ();
238
- }
239
- break ;
240
- }
241
- if (Init->isMemberInitializer () &&
242
- Index < Init->getMember ()->getFieldIndex ()) {
243
- InsertPos = Init->getSourceLocation ();
244
- // There are initializers after the one we are inserting, so add a
245
- // comma after this insertion in order to not break anything.
246
- AddComma = true ;
247
- break ;
234
+ if (Init->getMember () == Field) {
235
+ HasInitAlready = true ;
236
+ if (isa<ImplicitValueInitExpr>(Init->getInit ()))
237
+ InsertPos = Init->getRParenLoc ();
238
+ else {
239
+ ReplaceRange = Init->getInit ()->getSourceRange ();
248
240
}
249
- LastInListInit = Init ;
241
+ break ;
250
242
}
251
- if (HasInitAlready) {
252
- if (InsertPos.isValid ())
253
- InvalidFix |= InsertPos.isMacroID ();
254
- else
255
- InvalidFix |= ReplaceRange.getBegin ().isMacroID () ||
256
- ReplaceRange.getEnd ().isMacroID ();
257
- } else {
258
- if (InsertPos.isInvalid ()) {
259
- if (LastInListInit) {
260
- InsertPos = Lexer::getLocForEndOfToken (
261
- LastInListInit->getRParenLoc (), 0 , *Result.SourceManager ,
262
- getLangOpts ());
263
- // Inserting after the last constructor initializer, so we need a
264
- // comma.
265
- InsertPrefix = " , " ;
266
- } else {
267
- InsertPos = Lexer::getLocForEndOfToken (
268
- Ctor->getTypeSourceInfo ()
269
- ->getTypeLoc ()
270
- .getAs <clang::FunctionTypeLoc>()
271
- .getLocalRangeEnd (),
272
- 0 , *Result.SourceManager , getLangOpts ());
273
-
274
- // If this is first time in the loop, there are no initializers so
275
- // `:` declares member initialization list. If this is a
276
- // subsequent pass then we have already inserted a `:` so continue
277
- // with a comma.
278
- InsertPrefix = FirstToCtorInits ? " : " : " , " ;
279
- }
280
- }
243
+ if (Init->isMemberInitializer () &&
244
+ Index < Init->getMember ()->getFieldIndex ()) {
245
+ InsertPos = Init->getSourceLocation ();
246
+ // There are initializers after the one we are inserting, so add a
247
+ // comma after this insertion in order to not break anything.
248
+ AddComma = true ;
249
+ break ;
250
+ }
251
+ LastInListInit = Init;
252
+ }
253
+ if (HasInitAlready) {
254
+ if (InsertPos.isValid ())
281
255
InvalidFix |= InsertPos.isMacroID ();
256
+ else
257
+ InvalidFix |= ReplaceRange.getBegin ().isMacroID () ||
258
+ ReplaceRange.getEnd ().isMacroID ();
259
+ } else {
260
+ if (InsertPos.isInvalid ()) {
261
+ if (LastInListInit) {
262
+ InsertPos = Lexer::getLocForEndOfToken (
263
+ LastInListInit->getRParenLoc (), 0 , *Result.SourceManager ,
264
+ getLangOpts ());
265
+ // Inserting after the last constructor initializer, so we need a
266
+ // comma.
267
+ InsertPrefix = " , " ;
268
+ } else {
269
+ InsertPos = Lexer::getLocForEndOfToken (
270
+ Ctor->getTypeSourceInfo ()
271
+ ->getTypeLoc ()
272
+ .getAs <clang::FunctionTypeLoc>()
273
+ .getLocalRangeEnd (),
274
+ 0 , *Result.SourceManager , getLangOpts ());
275
+
276
+ // If this is first time in the loop, there are no initializers so
277
+ // `:` declares member initialization list. If this is a
278
+ // subsequent pass then we have already inserted a `:` so continue
279
+ // with a comma.
280
+ InsertPrefix = FirstToCtorInits ? " : " : " , " ;
281
+ }
282
282
}
283
+ InvalidFix |= InsertPos.isMacroID ();
284
+ }
283
285
284
- SourceLocation SemiColonEnd;
285
- if (auto NextToken = Lexer::findNextToken (
286
- S->getEndLoc (), *Result.SourceManager , getLangOpts ()))
287
- SemiColonEnd = NextToken->getEndLoc ();
286
+ SourceLocation SemiColonEnd;
287
+ if (auto NextToken = Lexer::findNextToken (
288
+ S->getEndLoc (), *Result.SourceManager , getLangOpts ()))
289
+ SemiColonEnd = NextToken->getEndLoc ();
290
+ else
291
+ InvalidFix = true ;
292
+
293
+ auto Diag = diag (S->getBeginLoc (), " %0 should be initialized in a member"
294
+ " initializer of the constructor" )
295
+ << Field;
296
+ if (InvalidFix)
297
+ continue ;
298
+ StringRef NewInit = Lexer::getSourceText (
299
+ CharSourceRange (InitValue->getSourceRange (), true ),
300
+ *Result.SourceManager , getLangOpts ());
301
+ if (HasInitAlready) {
302
+ if (InsertPos.isValid ())
303
+ Diag << FixItHint::CreateInsertion (InsertPos, NewInit);
288
304
else
289
- InvalidFix = true ;
290
-
291
- auto Diag =
292
- diag (S->getBeginLoc (), " %0 should be initialized in a member"
293
- " initializer of the constructor" )
294
- << Field;
295
- if (InvalidFix)
296
- continue ;
297
- StringRef NewInit = Lexer::getSourceText (
298
- CharSourceRange (InitValue->getSourceRange (), true ),
299
- *Result.SourceManager , getLangOpts ());
300
- if (HasInitAlready) {
301
- if (InsertPos.isValid ())
302
- Diag << FixItHint::CreateInsertion (InsertPos, NewInit);
303
- else
304
- Diag << FixItHint::CreateReplacement (ReplaceRange, NewInit);
305
- } else {
306
- SmallString<128 > Insertion ({InsertPrefix, Field->getName (), " (" ,
307
- NewInit, AddComma ? " ), " : " )" });
308
- Diag << FixItHint::CreateInsertion (InsertPos, Insertion,
309
- FirstToCtorInits);
310
- FirstToCtorInits = areDiagsSelfContained ();
311
- }
312
- Diag << FixItHint::CreateRemoval (
313
- CharSourceRange::getCharRange (S->getBeginLoc (), SemiColonEnd));
305
+ Diag << FixItHint::CreateReplacement (ReplaceRange, NewInit);
306
+ } else {
307
+ SmallString<128 > Insertion ({InsertPrefix, Field->getName (), " (" ,
308
+ NewInit, AddComma ? " ), " : " )" });
309
+ Diag << FixItHint::CreateInsertion (InsertPos, Insertion,
310
+ FirstToCtorInits);
311
+ FirstToCtorInits = areDiagsSelfContained ();
314
312
}
313
+ Diag << FixItHint::CreateRemoval (
314
+ CharSourceRange::getCharRange (S->getBeginLoc (), SemiColonEnd));
315
315
}
316
316
}
317
317
}
0 commit comments