xref: /llvm-project/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidDoWhileCheck.cpp (revision 7d2ea6c422d3f5712b7253407005e1a465a76946)
11ae33bf4SCarlos Galvez //===--- AvoidDoWhileCheck.cpp - clang-tidy -------------------------------===//
21ae33bf4SCarlos Galvez //
31ae33bf4SCarlos Galvez // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
41ae33bf4SCarlos Galvez // See https://llvm.org/LICENSE.txt for license information.
51ae33bf4SCarlos Galvez // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
61ae33bf4SCarlos Galvez //
71ae33bf4SCarlos Galvez //===----------------------------------------------------------------------===//
81ae33bf4SCarlos Galvez 
91ae33bf4SCarlos Galvez #include "AvoidDoWhileCheck.h"
101ae33bf4SCarlos Galvez #include "clang/AST/ASTContext.h"
111ae33bf4SCarlos Galvez #include "clang/ASTMatchers/ASTMatchFinder.h"
121ae33bf4SCarlos Galvez 
131ae33bf4SCarlos Galvez using namespace clang::ast_matchers;
141ae33bf4SCarlos Galvez 
15*7d2ea6c4SCarlos Galvez namespace clang::tidy::cppcoreguidelines {
161ae33bf4SCarlos Galvez 
AvoidDoWhileCheck(StringRef Name,ClangTidyContext * Context)171ae33bf4SCarlos Galvez AvoidDoWhileCheck::AvoidDoWhileCheck(StringRef Name, ClangTidyContext *Context)
181ae33bf4SCarlos Galvez     : ClangTidyCheck(Name, Context),
191ae33bf4SCarlos Galvez       IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", false)) {}
201ae33bf4SCarlos Galvez 
storeOptions(ClangTidyOptions::OptionMap & Opts)211ae33bf4SCarlos Galvez void AvoidDoWhileCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
221ae33bf4SCarlos Galvez   Options.store(Opts, "IgnoreMacros", IgnoreMacros);
231ae33bf4SCarlos Galvez }
241ae33bf4SCarlos Galvez 
registerMatchers(MatchFinder * Finder)251ae33bf4SCarlos Galvez void AvoidDoWhileCheck::registerMatchers(MatchFinder *Finder) {
261ae33bf4SCarlos Galvez   Finder->addMatcher(doStmt().bind("x"), this);
271ae33bf4SCarlos Galvez }
281ae33bf4SCarlos Galvez 
check(const MatchFinder::MatchResult & Result)291ae33bf4SCarlos Galvez void AvoidDoWhileCheck::check(const MatchFinder::MatchResult &Result) {
301ae33bf4SCarlos Galvez   if (const auto *MatchedDecl = Result.Nodes.getNodeAs<DoStmt>("x")) {
311ae33bf4SCarlos Galvez     if (IgnoreMacros && MatchedDecl->getBeginLoc().isMacroID())
321ae33bf4SCarlos Galvez       return;
331ae33bf4SCarlos Galvez     diag(MatchedDecl->getBeginLoc(), "avoid do-while loops");
341ae33bf4SCarlos Galvez   }
351ae33bf4SCarlos Galvez }
361ae33bf4SCarlos Galvez 
37*7d2ea6c4SCarlos Galvez } // namespace clang::tidy::cppcoreguidelines
38