clang-tools 22.0.0git
UnnecessaryCopyInitializationCheck.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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_UNNECESSARYCOPYINITIALIZATIONCHECK_H
10#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_UNNECESSARYCOPYINITIALIZATIONCHECK_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.
25//
26// For the user-facing documentation see:
27// https://clang.llvm.org/extra/clang-tidy/checks/performance/unnecessary-copy-initialization.html
29public:
31 bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
32 return LangOpts.CPlusPlus;
33 }
34 void registerMatchers(ast_matchers::MatchFinder *Finder) override;
35 void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
36 void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
37
38protected:
39 // A helper to manipulate the state common to
40 // `CopyFromMethodReturn` and `CopyFromLocalVar`.
41 struct CheckContext {
42 const VarDecl &Var;
43 const Stmt &BlockStmt;
44 const DeclStmt &VarDeclStmt;
45 clang::ASTContext &ASTCtx;
46 const bool IssueFix;
47 const bool IsVarUnused;
49 };
50
51 // Create diagnostics. These are virtual so that derived classes can change
52 // behaviour.
53 virtual void diagnoseCopyFromMethodReturn(const CheckContext &Ctx);
54 virtual void diagnoseCopyFromLocalVar(const CheckContext &Ctx,
55 const VarDecl &OldVar);
56
57private:
58 void handleCopyFromMethodReturn(const CheckContext &Ctx,
59 const VarDecl *ObjectArg);
60 void handleCopyFromLocalVar(const CheckContext &Ctx, const VarDecl &OldVar);
61
62 void maybeIssueFixes(const CheckContext &Ctx, DiagnosticBuilder &Diagnostic);
63
64 const std::vector<StringRef> AllowedTypes;
65 const std::vector<StringRef> ExcludedContainerTypes;
66};
67
68} // namespace clang::tidy::performance
69
70#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_UNNECESSARYCOPYINITIALIZATIONCHECK_H
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
virtual void diagnoseCopyFromLocalVar(const CheckContext &Ctx, const VarDecl &OldVar)
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override
llvm::StringMap< ClangTidyValue > OptionMap