clang-tools 10.0.0

UnnecessaryCopyInitialization.h
Go to the documentation of this file.
1 //===--- UnnecessaryCopyInitialization.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_PERFORMANCE_UNNECESSARY_COPY_INITIALIZATION_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_UNNECESSARY_COPY_INITIALIZATION_H
11
12 #include "../ClangTidyCheck.h"
13
14 namespace clang {
15 namespace tidy {
16 namespace performance {
17
18 // The check detects local variable declarations that are copy initialized with
19 // the const reference of a function call or the const reference of a method
20 // call whose object is guaranteed to outlive the variable's scope and suggests
21 // to use a const reference.
22 //
23 // The check currently only understands a subset of variables that are
24 // guaranteed to outlive the const reference returned, namely: const variables,
25 // const references, and const pointers to const.
26 class UnnecessaryCopyInitialization : public ClangTidyCheck {
27 public:
28 UnnecessaryCopyInitialization(StringRef Name, ClangTidyContext *Context);
29 void registerMatchers(ast_matchers::MatchFinder *Finder) override;
30 void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
31 void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
32
33 private:
34 void handleCopyFromMethodReturn(const VarDecl &Var, const Stmt &BlockStmt,
35 bool IssueFix, const VarDecl *ObjectArg,
36 ASTContext &Context);
37 void handleCopyFromLocalVar(const VarDecl &NewVar, const VarDecl &OldVar,
38 const Stmt &BlockStmt, bool IssueFix,
39 ASTContext &Context);
40 const std::vector<std::string> AllowedTypes;
41 };
42
43 } // namespace performance
44 } // namespace tidy
45 } // namespace clang
46
47 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_UNNECESSARY_COPY_INITIALIZATION_H
clang::tidy::performance::UnnecessaryCopyInitialization
Definition: UnnecessaryCopyInitialization.h:26
clang::tidy::performance::UnnecessaryCopyInitialization::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: UnnecessaryCopyInitialization.cpp:43
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:47
clang::tidy::performance::UnnecessaryCopyInitialization::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: UnnecessaryCopyInitialization.cpp:159
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:97
clang::tidy::performance::UnnecessaryCopyInitialization::UnnecessaryCopyInitialization
UnnecessaryCopyInitialization(StringRef Name, ClangTidyContext *Context)
Definition: UnnecessaryCopyInitialization.cpp:37
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:77
clang::tidy::performance::UnnecessaryCopyInitialization::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: UnnecessaryCopyInitialization.cpp:91