1*7330f729Sjoerg //===--- SemaFixItUtils.h - Sema FixIts -------------------------*- C++ -*-===// 2*7330f729Sjoerg // 3*7330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*7330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information. 5*7330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*7330f729Sjoerg // 7*7330f729Sjoerg //===----------------------------------------------------------------------===// 8*7330f729Sjoerg // 9*7330f729Sjoerg // This file defines helper classes for generation of Sema FixItHints. 10*7330f729Sjoerg // 11*7330f729Sjoerg //===----------------------------------------------------------------------===// 12*7330f729Sjoerg #ifndef LLVM_CLANG_SEMA_SEMAFIXITUTILS_H 13*7330f729Sjoerg #define LLVM_CLANG_SEMA_SEMAFIXITUTILS_H 14*7330f729Sjoerg 15*7330f729Sjoerg #include "clang/AST/Expr.h" 16*7330f729Sjoerg 17*7330f729Sjoerg namespace clang { 18*7330f729Sjoerg 19*7330f729Sjoerg enum OverloadFixItKind { 20*7330f729Sjoerg OFIK_Undefined = 0, 21*7330f729Sjoerg OFIK_Dereference, 22*7330f729Sjoerg OFIK_TakeAddress, 23*7330f729Sjoerg OFIK_RemoveDereference, 24*7330f729Sjoerg OFIK_RemoveTakeAddress 25*7330f729Sjoerg }; 26*7330f729Sjoerg 27*7330f729Sjoerg class Sema; 28*7330f729Sjoerg 29*7330f729Sjoerg /// The class facilities generation and storage of conversion FixIts. Hints for 30*7330f729Sjoerg /// new conversions are added using TryToFixConversion method. The default type 31*7330f729Sjoerg /// conversion checker can be reset. 32*7330f729Sjoerg struct ConversionFixItGenerator { 33*7330f729Sjoerg /// Performs a simple check to see if From type can be converted to To type. 34*7330f729Sjoerg static bool compareTypesSimple(CanQualType From, 35*7330f729Sjoerg CanQualType To, 36*7330f729Sjoerg Sema &S, 37*7330f729Sjoerg SourceLocation Loc, 38*7330f729Sjoerg ExprValueKind FromVK); 39*7330f729Sjoerg 40*7330f729Sjoerg /// The list of Hints generated so far. 41*7330f729Sjoerg std::vector<FixItHint> Hints; 42*7330f729Sjoerg 43*7330f729Sjoerg /// The number of Conversions fixed. This can be different from the size 44*7330f729Sjoerg /// of the Hints vector since we allow multiple FixIts per conversion. 45*7330f729Sjoerg unsigned NumConversionsFixed; 46*7330f729Sjoerg 47*7330f729Sjoerg /// The type of fix applied. If multiple conversions are fixed, corresponds 48*7330f729Sjoerg /// to the kid of the very first conversion. 49*7330f729Sjoerg OverloadFixItKind Kind; 50*7330f729Sjoerg 51*7330f729Sjoerg typedef bool (*TypeComparisonFuncTy) (const CanQualType FromTy, 52*7330f729Sjoerg const CanQualType ToTy, 53*7330f729Sjoerg Sema &S, 54*7330f729Sjoerg SourceLocation Loc, 55*7330f729Sjoerg ExprValueKind FromVK); 56*7330f729Sjoerg /// The type comparison function used to decide if expression FromExpr of 57*7330f729Sjoerg /// type FromTy can be converted to ToTy. For example, one could check if 58*7330f729Sjoerg /// an implicit conversion exists. Returns true if comparison exists. 59*7330f729Sjoerg TypeComparisonFuncTy CompareTypes; 60*7330f729Sjoerg ConversionFixItGeneratorConversionFixItGenerator61*7330f729Sjoerg ConversionFixItGenerator(TypeComparisonFuncTy Foo): NumConversionsFixed(0), 62*7330f729Sjoerg Kind(OFIK_Undefined), 63*7330f729Sjoerg CompareTypes(Foo) {} 64*7330f729Sjoerg ConversionFixItGeneratorConversionFixItGenerator65*7330f729Sjoerg ConversionFixItGenerator(): NumConversionsFixed(0), 66*7330f729Sjoerg Kind(OFIK_Undefined), 67*7330f729Sjoerg CompareTypes(compareTypesSimple) {} 68*7330f729Sjoerg 69*7330f729Sjoerg /// Resets the default conversion checker method. setConversionCheckerConversionFixItGenerator70*7330f729Sjoerg void setConversionChecker(TypeComparisonFuncTy Foo) { 71*7330f729Sjoerg CompareTypes = Foo; 72*7330f729Sjoerg } 73*7330f729Sjoerg 74*7330f729Sjoerg /// If possible, generates and stores a fix for the given conversion. 75*7330f729Sjoerg bool tryToFixConversion(const Expr *FromExpr, 76*7330f729Sjoerg const QualType FromQTy, const QualType ToQTy, 77*7330f729Sjoerg Sema &S); 78*7330f729Sjoerg clearConversionFixItGenerator79*7330f729Sjoerg void clear() { 80*7330f729Sjoerg Hints.clear(); 81*7330f729Sjoerg NumConversionsFixed = 0; 82*7330f729Sjoerg } 83*7330f729Sjoerg isNullConversionFixItGenerator84*7330f729Sjoerg bool isNull() { 85*7330f729Sjoerg return (NumConversionsFixed == 0); 86*7330f729Sjoerg } 87*7330f729Sjoerg }; 88*7330f729Sjoerg 89*7330f729Sjoerg } // endof namespace clang 90*7330f729Sjoerg #endif 91