11#include "clang/AST/ASTContext.h"
12#include "clang/AST/ExprCXX.h"
13#include "clang/AST/Type.h"
14#include "clang/Tooling/FixIt.h"
20 SourceLocation AmpLocation = Var.getLocation();
22 AmpLocation, Context.getSourceManager(), Context.getLangOpts());
23 if (!Token.is(tok::unknown))
24 AmpLocation = Lexer::getLocForEndOfToken(Token.getLocation(), 0,
25 Context.getSourceManager(),
26 Context.getLangOpts());
27 return FixItHint::CreateInsertion(AmpLocation,
"&");
31 return !(isa<PointerType>(T) || isa<ReferenceType>(T) || isa<ArrayType>(T) ||
32 isa<MemberPointerType>(T) || isa<ObjCObjectPointerType>(T));
36 return (QT->isPointerType() && QT->isFunctionPointerType()) ||
37 isa<MemberPointerType>(QT.getTypePtr());
41 return S.isInvalid() || S.isMacroID();
44static std::optional<SourceLocation>
49 auto PreviousTokenLParen = [&Start, &Context]() {
52 Context.getLangOpts());
53 return T.is(tok::l_paren);
56 while (Start.isValid() && PreviousTokenLParen())
58 Context.getLangOpts());
69 return FixItHint::CreateInsertion(
Loc,
Text);
75 bool WhitespaceBefore =
false) {
77 return (llvm::Twine(
' ') + DeclSpec::getSpecifierName(Qualifier)).str();
78 return (llvm::Twine(DeclSpec::getSpecifierName(Qualifier)) +
" ").str();
81static std::optional<FixItHint>
changeValue(
const VarDecl &Var,
82 DeclSpec::TQ Qualifier,
85 const ASTContext &Context) {
91 std::optional<SourceLocation> IgnoredParens =
98 llvm_unreachable(
"Unknown QualifierPolicy enum");
102 DeclSpec::TQ Qualifier,
103 const ASTContext &Context) {
107 std::optional<SourceLocation> IgnoredParens =
114static std::optional<FixItHint>
117 const ASTContext &Context) {
136 Var.getLocation(), Context.getSourceManager(), Context.getLangOpts(),
141 std::optional<SourceLocation> IgnoredParens =
157 Var.getLocation(), Context.getSourceManager(), Context.getLangOpts(),
165static std::optional<FixItHint>
168 const ASTContext &Context) {
174 Var.getLocation(), Context.getSourceManager(), Context.getLangOpts(),
175 tok::amp, tok::ampamp);
176 std::optional<SourceLocation> IgnoredParens =
185 const ASTContext &Context,
186 DeclSpec::TQ Qualifier,
191 "Unexpected Insertion Policy");
194 "Unexpected Target");
196 QualType ParenStrippedType = Var.getType().IgnoreParens();
198 return changeValue(Var, Qualifier, QualTarget, QualPolicy, Context);
200 if (ParenStrippedType->isReferenceType())
202 QualTarget, QualPolicy, Context);
207 if (ParenStrippedType->isPointerType())
209 ParenStrippedType->getPointeeType().getTypePtr(),
210 QualTarget, QualPolicy, Context);
212 if (ParenStrippedType->isArrayType()) {
213 const Type *AT = ParenStrippedType->getBaseElementTypeUnsafe();
214 assert(AT &&
"Did not retrieve array element type for an array.");
217 return changeValue(Var, Qualifier, QualTarget, QualPolicy, Context);
219 if (AT->isPointerType())
220 return changePointer(Var, Qualifier, AT->getPointeeType().getTypePtr(),
221 QualTarget, QualPolicy, Context);
228 if (isa<ParenExpr>(&
Node))
231 if (isa<clang::BinaryOperator>(&
Node) || isa<UnaryOperator>(&
Node))
234 if (isa<clang::ConditionalOperator>(&
Node) ||
235 isa<BinaryConditionalOperator>(&
Node))
238 if (
const auto *Op = dyn_cast<CXXOperatorCallExpr>(&
Node)) {
239 switch (Op->getOperator()) {
243 return Op->getNumArgs() != 2;
255 if (isa<CStyleCastExpr>(&
Node))
265 if (isa<clang::BinaryOperator>(&ExprNode) ||
266 isa<clang::ConditionalOperator>(&ExprNode)) {
269 if (
const auto *Op = dyn_cast<CXXOperatorCallExpr>(&ExprNode)) {
270 return Op->getNumArgs() == 2 && Op->getOperator() != OO_PlusPlus &&
271 Op->getOperator() != OO_MinusMinus && Op->getOperator() != OO_Call &&
272 Op->getOperator() != OO_Subscript;
280 if (
const auto *Op = dyn_cast<clang::UnaryOperator>(&ExprNode)) {
281 if (Op->getOpcode() == UO_AddrOf) {
284 tooling::fixit::getText(*Op->getSubExpr()->IgnoreParens(), Context));
287 StringRef
Text = tooling::fixit::getText(ExprNode, Context);
293 Text.consume_back(
"->");
297 return (llvm::Twine(
"*(") +
Text +
")").str();
299 return (llvm::Twine(
"*") +
Text).str();
::clang::DynTypedNode Node
bool areParensNeededForStatement(const Stmt &Node)
static std::optional< FixItHint > changePointerItself(const VarDecl &Var, DeclSpec::TQ Qualifier, const ASTContext &Context)
static std::optional< FixItHint > fixIfNotDangerous(SourceLocation Loc, StringRef Text)
static std::string buildQualifier(DeclSpec::TQ Qualifier, bool WhitespaceBefore=false)
static std::optional< FixItHint > changePointer(const VarDecl &Var, DeclSpec::TQ Qualifier, const Type *Pointee, 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.
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 &.
static std::optional< FixItHint > changeValue(const VarDecl &Var, DeclSpec::TQ Qualifier, QualifierTarget QualTarget, QualifierPolicy QualPolicy, const ASTContext &Context)
static std::optional< SourceLocation > skipLParensBackwards(SourceLocation Start, const ASTContext &Context)
static std::optional< FixItHint > changeReferencee(const VarDecl &Var, DeclSpec::TQ Qualifier, QualType Pointee, QualifierTarget QualTarget, QualifierPolicy QualPolicy, const ASTContext &Context)
static bool needParensAfterUnaryOperator(const Expr &ExprNode)
static bool locDangerous(SourceLocation S)
std::optional< FixItHint > addQualifierToVarDecl(const VarDecl &Var, const ASTContext &Context, DeclSpec::TQ Qualifier, QualifierTarget QualTarget, QualifierPolicy QualPolicy)
Creates fix to qualify VarDecl with the specified Qualifier.
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)