clang-tools  6.0.0
ProTypeMemberInitCheck.h
Go to the documentation of this file.
1 //===--- ProTypeMemberInitCheck.h - clang-tidy-------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_PRO_TYPE_MEMBER_INIT_H
11 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_PRO_TYPE_MEMBER_INIT_H
12 
13 #include "../ClangTidy.h"
14 
15 namespace clang {
16 namespace tidy {
17 namespace cppcoreguidelines {
18 
19 /// \brief Implements C++ Core Guidelines Type.6.
20 ///
21 /// Checks that every user-provided constructor value-initializes all class
22 /// members and base classes that would have undefined behavior otherwise. Also
23 /// check that any record types without user-provided default constructors are
24 /// value-initialized where used.
25 ///
26 /// Members initialized through function calls in the body of the constructor
27 /// will result in false positives.
28 ///
29 /// For the user-facing documentation see:
30 /// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-pro-type-member-init.html
31 /// TODO: See if 'fixes' for false positives are optimized away by the compiler.
32 /// TODO: For classes with multiple constructors, make sure that we don't offer
33 /// multiple in-class initializer fixits for the same member.
35 public:
36  ProTypeMemberInitCheck(StringRef Name, ClangTidyContext *Context);
37  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
38  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
39  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
40 
41 private:
42  // Checks Type.6 part 1:
43  // Issue a diagnostic for any constructor of a non-trivially-constructible
44  // type that does not initialize all member variables.
45  //
46  // To fix: Write a data member initializer, or mention it in the member
47  // initializer list.
48  void checkMissingMemberInitializer(ASTContext &Context,
49  const CXXRecordDecl &ClassDecl,
50  const CXXConstructorDecl *Ctor);
51 
52  // A subtle side effect of Type.6 part 2:
53  // Make sure to initialize trivially constructible base classes.
54  void checkMissingBaseClassInitializer(const ASTContext &Context,
55  const CXXRecordDecl &ClassDecl,
56  const CXXConstructorDecl *Ctor);
57 
58  // Checks Type.6 part 2:
59  // Issue a diagnostic when constructing an object of a trivially constructible
60  // type without () or {} to initialize its members.
61  //
62  // To fix: Add () or {}.
63  void checkUninitializedTrivialType(const ASTContext &Context,
64  const VarDecl *Var);
65 
66  // Whether arrays need to be initialized or not. Default is false.
67  bool IgnoreArrays;
68 };
69 
70 } // namespace cppcoreguidelines
71 } // namespace tidy
72 } // namespace clang
73 
74 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_PRO_TYPE_MEMBER_INIT_H
StringHandle Name
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
ProTypeMemberInitCheck(StringRef Name, ClangTidyContext *Context)
Base class for all clang-tidy checks.
Definition: ClangTidy.h:127
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
std::map< std::string, std::string > OptionMap
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.