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"
20using ast_matchers::MatchFinder;
21using dataflow::UncheckedOptionalAccessDiagnoser;
22using dataflow::UncheckedOptionalAccessModel;
24static constexpr llvm::StringLiteral
FuncID(
"fun");
27 using namespace ast_matchers;
29 auto HasOptionalCallDescendant = hasDescendant(callExpr(callee(cxxMethodDecl(
30 ofClass(UncheckedOptionalAccessModel::optionalClassDecl())))));
32 decl(anyOf(functionDecl(unless(isExpansionInSystemHeader()),
35 unless(hasDeclContext(cxxRecordDecl(isLambda()))),
36 hasBody(HasOptionalCallDescendant)),
37 cxxConstructorDecl(hasAnyConstructorInitializer(
38 withInitializer(HasOptionalCallDescendant)))))
44 const MatchFinder::MatchResult &Result) {
45 if (Result.SourceManager->getDiagnostics().hasUncompilableErrorOccurred())
48 const auto *
FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>(
FuncID);
52 UncheckedOptionalAccessDiagnoser Diagnoser(ModelOptions);
55 if (llvm::Expected<llvm::SmallVector<SourceLocation>> Locs =
56 dataflow::diagnoseFunction<UncheckedOptionalAccessModel,
57 SourceLocation>(*
FuncDecl, *Result.Context,
59 for (
const SourceLocation &
Loc : *Locs)
60 diag(
Loc,
"unchecked access to optional value");
62 llvm::consumeError(Locs.takeError());
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[]