| 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 | |
| 12 | using namespace clang::ast_matchers; |
| 13 | |
| 14 | namespace clang::tidy::portability { |
| 15 | |
| 16 | void StdAllocatorConstCheck::registerMatchers(MatchFinder *Finder) { |
| 17 | // Match std::allocator<const T>. |
| 18 | auto allocatorConst = |
| 19 | recordType(hasDeclaration(classTemplateSpecializationDecl( |
| 20 | hasName("::std::allocator"), |
| 21 | hasTemplateArgument(0, refersToType(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 | typeLoc( |
| 34 | templateSpecializationTypeLoc(), |
| 35 | loc(hasUnqualifiedDesugaredType(anyOf( |
| 36 | recordType(hasDeclaration(classTemplateSpecializationDecl( |
| 37 | hasContainerName, |
| 38 | anyOf( |
| 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), |
| 45 | hasTemplateArgument( |
| 46 | 0, refersToType(qualType(isConstQualified()))), |
| 47 | hasDeclaration(namedDecl(hasContainerName))))))) |
| 48 | .bind("type_loc"), |
| 49 | this); |
| 50 | } |
| 51 | |
| 52 | void StdAllocatorConstCheck::check(const MatchFinder::MatchResult &Result) { |
| 53 | const auto *T = Result.Nodes.getNodeAs<TypeLoc>("type_loc"); |
| 54 | if (!T) |
| 55 | return; |
| 56 | // Exclude TypeLoc matches in STL headers. |
| 57 | if (isSystem(Result.Context->getSourceManager().getFileCharacteristic( |
| 58 | T->getBeginLoc()))) |
| 59 | return; |
| 60 | |
| 61 | diag(T->getBeginLoc(), |
| 62 | "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 |