File: TypeTraitsCheck.cpp

package info (click to toggle)
llvm-toolchain-20 1%3A20.1.6-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,111,304 kB
  • sloc: cpp: 7,438,677; ansic: 1,393,822; asm: 1,012,926; python: 241,650; f90: 86,635; objc: 75,479; lisp: 42,144; pascal: 17,286; sh: 10,027; ml: 5,082; perl: 4,730; awk: 3,523; makefile: 3,349; javascript: 2,251; xml: 892; fortran: 672
file content (316 lines) | stat: -rw-r--r-- 10,162 bytes parent folder | download | duplicates (9)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//===--- TypeTraitsCheck.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 "TypeTraitsCheck.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Lex/Lexer.h"

using namespace clang::ast_matchers;

namespace clang::tidy::modernize {

static const llvm::StringSet<> ValueTraits = {
    "alignment_of",
    "conjunction",
    "disjunction",
    "extent",
    "has_unique_object_representations",
    "has_virtual_destructor",
    "is_abstract",
    "is_aggregate",
    "is_arithmetic",
    "is_array",
    "is_assignable",
    "is_base_of",
    "is_bounded_array",
    "is_class",
    "is_compound",
    "is_const",
    "is_constructible",
    "is_convertible",
    "is_copy_assignable",
    "is_copy_constructible",
    "is_default_constructible",
    "is_destructible",
    "is_empty",
    "is_enum",
    "is_final",
    "is_floating_point",
    "is_function",
    "is_fundamental",
    "is_integral",
    "is_invocable",
    "is_invocable_r",
    "is_layout_compatible",
    "is_lvalue_reference",
    "is_member_function_pointer",
    "is_member_object_pointer",
    "is_member_pointer",
    "is_move_assignable",
    "is_move_constructible",
    "is_nothrow_assignable",
    "is_nothrow_constructible",
    "is_nothrow_convertible",
    "is_nothrow_copy_assignable",
    "is_nothrow_copy_constructible",
    "is_nothrow_default_constructible",
    "is_nothrow_destructible",
    "is_nothrow_invocable",
    "is_nothrow_invocable_r",
    "is_nothrow_move_assignable",
    "is_nothrow_move_constructible",
    "is_nothrow_swappable",
    "is_nothrow_swappable_with",
    "is_null_pointer",
    "is_object",
    "is_pointer",
    "is_pointer_interconvertible_base_of",
    "is_polymorphic",
    "is_reference",
    "is_rvalue_reference",
    "is_same",
    "is_scalar",
    "is_scoped_enum",
    "is_signed",
    "is_standard_layout",
    "is_swappable",
    "is_swappable_with",
    "is_trivial",
    "is_trivially_assignable",
    "is_trivially_constructible",
    "is_trivially_copy_assignable",
    "is_trivially_copy_constructible",
    "is_trivially_copyable",
    "is_trivially_default_constructible",
    "is_trivially_destructible",
    "is_trivially_move_assignable",
    "is_trivially_move_constructible",
    "is_unbounded_array",
    "is_union",
    "is_unsigned",
    "is_void",
    "is_volatile",
    "negation",
    "rank",
    "reference_constructs_from_temporary",
    "reference_converts_from_temporary",
};

static const llvm::StringSet<> TypeTraits = {
    "remove_cv",
    "remove_const",
    "remove_volatile",
    "add_cv",
    "add_const",
    "add_volatile",
    "remove_reference",
    "add_lvalue_reference",
    "add_rvalue_reference",
    "remove_pointer",
    "add_pointer",
    "make_signed",
    "make_unsigned",
    "remove_extent",
    "remove_all_extents",
    "aligned_storage",
    "aligned_union",
    "decay",
    "remove_cvref",
    "enable_if",
    "conditional",
    "common_type",
    "common_reference",
    "underlying_type",
    "result_of",
    "invoke_result",
    "type_identity",
};

static DeclarationName getName(const DependentScopeDeclRefExpr &D) {
  return D.getDeclName();
}

static DeclarationName getName(const DeclRefExpr &D) {
  return D.getDecl()->getDeclName();
}

static bool isNamedType(const ElaboratedTypeLoc &ETL) {
  if (const auto *TFT =
          ETL.getNamedTypeLoc().getTypePtr()->getAs<TypedefType>()) {
    const TypedefNameDecl *Decl = TFT->getDecl();
    return Decl->getDeclName().isIdentifier() && Decl->getName() == "type";
  }
  return false;
}

static bool isNamedType(const DependentNameTypeLoc &DTL) {
  return DTL.getTypePtr()->getIdentifier()->getName() == "type";
}

namespace {
AST_POLYMORPHIC_MATCHER(isValue, AST_POLYMORPHIC_SUPPORTED_TYPES(
                                     DeclRefExpr, DependentScopeDeclRefExpr)) {
  const IdentifierInfo *Ident = getName(Node).getAsIdentifierInfo();
  return Ident && Ident->isStr("value");
}

AST_POLYMORPHIC_MATCHER(isType,
                        AST_POLYMORPHIC_SUPPORTED_TYPES(ElaboratedTypeLoc,
                                                        DependentNameTypeLoc)) {
  return Node.getBeginLoc().isValid() && isNamedType(Node);
}
} // namespace

static constexpr char Bind[] = "";

void TypeTraitsCheck::registerMatchers(MatchFinder *Finder) {
  const ast_matchers::internal::VariadicDynCastAllOfMatcher<
      Stmt,
      DependentScopeDeclRefExpr>
      dependentScopeDeclRefExpr; // NOLINT(readability-identifier-naming)
  const ast_matchers::internal::VariadicDynCastAllOfMatcher<
      TypeLoc,
      DependentNameTypeLoc>
      dependentNameTypeLoc; // NOLINT(readability-identifier-naming)

  // Only register matchers for trait<...>::value in c++17 mode.
  if (getLangOpts().CPlusPlus17) {
    Finder->addMatcher(mapAnyOf(declRefExpr, dependentScopeDeclRefExpr)
                           .with(isValue())
                           .bind(Bind),
                       this);
  }
  Finder->addMatcher(mapAnyOf(elaboratedTypeLoc, dependentNameTypeLoc)
                         .with(isType())
                         .bind(Bind),
                     this);
}

static bool isNamedDeclInStdTraitsSet(const NamedDecl *ND,
                                      const llvm::StringSet<> &Set) {
  return ND->isInStdNamespace() && ND->getDeclName().isIdentifier() &&
         Set.contains(ND->getName());
}

static bool checkTemplatedDecl(const NestedNameSpecifier *NNS,
                               const llvm::StringSet<> &Set) {
  if (!NNS)
    return false;
  const Type *NNST = NNS->getAsType();
  if (!NNST)
    return false;
  const auto *TST = NNST->getAs<TemplateSpecializationType>();
  if (!TST)
    return false;
  if (const TemplateDecl *TD = TST->getTemplateName().getAsTemplateDecl()) {
    return isNamedDeclInStdTraitsSet(TD, Set);
  }
  return false;
}

TypeTraitsCheck::TypeTraitsCheck(StringRef Name, ClangTidyContext *Context)
    : ClangTidyCheck(Name, Context),
      IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", false)) {}

void TypeTraitsCheck::check(const MatchFinder::MatchResult &Result) {
  auto EmitValueWarning = [this, &Result](const NestedNameSpecifierLoc &QualLoc,
                                          SourceLocation EndLoc) {
    SourceLocation TemplateNameEndLoc;
    if (auto TSTL = QualLoc.getTypeLoc().getAs<TemplateSpecializationTypeLoc>();
        !TSTL.isNull())
      TemplateNameEndLoc = Lexer::getLocForEndOfToken(
          TSTL.getTemplateNameLoc(), 0, *Result.SourceManager,
          Result.Context->getLangOpts());
    else
      return;

    if (EndLoc.isMacroID() || QualLoc.getEndLoc().isMacroID() ||
        TemplateNameEndLoc.isMacroID()) {
      if (IgnoreMacros)
        return;
      diag(QualLoc.getBeginLoc(), "use c++17 style variable templates");
      return;
    }
    diag(QualLoc.getBeginLoc(), "use c++17 style variable templates")
        << FixItHint::CreateInsertion(TemplateNameEndLoc, "_v")
        << FixItHint::CreateRemoval({QualLoc.getEndLoc(), EndLoc});
  };

  auto EmitTypeWarning = [this, &Result](const NestedNameSpecifierLoc &QualLoc,
                                         SourceLocation EndLoc,
                                         SourceLocation TypenameLoc) {
    SourceLocation TemplateNameEndLoc;
    if (auto TSTL = QualLoc.getTypeLoc().getAs<TemplateSpecializationTypeLoc>();
        !TSTL.isNull())
      TemplateNameEndLoc = Lexer::getLocForEndOfToken(
          TSTL.getTemplateNameLoc(), 0, *Result.SourceManager,
          Result.Context->getLangOpts());
    else
      return;

    if (EndLoc.isMacroID() || QualLoc.getEndLoc().isMacroID() ||
        TemplateNameEndLoc.isMacroID() || TypenameLoc.isMacroID()) {
      if (IgnoreMacros)
        return;
      diag(QualLoc.getBeginLoc(), "use c++14 style type templates");
      return;
    }
    auto Diag = diag(QualLoc.getBeginLoc(), "use c++14 style type templates");

    if (TypenameLoc.isValid())
      Diag << FixItHint::CreateRemoval(TypenameLoc);
    Diag << FixItHint::CreateInsertion(TemplateNameEndLoc, "_t")
         << FixItHint::CreateRemoval({QualLoc.getEndLoc(), EndLoc});
  };

  if (const auto *DRE = Result.Nodes.getNodeAs<DeclRefExpr>(Bind)) {
    if (!DRE->hasQualifier())
      return;
    if (const auto *CTSD = dyn_cast_if_present<ClassTemplateSpecializationDecl>(
            DRE->getQualifier()->getAsRecordDecl())) {
      if (isNamedDeclInStdTraitsSet(CTSD, ValueTraits))
        EmitValueWarning(DRE->getQualifierLoc(), DRE->getEndLoc());
    }
    return;
  }

  if (const auto *ETL = Result.Nodes.getNodeAs<ElaboratedTypeLoc>(Bind)) {
    const NestedNameSpecifierLoc QualLoc = ETL->getQualifierLoc();
    const auto *NNS = QualLoc.getNestedNameSpecifier();
    if (!NNS)
      return;
    if (const auto *CTSD = dyn_cast_if_present<ClassTemplateSpecializationDecl>(
            NNS->getAsRecordDecl())) {
      if (isNamedDeclInStdTraitsSet(CTSD, TypeTraits))
        EmitTypeWarning(ETL->getQualifierLoc(), ETL->getEndLoc(),
                        ETL->getElaboratedKeywordLoc());
    }
    return;
  }

  if (const auto *DSDRE =
          Result.Nodes.getNodeAs<DependentScopeDeclRefExpr>(Bind)) {
    if (checkTemplatedDecl(DSDRE->getQualifier(), ValueTraits))
      EmitValueWarning(DSDRE->getQualifierLoc(), DSDRE->getEndLoc());
    return;
  }

  if (const auto *DNTL = Result.Nodes.getNodeAs<DependentNameTypeLoc>(Bind)) {
    NestedNameSpecifierLoc QualLoc = DNTL->getQualifierLoc();
    if (checkTemplatedDecl(QualLoc.getNestedNameSpecifier(), TypeTraits))
      EmitTypeWarning(QualLoc, DNTL->getEndLoc(),
                      DNTL->getElaboratedKeywordLoc());
    return;
  }
}

void TypeTraitsCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
  Options.store(Opts, "IgnoreMacros", IgnoreMacros);
}
} // namespace clang::tidy::modernize