Skip to content

Commit 61cdaf6

Browse files
committed
[ADT] Remove APInt/APSInt toString() std::string variants
<string> is currently the highest impact header in a clang+llvm build: https://commondatastorage.googleapis.com/chromium-browser-clang/llvm-include-analysis.html One of the most common places this is being included is the APInt.h header, which needs it for an old toString() implementation that returns std::string - an inefficient method compared to the SmallString versions that it actually wraps. This patch replaces these APInt/APSInt methods with a pair of llvm::toString() helpers inside StringExtras.h, adjusts users accordingly and removes the <string> from APInt.h - I was hoping that more of these users could be converted to use the SmallString methods, but it appears that most end up creating a std::string anyhow. I avoided trying to use the raw_ostream << operators as well as I didn't want to lose having the integer radix explicit in the code. Differential Revision: https://reviews.llvm.org/D103888
1 parent 71a02dd commit 61cdaf6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+190
-136
lines changed

clang-tools-extra/clang-tidy/abseil/DurationRewriter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ stripFloatLiteralFraction(const MatchFinder::MatchResult &Result,
208208
if (const auto *LitFloat = llvm::dyn_cast<FloatingLiteral>(&Node))
209209
// Attempt to simplify a `Duration` factory call with a literal argument.
210210
if (llvm::Optional<llvm::APSInt> IntValue = truncateIfIntegral(*LitFloat))
211-
return IntValue->toString(/*radix=*/10);
211+
return toString(*IntValue, /*radix=*/10);
212212

213213
return llvm::None;
214214
}

clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void ProBoundsConstantArrayIndexCheck::check(
9999

100100
if (Index->isSigned() && Index->isNegative()) {
101101
diag(Matched->getExprLoc(), "std::array<> index %0 is negative")
102-
<< Index->toString(10);
102+
<< toString(*Index, 10);
103103
return;
104104
}
105105

@@ -118,7 +118,7 @@ void ProBoundsConstantArrayIndexCheck::check(
118118
diag(Matched->getExprLoc(),
119119
"std::array<> index %0 is past the end of the array "
120120
"(which contains %1 elements)")
121-
<< Index->toString(10) << ArraySize.toString(10, false);
121+
<< toString(*Index, 10) << toString(ArraySize, 10, false);
122122
}
123123
}
124124

