BitcodeAnalyzer.cpp 34 KB

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