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 20 void StaticObjectExceptionCheck::registerMatchers(MatchFinder *Finder) { 21 if (!getLangOpts().CPlusPlus) 22 return; 23 24 // Match any static or thread_local variable declaration that is initialized 25 // with a constructor that can throw. 26 Finder->addMatcher( 27 varDecl(anyOf(hasThreadStorageDuration(), hasStaticStorageDuration()), 28 hasInitializer(cxxConstructExpr(hasDeclaration( 29 cxxConstructorDecl(unless(isNoThrow())) 30 .bind("ctor"))))) 31 .bind("var"), 32 this); 33 } 34 35 void StaticObjectExceptionCheck::check(const MatchFinder::MatchResult &Result) { 36 const auto *VD = Result.Nodes.getNodeAs<VarDecl>("var"); 37 const auto *Ctor = Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor"); 38 39 diag(VD->getLocation(), 40 "construction of %0 with %select{static|thread_local}1 storage " 41 "duration may throw an exception that cannot be caught") 42 << VD << (VD->getStorageDuration() == SD_Static ? 0 : 1); 43 diag(Ctor->getLocation(), "possibly throwing constructor declared here", 44 DiagnosticIDs::Note); 45 } 46 47 } // namespace tidy 48 } // namespace clang 49 50