aboutsummaryrefslogtreecommitdiff
path: root/clang-tidy/bugprone
diff options
context:
space:
mode:
authorGabor Horvath <xazax.hun@gmail.com>2018-05-14 10:10:02 +0000
committerGabor Horvath <xazax.hun@gmail.com>2018-05-14 10:10:02 +0000
commitac3a34d04eea8743f625ccc4eea1d3e32e459291 (patch)
treecd2f14a7152a7ae30112afacd767a94816dbab7d /clang-tidy/bugprone
parent17f663b60574d91d30887d7173d89a7c3a4286af (diff)
[clang-tidy] Add terminating continue check
Patch by: Daniel Kolozsvari! Differential Revision: https://reviews.llvm.org/D33844 git-svn-id: https://llvm.org/svn/llvm-project/clang-tools-extra/trunk@332223 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'clang-tidy/bugprone')
-rw-r--r--clang-tidy/bugprone/BugproneTidyModule.cpp3
-rw-r--r--clang-tidy/bugprone/CMakeLists.txt1
-rw-r--r--clang-tidy/bugprone/TerminatingContinueCheck.cpp49
-rw-r--r--clang-tidy/bugprone/TerminatingContinueCheck.h36
4 files changed, 89 insertions, 0 deletions
diff --git a/clang-tidy/bugprone/BugproneTidyModule.cpp b/clang-tidy/bugprone/BugproneTidyModule.cpp
index 117f7fd3..b87961e0 100644
--- a/clang-tidy/bugprone/BugproneTidyModule.cpp
+++ b/clang-tidy/bugprone/BugproneTidyModule.cpp
@@ -40,6 +40,7 @@
#include "SuspiciousSemicolonCheck.h"
#include "SuspiciousStringCompareCheck.h"
#include "SwappedArgumentsCheck.h"
+#include "TerminatingContinueCheck.h"
#include "ThrowKeywordMissingCheck.h"
#include "UndefinedMemoryManipulationCheck.h"
#include "UndelegatedConstructorCheck.h"
@@ -115,6 +116,8 @@ public:
"bugprone-suspicious-string-compare");
CheckFactories.registerCheck<SwappedArgumentsCheck>(
"bugprone-swapped-arguments");
+ CheckFactories.registerCheck<TerminatingContinueCheck>(
+ "bugprone-terminating-continue");
CheckFactories.registerCheck<ThrowKeywordMissingCheck>(
"bugprone-throw-keyword-missing");
CheckFactories.registerCheck<UndefinedMemoryManipulationCheck>(
diff --git a/clang-tidy/bugprone/CMakeLists.txt b/clang-tidy/bugprone/CMakeLists.txt
index 75a74277..64b1e32d 100644
--- a/clang-tidy/bugprone/CMakeLists.txt
+++ b/clang-tidy/bugprone/CMakeLists.txt
@@ -32,6 +32,7 @@ add_clang_library(clangTidyBugproneModule
SuspiciousSemicolonCheck.cpp
SuspiciousStringCompareCheck.cpp
SwappedArgumentsCheck.cpp
+ TerminatingContinueCheck.cpp
ThrowKeywordMissingCheck.cpp
UndefinedMemoryManipulationCheck.cpp
UndelegatedConstructorCheck.cpp
diff --git a/clang-tidy/bugprone/TerminatingContinueCheck.cpp b/clang-tidy/bugprone/TerminatingContinueCheck.cpp
new file mode 100644
index 00000000..3e11e06c
--- /dev/null
+++ b/clang-tidy/bugprone/TerminatingContinueCheck.cpp
@@ -0,0 +1,49 @@
+//===--- TerminatingContinueCheck.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 "TerminatingContinueCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Lexer.h"
+#include "clang/Tooling/FixIt.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace bugprone {
+
+void TerminatingContinueCheck::registerMatchers(MatchFinder *Finder) {
+ const auto doWithFalse =
+ doStmt(hasCondition(ignoringImpCasts(
+ anyOf(cxxBoolLiteral(equals(false)), integerLiteral(equals(0)),
+ cxxNullPtrLiteralExpr(), gnuNullExpr()))),
+ equalsBoundNode("closestLoop"));
+
+ Finder->addMatcher(
+ continueStmt(hasAncestor(stmt(anyOf(forStmt(), whileStmt(),
+ cxxForRangeStmt(), doStmt()))
+ .bind("closestLoop")),
+ hasAncestor(doWithFalse))
+ .bind("continue"),
+ this);
+}
+
+void TerminatingContinueCheck::check(const MatchFinder::MatchResult &Result) {
+ const auto *ContStmt = Result.Nodes.getNodeAs<ContinueStmt>("continue");
+
+ auto Diag =
+ diag(ContStmt->getLocStart(),
+ "'continue' in loop with false condition is equivalent to 'break'")
+ << tooling::fixit::createReplacement(*ContStmt, "break");
+}
+
+} // namespace bugprone
+} // namespace tidy
+} // namespace clang
diff --git a/clang-tidy/bugprone/TerminatingContinueCheck.h b/clang-tidy/bugprone/TerminatingContinueCheck.h
new file mode 100644
index 00000000..5985693c
--- /dev/null
+++ b/clang-tidy/bugprone/TerminatingContinueCheck.h
@@ -0,0 +1,36 @@
+//===--- TerminatingContinueCheck.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_TERMINATINGCONTINUECHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_TERMINATINGCONTINUECHECK_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace bugprone {
+
+/// Checks if a 'continue' statement terminates the loop (i.e. the loop has
+/// a condition which always evaluates to false).
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone-terminating-continue.html
+class TerminatingContinueCheck : public ClangTidyCheck {
+public:
+ TerminatingContinueCheck(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_TERMINATINGCONTINUECHECK_H