1 //===-- StdAllocatorConstCheck.cpp - clang-tidy --------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "StdAllocatorConstCheck.h"
10 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 using namespace clang::ast_matchers
;
14 namespace clang::tidy::portability
{
16 void StdAllocatorConstCheck::registerMatchers(MatchFinder
*Finder
) {
17 // Match std::allocator<const T>.
19 recordType(hasDeclaration(classTemplateSpecializationDecl(
20 hasName("::std::allocator"),
21 hasTemplateArgument(0, refersToType(qualType(isConstQualified()))))));
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");
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
34 templateSpecializationTypeLoc(),
35 loc(hasUnqualifiedDesugaredType(anyOf(
36 recordType(hasDeclaration(classTemplateSpecializationDecl(
39 hasTemplateArgument(1, refersToType(allocatorConst
)),
40 hasTemplateArgument(2, refersToType(allocatorConst
)),
41 hasTemplateArgument(3, refersToType(allocatorConst
)))))),
42 // Match std::vector<const dependent>
43 templateSpecializationType(
44 templateArgumentCountIs(1),
46 0, refersToType(qualType(isConstQualified()))),
47 hasDeclaration(namedDecl(hasContainerName
)))))))
52 void StdAllocatorConstCheck::check(const MatchFinder::MatchResult
&Result
) {
53 const auto *T
= Result
.Nodes
.getNodeAs
<TypeLoc
>("type_loc");
56 // Exclude TypeLoc matches in STL headers.
57 if (isSystem(Result
.Context
->getSourceManager().getFileCharacteristic(
61 diag(T
->getBeginLoc(),
62 "container using std::allocator<const T> is a deprecated libc++ "
63 "extension; remove const for compatibility with other standard "
67 } // namespace clang::tidy::portability