Path: blob/main/contrib/llvm-project/clang/lib/Sema/SemaFixItUtils.cpp
35234 views
//===--- SemaFixItUtils.cpp - Sema FixIts ---------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file defines helper classes for generation of Sema FixItHints.9//10//===----------------------------------------------------------------------===//1112#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"1819using namespace clang;2021bool ConversionFixItGenerator::compareTypesSimple(CanQualType From,22CanQualType To,23Sema &S,24SourceLocation Loc,25ExprValueKind FromVK) {26if (!To.isAtLeastAsQualifiedAs(From))27return false;2829From = From.getNonReferenceType();30To = To.getNonReferenceType();3132// If both are pointer types, work with the pointee types.33if (isa<PointerType>(From) && isa<PointerType>(To)) {34From = S.Context.getCanonicalType(35(cast<PointerType>(From))->getPointeeType());36To = S.Context.getCanonicalType(37(cast<PointerType>(To))->getPointeeType());38}3940const CanQualType FromUnq = From.getUnqualifiedType();41const CanQualType ToUnq = To.getUnqualifiedType();4243if ((FromUnq == ToUnq || (S.IsDerivedFrom(Loc, FromUnq, ToUnq)) ) &&44To.isAtLeastAsQualifiedAs(From))45return true;46return false;47}4849bool ConversionFixItGenerator::tryToFixConversion(const Expr *FullExpr,50const QualType FromTy,51const QualType ToTy,52Sema &S) {53if (!FullExpr)54return false;5556const CanQualType FromQTy = S.Context.getCanonicalType(FromTy);57const CanQualType ToQTy = S.Context.getCanonicalType(ToTy);58const SourceLocation Begin = FullExpr->getSourceRange().getBegin();59const SourceLocation End = S.getLocForEndOfToken(FullExpr->getSourceRange()60.getEnd());6162// Strip the implicit casts - those are implied by the compiler, not the63// original source code.64const Expr* Expr = FullExpr->IgnoreImpCasts();6566bool NeedParen = true;67if (isa<ArraySubscriptExpr>(Expr) ||68isa<CallExpr>(Expr) ||69isa<DeclRefExpr>(Expr) ||70isa<CastExpr>(Expr) ||71isa<CXXNewExpr>(Expr) ||72isa<CXXConstructExpr>(Expr) ||73isa<CXXDeleteExpr>(Expr) ||74isa<CXXNoexceptExpr>(Expr) ||75isa<CXXPseudoDestructorExpr>(Expr) ||76isa<CXXScalarValueInitExpr>(Expr) ||77isa<CXXThisExpr>(Expr) ||78isa<CXXTypeidExpr>(Expr) ||79isa<CXXUnresolvedConstructExpr>(Expr) ||80isa<ObjCMessageExpr>(Expr) ||81isa<ObjCPropertyRefExpr>(Expr) ||82isa<ObjCProtocolExpr>(Expr) ||83isa<MemberExpr>(Expr) ||84isa<ParenExpr>(FullExpr) ||85isa<ParenListExpr>(Expr) ||86isa<SizeOfPackExpr>(Expr) ||87isa<UnaryOperator>(Expr))88NeedParen = false;8990// Check if the argument needs to be dereferenced:91// (type * -> type) or (type * -> type &).92if (const PointerType *FromPtrTy = dyn_cast<PointerType>(FromQTy)) {93OverloadFixItKind FixKind = OFIK_Dereference;9495bool CanConvert = CompareTypes(96S.Context.getCanonicalType(FromPtrTy->getPointeeType()), ToQTy,97S, Begin, VK_LValue);98if (CanConvert) {99// Do not suggest dereferencing a Null pointer.100if (Expr->IgnoreParenCasts()->101isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull))102return false;103104if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(Expr)) {105if (UO->getOpcode() == UO_AddrOf) {106FixKind = OFIK_RemoveTakeAddress;107Hints.push_back(FixItHint::CreateRemoval(108CharSourceRange::getTokenRange(Begin, Begin)));109}110} else if (NeedParen) {111Hints.push_back(FixItHint::CreateInsertion(Begin, "*("));112Hints.push_back(FixItHint::CreateInsertion(End, ")"));113} else {114Hints.push_back(FixItHint::CreateInsertion(Begin, "*"));115}116117NumConversionsFixed++;118if (NumConversionsFixed == 1)119Kind = FixKind;120return true;121}122}123124// Check if the pointer to the argument needs to be passed:125// (type -> type *) or (type & -> type *).126if (const auto *ToPtrTy = dyn_cast<PointerType>(ToQTy)) {127bool CanConvert = false;128OverloadFixItKind FixKind = OFIK_TakeAddress;129130// Only suggest taking address of L-values.131if (!Expr->isLValue() || Expr->getObjectKind() != OK_Ordinary)132return false;133134// Do no take address of const pointer to get void*135if (isa<PointerType>(FromQTy) && ToPtrTy->isVoidPointerType())136return false;137138CanConvert = CompareTypes(S.Context.getPointerType(FromQTy), ToQTy, S,139Begin, VK_PRValue);140if (CanConvert) {141142if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(Expr)) {143if (UO->getOpcode() == UO_Deref) {144FixKind = OFIK_RemoveDereference;145Hints.push_back(FixItHint::CreateRemoval(146CharSourceRange::getTokenRange(Begin, Begin)));147}148} else if (NeedParen) {149Hints.push_back(FixItHint::CreateInsertion(Begin, "&("));150Hints.push_back(FixItHint::CreateInsertion(End, ")"));151} else {152Hints.push_back(FixItHint::CreateInsertion(Begin, "&"));153}154155NumConversionsFixed++;156if (NumConversionsFixed == 1)157Kind = FixKind;158return true;159}160}161162return false;163}164165static bool isMacroDefined(const Sema &S, SourceLocation Loc, StringRef Name) {166return (bool)S.PP.getMacroDefinitionAtLoc(&S.getASTContext().Idents.get(Name),167Loc);168}169170static std::string getScalarZeroExpressionForType(171const Type &T, SourceLocation Loc, const Sema &S) {172assert(T.isScalarType() && "use scalar types only");173// Suggest "0" for non-enumeration scalar types, unless we can find a174// better initializer.175if (T.isEnumeralType())176return std::string();177if ((T.isObjCObjectPointerType() || T.isBlockPointerType()) &&178isMacroDefined(S, Loc, "nil"))179return "nil";180if (T.isRealFloatingType())181return "0.0";182if (T.isBooleanType() &&183(S.LangOpts.CPlusPlus || isMacroDefined(S, Loc, "false")))184return "false";185if (T.isPointerType() || T.isMemberPointerType()) {186if (S.LangOpts.CPlusPlus11)187return "nullptr";188if (isMacroDefined(S, Loc, "NULL"))189return "NULL";190}191if (T.isCharType())192return "'\\0'";193if (T.isWideCharType())194return "L'\\0'";195if (T.isChar16Type())196return "u'\\0'";197if (T.isChar32Type())198return "U'\\0'";199return "0";200}201202std::string203Sema::getFixItZeroInitializerForType(QualType T, SourceLocation Loc) const {204if (T->isScalarType()) {205std::string s = getScalarZeroExpressionForType(*T, Loc, *this);206if (!s.empty())207s = " = " + s;208return s;209}210211const CXXRecordDecl *RD = T->getAsCXXRecordDecl();212if (!RD || !RD->hasDefinition())213return std::string();214if (LangOpts.CPlusPlus11 && !RD->hasUserProvidedDefaultConstructor())215return "{}";216if (RD->isAggregate())217return " = {}";218return std::string();219}220221std::string222Sema::getFixItZeroLiteralForType(QualType T, SourceLocation Loc) const {223return getScalarZeroExpressionForType(*T, Loc, *this);224}225226227