clang-tools-extra/clangd/DumpAST.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ class DumpVisitor : public RecursiveASTVisitor<DumpVisitor> {
290290
}
291291
std::string getDetail(const TemplateArgumentLoc &TAL) {
292292
if (TAL.getArgument().getKind() == TemplateArgument::Integral)
293-
return TAL.getArgument().getAsIntegral().toString(10);
293+
return toString(TAL.getArgument().getAsIntegral(), 10);
294294
return "";
295295
}
296296
std::string getDetail(const TemplateName &TN) {

clang-tools-extra/clangd/Hover.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ HoverInfo getHoverContents(const NamedDecl *D, const PrintingPolicy &PP,
587587
} else if (const auto *ECD = dyn_cast<EnumConstantDecl>(D)) {
588588
// Dependent enums (e.g. nested in template classes) don't have values yet.
589589
if (!ECD->getType()->isDependentType())
590-
HI.Value = ECD->getInitVal().toString(10);
590+
HI.Value = toString(ECD->getInitVal(), 10);
591591
}
592592

593593
HI.Definition = printDefinition(D, PP);

clang/include/clang/ASTMatchers/ASTMatchers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,7 @@ AST_MATCHER_P(TemplateArgument, equalsIntegralValue,
12131213
std::string, Value) {
12141214
if (Node.getKind() != TemplateArgument::Integral)
12151215
return false;
1216-
return Node.getAsIntegral().toString(10) == Value;
1216+
return toString(Node.getAsIntegral(), 10) == Value;
12171217
}
12181218

12191219
/// Matches an Objective-C autorelease pool statement.

clang/lib/AST/ASTDiagnostic.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "clang/AST/ExprCXX.h"
2020
#include "clang/AST/TemplateBase.h"
2121
#include "clang/AST/Type.h"
22+
#include "llvm/ADT/StringExtras.h"
2223
#include "llvm/Support/raw_ostream.h"
2324

2425
using namespace clang;
@@ -1756,7 +1757,7 @@ class TemplateDiff {
17561757
if (FromIntType->isBooleanType()) {
17571758
OS << ((FromInt == 0) ? "false" : "true");
17581759
} else {
1759-
OS << FromInt.toString(10);
1760+
OS << toString(FromInt, 10);
17601761
}
17611762
return;
17621763
}
@@ -1800,7 +1801,7 @@ class TemplateDiff {
18001801
if (IntType->isBooleanType()) {
18011802
OS << ((Val == 0) ? "false" : "true");
18021803
} else {
1803-
OS << Val.toString(10);
1804+
OS << toString(Val, 10);
18041805
}
18051806
} else if (E) {
18061807
PrintExpr(E);

clang/lib/AST/ASTStructuralEquivalence.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
#include "llvm/ADT/APSInt.h"
8787
#include "llvm/ADT/None.h"
8888
#include "llvm/ADT/Optional.h"
89+
#include "llvm/ADT/StringExtras.h"
8990
#include "llvm/Support/Casting.h"
9091
#include "llvm/Support/Compiler.h"
9192
#include "llvm/Support/ErrorHandling.h"
@@ -1623,7 +1624,7 @@ static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
16231624
diag::err_odr_tag_type_inconsistent))
16241625
<< Context.ToCtx.getTypeDeclType(D2);
16251626
Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1626-
<< EC1->getDeclName() << EC1->getInitVal().toString(10);
1627+
<< EC1->getDeclName() << toString(EC1->getInitVal(), 10);
16271628
Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
16281629
}
16291630
return false;
@@ -1639,9 +1640,9 @@ static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
16391640
diag::err_odr_tag_type_inconsistent))
16401641
<< Context.ToCtx.getTypeDeclType(D2);
16411642
Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1642-
<< EC2->getDeclName() << EC2->getInitVal().toString(10);
1643+
<< EC2->getDeclName() << toString(EC2->getInitVal(), 10);
16431644
Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1644-
<< EC1->getDeclName() << EC1->getInitVal().toString(10);
1645+
<< EC1->getDeclName() << toString(EC1->getInitVal(), 10);
16451646
}
16461647
return false;
16471648
}
@@ -1653,7 +1654,7 @@ static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
16531654
diag::err_odr_tag_type_inconsistent))
16541655
<< Context.ToCtx.getTypeDeclType(D2);
16551656
Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1656-
<< EC2->getDeclName() << EC2->getInitVal().toString(10);
1657+
<< EC2->getDeclName() << toString(EC2->getInitVal(), 10);
16571658
Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
16581659
}
16591660
return false;

