clang-tools  14.0.0git
NoEscapeCheck.cpp
Go to the documentation of this file.
1 //===--- NoEscapeCheck.cpp - clang-tidy -----------------------------------===//
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 
9 #include "NoEscapeCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 
13 using namespace clang::ast_matchers;
14 
15 namespace clang {
16 namespace tidy {
17 namespace bugprone {
18 
19 void NoEscapeCheck::registerMatchers(MatchFinder *Finder) {
20  Finder->addMatcher(callExpr(callee(functionDecl(hasName("::dispatch_async"))),
21  argumentCountIs(2),
22  hasArgument(1, blockExpr().bind("arg-block"))),
23  this);
24  Finder->addMatcher(callExpr(callee(functionDecl(hasName("::dispatch_after"))),
25  argumentCountIs(3),
26  hasArgument(2, blockExpr().bind("arg-block"))),
27  this);
28 }
29 
30 void NoEscapeCheck::check(const MatchFinder::MatchResult &Result) {
31  const auto *MatchedEscapingBlock =
32  Result.Nodes.getNodeAs<BlockExpr>("arg-block");
33  const BlockDecl *EscapingBlockDecl = MatchedEscapingBlock->getBlockDecl();
34  for (const BlockDecl::Capture &CapturedVar : EscapingBlockDecl->captures()) {
35  const VarDecl *Var = CapturedVar.getVariable();
36  if (Var && Var->hasAttr<NoEscapeAttr>()) {
37  // FIXME: Add a method to get the location of the use of a CapturedVar so
38  // that we can diagnose the use of the pointer instead of the block.
39  diag(MatchedEscapingBlock->getBeginLoc(),
40  "pointer %0 with attribute 'noescape' is captured by an "
41  "asynchronously-executed block")
42  << Var;
43  diag(Var->getBeginLoc(), "the 'noescape' attribute is declared here.",
44  DiagnosticIDs::Note);
45  }
46  }
47 }
48 
49 } // namespace bugprone
50 } // namespace tidy
51 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:14
NoEscapeCheck.h
clang::clangd::check
bool check(llvm::StringRef File, llvm::function_ref< bool(const Position &)> ShouldCheckLine, const ThreadsafeFS &TFS, const ClangdLSPServer::Options &Opts, bool EnableCodeCompletion)
Definition: Check.cpp:259
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27