1 //===--- SemaFixItUtils.cpp - Sema FixIts ---------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines helper classes for generation of Sema FixItHints.
12 //===----------------------------------------------------------------------===//
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/ExprCXX.h"
16 #include "clang/AST/ExprObjC.h"
17 #include "clang/Lex/Preprocessor.h"
18 #include "clang/Sema/Sema.h"
19 #include "clang/Sema/SemaFixItUtils.h"
21 using namespace clang;
23 bool ConversionFixItGenerator::compareTypesSimple(CanQualType From,
27 ExprValueKind FromVK) {
28 if (!To.isAtLeastAsQualifiedAs(From))
31 From = From.getNonReferenceType();
32 To = To.getNonReferenceType();
34 // If both are pointer types, work with the pointee types.
35 if (isa<PointerType>(From) && isa<PointerType>(To)) {
36 From = S.Context.getCanonicalType(
37 (cast<PointerType>(From))->getPointeeType());
38 To = S.Context.getCanonicalType(
39 (cast<PointerType>(To))->getPointeeType());
42 const CanQualType FromUnq = From.getUnqualifiedType();
43 const CanQualType ToUnq = To.getUnqualifiedType();
45 if ((FromUnq == ToUnq || (S.IsDerivedFrom(FromUnq, ToUnq)) ) &&
46 To.isAtLeastAsQualifiedAs(From))
51 bool ConversionFixItGenerator::tryToFixConversion(const Expr *FullExpr,
52 const QualType FromTy,
58 const CanQualType FromQTy = S.Context.getCanonicalType(FromTy);
59 const CanQualType ToQTy = S.Context.getCanonicalType(ToTy);
60 const SourceLocation Begin = FullExpr->getSourceRange().getBegin();
61 const SourceLocation End = S.PP.getLocForEndOfToken(FullExpr->getSourceRange()
64 // Strip the implicit casts - those are implied by the compiler, not the
65 // original source code.
66 const Expr* Expr = FullExpr->IgnoreImpCasts();
68 bool NeedParen = true;
69 if (isa<ArraySubscriptExpr>(Expr) ||
70 isa<CallExpr>(Expr) ||
71 isa<DeclRefExpr>(Expr) ||
72 isa<CastExpr>(Expr) ||
73 isa<CXXNewExpr>(Expr) ||
74 isa<CXXConstructExpr>(Expr) ||
75 isa<CXXDeleteExpr>(Expr) ||
76 isa<CXXNoexceptExpr>(Expr) ||
77 isa<CXXPseudoDestructorExpr>(Expr) ||
78 isa<CXXScalarValueInitExpr>(Expr) ||
79 isa<CXXThisExpr>(Expr) ||
80 isa<CXXTypeidExpr>(Expr) ||
81 isa<CXXUnresolvedConstructExpr>(Expr) ||
82 isa<ObjCMessageExpr>(Expr) ||
83 isa<ObjCPropertyRefExpr>(Expr) ||
84 isa<ObjCProtocolExpr>(Expr) ||
85 isa<MemberExpr>(Expr) ||
86 isa<ParenExpr>(FullExpr) ||
87 isa<ParenListExpr>(Expr) ||
88 isa<SizeOfPackExpr>(Expr) ||
89 isa<UnaryOperator>(Expr))
92 // Check if the argument needs to be dereferenced:
93 // (type * -> type) or (type * -> type &).
94 if (const PointerType *FromPtrTy = dyn_cast<PointerType>(FromQTy)) {
95 OverloadFixItKind FixKind = OFIK_Dereference;
97 bool CanConvert = CompareTypes(
98 S.Context.getCanonicalType(FromPtrTy->getPointeeType()), ToQTy,
101 // Do not suggest dereferencing a Null pointer.
102 if (Expr->IgnoreParenCasts()->
103 isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull))
106 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(Expr)) {
107 if (UO->getOpcode() == UO_AddrOf) {
108 FixKind = OFIK_RemoveTakeAddress;
109 Hints.push_back(FixItHint::CreateRemoval(
110 CharSourceRange::getTokenRange(Begin, Begin)));
112 } else if (NeedParen) {
113 Hints.push_back(FixItHint::CreateInsertion(Begin, "*("));
114 Hints.push_back(FixItHint::CreateInsertion(End, ")"));
116 Hints.push_back(FixItHint::CreateInsertion(Begin, "*"));
119 NumConversionsFixed++;
120 if (NumConversionsFixed == 1)
126 // Check if the pointer to the argument needs to be passed:
127 // (type -> type *) or (type & -> type *).
128 if (isa<PointerType>(ToQTy)) {
129 bool CanConvert = false;
130 OverloadFixItKind FixKind = OFIK_TakeAddress;
132 // Only suggest taking address of L-values.
133 if (!Expr->isLValue() || Expr->getObjectKind() != OK_Ordinary)
136 CanConvert = CompareTypes(S.Context.getPointerType(FromQTy), ToQTy,
137 S, Begin, VK_RValue);
140 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(Expr)) {
141 if (UO->getOpcode() == UO_Deref) {
142 FixKind = OFIK_RemoveDereference;
143 Hints.push_back(FixItHint::CreateRemoval(
144 CharSourceRange::getTokenRange(Begin, Begin)));
146 } else if (NeedParen) {
147 Hints.push_back(FixItHint::CreateInsertion(Begin, "&("));
148 Hints.push_back(FixItHint::CreateInsertion(End, ")"));
150 Hints.push_back(FixItHint::CreateInsertion(Begin, "&"));
153 NumConversionsFixed++;
154 if (NumConversionsFixed == 1)
163 static bool isMacroDefined(const Sema &S, SourceLocation Loc, StringRef Name) {
164 const IdentifierInfo *II = &S.getASTContext().Idents.get(Name);
165 if (!II->hadMacroDefinition()) return false;
167 MacroDirective *Macro = S.PP.getMacroDirectiveHistory(II);
168 return Macro && Macro->findDirectiveAtLoc(Loc, S.getSourceManager());
171 static std::string getScalarZeroExpressionForType(
172 const Type &T, SourceLocation Loc, const Sema &S) {
173 assert(T.isScalarType() && "use scalar types only");
174 // Suggest "0" for non-enumeration scalar types, unless we can find a
175 // better initializer.
176 if (T.isEnumeralType())
177 return std::string();
178 if ((T.isObjCObjectPointerType() || T.isBlockPointerType()) &&
179 isMacroDefined(S, Loc, "nil"))
181 if (T.isRealFloatingType())
183 if (T.isBooleanType() &&
184 (S.LangOpts.CPlusPlus || isMacroDefined(S, Loc, "false")))
186 if (T.isPointerType() || T.isMemberPointerType()) {
187 if (S.LangOpts.CPlusPlus11)
189 if (isMacroDefined(S, Loc, "NULL"))
194 if (T.isWideCharType())
196 if (T.isChar16Type())
198 if (T.isChar32Type())
204 Sema::getFixItZeroInitializerForType(QualType T, SourceLocation Loc) const {
205 if (T->isScalarType()) {
206 std::string s = getScalarZeroExpressionForType(*T, Loc, *this);
212 const CXXRecordDecl *RD = T->getAsCXXRecordDecl();
213 if (!RD || !RD->hasDefinition())
214 return std::string();
215 if (LangOpts.CPlusPlus11 && !RD->hasUserProvidedDefaultConstructor())
217 if (RD->isAggregate())
219 return std::string();
223 Sema::getFixItZeroLiteralForType(QualType T, SourceLocation Loc) const {
224 return getScalarZeroExpressionForType(*T, Loc, *this);