BitcodeAnalyzer.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  1. //===- BitcodeAnalyzer.cpp - Internal BitcodeAnalyzer implementation ------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/Bitcode/BitcodeAnalyzer.h"
  9. #include "llvm/Bitcode/BitcodeReader.h"
  10. #include "llvm/Bitcode/LLVMBitCodes.h"
  11. #include "llvm/Bitstream/BitCodes.h"
  12. #include "llvm/Bitstream/BitstreamReader.h"
  13. #include "llvm/Support/Format.h"
  14. #include "llvm/Support/SHA1.h"
  15. using namespace llvm;
  16. static Error reportError(StringRef Message) {
  17. return createStringError(std::errc::illegal_byte_sequence, Message.data());
  18. }
  19. /// Return a symbolic block name if known, otherwise return null.
  20. static Optional<const char *> GetBlockName(unsigned BlockID,
  21. const BitstreamBlockInfo &BlockInfo,
  22. CurStreamTypeType CurStreamType) {
  23. // Standard blocks for all bitcode files.
  24. if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
  25. if (BlockID == bitc::BLOCKINFO_BLOCK_ID)
  26. return "BLOCKINFO_BLOCK";
  27. return None;
  28. }
  29. // Check to see if we have a blockinfo record for this block, with a name.
  30. if (const BitstreamBlockInfo::BlockInfo *Info =
  31. BlockInfo.getBlockInfo(BlockID)) {
  32. if (!Info->Name.empty())
  33. return Info->Name.c_str();
  34. }
  35. if (CurStreamType != LLVMIRBitstream)
  36. return None;
  37. switch (BlockID) {
  38. default:
  39. return None;
  40. case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID:
  41. return "OPERAND_BUNDLE_TAGS_BLOCK";
  42. case bitc::MODULE_BLOCK_ID:
  43. return "MODULE_BLOCK";
  44. case bitc::PARAMATTR_BLOCK_ID:
  45. return "PARAMATTR_BLOCK";
  46. case bitc::PARAMATTR_GROUP_BLOCK_ID:
  47. return "PARAMATTR_GROUP_BLOCK_ID";
  48. case bitc::TYPE_BLOCK_ID_NEW:
  49. return "TYPE_BLOCK_ID";
  50. case bitc::CONSTANTS_BLOCK_ID:
  51. return "CONSTANTS_BLOCK";
  52. case bitc::FUNCTION_BLOCK_ID:
  53. return "FUNCTION_BLOCK";
  54. case bitc::IDENTIFICATION_BLOCK_ID:
  55. return "IDENTIFICATION_BLOCK_ID";
  56. case bitc::VALUE_SYMTAB_BLOCK_ID:
  57. return "VALUE_SYMTAB";
  58. case bitc::METADATA_BLOCK_ID:
  59. return "METADATA_BLOCK";
  60. case bitc::METADATA_KIND_BLOCK_ID:
  61. return "METADATA_KIND_BLOCK";
  62. case bitc::METADATA_ATTACHMENT_ID:
  63. return "METADATA_ATTACHMENT_BLOCK";
  64. case bitc::USELIST_BLOCK_ID:
  65. return "USELIST_BLOCK_ID";
  66. case bitc::GLOBALVAL_SUMMARY_BLOCK_ID:
  67. return "GLOBALVAL_SUMMARY_BLOCK";
  68. case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID:
  69. return "FULL_LTO_GLOBALVAL_SUMMARY_BLOCK";
  70. case bitc::MODULE_STRTAB_BLOCK_ID:
  71. return "MODULE_STRTAB_BLOCK";
  72. case bitc::STRTAB_BLOCK_ID:
  73. return "STRTAB_BLOCK";
  74. case bitc::SYMTAB_BLOCK_ID:
  75. return "SYMTAB_BLOCK";
  76. }
  77. }
  78. /// Return a symbolic code name if known, otherwise return null.
  79. static Optional<const char *> GetCodeName(unsigned CodeID, unsigned BlockID,
  80. const BitstreamBlockInfo &BlockInfo,
  81. CurStreamTypeType CurStreamType) {
  82. // Standard blocks for all bitcode files.
  83. if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
  84. if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
  85. switch (CodeID) {
  86. default:
  87. return None;
  88. case bitc::BLOCKINFO_CODE_SETBID:
  89. return "SETBID";
  90. case bitc::BLOCKINFO_CODE_BLOCKNAME:
  91. return "BLOCKNAME";
  92. case bitc::BLOCKINFO_CODE_SETRECORDNAME:
  93. return "SETRECORDNAME";
  94. }
  95. }
  96. return None;
  97. }
  98. // Check to see if we have a blockinfo record for this record, with a name.
  99. if (const BitstreamBlockInfo::BlockInfo *Info =
  100. BlockInfo.getBlockInfo(BlockID)) {
  101. for (const std::pair<unsigned, std::string> &RN : Info->RecordNames)
  102. if (RN.first == CodeID)
  103. return RN.second.c_str();
  104. }
  105. if (CurStreamType != LLVMIRBitstream)
  106. return None;
  107. #define STRINGIFY_CODE(PREFIX, CODE) \
  108. case bitc::PREFIX##_##CODE: \
  109. return #CODE;
  110. switch (BlockID) {
  111. default:
  112. return None;
  113. case bitc::MODULE_BLOCK_ID:
  114. switch (CodeID) {
  115. default:
  116. return None;
  117. STRINGIFY_CODE(MODULE_CODE, VERSION)
  118. STRINGIFY_CODE(MODULE_CODE, TRIPLE)
  119. STRINGIFY_CODE(MODULE_CODE, DATALAYOUT)
  120. STRINGIFY_CODE(MODULE_CODE, ASM)
  121. STRINGIFY_CODE(MODULE_CODE, SECTIONNAME)
  122. STRINGIFY_CODE(MODULE_CODE, DEPLIB) // Deprecated, present in old bitcode
  123. STRINGIFY_CODE(MODULE_CODE, GLOBALVAR)
  124. STRINGIFY_CODE(MODULE_CODE, FUNCTION)
  125. STRINGIFY_CODE(MODULE_CODE, ALIAS)
  126. STRINGIFY_CODE(MODULE_CODE, GCNAME)
  127. STRINGIFY_CODE(MODULE_CODE, COMDAT)
  128. STRINGIFY_CODE(MODULE_CODE, VSTOFFSET)
  129. STRINGIFY_CODE(MODULE_CODE, METADATA_VALUES_UNUSED)
  130. STRINGIFY_CODE(MODULE_CODE, SOURCE_FILENAME)
  131. STRINGIFY_CODE(MODULE_CODE, HASH)
  132. }
  133. case bitc::IDENTIFICATION_BLOCK_ID:
  134. switch (CodeID) {
  135. default:
  136. return None;
  137. STRINGIFY_CODE(IDENTIFICATION_CODE, STRING)
  138. STRINGIFY_CODE(IDENTIFICATION_CODE, EPOCH)
  139. }
  140. case bitc::PARAMATTR_BLOCK_ID:
  141. switch (CodeID) {
  142. default:
  143. return None;
  144. // FIXME: Should these be different?
  145. case bitc::PARAMATTR_CODE_ENTRY_OLD:
  146. return "ENTRY";
  147. case bitc::PARAMATTR_CODE_ENTRY:
  148. return "ENTRY";
  149. }
  150. case bitc::PARAMATTR_GROUP_BLOCK_ID:
  151. switch (CodeID) {
  152. default:
  153. return None;
  154. case bitc::PARAMATTR_GRP_CODE_ENTRY:
  155. return "ENTRY";
  156. }
  157. case bitc::TYPE_BLOCK_ID_NEW:
  158. switch (CodeID) {
  159. default:
  160. return None;
  161. STRINGIFY_CODE(TYPE_CODE, NUMENTRY)
  162. STRINGIFY_CODE(TYPE_CODE, VOID)
  163. STRINGIFY_CODE(TYPE_CODE, FLOAT)
  164. STRINGIFY_CODE(TYPE_CODE, DOUBLE)
  165. STRINGIFY_CODE(TYPE_CODE, LABEL)
  166. STRINGIFY_CODE(TYPE_CODE, OPAQUE)
  167. STRINGIFY_CODE(TYPE_CODE, INTEGER)
  168. STRINGIFY_CODE(TYPE_CODE, POINTER)
  169. STRINGIFY_CODE(TYPE_CODE, HALF)
  170. STRINGIFY_CODE(TYPE_CODE, ARRAY)
  171. STRINGIFY_CODE(TYPE_CODE, VECTOR)
  172. STRINGIFY_CODE(TYPE_CODE, X86_FP80)
  173. STRINGIFY_CODE(TYPE_CODE, FP128)
  174. STRINGIFY_CODE(TYPE_CODE, PPC_FP128)
  175. STRINGIFY_CODE(TYPE_CODE, METADATA)
  176. STRINGIFY_CODE(TYPE_CODE, X86_MMX)
  177. STRINGIFY_CODE(TYPE_CODE, STRUCT_ANON)
  178. STRINGIFY_CODE(TYPE_CODE, STRUCT_NAME)
  179. STRINGIFY_CODE(TYPE_CODE, STRUCT_NAMED)
  180. STRINGIFY_CODE(TYPE_CODE, FUNCTION)
  181. STRINGIFY_CODE(TYPE_CODE, TOKEN)
  182. STRINGIFY_CODE(TYPE_CODE, BFLOAT)
  183. }
  184. case bitc::CONSTANTS_BLOCK_ID:
  185. switch (CodeID) {
  186. default:
  187. return None;
  188. STRINGIFY_CODE(CST_CODE, SETTYPE)
  189. STRINGIFY_CODE(CST_CODE, NULL)
  190. STRINGIFY_CODE(CST_CODE, UNDEF)
  191. STRINGIFY_CODE(CST_CODE, INTEGER)
  192. STRINGIFY_CODE(CST_CODE, WIDE_INTEGER)
  193. STRINGIFY_CODE(CST_CODE, FLOAT)
  194. STRINGIFY_CODE(CST_CODE, AGGREGATE)
  195. STRINGIFY_CODE(CST_CODE, STRING)
  196. STRINGIFY_CODE(CST_CODE, CSTRING)
  197. STRINGIFY_CODE(CST_CODE, CE_BINOP)
  198. STRINGIFY_CODE(CST_CODE, CE_CAST)
  199. STRINGIFY_CODE(CST_CODE, CE_GEP)
  200. STRINGIFY_CODE(CST_CODE, CE_INBOUNDS_GEP)
  201. STRINGIFY_CODE(CST_CODE, CE_SELECT)
  202. STRINGIFY_CODE(CST_CODE, CE_EXTRACTELT)
  203. STRINGIFY_CODE(CST_CODE, CE_INSERTELT)
  204. STRINGIFY_CODE(CST_CODE, CE_SHUFFLEVEC)
  205. STRINGIFY_CODE(CST_CODE, CE_CMP)
  206. STRINGIFY_CODE(CST_CODE, INLINEASM)
  207. STRINGIFY_CODE(CST_CODE, CE_SHUFVEC_EX)
  208. STRINGIFY_CODE(CST_CODE, CE_UNOP)
  209. STRINGIFY_CODE(CST_CODE, DSO_LOCAL_EQUIVALENT)
  210. STRINGIFY_CODE(CST_CODE, NO_CFI_VALUE)
  211. case bitc::CST_CODE_BLOCKADDRESS:
  212. return "CST_CODE_BLOCKADDRESS";
  213. STRINGIFY_CODE(CST_CODE, DATA)
  214. }
  215. case bitc::FUNCTION_BLOCK_ID:
  216. switch (CodeID) {
  217. default:
  218. return None;
  219. STRINGIFY_CODE(FUNC_CODE, DECLAREBLOCKS)
  220. STRINGIFY_CODE(FUNC_CODE, INST_BINOP)
  221. STRINGIFY_CODE(FUNC_CODE, INST_CAST)
  222. STRINGIFY_CODE(FUNC_CODE, INST_GEP_OLD)
  223. STRINGIFY_CODE(FUNC_CODE, INST_INBOUNDS_GEP_OLD)
  224. STRINGIFY_CODE(FUNC_CODE, INST_SELECT)
  225. STRINGIFY_CODE(FUNC_CODE, INST_EXTRACTELT)
  226. STRINGIFY_CODE(FUNC_CODE, INST_INSERTELT)
  227. STRINGIFY_CODE(FUNC_CODE, INST_SHUFFLEVEC)
  228. STRINGIFY_CODE(FUNC_CODE, INST_CMP)
  229. STRINGIFY_CODE(FUNC_CODE, INST_RET)
  230. STRINGIFY_CODE(FUNC_CODE, INST_BR)
  231. STRINGIFY_CODE(FUNC_CODE, INST_SWITCH)
  232. STRINGIFY_CODE(FUNC_CODE, INST_INVOKE)
  233. STRINGIFY_CODE(FUNC_CODE, INST_UNOP)
  234. STRINGIFY_CODE(FUNC_CODE, INST_UNREACHABLE)
  235. STRINGIFY_CODE(FUNC_CODE, INST_CLEANUPRET)
  236. STRINGIFY_CODE(FUNC_CODE, INST_CATCHRET)
  237. STRINGIFY_CODE(FUNC_CODE, INST_CATCHPAD)
  238. STRINGIFY_CODE(FUNC_CODE, INST_PHI)
  239. STRINGIFY_CODE(FUNC_CODE, INST_ALLOCA)
  240. STRINGIFY_CODE(FUNC_CODE, INST_LOAD)
  241. STRINGIFY_CODE(FUNC_CODE, INST_VAARG)
  242. STRINGIFY_CODE(FUNC_CODE, INST_STORE)
  243. STRINGIFY_CODE(FUNC_CODE, INST_EXTRACTVAL)
  244. STRINGIFY_CODE(FUNC_CODE, INST_INSERTVAL)
  245. STRINGIFY_CODE(FUNC_CODE, INST_CMP2)
  246. STRINGIFY_CODE(FUNC_CODE, INST_VSELECT)
  247. STRINGIFY_CODE(FUNC_CODE, DEBUG_LOC_AGAIN)
  248. STRINGIFY_CODE(FUNC_CODE, INST_CALL)
  249. STRINGIFY_CODE(FUNC_CODE, DEBUG_LOC)
  250. STRINGIFY_CODE(FUNC_CODE, INST_GEP)
  251. STRINGIFY_CODE(FUNC_CODE, OPERAND_BUNDLE)
  252. STRINGIFY_CODE(FUNC_CODE, INST_FENCE)
  253. STRINGIFY_CODE(FUNC_CODE, INST_ATOMICRMW)
  254. STRINGIFY_CODE(FUNC_CODE, INST_LOADATOMIC)
  255. STRINGIFY_CODE(FUNC_CODE, INST_STOREATOMIC)
  256. STRINGIFY_CODE(FUNC_CODE, INST_CMPXCHG)
  257. STRINGIFY_CODE(FUNC_CODE, INST_CALLBR)
  258. }
  259. case bitc::VALUE_SYMTAB_BLOCK_ID:
  260. switch (CodeID) {
  261. default:
  262. return None;
  263. STRINGIFY_CODE(VST_CODE, ENTRY)
  264. STRINGIFY_CODE(VST_CODE, BBENTRY)
  265. STRINGIFY_CODE(VST_CODE, FNENTRY)
  266. STRINGIFY_CODE(VST_CODE, COMBINED_ENTRY)
  267. }
  268. case bitc::MODULE_STRTAB_BLOCK_ID:
  269. switch (CodeID) {
  270. default:
  271. return None;
  272. STRINGIFY_CODE(MST_CODE, ENTRY)
  273. STRINGIFY_CODE(MST_CODE, HASH)
  274. }
  275. case bitc::GLOBALVAL_SUMMARY_BLOCK_ID:
  276. case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID:
  277. switch (CodeID) {
  278. default:
  279. return None;
  280. STRINGIFY_CODE(FS, PERMODULE)
  281. STRINGIFY_CODE(FS, PERMODULE_PROFILE)
  282. STRINGIFY_CODE(FS, PERMODULE_RELBF)
  283. STRINGIFY_CODE(FS, PERMODULE_GLOBALVAR_INIT_REFS)
  284. STRINGIFY_CODE(FS, PERMODULE_VTABLE_GLOBALVAR_INIT_REFS)
  285. STRINGIFY_CODE(FS, COMBINED)
  286. STRINGIFY_CODE(FS, COMBINED_PROFILE)
  287. STRINGIFY_CODE(FS, COMBINED_GLOBALVAR_INIT_REFS)
  288. STRINGIFY_CODE(FS, ALIAS)
  289. STRINGIFY_CODE(FS, COMBINED_ALIAS)
  290. STRINGIFY_CODE(FS, COMBINED_ORIGINAL_NAME)
  291. STRINGIFY_CODE(FS, VERSION)
  292. STRINGIFY_CODE(FS, FLAGS)
  293. STRINGIFY_CODE(FS, TYPE_TESTS)
  294. STRINGIFY_CODE(FS, TYPE_TEST_ASSUME_VCALLS)
  295. STRINGIFY_CODE(FS, TYPE_CHECKED_LOAD_VCALLS)
  296. STRINGIFY_CODE(FS, TYPE_TEST_ASSUME_CONST_VCALL)
  297. STRINGIFY_CODE(FS, TYPE_CHECKED_LOAD_CONST_VCALL)
  298. STRINGIFY_CODE(FS, VALUE_GUID)
  299. STRINGIFY_CODE(FS, CFI_FUNCTION_DEFS)
  300. STRINGIFY_CODE(FS, CFI_FUNCTION_DECLS)
  301. STRINGIFY_CODE(FS, TYPE_ID)
  302. STRINGIFY_CODE(FS, TYPE_ID_METADATA)
  303. STRINGIFY_CODE(FS, BLOCK_COUNT)
  304. STRINGIFY_CODE(FS, PARAM_ACCESS)
  305. }
  306. case bitc::METADATA_ATTACHMENT_ID:
  307. switch (CodeID) {
  308. default:
  309. return None;
  310. STRINGIFY_CODE(METADATA, ATTACHMENT)
  311. }
  312. case bitc::METADATA_BLOCK_ID:
  313. switch (CodeID) {
  314. default:
  315. return None;
  316. STRINGIFY_CODE(METADATA, STRING_OLD)
  317. STRINGIFY_CODE(METADATA, VALUE)
  318. STRINGIFY_CODE(METADATA, NODE)
  319. STRINGIFY_CODE(METADATA, NAME)
  320. STRINGIFY_CODE(METADATA, DISTINCT_NODE)
  321. STRINGIFY_CODE(METADATA, KIND) // Older bitcode has it in a MODULE_BLOCK
  322. STRINGIFY_CODE(METADATA, LOCATION)
  323. STRINGIFY_CODE(METADATA, OLD_NODE)
  324. STRINGIFY_CODE(METADATA, OLD_FN_NODE)
  325. STRINGIFY_CODE(METADATA, NAMED_NODE)
  326. STRINGIFY_CODE(METADATA, GENERIC_DEBUG)
  327. STRINGIFY_CODE(METADATA, SUBRANGE)
  328. STRINGIFY_CODE(METADATA, ENUMERATOR)
  329. STRINGIFY_CODE(METADATA, BASIC_TYPE)
  330. STRINGIFY_CODE(METADATA, FILE)
  331. STRINGIFY_CODE(METADATA, DERIVED_TYPE)
  332. STRINGIFY_CODE(METADATA, COMPOSITE_TYPE)
  333. STRINGIFY_CODE(METADATA, SUBROUTINE_TYPE)
  334. STRINGIFY_CODE(METADATA, COMPILE_UNIT)
  335. STRINGIFY_CODE(METADATA, SUBPROGRAM)
  336. STRINGIFY_CODE(METADATA, LEXICAL_BLOCK)
  337. STRINGIFY_CODE(METADATA, LEXICAL_BLOCK_FILE)
  338. STRINGIFY_CODE(METADATA, NAMESPACE)
  339. STRINGIFY_CODE(METADATA, TEMPLATE_TYPE)
  340. STRINGIFY_CODE(METADATA, TEMPLATE_VALUE)
  341. STRINGIFY_CODE(METADATA, GLOBAL_VAR)
  342. STRINGIFY_CODE(METADATA, LOCAL_VAR)
  343. STRINGIFY_CODE(METADATA, EXPRESSION)
  344. STRINGIFY_CODE(METADATA, OBJC_PROPERTY)
  345. STRINGIFY_CODE(METADATA, IMPORTED_ENTITY)
  346. STRINGIFY_CODE(METADATA, MODULE)
  347. STRINGIFY_CODE(METADATA, MACRO)
  348. STRINGIFY_CODE(METADATA, MACRO_FILE)
  349. STRINGIFY_CODE(METADATA, STRINGS)
  350. STRINGIFY_CODE(METADATA, GLOBAL_DECL_ATTACHMENT)
  351. STRINGIFY_CODE(METADATA, GLOBAL_VAR_EXPR)
  352. STRINGIFY_CODE(METADATA, INDEX_OFFSET)
  353. STRINGIFY_CODE(METADATA, INDEX)
  354. STRINGIFY_CODE(METADATA, ARG_LIST)
  355. }
  356. case bitc::METADATA_KIND_BLOCK_ID:
  357. switch (CodeID) {
  358. default:
  359. return None;
  360. STRINGIFY_CODE(METADATA, KIND)
  361. }
  362. case bitc::USELIST_BLOCK_ID:
  363. switch (CodeID) {
  364. default:
  365. return None;
  366. case bitc::USELIST_CODE_DEFAULT:
  367. return "USELIST_CODE_DEFAULT";
  368. case bitc::USELIST_CODE_BB:
  369. return "USELIST_CODE_BB";
  370. }
  371. case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID:
  372. switch (CodeID) {
  373. default:
  374. return None;
  375. case bitc::OPERAND_BUNDLE_TAG:
  376. return "OPERAND_BUNDLE_TAG";
  377. }
  378. case bitc::STRTAB_BLOCK_ID:
  379. switch (CodeID) {
  380. default:
  381. return None;
  382. case bitc::STRTAB_BLOB:
  383. return "BLOB";
  384. }
  385. case bitc::SYMTAB_BLOCK_ID:
  386. switch (CodeID) {
  387. default:
  388. return None;
  389. case bitc::SYMTAB_BLOB:
  390. return "BLOB";
  391. }
  392. }
  393. #undef STRINGIFY_CODE
  394. }
  395. static void printSize(raw_ostream &OS, double Bits) {
  396. OS << format("%.2f/%.2fB/%luW", Bits, Bits / 8, (unsigned long)(Bits / 32));
  397. }
  398. static void printSize(raw_ostream &OS, uint64_t Bits) {
  399. OS << format("%lub/%.2fB/%luW", (unsigned long)Bits, (double)Bits / 8,
  400. (unsigned long)(Bits / 32));
  401. }
  402. static Expected<CurStreamTypeType> ReadSignature(BitstreamCursor &Stream) {
  403. auto tryRead = [&Stream](char &Dest, size_t size) -> Error {
  404. if (Expected<SimpleBitstreamCursor::word_t> MaybeWord = Stream.Read(size))
  405. Dest = MaybeWord.get();
  406. else
  407. return MaybeWord.takeError();
  408. return Error::success();
  409. };
  410. char Signature[6];
  411. if (Error Err = tryRead(Signature[0], 8))
  412. return std::move(Err);
  413. if (Error Err = tryRead(Signature[1], 8))
  414. return std::move(Err);
  415. // Autodetect the file contents, if it is one we know.
  416. if (Signature[0] == 'C' && Signature[1] == 'P') {
  417. if (Error Err = tryRead(Signature[2], 8))
  418. return std::move(Err);
  419. if (Error Err = tryRead(Signature[3], 8))
  420. return std::move(Err);
  421. if (Signature[2] == 'C' && Signature[3] == 'H')
  422. return ClangSerializedASTBitstream;
  423. } else if (Signature[0] == 'D' && Signature[1] == 'I') {
  424. if (Error Err = tryRead(Signature[2], 8))
  425. return std::move(Err);
  426. if (Error Err = tryRead(Signature[3], 8))
  427. return std::move(Err);
  428. if (Signature[2] == 'A' && Signature[3] == 'G')
  429. return ClangSerializedDiagnosticsBitstream;
  430. } else if (Signature[0] == 'R' && Signature[1] == 'M') {
  431. if (Error Err = tryRead(Signature[2], 8))
  432. return std::move(Err);
  433. if (Error Err = tryRead(Signature[3], 8))
  434. return std::move(Err);
  435. if (Signature[2] == 'R' && Signature[3] == 'K')
  436. return LLVMBitstreamRemarks;
  437. } else {
  438. if (Error Err = tryRead(Signature[2], 4))
  439. return std::move(Err);
  440. if (Error Err = tryRead(Signature[3], 4))
  441. return std::move(Err);
  442. if (Error Err = tryRead(Signature[4], 4))
  443. return std::move(Err);
  444. if (Error Err = tryRead(Signature[5], 4))
  445. return std::move(Err);
  446. if (Signature[0] == 'B' && Signature[1] == 'C' && Signature[2] == 0x0 &&
  447. Signature[3] == 0xC && Signature[4] == 0xE && Signature[5] == 0xD)
  448. return LLVMIRBitstream;
  449. }
  450. return UnknownBitstream;
  451. }
  452. static Expected<CurStreamTypeType> analyzeHeader(Optional<BCDumpOptions> O,
  453. BitstreamCursor &Stream) {
  454. ArrayRef<uint8_t> Bytes = Stream.getBitcodeBytes();
  455. const unsigned char *BufPtr = (const unsigned char *)Bytes.data();
  456. const unsigned char *EndBufPtr = BufPtr + Bytes.size();
  457. // If we have a wrapper header, parse it and ignore the non-bc file
  458. // contents. The magic number is 0x0B17C0DE stored in little endian.
  459. if (isBitcodeWrapper(BufPtr, EndBufPtr)) {
  460. if (Bytes.size() < BWH_HeaderSize)
  461. return reportError("Invalid bitcode wrapper header");
  462. if (O) {
  463. unsigned Magic = support::endian::read32le(&BufPtr[BWH_MagicField]);
  464. unsigned Version = support::endian::read32le(&BufPtr[BWH_VersionField]);
  465. unsigned Offset = support::endian::read32le(&BufPtr[BWH_OffsetField]);
  466. unsigned Size = support::endian::read32le(&BufPtr[BWH_SizeField]);
  467. unsigned CPUType = support::endian::read32le(&BufPtr[BWH_CPUTypeField]);
  468. O->OS << "<BITCODE_WRAPPER_HEADER"
  469. << " Magic=" << format_hex(Magic, 10)
  470. << " Version=" << format_hex(Version, 10)
  471. << " Offset=" << format_hex(Offset, 10)
  472. << " Size=" << format_hex(Size, 10)
  473. << " CPUType=" << format_hex(CPUType, 10) << "/>\n";
  474. }
  475. if (SkipBitcodeWrapperHeader(BufPtr, EndBufPtr, true))
  476. return reportError("Invalid bitcode wrapper header");
  477. }
  478. // Use the cursor modified by skipping the wrapper header.
  479. Stream = BitstreamCursor(ArrayRef<uint8_t>(BufPtr, EndBufPtr));
  480. return ReadSignature(Stream);
  481. }
  482. static bool canDecodeBlob(unsigned Code, unsigned BlockID) {
  483. return BlockID == bitc::METADATA_BLOCK_ID && Code == bitc::METADATA_STRINGS;
  484. }
  485. Error BitcodeAnalyzer::decodeMetadataStringsBlob(StringRef Indent,
  486. ArrayRef<uint64_t> Record,
  487. StringRef Blob,
  488. raw_ostream &OS) {
  489. if (Blob.empty())
  490. return reportError("Cannot decode empty blob.");
  491. if (Record.size() != 2)
  492. return reportError(
  493. "Decoding metadata strings blob needs two record entries.");
  494. unsigned NumStrings = Record[0];
  495. unsigned StringsOffset = Record[1];
  496. OS << " num-strings = " << NumStrings << " {\n";
  497. StringRef Lengths = Blob.slice(0, StringsOffset);
  498. SimpleBitstreamCursor R(Lengths);
  499. StringRef Strings = Blob.drop_front(StringsOffset);
  500. do {
  501. if (R.AtEndOfStream())
  502. return reportError("bad length");
  503. uint32_t Size;
  504. if (Error E = R.ReadVBR(6).moveInto(Size))
  505. return E;
  506. if (Strings.size() < Size)
  507. return reportError("truncated chars");
  508. OS << Indent << " '";
  509. OS.write_escaped(Strings.slice(0, Size), /*hex=*/true);
  510. OS << "'\n";
  511. Strings = Strings.drop_front(Size);
  512. } while (--NumStrings);
  513. OS << Indent << " }";
  514. return Error::success();
  515. }
  516. BitcodeAnalyzer::BitcodeAnalyzer(StringRef Buffer,
  517. Optional<StringRef> BlockInfoBuffer)
  518. : Stream(Buffer) {
  519. if (BlockInfoBuffer)
  520. BlockInfoStream.emplace(*BlockInfoBuffer);
  521. }
  522. Error BitcodeAnalyzer::analyze(Optional<BCDumpOptions> O,
  523. Optional<StringRef> CheckHash) {
  524. if (Error E = analyzeHeader(O, Stream).moveInto(CurStreamType))
  525. return E;
  526. Stream.setBlockInfo(&BlockInfo);
  527. // Read block info from BlockInfoStream, if specified.
  528. // The block info must be a top-level block.
  529. if (BlockInfoStream) {
  530. BitstreamCursor BlockInfoCursor(*BlockInfoStream);
  531. if (Error E = analyzeHeader(O, BlockInfoCursor).takeError())
  532. return E;
  533. while (!BlockInfoCursor.AtEndOfStream()) {
  534. Expected<unsigned> MaybeCode = BlockInfoCursor.ReadCode();
  535. if (!MaybeCode)
  536. return MaybeCode.takeError();
  537. if (MaybeCode.get() != bitc::ENTER_SUBBLOCK)
  538. return reportError("Invalid record at top-level in block info file");
  539. Expected<unsigned> MaybeBlockID = BlockInfoCursor.ReadSubBlockID();
  540. if (!MaybeBlockID)
  541. return MaybeBlockID.takeError();
  542. if (MaybeBlockID.get() == bitc::BLOCKINFO_BLOCK_ID) {
  543. Optional<BitstreamBlockInfo> NewBlockInfo;
  544. if (Error E =
  545. BlockInfoCursor.ReadBlockInfoBlock(/*ReadBlockInfoNames=*/true)
  546. .moveInto(NewBlockInfo))
  547. return E;
  548. if (!NewBlockInfo)
  549. return reportError("Malformed BlockInfoBlock in block info file");
  550. BlockInfo = std::move(*NewBlockInfo);
  551. break;
  552. }
  553. if (Error Err = BlockInfoCursor.SkipBlock())
  554. return Err;
  555. }
  556. }
  557. // Parse the top-level structure. We only allow blocks at the top-level.
  558. while (!Stream.AtEndOfStream()) {
  559. Expected<unsigned> MaybeCode = Stream.ReadCode();
  560. if (!MaybeCode)
  561. return MaybeCode.takeError();
  562. if (MaybeCode.get() != bitc::ENTER_SUBBLOCK)
  563. return reportError("Invalid record at top-level");
  564. Expected<unsigned> MaybeBlockID = Stream.ReadSubBlockID();
  565. if (!MaybeBlockID)
  566. return MaybeBlockID.takeError();
  567. if (Error E = parseBlock(MaybeBlockID.get(), 0, O, CheckHash))
  568. return E;
  569. ++NumTopBlocks;
  570. }
  571. return Error::success();
  572. }
  573. void BitcodeAnalyzer::printStats(BCDumpOptions O,
  574. Optional<StringRef> Filename) {
  575. uint64_t BufferSizeBits = Stream.getBitcodeBytes().size() * CHAR_BIT;
  576. // Print a summary of the read file.
  577. O.OS << "Summary ";
  578. if (Filename)
  579. O.OS << "of " << Filename->data() << ":\n";
  580. O.OS << " Total size: ";
  581. printSize(O.OS, BufferSizeBits);
  582. O.OS << "\n";
  583. O.OS << " Stream type: ";
  584. switch (CurStreamType) {
  585. case UnknownBitstream:
  586. O.OS << "unknown\n";
  587. break;
  588. case LLVMIRBitstream:
  589. O.OS << "LLVM IR\n";
  590. break;
  591. case ClangSerializedASTBitstream:
  592. O.OS << "Clang Serialized AST\n";
  593. break;
  594. case ClangSerializedDiagnosticsBitstream:
  595. O.OS << "Clang Serialized Diagnostics\n";
  596. break;
  597. case LLVMBitstreamRemarks:
  598. O.OS << "LLVM Remarks\n";
  599. break;
  600. }
  601. O.OS << " # Toplevel Blocks: " << NumTopBlocks << "\n";
  602. O.OS << "\n";
  603. // Emit per-block stats.
  604. O.OS << "Per-block Summary:\n";
  605. for (const auto &Stat : BlockIDStats) {
  606. O.OS << " Block ID #" << Stat.first;
  607. if (Optional<const char *> BlockName =
  608. GetBlockName(Stat.first, BlockInfo, CurStreamType))
  609. O.OS << " (" << *BlockName << ")";
  610. O.OS << ":\n";
  611. const PerBlockIDStats &Stats = Stat.second;
  612. O.OS << " Num Instances: " << Stats.NumInstances << "\n";
  613. O.OS << " Total Size: ";
  614. printSize(O.OS, Stats.NumBits);
  615. O.OS << "\n";
  616. double pct = (Stats.NumBits * 100.0) / BufferSizeBits;
  617. O.OS << " Percent of file: " << format("%2.4f%%", pct) << "\n";
  618. if (Stats.NumInstances > 1) {
  619. O.OS << " Average Size: ";
  620. printSize(O.OS, Stats.NumBits / (double)Stats.NumInstances);
  621. O.OS << "\n";
  622. O.OS << " Tot/Avg SubBlocks: " << Stats.NumSubBlocks << "/"
  623. << Stats.NumSubBlocks / (double)Stats.NumInstances << "\n";
  624. O.OS << " Tot/Avg Abbrevs: " << Stats.NumAbbrevs << "/"
  625. << Stats.NumAbbrevs / (double)Stats.NumInstances << "\n";
  626. O.OS << " Tot/Avg Records: " << Stats.NumRecords << "/"
  627. << Stats.NumRecords / (double)Stats.NumInstances << "\n";
  628. } else {
  629. O.OS << " Num SubBlocks: " << Stats.NumSubBlocks << "\n";
  630. O.OS << " Num Abbrevs: " << Stats.NumAbbrevs << "\n";
  631. O.OS << " Num Records: " << Stats.NumRecords << "\n";
  632. }
  633. if (Stats.NumRecords) {
  634. double pct = (Stats.NumAbbreviatedRecords * 100.0) / Stats.NumRecords;
  635. O.OS << " Percent Abbrevs: " << format("%2.4f%%", pct) << "\n";
  636. }
  637. O.OS << "\n";
  638. // Print a histogram of the codes we see.
  639. if (O.Histogram && !Stats.CodeFreq.empty()) {
  640. std::vector<std::pair<unsigned, unsigned>> FreqPairs; // <freq,code>
  641. for (unsigned i = 0, e = Stats.CodeFreq.size(); i != e; ++i)
  642. if (unsigned Freq = Stats.CodeFreq[i].NumInstances)
  643. FreqPairs.push_back(std::make_pair(Freq, i));
  644. llvm::stable_sort(FreqPairs);
  645. std::reverse(FreqPairs.begin(), FreqPairs.end());
  646. O.OS << "\tRecord Histogram:\n";
  647. O.OS << "\t\t Count # Bits b/Rec % Abv Record Kind\n";
  648. for (const auto &FreqPair : FreqPairs) {
  649. const PerRecordStats &RecStats = Stats.CodeFreq[FreqPair.second];
  650. O.OS << format("\t\t%7d %9lu", RecStats.NumInstances,
  651. (unsigned long)RecStats.TotalBits);
  652. if (RecStats.NumInstances > 1)
  653. O.OS << format(" %9.1f",
  654. (double)RecStats.TotalBits / RecStats.NumInstances);
  655. else
  656. O.OS << " ";
  657. if (RecStats.NumAbbrev)
  658. O.OS << format(" %7.2f", (double)RecStats.NumAbbrev /
  659. RecStats.NumInstances * 100);
  660. else
  661. O.OS << " ";
  662. O.OS << " ";
  663. if (Optional<const char *> CodeName = GetCodeName(
  664. FreqPair.second, Stat.first, BlockInfo, CurStreamType))
  665. O.OS << *CodeName << "\n";
  666. else
  667. O.OS << "UnknownCode" << FreqPair.second << "\n";
  668. }
  669. O.OS << "\n";
  670. }
  671. }
  672. }
  673. Error BitcodeAnalyzer::parseBlock(unsigned BlockID, unsigned IndentLevel,
  674. Optional<BCDumpOptions> O,
  675. Optional<StringRef> CheckHash) {
  676. std::string Indent(IndentLevel * 2, ' ');
  677. uint64_t BlockBitStart = Stream.GetCurrentBitNo();
  678. // Get the statistics for this BlockID.
  679. PerBlockIDStats &BlockStats = BlockIDStats[BlockID];
  680. BlockStats.NumInstances++;
  681. // BLOCKINFO is a special part of the stream.
  682. bool DumpRecords = O.hasValue();
  683. if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
  684. if (O && !O->DumpBlockinfo)
  685. O->OS << Indent << "<BLOCKINFO_BLOCK/>\n";
  686. Optional<BitstreamBlockInfo> NewBlockInfo;
  687. if (Error E = Stream.ReadBlockInfoBlock(/*ReadBlockInfoNames=*/true)
  688. .moveInto(NewBlockInfo))
  689. return E;
  690. if (!NewBlockInfo)
  691. return reportError("Malformed BlockInfoBlock");
  692. BlockInfo = std::move(*NewBlockInfo);
  693. if (Error Err = Stream.JumpToBit(BlockBitStart))
  694. return Err;
  695. // It's not really interesting to dump the contents of the blockinfo
  696. // block, so only do it if the user explicitly requests it.
  697. DumpRecords = O && O->DumpBlockinfo;
  698. }
  699. unsigned NumWords = 0;
  700. if (Error Err = Stream.EnterSubBlock(BlockID, &NumWords))
  701. return Err;
  702. // Keep it for later, when we see a MODULE_HASH record
  703. uint64_t BlockEntryPos = Stream.getCurrentByteNo();
  704. Optional<const char *> BlockName = None;
  705. if (DumpRecords) {
  706. O->OS << Indent << "<";
  707. if ((BlockName = GetBlockName(BlockID, BlockInfo, CurStreamType)))
  708. O->OS << *BlockName;
  709. else
  710. O->OS << "UnknownBlock" << BlockID;
  711. if (!O->Symbolic && BlockName)
  712. O->OS << " BlockID=" << BlockID;
  713. O->OS << " NumWords=" << NumWords
  714. << " BlockCodeSize=" << Stream.getAbbrevIDWidth() << ">\n";
  715. }
  716. SmallVector<uint64_t, 64> Record;
  717. // Keep the offset to the metadata index if seen.
  718. uint64_t MetadataIndexOffset = 0;
  719. // Read all the records for this block.
  720. while (true) {
  721. if (Stream.AtEndOfStream())
  722. return reportError("Premature end of bitstream");
  723. uint64_t RecordStartBit = Stream.GetCurrentBitNo();
  724. BitstreamEntry Entry;
  725. if (Error E = Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs)
  726. .moveInto(Entry))
  727. return E;
  728. switch (Entry.Kind) {
  729. case BitstreamEntry::Error:
  730. return reportError("malformed bitcode file");
  731. case BitstreamEntry::EndBlock: {
  732. uint64_t BlockBitEnd = Stream.GetCurrentBitNo();
  733. BlockStats.NumBits += BlockBitEnd - BlockBitStart;
  734. if (DumpRecords) {
  735. O->OS << Indent << "</";
  736. if (BlockName)
  737. O->OS << *BlockName << ">\n";
  738. else
  739. O->OS << "UnknownBlock" << BlockID << ">\n";
  740. }
  741. return Error::success();
  742. }
  743. case BitstreamEntry::SubBlock: {
  744. uint64_t SubBlockBitStart = Stream.GetCurrentBitNo();
  745. if (Error E = parseBlock(Entry.ID, IndentLevel + 1, O, CheckHash))
  746. return E;
  747. ++BlockStats.NumSubBlocks;
  748. uint64_t SubBlockBitEnd = Stream.GetCurrentBitNo();
  749. // Don't include subblock sizes in the size of this block.
  750. BlockBitStart += SubBlockBitEnd - SubBlockBitStart;
  751. continue;
  752. }
  753. case BitstreamEntry::Record:
  754. // The interesting case.
  755. break;
  756. }
  757. if (Entry.ID == bitc::DEFINE_ABBREV) {
  758. if (Error Err = Stream.ReadAbbrevRecord())
  759. return Err;
  760. ++BlockStats.NumAbbrevs;
  761. continue;
  762. }
  763. Record.clear();
  764. ++BlockStats.NumRecords;
  765. StringRef Blob;
  766. uint64_t CurrentRecordPos = Stream.GetCurrentBitNo();
  767. unsigned Code;
  768. if (Error E = Stream.readRecord(Entry.ID, Record, &Blob).moveInto(Code))
  769. return E;
  770. // Increment the # occurrences of this code.
  771. if (BlockStats.CodeFreq.size() <= Code)
  772. BlockStats.CodeFreq.resize(Code + 1);
  773. BlockStats.CodeFreq[Code].NumInstances++;
  774. BlockStats.CodeFreq[Code].TotalBits +=
  775. Stream.GetCurrentBitNo() - RecordStartBit;
  776. if (Entry.ID != bitc::UNABBREV_RECORD) {
  777. BlockStats.CodeFreq[Code].NumAbbrev++;
  778. ++BlockStats.NumAbbreviatedRecords;
  779. }
  780. if (DumpRecords) {
  781. O->OS << Indent << " <";
  782. Optional<const char *> CodeName =
  783. GetCodeName(Code, BlockID, BlockInfo, CurStreamType);
  784. if (CodeName)
  785. O->OS << *CodeName;
  786. else
  787. O->OS << "UnknownCode" << Code;
  788. if (!O->Symbolic && CodeName)
  789. O->OS << " codeid=" << Code;
  790. const BitCodeAbbrev *Abbv = nullptr;
  791. if (Entry.ID != bitc::UNABBREV_RECORD) {
  792. Abbv = Stream.getAbbrev(Entry.ID);
  793. O->OS << " abbrevid=" << Entry.ID;
  794. }
  795. for (unsigned i = 0, e = Record.size(); i != e; ++i)
  796. O->OS << " op" << i << "=" << (int64_t)Record[i];
  797. // If we found a metadata index, let's verify that we had an offset
  798. // before and validate its forward reference offset was correct!
  799. if (BlockID == bitc::METADATA_BLOCK_ID) {
  800. if (Code == bitc::METADATA_INDEX_OFFSET) {
  801. if (Record.size() != 2)
  802. O->OS << "(Invalid record)";
  803. else {
  804. auto Offset = Record[0] + (Record[1] << 32);
  805. MetadataIndexOffset = Stream.GetCurrentBitNo() + Offset;
  806. }
  807. }
  808. if (Code == bitc::METADATA_INDEX) {
  809. O->OS << " (offset ";
  810. if (MetadataIndexOffset == RecordStartBit)
  811. O->OS << "match)";
  812. else
  813. O->OS << "mismatch: " << MetadataIndexOffset << " vs "
  814. << RecordStartBit << ")";
  815. }
  816. }
  817. // If we found a module hash, let's verify that it matches!
  818. if (BlockID == bitc::MODULE_BLOCK_ID && Code == bitc::MODULE_CODE_HASH &&
  819. CheckHash.hasValue()) {
  820. if (Record.size() != 5)
  821. O->OS << " (invalid)";
  822. else {
  823. // Recompute the hash and compare it to the one in the bitcode
  824. SHA1 Hasher;
  825. StringRef Hash;
  826. Hasher.update(*CheckHash);
  827. {
  828. int BlockSize = (CurrentRecordPos / 8) - BlockEntryPos;
  829. auto Ptr = Stream.getPointerToByte(BlockEntryPos, BlockSize);
  830. Hasher.update(ArrayRef<uint8_t>(Ptr, BlockSize));
  831. Hash = Hasher.result();
  832. }
  833. std::array<char, 20> RecordedHash;
  834. int Pos = 0;
  835. for (auto &Val : Record) {
  836. assert(!(Val >> 32) && "Unexpected high bits set");
  837. support::endian::write32be(&RecordedHash[Pos], Val);
  838. Pos += 4;
  839. }
  840. if (Hash == StringRef(RecordedHash.data(), RecordedHash.size()))
  841. O->OS << " (match)";
  842. else
  843. O->OS << " (!mismatch!)";
  844. }
  845. }
  846. O->OS << "/>";
  847. if (Abbv) {
  848. for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) {
  849. const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
  850. if (!Op.isEncoding() || Op.getEncoding() != BitCodeAbbrevOp::Array)
  851. continue;
  852. assert(i + 2 == e && "Array op not second to last");
  853. std::string Str;
  854. bool ArrayIsPrintable = true;
  855. for (unsigned j = i - 1, je = Record.size(); j != je; ++j) {
  856. if (!isPrint(static_cast<unsigned char>(Record[j]))) {
  857. ArrayIsPrintable = false;
  858. break;
  859. }
  860. Str += (char)Record[j];
  861. }
  862. if (ArrayIsPrintable)
  863. O->OS << " record string = '" << Str << "'";
  864. break;
  865. }
  866. }
  867. if (Blob.data()) {
  868. if (canDecodeBlob(Code, BlockID)) {
  869. if (Error E = decodeMetadataStringsBlob(Indent, Record, Blob, O->OS))
  870. return E;
  871. } else {
  872. O->OS << " blob data = ";
  873. if (O->ShowBinaryBlobs) {
  874. O->OS << "'";
  875. O->OS.write_escaped(Blob, /*hex=*/true) << "'";
  876. } else {
  877. bool BlobIsPrintable = true;
  878. for (char C : Blob)
  879. if (!isPrint(static_cast<unsigned char>(C))) {
  880. BlobIsPrintable = false;
  881. break;
  882. }
  883. if (BlobIsPrintable)
  884. O->OS << "'" << Blob << "'";
  885. else
  886. O->OS << "unprintable, " << Blob.size() << " bytes.";
  887. }
  888. }
  889. }
  890. O->OS << "\n";
  891. }
  892. // Make sure that we can skip the current record.
  893. if (Error Err = Stream.JumpToBit(CurrentRecordPos))
  894. return Err;
  895. if (Expected<unsigned> Skipped = Stream.skipRecord(Entry.ID))
  896. ; // Do nothing.
  897. else
  898. return Skipped.takeError();
  899. }
  900. }