diff options
| author | Gabor Horvath <xazax.hun@gmail.com> | 2018-02-15 09:08:51 +0000 |
|---|---|---|
| committer | Gabor Horvath <xazax.hun@gmail.com> | 2018-02-15 09:08:51 +0000 |
| commit | c23f924b752151a59694146225261b17d68693d7 (patch) | |
| tree | 7eef1ac3e0f63fbc5e71907ee1854879aa0efbd9 /clang-tools-extra/clang-tidy/bugprone | |
| parent | ce4f0af3023fb3e023030d83998e3055545f13d0 (diff) | |
| download | bcm5719-llvm-c23f924b752151a59694146225261b17d68693d7.tar.gz bcm5719-llvm-c23f924b752151a59694146225261b17d68693d7.zip | |
[clang-tidy] New checker for exceptions that are created but not thrown
Patch by: Kristof Umann
Differential Revision: https://reviews.llvm.org/D43120
llvm-svn: 325222
Diffstat (limited to 'clang-tools-extra/clang-tidy/bugprone')
4 files changed, 92 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp index 3b7314298d7..68effd14c88 100644 --- a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp @@ -25,6 +25,7 @@ #include "MultipleStatementMacroCheck.h" #include "StringConstructorCheck.h" #include "SuspiciousMemsetUsageCheck.h" +#include "ThrowKeywordMissingCheck.h" #include "UndefinedMemoryManipulationCheck.h" #include "UseAfterMoveCheck.h" #include "VirtualNearMissCheck.h" @@ -66,6 +67,8 @@ public: "bugprone-string-constructor"); CheckFactories.registerCheck<SuspiciousMemsetUsageCheck>( "bugprone-suspicious-memset-usage"); + CheckFactories.registerCheck<ThrowKeywordMissingCheck>( + "bugprone-throw-keyword-missing"); CheckFactories.registerCheck<UndefinedMemoryManipulationCheck>( "bugprone-undefined-memory-manipulation"); CheckFactories.registerCheck<UseAfterMoveCheck>( diff --git a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt index 11b66c64da3..54d93ef2de1 100644 --- a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt @@ -17,6 +17,7 @@ add_clang_library(clangTidyBugproneModule MultipleStatementMacroCheck.cpp StringConstructorCheck.cpp SuspiciousMemsetUsageCheck.cpp + ThrowKeywordMissingCheck.cpp UndefinedMemoryManipulationCheck.cpp UseAfterMoveCheck.cpp VirtualNearMissCheck.cpp diff --git a/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp new file mode 100644 index 00000000000..350cf3bc61b --- /dev/null +++ b/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp @@ -0,0 +1,52 @@ +//===--- ThrowKeywordMissingCheck.cpp - clang-tidy-------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "ThrowKeywordMissingCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace bugprone { + +void ThrowKeywordMissingCheck::registerMatchers(MatchFinder *Finder) { + if (!getLangOpts().CPlusPlus) + return; + + auto CtorInitializerList = + cxxConstructorDecl(hasAnyConstructorInitializer(anything())); + + Finder->addMatcher( + expr(anyOf(cxxFunctionalCastExpr(), cxxBindTemporaryExpr(), + cxxTemporaryObjectExpr()), + hasType(cxxRecordDecl( + isSameOrDerivedFrom(matchesName("[Ee]xception|EXCEPTION")))), + unless(anyOf(hasAncestor(stmt( + anyOf(cxxThrowExpr(), callExpr(), returnStmt()))), + hasAncestor(varDecl()), + allOf(hasAncestor(CtorInitializerList), + unless(hasAncestor(cxxCatchStmt())))))) + .bind("temporary-exception-not-thrown"), + this); +} + +void ThrowKeywordMissingCheck::check(const MatchFinder::MatchResult &Result) { + const auto *TemporaryExpr = + Result.Nodes.getNodeAs<Expr>("temporary-exception-not-thrown"); + + diag(TemporaryExpr->getLocStart(), "suspicious exception object created but " + "not thrown; did you mean 'throw %0'?") + << TemporaryExpr->getType().getBaseTypeIdentifier()->getName(); +} + +} // namespace bugprone +} // namespace tidy +} // namespace clang diff --git a/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.h b/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.h new file mode 100644 index 00000000000..cc57083c40f --- /dev/null +++ b/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.h @@ -0,0 +1,36 @@ +//===--- ThrowKeywordMissingCheck.h - clang-tidy-----------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_THROWKEYWORDMISSINGCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_THROWKEYWORDMISSINGCHECK_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace bugprone { + +/// Emits a warning about temporary objects whose type is (or is derived from) a +/// class that has 'EXCEPTION', 'Exception' or 'exception' in its name. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone-throw-keyword-missing.html +class ThrowKeywordMissingCheck : public ClangTidyCheck { +public: + ThrowKeywordMissingCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace bugprone +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_THROWKEYWORDMISSINGCHECK_H |

