10#include "clang/AST/ASTContext.h"
11#include "clang/AST/ComputeDependence.h"
12#include "clang/AST/Decl.h"
13#include "clang/AST/Expr.h"
14#include "clang/AST/ExprObjC.h"
15#include "clang/AST/Type.h"
16#include "clang/AST/TypeLoc.h"
17#include "clang/ASTMatchers/ASTMatchFinder.h"
18#include "clang/ASTMatchers/ASTMatchers.h"
19#include "clang/ASTMatchers/ASTMatchersMacros.h"
20#include "clang/Basic/Diagnostic.h"
21#include "clang/Basic/LLVM.h"
22#include "clang/Basic/LangOptions.h"
23#include "clang/Basic/SourceLocation.h"
24#include "clang/Basic/SourceManager.h"
25#include "clang/Lex/Lexer.h"
26#include "llvm/ADT/StringRef.h"
43 AST_POLYMORPHIC_SUPPORTED_TYPES(ObjCIvarRefExpr,
46 QualType QT = Node.getType();
47 return QT->isScalarType() &&
48 (QT->getScalarTypeKind() == Type::STK_ObjCObjectPointer ||
49 QT->getScalarTypeKind() == Type::STK_BlockPointer) &&
50 QT.getQualifiers().getObjCLifetime() > Qualifiers::OCL_ExplicitNone;
55static std::optional<FixItHint>
57 StringRef Ownership) {
58 size_t Index = Text.find(Ownership);
59 if (Index == StringRef::npos)
62 SourceLocation Begin = Range.getBegin().getLocWithOffset(Index);
63 SourceLocation End = Begin.getLocWithOffset(Ownership.size());
64 return FixItHint::CreateReplacement(SourceRange(Begin, End),
68static std::optional<FixItHint>
70 const LangOptions &LangOpts) {
71 assert(VD &&
"VarDecl parameter must not be null");
73 if (isa<ParmVarDecl>(VD))
79 CharSourceRange Range = Lexer::makeFileCharRange(
80 CharSourceRange::getTokenRange(VD->getSourceRange()), SM, LangOpts);
81 if (Range.isInvalid()) {
87 StringRef VarDeclText = Lexer::getSourceText(Range, SM, LangOpts);
88 if (std::optional<FixItHint> Hint =
96 return FixItHint::CreateInsertion(Range.getBegin(),
"__unsafe_unretained ");
104 hasReceiverType(asString(
"NSInvocation *")),
105 anyOf(hasSelector(
"getArgument:atIndex:"),
106 hasSelector(
"getReturnValue:")),
109 anyOf(hasDescendant(memberExpr(isObjCManagedLifetime())),
110 hasDescendant(objcIvarRefExpr(isObjCManagedLifetime())),
117 declRefExpr(to(varDecl().bind(
"var")),
118 unless(hasParent(implicitCastExpr())),
119 isObjCManagedLifetime())))))
125 const MatchFinder::MatchResult &Result) {
126 const auto *MatchedExpr = Result.Nodes.getNodeAs<ObjCMessageExpr>(
"call");
128 auto Diag = diag(MatchedExpr->getArg(0)->getBeginLoc(),
129 "NSInvocation %objcinstance0 should only pass pointers to "
130 "objects with ownership __unsafe_unretained")
131 << MatchedExpr->getSelector();
135 const auto *VD = Result.Nodes.getNodeAs<VarDecl>(
"var");
140 Result.Context->getLangOpts()))
void registerMatchers(ast_matchers::MatchFinder *Finder) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
AST_POLYMORPHIC_MATCHER(isInAbseilFile, AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc, NestedNameSpecifierLoc))
Matches AST nodes that were found within Abseil files.
static constexpr StringRef WeakText
static constexpr StringRef UnsafeUnretainedText
static std::optional< FixItHint > fixItHintForVarDecl(const VarDecl *VD, const SourceManager &SM, const LangOptions &LangOpts)
static std::optional< FixItHint > fixItHintReplacementForOwnershipString(StringRef Text, CharSourceRange Range, StringRef Ownership)
static constexpr StringRef StrongText