xref: /llvm-project/clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.h (revision 4718da506091a37ca4863d979bc541e359b79b10)
1 //===--- InefficientVectorOperationCheck.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_INEFFICIENT_VECTOR_OPERATION_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_INEFFICIENT_VECTOR_OPERATION_H
11 
12 #include "../ClangTidyCheck.h"
13 
14 namespace clang::tidy::performance {
15 
16 /// Finds possible inefficient `std::vector` operations (e.g. `push_back`) in
17 /// for loops that may cause unnecessary memory reallocations.
18 ///
19 /// When EnableProto, also finds calls that add element to protobuf repeated
20 /// field without calling Reserve() first.
21 ///
22 /// For the user-facing documentation see:
23 /// http://clang.llvm.org/extra/clang-tidy/checks/performance/inefficient-vector-operation.html
24 class InefficientVectorOperationCheck : public ClangTidyCheck {
25 public:
26   InefficientVectorOperationCheck(StringRef Name, ClangTidyContext *Context);
isLanguageVersionSupported(const LangOptions & LangOpts)27   bool isLanguageVersionSupported(const LangOptions &LangOpts) const override{
28     return LangOpts.CPlusPlus;
29   }
30   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
31   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
32   void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
33 
34 private:
35   void addMatcher(const ast_matchers::DeclarationMatcher &TargetRecordDecl,
36                   StringRef VarDeclName, StringRef VarDeclStmtName,
37                   const ast_matchers::DeclarationMatcher &AppendMethodDecl,
38                   StringRef AppendCallName, ast_matchers::MatchFinder *Finder);
39   const std::vector<StringRef> VectorLikeClasses;
40 
41   // If true, also check inefficient operations for proto repeated fields.
42   bool EnableProto;
43 };
44 
45 } // namespace clang::tidy::performance
46 
47 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_INEFFICIENT_VECTOR_OPERATION_H
48