clang-tools  10.0.0svn
ProTypeMemberInitCheck.h
Go to the documentation of this file.
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 "../ClangTidy.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.
34 public:
35  ProTypeMemberInitCheck(StringRef Name, ClangTidyContext *Context);
36  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
37  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
38  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
39 
40 private:
41  // Checks Type.6 part 1:
42  // Issue a diagnostic for any constructor of a non-trivially-constructible
43  // type that does not initialize all member variables.
44  //
45  // To fix: Write a data member initializer, or mention it in the member
46  // initializer list.
47  void checkMissingMemberInitializer(ASTContext &Context,
48  const CXXRecordDecl &ClassDecl,
49  const CXXConstructorDecl *Ctor);
50 
51  // A subtle side effect of Type.6 part 2:
52  // Make sure to initialize trivially constructible base classes.
53  void checkMissingBaseClassInitializer(const ASTContext &Context,
54  const CXXRecordDecl &ClassDecl,
55  const CXXConstructorDecl *Ctor);
56 
57  // Checks Type.6 part 2:
58  // Issue a diagnostic when constructing an object of a trivially constructible
59  // type without () or {} to initialize its members.
60  //
61  // To fix: Add () or {}.
62  void checkUninitializedTrivialType(const ASTContext &Context,
63  const VarDecl *Var);
64 
65  // Whether arrays need to be initialized or not. Default is false.
66  bool IgnoreArrays;
67 
68  // Whether fix-its for initialization of fundamental type use assignment
69  // instead of brace initalization. Only effective in C++11 mode. Default is
70  // false.
71  bool UseAssignment;
72 };
73 
74 } // namespace cppcoreguidelines
75 } // namespace tidy
76 } // namespace clang
77 
78 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_PRO_TYPE_MEMBER_INIT_H
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.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.