Aaron Ballman | 43aef4c | 2015-12-01 14:05:39 +0000 | [diff] [blame] | 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 { |
Aaron Ballman | 1284f04 | 2016-01-04 14:31:14 +0000 | [diff] [blame] | 19 | namespace cert { |
Aaron Ballman | 43aef4c | 2015-12-01 14:05:39 +0000 | [diff] [blame] | 20 | |
| 21 | void StaticObjectExceptionCheck::registerMatchers(MatchFinder *Finder) { |
| 22 | if (!getLangOpts().CPlusPlus) |
| 23 | return; |
| 24 | |
Malcolm Parsons | 2792dcc | 2016-10-31 22:47:04 +0000 | [diff] [blame] | 25 | // Match any static or thread_local variable declaration that has an |
| 26 | // initializer that can throw. |
Aaron Ballman | 43aef4c | 2015-12-01 14:05:39 +0000 | [diff] [blame] | 27 | Finder->addMatcher( |
| 28 | varDecl(anyOf(hasThreadStorageDuration(), hasStaticStorageDuration()), |
Aaron Ballman | 1605728 | 2016-09-26 15:00:45 +0000 | [diff] [blame] | 29 | unless(hasAncestor(functionDecl())), |
Malcolm Parsons | 2792dcc | 2016-10-31 22:47:04 +0000 | [diff] [blame] | 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")))))) |
Aaron Ballman | 43aef4c | 2015-12-01 14:05:39 +0000 | [diff] [blame] | 36 | .bind("var"), |
| 37 | this); |
| 38 | } |
| 39 | |
| 40 | void StaticObjectExceptionCheck::check(const MatchFinder::MatchResult &Result) { |
| 41 | const auto *VD = Result.Nodes.getNodeAs<VarDecl>("var"); |
Malcolm Parsons | 2792dcc | 2016-10-31 22:47:04 +0000 | [diff] [blame] | 42 | const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>("func"); |
Aaron Ballman | 43aef4c | 2015-12-01 14:05:39 +0000 | [diff] [blame] | 43 | |
| 44 | diag(VD->getLocation(), |
Malcolm Parsons | 2792dcc | 2016-10-31 22:47:04 +0000 | [diff] [blame] | 45 | "initialization of %0 with %select{static|thread_local}1 storage " |
Aaron Ballman | 43aef4c | 2015-12-01 14:05:39 +0000 | [diff] [blame] | 46 | "duration may throw an exception that cannot be caught") |
| 47 | << VD << (VD->getStorageDuration() == SD_Static ? 0 : 1); |
Malcolm Parsons | 2792dcc | 2016-10-31 22:47:04 +0000 | [diff] [blame] | 48 | |
| 49 | SourceLocation FuncLocation = Func->getLocation(); |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 50 | if (FuncLocation.isValid()) { |
Malcolm Parsons | 2792dcc | 2016-10-31 22:47:04 +0000 | [diff] [blame] | 51 | diag(FuncLocation, |
| 52 | "possibly throwing %select{constructor|function}0 declared here", |
| 53 | DiagnosticIDs::Note) |
| 54 | << (isa<CXXConstructorDecl>(Func) ? 0 : 1); |
| 55 | } |
Aaron Ballman | 43aef4c | 2015-12-01 14:05:39 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Aaron Ballman | 1284f04 | 2016-01-04 14:31:14 +0000 | [diff] [blame] | 58 | } // namespace cert |
Aaron Ballman | 43aef4c | 2015-12-01 14:05:39 +0000 | [diff] [blame] | 59 | } // namespace tidy |
| 60 | } // namespace clang |