SemaFixItUtils.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //===--- SemaFixItUtils.cpp - Sema FixIts ---------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines helper classes for generation of Sema FixItHints.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/AST/ASTContext.h"
  14. #include "clang/AST/ExprCXX.h"
  15. #include "clang/AST/ExprObjC.h"
  16. #include "clang/Lex/Preprocessor.h"
  17. #include "clang/Sema/Sema.h"
  18. #include "clang/Sema/SemaFixItUtils.h"
  19. using namespace clang;
  20. bool ConversionFixItGenerator::compareTypesSimple(CanQualType From,
  21. CanQualType To,
  22. Sema &S,
  23. SourceLocation Loc,
  24. ExprValueKind FromVK) {
  25. if (!To.isAtLeastAsQualifiedAs(From))
  26. return false;
  27. From = From.getNonReferenceType();
  28. To = To.getNonReferenceType();
  29. // If both are pointer types, work with the pointee types.
  30. if (isa<PointerType>(From) && isa<PointerType>(To)) {
  31. From = S.Context.getCanonicalType(
  32. (cast<PointerType>(From))->getPointeeType());
  33. To = S.Context.getCanonicalType(
  34. (cast<PointerType>(To))->getPointeeType());
  35. }
  36. const CanQualType FromUnq = From.getUnqualifiedType();
  37. const CanQualType ToUnq = To.getUnqualifiedType();
  38. if ((FromUnq == ToUnq || (S.IsDerivedFrom(FromUnq, ToUnq)) ) &&
  39. To.isAtLeastAsQualifiedAs(From))
  40. return true;
  41. return false;
  42. }
  43. bool ConversionFixItGenerator::tryToFixConversion(const Expr *FullExpr,
  44. const QualType FromTy,
  45. const QualType ToTy,
  46. Sema &S) {
  47. if (!FullExpr)
  48. return false;
  49. const CanQualType FromQTy = S.Context.getCanonicalType(FromTy);
  50. const CanQualType ToQTy = S.Context.getCanonicalType(ToTy);
  51. const SourceLocation Begin = FullExpr->getSourceRange().getBegin();
  52. const SourceLocation End = S.PP.getLocForEndOfToken(FullExpr->getSourceRange()
  53. .getEnd());
  54. // Strip the implicit casts - those are implied by the compiler, not the
  55. // original source code.
  56. const Expr* Expr = FullExpr->IgnoreImpCasts();
  57. bool NeedParen = true;
  58. if (isa<ArraySubscriptExpr>(Expr) ||
  59. isa<CallExpr>(Expr) ||
  60. isa<DeclRefExpr>(Expr) ||
  61. isa<CastExpr>(Expr) ||
  62. isa<CXXNewExpr>(Expr) ||
  63. isa<CXXConstructExpr>(Expr) ||
  64. isa<CXXDeleteExpr>(Expr) ||
  65. isa<CXXNoexceptExpr>(Expr) ||
  66. isa<CXXPseudoDestructorExpr>(Expr) ||
  67. isa<CXXScalarValueInitExpr>(Expr) ||
  68. isa<CXXThisExpr>(Expr) ||
  69. isa<CXXTypeidExpr>(Expr) ||
  70. isa<CXXUnresolvedConstructExpr>(Expr) ||
  71. isa<ObjCMessageExpr>(Expr) ||
  72. isa<ObjCPropertyRefExpr>(Expr) ||
  73. isa<ObjCProtocolExpr>(Expr) ||
  74. isa<MemberExpr>(Expr) ||
  75. isa<ParenExpr>(FullExpr) ||
  76. isa<ParenListExpr>(Expr) ||
  77. isa<SizeOfPackExpr>(Expr) ||
  78. isa<UnaryOperator>(Expr))
  79. NeedParen = false;
  80. // Check if the argument needs to be dereferenced:
  81. // (type * -> type) or (type * -> type &).
  82. if (const PointerType *FromPtrTy = dyn_cast<PointerType>(FromQTy)) {
  83. OverloadFixItKind FixKind = OFIK_Dereference;
  84. bool CanConvert = CompareTypes(
  85. S.Context.getCanonicalType(FromPtrTy->getPointeeType()), ToQTy,
  86. S, Begin, VK_LValue);
  87. if (CanConvert) {
  88. // Do not suggest dereferencing a Null pointer.
  89. if (Expr->IgnoreParenCasts()->
  90. isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull))
  91. return false;
  92. if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(Expr)) {
  93. if (UO->getOpcode() == UO_AddrOf) {
  94. FixKind = OFIK_RemoveTakeAddress;
  95. Hints.push_back(FixItHint::CreateRemoval(
  96. CharSourceRange::getTokenRange(Begin, Begin)));
  97. }
  98. } else if (NeedParen) {
  99. Hints.push_back(FixItHint::CreateInsertion(Begin, "*("));
  100. Hints.push_back(FixItHint::CreateInsertion(End, ")"));
  101. } else {
  102. Hints.push_back(FixItHint::CreateInsertion(Begin, "*"));
  103. }
  104. NumConversionsFixed++;
  105. if (NumConversionsFixed == 1)
  106. Kind = FixKind;
  107. return true;
  108. }
  109. }
  110. // Check if the pointer to the argument needs to be passed:
  111. // (type -> type *) or (type & -> type *).
  112. if (isa<PointerType>(ToQTy)) {
  113. bool CanConvert = false;
  114. OverloadFixItKind FixKind = OFIK_TakeAddress;
  115. // Only suggest taking address of L-values.
  116. if (!Expr->isLValue() || Expr->getObjectKind() != OK_Ordinary)
  117. return false;
  118. CanConvert = CompareTypes(S.Context.getPointerType(FromQTy), ToQTy,
  119. S, Begin, VK_RValue);
  120. if (CanConvert) {
  121. if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(Expr)) {
  122. if (UO->getOpcode() == UO_Deref) {
  123. FixKind = OFIK_RemoveDereference;
  124. Hints.push_back(FixItHint::CreateRemoval(
  125. CharSourceRange::getTokenRange(Begin, Begin)));
  126. }
  127. } else if (NeedParen) {
  128. Hints.push_back(FixItHint::CreateInsertion(Begin, "&("));
  129. Hints.push_back(FixItHint::CreateInsertion(End, ")"));
  130. } else {
  131. Hints.push_back(FixItHint::CreateInsertion(Begin, "&"));
  132. }
  133. NumConversionsFixed++;
  134. if (NumConversionsFixed == 1)
  135. Kind = FixKind;
  136. return true;
  137. }
  138. }
  139. return false;
  140. }
  141. static bool isMacroDefined(const Sema &S, SourceLocation Loc, StringRef Name) {
  142. return (bool)S.PP.getMacroDefinitionAtLoc(&S.getASTContext().Idents.get(Name),
  143. Loc);
  144. }
  145. static std::string getScalarZeroExpressionForType(
  146. const Type &T, SourceLocation Loc, const Sema &S) {
  147. assert(T.isScalarType() && "use scalar types only");
  148. // Suggest "0" for non-enumeration scalar types, unless we can find a
  149. // better initializer.
  150. if (T.isEnumeralType())
  151. return std::string();
  152. if ((T.isObjCObjectPointerType() || T.isBlockPointerType()) &&
  153. isMacroDefined(S, Loc, "nil"))
  154. return "nil";
  155. if (T.isRealFloatingType())
  156. return "0.0";
  157. if (T.isBooleanType() &&
  158. (S.LangOpts.CPlusPlus || isMacroDefined(S, Loc, "false")))
  159. return "false";
  160. if (T.isPointerType() || T.isMemberPointerType()) {
  161. if (S.LangOpts.CPlusPlus11)
  162. return "nullptr";
  163. if (isMacroDefined(S, Loc, "NULL"))
  164. return "NULL";
  165. }
  166. if (T.isCharType())
  167. return "'\\0'";
  168. if (T.isWideCharType())
  169. return "L'\\0'";
  170. if (T.isChar16Type())
  171. return "u'\\0'";
  172. if (T.isChar32Type())
  173. return "U'\\0'";
  174. return "0";
  175. }
  176. std::string
  177. Sema::getFixItZeroInitializerForType(QualType T, SourceLocation Loc) const {
  178. if (T->isScalarType()) {
  179. std::string s = getScalarZeroExpressionForType(*T, Loc, *this);
  180. if (!s.empty())
  181. s = " = " + s;
  182. return s;
  183. }
  184. const CXXRecordDecl *RD = T->getAsCXXRecordDecl();
  185. if (!RD || !RD->hasDefinition())
  186. return std::string();
  187. if (LangOpts.CPlusPlus11 && !RD->hasUserProvidedDefaultConstructor())
  188. return "{}";
  189. if (RD->isAggregate())
  190. return " = {}";
  191. return std::string();
  192. }
  193. std::string
  194. Sema::getFixItZeroLiteralForType(QualType T, SourceLocation Loc) const {
  195. return getScalarZeroExpressionForType(*T, Loc, *this);
  196. }