clang-tools 6.0.0

StaticObjectExceptionCheck.cpp
Go to the documentation of this file.
1 //===--- StaticObjectExceptionCheck.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 "StaticObjectExceptionCheck.h"
11 #include "../utils/Matchers.h"
12 #include "clang/AST/ASTContext.h"
13 #include "clang/ASTMatchers/ASTMatchFinder.h"
14
15 using namespace clang::ast_matchers;
16
17 namespace clang {
18 namespace tidy {
19 namespace cert {
20
21 void StaticObjectExceptionCheck::registerMatchers(MatchFinder *Finder) {
22 if ((!getLangOpts().CPlusPlus) || (!getLangOpts().CXXExceptions))
23 return;
24
25 // Match any static or thread_local variable declaration that has an
26 // initializer that can throw.
27 Finder->addMatcher(
28 varDecl(anyOf(hasThreadStorageDuration(), hasStaticStorageDuration()),
29 unless(hasAncestor(functionDecl())),
30 anyOf(hasDescendant(cxxConstructExpr(hasDeclaration(
31 cxxConstructorDecl(unless(isNoThrow())).bind("func")))),
32 hasDescendant(cxxNewExpr(hasDeclaration(
33 functionDecl(unless(isNoThrow())).bind("func")))),
34 hasDescendant(callExpr(hasDeclaration(
35 functionDecl(unless(isNoThrow())).bind("func"))))))
36 .bind("var"),
37 this);
38 }
39
40 void StaticObjectExceptionCheck::check(const MatchFinder::MatchResult &Result) {
41 const auto *VD = Result.Nodes.getNodeAs<VarDecl>("var");
42 const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>("func");
43
44 diag(VD->getLocation(),
45 "initialization of %0 with %select{static|thread_local}1 storage "
46 "duration may throw an exception that cannot be caught")
47 << VD << (VD->getStorageDuration() == SD_Static ? 0 : 1);
48
49 SourceLocation FuncLocation = Func->getLocation();
50 if (FuncLocation.isValid()) {
51 diag(FuncLocation,
52 "possibly throwing %select{constructor|function}0 declared here",
53 DiagnosticIDs::Note)
54 << (isa<CXXConstructorDecl>(Func) ? 0 : 1);
55 }
56 }
57
58 } // namespace cert
59 } // namespace tidy
60 } // namespace clang
clang::ast_matchers
Definition: ClangTidyDiagnosticConsumer.h:27
StaticObjectExceptionCheck.h
clang
Definition: AndroidTidyModule.cpp:28