TooSmallLoopVariableCheck.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. //===--- TooSmallLoopVariableCheck.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_BUGPRONE_TOOSMALLLOOPVARIABLECHECK_H
  9. #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_TOOSMALLLOOPVARIABLECHECK_H
  10. #include "../ClangTidyCheck.h"
  11. namespace clang::tidy::bugprone {
  12. /// This check gives a warning if a loop variable has a too small type which
  13. /// might not be able to represent all values which are part of the whole range
  14. /// in which the loop iterates.
  15. /// If the loop variable's type is too small we might end up in an infinite
  16. /// loop. Example:
  17. /// \code
  18. /// long size = 294967296l;
  19. /// for (short i = 0; i < size; ++i) {} { ... }
  20. /// \endcode
  21. ///
  22. /// For the user-facing documentation see:
  23. /// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/too-small-loop-variable.html
  24. class TooSmallLoopVariableCheck : public ClangTidyCheck {
  25. public:
  26. TooSmallLoopVariableCheck(StringRef Name, ClangTidyContext *Context);
  27. void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
  28. void registerMatchers(ast_matchers::MatchFinder *Finder) override;
  29. void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
  30. private:
  31. const unsigned MagnitudeBitsUpperLimit;
  32. };
  33. } // namespace clang::tidy::bugprone
  34. #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_TOOSMALLLOOPVARIABLECHECK_H