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