diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidDoWhileCheck.h b/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidDoWhileCheck.h new file mode 100644 --- /dev/null +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidDoWhileCheck.h @@ -0,0 +1,37 @@ +//===--- AvoidDoWhileCheck.h - clang-tidy -----------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_AVOIDDOWHILECHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_AVOIDDOWHILECHECK_H + +#include "../ClangTidyCheck.h" + +namespace clang { +namespace tidy { +namespace cppcoreguidelines { + +/// Checks if a do-while loop exists and flags it. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-avoid-do-while.html +class AvoidDoWhileCheck : public ClangTidyCheck { +public: + AvoidDoWhileCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { + return LangOpts.CPlusPlus; + } + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace cppcoreguidelines +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_AVOIDDOWHILECHECK_H diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidDoWhileCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidDoWhileCheck.cpp new file mode 100644 --- /dev/null +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidDoWhileCheck.cpp @@ -0,0 +1,42 @@ +//===--- AvoidDoWhileCheck.cpp - clang-tidy -------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "AvoidDoWhileCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace cppcoreguidelines { + +AST_MATCHER(DoStmt, isInMacro) { return Node.getBeginLoc().isMacroID(); } + +void AvoidDoWhileCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + doStmt(unless(allOf( + isInMacro(), + hasCondition(ignoringImpCasts(anyOf( + cxxBoolLiteral(equals(false)), integerLiteral(equals(0)), + cxxNullPtrLiteralExpr(), gnuNullExpr())))))) + .bind("doStmt"), + this); +} + +void AvoidDoWhileCheck::check(const MatchFinder::MatchResult &Result) { + const auto *MatchedDoStmt = Result.Nodes.getNodeAs("doStmt"); + + diag(MatchedDoStmt->getDoLoc(), + "Try to avoid using do-while loops in terms of readability. Consider " + "using a while loop instead."); +} + +} // namespace cppcoreguidelines +} // namespace tidy +} // namespace clang diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/CMakeLists.txt b/clang-tools-extra/clang-tidy/cppcoreguidelines/CMakeLists.txt --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/CMakeLists.txt @@ -4,6 +4,7 @@ ) add_clang_library(clangTidyCppCoreGuidelinesModule + AvoidDoWhileCheck.cpp AvoidGotoCheck.cpp AvoidNonConstGlobalVariablesCheck.cpp CppCoreGuidelinesTidyModule.cpp diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp @@ -14,6 +14,7 @@ #include "../modernize/AvoidCArraysCheck.h" #include "../modernize/UseOverrideCheck.h" #include "../readability/MagicNumbersCheck.h" +#include "AvoidDoWhileCheck.h" #include "AvoidGotoCheck.h" #include "AvoidNonConstGlobalVariablesCheck.h" #include "InitVariablesCheck.h" @@ -46,6 +47,8 @@ void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { CheckFactories.registerCheck( "cppcoreguidelines-avoid-c-arrays"); + CheckFactories.registerCheck( + "cppcoreguidelines-avoid-do-while"); CheckFactories.registerCheck( "cppcoreguidelines-avoid-goto"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -108,6 +108,11 @@ Finds inner loops that have not been unrolled, as well as fully unrolled loops with unknown loops bounds or a large number of iterations. +- New :doc:`cppcoreguidelines-avoid-do-while + ` check. + + Checks if a ``do-while`` loop exists and flags it. + - New :doc:`cppcoreguidelines-prefer-member-initializer ` check. diff --git a/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines-avoid-do-while.rst b/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines-avoid-do-while.rst new file mode 100644 --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines-avoid-do-while.rst @@ -0,0 +1,36 @@ +.. title:: clang-tidy - cppcoreguidelines-avoid-do-while + +cppcoreguidelines-avoid-do-while +================================ + +Checks if a ``do-while`` loop exists and flags it. + +Using a ``while`` loop instead of a ``do-while`` could improve readability and +prevents overlooking the condition at the end. + +Usages of ``do-while`` loops inside a macro definition are not flagged by this +check if the condition is either ``false``, ``0``, ``nullptr`` or ``__null``. + +.. code-block:: c++ + + void func1() { + int I{0}; + const int Limit{10}; + + // Consider using a while loop + do { + I++; + } while (I <= Limit); + } + + void func2() { + int I{0}; + const int Limit{10}; + + // Better + while (I <= Limit) { + I++; + } + } + +This check implements rule `ES.75 `_ of the C++ Core Guidelines. diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -144,6 +144,7 @@ `clang-analyzer-valist.Unterminated `_, `concurrency-mt-unsafe `_, `concurrency-thread-canceltype-asynchronous `_, + `cppcoreguidelines-avoid-do-while `_, `cppcoreguidelines-avoid-goto `_, `cppcoreguidelines-avoid-non-const-global-variables `_, `cppcoreguidelines-init-variables `_, "Yes" diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-avoid-do-while.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-avoid-do-while.cpp new file mode 100644 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-avoid-do-while.cpp @@ -0,0 +1,87 @@ +// RUN: %check_clang_tidy %s cppcoreguidelines-avoid-do-while %t + +void func1() { + int I{0}; + const int Limit{10}; + + // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: Try to avoid using do-while loops in terms of readability. Consider using a while loop instead. [cppcoreguidelines-avoid-do-while] + do { + I++; + } while (I <= Limit); +} + +void func2() { + int I{0}; + const int Limit{10}; + + // OK + while (I <= Limit) { + I++; + } +} + +void func3() { +#define MACRO \ + do { \ + } while (true) + + // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: Try to avoid using do-while loops in terms of readability. Consider using a while loop instead. [cppcoreguidelines-avoid-do-while] + MACRO; + +#undef MACRO +} + +void func4() { +#define MACRO \ + do { \ + } while (1) + + // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: Try to avoid using do-while loops in terms of readability. Consider using a while loop instead. [cppcoreguidelines-avoid-do-while] + MACRO; + +#undef MACRO +} + +void func5() { +#define MACRO \ + do { \ + } while (false) + + // OK + MACRO; + +#undef MACRO +} + +void func6() { +#define MACRO \ + do { \ + } while (0) + + // OK + MACRO; + +#undef MACRO +} + +void func7() { +#define MACRO \ + do { \ + } while (nullptr) + + // OK + MACRO; + +#undef MACRO +} + +void func8() { +#define MACRO \ + do { \ + } while (__null) + + // OK + MACRO; + +#undef MACRO +}