From 5d4cff72b4c0e2c3cbb3f784c869d5b68427208f Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Thu, 7 Jun 2012 15:54:03 +0000 Subject: [PATCH] Reuse APInt's getNumWords, which gets rounding right (my ad-hoc solution missed it). llvm-svn: 158151 --- clang/include/clang/AST/TemplateBase.h | 11 ++++++----- clang/lib/AST/TemplateBase.cpp | 7 ++++--- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/clang/include/clang/AST/TemplateBase.h b/clang/include/clang/AST/TemplateBase.h index 9d3f45f4b2cf..76cbcfbf7fe0 100644 --- a/clang/include/clang/AST/TemplateBase.h +++ b/clang/include/clang/AST/TemplateBase.h @@ -244,12 +244,13 @@ public: /// \brief Retrieve the template argument as an integral value. // FIXME: Provide a way to read the integral data without copying the value. llvm::APSInt getAsIntegral() const { + using namespace llvm; if (Integer.BitWidth <= 64) - return llvm::APSInt(llvm::APInt(Integer.BitWidth, Integer.VAL), - Integer.IsUnsigned); - return llvm::APSInt(llvm::APInt(Integer.BitWidth, - llvm::makeArrayRef(Integer.pVal, Integer.BitWidth / 8)), - Integer.IsUnsigned); + return APSInt(APInt(Integer.BitWidth, Integer.VAL), Integer.IsUnsigned); + + unsigned NumWords = APInt::getNumWords(Integer.BitWidth); + return APSInt(APInt(Integer.BitWidth, makeArrayRef(Integer.pVal, NumWords)), + Integer.IsUnsigned); } /// \brief Retrieve the type of the integral value. diff --git a/clang/lib/AST/TemplateBase.cpp b/clang/lib/AST/TemplateBase.cpp index 284c1b5baae0..f8dd396d92df 100644 --- a/clang/lib/AST/TemplateBase.cpp +++ b/clang/lib/AST/TemplateBase.cpp @@ -61,9 +61,10 @@ TemplateArgument::TemplateArgument(ASTContext &Ctx, const llvm::APSInt &Value, Integer.BitWidth = Value.getBitWidth(); Integer.IsUnsigned = Value.isUnsigned(); // If the value is large, we have to get additional memory from the ASTContext - if (Integer.BitWidth > 64) { - void *Mem = Ctx.Allocate(Integer.BitWidth / 8); - std::memcpy(Mem, Value.getRawData(), Integer.BitWidth / 8); + unsigned NumWords = Value.getNumWords(); + if (NumWords > 1) { + void *Mem = Ctx.Allocate(NumWords * sizeof(uint64_t)); + std::memcpy(Mem, Value.getRawData(), NumWords * sizeof(uint64_t)); Integer.pVal = static_cast(Mem); } else { Integer.VAL = Value.getZExtValue();