Debian Codegraph @

llvm-project/clang-tools-extra/clang-tidy/readability/ContainerDataPointerCheck.cpp

raw
1 //===--- ContainerDataPointerCheck.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 "ContainerDataPointerCheck.h"
10
11 #include "clang/Lex/Lexer.h"
12 #include "llvm/ADT/StringRef.h"
13
14 using namespace clang::ast_matchers;
15
16 namespace clang::tidy::readability {
17
18 constexpr llvm::StringLiteral ContainerExprName = "container-expr";
19 constexpr llvm::StringLiteral DerefContainerExprName = "deref-container-expr";
20 constexpr llvm::StringLiteral AddrOfContainerExprName =
21 "addr-of-container-expr";
22 constexpr llvm::StringLiteral AddressOfName = "address-of";
23
24 ContainerDataPointerCheck::ContainerDataPointerCheck(StringRef Name,
25 ClangTidyContext *Context)
26 : ClangTidyCheck(Name, Context) {}
27
28 void ContainerDataPointerCheck::registerMatchers(MatchFinder *Finder) {
29 const auto Record =
30 cxxRecordDecl(
31 isSameOrDerivedFrom(
32 namedDecl(
33 has(cxxMethodDecl(isPublic(), hasName("data")).bind("data")))
34 .bind("container")))
35 .bind("record");
36
37 const auto NonTemplateContainerType =
38 qualType(hasUnqualifiedDesugaredType(recordType(hasDeclaration(Record))));
39 const auto TemplateContainerType =
40 qualType(hasUnqualifiedDesugaredType(templateSpecializationType(
41 hasDeclaration(classTemplateDecl(has(Record))))));
42
43 const auto Container =
44 qualType(anyOf(NonTemplateContainerType, TemplateContainerType));
45
46 const auto ContainerExpr = anyOf(
47 unaryOperator(
48 hasOperatorName("*"),
49 hasUnaryOperand(
50 expr(hasType(pointsTo(Container))).bind(DerefContainerExprName)))
51 .bind(ContainerExprName),
52 unaryOperator(hasOperatorName("&"),
53 hasUnaryOperand(expr(anyOf(hasType(Container),
54 hasType(references(Container))))
55 .bind(AddrOfContainerExprName)))
56 .bind(ContainerExprName),
57 expr(anyOf(hasType(Container), hasType(pointsTo(Container)),
58 hasType(references(Container))))
59 .bind(ContainerExprName));
60
61 const auto Zero = integerLiteral(equals(0));
62
63 const auto SubscriptOperator = callee(cxxMethodDecl(hasName("operator[]")));
64
65 Finder->addMatcher(
66 unaryOperator(
67 unless(isExpansionInSystemHeader()), hasOperatorName("&"),
68 hasUnaryOperand(expr(
69 anyOf(cxxOperatorCallExpr(SubscriptOperator, argumentCountIs(2),
70 hasArgument(0, ContainerExpr),
71 hasArgument(1, Zero)),
72 cxxMemberCallExpr(SubscriptOperator, on(ContainerExpr),
73 argumentCountIs(1), hasArgument(0, Zero)),
74 arraySubscriptExpr(hasLHS(ContainerExpr), hasRHS(Zero))))))
75 .bind(AddressOfName),
76 this);
77 }
78
79 void ContainerDataPointerCheck::check(const MatchFinder::MatchResult &Result) {
80 const auto *UO = Result.Nodes.getNodeAs<UnaryOperator>(AddressOfName);
81 const auto *CE = Result.Nodes.getNodeAs<Expr>(ContainerExprName);
82 const auto *DCE = Result.Nodes.getNodeAs<Expr>(DerefContainerExprName);
83 const auto *ACE = Result.Nodes.getNodeAs<Expr>(AddrOfContainerExprName);
84
85 if (!UO || !CE)
86 return;
87
88 if (DCE && !CE->getType()->isPointerType())
89 CE = DCE;
90 else if (ACE)
91 CE = ACE;
92
93 SourceRange SrcRange = CE->getSourceRange();
94
95 std::string ReplacementText{
96 Lexer::getSourceText(CharSourceRange::getTokenRange(SrcRange),
97 *Result.SourceManager, getLangOpts())};
98
99 if (!isa<DeclRefExpr, ArraySubscriptExpr, CXXOperatorCallExpr, CallExpr,
100 MemberExpr>(CE))
101 ReplacementText = "(" + ReplacementText + ")";
102
103 if (CE->getType()->isPointerType())
104 ReplacementText += "->data()";
105 else
106 ReplacementText += ".data()";
107
108 FixItHint Hint =
109 FixItHint::CreateReplacement(UO->getSourceRange(), ReplacementText);
110 diag(UO->getBeginLoc(),
111 "'data' should be used for accessing the data pointer instead of taking "
112 "the address of the 0-th element")
113 << Hint;
114 }
115 } // namespace clang::tidy::readability