blob: eeb9138d8532d8e128db5e37b9de5943b494bc2c [file] [log] [blame]
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001//===--- 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
14namespace clang {
15namespace tidy {
16namespace 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.
26class UnnecessaryCopyInitialization : public ClangTidyCheck {
27public:
28 UnnecessaryCopyInitialization(StringRef Name, ClangTidyContext *Context);
29 bool isLanguageVersionSupported(const LangOptions &LangOpts) const override{
30 return LangOpts.CPlusPlus;
31 }
32 void registerMatchers(ast_matchers::MatchFinder *Finder) override;
33 void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
34 void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
35
36private:
37 void handleCopyFromMethodReturn(const VarDecl &Var, const Stmt &BlockStmt,
38 bool IssueFix, const VarDecl *ObjectArg,
39 ASTContext &Context);
40 void handleCopyFromLocalVar(const VarDecl &NewVar, const VarDecl &OldVar,
41 const Stmt &BlockStmt, bool IssueFix,
42 ASTContext &Context);
43 const std::vector<std::string> AllowedTypes;
44};
45
46} // namespace performance
47} // namespace tidy
48} // namespace clang
49
50#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_UNNECESSARY_COPY_INITIALIZATION_H