InefficientVectorOperationCheck.h 2.0 KB

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