aboutsummaryrefslogtreecommitdiff
path: root/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.h
blob: 807acfe3bb2207d8fcd6892c443d50dca03fb669 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//===--- ProTypeMemberInitCheck.h - clang-tidy-------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_PRO_TYPE_MEMBER_INIT_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_PRO_TYPE_MEMBER_INIT_H

#include "../ClangTidy.h"

namespace clang {
namespace tidy {
namespace cppcoreguidelines {

/// \brief Implements C++ Core Guidelines Type.6.
///
/// Checks that every user-provided constructor value-initializes all class
/// members and base classes that would have undefined behavior otherwise. Also
/// check that any record types without user-provided default constructors are
/// value-initialized where used.
///
/// Members initialized through function calls in the body of the constructor
/// will result in false positives.
///
/// For the user-facing documentation see:
/// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-pro-type-member-init.html
/// TODO: See if 'fixes' for false positives are optimized away by the compiler.
/// TODO: For classes with multiple constructors, make sure that we don't offer
///     multiple in-class initializer fixits for the same  member.
class ProTypeMemberInitCheck : public ClangTidyCheck {
public:
  ProTypeMemberInitCheck(StringRef Name, ClangTidyContext *Context);
  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;

private:
  // Checks Type.6 part 1:
  // Issue a diagnostic for any constructor of a non-trivially-constructible
  // type that does not initialize all member variables.
  //
  // To fix: Write a data member initializer, or mention it in the member
  // initializer list.
  void checkMissingMemberInitializer(ASTContext &Context,
                                     const CXXRecordDecl &ClassDecl,
                                     const CXXConstructorDecl *Ctor);

  // A subtle side effect of Type.6 part 2:
  // Make sure to initialize trivially constructible base classes.
  void checkMissingBaseClassInitializer(const ASTContext &Context,
                                        const CXXRecordDecl &ClassDecl,
                                        const CXXConstructorDecl *Ctor);

  // Checks Type.6 part 2:
  // Issue a diagnostic when constructing an object of a trivially constructible
  // type without () or {} to initialize its members.
  //
  // To fix: Add () or {}.
  void checkUninitializedTrivialType(const ASTContext &Context,
                                     const VarDecl *Var);

  // Whether arrays need to be initialized or not. Default is false.
  bool IgnoreArrays;
};

} // namespace cppcoreguidelines
} // namespace tidy
} // namespace clang

#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_PRO_TYPE_MEMBER_INIT_H