AssertSideEffectCheck.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. //===--- AssertSideEffectCheck.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_ASSERTSIDEEFFECTCHECK_H
  9. #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_ASSERTSIDEEFFECTCHECK_H
  10. #include "../ClangTidyCheck.h"
  11. #include "llvm/ADT/SmallVector.h"
  12. #include "llvm/ADT/StringRef.h"
  13. #include <string>
  14. namespace clang::tidy::bugprone {
  15. /// Finds `assert()` with side effect.
  16. ///
  17. /// The condition of `assert()` is evaluated only in debug builds so a
  18. /// condition with side effect can cause different behavior in debug / release
  19. /// builds.
  20. ///
  21. /// There are two options:
  22. ///
  23. /// - `AssertMacros`: A comma-separated list of the names of assert macros to
  24. /// be checked.
  25. /// - `CheckFunctionCalls`: Whether to treat non-const member and non-member
  26. /// functions as they produce side effects. Disabled by default because it
  27. /// can increase the number of false positive warnings.
  28. class AssertSideEffectCheck : public ClangTidyCheck {
  29. public:
  30. AssertSideEffectCheck(StringRef Name, ClangTidyContext *Context);
  31. void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
  32. void registerMatchers(ast_matchers::MatchFinder *Finder) override;
  33. void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
  34. private:
  35. const bool CheckFunctionCalls;
  36. const StringRef RawAssertList;
  37. SmallVector<StringRef, 5> AssertMacros;
  38. const std::vector<StringRef> IgnoredFunctions;
  39. };
  40. } // namespace clang::tidy::bugprone
  41. #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_ASSERTSIDEEFFECTCHECK_H