llvm.org GIT mirror llvm / 81e4992
Return a std::unique_ptr from the IRReader.h functions. NFC. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@216466 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 5 years ago
10 changed file(s) with 51 addition(s) and 55 deletion(s). Raw diff Collapse all Expand all
1414 #ifndef LLVM_IRREADER_IRREADER_H
1515 #define LLVM_IRREADER_IRREADER_H
1616
17 #include "llvm/ADT/StringRef.h"
18 #include
1719 #include
1820
1921 namespace llvm {
2729 /// for it which does lazy deserialization of function bodies. Otherwise,
2830 /// attempt to parse it as LLVM Assembly and return a fully populated
2931 /// Module.
30 Module *getLazyIRFileModule(const std::string &Filename, SMDiagnostic &Err,
31 LLVMContext &Context);
32 std::unique_ptr getLazyIRFileModule(StringRef Filename,
33 SMDiagnostic &Err,
34 LLVMContext &Context);
3235
3336 /// If the given MemoryBuffer holds a bitcode image, return a Module
3437 /// for it. Otherwise, attempt to parse it as LLVM Assembly and return
3538 /// a Module for it. This function *never* takes ownership of Buffer.
36 Module *ParseIR(MemoryBuffer *Buffer, SMDiagnostic &Err, LLVMContext &Context);
39 std::unique_ptr parseIR(MemoryBuffer *Buffer, SMDiagnostic &Err,
40 LLVMContext &Context);
3741
3842 /// If the given file holds a bitcode image, return a Module for it.
3943 /// Otherwise, attempt to parse it as LLVM Assembly and return a Module
4044 /// for it.
41 Module *ParseIRFile(const std::string &Filename, SMDiagnostic &Err,
42 LLVMContext &Context);
43
45 std::unique_ptr parseIRFile(StringRef Filename, SMDiagnostic &Err,
46 LLVMContext &Context);
4447 }
4548
4649 #endif
4848 return parseAssembly(std::move(Buffer), Err, Context);
4949 }
5050
51 Module *llvm::getLazyIRFileModule(const std::string &Filename,
52 SMDiagnostic &Err, LLVMContext &Context) {
51 std::unique_ptr llvm::getLazyIRFileModule(StringRef Filename,
52 SMDiagnostic &Err,
53 LLVMContext &Context) {
5354 ErrorOr> FileOrErr =
5455 MemoryBuffer::getFileOrSTDIN(Filename);
5556 if (std::error_code EC = FileOrErr.getError()) {
5859 return nullptr;
5960 }
6061
61 return getLazyIRModule(std::move(FileOrErr.get()), Err, Context).release();
62 return getLazyIRModule(std::move(FileOrErr.get()), Err, Context);
6263 }
6364
64 Module *llvm::ParseIR(MemoryBuffer *Buffer, SMDiagnostic &Err,
65 LLVMContext &Context) {
65 std::unique_ptr llvm::parseIR(MemoryBuffer *Buffer, SMDiagnostic &Err,
66 LLVMContext &Context) {
6667 NamedRegionTimer T(TimeIRParsingName, TimeIRParsingGroupName,
6768 TimePassesIsEnabled);
6869 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
6970 (const unsigned char *)Buffer->getBufferEnd())) {
7071 ErrorOr ModuleOrErr = parseBitcodeFile(Buffer, Context);
71 Module *M = nullptr;
72 if (std::error_code EC = ModuleOrErr.getError())
72 if (std::error_code EC = ModuleOrErr.getError()) {
7373 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
7474 EC.message());
75 else
76 M = ModuleOrErr.get();
77 // parseBitcodeFile does not take ownership of the Buffer.
78 return M;
75 return nullptr;
76 }
77 return std::unique_ptr(ModuleOrErr.get());
7978 }
8079
8180 return parseAssembly(std::unique_ptr(MemoryBuffer::getMemBuffer(
8281 Buffer->getBuffer(), Buffer->getBufferIdentifier())),
83 Err, Context).release();
82 Err, Context);
8483 }
8584
86 Module *llvm::ParseIRFile(const std::string &Filename, SMDiagnostic &Err,
87 LLVMContext &Context) {
85 std::unique_ptr llvm::parseIRFile(StringRef Filename, SMDiagnostic &Err,
86 LLVMContext &Context) {
8887 ErrorOr> FileOrErr =
8988 MemoryBuffer::getFileOrSTDIN(Filename);
9089 if (std::error_code EC = FileOrErr.getError()) {
9392 return nullptr;
9493 }
9594
96 return ParseIR(FileOrErr.get().get(), Err, Context);
95 return parseIR(FileOrErr.get().get(), Err, Context);
9796 }
9897
9998 //===----------------------------------------------------------------------===//
106105 SMDiagnostic Diag;
107106
108107 std::unique_ptr MB(unwrap(MemBuf));
109 *OutM = wrap(ParseIR(MB.get(), Diag, *unwrap(ContextRef)));
108 *OutM = wrap(parseIR(MB.get(), Diag, *unwrap(ContextRef)).release());
110109
111110 if(!*OutM) {
112111 if (OutMessage) {
8484 std::unique_ptr llvm::parseInputFile(StringRef Filename,
8585 LLVMContext &Ctxt) {
8686 SMDiagnostic Err;
87 std::unique_ptr Result (ParseIRFile(Filename, Err, Ctxt));
87 std::unique_ptr Result = parseIRFile(Filename, Err, Ctxt);
8888 if (!Result)
8989 Err.print("bugpoint", errs());
9090
230230
231231 // If user just wants to list available options, skip module loading
232232 if (!SkipModule) {
233 M.reset(ParseIRFile(InputFilename, Err, Context));
233 M = parseIRFile(InputFilename, Err, Context);
234234 mod = M.get();
235235 if (mod == nullptr) {
236236 Err.print(argv[0], errs());
398398
399399 // Load the bitcode...
400400 SMDiagnostic Err;
401 std::unique_ptr Owner(ParseIRFile(InputFile, Err, Context));
401 std::unique_ptr Owner = parseIRFile(InputFile, Err, Context);
402402 Module *Mod = Owner.get();
403403 if (!Mod) {
404404 Err.print(argv[0], errs());
512512
513513 // Load any additional modules specified on the command line.
514514 for (unsigned i = 0, e = ExtraModules.size(); i != e; ++i) {
515 std::unique_ptr XMod(ParseIRFile(ExtraModules[i], Err, Context));
515 std::unique_ptr XMod = parseIRFile(ExtraModules[i], Err, Context);
516516 if (!XMod) {
517517 Err.print(argv[0], errs());
518518 return 1;
3131
3232 /// Reads a module from a file. On error, messages are written to stderr
3333 /// and null is returned.
34 static Module *ReadModule(LLVMContext &Context, StringRef Name) {
34 static std::unique_ptr readModule(LLVMContext &Context,
35 StringRef Name) {
3536 SMDiagnostic Diag;
36 Module *M = ParseIRFile(Name, Diag, Context);
37 std::unique_ptr M = parseIRFile(Name, Diag, Context);
3738 if (!M)
3839 Diag.print("llvm-diff", errs());
3940 return M;
4041 }
4142
42 static void diffGlobal(DifferenceEngine &Engine, Module *L, Module *R,
43 static void diffGlobal(DifferenceEngine &Engine, Module &L, Module &R,
4344 StringRef Name) {
4445 // Drop leading sigils from the global name.
4546 if (Name.startswith("@")) Name = Name.substr(1);
4647
47 Function *LFn = L->getFunction(Name);
48 Function *RFn = R->getFunction(Name);
48 Function *LFn = L.getFunction(Name);
49 Function *RFn = R.getFunction(Name);
4950 if (LFn && RFn)
5051 Engine.diff(LFn, RFn);
5152 else if (!LFn && !RFn)
7172 LLVMContext Context;
7273
7374 // Load both modules. Die if that fails.
74 Module *LModule = ReadModule(Context, LeftFilename);
75 Module *RModule = ReadModule(Context, RightFilename);
75 std::unique_ptr LModule = readModule(Context, LeftFilename);
76 std::unique_ptr RModule = readModule(Context, RightFilename);
7677 if (!LModule || !RModule) return 1;
7778
7879 DiffConsumer Consumer;
8182 // If any global names were given, just diff those.
8283 if (!GlobalsToCompare.empty()) {
8384 for (unsigned I = 0, E = GlobalsToCompare.size(); I != E; ++I)
84 diffGlobal(Engine, LModule, RModule, GlobalsToCompare[I]);
85 diffGlobal(Engine, *LModule, *RModule, GlobalsToCompare[I]);
8586
8687 // Otherwise, diff everything in the module.
8788 } else {
88 Engine.diff(LModule, RModule);
89 Engine.diff(LModule.get(), RModule.get());
8990 }
90
91 delete LModule;
92 delete RModule;
9391
9492 return Consumer.hadDifferences();
9593 }
100100
101101 // Use lazy loading, since we only care about selected global values.
102102 SMDiagnostic Err;
103 std::unique_ptr M;
104 M.reset(getLazyIRFileModule(InputFilename, Err, Context));
103 std::unique_ptr M = getLazyIRFileModule(InputFilename, Err, Context);
105104
106105 if (!M.get()) {
107106 Err.print(argv[0], errs());
5454 SuppressWarnings("suppress-warnings", cl::desc("Suppress all linking warnings"),
5555 cl::init(false));
5656
57 // LoadFile - Read the specified bitcode file in and return it. This routine
58 // searches the link path for the specified file to try to find it...
57 // Read the specified bitcode file in and return it. This routine searches the
58 // link path for the specified file to try to find it...
5959 //
60 static inline Module *LoadFile(const char *argv0, const std::string &FN,
61 LLVMContext& Context) {
60 static std::unique_ptr
61 loadFile(const char *argv0, const std::string &FN, LLVMContext &Context) {
6262 SMDiagnostic Err;
6363 if (Verbose) errs() << "Loading '" << FN << "'\n";
64 Module* Result = nullptr;
65
66 Result = ParseIRFile(FN, Err, Context);
67 if (Result) return Result; // Load successful!
64 std::unique_ptr Result = parseIRFile(FN, Err, Context);
65 if (Result)
66 return Result;
6867
6968 Err.print(argv0, errs());
7069 return nullptr;
8281 unsigned BaseArg = 0;
8382 std::string ErrorMessage;
8483
85 std::unique_ptr Composite(
86 LoadFile(argv[0], InputFilenames[BaseArg], Context));
84 std::unique_ptr Composite =
85 loadFile(argv[0], InputFilenames[BaseArg], Context);
8786 if (!Composite.get()) {
8887 errs() << argv[0] << ": error loading file '"
8988 << InputFilenames[BaseArg] << "'\n";
9291
9392 Linker L(Composite.get(), SuppressWarnings);
9493 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
95 std::unique_ptr M(LoadFile(argv[0], InputFilenames[i], Context));
94 std::unique_ptr M = loadFile(argv[0], InputFilenames[i], Context);
9695 if (!M.get()) {
9796 errs() << argv[0] << ": error loading file '" <
9897 return 1;
361361 SMDiagnostic Err;
362362
363363 // Load the input module...
364 std::unique_ptr M;
365 M.reset(ParseIRFile(InputFilename, Err, Context));
364 std::unique_ptr M = parseIRFile(InputFilename, Err, Context);
366365
367366 if (!M.get()) {
368367 Err.print(argv[0], errs());
519519 SMDiagnostic Err;
520520
521521 // Load the input module...
522 std::unique_ptr M;
523 M.reset(ParseIRFile(InputFilename, Err, Context));
522 std::unique_ptr M = parseIRFile(InputFilename, Err, Context);
524523
525524 if (!M.get()) {
526525 Err.print(argv[0], errs());