llvm.org GIT mirror llvm / 9f72a18
Convert a few std::strings to StringRef. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@212342 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 6 years ago
4 changed file(s) with 29 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
1313 #ifndef LLVM_BITCODE_READERWRITER_H
1414 #define LLVM_BITCODE_READERWRITER_H
1515
16 #include "llvm/ADT/StringRef.h"
1617 #include "llvm/Support/ErrorOr.h"
1718 #include
1819
4344 /// Read the header of the specified bitcode buffer and extract just the
4445 /// triple information. If successful, this returns a string and *does not*
4546 /// take ownership of 'buffer'. On error, this returns "".
46 std::string getBitcodeTargetTriple(MemoryBuffer *Buffer,
47 LLVMContext &Context);
47 StringRef getBitcodeTargetTriple(MemoryBuffer *Buffer, LLVMContext &Context);
4848
4949 /// Read the specified bitcode file, returning the module.
5050 /// This method *never* takes ownership of Buffer.
6969 Result += (char)Record[i];
7070 return false;
7171 }
72
73 ErrorOr BitcodeReader::convertToStringRef(ArrayRef Record,
74 unsigned Idx) {
75 if (Idx > Record.size())
76 return Error(InvalidRecord);
77
78 return StringRef((char*)&Record[Idx], Record.size() - Idx);
79 }
80
7281
7382 static GlobalValue::LinkageTypes GetDecodedLinkage(unsigned Val) {
7483 switch (Val) {
21152124 }
21162125 }
21172126
2118 ErrorOr<std::string> BitcodeReader::parseModuleTriple() {
2127 ErrorOr<StringRef> BitcodeReader::parseModuleTriple() {
21192128 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
21202129 return Error(InvalidRecord);
21212130
21222131 SmallVector Record;
21232132
2124 std::string Triple;
2133 StringRef Triple;
21252134 // Read all the records for this module.
21262135 while (1) {
21272136 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
21412150 switch (Stream.readRecord(Entry.ID, Record)) {
21422151 default: break; // Default behavior, ignore unknown content.
21432152 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
2144 std::string S;
2145 if (ConvertToString(Record, 0, S))
2146 return Error(InvalidRecord);
2147 Triple = S;
2153 ErrorOr S = convertToStringRef(Record, 0);
2154 if (std::error_code EC = S.getError())
2155 return EC;
2156 Triple = S.get();
21482157 break;
21492158 }
21502159 }
21532162 return Triple;
21542163 }
21552164
2156 ErrorOr<std::string> BitcodeReader::parseTriple() {
2165 ErrorOr<StringRef> BitcodeReader::parseTriple() {
21572166 if (std::error_code EC = InitStream())
21582167 return EC;
21592168
34683477 return M;
34693478 }
34703479
3471 std::string llvm::getBitcodeTargetTriple(MemoryBuffer *Buffer,
3472 LLVMContext &Context) {
3480 StringRef llvm::getBitcodeTargetTriple(MemoryBuffer *Buffer,
3481 LLVMContext &Context) {
34733482 BitcodeReader *R = new BitcodeReader(Buffer, Context);
3474 ErrorOr<std::string> Triple = R->parseTriple();
3483 ErrorOr<StringRef> Triple = R->parseTriple();
34753484 R->releaseBuffer();
34763485 delete R;
34773486 if (Triple.getError())
194194 bool UseRelativeIDs;
195195
196196 static const std::error_category &BitcodeErrorCategory();
197
198 static ErrorOr convertToStringRef(ArrayRef Record,
199 unsigned Idx);
197200
198201 public:
199202 enum ErrorType {
219222 InvalidValue // Invalid version, inst number, attr number, etc
220223 };
221224
222 std::error_code Error(ErrorType E) {
225 static std::error_code Error(ErrorType E) {
223226 return std::error_code(E, BitcodeErrorCategory());
224227 }
225228
251254
252255 /// @brief Cheap mechanism to just extract module triple
253256 /// @returns true if an error occurred.
254 ErrorOr<std::string> parseTriple();
257 ErrorOr<StringRef> parseTriple();
255258
256259 static uint64_t decodeSignRotatedValue(uint64_t V);
257260
353356 std::error_code ResolveGlobalAndAliasInits();
354357 std::error_code ParseMetadata();
355358 std::error_code ParseMetadataAttachment();
356 ErrorOr<std::string> parseModuleTriple();
359 ErrorOr<StringRef> parseModuleTriple();
357360 std::error_code ParseUseLists();
358361 std::error_code InitStream();
359362 std::error_code InitStreamFromBuffer();
6969
7070 bool LTOModule::isBitcodeForTarget(MemoryBuffer *buffer,
7171 StringRef triplePrefix) {
72 std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext());
73 return StringRef(Triple).startswith(triplePrefix);
72 StringRef Triple = getBitcodeTargetTriple(buffer, getGlobalContext());
73 return Triple.startswith(triplePrefix);
7474 }
7575
7676 LTOModule *LTOModule::createFromFile(const char *path, TargetOptions options,