1 //===--- ProTypeMemberInitCheck.h - clang-tidy-------------------*- C++ -*-===// 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 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_PRO_TYPE_MEMBER_INIT_H 10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_PRO_TYPE_MEMBER_INIT_H 11 12 #include "../ClangTidyCheck.h" 13 14 namespace clang { 15 namespace tidy { 16 namespace cppcoreguidelines { 17 18 /// Implements C++ Core Guidelines Type.6. 19 /// 20 /// Checks that every user-provided constructor value-initializes all class 21 /// members and base classes that would have undefined behavior otherwise. Also 22 /// check that any record types without user-provided default constructors are 23 /// value-initialized where used. 24 /// 25 /// Members initialized through function calls in the body of the constructor 26 /// will result in false positives. 27 /// 28 /// For the user-facing documentation see: 29 /// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-pro-type-member-init.html 30 /// TODO: See if 'fixes' for false positives are optimized away by the compiler. 31 /// TODO: For classes with multiple constructors, make sure that we don't offer 32 /// multiple in-class initializer fixits for the same member. 33 class ProTypeMemberInitCheck : public ClangTidyCheck { 34 public: 35 ProTypeMemberInitCheck(StringRef Name, ClangTidyContext *Context); isLanguageVersionSupported(const LangOptions & LangOpts)36 bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { 37 return LangOpts.CPlusPlus; 38 } 39 void registerMatchers(ast_matchers::MatchFinder *Finder) override; 40 void check(const ast_matchers::MatchFinder::MatchResult &Result) override; 41 void storeOptions(ClangTidyOptions::OptionMap &Opts) override; 42 43 private: 44 // Checks Type.6 part 1: 45 // Issue a diagnostic for any constructor of a non-trivially-constructible 46 // type that does not initialize all member variables. 47 // 48 // To fix: Write a data member initializer, or mention it in the member 49 // initializer list. 50 void checkMissingMemberInitializer(ASTContext &Context, 51 const CXXRecordDecl &ClassDecl, 52 const CXXConstructorDecl *Ctor); 53 54 // A subtle side effect of Type.6 part 2: 55 // Make sure to initialize trivially constructible base classes. 56 void checkMissingBaseClassInitializer(const ASTContext &Context, 57 const CXXRecordDecl &ClassDecl, 58 const CXXConstructorDecl *Ctor); 59 60 // Checks Type.6 part 2: 61 // Issue a diagnostic when constructing an object of a trivially constructible 62 // type without () or {} to initialize its members. 63 // 64 // To fix: Add () or {}. 65 void checkUninitializedTrivialType(const ASTContext &Context, 66 const VarDecl *Var); 67 68 // Whether arrays need to be initialized or not. Default is false. 69 bool IgnoreArrays; 70 71 // Whether fix-its for initialization of fundamental type use assignment 72 // instead of brace initialization. Only effective in C++11 mode. Default is 73 // false. 74 bool UseAssignment; 75 }; 76 77 } // namespace cppcoreguidelines 78 } // namespace tidy 79 } // namespace clang 80 81 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_PRO_TYPE_MEMBER_INIT_H 82