clang-tools 3.8.0

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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