clang-tools 24.0.0git
UncheckedOptionalAccessCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
10#include "../ClangTidyCheck.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/ASTMatchers/ASTMatchers.h"
14#include "clang/Analysis/FlowSensitive/DataflowAnalysis.h"
15#include "clang/Analysis/FlowSensitive/DataflowAnalysisContext.h"
16#include "clang/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.h"
17#include "clang/Basic/SourceLocation.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/Support/Error.h"
20
21#include <optional>
22#include <utility>
23
24namespace clang::tidy {
25template <> struct OptionEnumMapping<dataflow::BoolModelKind> {
26 static llvm::ArrayRef<std::pair<dataflow::BoolModelKind, llvm::StringRef>>
28 static constexpr std::pair<dataflow::BoolModelKind, llvm::StringRef>
29 Mapping[] = {{dataflow::BoolModelKind::SAT, "SAT"},
30 {dataflow::BoolModelKind::Simple, "Simple"}};
31 return {Mapping};
32 }
33};
34} // namespace clang::tidy
35
36namespace clang::tidy::bugprone {
37using ast_matchers::MatchFinder;
38using dataflow::UncheckedOptionalAccessDiagnoser;
39using dataflow::UncheckedOptionalAccessDiagnostic;
40using dataflow::UncheckedOptionalAccessModel;
41
42static constexpr StringRef FuncID = "fun";
43
45 StringRef Name, ClangTidyContext *Context)
46 : ClangTidyCheck(Name, Context),
47 ModelOptions{Options.get("IgnoreSmartPointerDereference", false),
48 Options.get("IgnoreValueCalls", false)},
49 BoolModel(Options.get("BoolSolverModel", dataflow::BoolModelKind::SAT)) {}
50
53 Options.store(Opts, "IgnoreSmartPointerDereference",
54 ModelOptions.IgnoreSmartPointerDereference);
55 Options.store(Opts, "IgnoreValueCalls", ModelOptions.IgnoreValueCalls);
56 Options.store(Opts, "BoolSolverModel", BoolModel);
57}
58
60 using namespace ast_matchers;
61
62 const auto HasOptionalCallDescendant = hasDescendant(callExpr(
63 anyOf(UncheckedOptionalAccessModel::memberCallToOptionalClass(),
64 UncheckedOptionalAccessModel::operatorCallToOptionalClass())));
65 Finder->addMatcher(
66 decl(anyOf(functionDecl(
67 // FIXME: Remove the filter below when lambdas are
68 // well supported by the check.
69 unless(hasDeclContext(cxxRecordDecl(isLambda()))),
70 hasBody(HasOptionalCallDescendant)),
71 cxxConstructorDecl(hasAnyConstructorInitializer(
72 withInitializer(HasOptionalCallDescendant)))))
73 .bind(FuncID),
74 this);
75}
76
78 const MatchFinder::MatchResult &Result) {
79 if (Result.SourceManager->getDiagnostics().hasUncompilableErrorOccurred())
80 return;
81
82 const auto *FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>(FuncID);
83 if (FuncDecl->isTemplated())
84 return;
85
86 UncheckedOptionalAccessDiagnoser Diagnoser(ModelOptions);
87 // FIXME: Allow user to set the (defaulted) SAT iterations max for
88 // `diagnoseFunction` with config options.
89 if (llvm::Expected<SmallVector<UncheckedOptionalAccessDiagnostic>> Diags =
90 dataflow::diagnoseFunction<UncheckedOptionalAccessModel,
91 UncheckedOptionalAccessDiagnostic>(
92 *FuncDecl, *Result.Context, Diagnoser,
93 dataflow::kDefaultMaxSATIterations,
94 dataflow::kDefaultMaxBlockVisits,
95 {/*ContextSensitiveOpts=*/std::nullopt,
96 /*Logger=*/nullptr,
97 /*BoolModel=*/BoolModel})) {
98 for (const UncheckedOptionalAccessDiagnostic &Diag : *Diags) {
99 diag(Diag.Range.getBegin(), "unchecked access to optional value")
100 << Diag.Range;
101 }
102 } else {
103 llvm::consumeError(Diags.takeError());
104 }
105}
106
107} // namespace clang::tidy::bugprone
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
UncheckedOptionalAccessCheck(StringRef Name, ClangTidyContext *Context)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
static constexpr StringRef FuncID
llvm::StringMap< ClangTidyValue > OptionMap
static llvm::ArrayRef< std::pair< dataflow::BoolModelKind, llvm::StringRef > > getEnumMapping()
This class should be specialized by any enum type that needs to be converted to and from an llvm::Str...
static constexpr const char FuncDecl[]