diff options
author | Rafael Espindola <rafael.espindola@gmail.com> | 2014-07-04 14:12:46 +0000 |
---|---|---|
committer | Rafael Espindola <rafael.espindola@gmail.com> | 2014-07-04 14:12:46 +0000 |
commit | f98536a04638b2ba4662e11b12b29ea8d7e1aa25 (patch) | |
tree | 5df3413eba9bb542613c4fc1cd1e68c920049216 | |
parent | d346cc8efc614c5598476a9388b3f120b98d92c0 (diff) | |
download | llvm-f98536a04638b2ba4662e11b12b29ea8d7e1aa25.zip llvm-f98536a04638b2ba4662e11b12b29ea8d7e1aa25.tar.gz llvm-f98536a04638b2ba4662e11b12b29ea8d7e1aa25.tar.bz2 |
Convert a few std::strings to StringRef.
llvm-svn: 212342
-rw-r--r-- | llvm/include/llvm/Bitcode/ReaderWriter.h | 4 | ||||
-rw-r--r-- | llvm/lib/Bitcode/Reader/BitcodeReader.cpp | 29 | ||||
-rw-r--r-- | llvm/lib/Bitcode/Reader/BitcodeReader.h | 9 | ||||
-rw-r--r-- | llvm/lib/LTO/LTOModule.cpp | 4 |
4 files changed, 29 insertions, 17 deletions
diff --git a/llvm/include/llvm/Bitcode/ReaderWriter.h b/llvm/include/llvm/Bitcode/ReaderWriter.h index 8cf5735..29c15af 100644 --- a/llvm/include/llvm/Bitcode/ReaderWriter.h +++ b/llvm/include/llvm/Bitcode/ReaderWriter.h @@ -14,6 +14,7 @@ #ifndef LLVM_BITCODE_READERWRITER_H #define LLVM_BITCODE_READERWRITER_H +#include "llvm/ADT/StringRef.h" #include "llvm/Support/ErrorOr.h" #include <string> @@ -44,8 +45,7 @@ namespace llvm { /// Read the header of the specified bitcode buffer and extract just the /// triple information. If successful, this returns a string and *does not* /// take ownership of 'buffer'. On error, this returns "". - std::string getBitcodeTargetTriple(MemoryBuffer *Buffer, - LLVMContext &Context); + StringRef getBitcodeTargetTriple(MemoryBuffer *Buffer, LLVMContext &Context); /// Read the specified bitcode file, returning the module. /// This method *never* takes ownership of Buffer. diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp index c02b587..348234d 100644 --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -71,6 +71,15 @@ static bool ConvertToString(ArrayRef<uint64_t> Record, unsigned Idx, return false; } +ErrorOr<StringRef> BitcodeReader::convertToStringRef(ArrayRef<uint64_t> Record, + unsigned Idx) { + if (Idx > Record.size()) + return Error(InvalidRecord); + + return StringRef((char*)&Record[Idx], Record.size() - Idx); +} + + static GlobalValue::LinkageTypes GetDecodedLinkage(unsigned Val) { switch (Val) { default: // Map unknown/new linkages to external @@ -2116,13 +2125,13 @@ std::error_code BitcodeReader::ParseBitcodeInto(Module *M) { } } -ErrorOr<std::string> BitcodeReader::parseModuleTriple() { +ErrorOr<StringRef> BitcodeReader::parseModuleTriple() { if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) return Error(InvalidRecord); SmallVector<uint64_t, 64> Record; - std::string Triple; + StringRef Triple; // Read all the records for this module. while (1) { BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); @@ -2142,10 +2151,10 @@ ErrorOr<std::string> BitcodeReader::parseModuleTriple() { switch (Stream.readRecord(Entry.ID, Record)) { default: break; // Default behavior, ignore unknown content. case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] - std::string S; - if (ConvertToString(Record, 0, S)) - return Error(InvalidRecord); - Triple = S; + ErrorOr<StringRef> S = convertToStringRef(Record, 0); + if (std::error_code EC = S.getError()) + return EC; + Triple = S.get(); break; } } @@ -2154,7 +2163,7 @@ ErrorOr<std::string> BitcodeReader::parseModuleTriple() { return Triple; } -ErrorOr<std::string> BitcodeReader::parseTriple() { +ErrorOr<StringRef> BitcodeReader::parseTriple() { if (std::error_code EC = InitStream()) return EC; @@ -3469,10 +3478,10 @@ ErrorOr<Module *> llvm::parseBitcodeFile(MemoryBuffer *Buffer, return M; } -std::string llvm::getBitcodeTargetTriple(MemoryBuffer *Buffer, - LLVMContext &Context) { +StringRef llvm::getBitcodeTargetTriple(MemoryBuffer *Buffer, + LLVMContext &Context) { BitcodeReader *R = new BitcodeReader(Buffer, Context); - ErrorOr<std::string> Triple = R->parseTriple(); + ErrorOr<StringRef> Triple = R->parseTriple(); R->releaseBuffer(); delete R; if (Triple.getError()) diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.h b/llvm/lib/Bitcode/Reader/BitcodeReader.h index 1d4869a..e831ea5 100644 --- a/llvm/lib/Bitcode/Reader/BitcodeReader.h +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.h @@ -196,6 +196,9 @@ class BitcodeReader : public GVMaterializer { static const std::error_category &BitcodeErrorCategory(); + static ErrorOr<StringRef> convertToStringRef(ArrayRef<uint64_t> Record, + unsigned Idx); + public: enum ErrorType { BitcodeStreamInvalidSize, @@ -220,7 +223,7 @@ public: InvalidValue // Invalid version, inst number, attr number, etc }; - std::error_code Error(ErrorType E) { + static std::error_code Error(ErrorType E) { return std::error_code(E, BitcodeErrorCategory()); } @@ -252,7 +255,7 @@ public: /// @brief Cheap mechanism to just extract module triple /// @returns true if an error occurred. - ErrorOr<std::string> parseTriple(); + ErrorOr<StringRef> parseTriple(); static uint64_t decodeSignRotatedValue(uint64_t V); @@ -354,7 +357,7 @@ private: std::error_code ResolveGlobalAndAliasInits(); std::error_code ParseMetadata(); std::error_code ParseMetadataAttachment(); - ErrorOr<std::string> parseModuleTriple(); + ErrorOr<StringRef> parseModuleTriple(); std::error_code ParseUseLists(); std::error_code InitStream(); std::error_code InitStreamFromBuffer(); diff --git a/llvm/lib/LTO/LTOModule.cpp b/llvm/lib/LTO/LTOModule.cpp index 961c759..589ce2d 100644 --- a/llvm/lib/LTO/LTOModule.cpp +++ b/llvm/lib/LTO/LTOModule.cpp @@ -70,8 +70,8 @@ bool LTOModule::isBitcodeFile(const char *path) { bool LTOModule::isBitcodeForTarget(MemoryBuffer *buffer, StringRef triplePrefix) { - std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext()); - return StringRef(Triple).startswith(triplePrefix); + StringRef Triple = getBitcodeTargetTriple(buffer, getGlobalContext()); + return Triple.startswith(triplePrefix); } LTOModule *LTOModule::createFromFile(const char *path, TargetOptions options, |