SemaFixItUtils.cpp 7.7 KB

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