From: Fangrui Song Date: Sun, 7 Apr 2019 03:58:42 +0000 (+0000) Subject: Change some StringRef::data() reinterpret_cast to bytes_begin() or arrayRefFromString... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9cc714957fa0ea29493d2f95c7552a86f6f22d4d;p=llvm Change some StringRef::data() reinterpret_cast to bytes_begin() or arrayRefFromStringRef() git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@357852 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/Bitcode/BitstreamReader.h b/include/llvm/Bitcode/BitstreamReader.h index 90cbe7b4b1e..03a7fb8758b 100644 --- a/include/llvm/Bitcode/BitstreamReader.h +++ b/include/llvm/Bitcode/BitstreamReader.h @@ -103,8 +103,7 @@ public: explicit SimpleBitstreamCursor(ArrayRef BitcodeBytes) : BitcodeBytes(BitcodeBytes) {} explicit SimpleBitstreamCursor(StringRef BitcodeBytes) - : BitcodeBytes(reinterpret_cast(BitcodeBytes.data()), - BitcodeBytes.size()) {} + : BitcodeBytes(arrayRefFromStringRef(BitcodeBytes)) {} explicit SimpleBitstreamCursor(MemoryBufferRef BitcodeBytes) : SimpleBitstreamCursor(BitcodeBytes.getBuffer()) {} diff --git a/include/llvm/Object/ELF.h b/include/llvm/Object/ELF.h index a49f7128c64..a1c9ddacd78 100644 --- a/include/llvm/Object/ELF.h +++ b/include/llvm/Object/ELF.h @@ -79,9 +79,7 @@ public: using Elf_Relr_Range = typename ELFT::RelrRange; using Elf_Phdr_Range = typename ELFT::PhdrRange; - const uint8_t *base() const { - return reinterpret_cast(Buf.data()); - } + const uint8_t *base() const { return Buf.bytes_begin(); } size_t getBufSize() const { return Buf.size(); } diff --git a/include/llvm/ObjectYAML/YAML.h b/include/llvm/ObjectYAML/YAML.h index a14bfbc5f38..37014109a61 100644 --- a/include/llvm/ObjectYAML/YAML.h +++ b/include/llvm/ObjectYAML/YAML.h @@ -73,8 +73,7 @@ class BinaryRef { public: BinaryRef() = default; BinaryRef(ArrayRef Data) : Data(Data), DataIsHexString(false) {} - BinaryRef(StringRef Data) - : Data(reinterpret_cast(Data.data()), Data.size()) {} + BinaryRef(StringRef Data) : Data(arrayRefFromStringRef(Data)) {} /// The number of bytes that are represented by this BinaryRef. /// This is the number of bytes that writeAsBinary() will write. diff --git a/lib/DebugInfo/DWARF/DWARFFormValue.cpp b/lib/DebugInfo/DWARF/DWARFFormValue.cpp index 4129593848c..bdc2e54b3e9 100644 --- a/lib/DebugInfo/DWARF/DWARFFormValue.cpp +++ b/lib/DebugInfo/DWARF/DWARFFormValue.cpp @@ -354,7 +354,7 @@ bool DWARFFormValue::extractValue(const DWARFDataExtractor &Data, StringRef Str = Data.getData().substr(*OffsetPtr, Value.uval); Value.data = nullptr; if (!Str.empty()) { - Value.data = reinterpret_cast(Str.data()); + Value.data = Str.bytes_begin(); *OffsetPtr += Value.uval; } } diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldChecker.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldChecker.cpp index 642c381aaf9..28c8a69939e 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldChecker.cpp +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldChecker.cpp @@ -666,9 +666,7 @@ private: bool decodeInst(StringRef Symbol, MCInst &Inst, uint64_t &Size) const { MCDisassembler *Dis = Checker.Disassembler; StringRef SectionMem = Checker.getSubsectionStartingAt(Symbol); - ArrayRef SectionBytes( - reinterpret_cast(SectionMem.data()), - SectionMem.size()); + ArrayRef SectionBytes(SectionMem.bytes_begin(), SectionMem.size()); MCDisassembler::DecodeStatus S = Dis->getInstruction(Inst, Size, SectionBytes, 0, nulls(), nulls()); diff --git a/lib/IR/Constants.cpp b/lib/IR/Constants.cpp index a1619921f8b..00c722651ed 100644 --- a/lib/IR/Constants.cpp +++ b/lib/IR/Constants.cpp @@ -2576,7 +2576,7 @@ Constant *ConstantDataArray::getFP(LLVMContext &Context, Constant *ConstantDataArray::getString(LLVMContext &Context, StringRef Str, bool AddNull) { if (!AddNull) { - const uint8_t *Data = reinterpret_cast(Str.data()); + const uint8_t *Data = Str.bytes_begin(); return get(Context, makeArrayRef(Data, Str.size())); } diff --git a/lib/Object/WasmObjectFile.cpp b/lib/Object/WasmObjectFile.cpp index 9d521544593..16645002a6d 100644 --- a/lib/Object/WasmObjectFile.cpp +++ b/lib/Object/WasmObjectFile.cpp @@ -255,7 +255,7 @@ WasmObjectFile::WasmObjectFile(MemoryBufferRef Buffer, Error &Err) } ReadContext Ctx; - Ctx.Start = reinterpret_cast(getData().data()); + Ctx.Start = getData().bytes_begin(); Ctx.Ptr = Ctx.Start + 4; Ctx.End = Ctx.Start + getData().size(); diff --git a/lib/ProfileData/Coverage/CoverageMappingReader.cpp b/lib/ProfileData/Coverage/CoverageMappingReader.cpp index 3724329e132..fd522dc309e 100644 --- a/lib/ProfileData/Coverage/CoverageMappingReader.cpp +++ b/lib/ProfileData/Coverage/CoverageMappingReader.cpp @@ -59,7 +59,7 @@ Error RawCoverageReader::readULEB128(uint64_t &Result) { if (Data.empty()) return make_error(coveragemap_error::truncated); unsigned N = 0; - Result = decodeULEB128(reinterpret_cast(Data.data()), &N); + Result = decodeULEB128(Data.bytes_begin(), &N); if (N > Data.size()) return make_error(coveragemap_error::malformed); Data = Data.substr(N); @@ -595,16 +595,14 @@ static Error loadTestingFormat(StringRef Data, InstrProfSymtab &ProfileNames, if (Data.empty()) return make_error(coveragemap_error::truncated); unsigned N = 0; - auto ProfileNamesSize = - decodeULEB128(reinterpret_cast(Data.data()), &N); + uint64_t ProfileNamesSize = decodeULEB128(Data.bytes_begin(), &N); if (N > Data.size()) return make_error(coveragemap_error::malformed); Data = Data.substr(N); if (Data.empty()) return make_error(coveragemap_error::truncated); N = 0; - uint64_t Address = - decodeULEB128(reinterpret_cast(Data.data()), &N); + uint64_t Address = decodeULEB128(Data.bytes_begin(), &N); if (N > Data.size()) return make_error(coveragemap_error::malformed); Data = Data.substr(N); diff --git a/lib/ProfileData/InstrProf.cpp b/lib/ProfileData/InstrProf.cpp index ccb651a22b3..91a3d173111 100644 --- a/lib/ProfileData/InstrProf.cpp +++ b/lib/ProfileData/InstrProf.cpp @@ -434,9 +434,8 @@ Error collectPGOFuncNameStrings(ArrayRef NameVars, } Error readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab) { - const uint8_t *P = reinterpret_cast(NameStrings.data()); - const uint8_t *EndP = reinterpret_cast(NameStrings.data() + - NameStrings.size()); + const uint8_t *P = NameStrings.bytes_begin(); + const uint8_t *EndP = NameStrings.bytes_end(); while (P < EndP) { uint32_t N; uint64_t UncompressedSize = decodeULEB128(P, &N); diff --git a/tools/llvm-objdump/MachODump.cpp b/tools/llvm-objdump/MachODump.cpp index dc60ee1f0c6..b2c52869501 100644 --- a/tools/llvm-objdump/MachODump.cpp +++ b/tools/llvm-objdump/MachODump.cpp @@ -7275,8 +7275,7 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, StringRef BytesStr; Sections[SectIdx].getContents(BytesStr); - ArrayRef Bytes(reinterpret_cast(BytesStr.data()), - BytesStr.size()); + ArrayRef Bytes = arrayRefFromStringRef(BytesStr); uint64_t SectAddress = Sections[SectIdx].getAddress(); bool symbolTableWorked = false; diff --git a/tools/llvm-objdump/llvm-objdump.cpp b/tools/llvm-objdump/llvm-objdump.cpp index 4f0446a3144..b035cfa7c09 100644 --- a/tools/llvm-objdump/llvm-objdump.cpp +++ b/tools/llvm-objdump/llvm-objdump.cpp @@ -1084,8 +1084,7 @@ static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj, StringRef BytesStr; error(Section.getContents(BytesStr)); - ArrayRef Bytes(reinterpret_cast(BytesStr.data()), - BytesStr.size()); + ArrayRef Bytes = arrayRefFromStringRef(BytesStr); uint64_t VMAAdjustment = 0; if (shouldAdjustVA(Section)) diff --git a/tools/llvm-readobj/COFFDumper.cpp b/tools/llvm-readobj/COFFDumper.cpp index 481838ed366..334203c0d99 100644 --- a/tools/llvm-readobj/COFFDumper.cpp +++ b/tools/llvm-readobj/COFFDumper.cpp @@ -1862,9 +1862,8 @@ void COFFDumper::printStackMap() const { StringRef StackMapContents; StackMapSection.getContents(StackMapContents); - ArrayRef StackMapContentsArray( - reinterpret_cast(StackMapContents.data()), - StackMapContents.size()); + ArrayRef StackMapContentsArray = + arrayRefFromStringRef(StackMapContents); if (Obj->isLittleEndian()) prettyPrintStackMap( @@ -1890,13 +1889,12 @@ void COFFDumper::printAddrsig() { StringRef AddrsigContents; AddrsigSection.getContents(AddrsigContents); - ArrayRef AddrsigContentsArray( - reinterpret_cast(AddrsigContents.data()), - AddrsigContents.size()); + ArrayRef AddrsigContentsArray(AddrsigContents.bytes_begin(), + AddrsigContents.size()); ListScope L(W, "Addrsig"); - auto *Cur = reinterpret_cast(AddrsigContents.begin()); - auto *End = reinterpret_cast(AddrsigContents.end()); + const uint8_t *Cur = AddrsigContents.bytes_begin(); + const uint8_t *End = AddrsigContents.bytes_end(); while (Cur != End) { unsigned Size; const char *Err; diff --git a/tools/llvm-readobj/MachODumper.cpp b/tools/llvm-readobj/MachODumper.cpp index 1c3e33806b7..0cb954c1ed8 100644 --- a/tools/llvm-readobj/MachODumper.cpp +++ b/tools/llvm-readobj/MachODumper.cpp @@ -661,9 +661,8 @@ void MachODumper::printStackMap() const { StringRef StackMapContents; StackMapSection.getContents(StackMapContents); - ArrayRef StackMapContentsArray( - reinterpret_cast(StackMapContents.data()), - StackMapContents.size()); + ArrayRef StackMapContentsArray = + arrayRefFromStringRef(StackMapContents); if (Obj->isLittleEndian()) prettyPrintStackMap( diff --git a/tools/sancov/sancov.cpp b/tools/sancov/sancov.cpp index cd149a5b03f..31949a7c5bc 100644 --- a/tools/sancov/sancov.cpp +++ b/tools/sancov/sancov.cpp @@ -843,8 +843,7 @@ static void getObjectCoveragePoints(const object::ObjectFile &O, StringRef BytesStr; failIfError(Section.getContents(BytesStr)); - ArrayRef Bytes(reinterpret_cast(BytesStr.data()), - BytesStr.size()); + ArrayRef Bytes = arrayRefFromStringRef(BytesStr); for (uint64_t Index = 0, Size = 0; Index < Section.getSize(); Index += Size) { diff --git a/unittests/MC/DwarfLineTables.cpp b/unittests/MC/DwarfLineTables.cpp index 95ae72bceac..af1250daee5 100644 --- a/unittests/MC/DwarfLineTables.cpp +++ b/unittests/MC/DwarfLineTables.cpp @@ -57,9 +57,7 @@ void verifyEncoding(MCDwarfLineTableParams Params, int LineDelta, int AddrDelta, raw_svector_ostream EncodingOS(Buffer); MCDwarfLineAddr::Encode(getContext(), Params, LineDelta, AddrDelta, EncodingOS); - ArrayRef Encoding(reinterpret_cast(Buffer.data()), - Buffer.size()); - EXPECT_EQ(ExpectedEncoding, Encoding); + EXPECT_EQ(ExpectedEncoding, arrayRefFromStringRef(Buffer)); } TEST(DwarfLineTables, TestDefaultParams) {