clang-tools 20.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
16
17// The check detects local variable declarations that are copy initialized with
18// the const reference of a function call or the const reference of a method
19// call whose object is guaranteed to outlive the variable's scope and suggests
20// to use a const reference.
21//
22// The check currently only understands a subset of variables that are
23// guaranteed to outlive the const reference returned, namely: const variables,
24// const references, and const pointers to const.
26public:
28 bool isLanguageVersionSupported(const LangOptions &LangOpts) const override{
29 return LangOpts.CPlusPlus;
30 }
31 void registerMatchers(ast_matchers::MatchFinder *Finder) override;
32 void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
33 void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
34
35protected:
36 // A helper to manipulate the state common to
37 // `CopyFromMethodReturn` and `CopyFromLocalVar`.
38 struct CheckContext {
39 const VarDecl &Var;
40 const Stmt &BlockStmt;
41 const DeclStmt &VarDeclStmt;
42 clang::ASTContext &ASTCtx;
43 const bool IssueFix;
44 const bool IsVarUnused;
46 };
47
48 // Create diagnostics. These are virtual so that derived classes can change
49 // behaviour.
50 virtual void diagnoseCopyFromMethodReturn(const CheckContext &Ctx);
51 virtual void diagnoseCopyFromLocalVar(const CheckContext &Ctx,
52 const VarDecl &OldVar);
53
54private:
55 void handleCopyFromMethodReturn(const CheckContext &Ctx,
56 const VarDecl *ObjectArg);
57 void handleCopyFromLocalVar(const CheckContext &Ctx, const VarDecl &OldVar);
58
59 void maybeIssueFixes(const CheckContext &Ctx, DiagnosticBuilder &Diagnostic);
60
61 const std::vector<StringRef> AllowedTypes;
62 const std::vector<StringRef> ExcludedContainerTypes;
63};
64
65} // namespace clang::tidy::performance
66
67#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_UNNECESSARY_COPY_INITIALIZATION_H
llvm::SmallString< 256U > Name
DiagnosticCallback Diagnostic
Base class for all clang-tidy checks.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
virtual void diagnoseCopyFromLocalVar(const CheckContext &Ctx, const VarDecl &OldVar)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override
Override this to disable registering matchers and PP callbacks if an invalid language version is bein...
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
llvm::StringMap< ClangTidyValue > OptionMap