clang/lib/AST/ExprConstant.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2720,7 +2720,7 @@ static bool CheckedIntArithmetic(EvalInfo &Info, const Expr *E,
27202720
if (Info.checkingForUndefinedBehavior())
27212721
Info.Ctx.getDiagnostics().Report(E->getExprLoc(),
27222722
diag::warn_integer_constant_overflow)
2723-
<< Result.toString(10) << E->getType();
2723+
<< toString(Result, 10) << E->getType();
27242724
return HandleOverflow(Info, E, Value, E->getType());
27252725
}
27262726
return true;
@@ -6964,7 +6964,7 @@ class BufferToAPValueConverter {
69646964
llvm::NoneType unrepresentableValue(QualType Ty, const APSInt &Val) {
69656965
Info.FFDiag(BCE->getBeginLoc(),
69666966
diag::note_constexpr_bit_cast_unrepresentable_value)
6967-
<< Ty << Val.toString(/*Radix=*/10);
6967+
<< Ty << toString(Val, /*Radix=*/10);
69686968
return None;
69696969
}
69706970

@@ -9265,7 +9265,7 @@ bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
92659265
llvm::APInt::udivrem(OrigN, TSize, N, Remainder);
92669266
if (Remainder) {
92679267
Info.FFDiag(E, diag::note_constexpr_memcpy_unsupported)
9268-
<< Move << WChar << 0 << T << OrigN.toString(10, /*Signed*/false)
9268+
<< Move << WChar << 0 << T << toString(OrigN, 10, /*Signed*/false)
92699269
<< (unsigned)TSize;
92709270
return false;
92719271
}
@@ -9279,7 +9279,7 @@ bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
92799279
if (N.ugt(RemainingSrcSize) || N.ugt(RemainingDestSize)) {
92809280
Info.FFDiag(E, diag::note_constexpr_memcpy_unsupported)
92819281
<< Move << WChar << (N.ugt(RemainingSrcSize) ? 1 : 2) << T
9282-
<< N.toString(10, /*Signed*/false);
9282+
<< toString(N, 10, /*Signed*/false);
92839283
return false;
92849284
}
92859285
uint64_t NElems = N.getZExtValue();
@@ -9454,8 +9454,8 @@ bool PointerExprEvaluator::VisitCXXNewExpr(const CXXNewExpr *E) {
94549454
return ZeroInitialization(E);
94559455

94569456
Info.FFDiag(*ArraySize, diag::note_constexpr_new_too_small)
9457-
<< AllocBound.toString(10, /*Signed=*/false)
9458-
<< InitBound.toString(10, /*Signed=*/false)
9457+
<< toString(AllocBound, 10, /*Signed=*/false)
9458+
<< toString(InitBound, 10, /*Signed=*/false)
94599459
<< (*ArraySize)->getSourceRange();
94609460
return false;
94619461
}

clang/lib/AST/StmtPrinter.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "llvm/ADT/ArrayRef.h"
4848
#include "llvm/ADT/SmallString.h"
4949
#include "llvm/ADT/SmallVector.h"
50+
#include "llvm/ADT/StringExtras.h"
5051
#include "llvm/ADT/StringRef.h"
5152
#include "llvm/Support/Casting.h"
5253
#include "llvm/Support/Compiler.h"
@@ -1122,7 +1123,7 @@ void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
11221123
if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
11231124
return;
11241125
bool isSigned = Node->getType()->isSignedIntegerType();
1125-
OS << Node->getValue().toString(10, isSigned);
1126+
OS << toString(Node->getValue(), 10, isSigned);
11261127

11271128
// Emit suffixes. Integer literals are always a builtin integer type.
11281129
switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
@@ -1867,7 +1868,7 @@ void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
18671868
case UserDefinedLiteral::LOK_Integer: {
18681869
// Print integer literal without suffix.
18691870
const auto *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1870-
OS << Int->getValue().toString(10, /*isSigned*/false);
1871+
OS << toString(Int->getValue(), 10, /*isSigned*/false);
18711872
break;
18721873
}
18731874
case UserDefinedLiteral::LOK_Floating: {

clang/lib/AST/TemplateBase.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "llvm/ADT/FoldingSet.h"
3232
#include "llvm/ADT/None.h"
3333
#include "llvm/ADT/SmallString.h"
34+
#include "llvm/ADT/StringExtras.h"
3435
#include "llvm/ADT/StringRef.h"
3536
#include "llvm/Support/Casting.h"
3637
#include "llvm/Support/Compiler.h"
@@ -554,7 +555,7 @@ static const T &DiagTemplateArg(const T &DB, const TemplateArgument &Arg) {
554555
return DB << "nullptr";
555556

556557
case TemplateArgument::Integral:
557-
return DB << Arg.getAsIntegral().toString(10);
558+
return DB << toString(Arg.getAsIntegral(), 10);
558559

559560
case TemplateArgument::Template:
560561
return DB << Arg.getAsTemplate();

0 commit comments

Comments
 (0)