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/ASTMatchers/ASTMatchFinder.h" |
12 | #include "clang/ASTMatchers/ASTMatchers.h" |
13 | #include "clang/Analysis/FlowSensitive/DataflowAnalysis.h" |
14 | #include "clang/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.h" |
15 | #include "clang/Basic/SourceLocation.h" |
16 | #include "llvm/ADT/SmallVector.h" |
17 | #include "llvm/Support/Error.h" |
18 | |
19 | namespace clang::tidy::bugprone { |
20 | using ast_matchers::MatchFinder; |
21 | using dataflow::UncheckedOptionalAccessDiagnoser; |
22 | using dataflow::UncheckedOptionalAccessDiagnostic; |
23 | using dataflow::UncheckedOptionalAccessModel; |
24 | |
25 | static constexpr llvm::StringLiteral FuncID("fun" ); |
26 | |
27 | void UncheckedOptionalAccessCheck::registerMatchers(MatchFinder *Finder) { |
28 | using namespace ast_matchers; |
29 | |
30 | auto HasOptionalCallDescendant = hasDescendant(callExpr(callee(InnerMatcher: cxxMethodDecl( |
31 | ofClass(InnerMatcher: UncheckedOptionalAccessModel::optionalClassDecl()))))); |
32 | Finder->addMatcher( |
33 | NodeMatch: decl(anyOf(functionDecl(unless(isExpansionInSystemHeader()), |
34 | // FIXME: Remove the filter below when lambdas are |
35 | // well supported by the check. |
36 | unless(hasDeclContext(InnerMatcher: cxxRecordDecl(isLambda()))), |
37 | hasBody(InnerMatcher: HasOptionalCallDescendant)), |
38 | cxxConstructorDecl(hasAnyConstructorInitializer( |
39 | InnerMatcher: withInitializer(InnerMatcher: HasOptionalCallDescendant))))) |
40 | .bind(ID: FuncID), |
41 | Action: this); |
42 | } |
43 | |
44 | void UncheckedOptionalAccessCheck::check( |
45 | const MatchFinder::MatchResult &Result) { |
46 | if (Result.SourceManager->getDiagnostics().hasUncompilableErrorOccurred()) |
47 | return; |
48 | |
49 | const auto *FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>(ID: FuncID); |
50 | if (FuncDecl->isTemplated()) |
51 | return; |
52 | |
53 | UncheckedOptionalAccessDiagnoser Diagnoser(ModelOptions); |
54 | // FIXME: Allow user to set the (defaulted) SAT iterations max for |
55 | // `diagnoseFunction` with config options. |
56 | if (llvm::Expected<llvm::SmallVector<UncheckedOptionalAccessDiagnostic>> |
57 | Diags = dataflow::diagnoseFunction<UncheckedOptionalAccessModel, |
58 | UncheckedOptionalAccessDiagnostic>( |
59 | FuncDecl: *FuncDecl, ASTCtx&: *Result.Context, Diagnoser)) |
60 | for (const UncheckedOptionalAccessDiagnostic &Diag : *Diags) { |
61 | diag(Loc: Diag.Range.getBegin(), Description: "unchecked access to optional value" ) |
62 | << Diag.Range; |
63 | } |
64 | else |
65 | llvm::consumeError(Err: Diags.takeError()); |
66 | } |
67 | |
68 | } // namespace clang::tidy::bugprone |
69 | |