Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:21

0001 //===--- ForRangeCopyCheck.h - clang-tidy------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_FORRANGECOPYCHECK_H
0010 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_FORRANGECOPYCHECK_H
0011 
0012 #include "../ClangTidyCheck.h"
0013 
0014 namespace clang::tidy::performance {
0015 
0016 /// A check that detects copied loop variables and suggests using const
0017 /// references.
0018 /// For the user-facing documentation see:
0019 /// http://clang.llvm.org/extra/clang-tidy/checks/performance/for-range-copy.html
0020 class ForRangeCopyCheck : public ClangTidyCheck {
0021 public:
0022   ForRangeCopyCheck(StringRef Name, ClangTidyContext *Context);
0023   bool isLanguageVersionSupported(const LangOptions &LangOpts) const override{
0024     return LangOpts.CPlusPlus11;
0025   }
0026   void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
0027   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
0028   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
0029 
0030 private:
0031   // Checks if the loop variable is a const value and expensive to copy. If so
0032   // suggests it be converted to a const reference.
0033   bool handleConstValueCopy(const VarDecl &LoopVar, ASTContext &Context);
0034 
0035   // Checks if the loop variable is a non-const value and whether only
0036   // const methods are invoked on it or whether it is only used as a const
0037   // reference argument. If so it suggests it be made a const reference.
0038   bool handleCopyIsOnlyConstReferenced(const VarDecl &LoopVar,
0039                                        const CXXForRangeStmt &ForRange,
0040                                        ASTContext &Context);
0041 
0042   const bool WarnOnAllAutoCopies;
0043   const std::vector<StringRef> AllowedTypes;
0044 };
0045 
0046 } // namespace clang::tidy::performance
0047 
0048 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_FORRANGECOPYCHECK_H