clang-tools  9.0.0svn
SetLongJmpCheck.cpp
Go to the documentation of this file.
1 //===--- SetLongJmpCheck.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 "SetLongJmpCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Frontend/CompilerInstance.h"
13 #include "clang/Lex/PPCallbacks.h"
14 #include "clang/Lex/Preprocessor.h"
15 
16 using namespace clang::ast_matchers;
17 
18 namespace clang {
19 namespace tidy {
20 namespace cert {
21 
22 const char SetLongJmpCheck::DiagWording[] =
23  "do not call %0; consider using exception handling instead";
24 
25 namespace {
26 class SetJmpMacroCallbacks : public PPCallbacks {
27  SetLongJmpCheck &Check;
28 
29 public:
30  explicit SetJmpMacroCallbacks(SetLongJmpCheck &Check) : Check(Check) {}
31 
32  void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
33  SourceRange Range, const MacroArgs *Args) override {
34  const auto *II = MacroNameTok.getIdentifierInfo();
35  if (!II)
36  return;
37 
38  if (II->getName() == "setjmp")
39  Check.diag(Range.getBegin(), Check.DiagWording) << II;
40  }
41 };
42 } // namespace
43 
44 void SetLongJmpCheck::registerPPCallbacks(CompilerInstance &Compiler) {
45  // This checker only applies to C++, where exception handling is a superior
46  // solution to setjmp/longjmp calls.
47  if (!getLangOpts().CPlusPlus)
48  return;
49 
50  // Per [headers]p5, setjmp must be exposed as a macro instead of a function,
51  // despite the allowance in C for setjmp to also be an extern function.
52  Compiler.getPreprocessor().addPPCallbacks(
53  llvm::make_unique<SetJmpMacroCallbacks>(*this));
54 }
55 
56 void SetLongJmpCheck::registerMatchers(MatchFinder *Finder) {
57  // This checker only applies to C++, where exception handling is a superior
58  // solution to setjmp/longjmp calls.
59  if (!getLangOpts().CPlusPlus)
60  return;
61 
62  // In case there is an implementation that happens to define setjmp as a
63  // function instead of a macro, this will also catch use of it. However, we
64  // are primarily searching for uses of longjmp.
65  Finder->addMatcher(callExpr(callee(functionDecl(anyOf(hasName("setjmp"),
66  hasName("longjmp")))))
67  .bind("expr"),
68  this);
69 }
70 
71 void SetLongJmpCheck::check(const MatchFinder::MatchResult &Result) {
72  const auto *E = Result.Nodes.getNodeAs<CallExpr>("expr");
73  diag(E->getExprLoc(), DiagWording) << cast<NamedDecl>(E->getCalleeDecl());
74 }
75 
76 } // namespace cert
77 } // namespace tidy
78 } // namespace clang
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static GeneratorRegistry::Add< MDGenerator > MD(MDGenerator::Format, "Generator for MD output.")
CharSourceRange Range
SourceRange for the file name.