StaticAssertCheck.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. //===--- StaticAssertCheck.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_MISC_STATICASSERTCHECK_H
  9. #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_STATICASSERTCHECK_H
  10. #include "../ClangTidyCheck.h"
  11. #include "llvm/ADT/StringRef.h"
  12. #include <string>
  13. namespace clang::tidy::misc {
  14. /// Replaces `assert()` with `static_assert()` if the condition is evaluatable
  15. /// at compile time.
  16. ///
  17. /// The condition of `static_assert()` is evaluated at compile time which is
  18. /// safer and more efficient.
  19. class StaticAssertCheck : public ClangTidyCheck {
  20. public:
  21. StaticAssertCheck(StringRef Name, ClangTidyContext *Context);
  22. bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
  23. return LangOpts.CPlusPlus11 || LangOpts.C11;
  24. }
  25. void registerMatchers(ast_matchers::MatchFinder *Finder) override;
  26. void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
  27. private:
  28. SourceLocation getLastParenLoc(const ASTContext *ASTCtx,
  29. SourceLocation AssertLoc);
  30. };
  31. } // namespace clang::tidy::misc
  32. #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_STATICASSERTCHECK_H