SemaFixItUtils.cpp 7.8 KB

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