1//===-- StdAllocatorConstCheck.cpp - clang-tidy --------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "StdAllocatorConstCheck.h"
10#include "clang/ASTMatchers/ASTMatchFinder.h"
11
12using namespace clang::ast_matchers;
13
14namespace clang::tidy::portability {
15
16void StdAllocatorConstCheck::registerMatchers(MatchFinder *Finder) {
17 // Match std::allocator<const T>.
18 auto allocatorConst =
19 recordType(hasDeclaration(InnerMatcher: classTemplateSpecializationDecl(
20 hasName(Name: "::std::allocator"),
21 hasTemplateArgument(N: 0, InnerMatcher: refersToType(InnerMatcher: qualType(isConstQualified()))))));
22
23 auto hasContainerName =
24 hasAnyName("::std::vector", "::std::deque", "::std::list",
25 "::std::multiset", "::std::set", "::std::unordered_multiset",
26 "::std::unordered_set", "::absl::flat_hash_set");
27
28 // Match `std::vector<const T> var;` and other common containers like deque,
29 // list, and absl::flat_hash_set. Containers like queue and stack use deque
30 // but do not directly use std::allocator as a template argument, so they
31 // aren't caught.
32 Finder->addMatcher(
33 NodeMatch: typeLoc(
34 templateSpecializationTypeLoc(),
35 loc(InnerMatcher: hasUnqualifiedDesugaredType(InnerMatcher: anyOf(
36 recordType(hasDeclaration(InnerMatcher: classTemplateSpecializationDecl(
37 hasContainerName,
38 anyOf(
39 hasTemplateArgument(N: 1, InnerMatcher: refersToType(InnerMatcher: allocatorConst)),
40 hasTemplateArgument(N: 2, InnerMatcher: refersToType(InnerMatcher: allocatorConst)),
41 hasTemplateArgument(N: 3, InnerMatcher: refersToType(InnerMatcher: allocatorConst)))))),
42 // Match std::vector<const dependent>
43 templateSpecializationType(
44 templateArgumentCountIs(N: 1),
45 hasTemplateArgument(
46 N: 0, InnerMatcher: refersToType(InnerMatcher: qualType(isConstQualified()))),
47 hasDeclaration(InnerMatcher: namedDecl(hasContainerName)))))))
48 .bind(ID: "type_loc"),
49 Action: this);
50}
51
52void StdAllocatorConstCheck::check(const MatchFinder::MatchResult &Result) {
53 const auto *T = Result.Nodes.getNodeAs<TypeLoc>(ID: "type_loc");
54 if (!T)
55 return;
56 // Exclude TypeLoc matches in STL headers.
57 if (isSystem(CK: Result.Context->getSourceManager().getFileCharacteristic(
58 Loc: T->getBeginLoc())))
59 return;
60
61 diag(Loc: T->getBeginLoc(),
62 Description: "container using std::allocator<const T> is a deprecated libc++ "
63 "extension; remove const for compatibility with other standard "
64 "libraries");
65}
66
67} // namespace clang::tidy::portability
68

source code of clang-tools-extra/clang-tidy/portability/StdAllocatorConstCheck.cpp