clang-tools  14.0.0git
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 #include "clang/AST/Decl.h"
14 
15 namespace clang {
16 namespace tidy {
17 namespace performance {
18 
19 // The check detects local variable declarations that are copy initialized with
20 // the const reference of a function call or the const reference of a method
21 // call whose object is guaranteed to outlive the variable's scope and suggests
22 // to use a const reference.
23 //
24 // The check currently only understands a subset of variables that are
25 // guaranteed to outlive the const reference returned, namely: const variables,
26 // const references, and const pointers to const.
28 public:
30  bool isLanguageVersionSupported(const LangOptions &LangOpts) const override{
31  return LangOpts.CPlusPlus;
32  }
33  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
34  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
35  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
36 
37 private:
38  void handleCopyFromMethodReturn(const VarDecl &Var, const Stmt &BlockStmt,
39  const DeclStmt &Stmt, bool IssueFix,
40  const VarDecl *ObjectArg,
41  ASTContext &Context);
42  void handleCopyFromLocalVar(const VarDecl &NewVar, const VarDecl &OldVar,
43  const Stmt &BlockStmt, const DeclStmt &Stmt,
44  bool IssueFix, ASTContext &Context);
45  const std::vector<std::string> AllowedTypes;
46  const std::vector<std::string> ExcludedContainerTypes;
47 };
48 
49 } // namespace performance
50 } // namespace tidy
51 } // namespace clang
52 
53 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_UNNECESSARY_COPY_INITIALIZATION_H
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:115
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:54
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:265
clang::tidy::performance::UnnecessaryCopyInitialization
Definition: UnnecessaryCopyInitialization.h:27
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:72
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
clang::tidy::performance::UnnecessaryCopyInitialization::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: UnnecessaryCopyInitialization.cpp:227
clang::tidy::performance::UnnecessaryCopyInitialization::UnnecessaryCopyInitialization
UnnecessaryCopyInitialization(StringRef Name, ClangTidyContext *Context)
Definition: UnnecessaryCopyInitialization.cpp:219
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::performance::UnnecessaryCopyInitialization::isLanguageVersionSupported
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override
Override this to disable registering matchers and PP callbacks if an invalid language version is bein...
Definition: UnnecessaryCopyInitialization.h:30
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:364