===----------------------------------------------------------------------===// String Section ===----------------------------------------------------------------------===// string-section =: numStrings[varint] padding[bytes] // Align to 4 bytes stringOffsets[uint32_t] // Array of offsets, one per string stringData[bytes] // Concatenated string data Parses the string section a
| 382 | // |
| 383 | /// Parses the string section and sets up the string table for lazy loading. |
| 384 | static LogicalResult parseStringSection(ArrayRef<uint8_t> payload, |
| 385 | EncodingReader &reader, |
| 386 | MLIRContext &context) { |
| 387 | EncodingReader sectionReader(payload, context); |
| 388 | uint64_t numStrings; |
| 389 | if (failed(sectionReader.readVarInt(numStrings))) |
| 390 | return failure(); |
| 391 | |
| 392 | // Handle empty string table case. |
| 393 | if (numStrings == 0) { |
| 394 | reader.setStringTable(StringRef(), ArrayRef<uint32_t>()); |
| 395 | return success(); |
| 396 | } |
| 397 | |
| 398 | if (numStrings > |
| 399 | (payload.size() - sectionReader.currentOffset()) / sizeof(uint32_t)) { |
| 400 | return sectionReader.emitError() |
| 401 | << "number of strings (" << numStrings << ") exceeds the maximum of " |
| 402 | << (payload.size() - sectionReader.currentOffset()) / |
| 403 | sizeof(uint32_t) |
| 404 | << " that can fit in the remaining payload of " |
| 405 | << (payload.size() - sectionReader.currentOffset()) << " bytes."; |
| 406 | } |
| 407 | |
| 408 | // Ensure 4-byte alignment for the start indices array. |
| 409 | if (failed(sectionReader.skipPadding(alignof(uint32_t)))) |
| 410 | return failure(); |
| 411 | // Read the string offsets directly from the payload. |
| 412 | const uint32_t *startIndicesPtr = |
| 413 | reinterpret_cast<const uint32_t *>(sectionReader.getCurrentPtr()); |
| 414 | if (!startIndicesPtr) |
| 415 | return failure(); |
| 416 | ArrayRef<uint32_t> stringOffsets(startIndicesPtr, numStrings); |
| 417 | if (failed(sectionReader.skip(numStrings * sizeof(uint32_t)))) |
| 418 | return failure(); |
| 419 | // Get the string data |
| 420 | StringRef stringData( |
| 421 | reinterpret_cast<const char *>(sectionReader.getCurrentPtr()), |
| 422 | sectionReader.remaining()); |
| 423 | // Set up the string table in the main reader. |
| 424 | reader.setStringTable(stringData, stringOffsets); |
| 425 | return success(); |
| 426 | } |
| 427 | |
| 428 | //===----------------------------------------------------------------------===// |
| 429 | // Enum Parsing |
no test coverage detected