clang-tools 23.0.0git
UncheckedStatusOrAccessCheck.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 "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/UncheckedStatusOrAccessModel.h"
15#include "clang/Basic/SourceLocation.h"
16#include "llvm/ADT/SmallVector.h"
17#include "llvm/Support/Error.h"
18
19namespace clang::tidy::abseil {
20using ast_matchers::MatchFinder;
21using dataflow::statusor_model::UncheckedStatusOrAccessDiagnoser;
22using dataflow::statusor_model::UncheckedStatusOrAccessModel;
23
24static constexpr StringRef FuncID = "fun";
25
27 using namespace ast_matchers;
28
29 auto HasStatusOrCallDescendant =
30 hasDescendant(callExpr(callee(cxxMethodDecl(ofClass(hasAnyName(
31 "absl::StatusOr", "absl::internal_statusor::OperatorBase"))))));
32 Finder->addMatcher(
33 functionDecl(hasBody(HasStatusOrCallDescendant)).bind(FuncID), this);
34 Finder->addMatcher(
35 cxxConstructorDecl(hasAnyConstructorInitializer(
36 withInitializer(HasStatusOrCallDescendant)))
37 .bind(FuncID),
38 this);
39}
40
42 const MatchFinder::MatchResult &Result) {
43 if (Result.SourceManager->getDiagnostics().hasUncompilableErrorOccurred())
44 return;
45
46 const auto *FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>(FuncID);
47 if (FuncDecl->isTemplated())
48 return;
49
50 UncheckedStatusOrAccessDiagnoser Diagnoser;
51 if (llvm::Expected<llvm::SmallVector<SourceLocation>> Locs =
52 dataflow::diagnoseFunction<UncheckedStatusOrAccessModel,
53 SourceLocation>(*FuncDecl, *Result.Context,
54 Diagnoser))
55 for (const SourceLocation &Loc : *Locs)
56 diag(Loc, "unchecked access to 'absl::StatusOr' value");
57 else
58 llvm::consumeError(Locs.takeError());
59}
60
62 const LangOptions &LangOpts) const {
63 return LangOpts.CPlusPlus;
64}
65
66} // namespace clang::tidy::abseil
void registerMatchers(ast_matchers::MatchFinder *Finder) override
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
static constexpr StringRef FuncID
static constexpr const char FuncDecl[]