clang-tools 20.0.0git
UncheckedOptionalAccessCheck.cpp
Go to the documentation of this file.
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
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
19namespace clang::tidy::bugprone {
20using ast_matchers::MatchFinder;
21using dataflow::UncheckedOptionalAccessDiagnoser;
22using dataflow::UncheckedOptionalAccessModel;
23
24static constexpr llvm::StringLiteral FuncID("fun");
25
27 using namespace ast_matchers;
28
29 auto HasOptionalCallDescendant = hasDescendant(callExpr(callee(cxxMethodDecl(
30 ofClass(UncheckedOptionalAccessModel::optionalClassDecl())))));
31 Finder->addMatcher(
32 decl(anyOf(functionDecl(unless(isExpansionInSystemHeader()),
33 // FIXME: Remove the filter below when lambdas are
34 // well supported by the check.
35 unless(hasDeclContext(cxxRecordDecl(isLambda()))),
36 hasBody(HasOptionalCallDescendant)),
37 cxxConstructorDecl(hasAnyConstructorInitializer(
38 withInitializer(HasOptionalCallDescendant)))))
39 .bind(FuncID),
40 this);
41}
42
44 const MatchFinder::MatchResult &Result) {
45 if (Result.SourceManager->getDiagnostics().hasUncompilableErrorOccurred())
46 return;
47
48 const auto *FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>(FuncID);
49 if (FuncDecl->isTemplated())
50 return;
51
52 UncheckedOptionalAccessDiagnoser Diagnoser(ModelOptions);
53 // FIXME: Allow user to set the (defaulted) SAT iterations max for
54 // `diagnoseFunction` with config options.
55 if (llvm::Expected<llvm::SmallVector<SourceLocation>> Locs =
56 dataflow::diagnoseFunction<UncheckedOptionalAccessModel,
57 SourceLocation>(*FuncDecl, *Result.Context,
58 Diagnoser))
59 for (const SourceLocation &Loc : *Locs)
60 diag(Loc, "unchecked access to optional value");
61 else
62 llvm::consumeError(Locs.takeError());
63}
64
65} // namespace clang::tidy::bugprone
SourceLocation Loc
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
static constexpr llvm::StringLiteral FuncID("fun")
static constexpr const char FuncDecl[]