UncheckedOptionalAccessCheck.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //===--- UncheckedOptionalAccessCheck.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_UNCHECKEDOPTIONALACCESSCHECK_H
  9. #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_UNCHECKEDOPTIONALACCESSCHECK_H
  10. #include "../ClangTidyCheck.h"
  11. #include "clang/ASTMatchers/ASTMatchFinder.h"
  12. #include "clang/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.h"
  13. #include <optional>
  14. namespace clang::tidy::bugprone {
  15. /// Warns when the code is unwrapping a `std::optional<T>`, `absl::optional<T>`,
  16. /// or `base::std::optional<T>` object without assuring that it contains a
  17. /// value.
  18. ///
  19. /// For the user-facing documentation see:
  20. /// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/unchecked-optional-access.html
  21. class UncheckedOptionalAccessCheck : public ClangTidyCheck {
  22. public:
  23. UncheckedOptionalAccessCheck(StringRef Name, ClangTidyContext *Context)
  24. : ClangTidyCheck(Name, Context),
  25. ModelOptions{
  26. Options.getLocalOrGlobal("IgnoreSmartPointerDereference", false)} {}
  27. void registerMatchers(ast_matchers::MatchFinder *Finder) override;
  28. void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
  29. bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
  30. return LangOpts.CPlusPlus;
  31. }
  32. void storeOptions(ClangTidyOptions::OptionMap &Opts) override {
  33. Options.store(Opts, "IgnoreSmartPointerDereference",
  34. ModelOptions.IgnoreSmartPointerDereference);
  35. }
  36. private:
  37. dataflow::UncheckedOptionalAccessModelOptions ModelOptions;
  38. };
  39. } // namespace clang::tidy::bugprone
  40. #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_UNCHECKEDOPTIONALACCESSCHECK_H