Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
4.2 KiB

  1. //===---- llvm/Support/IRReader.h - Reader for LLVM IR files ----*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines functions for reading LLVM IR. They support both
  11. // Bitcode and Assembly, automatically detecting the input format.
  12. //
  13. // These functions must be defined in a header file in order to avoid
  14. // library dependencies, since they reference both Bitcode and Assembly
  15. // functions.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_SUPPORT_IRREADER_H
  19. #define LLVM_SUPPORT_IRREADER_H
  20. #include "llvm/ADT/OwningPtr.h"
  21. #include "llvm/Assembly/Parser.h"
  22. #include "llvm/Bitcode/ReaderWriter.h"
  23. #include "llvm/Support/MemoryBuffer.h"
  24. #include "llvm/Support/SourceMgr.h"
  25. #include "llvm/Support/system_error.h"
  26. namespace llvm {
  27. /// If the given MemoryBuffer holds a bitcode image, return a Module for it
  28. /// which does lazy deserialization of function bodies. Otherwise, attempt to
  29. /// parse it as LLVM Assembly and return a fully populated Module. This
  30. /// function *always* takes ownership of the given MemoryBuffer.
  31. inline Module *getLazyIRModule(MemoryBuffer *Buffer,
  32. SMDiagnostic &Err,
  33. LLVMContext &Context) {
  34. if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
  35. (const unsigned char *)Buffer->getBufferEnd())) {
  36. std::string ErrMsg;
  37. Module *M = getLazyBitcodeModule(Buffer, Context, &ErrMsg);
  38. if (M == 0) {
  39. Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
  40. ErrMsg);
  41. // ParseBitcodeFile does not take ownership of the Buffer in the
  42. // case of an error.
  43. delete Buffer;
  44. }
  45. return M;
  46. }
  47. return ParseAssembly(Buffer, 0, Err, Context);
  48. }
  49. /// If the given file holds a bitcode image, return a Module
  50. /// for it which does lazy deserialization of function bodies. Otherwise,
  51. /// attempt to parse it as LLVM Assembly and return a fully populated
  52. /// Module.
  53. inline Module *getLazyIRFileModule(const std::string &Filename,
  54. SMDiagnostic &Err,
  55. LLVMContext &Context) {
  56. OwningPtr<MemoryBuffer> File;
  57. if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), File)) {
  58. Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
  59. "Could not open input file: " + ec.message());
  60. return 0;
  61. }
  62. return getLazyIRModule(File.take(), Err, Context);
  63. }
  64. /// If the given MemoryBuffer holds a bitcode image, return a Module
  65. /// for it. Otherwise, attempt to parse it as LLVM Assembly and return
  66. /// a Module for it. This function *always* takes ownership of the given
  67. /// MemoryBuffer.
  68. inline Module *ParseIR(MemoryBuffer *Buffer,
  69. SMDiagnostic &Err,
  70. LLVMContext &Context) {
  71. if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
  72. (const unsigned char *)Buffer->getBufferEnd())) {
  73. std::string ErrMsg;
  74. Module *M = ParseBitcodeFile(Buffer, Context, &ErrMsg);
  75. if (M == 0)
  76. Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
  77. ErrMsg);
  78. // ParseBitcodeFile does not take ownership of the Buffer.
  79. delete Buffer;
  80. return M;
  81. }
  82. return ParseAssembly(Buffer, 0, Err, Context);
  83. }
  84. /// If the given file holds a bitcode image, return a Module for it.
  85. /// Otherwise, attempt to parse it as LLVM Assembly and return a Module
  86. /// for it.
  87. inline Module *ParseIRFile(const std::string &Filename,
  88. SMDiagnostic &Err,
  89. LLVMContext &Context) {
  90. OwningPtr<MemoryBuffer> File;
  91. if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), File)) {
  92. Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
  93. "Could not open input file: " + ec.message());
  94. return 0;
  95. }
  96. return ParseIR(File.take(), Err, Context);
  97. }
  98. }
  99. #endif