1 //===--- SemaFixItUtils.h - 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 #ifndef LLVM_CLANG_SEMA_FIXITUTILS_H 14 #define LLVM_CLANG_SEMA_FIXITUTILS_H 15 16 #include "clang/AST/Expr.h" 17 18 namespace clang { 19 20 enum OverloadFixItKind { 21 OFIK_Undefined = 0, 22 OFIK_Dereference, 23 OFIK_TakeAddress, 24 OFIK_RemoveDereference, 25 OFIK_RemoveTakeAddress 26 }; 27 28 class Sema; 29 30 /// The class facilities generation and storage of conversion FixIts. Hints for 31 /// new conversions are added using TryToFixConversion method. The default type 32 /// conversion checker can be reset. 33 struct ConversionFixItGenerator { 34 /// Performs a simple check to see if From type can be converted to To type. 35 static bool compareTypesSimple(CanQualType From, 36 CanQualType To, 37 Sema &S, 38 SourceLocation Loc, 39 ExprValueKind FromVK); 40 41 /// The list of Hints generated so far. 42 std::vector<FixItHint> Hints; 43 44 /// The number of Conversions fixed. This can be different from the size 45 /// of the Hints vector since we allow multiple FixIts per conversion. 46 unsigned NumConversionsFixed; 47 48 /// The type of fix applied. If multiple conversions are fixed, corresponds 49 /// to the kid of the very first conversion. 50 OverloadFixItKind Kind; 51 52 typedef bool (*TypeComparisonFuncTy) (const CanQualType FromTy, 53 const CanQualType ToTy, 54 Sema &S, 55 SourceLocation Loc, 56 ExprValueKind FromVK); 57 /// The type comparison function used to decide if expression FromExpr of 58 /// type FromTy can be converted to ToTy. For example, one could check if 59 /// an implicit conversion exists. Returns true if comparison exists. 60 TypeComparisonFuncTy CompareTypes; 61 62 ConversionFixItGenerator(TypeComparisonFuncTy Foo): NumConversionsFixed(0), 63 Kind(OFIK_Undefined), 64 CompareTypes(Foo) {} 65 66 ConversionFixItGenerator(): NumConversionsFixed(0), 67 Kind(OFIK_Undefined), 68 CompareTypes(compareTypesSimple) {} 69 70 /// Resets the default conversion checker method. 71 void setConversionChecker(TypeComparisonFuncTy Foo) { 72 CompareTypes = Foo; 73 } 74 75 /// If possible, generates and stores a fix for the given conversion. 76 bool tryToFixConversion(const Expr *FromExpr, 77 const QualType FromQTy, const QualType ToQTy, 78 Sema &S); 79 80 void clear() { 81 Hints.clear(); 82 NumConversionsFixed = 0; 83 } 84 85 bool isNull() { 86 return (NumConversionsFixed == 0); 87 } 88 }; 89 90 } // endof namespace clang 91 #endif // LLVM_CLANG_SEMA_FIXITUTILS_H 92