clang-tools 22.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 llvm::StringLiteral 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(functionDecl(unless(isExpansionInSystemHeader()),
33 hasBody(HasStatusOrCallDescendant))
34 .bind(FuncID),
35 this);
36 Finder->addMatcher(
37 cxxConstructorDecl(hasAnyConstructorInitializer(
38 withInitializer(HasStatusOrCallDescendant)))
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 UncheckedStatusOrAccessDiagnoser Diagnoser;
53 if (llvm::Expected<llvm::SmallVector<SourceLocation>> Locs =
54 dataflow::diagnoseFunction<UncheckedStatusOrAccessModel,
55 SourceLocation>(*FuncDecl, *Result.Context,
56 Diagnoser))
57 for (const SourceLocation &Loc : *Locs)
58 diag(Loc, "unchecked access to 'absl::StatusOr' value");
59 else
60 llvm::consumeError(Locs.takeError());
61}
62
64 const LangOptions &LangOpts) const {
65 return LangOpts.CPlusPlus;
66}
67
68} // 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 llvm::StringLiteral FuncID("fun")
static constexpr const char FuncDecl[]