File: StringConcatenationUseCmstrcatCheck.cxx

package info (click to toggle)
cmake 4.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 152,348 kB
  • sloc: ansic: 403,894; cpp: 303,807; sh: 4,097; python: 3,582; yacc: 3,106; lex: 1,279; f90: 538; asm: 471; lisp: 375; cs: 270; java: 266; fortran: 239; objc: 215; perl: 213; xml: 198; makefile: 108; javascript: 83; pascal: 63; tcl: 55; php: 25; ruby: 22
file content (180 lines) | stat: -rw-r--r-- 5,966 bytes parent folder | download
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file LICENSE.rst or https://cmake.org/licensing for details.  */
#include "StringConcatenationUseCmstrcatCheck.h"

#include <cassert>

#include <clang/ASTMatchers/ASTMatchFinder.h>
#include <clang/Lex/Lexer.h>

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

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

void StringConcatenationUseCmstrcatCheck::registerMatchers(MatchFinder* Finder)
{
  auto IsString = expr(hasType(qualType(hasUnqualifiedDesugaredType(
    recordType(hasDeclaration(classTemplateSpecializationDecl(
      hasName("::std::basic_string"),
      hasTemplateArgument(
        0, templateArgument(refersToType(asString("char")))))))))));

  auto IsChar = expr(hasType(asString("char")));

  auto IsCharPtr = expr(hasType(pointerType(pointee(asString("const char")))));

  auto IsStringConcat =
    cxxOperatorCallExpr(hasOperatorName("+"),
                        anyOf(allOf(hasLHS(IsString), hasRHS(IsString)),
                              allOf(hasLHS(IsString), hasRHS(IsChar)),
                              allOf(hasLHS(IsString), hasRHS(IsCharPtr)),
                              allOf(hasLHS(IsChar), hasRHS(IsString)),
                              allOf(hasLHS(IsCharPtr), hasRHS(IsString))));

  auto IsStringAppend = cxxOperatorCallExpr(
    hasOperatorName("+="), hasLHS(IsString),
    anyOf(hasRHS(IsString), hasRHS(IsChar), hasRHS(IsCharPtr)));

  auto IsStringConcatWithLHS =
    cxxOperatorCallExpr(
      IsStringConcat,
      optionally(hasLHS(materializeTemporaryExpr(
        has(cxxBindTemporaryExpr(has(IsStringConcat.bind("lhs"))))))))
      .bind("concat");

  auto IsStringAppendWithRHS =
    cxxOperatorCallExpr(
      IsStringAppend,
      optionally(hasRHS(materializeTemporaryExpr(has(implicitCastExpr(
        has(cxxBindTemporaryExpr(has(IsStringConcat.bind("rhs"))))))))))
      .bind("append");

  Finder->addMatcher(IsStringConcatWithLHS, this);
  Finder->addMatcher(IsStringAppendWithRHS, this);
}

void StringConcatenationUseCmstrcatCheck::check(
  MatchFinder::MatchResult const& Result)
{
  CXXOperatorCallExpr const* AppendNode =
    Result.Nodes.getNodeAs<CXXOperatorCallExpr>("append");
  CXXOperatorCallExpr const* ConcatNode =
    Result.Nodes.getNodeAs<CXXOperatorCallExpr>("concat");

  if (AppendNode != nullptr) {
    if (AppendNode->getBeginLoc().isValid()) {
      assert(InProgressExprChains.find(AppendNode) ==
             InProgressExprChains.end());

      ExprChain TmpExprChain =
        std::make_pair(OperatorType::PlusEquals,
                       std::vector<CXXOperatorCallExpr const*>{ AppendNode });
      CXXOperatorCallExpr const* RHSNode =
        Result.Nodes.getNodeAs<CXXOperatorCallExpr>("rhs");

      if (RHSNode != nullptr) {
        if (RHSNode->getBeginLoc().isValid()) {
          InProgressExprChains[RHSNode] = std::move(TmpExprChain);
        }
      } else {
        issueCorrection(TmpExprChain, Result);
      }
    }
  }

  if (ConcatNode != nullptr) {
    if (ConcatNode->getBeginLoc().isValid()) {
      ExprChain TmpExprChain;

      if (!(InProgressExprChains.find(ConcatNode) ==
            InProgressExprChains.end())) {
        TmpExprChain = std::move(InProgressExprChains[ConcatNode]);
        InProgressExprChains.erase(ConcatNode);
        if (TmpExprChain.first == OperatorType::PlusEquals) {
          TmpExprChain.second.insert(TmpExprChain.second.begin() + 1,
                                     ConcatNode);
        } else {
          TmpExprChain.second.insert(TmpExprChain.second.begin(), ConcatNode);
        }
      } else {
        TmpExprChain = std::make_pair(
          OperatorType::Plus,
          std::vector<CXXOperatorCallExpr const*>{ ConcatNode });
      }

      CXXOperatorCallExpr const* LHSNode =
        Result.Nodes.getNodeAs<CXXOperatorCallExpr>("lhs");

      if (LHSNode != nullptr) {
        if (LHSNode->getBeginLoc().isValid()) {
          InProgressExprChains[LHSNode] = std::move(TmpExprChain);
        }
      } else {
        issueCorrection(TmpExprChain, Result);
      }
    }
  }
}

void StringConcatenationUseCmstrcatCheck::issueCorrection(
  ExprChain const& Chain, MatchFinder::MatchResult const& Result)
{
  std::vector<FixItHint> FixIts;
  CXXOperatorCallExpr const* ExprNode;
  std::vector<clang::CXXOperatorCallExpr const*>::const_iterator It =
    Chain.second.begin();

  if (Chain.first == OperatorType::PlusEquals) {
    ExprNode = *It;
    StringRef LHS = Lexer::getSourceText(
      CharSourceRange::getTokenRange(ExprNode->getArg(0)->getSourceRange()),
      Result.Context->getSourceManager(), Result.Context->getLangOpts());

    FixIts.push_back(FixItHint::CreateReplacement(
      ExprNode->getExprLoc(), "= cmStrCat(" + LHS.str() + ","));
    It++;
  } else {
    ExprNode = *It;
    FixIts.push_back(
      FixItHint::CreateInsertion(ExprNode->getBeginLoc(), "cmStrCat("));
  }

  while (It != std::end(Chain.second)) {
    ExprNode = *It;
    FixIts.push_back(
      FixItHint::CreateReplacement(ExprNode->getOperatorLoc(), ","));
    It++;
  }
  It--;
  ExprNode = *It;

  StringRef LastToken = Lexer::getSourceText(
    CharSourceRange::getTokenRange(
      ExprNode->getArg(1)->getSourceRange().getEnd()),
    Result.Context->getSourceManager(), Result.Context->getLangOpts());
  FixIts.push_back(FixItHint::CreateInsertion(
    ExprNode->getEndLoc().getLocWithOffset(LastToken.str().size()), ")"));

  It = Chain.second.begin();
  ExprNode = *It;

  if (Chain.first == OperatorType::PlusEquals) {
    this->diag(ExprNode->getOperatorLoc(),
               "use cmStrCat() instead of string append")
      << FixIts;
  } else {
    this->diag(ExprNode->getBeginLoc(),
               "use cmStrCat() instead of string concatenation")
      << FixIts;
  }
}
}
}
}