brintos

brintos / llvm-project-archived public Read only

0
0
Text · 5.6 KiB · b7de839 Raw
145 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 "ExceptionEscapeCheck.h"10 11#include "clang/ASTMatchers/ASTMatchFinder.h"12#include "llvm/ADT/StringSet.h"13 14using namespace clang::ast_matchers;15 16namespace clang::tidy::bugprone {17namespace {18 19AST_MATCHER_P(FunctionDecl, isEnabled, llvm::StringSet<>,20              FunctionsThatShouldNotThrow) {21  return FunctionsThatShouldNotThrow.contains(Node.getNameAsString());22}23 24AST_MATCHER(FunctionDecl, isExplicitThrow) {25  return isExplicitThrowExceptionSpec(Node.getExceptionSpecType()) &&26         Node.getExceptionSpecSourceRange().isValid();27}28 29AST_MATCHER(FunctionDecl, hasAtLeastOneParameter) {30  return Node.getNumParams() > 0;31}32 33} // namespace34 35ExceptionEscapeCheck::ExceptionEscapeCheck(StringRef Name,36                                           ClangTidyContext *Context)37    : ClangTidyCheck(Name, Context), RawFunctionsThatShouldNotThrow(Options.get(38                                         "FunctionsThatShouldNotThrow", "")),39      RawIgnoredExceptions(Options.get("IgnoredExceptions", "")),40      RawCheckedSwapFunctions(41          Options.get("CheckedSwapFunctions", "swap,iter_swap,iter_move")),42      CheckDestructors(Options.get("CheckDestructors", true)),43      CheckMoveMemberFunctions(Options.get("CheckMoveMemberFunctions", true)),44      CheckMain(Options.get("CheckMain", true)),45      CheckNothrowFunctions(Options.get("CheckNothrowFunctions", true)) {46  llvm::SmallVector<StringRef, 8> FunctionsThatShouldNotThrowVec,47      IgnoredExceptionsVec, CheckedSwapFunctionsVec;48  RawFunctionsThatShouldNotThrow.split(FunctionsThatShouldNotThrowVec, ",", -1,49                                       false);50  FunctionsThatShouldNotThrow.insert_range(FunctionsThatShouldNotThrowVec);51 52  RawCheckedSwapFunctions.split(CheckedSwapFunctionsVec, ",", -1, false);53  CheckedSwapFunctions.insert_range(CheckedSwapFunctionsVec);54 55  llvm::StringSet<> IgnoredExceptions;56  RawIgnoredExceptions.split(IgnoredExceptionsVec, ",", -1, false);57  IgnoredExceptions.insert_range(IgnoredExceptionsVec);58  Tracer.ignoreExceptions(std::move(IgnoredExceptions));59  Tracer.ignoreBadAlloc(true);60}61 62void ExceptionEscapeCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {63  Options.store(Opts, "FunctionsThatShouldNotThrow",64                RawFunctionsThatShouldNotThrow);65  Options.store(Opts, "IgnoredExceptions", RawIgnoredExceptions);66  Options.store(Opts, "CheckedSwapFunctions", RawCheckedSwapFunctions);67  Options.store(Opts, "CheckDestructors", CheckDestructors);68  Options.store(Opts, "CheckMoveMemberFunctions", CheckMoveMemberFunctions);69  Options.store(Opts, "CheckMain", CheckMain);70  Options.store(Opts, "CheckNothrowFunctions", CheckNothrowFunctions);71}72 73void ExceptionEscapeCheck::registerMatchers(MatchFinder *Finder) {74  auto MatchIf = [](bool Enabled, const auto &Matcher) {75    ast_matchers::internal::Matcher<FunctionDecl> Nothing = unless(anything());76    return Enabled ? Matcher : Nothing;77  };78  Finder->addMatcher(79      functionDecl(80          isDefinition(),81          anyOf(82              MatchIf(CheckNothrowFunctions, isNoThrow()),83              allOf(anyOf(MatchIf(CheckDestructors, cxxDestructorDecl()),84                          MatchIf(85                              CheckMoveMemberFunctions,86                              anyOf(cxxConstructorDecl(isMoveConstructor()),87                                    cxxMethodDecl(isMoveAssignmentOperator()))),88                          MatchIf(CheckMain, isMain()),89                          allOf(isEnabled(CheckedSwapFunctions),90                                hasAtLeastOneParameter())),91                    unless(isExplicitThrow())),92              isEnabled(FunctionsThatShouldNotThrow)))93          .bind("thrower"),94      this);95}96 97void ExceptionEscapeCheck::check(const MatchFinder::MatchResult &Result) {98  const auto *MatchedDecl = Result.Nodes.getNodeAs<FunctionDecl>("thrower");99 100  if (!MatchedDecl)101    return;102 103  const utils::ExceptionAnalyzer::ExceptionInfo Info =104      Tracer.analyze(MatchedDecl);105 106  if (Info.getBehaviour() != utils::ExceptionAnalyzer::State::Throwing)107    return;108 109  diag(MatchedDecl->getLocation(), "an exception may be thrown in function "110                                   "%0 which should not throw exceptions")111      << MatchedDecl;112 113  const auto &[ThrowType, ThrowInfo] = *Info.getExceptions().begin();114 115  if (ThrowInfo.Loc.isInvalid())116    return;117 118  const utils::ExceptionAnalyzer::CallStack &Stack = ThrowInfo.Stack;119  diag(ThrowInfo.Loc,120       "frame #0: unhandled exception of type %0 may be thrown in function %1 "121       "here",122       DiagnosticIDs::Note)123      << QualType(ThrowType, 0U) << Stack.back().first;124 125  size_t FrameNo = 1;126  for (auto CurrIt = ++Stack.rbegin(), PrevIt = Stack.rbegin();127       CurrIt != Stack.rend(); ++CurrIt, ++PrevIt) {128    const FunctionDecl *CurrFunction = CurrIt->first;129    const FunctionDecl *PrevFunction = PrevIt->first;130    const SourceLocation PrevLocation = PrevIt->second;131    if (PrevLocation.isValid()) {132      diag(PrevLocation, "frame #%0: function %1 calls function %2 here",133           DiagnosticIDs::Note)134          << FrameNo << CurrFunction << PrevFunction;135    } else {136      diag(CurrFunction->getLocation(),137           "frame #%0: function %1 calls function %2", DiagnosticIDs::Note)138          << FrameNo << CurrFunction << PrevFunction;139    }140    ++FrameNo;141  }142}143 144} // namespace clang::tidy::bugprone145