Debian Codegraph @

llvm-project/clang-tools-extra/clang-tidy/bugprone/UncheckedOptionalAccessCheck.cpp

raw
1 //===--- UncheckedOptionalAccessCheck.cpp - clang-tidy --------------------===//
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
9 #include "UncheckedOptionalAccessCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/AST/DeclCXX.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/ASTMatchers/ASTMatchers.h"
14 #include "clang/Analysis/CFG.h"
15 #include "clang/Analysis/FlowSensitive/ControlFlowContext.h"
16 #include "clang/Analysis/FlowSensitive/DataflowAnalysisContext.h"
17 #include "clang/Analysis/FlowSensitive/DataflowEnvironment.h"
18 #include "clang/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.h"
19 #include "clang/Analysis/FlowSensitive/WatchedLiteralsSolver.h"
20 #include "clang/Basic/SourceLocation.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/Support/Error.h"
23 #include <memory>
24 #include <optional>
25 #include <vector>
26
27 namespace clang::tidy::bugprone {
28 using ast_matchers::MatchFinder;
29 using dataflow::UncheckedOptionalAccessDiagnoser;
30 using dataflow::UncheckedOptionalAccessModel;
31 using dataflow::UncheckedOptionalAccessModelOptions;
32
33 static constexpr llvm::StringLiteral FuncID("fun");
34
35 static std::optional<std::vector<SourceLocation>>
36 analyzeFunction(const FunctionDecl &FuncDecl, ASTContext &ASTCtx,
37 UncheckedOptionalAccessModelOptions ModelOptions) {
38 using dataflow::ControlFlowContext;
39 using dataflow::DataflowAnalysisState;
40 using llvm::Expected;
41
42 Expected<ControlFlowContext> Context =
43 ControlFlowContext::build(&FuncDecl, *FuncDecl.getBody(), ASTCtx);
44 if (!Context)
45 return std::nullopt;
46
47 dataflow::DataflowAnalysisContext AnalysisContext(
48 std::make_unique<dataflow::WatchedLiteralsSolver>());
49 dataflow::Environment Env(AnalysisContext, FuncDecl);
50 UncheckedOptionalAccessModel Analysis(ASTCtx);
51 UncheckedOptionalAccessDiagnoser Diagnoser(ModelOptions);
52 std::vector<SourceLocation> Diagnostics;
53 Expected<std::vector<std::optional<
54 DataflowAnalysisState<UncheckedOptionalAccessModel::Lattice>>>>
55 BlockToOutputState = dataflow::runDataflowAnalysis(
56 *Context, Analysis, Env,
57 [&ASTCtx, &Diagnoser, &Diagnostics](
58 const CFGElement &Elt,
59 const DataflowAnalysisState<UncheckedOptionalAccessModel::Lattice>
60 &State) mutable {
61 auto EltDiagnostics = Diagnoser.diagnose(ASTCtx, &Elt, State.Env);
62 llvm::move(EltDiagnostics, std::back_inserter(Diagnostics));
63 });
64 if (!BlockToOutputState)
65 return std::nullopt;
66
67 return Diagnostics;
68 }
69
70 void UncheckedOptionalAccessCheck::registerMatchers(MatchFinder *Finder) {
71 using namespace ast_matchers;
72
73 auto HasOptionalCallDescendant = hasDescendant(callExpr(callee(cxxMethodDecl(
74 ofClass(UncheckedOptionalAccessModel::optionalClassDecl())))));
75 Finder->addMatcher(
76 decl(anyOf(functionDecl(unless(isExpansionInSystemHeader()),
77 // FIXME: Remove the filter below when lambdas are
78 // well supported by the check.
79 unless(hasDeclContext(cxxRecordDecl(isLambda()))),
80 hasBody(HasOptionalCallDescendant)),
81 cxxConstructorDecl(hasAnyConstructorInitializer(
82 withInitializer(HasOptionalCallDescendant)))))
83 .bind(FuncID),
84 this);
85 }
86
87 void UncheckedOptionalAccessCheck::check(
88 const MatchFinder::MatchResult &Result) {
89 if (Result.SourceManager->getDiagnostics().hasUncompilableErrorOccurred())
90 return;
91
92 const auto *FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>(FuncID);
93 if (FuncDecl->isTemplated())
94 return;
95
96 if (std::optional<std::vector<SourceLocation>> Errors =
97 analyzeFunction(*FuncDecl, *Result.Context, ModelOptions))
98 for (const SourceLocation &Loc : *Errors)
99 diag(Loc, "unchecked access to optional value");
100 }
101
102 } // namespace clang::tidy::bugprone