SemaFixItUtils.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 (isa<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. CanConvert = CompareTypes(S.Context.getPointerType(FromQTy), ToQTy, S,
  118. Begin, VK_PRValue);
  119. if (CanConvert) {
  120. if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(Expr)) {
  121. if (UO->getOpcode() == UO_Deref) {
  122. FixKind = OFIK_RemoveDereference;
  123. Hints.push_back(FixItHint::CreateRemoval(
  124. CharSourceRange::getTokenRange(Begin, Begin)));
  125. }
  126. } else if (NeedParen) {
  127. Hints.push_back(FixItHint::CreateInsertion(Begin, "&("));
  128. Hints.push_back(FixItHint::CreateInsertion(End, ")"));
  129. } else {
  130. Hints.push_back(FixItHint::CreateInsertion(Begin, "&"));
  131. }
  132. NumConversionsFixed++;
  133. if (NumConversionsFixed == 1)
  134. Kind = FixKind;
  135. return true;
  136. }
  137. }
  138. return false;
  139. }
  140. static bool isMacroDefined(const Sema &S, SourceLocation Loc, StringRef Name) {
  141. return (bool)S.PP.getMacroDefinitionAtLoc(&S.getASTContext().Idents.get(Name),
  142. Loc);
  143. }
  144. static std::string getScalarZeroExpressionForType(
  145. const Type &T, SourceLocation Loc, const Sema &S) {
  146. assert(T.isScalarType() && "use scalar types only");
  147. // Suggest "0" for non-enumeration scalar types, unless we can find a
  148. // better initializer.
  149. if (T.isEnumeralType())
  150. return std::string();
  151. if ((T.isObjCObjectPointerType() || T.isBlockPointerType()) &&
  152. isMacroDefined(S, Loc, "nil"))
  153. return "nil";
  154. if (T.isRealFloatingType())
  155. return "0.0";
  156. if (T.isBooleanType() &&
  157. (S.LangOpts.CPlusPlus || isMacroDefined(S, Loc, "false")))
  158. return "false";
  159. if (T.isPointerType() || T.isMemberPointerType()) {
  160. if (S.LangOpts.CPlusPlus11)
  161. return "nullptr";
  162. if (isMacroDefined(S, Loc, "NULL"))
  163. return "NULL";
  164. }
  165. if (T.isCharType())
  166. return "'\\0'";
  167. if (T.isWideCharType())
  168. return "L'\\0'";
  169. if (T.isChar16Type())
  170. return "u'\\0'";
  171. if (T.isChar32Type())
  172. return "U'\\0'";
  173. return "0";
  174. }
  175. std::string
  176. Sema::getFixItZeroInitializerForType(QualType T, SourceLocation Loc) const {
  177. if (T->isScalarType()) {
  178. std::string s = getScalarZeroExpressionForType(*T, Loc, *this);
  179. if (!s.empty())
  180. s = " = " + s;
  181. return s;
  182. }
  183. const CXXRecordDecl *RD = T->getAsCXXRecordDecl();
  184. if (!RD || !RD->hasDefinition())
  185. return std::string();
  186. if (LangOpts.CPlusPlus11 && !RD->hasUserProvidedDefaultConstructor())
  187. return "{}";
  188. if (RD->isAggregate())
  189. return " = {}";
  190. return std::string();
  191. }
  192. std::string
  193. Sema::getFixItZeroLiteralForType(QualType T, SourceLocation Loc) const {
  194. return getScalarZeroExpressionForType(*T, Loc, *this);
  195. }