brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.8 KiB · e8f2041 Raw
69 lines · cpp
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-exception6//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 19namespace clang::tidy::bugprone {20using ast_matchers::MatchFinder;21using dataflow::UncheckedOptionalAccessDiagnoser;22using dataflow::UncheckedOptionalAccessDiagnostic;23using dataflow::UncheckedOptionalAccessModel;24 25static constexpr llvm::StringLiteral FuncID("fun");26 27void UncheckedOptionalAccessCheck::registerMatchers(MatchFinder *Finder) {28  using namespace ast_matchers;29 30  auto HasOptionalCallDescendant = hasDescendant(callExpr(callee(cxxMethodDecl(31      ofClass(UncheckedOptionalAccessModel::optionalClassDecl())))));32  Finder->addMatcher(33      decl(anyOf(functionDecl(unless(isExpansionInSystemHeader()),34                              // FIXME: Remove the filter below when lambdas are35                              // well supported by the check.36                              unless(hasDeclContext(cxxRecordDecl(isLambda()))),37                              hasBody(HasOptionalCallDescendant)),38                 cxxConstructorDecl(hasAnyConstructorInitializer(39                     withInitializer(HasOptionalCallDescendant)))))40          .bind(FuncID),41      this);42}43 44void UncheckedOptionalAccessCheck::check(45    const MatchFinder::MatchResult &Result) {46  if (Result.SourceManager->getDiagnostics().hasUncompilableErrorOccurred())47    return;48 49  const auto *FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>(FuncID);50  if (FuncDecl->isTemplated())51    return;52 53  UncheckedOptionalAccessDiagnoser Diagnoser(ModelOptions);54  // FIXME: Allow user to set the (defaulted) SAT iterations max for55  // `diagnoseFunction` with config options.56  if (llvm::Expected<llvm::SmallVector<UncheckedOptionalAccessDiagnostic>>57          Diags = dataflow::diagnoseFunction<UncheckedOptionalAccessModel,58                                             UncheckedOptionalAccessDiagnostic>(59              *FuncDecl, *Result.Context, Diagnoser))60    for (const UncheckedOptionalAccessDiagnostic &Diag : *Diags) {61      diag(Diag.Range.getBegin(), "unchecked access to optional value")62          << Diag.Range;63    }64  else65    llvm::consumeError(Diags.takeError());66}67 68} // namespace clang::tidy::bugprone69