summaryrefslogtreecommitdiff
path: root/Utilities/ClangTidyModule/UseCmstrlenCheck.cxx
blob: 590d260b4e9e18a8cc1d1614b32a14075f1c42d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
#include "UseCmstrlenCheck.h"

#include <clang/ASTMatchers/ASTMatchFinder.h>

namespace clang {
namespace tidy {
namespace cmake {
using namespace ast_matchers;

UseCmstrlenCheck::UseCmstrlenCheck(StringRef Name, ClangTidyContext* Context)
  : ClangTidyCheck(Name, Context)
{
}

void UseCmstrlenCheck::registerMatchers(MatchFinder* Finder)
{
  Finder->addMatcher(callExpr(callee(functionDecl(hasName("::strlen"))),
                              callee(expr().bind("callee")),
                              hasArgument(0, stringLiteral())),
                     this);
}

void UseCmstrlenCheck::check(const MatchFinder::MatchResult& Result)
{
  const Expr* Node = Result.Nodes.getNodeAs<Expr>("callee");

  this->diag(Node->getBeginLoc(), "use cmStrLen() for string literals")
    << FixItHint::CreateReplacement(Node->getSourceRange(), "cmStrLen");
}
}
}
}