brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.8 KiB · 89957a5 Raw
71 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_UNNECESSARYCOPYINITIALIZATIONCHECK_H10#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_UNNECESSARYCOPYINITIALIZATIONCHECK_H11 12#include "../ClangTidyCheck.h"13#include "clang/AST/Decl.h"14 15namespace clang::tidy::performance {16 17// The check detects local variable declarations that are copy initialized with18// the const reference of a function call or the const reference of a method19// call whose object is guaranteed to outlive the variable's scope and suggests20// to use a const reference.21//22// The check currently only understands a subset of variables that are23// 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.html28class UnnecessaryCopyInitializationCheck : public ClangTidyCheck {29public:30  UnnecessaryCopyInitializationCheck(StringRef Name, ClangTidyContext *Context);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 to40  // `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;48    const bool IsVarOnlyUsedAsConst;49  };50 51  // Create diagnostics. These are virtual so that derived classes can change52  // 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::performance69 70#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_UNNECESSARYCOPYINITIALIZATIONCHECK_H71