UncheckedOptionalAccessModel.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- UncheckedOptionalAccessModel.h --------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines a dataflow analysis that detects unsafe uses of optional
  15. // values.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_UNCHECKEDOPTIONALACCESSMODEL_H
  19. #define CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_UNCHECKEDOPTIONALACCESSMODEL_H
  20. #include "clang/AST/ASTContext.h"
  21. #include "clang/Analysis/CFG.h"
  22. #include "clang/Analysis/FlowSensitive/CFGMatchSwitch.h"
  23. #include "clang/Analysis/FlowSensitive/DataflowAnalysis.h"
  24. #include "clang/Analysis/FlowSensitive/DataflowEnvironment.h"
  25. #include "clang/Analysis/FlowSensitive/NoopLattice.h"
  26. #include "clang/Basic/SourceLocation.h"
  27. #include <vector>
  28. namespace clang {
  29. namespace dataflow {
  30. // FIXME: Explore using an allowlist-approach, where constructs supported by the
  31. // analysis are always enabled and additional constructs are enabled through the
  32. // `Options`.
  33. struct UncheckedOptionalAccessModelOptions {
  34. /// In generating diagnostics, ignore optionals reachable through overloaded
  35. /// `operator*` or `operator->` (other than those of the optional type
  36. /// itself). The analysis does not equate the results of such calls, so it
  37. /// can't identify when their results are used safely (across calls),
  38. /// resulting in false positives in all such cases. Note: this option does not
  39. /// cover access through `operator[]`.
  40. bool IgnoreSmartPointerDereference = false;
  41. };
  42. /// Dataflow analysis that models whether optionals hold values or not.
  43. ///
  44. /// Models the `std::optional`, `absl::optional`, and `base::Optional` types.
  45. class UncheckedOptionalAccessModel
  46. : public DataflowAnalysis<UncheckedOptionalAccessModel, NoopLattice> {
  47. public:
  48. UncheckedOptionalAccessModel(ASTContext &Ctx);
  49. /// Returns a matcher for the optional classes covered by this model.
  50. static ast_matchers::DeclarationMatcher optionalClassDecl();
  51. static NoopLattice initialElement() { return {}; }
  52. void transfer(const CFGElement *Elt, NoopLattice &L, Environment &Env);
  53. ComparisonResult compare(QualType Type, const Value &Val1,
  54. const Environment &Env1, const Value &Val2,
  55. const Environment &Env2) override;
  56. bool merge(QualType Type, const Value &Val1, const Environment &Env1,
  57. const Value &Val2, const Environment &Env2, Value &MergedVal,
  58. Environment &MergedEnv) override;
  59. Value *widen(QualType Type, Value &Prev, const Environment &PrevEnv,
  60. Value &Current, Environment &CurrentEnv) override;
  61. private:
  62. CFGMatchSwitch<TransferState<NoopLattice>> TransferMatchSwitch;
  63. };
  64. class UncheckedOptionalAccessDiagnoser {
  65. public:
  66. UncheckedOptionalAccessDiagnoser(
  67. UncheckedOptionalAccessModelOptions Options = {});
  68. std::vector<SourceLocation> diagnose(ASTContext &Ctx, const CFGElement *Elt,
  69. const Environment &Env);
  70. private:
  71. CFGMatchSwitch<const Environment, std::vector<SourceLocation>>
  72. DiagnoseMatchSwitch;
  73. };
  74. } // namespace dataflow
  75. } // namespace clang
  76. #endif // CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_UNCHECKEDOPTIONALACCESSMODEL_H
  77. #ifdef __GNUC__
  78. #pragma GCC diagnostic pop
  79. #endif