diff --git a/clang-tools-extra/clang-tidy/abseil/DurationRewriter.cpp b/clang-tools-extra/clang-tidy/abseil/DurationRewriter.cpp --- a/clang-tools-extra/clang-tidy/abseil/DurationRewriter.cpp +++ b/clang-tools-extra/clang-tidy/abseil/DurationRewriter.cpp @@ -208,7 +208,7 @@ if (const auto *LitFloat = llvm::dyn_cast(&Node)) // Attempt to simplify a `Duration` factory call with a literal argument. if (llvm::Optional IntValue = truncateIfIntegral(*LitFloat)) - return IntValue->toString(/*radix=*/10); + return toString(*IntValue, /*radix=*/10); return llvm::None; } diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp @@ -99,7 +99,7 @@ if (Index->isSigned() && Index->isNegative()) { diag(Matched->getExprLoc(), "std::array<> index %0 is negative") - << Index->toString(10); + << toString(*Index, 10); return; } @@ -118,7 +118,7 @@ diag(Matched->getExprLoc(), "std::array<> index %0 is past the end of the array " "(which contains %1 elements)") - << Index->toString(10) << ArraySize.toString(10, false); + << toString(*Index, 10) << toString(ArraySize, 10, false); } } diff --git a/clang-tools-extra/clangd/DumpAST.cpp b/clang-tools-extra/clangd/DumpAST.cpp --- a/clang-tools-extra/clangd/DumpAST.cpp +++ b/clang-tools-extra/clangd/DumpAST.cpp @@ -290,7 +290,7 @@ } std::string getDetail(const TemplateArgumentLoc &TAL) { if (TAL.getArgument().getKind() == TemplateArgument::Integral) - return TAL.getArgument().getAsIntegral().toString(10); + return toString(TAL.getArgument().getAsIntegral(), 10); return ""; } std::string getDetail(const TemplateName &TN) { diff --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp --- a/clang-tools-extra/clangd/Hover.cpp +++ b/clang-tools-extra/clangd/Hover.cpp @@ -587,7 +587,7 @@ } else if (const auto *ECD = dyn_cast(D)) { // Dependent enums (e.g. nested in template classes) don't have values yet. if (!ECD->getType()->isDependentType()) - HI.Value = ECD->getInitVal().toString(10); + HI.Value = toString(ECD->getInitVal(), 10); } HI.Definition = printDefinition(D, PP); diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h --- a/clang/include/clang/ASTMatchers/ASTMatchers.h +++ b/clang/include/clang/ASTMatchers/ASTMatchers.h @@ -1213,7 +1213,7 @@ std::string, Value) { if (Node.getKind() != TemplateArgument::Integral) return false; - return Node.getAsIntegral().toString(10) == Value; + return toString(Node.getAsIntegral(), 10) == Value; } /// Matches an Objective-C autorelease pool statement. diff --git a/clang/lib/AST/ASTDiagnostic.cpp b/clang/lib/AST/ASTDiagnostic.cpp --- a/clang/lib/AST/ASTDiagnostic.cpp +++ b/clang/lib/AST/ASTDiagnostic.cpp @@ -19,6 +19,7 @@ #include "clang/AST/ExprCXX.h" #include "clang/AST/TemplateBase.h" #include "clang/AST/Type.h" +#include "llvm/ADT/StringExtras.h" #include "llvm/Support/raw_ostream.h" using namespace clang; @@ -1756,7 +1757,7 @@ if (FromIntType->isBooleanType()) { OS << ((FromInt == 0) ? "false" : "true"); } else { - OS << FromInt.toString(10); + OS << toString(FromInt, 10); } return; } @@ -1800,7 +1801,7 @@ if (IntType->isBooleanType()) { OS << ((Val == 0) ? "false" : "true"); } else { - OS << Val.toString(10); + OS << toString(Val, 10); } } else if (E) { PrintExpr(E); diff --git a/clang/lib/AST/ASTStructuralEquivalence.cpp b/clang/lib/AST/ASTStructuralEquivalence.cpp --- a/clang/lib/AST/ASTStructuralEquivalence.cpp +++ b/clang/lib/AST/ASTStructuralEquivalence.cpp @@ -86,6 +86,7 @@ #include "llvm/ADT/APSInt.h" #include "llvm/ADT/None.h" #include "llvm/ADT/Optional.h" +#include "llvm/ADT/StringExtras.h" #include "llvm/Support/Casting.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/ErrorHandling.h" @@ -1623,7 +1624,7 @@ diag::err_odr_tag_type_inconsistent)) << Context.ToCtx.getTypeDeclType(D2); Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator) - << EC1->getDeclName() << EC1->getInitVal().toString(10); + << EC1->getDeclName() << toString(EC1->getInitVal(), 10); Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator); } return false; @@ -1639,9 +1640,9 @@ diag::err_odr_tag_type_inconsistent)) << Context.ToCtx.getTypeDeclType(D2); Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator) - << EC2->getDeclName() << EC2->getInitVal().toString(10); + << EC2->getDeclName() << toString(EC2->getInitVal(), 10); Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator) - << EC1->getDeclName() << EC1->getInitVal().toString(10); + << EC1->getDeclName() << toString(EC1->getInitVal(), 10); } return false; } @@ -1653,7 +1654,7 @@ diag::err_odr_tag_type_inconsistent)) << Context.ToCtx.getTypeDeclType(D2); Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator) - << EC2->getDeclName() << EC2->getInitVal().toString(10); + << EC2->getDeclName() << toString(EC2->getInitVal(), 10); Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator); } return false; diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -2720,7 +2720,7 @@ if (Info.checkingForUndefinedBehavior()) Info.Ctx.getDiagnostics().Report(E->getExprLoc(), diag::warn_integer_constant_overflow) - << Result.toString(10) << E->getType(); + << toString(Result, 10) << E->getType(); return HandleOverflow(Info, E, Value, E->getType()); } return true; @@ -6964,7 +6964,7 @@ llvm::NoneType unrepresentableValue(QualType Ty, const APSInt &Val) { Info.FFDiag(BCE->getBeginLoc(), diag::note_constexpr_bit_cast_unrepresentable_value) - << Ty << Val.toString(/*Radix=*/10); + << Ty << toString(Val, /*Radix=*/10); return None; } @@ -9265,7 +9265,7 @@ llvm::APInt::udivrem(OrigN, TSize, N, Remainder); if (Remainder) { Info.FFDiag(E, diag::note_constexpr_memcpy_unsupported) - << Move << WChar << 0 << T << OrigN.toString(10, /*Signed*/false) + << Move << WChar << 0 << T << toString(OrigN, 10, /*Signed*/false) << (unsigned)TSize; return false; } @@ -9279,7 +9279,7 @@ if (N.ugt(RemainingSrcSize) || N.ugt(RemainingDestSize)) { Info.FFDiag(E, diag::note_constexpr_memcpy_unsupported) << Move << WChar << (N.ugt(RemainingSrcSize) ? 1 : 2) << T - << N.toString(10, /*Signed*/false); + << toString(N, 10, /*Signed*/false); return false; } uint64_t NElems = N.getZExtValue(); @@ -9454,8 +9454,8 @@ return ZeroInitialization(E); Info.FFDiag(*ArraySize, diag::note_constexpr_new_too_small) - << AllocBound.toString(10, /*Signed=*/false) - << InitBound.toString(10, /*Signed=*/false) + << toString(AllocBound, 10, /*Signed=*/false) + << toString(InitBound, 10, /*Signed=*/false) << (*ArraySize)->getSourceRange(); return false; } diff --git a/clang/lib/AST/StmtPrinter.cpp b/clang/lib/AST/StmtPrinter.cpp --- a/clang/lib/AST/StmtPrinter.cpp +++ b/clang/lib/AST/StmtPrinter.cpp @@ -47,6 +47,7 @@ #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Casting.h" #include "llvm/Support/Compiler.h" @@ -1122,7 +1123,7 @@ if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context)) return; bool isSigned = Node->getType()->isSignedIntegerType(); - OS << Node->getValue().toString(10, isSigned); + OS << toString(Node->getValue(), 10, isSigned); // Emit suffixes. Integer literals are always a builtin integer type. switch (Node->getType()->castAs()->getKind()) { @@ -1867,7 +1868,7 @@ case UserDefinedLiteral::LOK_Integer: { // Print integer literal without suffix. const auto *Int = cast(Node->getCookedLiteral()); - OS << Int->getValue().toString(10, /*isSigned*/false); + OS << toString(Int->getValue(), 10, /*isSigned*/false); break; } case UserDefinedLiteral::LOK_Floating: { diff --git a/clang/lib/AST/TemplateBase.cpp b/clang/lib/AST/TemplateBase.cpp --- a/clang/lib/AST/TemplateBase.cpp +++ b/clang/lib/AST/TemplateBase.cpp @@ -31,6 +31,7 @@ #include "llvm/ADT/FoldingSet.h" #include "llvm/ADT/None.h" #include "llvm/ADT/SmallString.h" +#include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Casting.h" #include "llvm/Support/Compiler.h" @@ -554,7 +555,7 @@ return DB << "nullptr"; case TemplateArgument::Integral: - return DB << Arg.getAsIntegral().toString(10); + return DB << toString(Arg.getAsIntegral(), 10); case TemplateArgument::Template: return DB << Arg.getAsTemplate(); diff --git a/clang/lib/AST/TextNodeDumper.cpp b/clang/lib/AST/TextNodeDumper.cpp --- a/clang/lib/AST/TextNodeDumper.cpp +++ b/clang/lib/AST/TextNodeDumper.cpp @@ -21,6 +21,7 @@ #include "clang/Basic/SourceManager.h" #include "clang/Basic/Specifiers.h" #include "clang/Basic/TypeTraits.h" +#include "llvm/ADT/StringExtras.h" #include #include @@ -1035,7 +1036,7 @@ void TextNodeDumper::VisitIntegerLiteral(const IntegerLiteral *Node) { bool isSigned = Node->getType()->isSignedIntegerType(); ColorScope Color(OS, ShowColors, ValueColor); - OS << " " << Node->getValue().toString(10, isSigned); + OS << " " << toString(Node->getValue(), 10, isSigned); } void TextNodeDumper::VisitFixedPointLiteral(const FixedPointLiteral *Node) { diff --git a/clang/lib/Analysis/ThreadSafetyCommon.cpp b/clang/lib/Analysis/ThreadSafetyCommon.cpp --- a/clang/lib/Analysis/ThreadSafetyCommon.cpp +++ b/clang/lib/Analysis/ThreadSafetyCommon.cpp @@ -26,6 +26,7 @@ #include "clang/Basic/LLVM.h" #include "clang/Basic/OperatorKinds.h" #include "clang/Basic/Specifiers.h" +#include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Casting.h" #include @@ -40,7 +41,7 @@ std::string threadSafety::getSourceLiteralString(const Expr *CE) { switch (CE->getStmtClass()) { case Stmt::IntegerLiteralClass: - return cast(CE)->getValue().toString(10, true); + return toString(cast(CE)->getValue(), 10, true); case Stmt::StringLiteralClass: { std::string ret("\""); ret += cast(CE)->getString(); diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -9441,7 +9441,7 @@ llvm::APSInt Value = Result.Val.getInt(); LLVMContext &Context = CGM.getLLVMContext(); - std::string Reg = Value == 31 ? "sp" : "x" + Value.toString(10); + std::string Reg = Value == 31 ? "sp" : "x" + toString(Value, 10); llvm::Metadata *Ops[] = {llvm::MDString::get(Context, Reg)}; llvm::MDNode *RegName = llvm::MDNode::get(Context, Ops); diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -206,7 +206,7 @@ // created with -funique-internal-linakge-symbols and the tools can strip or // keep the prefix as needed. ModuleNameHash = (Twine(".__uniq.") + - Twine(IntHash.toString(/* Radix = */ 10, /* Signed = */false))).str(); + Twine(toString(IntHash, /* Radix = */ 10, /* Signed = */false))).str(); } } diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -4581,7 +4581,7 @@ if (!SanHash.empty()) code = hash_combine(code, SanHash.Mask); - return llvm::APInt(64, code).toString(36, /*Signed=*/false); + return toString(llvm::APInt(64, code), 36, /*Signed=*/false); } void CompilerInvocation::generateCC1CommandLine( diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp --- a/clang/lib/Frontend/InitPreprocessor.cpp +++ b/clang/lib/Frontend/InitPreprocessor.cpp @@ -168,7 +168,7 @@ MacroBuilder &Builder) { llvm::APInt MaxVal = isSigned ? llvm::APInt::getSignedMaxValue(TypeWidth) : llvm::APInt::getMaxValue(TypeWidth); - Builder.defineMacro(MacroName, MaxVal.toString(10, isSigned) + ValSuffix); + Builder.defineMacro(MacroName, toString(MaxVal, 10, isSigned) + ValSuffix); } /// DefineTypeSize - An overloaded helper that uses TargetInfo to determine diff --git a/clang/lib/Frontend/Rewrite/RewriteModernObjC.cpp b/clang/lib/Frontend/Rewrite/RewriteModernObjC.cpp --- a/clang/lib/Frontend/Rewrite/RewriteModernObjC.cpp +++ b/clang/lib/Frontend/Rewrite/RewriteModernObjC.cpp @@ -3674,8 +3674,7 @@ Result += " {\n"; for (const auto *EC : ED->enumerators()) { Result += "\t"; Result += EC->getName(); Result += " = "; - llvm::APSInt Val = EC->getInitVal(); - Result += Val.toString(10); + Result += toString(EC->getInitVal(), 10); Result += ",\n"; } Result += "\t} "; diff --git a/clang/lib/Lex/PPExpressions.cpp b/clang/lib/Lex/PPExpressions.cpp --- a/clang/lib/Lex/PPExpressions.cpp +++ b/clang/lib/Lex/PPExpressions.cpp @@ -666,13 +666,13 @@ if (ValueLive && Res.isUnsigned()) { if (!LHS.isUnsigned() && LHS.Val.isNegative()) PP.Diag(OpLoc, diag::warn_pp_convert_to_positive) << 0 - << LHS.Val.toString(10, true) + " to " + - LHS.Val.toString(10, false) + << toString(LHS.Val, 10, true) + " to " + + toString(LHS.Val, 10, false) << LHS.getRange() << RHS.getRange(); if (!RHS.isUnsigned() && RHS.Val.isNegative()) PP.Diag(OpLoc, diag::warn_pp_convert_to_positive) << 1 - << RHS.Val.toString(10, true) + " to " + - RHS.Val.toString(10, false) + << toString(RHS.Val, 10, true) + " to " + + toString(RHS.Val, 10, false) << LHS.getRange() << RHS.getRange(); } LHS.Val.setIsUnsigned(Res.isUnsigned()); diff --git a/clang/lib/Sema/SemaCast.cpp b/clang/lib/Sema/SemaCast.cpp --- a/clang/lib/Sema/SemaCast.cpp +++ b/clang/lib/Sema/SemaCast.cpp @@ -2917,7 +2917,7 @@ } Self.Diag(OpRange.getBegin(), diag::err_opencl_cast_non_zero_to_event_t) - << CastInt.toString(10) << SrcExpr.get()->getSourceRange(); + << toString(CastInt, 10) << SrcExpr.get()->getSourceRange(); SrcExpr = ExprError(); return; } diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -253,7 +253,7 @@ } if (llvm::APSInt::compareValues(AlignValue, MaxValue) > 0) { S.Diag(AlignOp->getExprLoc(), diag::err_alignment_too_big) - << MaxValue.toString(10); + << toString(MaxValue, 10); return true; } if (!AlignValue.isPowerOf2()) { @@ -772,8 +772,8 @@ DiagRuntimeBehavior(TheCall->getBeginLoc(), TheCall, PDiag(DiagID) - << FunctionName << ObjectSize.toString(/*Radix=*/10) - << UsedSize.getValue().toString(/*Radix=*/10)); + << FunctionName << toString(ObjectSize, /*Radix=*/10) + << toString(UsedSize.getValue(), /*Radix=*/10)); } static bool SemaBuiltinSEHScopeCheck(Sema &SemaRef, CallExpr *TheCall, @@ -6620,13 +6620,13 @@ if (Result.getSExtValue() < Low || Result.getSExtValue() > High) { if (RangeIsError) return Diag(TheCall->getBeginLoc(), diag::err_argument_invalid_range) - << Result.toString(10) << Low << High << Arg->getSourceRange(); + << toString(Result, 10) << Low << High << Arg->getSourceRange(); else // Defer the warning until we know if the code will be emitted so that // dead code can ignore this. DiagRuntimeBehavior(TheCall->getBeginLoc(), TheCall, PDiag(diag::warn_argument_invalid_range) - << Result.toString(10) << Low << High + << toString(Result, 10) << Low << High << Arg->getSourceRange()); } @@ -11743,8 +11743,8 @@ if (FieldWidth == 1 && Value == 1) return false; - std::string PrettyValue = Value.toString(10); - std::string PrettyTrunc = TruncatedValue.toString(10); + std::string PrettyValue = toString(Value, 10); + std::string PrettyTrunc = toString(TruncatedValue, 10); S.Diag(InitLoc, diag::warn_impcast_bitfield_precision_constant) << PrettyValue << PrettyTrunc << OriginalInit->getType() @@ -11980,7 +11980,7 @@ llvm::APSInt ValueInRange = Value; ValueInRange.setIsSigned(!Range.NonNegative); ValueInRange = ValueInRange.trunc(Range.Width); - return ValueInRange.toString(10); + return toString(ValueInRange, 10); } static bool IsImplicitBoolFloatConversion(Sema &S, Expr *Ex, bool ToBool) { @@ -12296,7 +12296,7 @@ adornObjCBoolConversionDiagWithTernaryFixit( S, E, S.Diag(CC, diag::warn_impcast_constant_value_to_objc_bool) - << Result.Val.getInt().toString(10)); + << toString(Result.Val.getInt(), 10)); } return; } @@ -12480,7 +12480,7 @@ if (Overflowed) { S.DiagRuntimeBehavior(E->getExprLoc(), E, S.PDiag(diag::warn_impcast_fixed_point_range) - << Value.toString(/*Radix=*/10) << T + << toString(Value, /*Radix=*/10) << T << E->getSourceRange() << clang::SourceRange(CC)); return; @@ -12520,7 +12520,8 @@ llvm::APFloat::rmNearestTiesToEven); if (ConversionStatus != llvm::APFloat::opOK) { - std::string PrettySourceValue = SourceInt->toString(10); + SmallString<32> PrettySourceValue; + SourceInt->toString(PrettySourceValue, 10); SmallString<32> PrettyTargetValue; TargetFloatValue.toString(PrettyTargetValue, TargetPrecision); @@ -12579,7 +12580,7 @@ if (S.SourceMgr.isInSystemMacro(CC)) return; - std::string PrettySourceValue = Value.toString(10); + std::string PrettySourceValue = toString(Value, 10); std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange); S.DiagRuntimeBehavior( @@ -12625,7 +12626,7 @@ !S.SourceMgr.isInSystemMacro(CC)) { llvm::APSInt Value = Result.Val.getInt(); if (isSameWidthConstantConversion(S, E, T, CC)) { - std::string PrettySourceValue = Value.toString(10); + std::string PrettySourceValue = toString(Value, 10); std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange); S.DiagRuntimeBehavior( @@ -14623,8 +14624,8 @@ DiagID = diag::warn_array_index_exceeds_bounds; DiagRuntimeBehavior(BaseExpr->getBeginLoc(), BaseExpr, - PDiag(DiagID) << index.toString(10, true) - << size.toString(10, true) + PDiag(DiagID) << toString(index, 10, true) + << toString(size, 10, true) << (unsigned)size.getLimitedValue(~0U) << IndexExpr->getSourceRange()); } else { @@ -14635,7 +14636,7 @@ } DiagRuntimeBehavior(BaseExpr->getBeginLoc(), BaseExpr, - PDiag(DiagID) << index.toString(10, true) + PDiag(DiagID) << toString(index, 10, true) << IndexExpr->getSourceRange()); } diff --git a/clang/lib/Sema/SemaConcept.cpp b/clang/lib/Sema/SemaConcept.cpp --- a/clang/lib/Sema/SemaConcept.cpp +++ b/clang/lib/Sema/SemaConcept.cpp @@ -25,6 +25,8 @@ #include "clang/Basic/OperatorPrecedence.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/PointerUnion.h" +#include "llvm/ADT/StringExtras.h" + using namespace clang; using namespace sema; @@ -573,9 +575,9 @@ S.Diag(SubstExpr->getBeginLoc(), diag::note_atomic_constraint_evaluated_to_false_elaborated) << (int)First << SubstExpr - << SimplifiedLHS.Val.getInt().toString(10) + << toString(SimplifiedLHS.Val.getInt(), 10) << BinaryOperator::getOpcodeStr(BO->getOpcode()) - << SimplifiedRHS.Val.getInt().toString(10); + << toString(SimplifiedRHS.Val.getInt(), 10); return; } } diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -6135,7 +6135,7 @@ if (SizeIsNegative) Diag(Loc, diag::err_typecheck_negative_array_size); else if (Oversized.getBoolValue()) - Diag(Loc, diag::err_array_too_large) << Oversized.toString(10); + Diag(Loc, diag::err_array_too_large) << toString(Oversized, 10); else if (FailedFoldDiagID) Diag(Loc, FailedFoldDiagID); return false; @@ -6255,7 +6255,7 @@ Diag(NewTD->getLocation(), diag::err_vla_decl_in_file_scope); else if (Oversized.getBoolValue()) Diag(NewTD->getLocation(), diag::err_array_too_large) - << Oversized.toString(10); + << toString(Oversized, 10); else Diag(NewTD->getLocation(), diag::err_vm_decl_in_file_scope); NewTD->setInvalidDecl(); @@ -16669,16 +16669,16 @@ if (Value.isSigned() && Value.isNegative()) { if (FieldName) return Diag(FieldLoc, diag::err_bitfield_has_negative_width) - << FieldName << Value.toString(10); + << FieldName << toString(Value, 10); return Diag(FieldLoc, diag::err_anon_bitfield_has_negative_width) - << Value.toString(10); + << toString(Value, 10); } // The size of the bit-field must not exceed our maximum permitted object // size. if (Value.getActiveBits() > ConstantArrayType::getMaxSizeBits(Context)) { return Diag(FieldLoc, diag::err_bitfield_too_wide) - << !FieldName << FieldName << Value.toString(10); + << !FieldName << FieldName << toString(Value, 10); } if (!FieldTy->isDependentType()) { @@ -16698,11 +16698,11 @@ CStdConstraintViolation ? TypeWidth : TypeStorageSize; if (FieldName) return Diag(FieldLoc, diag::err_bitfield_width_exceeds_type_width) - << FieldName << Value.toString(10) + << FieldName << toString(Value, 10) << !CStdConstraintViolation << DiagWidth; return Diag(FieldLoc, diag::err_anon_bitfield_width_exceeds_type_width) - << Value.toString(10) << !CStdConstraintViolation + << toString(Value, 10) << !CStdConstraintViolation << DiagWidth; } @@ -16711,7 +16711,7 @@ // 'bool'. if (BitfieldIsOverwide && !FieldTy->isBooleanType() && FieldName) { Diag(FieldLoc, diag::warn_bitfield_width_exceeds_type_width) - << FieldName << Value.toString(10) + << FieldName << toString(Value, 10) << (unsigned)TypeWidth; } } @@ -17823,7 +17823,7 @@ // Complain if the value is not representable in an int. if (!isRepresentableIntegerValue(Context, EnumVal, Context.IntTy)) Diag(IdLoc, diag::ext_enum_value_not_int) - << EnumVal.toString(10) << Val->getSourceRange() + << toString(EnumVal, 10) << Val->getSourceRange() << (EnumVal.isUnsigned() || EnumVal.isNonNegative()); else if (!Context.hasSameType(Val->getType(), Context.IntTy)) { // Force the type of the expression to 'int'. @@ -17881,11 +17881,11 @@ if (Enum->isFixed()) // When the underlying type is fixed, this is ill-formed. Diag(IdLoc, diag::err_enumerator_wrapped) - << EnumVal.toString(10) + << toString(EnumVal, 10) << EltTy; else Diag(IdLoc, diag::ext_enumerator_increment_too_large) - << EnumVal.toString(10); + << toString(EnumVal, 10); } else { EltTy = T; } @@ -17909,7 +17909,7 @@ !isRepresentableIntegerValue(Context, EnumVal, EltTy)) { // Enforce C99 6.7.2.2p2 even when we compute the next value. Diag(IdLoc, diag::ext_enum_value_not_int) - << EnumVal.toString(10) << 1; + << toString(EnumVal, 10) << 1; } } } @@ -18163,14 +18163,14 @@ // Emit warning for one enum constant. auto *FirstECD = Vec->front(); S.Diag(FirstECD->getLocation(), diag::warn_duplicate_enum_values) - << FirstECD << FirstECD->getInitVal().toString(10) + << FirstECD << toString(FirstECD->getInitVal(), 10) << FirstECD->getSourceRange(); // Emit one note for each of the remaining enum constants with // the same value. for (auto *ECD : llvm::make_range(Vec->begin() + 1, Vec->end())) S.Diag(ECD->getLocation(), diag::note_duplicate_element) - << ECD << ECD->getInitVal().toString(10) + << ECD << toString(ECD->getInitVal(), 10) << ECD->getSourceRange(); } } diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -229,7 +229,7 @@ if (!I->isIntN(32)) { S.Diag(Expr->getExprLoc(), diag::err_ice_too_large) - << I->toString(10, false) << 32 << /* Unsigned */ 1; + << toString(*I, 10, false) << 32 << /* Unsigned */ 1; return false; } @@ -257,7 +257,7 @@ llvm::APSInt I(32); // for toString I = UVal; S.Diag(Expr->getExprLoc(), diag::err_ice_too_large) - << I.toString(10, false) << 32 << /* Unsigned */ 0; + << toString(I, 10, false) << 32 << /* Unsigned */ 0; return false; } @@ -4990,7 +4990,7 @@ // Make sure we can fit it in 32 bits. if (!I->isIntN(32)) { S.Diag(E->getExprLoc(), diag::err_ice_too_large) - << I->toString(10, false) << 32 << /* Unsigned */ 1; + << toString(*I, 10, false) << 32 << /* Unsigned */ 1; return nullptr; } if (*I < 0) diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -908,8 +908,8 @@ if ((int64_t)Bindings.size() != NumElems) { S.Diag(Src->getLocation(), diag::err_decomp_decl_wrong_number_bindings) << DecompType << (unsigned)Bindings.size() - << (unsigned)NumElems.getLimitedValue(UINT_MAX) << NumElems.toString(10) - << (NumElems < Bindings.size()); + << (unsigned)NumElems.getLimitedValue(UINT_MAX) + << toString(NumElems, 10) << (NumElems < Bindings.size()); return true; } @@ -1164,7 +1164,7 @@ S.Diag(Src->getLocation(), diag::err_decomp_decl_wrong_number_bindings) << DecompType << (unsigned)Bindings.size() << (unsigned)TupleSize.getLimitedValue(UINT_MAX) - << TupleSize.toString(10) << (TupleSize < Bindings.size()); + << toString(TupleSize, 10) << (TupleSize < Bindings.size()); return true; } diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -47,8 +47,10 @@ #include "clang/Sema/SemaInternal.h" #include "clang/Sema/Template.h" #include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/StringExtras.h" #include "llvm/Support/ConvertUTF.h" #include "llvm/Support/SaveAndRestore.h" + using namespace clang; using namespace sema; using llvm::RoundingMode; @@ -3671,7 +3673,7 @@ bool ValueIsPositive = ValueAPS.isStrictlyPositive(); if (!ValueIsPositive || ValueAPS.getActiveBits() > 31) { Diag(E->getExprLoc(), diag::err_pragma_loop_invalid_argument_value) - << ValueAPS.toString(10) << ValueIsPositive; + << toString(ValueAPS, 10) << ValueIsPositive; return true; } @@ -5058,7 +5060,7 @@ llvm::APSInt LengthValue = Result.Val.getInt(); if (LengthValue.isNegative()) { Diag(Length->getExprLoc(), diag::err_omp_section_length_negative) - << LengthValue.toString(/*Radix=*/10, /*Signed=*/true) + << toString(LengthValue, /*Radix=*/10, /*Signed=*/true) << Length->getSourceRange(); return ExprError(); } @@ -5082,7 +5084,7 @@ llvm::APSInt StrideValue = Result.Val.getInt(); if (!StrideValue.isStrictlyPositive()) { Diag(Stride->getExprLoc(), diag::err_omp_section_stride_non_positive) - << StrideValue.toString(/*Radix=*/10, /*Signed=*/true) + << toString(StrideValue, /*Radix=*/10, /*Signed=*/true) << Stride->getSourceRange(); return ExprError(); } @@ -5161,7 +5163,7 @@ llvm::APSInt Value = EvResult.Val.getInt(); if (!Value.isStrictlyPositive()) { Diag(Dim->getExprLoc(), diag::err_omp_shaping_dimension_not_positive) - << Value.toString(/*Radix=*/10, /*Signed=*/true) + << toString(Value, /*Radix=*/10, /*Signed=*/true) << Dim->getSourceRange(); ErrorFound = true; continue; @@ -12319,7 +12321,8 @@ RHSStrRef.find('\'') != StringRef::npos) return; - bool SuggestXor = S.getLangOpts().CPlusPlus || S.getPreprocessor().isMacroDefined("xor"); + bool SuggestXor = + S.getLangOpts().CPlusPlus || S.getPreprocessor().isMacroDefined("xor"); const llvm::APInt XorValue = LeftSideValue ^ RightSideValue; int64_t RightSideIntValue = RightSideValue.getSExtValue(); if (LeftSideValue == 2 && RightSideIntValue >= 0) { @@ -12330,27 +12333,30 @@ if (Overflow) { if (RightSideIntValue < 64) S.Diag(Loc, diag::warn_xor_used_as_pow_base) - << ExprStr << XorValue.toString(10, true) << ("1LL << " + RHSStr) + << ExprStr << toString(XorValue, 10, true) << ("1LL << " + RHSStr) << FixItHint::CreateReplacement(ExprRange, "1LL << " + RHSStr); else if (RightSideIntValue == 64) - S.Diag(Loc, diag::warn_xor_used_as_pow) << ExprStr << XorValue.toString(10, true); + S.Diag(Loc, diag::warn_xor_used_as_pow) + << ExprStr << toString(XorValue, 10, true); else return; } else { S.Diag(Loc, diag::warn_xor_used_as_pow_base_extra) - << ExprStr << XorValue.toString(10, true) << SuggestedExpr - << PowValue.toString(10, true) + << ExprStr << toString(XorValue, 10, true) << SuggestedExpr + << toString(PowValue, 10, true) << FixItHint::CreateReplacement( ExprRange, (RightSideIntValue == 0) ? "1" : SuggestedExpr); } - S.Diag(Loc, diag::note_xor_used_as_pow_silence) << ("0x2 ^ " + RHSStr) << SuggestXor; + S.Diag(Loc, diag::note_xor_used_as_pow_silence) + << ("0x2 ^ " + RHSStr) << SuggestXor; } else if (LeftSideValue == 10) { std::string SuggestedValue = "1e" + std::to_string(RightSideIntValue); S.Diag(Loc, diag::warn_xor_used_as_pow_base) - << ExprStr << XorValue.toString(10, true) << SuggestedValue + << ExprStr << toString(XorValue, 10, true) << SuggestedValue << FixItHint::CreateReplacement(ExprRange, SuggestedValue); - S.Diag(Loc, diag::note_xor_used_as_pow_silence) << ("0xA ^ " + RHSStr) << SuggestXor; + S.Diag(Loc, diag::note_xor_used_as_pow_silence) + << ("0xA ^ " + RHSStr) << SuggestXor; } } diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp --- a/clang/lib/Sema/SemaExprCXX.cpp +++ b/clang/lib/Sema/SemaExprCXX.cpp @@ -2143,7 +2143,7 @@ if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) return ExprError( Diag((*ArraySize)->getBeginLoc(), diag::err_array_too_large) - << Value->toString(10) << (*ArraySize)->getSourceRange()); + << toString(*Value, 10) << (*ArraySize)->getSourceRange()); } KnownArraySize = Value->getZExtValue(); diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp --- a/clang/lib/Sema/SemaInit.cpp +++ b/clang/lib/Sema/SemaInit.cpp @@ -2873,7 +2873,7 @@ if (!VerifyOnly) SemaRef.Diag(IndexExpr->getBeginLoc(), diag::err_array_designator_too_large) - << DesignatedEndIndex.toString(10) << MaxElements.toString(10) + << toString(DesignatedEndIndex, 10) << toString(MaxElements, 10) << IndexExpr->getSourceRange(); ++Index; return true; @@ -3167,7 +3167,7 @@ if (Value.isSigned() && Value.isNegative()) return S.Diag(Loc, diag::err_array_designator_negative) - << Value.toString(10) << Index->getSourceRange(); + << toString(Value, 10) << Index->getSourceRange(); Value.setIsUnsigned(true); return Result; @@ -3236,7 +3236,7 @@ if (!StartDependent && !EndDependent && EndValue < StartValue) { Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) - << StartValue.toString(10) << EndValue.toString(10) + << toString(StartValue, 10) << toString(EndValue, 10) << StartIndex->getSourceRange() << EndIndex->getSourceRange(); Invalid = true; } else { diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp --- a/clang/lib/Sema/SemaOpenMP.cpp +++ b/clang/lib/Sema/SemaOpenMP.cpp @@ -9965,14 +9965,14 @@ Diag(StartLoc, diag::err_omp_critical_with_hint); if (HintLoc.isValid()) Diag(HintLoc, diag::note_omp_critical_hint_here) - << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false); + << 0 << toString(Hint, /*Radix=*/10, /*Signed=*/false); else Diag(StartLoc, diag::note_omp_critical_no_hint) << 0; if (const auto *C = Pair.first->getSingleClause()) { Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here) << 1 - << C->getHint()->EvaluateKnownConstInt(Context).toString( - /*Radix=*/10, /*Signed=*/false); + << toString(C->getHint()->EvaluateKnownConstInt(Context), + /*Radix=*/10, /*Signed=*/false); } else { Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1; } diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp --- a/clang/lib/Sema/SemaStmt.cpp +++ b/clang/lib/Sema/SemaStmt.cpp @@ -1106,8 +1106,8 @@ // type versus "switch expression cannot have this value". Use proper // IntRange checking rather than just looking at the unpromoted type here. if (ConvVal != Val) - S.Diag(Loc, diag::warn_case_value_overflow) << Val.toString(10) - << ConvVal.toString(10); + S.Diag(Loc, diag::warn_case_value_overflow) << toString(Val, 10) + << toString(ConvVal, 10); } } @@ -1432,7 +1432,7 @@ if (OverlapStmt) { // If we have a duplicate, report it. Diag(CR->getLHS()->getBeginLoc(), diag::err_duplicate_case) - << OverlapVal.toString(10); + << toString(OverlapVal, 10); Diag(OverlapStmt->getLHS()->getBeginLoc(), diag::note_duplicate_case_prev); // FIXME: We really want to remove the bogus case stmt from the @@ -1448,7 +1448,7 @@ // TODO: it would be nice if we printed enums as enums, chars as // chars, etc. Diag(CondExpr->getExprLoc(), diag::warn_missing_case_for_condition) - << ConstantCondValue.toString(10) + << toString(ConstantCondValue, 10) << CondExpr->getSourceRange(); } diff --git a/clang/lib/Sema/SemaStmtAsm.cpp b/clang/lib/Sema/SemaStmtAsm.cpp --- a/clang/lib/Sema/SemaStmtAsm.cpp +++ b/clang/lib/Sema/SemaStmtAsm.cpp @@ -405,7 +405,7 @@ if (!Info.isValidAsmImmediate(IntResult)) return StmtError(Diag(InputExpr->getBeginLoc(), diag::err_invalid_asm_value_for_constraint) - << IntResult.toString(10) + << toString(IntResult, 10) << Info.getConstraintStr() << InputExpr->getSourceRange()); } diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp --- a/clang/lib/Sema/SemaTemplate.cpp +++ b/clang/lib/Sema/SemaTemplate.cpp @@ -7202,10 +7202,10 @@ Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); // Complain if an unsigned parameter received a negative value. - if (IntegerType->isUnsignedIntegerOrEnumerationType() - && (OldValue.isSigned() && OldValue.isNegative())) { + if (IntegerType->isUnsignedIntegerOrEnumerationType() && + (OldValue.isSigned() && OldValue.isNegative())) { Diag(Arg->getBeginLoc(), diag::warn_template_arg_negative) - << OldValue.toString(10) << Value.toString(10) << Param->getType() + << toString(OldValue, 10) << toString(Value, 10) << Param->getType() << Arg->getSourceRange(); Diag(Param->getLocation(), diag::note_template_param_here); } @@ -7220,7 +7220,7 @@ RequiredBits = OldValue.getMinSignedBits(); if (RequiredBits > AllowedBits) { Diag(Arg->getBeginLoc(), diag::warn_template_arg_too_large) - << OldValue.toString(10) << Value.toString(10) << Param->getType() + << toString(OldValue, 10) << toString(Value, 10) << Param->getType() << Arg->getSourceRange(); Diag(Param->getLocation(), diag::note_template_param_here); } diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp --- a/clang/lib/Sema/SemaType.cpp +++ b/clang/lib/Sema/SemaType.cpp @@ -2498,7 +2498,7 @@ : ConstVal.getActiveBits(); if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) { Diag(ArraySize->getBeginLoc(), diag::err_array_too_large) - << ConstVal.toString(10) << ArraySize->getSourceRange(); + << toString(ConstVal, 10) << ArraySize->getSourceRange(); return QualType(); } diff --git a/clang/lib/StaticAnalyzer/Core/RangeConstraintManager.cpp b/clang/lib/StaticAnalyzer/Core/RangeConstraintManager.cpp --- a/clang/lib/StaticAnalyzer/Core/RangeConstraintManager.cpp +++ b/clang/lib/StaticAnalyzer/Core/RangeConstraintManager.cpp @@ -20,6 +20,7 @@ #include "llvm/ADT/FoldingSet.h" #include "llvm/ADT/ImmutableSet.h" #include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/StringExtras.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/raw_ostream.h" #include @@ -493,7 +494,7 @@ } void Range::dump(raw_ostream &OS) const { - OS << '[' << From().toString(10) << ", " << To().toString(10) << ']'; + OS << '[' << toString(From(), 10) << ", " << toString(To(), 10) << ']'; } void RangeSet::dump(raw_ostream &OS) const { diff --git a/clang/unittests/StaticAnalyzer/RangeSetTest.cpp b/clang/unittests/StaticAnalyzer/RangeSetTest.cpp --- a/clang/unittests/StaticAnalyzer/RangeSetTest.cpp +++ b/clang/unittests/StaticAnalyzer/RangeSetTest.cpp @@ -28,7 +28,7 @@ return SS.str(); } LLVM_ATTRIBUTE_UNUSED static std::string toString(const llvm::APSInt &Point) { - return Point.toString(10); + return toString(Point, 10); } // We need it here for better fail diagnostics from gtest. LLVM_ATTRIBUTE_UNUSED static std::ostream &operator<<(std::ostream &OS, diff --git a/clang/unittests/Tooling/RecursiveASTVisitorTestPostOrderVisitor.cpp b/clang/unittests/Tooling/RecursiveASTVisitorTestPostOrderVisitor.cpp --- a/clang/unittests/Tooling/RecursiveASTVisitorTestPostOrderVisitor.cpp +++ b/clang/unittests/Tooling/RecursiveASTVisitorTestPostOrderVisitor.cpp @@ -41,7 +41,7 @@ } bool VisitIntegerLiteral(IntegerLiteral *Lit) { - VisitedNodes.push_back(Lit->getValue().toString(10, false)); + VisitedNodes.push_back(toString(Lit->getValue(), 10, false)); return true; } diff --git a/clang/unittests/Tooling/RecursiveASTVisitorTests/CallbacksCommon.h b/clang/unittests/Tooling/RecursiveASTVisitorTests/CallbacksCommon.h --- a/clang/unittests/Tooling/RecursiveASTVisitorTests/CallbacksCommon.h +++ b/clang/unittests/Tooling/RecursiveASTVisitorTests/CallbacksCommon.h @@ -39,7 +39,7 @@ std::string stmtToString(Stmt *S) { StringRef ClassName = S->getStmtClassName(); if (IntegerLiteral *IL = dyn_cast(S)) { - return (ClassName + "(" + IL->getValue().toString(10, false) + ")").str(); + return (ClassName + "(" + toString(IL->getValue(), 10, false) + ")").str(); } if (UnaryOperator *UO = dyn_cast(S)) { return (ClassName + "(" + UnaryOperator::getOpcodeStr(UO->getOpcode()) + diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h --- a/llvm/include/llvm/ADT/APInt.h +++ b/llvm/include/llvm/ADT/APInt.h @@ -20,7 +20,6 @@ #include #include #include -#include namespace llvm { class FoldingSetNodeID; @@ -1753,13 +1752,6 @@ toString(Str, Radix, true, false); } - /// Return the APInt as a std::string. - /// - /// Note that this is an inefficient method. It is better to pass in a - /// SmallVector/SmallString to the methods above to avoid thrashing the heap - /// for the string. - std::string toString(unsigned Radix, bool Signed) const; - /// \returns a byte-swapped representation of this APInt Value. APInt byteSwap() const; diff --git a/llvm/include/llvm/ADT/APSInt.h b/llvm/include/llvm/ADT/APSInt.h --- a/llvm/include/llvm/ADT/APSInt.h +++ b/llvm/include/llvm/ADT/APSInt.h @@ -82,11 +82,6 @@ void toString(SmallVectorImpl &Str, unsigned Radix = 10) const { APInt::toString(Str, Radix, isSigned()); } - /// Converts an APInt to a std::string. This is an inefficient - /// method; you should prefer passing in a SmallString instead. - std::string toString(unsigned Radix) const { - return APInt::toString(Radix, isSigned()); - } using APInt::toString; /// Get the correctly-extended \c int64_t value. diff --git a/llvm/include/llvm/ADT/StringExtras.h b/llvm/include/llvm/ADT/StringExtras.h --- a/llvm/include/llvm/ADT/StringExtras.h +++ b/llvm/include/llvm/ADT/StringExtras.h @@ -13,6 +13,7 @@ #ifndef LLVM_ADT_STRINGEXTRAS_H #define LLVM_ADT_STRINGEXTRAS_H +#include "llvm/ADT/APSInt.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringRef.h" @@ -296,6 +297,17 @@ return utostr(static_cast(X)); } +inline std::string toString(const APInt &I, unsigned Radix, bool Signed, + bool formatAsCLiteral = false) { + SmallString<40> S; + I.toString(S, Radix, Signed, formatAsCLiteral); + return std::string(S.str()); +} + +inline std::string toString(const APSInt &I, unsigned Radix) { + return toString(I, Radix, I.isSigned()); +} + /// StrInStrNoCase - Portable version of strcasestr. Locates the first /// occurrence of string 's1' in string 's2', ignoring case. Returns /// the offset of s2 in s1 or npos if s2 cannot be found. diff --git a/llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp b/llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp --- a/llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp +++ b/llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp @@ -88,10 +88,10 @@ dumpResult(const MachineInstr &MI, const KnownBits &Known, unsigned Depth) { dbgs() << "[" << Depth << "] Compute known bits: " << MI << "[" << Depth << "] Computed for: " << MI << "[" << Depth << "] Known: 0x" - << (Known.Zero | Known.One).toString(16, false) << "\n" - << "[" << Depth << "] Zero: 0x" << Known.Zero.toString(16, false) + << toString(Known.Zero | Known.One, 16, false) << "\n" + << "[" << Depth << "] Zero: 0x" << toString(Known.Zero, 16, false) << "\n" - << "[" << Depth << "] One: 0x" << Known.One.toString(16, false) + << "[" << Depth << "] One: 0x" << toString(Known.One, 16, false) << "\n"; } diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp --- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -1930,7 +1930,7 @@ static std::string APIntToHexString(const APInt &AI) { unsigned Width = (AI.getBitWidth() / 8) * 2; - std::string HexString = AI.toString(16, /*Signed=*/false); + std::string HexString = toString(AI, 16, /*Signed=*/false); llvm::transform(HexString, HexString.begin(), tolower); unsigned Size = HexString.size(); assert(Width >= Size && "hex string is too large!"); diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp --- a/llvm/lib/Support/APInt.cpp +++ b/llvm/lib/Support/APInt.cpp @@ -2281,14 +2281,6 @@ std::reverse(Str.begin()+StartDig, Str.end()); } -/// Returns the APInt as a std::string. Note that this is an inefficient method. -/// It is better to pass in a SmallVector/SmallString to the methods above. -std::string APInt::toString(unsigned Radix = 10, bool Signed = true) const { - SmallString<40> S; - toString(S, Radix, Signed, /* formatAsCLiteral = */false); - return std::string(S.str()); -} - #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) LLVM_DUMP_METHOD void APInt::dump() const { SmallString<40> S, U; diff --git a/llvm/lib/Target/X86/X86MCInstLower.cpp b/llvm/lib/Target/X86/X86MCInstLower.cpp --- a/llvm/lib/Target/X86/X86MCInstLower.cpp +++ b/llvm/lib/Target/X86/X86MCInstLower.cpp @@ -2167,7 +2167,7 @@ const MachineOperand &DstOp = MI->getOperand(0); CS << X86ATTInstPrinter::getRegisterName(DstOp.getReg()) << " = "; if (auto *CF = dyn_cast(C)) { - CS << "0x" << CF->getValueAPF().bitcastToAPInt().toString(16, false); + CS << "0x" << toString(CF->getValueAPF().bitcastToAPInt(), 16, false); OutStreamer.AddComment(CS.str()); } } diff --git a/llvm/lib/Transforms/IPO/OpenMPOpt.cpp b/llvm/lib/Transforms/IPO/OpenMPOpt.cpp --- a/llvm/lib/Transforms/IPO/OpenMPOpt.cpp +++ b/llvm/lib/Transforms/IPO/OpenMPOpt.cpp @@ -585,7 +585,7 @@ return ORA << "OpenMP ICV " << ore::NV("OpenMPICV", ICVInfo.Name) << " Value: " << (ICVInfo.InitValue - ? ICVInfo.InitValue->getValue().toString(10, true) + ? toString(ICVInfo.InitValue->getValue(), 10, true) : "IMPLEMENTATION_DEFINED"); }; diff --git a/llvm/tools/llvm-exegesis/lib/BenchmarkResult.cpp b/llvm/tools/llvm-exegesis/lib/BenchmarkResult.cpp --- a/llvm/tools/llvm-exegesis/lib/BenchmarkResult.cpp +++ b/llvm/tools/llvm-exegesis/lib/BenchmarkResult.cpp @@ -255,7 +255,7 @@ raw_ostream &Out) { YamlContext &Context = getTypedContext(Ctx); Out << Context.getRegName(RV.Register) << "=0x" - << RV.Value.toString(kRadix, kSigned); + << toString(RV.Value, kRadix, kSigned); } static StringRef input(StringRef String, void *Ctx, diff --git a/llvm/tools/llvm-pdbutil/MinimalSymbolDumper.cpp b/llvm/tools/llvm-pdbutil/MinimalSymbolDumper.cpp --- a/llvm/tools/llvm-pdbutil/MinimalSymbolDumper.cpp +++ b/llvm/tools/llvm-pdbutil/MinimalSymbolDumper.cpp @@ -559,7 +559,7 @@ P.format(" `{0}`", Constant.Name); AutoIndent Indent(P, 7); P.formatLine("type = {0}, value = {1}", typeIndex(Constant.Type), - Constant.Value.toString(10)); + toString(Constant.Value, 10)); return Error::success(); } diff --git a/llvm/tools/llvm-pdbutil/MinimalTypeDumper.cpp b/llvm/tools/llvm-pdbutil/MinimalTypeDumper.cpp --- a/llvm/tools/llvm-pdbutil/MinimalTypeDumper.cpp +++ b/llvm/tools/llvm-pdbutil/MinimalTypeDumper.cpp @@ -557,7 +557,7 @@ Error MinimalTypeDumpVisitor::visitKnownMember(CVMemberRecord &CVR, EnumeratorRecord &Enum) { P.format(" [{0} = {1}]", Enum.Name, - Enum.Value.toString(10, Enum.Value.isSigned())); + toString(Enum.Value, 10, Enum.Value.isSigned())); return Error::success(); } diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp --- a/llvm/unittests/ADT/APIntTest.cpp +++ b/llvm/unittests/ADT/APIntTest.cpp @@ -1461,7 +1461,10 @@ APInt ValC(65, 0); ValC = ValA * ValB; ValA *= ValB; - EXPECT_EQ(ValA.toString(10, false), ValC.toString(10, false)); + SmallString<16> StrA, StrC; + ValA.toString(StrA, 10, false); + ValC.toString(StrC, 10, false); + EXPECT_EQ(std::string(StrA), std::string(StrC)); } TEST(APIntTest, Rotate) { diff --git a/llvm/unittests/ADT/StringExtrasTest.cpp b/llvm/unittests/ADT/StringExtrasTest.cpp --- a/llvm/unittests/ADT/StringExtrasTest.cpp +++ b/llvm/unittests/ADT/StringExtrasTest.cpp @@ -229,3 +229,48 @@ S = LS2; EXPECT_EQ(S, " "); } + +TEST(StringExtrasTest, toStringAPInt) { + bool isSigned; + + EXPECT_EQ(toString(APInt(8, 0), 2, true, true), "0b0"); + EXPECT_EQ(toString(APInt(8, 0), 8, true, true), "00"); + EXPECT_EQ(toString(APInt(8, 0), 10, true, true), "0"); + EXPECT_EQ(toString(APInt(8, 0), 16, true, true), "0x0"); + EXPECT_EQ(toString(APInt(8, 0), 36, true, false), "0"); + + isSigned = false; + EXPECT_EQ(toString(APInt(8, 255, isSigned), 2, isSigned, true), "0b11111111"); + EXPECT_EQ(toString(APInt(8, 255, isSigned), 8, isSigned, true), "0377"); + EXPECT_EQ(toString(APInt(8, 255, isSigned), 10, isSigned, true), "255"); + EXPECT_EQ(toString(APInt(8, 255, isSigned), 16, isSigned, true), "0xFF"); + EXPECT_EQ(toString(APInt(8, 255, isSigned), 36, isSigned, false), "73"); + + isSigned = true; + EXPECT_EQ(toString(APInt(8, 255, isSigned), 2, isSigned, true), "-0b1"); + EXPECT_EQ(toString(APInt(8, 255, isSigned), 8, isSigned, true), "-01"); + EXPECT_EQ(toString(APInt(8, 255, isSigned), 10, isSigned, true), "-1"); + EXPECT_EQ(toString(APInt(8, 255, isSigned), 16, isSigned, true), "-0x1"); + EXPECT_EQ(toString(APInt(8, 255, isSigned), 36, isSigned, false), "-1"); +} + +TEST(StringExtrasTest, toStringAPSInt) { + bool isUnsigned; + + EXPECT_EQ(toString(APSInt(APInt(8, 0), false), 2), "0"); + EXPECT_EQ(toString(APSInt(APInt(8, 0), false), 8), "0"); + EXPECT_EQ(toString(APSInt(APInt(8, 0), false), 10), "0"); + EXPECT_EQ(toString(APSInt(APInt(8, 0), false), 16), "0"); + + isUnsigned = true; + EXPECT_EQ(toString(APSInt(APInt(8, 255), isUnsigned), 2), "11111111"); + EXPECT_EQ(toString(APSInt(APInt(8, 255), isUnsigned), 8), "377"); + EXPECT_EQ(toString(APSInt(APInt(8, 255), isUnsigned), 10), "255"); + EXPECT_EQ(toString(APSInt(APInt(8, 255), isUnsigned), 16), "FF"); + + isUnsigned = false; + EXPECT_EQ(toString(APSInt(APInt(8, 255), isUnsigned), 2), "-1"); + EXPECT_EQ(toString(APSInt(APInt(8, 255), isUnsigned), 8), "-1"); + EXPECT_EQ(toString(APSInt(APInt(8, 255), isUnsigned), 10), "-1"); + EXPECT_EQ(toString(APSInt(APInt(8, 255), isUnsigned), 16), "-1"); +} \ No newline at end of file