| 1 | //===--- ProTypeMemberInitCheck.cpp - clang-tidy---------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "ProTypeMemberInitCheck.h" |
| 10 | #include "../utils/LexerUtils.h" |
| 11 | #include "../utils/Matchers.h" |
| 12 | #include "../utils/TypeTraits.h" |
| 13 | #include "clang/AST/ASTContext.h" |
| 14 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 15 | #include "clang/Lex/Lexer.h" |
| 16 | #include "llvm/ADT/SmallPtrSet.h" |
| 17 | |
| 18 | using namespace clang::ast_matchers; |
| 19 | using namespace clang::tidy::matchers; |
| 20 | using llvm::SmallPtrSet; |
| 21 | using llvm::SmallPtrSetImpl; |
| 22 | |
| 23 | namespace clang::tidy::cppcoreguidelines { |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | AST_MATCHER(CXXRecordDecl, hasDefaultConstructor) { |
| 28 | return Node.hasDefaultConstructor(); |
| 29 | } |
| 30 | |
| 31 | // Iterate over all the fields in a record type, both direct and indirect (e.g. |
| 32 | // if the record contains an anonymous struct). |
| 33 | template <typename T, typename Func> |
| 34 | void forEachField(const RecordDecl &Record, const T &Fields, const Func &Fn) { |
| 35 | for (const FieldDecl *F : Fields) { |
| 36 | if (F->isAnonymousStructOrUnion()) { |
| 37 | if (const CXXRecordDecl *R = F->getType()->getAsCXXRecordDecl()) |
| 38 | forEachField(*R, R->fields(), Fn); |
| 39 | } else { |
| 40 | Fn(F); |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | template <typename T, typename Func> |
| 46 | void forEachFieldWithFilter(const RecordDecl &Record, const T &Fields, |
| 47 | bool &AnyMemberHasInitPerUnion, const Func &Fn) { |
| 48 | for (const FieldDecl *F : Fields) { |
| 49 | if (F->isAnonymousStructOrUnion()) { |
| 50 | if (const CXXRecordDecl *R = F->getType()->getAsCXXRecordDecl()) { |
| 51 | AnyMemberHasInitPerUnion = false; |
| 52 | forEachFieldWithFilter(*R, R->fields(), AnyMemberHasInitPerUnion, Fn); |
| 53 | } |
| 54 | } else { |
| 55 | Fn(F); |
| 56 | } |
| 57 | if (Record.isUnion() && AnyMemberHasInitPerUnion) |
| 58 | break; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void removeFieldInitialized(const FieldDecl *M, |
| 63 | SmallPtrSetImpl<const FieldDecl *> &FieldDecls) { |
| 64 | const RecordDecl *R = M->getParent(); |
| 65 | if (R && R->isUnion()) { |
| 66 | // Erase all members in a union if any member of it is initialized. |
| 67 | for (const auto *F : R->fields()) |
| 68 | FieldDecls.erase(Ptr: F); |
| 69 | } else |
| 70 | FieldDecls.erase(Ptr: M); |
| 71 | } |
| 72 | |
| 73 | void removeFieldsInitializedInBody( |
| 74 | const Stmt &Stmt, ASTContext &Context, |
| 75 | SmallPtrSetImpl<const FieldDecl *> &FieldDecls) { |
| 76 | auto Matches = |
| 77 | match(Matcher: findAll(Matcher: binaryOperator( |
| 78 | hasOperatorName(Name: "=" ), |
| 79 | hasLHS(InnerMatcher: memberExpr(member(InnerMatcher: fieldDecl().bind(ID: "fieldDecl" )))))), |
| 80 | Node: Stmt, Context); |
| 81 | for (const auto &Match : Matches) |
| 82 | removeFieldInitialized(M: Match.getNodeAs<FieldDecl>(ID: "fieldDecl" ), FieldDecls); |
| 83 | } |
| 84 | |
| 85 | StringRef getName(const FieldDecl *Field) { return Field->getName(); } |
| 86 | |
| 87 | StringRef getName(const RecordDecl *Record) { |
| 88 | // Get the typedef name if this is a C-style anonymous struct and typedef. |
| 89 | if (const TypedefNameDecl *Typedef = Record->getTypedefNameForAnonDecl()) |
| 90 | return Typedef->getName(); |
| 91 | return Record->getName(); |
| 92 | } |
| 93 | |
| 94 | // Creates comma separated list of decls requiring initialization in order of |
| 95 | // declaration. |
| 96 | template <typename R, typename T> |
| 97 | std::string |
| 98 | toCommaSeparatedString(const R &OrderedDecls, |
| 99 | const SmallPtrSetImpl<const T *> &DeclsToInit) { |
| 100 | SmallVector<StringRef, 16> Names; |
| 101 | for (const T *Decl : OrderedDecls) { |
| 102 | if (DeclsToInit.count(Decl)) |
| 103 | Names.emplace_back(getName(Decl)); |
| 104 | } |
| 105 | return llvm::join(Begin: Names.begin(), End: Names.end(), Separator: ", " ); |
| 106 | } |
| 107 | |
| 108 | SourceLocation getLocationForEndOfToken(const ASTContext &Context, |
| 109 | SourceLocation Location) { |
| 110 | return Lexer::getLocForEndOfToken(Loc: Location, Offset: 0, SM: Context.getSourceManager(), |
| 111 | LangOpts: Context.getLangOpts()); |
| 112 | } |
| 113 | |
| 114 | // There are 3 kinds of insertion placements: |
| 115 | enum class InitializerPlacement { |
| 116 | // 1. The fields are inserted after an existing CXXCtorInitializer stored in |
| 117 | // Where. This will be the case whenever there is a written initializer before |
| 118 | // the fields available. |
| 119 | After, |
| 120 | |
| 121 | // 2. The fields are inserted before the first existing initializer stored in |
| 122 | // Where. |
| 123 | Before, |
| 124 | |
| 125 | // 3. There are no written initializers and the fields will be inserted before |
| 126 | // the constructor's body creating a new initializer list including the ':'. |
| 127 | New |
| 128 | }; |
| 129 | |
| 130 | // An InitializerInsertion contains a list of fields and/or base classes to |
| 131 | // insert into the initializer list of a constructor. We use this to ensure |
| 132 | // proper absolute ordering according to the class declaration relative to the |
| 133 | // (perhaps improper) ordering in the existing initializer list, if any. |
| 134 | struct InitializerInsertion { |
| 135 | InitializerInsertion(InitializerPlacement Placement, |
| 136 | const CXXCtorInitializer *Where) |
| 137 | : Placement(Placement), Where(Where) {} |
| 138 | |
| 139 | SourceLocation getLocation(const ASTContext &Context, |
| 140 | const CXXConstructorDecl &Constructor) const { |
| 141 | assert((Where != nullptr || Placement == InitializerPlacement::New) && |
| 142 | "Location should be relative to an existing initializer or this " |
| 143 | "insertion represents a new initializer list." ); |
| 144 | SourceLocation Location; |
| 145 | switch (Placement) { |
| 146 | case InitializerPlacement::New: |
| 147 | Location = utils::lexer::getPreviousToken( |
| 148 | Location: Constructor.getBody()->getBeginLoc(), |
| 149 | SM: Context.getSourceManager(), LangOpts: Context.getLangOpts()) |
| 150 | .getLocation(); |
| 151 | break; |
| 152 | case InitializerPlacement::Before: |
| 153 | Location = utils::lexer::getPreviousToken( |
| 154 | Location: Where->getSourceRange().getBegin(), |
| 155 | SM: Context.getSourceManager(), LangOpts: Context.getLangOpts()) |
| 156 | .getLocation(); |
| 157 | break; |
| 158 | case InitializerPlacement::After: |
| 159 | Location = Where->getRParenLoc(); |
| 160 | break; |
| 161 | } |
| 162 | return getLocationForEndOfToken(Context, Location); |
| 163 | } |
| 164 | |
| 165 | std::string codeToInsert() const { |
| 166 | assert(!Initializers.empty() && "No initializers to insert" ); |
| 167 | std::string Code; |
| 168 | llvm::raw_string_ostream Stream(Code); |
| 169 | std::string Joined = |
| 170 | llvm::join(Begin: Initializers.begin(), End: Initializers.end(), Separator: "(), " ); |
| 171 | switch (Placement) { |
| 172 | case InitializerPlacement::New: |
| 173 | Stream << " : " << Joined << "()" ; |
| 174 | break; |
| 175 | case InitializerPlacement::Before: |
| 176 | Stream << " " << Joined << "()," ; |
| 177 | break; |
| 178 | case InitializerPlacement::After: |
| 179 | Stream << ", " << Joined << "()" ; |
| 180 | break; |
| 181 | } |
| 182 | return Stream.str(); |
| 183 | } |
| 184 | |
| 185 | InitializerPlacement Placement; |
| 186 | const CXXCtorInitializer *Where; |
| 187 | SmallVector<std::string, 4> Initializers; |
| 188 | }; |
| 189 | |
| 190 | // Convenience utility to get a RecordDecl from a QualType. |
| 191 | const RecordDecl *getCanonicalRecordDecl(const QualType &Type) { |
| 192 | if (const auto *RT = Type.getCanonicalType()->getAs<RecordType>()) |
| 193 | return RT->getDecl(); |
| 194 | return nullptr; |
| 195 | } |
| 196 | |
| 197 | template <typename R, typename T> |
| 198 | SmallVector<InitializerInsertion, 16> |
| 199 | computeInsertions(const CXXConstructorDecl::init_const_range &Inits, |
| 200 | const R &OrderedDecls, |
| 201 | const SmallPtrSetImpl<const T *> &DeclsToInit) { |
| 202 | SmallVector<InitializerInsertion, 16> Insertions; |
| 203 | Insertions.emplace_back(Args: InitializerPlacement::New, Args: nullptr); |
| 204 | |
| 205 | typename R::const_iterator Decl = std::begin(OrderedDecls); |
| 206 | for (const CXXCtorInitializer *Init : Inits) { |
| 207 | if (Init->isWritten()) { |
| 208 | if (Insertions.size() == 1) |
| 209 | Insertions.emplace_back(Args: InitializerPlacement::Before, Args&: Init); |
| 210 | |
| 211 | // Gets either the field or base class being initialized by the provided |
| 212 | // initializer. |
| 213 | const auto *InitDecl = |
| 214 | Init->isAnyMemberInitializer() |
| 215 | ? static_cast<const NamedDecl *>(Init->getAnyMember()) |
| 216 | : Init->getBaseClass()->getAsCXXRecordDecl(); |
| 217 | |
| 218 | // Add all fields between current field up until the next initializer. |
| 219 | for (; Decl != std::end(OrderedDecls) && *Decl != InitDecl; ++Decl) { |
| 220 | if (const auto *D = dyn_cast<T>(*Decl)) { |
| 221 | if (DeclsToInit.contains(D)) |
| 222 | Insertions.back().Initializers.emplace_back(getName(D)); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | Insertions.emplace_back(Args: InitializerPlacement::After, Args&: Init); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | // Add remaining decls that require initialization. |
| 231 | for (; Decl != std::end(OrderedDecls); ++Decl) { |
| 232 | if (const auto *D = dyn_cast<T>(*Decl)) { |
| 233 | if (DeclsToInit.contains(D)) |
| 234 | Insertions.back().Initializers.emplace_back(getName(D)); |
| 235 | } |
| 236 | } |
| 237 | return Insertions; |
| 238 | } |
| 239 | |
| 240 | // Gets the list of bases and members that could possibly be initialized, in |
| 241 | // order as they appear in the class declaration. |
| 242 | void getInitializationsInOrder(const CXXRecordDecl &ClassDecl, |
| 243 | SmallVectorImpl<const NamedDecl *> &Decls) { |
| 244 | Decls.clear(); |
| 245 | for (const auto &Base : ClassDecl.bases()) { |
| 246 | // Decl may be null if the base class is a template parameter. |
| 247 | if (const NamedDecl *Decl = getCanonicalRecordDecl(Type: Base.getType())) { |
| 248 | Decls.emplace_back(Args&: Decl); |
| 249 | } |
| 250 | } |
| 251 | forEachField(ClassDecl, ClassDecl.fields(), |
| 252 | [&](const FieldDecl *F) { Decls.push_back(F); }); |
| 253 | } |
| 254 | |
| 255 | template <typename T> |
| 256 | void fixInitializerList(const ASTContext &Context, DiagnosticBuilder &Diag, |
| 257 | const CXXConstructorDecl *Ctor, |
| 258 | const SmallPtrSetImpl<const T *> &DeclsToInit) { |
| 259 | // Do not propose fixes in macros since we cannot place them correctly. |
| 260 | if (Ctor->getBeginLoc().isMacroID()) |
| 261 | return; |
| 262 | |
| 263 | SmallVector<const NamedDecl *, 16> OrderedDecls; |
| 264 | getInitializationsInOrder(*Ctor->getParent(), OrderedDecls); |
| 265 | |
| 266 | for (const auto &Insertion : |
| 267 | computeInsertions(Ctor->inits(), OrderedDecls, DeclsToInit)) { |
| 268 | if (!Insertion.Initializers.empty()) |
| 269 | Diag << FixItHint::CreateInsertion(InsertionLoc: Insertion.getLocation(Context, *Ctor), |
| 270 | Code: Insertion.codeToInsert()); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | } // anonymous namespace |
| 275 | |
| 276 | ProTypeMemberInitCheck::ProTypeMemberInitCheck(StringRef Name, |
| 277 | ClangTidyContext *Context) |
| 278 | : ClangTidyCheck(Name, Context), |
| 279 | IgnoreArrays(Options.get(LocalName: "IgnoreArrays" , Default: false)), |
| 280 | UseAssignment(Options.get(LocalName: "UseAssignment" , Default: false)) {} |
| 281 | |
| 282 | void ProTypeMemberInitCheck::registerMatchers(MatchFinder *Finder) { |
| 283 | auto IsUserProvidedNonDelegatingConstructor = |
| 284 | allOf(isUserProvided(), unless(isInstantiated()), |
| 285 | unless(isDelegatingConstructor()), |
| 286 | ofClass(InnerMatcher: cxxRecordDecl().bind(ID: "parent" )), |
| 287 | unless(hasAnyConstructorInitializer(InnerMatcher: cxxCtorInitializer( |
| 288 | isWritten(), unless(isMemberInitializer()), |
| 289 | hasTypeLoc(Inner: loc( |
| 290 | InnerMatcher: qualType(hasDeclaration(InnerMatcher: equalsBoundNode(ID: "parent" ))))))))); |
| 291 | |
| 292 | auto IsNonTrivialDefaultConstructor = allOf( |
| 293 | isDefaultConstructor(), unless(isUserProvided()), |
| 294 | hasParent(cxxRecordDecl(unless(isTriviallyDefaultConstructible())))); |
| 295 | Finder->addMatcher( |
| 296 | NodeMatch: cxxConstructorDecl(isDefinition(), |
| 297 | anyOf(IsUserProvidedNonDelegatingConstructor, |
| 298 | IsNonTrivialDefaultConstructor)) |
| 299 | .bind(ID: "ctor" ), |
| 300 | Action: this); |
| 301 | |
| 302 | // Match classes with a default constructor that is defaulted or is not in the |
| 303 | // AST. |
| 304 | Finder->addMatcher( |
| 305 | NodeMatch: cxxRecordDecl( |
| 306 | isDefinition(), unless(isInstantiated()), hasDefaultConstructor(), |
| 307 | anyOf(has(cxxConstructorDecl(isDefaultConstructor(), isDefaulted(), |
| 308 | unless(isImplicit()))), |
| 309 | unless(has(cxxConstructorDecl()))), |
| 310 | unless(isTriviallyDefaultConstructible())) |
| 311 | .bind(ID: "record" ), |
| 312 | Action: this); |
| 313 | |
| 314 | auto HasDefaultConstructor = hasInitializer( |
| 315 | InnerMatcher: cxxConstructExpr(unless(requiresZeroInitialization()), |
| 316 | hasDeclaration(InnerMatcher: cxxConstructorDecl( |
| 317 | isDefaultConstructor(), unless(isUserProvided()))))); |
| 318 | Finder->addMatcher( |
| 319 | NodeMatch: varDecl(isDefinition(), HasDefaultConstructor, |
| 320 | hasAutomaticStorageDuration(), |
| 321 | hasType(InnerMatcher: recordDecl(has(fieldDecl()), |
| 322 | isTriviallyDefaultConstructible()))) |
| 323 | .bind(ID: "var" ), |
| 324 | Action: this); |
| 325 | } |
| 326 | |
| 327 | void ProTypeMemberInitCheck::check(const MatchFinder::MatchResult &Result) { |
| 328 | if (const auto *Ctor = Result.Nodes.getNodeAs<CXXConstructorDecl>(ID: "ctor" )) { |
| 329 | // Skip declarations delayed by late template parsing without a body. |
| 330 | if (!Ctor->getBody()) |
| 331 | return; |
| 332 | // Skip out-of-band explicitly defaulted special member functions |
| 333 | // (except the default constructor). |
| 334 | if (Ctor->isExplicitlyDefaulted() && !Ctor->isDefaultConstructor()) |
| 335 | return; |
| 336 | checkMissingMemberInitializer(Context&: *Result.Context, ClassDecl: *Ctor->getParent(), Ctor); |
| 337 | checkMissingBaseClassInitializer(Context: *Result.Context, ClassDecl: *Ctor->getParent(), Ctor); |
| 338 | } else if (const auto *Record = |
| 339 | Result.Nodes.getNodeAs<CXXRecordDecl>(ID: "record" )) { |
| 340 | assert(Record->hasDefaultConstructor() && |
| 341 | "Matched record should have a default constructor" ); |
| 342 | checkMissingMemberInitializer(Context&: *Result.Context, ClassDecl: *Record, Ctor: nullptr); |
| 343 | checkMissingBaseClassInitializer(Context: *Result.Context, ClassDecl: *Record, Ctor: nullptr); |
| 344 | } else if (const auto *Var = Result.Nodes.getNodeAs<VarDecl>(ID: "var" )) { |
| 345 | checkUninitializedTrivialType(Context: *Result.Context, Var); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | void ProTypeMemberInitCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { |
| 350 | Options.store(Options&: Opts, LocalName: "IgnoreArrays" , Value: IgnoreArrays); |
| 351 | Options.store(Options&: Opts, LocalName: "UseAssignment" , Value: UseAssignment); |
| 352 | } |
| 353 | |
| 354 | // FIXME: Copied from clang/lib/Sema/SemaDeclCXX.cpp. |
| 355 | static bool isIncompleteOrZeroLengthArrayType(ASTContext &Context, QualType T) { |
| 356 | if (T->isIncompleteArrayType()) |
| 357 | return true; |
| 358 | |
| 359 | while (const ConstantArrayType *ArrayT = Context.getAsConstantArrayType(T)) { |
| 360 | if (!ArrayT->getSize()) |
| 361 | return true; |
| 362 | |
| 363 | T = ArrayT->getElementType(); |
| 364 | } |
| 365 | |
| 366 | return false; |
| 367 | } |
| 368 | |
| 369 | static bool isEmpty(ASTContext &Context, const QualType &Type) { |
| 370 | if (const CXXRecordDecl *ClassDecl = Type->getAsCXXRecordDecl()) { |
| 371 | return ClassDecl->isEmpty(); |
| 372 | } |
| 373 | return isIncompleteOrZeroLengthArrayType(Context, T: Type); |
| 374 | } |
| 375 | |
| 376 | static const char *getInitializer(QualType QT, bool UseAssignment) { |
| 377 | const char *DefaultInitializer = "{}" ; |
| 378 | if (!UseAssignment) |
| 379 | return DefaultInitializer; |
| 380 | |
| 381 | if (QT->isPointerType()) |
| 382 | return " = nullptr" ; |
| 383 | |
| 384 | const auto *BT = dyn_cast<BuiltinType>(Val: QT.getCanonicalType().getTypePtr()); |
| 385 | if (!BT) |
| 386 | return DefaultInitializer; |
| 387 | |
| 388 | switch (BT->getKind()) { |
| 389 | case BuiltinType::Bool: |
| 390 | return " = false" ; |
| 391 | case BuiltinType::Float: |
| 392 | return " = 0.0F" ; |
| 393 | case BuiltinType::Double: |
| 394 | return " = 0.0" ; |
| 395 | case BuiltinType::LongDouble: |
| 396 | return " = 0.0L" ; |
| 397 | case BuiltinType::SChar: |
| 398 | case BuiltinType::Char_S: |
| 399 | case BuiltinType::WChar_S: |
| 400 | case BuiltinType::Char16: |
| 401 | case BuiltinType::Char32: |
| 402 | case BuiltinType::Short: |
| 403 | case BuiltinType::Int: |
| 404 | return " = 0" ; |
| 405 | case BuiltinType::UChar: |
| 406 | case BuiltinType::Char_U: |
| 407 | case BuiltinType::WChar_U: |
| 408 | case BuiltinType::UShort: |
| 409 | case BuiltinType::UInt: |
| 410 | return " = 0U" ; |
| 411 | case BuiltinType::Long: |
| 412 | return " = 0L" ; |
| 413 | case BuiltinType::ULong: |
| 414 | return " = 0UL" ; |
| 415 | case BuiltinType::LongLong: |
| 416 | return " = 0LL" ; |
| 417 | case BuiltinType::ULongLong: |
| 418 | return " = 0ULL" ; |
| 419 | |
| 420 | default: |
| 421 | return DefaultInitializer; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | void ProTypeMemberInitCheck::checkMissingMemberInitializer( |
| 426 | ASTContext &Context, const CXXRecordDecl &ClassDecl, |
| 427 | const CXXConstructorDecl *Ctor) { |
| 428 | bool IsUnion = ClassDecl.isUnion(); |
| 429 | |
| 430 | if (IsUnion && ClassDecl.hasInClassInitializer()) |
| 431 | return; |
| 432 | |
| 433 | // Gather all fields (direct and indirect) that need to be initialized. |
| 434 | SmallPtrSet<const FieldDecl *, 16> FieldsToInit; |
| 435 | bool AnyMemberHasInitPerUnion = false; |
| 436 | forEachFieldWithFilter( |
| 437 | ClassDecl, ClassDecl.fields(), AnyMemberHasInitPerUnion, |
| 438 | [&](const FieldDecl *F) { |
| 439 | if (IgnoreArrays && F->getType()->isArrayType()) |
| 440 | return; |
| 441 | if (F->hasInClassInitializer() && F->getParent()->isUnion()) { |
| 442 | AnyMemberHasInitPerUnion = true; |
| 443 | removeFieldInitialized(M: F, FieldDecls&: FieldsToInit); |
| 444 | } |
| 445 | if (!F->hasInClassInitializer() && |
| 446 | utils::type_traits::isTriviallyDefaultConstructible(Type: F->getType(), |
| 447 | Context) && |
| 448 | !isEmpty(Context, F->getType()) && !F->isUnnamedBitField() && |
| 449 | !AnyMemberHasInitPerUnion) |
| 450 | FieldsToInit.insert(Ptr: F); |
| 451 | }); |
| 452 | if (FieldsToInit.empty()) |
| 453 | return; |
| 454 | |
| 455 | if (Ctor) { |
| 456 | for (const CXXCtorInitializer *Init : Ctor->inits()) { |
| 457 | // Remove any fields that were explicitly written in the initializer list |
| 458 | // or in-class. |
| 459 | if (Init->isAnyMemberInitializer() && Init->isWritten()) { |
| 460 | if (IsUnion) |
| 461 | return; // We can only initialize one member of a union. |
| 462 | removeFieldInitialized(M: Init->getAnyMember(), FieldDecls&: FieldsToInit); |
| 463 | } |
| 464 | } |
| 465 | removeFieldsInitializedInBody(Stmt: *Ctor->getBody(), Context, FieldDecls&: FieldsToInit); |
| 466 | } |
| 467 | |
| 468 | // Collect all fields in order, both direct fields and indirect fields from |
| 469 | // anonymous record types. |
| 470 | SmallVector<const FieldDecl *, 16> OrderedFields; |
| 471 | forEachField(ClassDecl, ClassDecl.fields(), |
| 472 | [&](const FieldDecl *F) { OrderedFields.push_back(Elt: F); }); |
| 473 | |
| 474 | // Collect all the fields we need to initialize, including indirect fields. |
| 475 | // It only includes fields that have not been fixed |
| 476 | SmallPtrSet<const FieldDecl *, 16> AllFieldsToInit; |
| 477 | forEachField(ClassDecl, FieldsToInit, [&](const FieldDecl *F) { |
| 478 | if (HasRecordClassMemberSet.insert(V: F).second) |
| 479 | AllFieldsToInit.insert(Ptr: F); |
| 480 | }); |
| 481 | if (FieldsToInit.empty()) |
| 482 | return; |
| 483 | |
| 484 | DiagnosticBuilder Diag = |
| 485 | diag(Ctor ? Ctor->getBeginLoc() : ClassDecl.getLocation(), |
| 486 | "%select{|union }0constructor %select{does not|should}0 initialize " |
| 487 | "%select{|one of }0these fields: %1" ) |
| 488 | << IsUnion << toCommaSeparatedString(OrderedDecls: OrderedFields, DeclsToInit: FieldsToInit); |
| 489 | |
| 490 | if (AllFieldsToInit.empty()) |
| 491 | return; |
| 492 | |
| 493 | // Do not propose fixes for constructors in macros since we cannot place them |
| 494 | // correctly. |
| 495 | if (Ctor && Ctor->getBeginLoc().isMacroID()) |
| 496 | return; |
| 497 | |
| 498 | // Collect all fields but only suggest a fix for the first member of unions, |
| 499 | // as initializing more than one union member is an error. |
| 500 | SmallPtrSet<const FieldDecl *, 16> FieldsToFix; |
| 501 | AnyMemberHasInitPerUnion = false; |
| 502 | forEachFieldWithFilter(ClassDecl, ClassDecl.fields(), |
| 503 | AnyMemberHasInitPerUnion, [&](const FieldDecl *F) { |
| 504 | if (!FieldsToInit.count(Ptr: F)) |
| 505 | return; |
| 506 | // Don't suggest fixes for enums because we don't |
| 507 | // know a good default. Don't suggest fixes for |
| 508 | // bitfields because in-class initialization is not |
| 509 | // possible until C++20. |
| 510 | if (F->getType()->isEnumeralType() || |
| 511 | (!getLangOpts().CPlusPlus20 && F->isBitField())) |
| 512 | return; |
| 513 | FieldsToFix.insert(Ptr: F); |
| 514 | AnyMemberHasInitPerUnion = true; |
| 515 | }); |
| 516 | if (FieldsToFix.empty()) |
| 517 | return; |
| 518 | |
| 519 | // Use in-class initialization if possible. |
| 520 | if (Context.getLangOpts().CPlusPlus11) { |
| 521 | for (const FieldDecl *Field : FieldsToFix) { |
| 522 | Diag << FixItHint::CreateInsertion( |
| 523 | InsertionLoc: getLocationForEndOfToken(Context, Location: Field->getSourceRange().getEnd()), |
| 524 | Code: getInitializer(Field->getType(), UseAssignment)); |
| 525 | } |
| 526 | } else if (Ctor) { |
| 527 | // Otherwise, rewrite the constructor's initializer list. |
| 528 | fixInitializerList(Context, Diag, Ctor, DeclsToInit: FieldsToFix); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | void ProTypeMemberInitCheck::checkMissingBaseClassInitializer( |
| 533 | const ASTContext &Context, const CXXRecordDecl &ClassDecl, |
| 534 | const CXXConstructorDecl *Ctor) { |
| 535 | |
| 536 | // Gather any base classes that need to be initialized. |
| 537 | SmallVector<const RecordDecl *, 4> AllBases; |
| 538 | SmallPtrSet<const RecordDecl *, 4> BasesToInit; |
| 539 | for (const CXXBaseSpecifier &Base : ClassDecl.bases()) { |
| 540 | if (const auto *BaseClassDecl = getCanonicalRecordDecl(Type: Base.getType())) { |
| 541 | AllBases.emplace_back(Args&: BaseClassDecl); |
| 542 | if (!BaseClassDecl->field_empty() && |
| 543 | utils::type_traits::isTriviallyDefaultConstructible(Type: Base.getType(), |
| 544 | Context)) |
| 545 | BasesToInit.insert(Ptr: BaseClassDecl); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | if (BasesToInit.empty()) |
| 550 | return; |
| 551 | |
| 552 | // Remove any bases that were explicitly written in the initializer list. |
| 553 | if (Ctor) { |
| 554 | if (Ctor->isImplicit()) |
| 555 | return; |
| 556 | |
| 557 | for (const CXXCtorInitializer *Init : Ctor->inits()) { |
| 558 | if (Init->isBaseInitializer() && Init->isWritten()) |
| 559 | BasesToInit.erase(Init->getBaseClass()->getAsCXXRecordDecl()); |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | if (BasesToInit.empty()) |
| 564 | return; |
| 565 | |
| 566 | DiagnosticBuilder Diag = |
| 567 | diag(Ctor ? Ctor->getBeginLoc() : ClassDecl.getLocation(), |
| 568 | "constructor does not initialize these bases: %0" ) |
| 569 | << toCommaSeparatedString(OrderedDecls: AllBases, DeclsToInit: BasesToInit); |
| 570 | |
| 571 | if (Ctor) |
| 572 | fixInitializerList(Context, Diag, Ctor, DeclsToInit: BasesToInit); |
| 573 | } |
| 574 | |
| 575 | void ProTypeMemberInitCheck::checkUninitializedTrivialType( |
| 576 | const ASTContext &Context, const VarDecl *Var) { |
| 577 | DiagnosticBuilder Diag = |
| 578 | diag(Var->getBeginLoc(), "uninitialized record type: %0" ) << Var; |
| 579 | |
| 580 | Diag << FixItHint::CreateInsertion( |
| 581 | InsertionLoc: getLocationForEndOfToken(Context, Location: Var->getSourceRange().getEnd()), |
| 582 | Code: Context.getLangOpts().CPlusPlus11 ? "{}" : " = {}" ); |
| 583 | } |
| 584 | |
| 585 | } // namespace clang::tidy::cppcoreguidelines |
| 586 | |