blob: 41a7a90a3727eda5a51b9df4efbb0960cad5df98 [file] [log] [blame]
Eugene Zelenko1e95bc02018-04-05 22:15:42 +00001//===--- SemaFixItUtils.cpp - Sema FixIts ---------------------------------===//
Anna Zaks1b068122011-07-28 19:46:48 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Anna Zaks1b068122011-07-28 19:46:48 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines helper classes for generation of Sema FixItHints.
10//
11//===----------------------------------------------------------------------===//
12
Benjamin Kramer1ea8e092012-07-04 17:04:04 +000013#include "clang/AST/ASTContext.h"
Anna Zaks1b068122011-07-28 19:46:48 +000014#include "clang/AST/ExprCXX.h"
15#include "clang/AST/ExprObjC.h"
16#include "clang/Lex/Preprocessor.h"
17#include "clang/Sema/Sema.h"
Eugene Zelenko1e95bc02018-04-05 22:15:42 +000018#include "clang/Sema/SemaFixItUtils.h"
Anna Zaks1b068122011-07-28 19:46:48 +000019
20using namespace clang;
21
22bool ConversionFixItGenerator::compareTypesSimple(CanQualType From,
23 CanQualType To,
24 Sema &S,
25 SourceLocation Loc,
26 ExprValueKind FromVK) {
27 if (!To.isAtLeastAsQualifiedAs(From))
28 return false;
29
30 From = From.getNonReferenceType();
31 To = To.getNonReferenceType();
32
33 // If both are pointer types, work with the pointee types.
34 if (isa<PointerType>(From) && isa<PointerType>(To)) {
35 From = S.Context.getCanonicalType(
36 (cast<PointerType>(From))->getPointeeType());
37 To = S.Context.getCanonicalType(
38 (cast<PointerType>(To))->getPointeeType());
39 }
40
41 const CanQualType FromUnq = From.getUnqualifiedType();
42 const CanQualType ToUnq = To.getUnqualifiedType();
43
Richard Smith0f59cb32015-12-18 21:45:41 +000044 if ((FromUnq == ToUnq || (S.IsDerivedFrom(Loc, FromUnq, ToUnq)) ) &&
Anna Zaks1b068122011-07-28 19:46:48 +000045 To.isAtLeastAsQualifiedAs(From))
46 return true;
47 return false;
48}
49
50bool ConversionFixItGenerator::tryToFixConversion(const Expr *FullExpr,
51 const QualType FromTy,
52 const QualType ToTy,
53 Sema &S) {
54 if (!FullExpr)
55 return false;
56
57 const CanQualType FromQTy = S.Context.getCanonicalType(FromTy);
58 const CanQualType ToQTy = S.Context.getCanonicalType(ToTy);
59 const SourceLocation Begin = FullExpr->getSourceRange().getBegin();
Craig Topper07fa1762015-11-15 02:31:46 +000060 const SourceLocation End = S.getLocForEndOfToken(FullExpr->getSourceRange()
61 .getEnd());
Anna Zaks1b068122011-07-28 19:46:48 +000062
63 // Strip the implicit casts - those are implied by the compiler, not the
64 // original source code.
65 const Expr* Expr = FullExpr->IgnoreImpCasts();
66
67 bool NeedParen = true;
68 if (isa<ArraySubscriptExpr>(Expr) ||
69 isa<CallExpr>(Expr) ||
70 isa<DeclRefExpr>(Expr) ||
71 isa<CastExpr>(Expr) ||
72 isa<CXXNewExpr>(Expr) ||
73 isa<CXXConstructExpr>(Expr) ||
74 isa<CXXDeleteExpr>(Expr) ||
75 isa<CXXNoexceptExpr>(Expr) ||
76 isa<CXXPseudoDestructorExpr>(Expr) ||
77 isa<CXXScalarValueInitExpr>(Expr) ||
78 isa<CXXThisExpr>(Expr) ||
79 isa<CXXTypeidExpr>(Expr) ||
80 isa<CXXUnresolvedConstructExpr>(Expr) ||
81 isa<ObjCMessageExpr>(Expr) ||
82 isa<ObjCPropertyRefExpr>(Expr) ||
83 isa<ObjCProtocolExpr>(Expr) ||
84 isa<MemberExpr>(Expr) ||
85 isa<ParenExpr>(FullExpr) ||
86 isa<ParenListExpr>(Expr) ||
87 isa<SizeOfPackExpr>(Expr) ||
88 isa<UnaryOperator>(Expr))
89 NeedParen = false;
90
91 // Check if the argument needs to be dereferenced:
92 // (type * -> type) or (type * -> type &).
Eugene Zelenko1e95bc02018-04-05 22:15:42 +000093 if (const PointerType *FromPtrTy = dyn_cast<PointerType>(FromQTy)) {
Anna Zaks1b068122011-07-28 19:46:48 +000094 OverloadFixItKind FixKind = OFIK_Dereference;
95
96 bool CanConvert = CompareTypes(
97 S.Context.getCanonicalType(FromPtrTy->getPointeeType()), ToQTy,
98 S, Begin, VK_LValue);
99 if (CanConvert) {
100 // Do not suggest dereferencing a Null pointer.
101 if (Expr->IgnoreParenCasts()->
102 isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull))
103 return false;
104
Eugene Zelenko1e95bc02018-04-05 22:15:42 +0000105 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(Expr)) {
Anna Zaks1b068122011-07-28 19:46:48 +0000106 if (UO->getOpcode() == UO_AddrOf) {
107 FixKind = OFIK_RemoveTakeAddress;
108 Hints.push_back(FixItHint::CreateRemoval(
109 CharSourceRange::getTokenRange(Begin, Begin)));
110 }
111 } else if (NeedParen) {
112 Hints.push_back(FixItHint::CreateInsertion(Begin, "*("));
113 Hints.push_back(FixItHint::CreateInsertion(End, ")"));
114 } else {
115 Hints.push_back(FixItHint::CreateInsertion(Begin, "*"));
116 }
117
118 NumConversionsFixed++;
119 if (NumConversionsFixed == 1)
120 Kind = FixKind;
121 return true;
122 }
123 }
124
125 // Check if the pointer to the argument needs to be passed:
126 // (type -> type *) or (type & -> type *).
127 if (isa<PointerType>(ToQTy)) {
128 bool CanConvert = false;
129 OverloadFixItKind FixKind = OFIK_TakeAddress;
130
131 // Only suggest taking address of L-values.
132 if (!Expr->isLValue() || Expr->getObjectKind() != OK_Ordinary)
133 return false;
134
135 CanConvert = CompareTypes(S.Context.getPointerType(FromQTy), ToQTy,
136 S, Begin, VK_RValue);
137 if (CanConvert) {
Eugene Zelenko1e95bc02018-04-05 22:15:42 +0000138
139 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(Expr)) {
Anna Zaks1b068122011-07-28 19:46:48 +0000140 if (UO->getOpcode() == UO_Deref) {
141 FixKind = OFIK_RemoveDereference;
142 Hints.push_back(FixItHint::CreateRemoval(
143 CharSourceRange::getTokenRange(Begin, Begin)));
144 }
145 } else if (NeedParen) {
146 Hints.push_back(FixItHint::CreateInsertion(Begin, "&("));
147 Hints.push_back(FixItHint::CreateInsertion(End, ")"));
148 } else {
149 Hints.push_back(FixItHint::CreateInsertion(Begin, "&"));
150 }
151
152 NumConversionsFixed++;
153 if (NumConversionsFixed == 1)
154 Kind = FixKind;
155 return true;
156 }
157 }
158
159 return false;
160}
Richard Smith8d06f422012-01-12 23:53:29 +0000161
Richard Smithf7ec86a2013-09-20 00:27:40 +0000162static bool isMacroDefined(const Sema &S, SourceLocation Loc, StringRef Name) {
Richard Smith20e883e2015-04-29 23:20:19 +0000163 return (bool)S.PP.getMacroDefinitionAtLoc(&S.getASTContext().Idents.get(Name),
164 Loc);
Richard Smithd7d57902012-01-13 19:34:55 +0000165}
166
Richard Smithf7ec86a2013-09-20 00:27:40 +0000167static std::string getScalarZeroExpressionForType(
168 const Type &T, SourceLocation Loc, const Sema &S) {
David Blaikie7665a622012-04-30 18:27:22 +0000169 assert(T.isScalarType() && "use scalar types only");
170 // Suggest "0" for non-enumeration scalar types, unless we can find a
171 // better initializer.
172 if (T.isEnumeralType())
Eugene Zelenko1e95bc02018-04-05 22:15:42 +0000173 return std::string();
David Blaikie7665a622012-04-30 18:27:22 +0000174 if ((T.isObjCObjectPointerType() || T.isBlockPointerType()) &&
Richard Smithf7ec86a2013-09-20 00:27:40 +0000175 isMacroDefined(S, Loc, "nil"))
David Blaikie7665a622012-04-30 18:27:22 +0000176 return "nil";
177 if (T.isRealFloatingType())
178 return "0.0";
Richard Smithf7ec86a2013-09-20 00:27:40 +0000179 if (T.isBooleanType() &&
180 (S.LangOpts.CPlusPlus || isMacroDefined(S, Loc, "false")))
David Blaikie7665a622012-04-30 18:27:22 +0000181 return "false";
182 if (T.isPointerType() || T.isMemberPointerType()) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000183 if (S.LangOpts.CPlusPlus11)
David Blaikie7665a622012-04-30 18:27:22 +0000184 return "nullptr";
Richard Smithf7ec86a2013-09-20 00:27:40 +0000185 if (isMacroDefined(S, Loc, "NULL"))
David Blaikie7665a622012-04-30 18:27:22 +0000186 return "NULL";
187 }
188 if (T.isCharType())
189 return "'\\0'";
190 if (T.isWideCharType())
191 return "L'\\0'";
192 if (T.isChar16Type())
193 return "u'\\0'";
194 if (T.isChar32Type())
195 return "U'\\0'";
196 return "0";
197}
198
Richard Smithf7ec86a2013-09-20 00:27:40 +0000199std::string
200Sema::getFixItZeroInitializerForType(QualType T, SourceLocation Loc) const {
Richard Smithd7d57902012-01-13 19:34:55 +0000201 if (T->isScalarType()) {
Richard Smithf7ec86a2013-09-20 00:27:40 +0000202 std::string s = getScalarZeroExpressionForType(*T, Loc, *this);
David Blaikie7665a622012-04-30 18:27:22 +0000203 if (!s.empty())
204 s = " = " + s;
205 return s;
Richard Smithd7d57902012-01-13 19:34:55 +0000206 }
207
Richard Smith8d06f422012-01-12 23:53:29 +0000208 const CXXRecordDecl *RD = T->getAsCXXRecordDecl();
Richard Smith90b748e2012-01-13 02:14:39 +0000209 if (!RD || !RD->hasDefinition())
Eugene Zelenko1e95bc02018-04-05 22:15:42 +0000210 return std::string();
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000211 if (LangOpts.CPlusPlus11 && !RD->hasUserProvidedDefaultConstructor())
Richard Smith8d06f422012-01-12 23:53:29 +0000212 return "{}";
Richard Smith90b748e2012-01-13 02:14:39 +0000213 if (RD->isAggregate())
Richard Smith8d06f422012-01-12 23:53:29 +0000214 return " = {}";
Eugene Zelenko1e95bc02018-04-05 22:15:42 +0000215 return std::string();
David Blaikie7665a622012-04-30 18:27:22 +0000216}
217
Richard Smithf7ec86a2013-09-20 00:27:40 +0000218std::string
219Sema::getFixItZeroLiteralForType(QualType T, SourceLocation Loc) const {
220 return getScalarZeroExpressionForType(*T, Loc, *this);
Richard Smith8d06f422012-01-12 23:53:29 +0000221}