blob: c465ecc5996dddd3e814f82309457e5c6c119477 [file] [log] [blame]
Aaron Ballman43aef4c2015-12-01 14:05:39 +00001//===--- 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
15using namespace clang::ast_matchers;
16
17namespace clang {
18namespace tidy {
Aaron Ballman1284f042016-01-04 14:31:14 +000019namespace cert {
Aaron Ballman43aef4c2015-12-01 14:05:39 +000020
21void StaticObjectExceptionCheck::registerMatchers(MatchFinder *Finder) {
22 if (!getLangOpts().CPlusPlus)
23 return;
24
Malcolm Parsons2792dcc2016-10-31 22:47:04 +000025 // Match any static or thread_local variable declaration that has an
26 // initializer that can throw.
Aaron Ballman43aef4c2015-12-01 14:05:39 +000027 Finder->addMatcher(
28 varDecl(anyOf(hasThreadStorageDuration(), hasStaticStorageDuration()),
Aaron Ballman16057282016-09-26 15:00:45 +000029 unless(hasAncestor(functionDecl())),
Malcolm Parsons2792dcc2016-10-31 22:47:04 +000030 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 Ballman43aef4c2015-12-01 14:05:39 +000036 .bind("var"),
37 this);
38}
39
40void StaticObjectExceptionCheck::check(const MatchFinder::MatchResult &Result) {
41 const auto *VD = Result.Nodes.getNodeAs<VarDecl>("var");
Malcolm Parsons2792dcc2016-10-31 22:47:04 +000042 const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>("func");
Aaron Ballman43aef4c2015-12-01 14:05:39 +000043
44 diag(VD->getLocation(),
Malcolm Parsons2792dcc2016-10-31 22:47:04 +000045 "initialization of %0 with %select{static|thread_local}1 storage "
Aaron Ballman43aef4c2015-12-01 14:05:39 +000046 "duration may throw an exception that cannot be caught")
47 << VD << (VD->getStorageDuration() == SD_Static ? 0 : 1);
Malcolm Parsons2792dcc2016-10-31 22:47:04 +000048
49 SourceLocation FuncLocation = Func->getLocation();
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +000050 if (FuncLocation.isValid()) {
Malcolm Parsons2792dcc2016-10-31 22:47:04 +000051 diag(FuncLocation,
52 "possibly throwing %select{constructor|function}0 declared here",
53 DiagnosticIDs::Note)
54 << (isa<CXXConstructorDecl>(Func) ? 0 : 1);
55 }
Aaron Ballman43aef4c2015-12-01 14:05:39 +000056}
57
Aaron Ballman1284f042016-01-04 14:31:14 +000058} // namespace cert
Aaron Ballman43aef4c2015-12-01 14:05:39 +000059} // namespace tidy
60} // namespace clang