11#include "clang/AST/ASTContext.h"
12#include "clang/AST/ExprCXX.h"
13#include "clang/AST/Type.h"
14#include "clang/Sema/DeclSpec.h"
15#include "clang/Tooling/FixIt.h"
21 SourceLocation AmpLocation = Var.getLocation();
23 AmpLocation, Context.getSourceManager(), Context.getLangOpts());
24 if (!Token.is(tok::unknown))
25 AmpLocation = Lexer::getLocForEndOfToken(Token.getLocation(), 0,
26 Context.getSourceManager(),
27 Context.getLangOpts());
28 return FixItHint::CreateInsertion(AmpLocation,
"&");
32 return !(isa<PointerType>(T) || isa<ReferenceType>(T) || isa<ArrayType>(T) ||
33 isa<MemberPointerType>(T) || isa<ObjCObjectPointerType>(T));
37 return (QT->isPointerType() && QT->isFunctionPointerType()) ||
38 isa<MemberPointerType>(QT.getTypePtr());
42 return S.isInvalid() || S.isMacroID();
45static std::optional<SourceLocation>
50 auto PreviousTokenLParen = [&Start, &Context]() {
53 Context.getLangOpts());
54 return T.is(tok::l_paren);
57 while (Start.isValid() && PreviousTokenLParen())
59 Context.getLangOpts());
70 return FixItHint::CreateInsertion(
Loc,
Text);
76 bool WhitespaceBefore =
false) {
78 return (llvm::Twine(
' ') + Qualifiers::fromCVRMask(Qualifier).getAsString())
80 return (llvm::Twine(Qualifiers::fromCVRMask(Qualifier).getAsString()) +
" ")
84static std::optional<FixItHint>
changeValue(
const VarDecl &Var,
85 Qualifiers::TQ Qualifier,
88 const ASTContext &Context) {
94 std::optional<SourceLocation> IgnoredParens =
101 llvm_unreachable(
"Unknown QualifierPolicy enum");
105 Qualifiers::TQ Qualifier,
106 const ASTContext &Context) {
110 std::optional<SourceLocation> IgnoredParens =
117static std::optional<FixItHint>
120 const ASTContext &Context) {
139 Var.getLocation(), Context.getSourceManager(), Context.getLangOpts(),
144 std::optional<SourceLocation> IgnoredParens =
160 Var.getLocation(), Context.getSourceManager(), Context.getLangOpts(),
168static std::optional<FixItHint>
171 const ASTContext &Context) {
177 Var.getLocation(), Context.getSourceManager(), Context.getLangOpts(),
178 tok::amp, tok::ampamp);
179 std::optional<SourceLocation> IgnoredParens =
188 const ASTContext &Context,
189 Qualifiers::TQ Qualifier,
194 "Unexpected Insertion Policy");
197 "Unexpected Target");
199 QualType ParenStrippedType = Var.getType().IgnoreParens();
201 return changeValue(Var, Qualifier, QualTarget, QualPolicy, Context);
203 if (ParenStrippedType->isReferenceType())
205 QualTarget, QualPolicy, Context);
210 if (ParenStrippedType->isPointerType())
212 ParenStrippedType->getPointeeType().getTypePtr(),
213 QualTarget, QualPolicy, Context);
215 if (ParenStrippedType->isArrayType()) {
216 const Type *AT = ParenStrippedType->getBaseElementTypeUnsafe();
217 assert(AT &&
"Did not retrieve array element type for an array.");
220 return changeValue(Var, Qualifier, QualTarget, QualPolicy, Context);
222 if (AT->isPointerType())
223 return changePointer(Var, Qualifier, AT->getPointeeType().getTypePtr(),
224 QualTarget, QualPolicy, Context);
231 if (isa<ParenExpr>(&
Node))
234 if (isa<clang::BinaryOperator>(&
Node) || isa<UnaryOperator>(&
Node))
237 if (isa<clang::ConditionalOperator>(&
Node) ||
238 isa<BinaryConditionalOperator>(&
Node))
241 if (
const auto *Op = dyn_cast<CXXOperatorCallExpr>(&
Node)) {
242 switch (Op->getOperator()) {
246 return Op->getNumArgs() != 2;
258 if (isa<CStyleCastExpr>(&
Node))
268 if (isa<clang::BinaryOperator>(&ExprNode) ||
269 isa<clang::ConditionalOperator>(&ExprNode)) {
272 if (
const auto *Op = dyn_cast<CXXOperatorCallExpr>(&ExprNode)) {
273 return Op->getNumArgs() == 2 && Op->getOperator() != OO_PlusPlus &&
274 Op->getOperator() != OO_MinusMinus && Op->getOperator() != OO_Call &&
275 Op->getOperator() != OO_Subscript;
283 if (
const auto *Op = dyn_cast<clang::UnaryOperator>(&ExprNode)) {
284 if (Op->getOpcode() == UO_AddrOf) {
287 tooling::fixit::getText(*Op->getSubExpr()->IgnoreParens(), Context));
290 StringRef
Text = tooling::fixit::getText(ExprNode, Context);
296 Text.consume_back(
"->");
300 return (llvm::Twine(
"*(") +
Text +
")").str();
302 return (llvm::Twine(
"*") +
Text).str();
::clang::DynTypedNode Node
static std::string buildQualifier(Qualifiers::TQ Qualifier, bool WhitespaceBefore=false)
bool areParensNeededForStatement(const Stmt &Node)
static std::optional< FixItHint > changePointer(const VarDecl &Var, Qualifiers::TQ Qualifier, const Type *Pointee, QualifierTarget QualTarget, QualifierPolicy QualPolicy, const ASTContext &Context)
static std::optional< FixItHint > changePointerItself(const VarDecl &Var, Qualifiers::TQ Qualifier, const ASTContext &Context)
static std::optional< FixItHint > fixIfNotDangerous(SourceLocation Loc, StringRef Text)
static std::optional< FixItHint > changeValue(const VarDecl &Var, Qualifiers::TQ Qualifier, QualifierTarget QualTarget, QualifierPolicy QualPolicy, const ASTContext &Context)
static bool isMemberOrFunctionPointer(QualType QT)
QualifierTarget
This enum defines which entity is the target for adding the qualifier.
@ Value
Transforming a pointer attaches to the pointee and not the pointer itself.
static std::optional< FixItHint > changeReferencee(const VarDecl &Var, Qualifiers::TQ Qualifier, QualType Pointee, QualifierTarget QualTarget, QualifierPolicy QualPolicy, const ASTContext &Context)
QualifierPolicy
This enum defines where the qualifier shall be preferably added.
FixItHint changeVarDeclToReference(const VarDecl &Var, ASTContext &Context)
Creates fix to make VarDecl a reference by adding &.
std::optional< FixItHint > addQualifierToVarDecl(const VarDecl &Var, const ASTContext &Context, Qualifiers::TQ Qualifier, QualifierTarget QualTarget, QualifierPolicy QualPolicy)
Creates fix to qualify VarDecl with the specified Qualifier.
static std::optional< SourceLocation > skipLParensBackwards(SourceLocation Start, const ASTContext &Context)
static bool needParensAfterUnaryOperator(const Expr &ExprNode)
static bool locDangerous(SourceLocation S)
std::string formatDereference(const Expr &ExprNode, const ASTContext &Context)
static bool isValueType(const Type *T)
Token getPreviousToken(SourceLocation Location, const SourceManager &SM, const LangOptions &LangOpts, bool SkipComments)
Returns previous token or tok::unknown if not found.
SourceLocation findPreviousTokenStart(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts)
SourceLocation findPreviousTokenKind(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts, tok::TokenKind TK)
SourceLocation findPreviousAnyTokenKind(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts, TokenKind TK, TokenKinds... TKs)