llvm.org GIT mirror llvm / 79002da
Use std::unique_ptr to make the ownership explicit. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@214377 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 6 years ago
25 changed file(s) with 77 addition(s) and 63 deletion(s). Raw diff Collapse all Expand all
125125 /// @brief Create a Binary from Source, autodetecting the file type.
126126 ///
127127 /// @param Source The data to create the Binary from.
128 ErrorOr createBinary(std::unique_ptr Source,
129 LLVMContext *Context = nullptr);
128 ErrorOr>
129 createBinary(std::unique_ptr Source,
130 LLVMContext *Context = nullptr);
130131
131 ErrorOr<Binary *> createBinary(StringRef Path);
132 ErrorOr<std::unique_ptr> createBinary(StringRef Path);
132133 }
133134 }
134135
2626 namespace object {
2727
2828 class ObjectFile;
29 class COFFObjectFile;
30 class MachOObjectFile;
2931
3032 class SymbolRef;
3133 class symbol_iterator;
342344 /// @param ObjectPath The path to the object file. ObjectPath.isObject must
343345 /// return true.
344346 /// @brief Create ObjectFile from path.
345 static ErrorOr createObjectFile(StringRef ObjectPath);
346 static ErrorOr<ObjectFile *>
347 static ErrorOr<std::unique_ptr>
348 createObjectFile(StringRef ObjectPath);
349
350 static ErrorOr>
347351 createObjectFile(std::unique_ptr &Object,
348352 sys::fs::file_magic Type);
349 static ErrorOr<ObjectFile *>
353 static ErrorOr<std::unique_ptr>
350354 createObjectFile(std::unique_ptr &Object) {
351355 return createObjectFile(Object, sys::fs::file_magic::unknown);
352356 }
357361 }
358362
359363 public:
360 static ErrorOr<ObjectFile *>
364 static ErrorOr<std::unique_ptr>
361365 createCOFFObjectFile(std::unique_ptr Object);
362 static ErrorOr
366
367 static ErrorOr>
363368 createELFObjectFile(std::unique_ptr &Object);
364 static ErrorOr
369
370 static ErrorOr>
365371 createMachOObjectFile(std::unique_ptr &Object);
366372 };
367373
141141 }
142142
143143 // construction aux.
144 static ErrorOr<SymbolicFile *>
144 static ErrorOr<std::unique_ptr>
145145 createSymbolicFile(std::unique_ptr &Object,
146146 sys::fs::file_magic Type, LLVMContext *Context);
147147
148 static ErrorOr<SymbolicFile *>
148 static ErrorOr<std::unique_ptr>
149149 createSymbolicFile(std::unique_ptr &Object) {
150150 return createSymbolicFile(Object, sys::fs::file_magic::unknown, nullptr);
151151 }
152 static ErrorOr<SymbolicFile *> createSymbolicFile(StringRef ObjectPath);
152 static ErrorOr<std::unique_ptr>
153 createSymbolicFile(StringRef ObjectPath);
153154
154155 static inline bool classof(const Binary *v) {
155156 return v->isSymbolic();
259259 sys::path::append(AbsolutePath, CompilationDir);
260260 }
261261 sys::path::append(AbsolutePath, DWOFileName);
262 ErrorOr<object::ObjectFile *> DWOFile =
262 ErrorOr<std::unique_ptr> DWOFile =
263263 object::ObjectFile::createObjectFile(AbsolutePath);
264264 if (!DWOFile)
265265 return false;
266266 // Reset DWOHolder.
267 DWO.reset(new DWOHolder(DWOFile.get()));
267 DWO.reset(new DWOHolder(DWOFile.get().get()));
268268 DWARFUnit *DWOCU = DWO->getUnit();
269269 // Verify that compile unit in .dwo file is valid.
270270 if (!DWOCU || DWOCU->getDWOId() != getDWOId()) {
4848 // FIXME: error checking? createObjectFile returns an ErrorOr
4949 // and should probably be checked for failure.
5050 std::unique_ptr Buf(Buffer->getMemBuffer());
51 ObjFile.reset(object::ObjectFile::createObjectFile(Buf).get());
51 ObjFile = std::move(object::ObjectFile::createObjectFile(Buf).get());
5252 }
5353 ObjectImageCommon(std::unique_ptr Input)
5454 : ObjectImage(nullptr), ObjFile(std::move(Input)) {}
3737 return Data->getBufferIdentifier();
3838 }
3939
40 ErrorOr object::createBinary(std::unique_ptr Buffer,
41 LLVMContext *Context) {
40 ErrorOr>
41 object::createBinary(std::unique_ptr Buffer,
42 LLVMContext *Context) {
4243 sys::fs::file_magic Type = sys::fs::identify_magic(Buffer->getBuffer());
4344
4445 switch (Type) {
7374 llvm_unreachable("Unexpected Binary File Type");
7475 }
7576
76 ErrorOr<Binary *> object::createBinary(StringRef Path) {
77 ErrorOr<std::unique_ptr> object::createBinary(StringRef Path) {
7778 ErrorOr> FileOrErr =
7879 MemoryBuffer::getFileOrSTDIN(Path);
7980 if (std::error_code EC = FileOrErr.getError())
11111111 return object_error::success;
11121112 }
11131113
1114 ErrorOr<ObjectFile *>
1114 ErrorOr<std::unique_ptr>
11151115 ObjectFile::createCOFFObjectFile(std::unique_ptr Object) {
11161116 std::error_code EC;
11171117 std::unique_ptr Ret(
11181118 new COFFObjectFile(std::move(Object), EC));
11191119 if (EC)
11201120 return EC;
1121 return Ret.release();
1122 }
1121 return std::move(Ret);
1122 }
1616 namespace llvm {
1717 using namespace object;
1818
19 ErrorOr<ObjectFile *>
19 ErrorOr<std::unique_ptr>
2020 ObjectFile::createELFObjectFile(std::unique_ptr &Obj) {
2121 std::pair Ident =
2222 getElfArchType(Obj->getBuffer());
7979
8080 if (EC)
8181 return EC;
82 return R.release();
82 return std::move(R);
8383 }
8484
8585 } // end namespace llvm
17201720 }
17211721 }
17221722
1723 ErrorOr<ObjectFile *>
1723 ErrorOr<std::unique_ptr>
17241724 ObjectFile::createMachOObjectFile(std::unique_ptr &Buffer) {
17251725 StringRef Magic = Buffer->getBuffer().slice(0, 4);
17261726 std::error_code EC;
17381738
17391739 if (EC)
17401740 return EC;
1741 return Ret.release();
1742 }
1743
1741 return std::move(Ret);
1742 }
1743
5959 // ObjectFile creation
6060 LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf) {
6161 std::unique_ptr Buf(unwrap(MemBuf));
62 ErrorOr<ObjectFile *> ObjOrErr(ObjectFile::createObjectFile(Buf));
62 ErrorOr<std::unique_ptr> ObjOrErr(
63 ObjectFile::createObjectFile(Buf));
6364 Buf.release();
64 ObjectFile *Obj = ObjOrErr ? ObjOrErr.get() : nullptr;
65 ObjectFile *Obj = ObjOrErr ? ObjOrErr.get().release() : nullptr;
6566 return wrap(Obj);
6667 }
6768
1010 //
1111 //===----------------------------------------------------------------------===//
1212
13 #include "llvm/Object/COFF.h"
14 #include "llvm/Object/MachO.h"
1315 #include "llvm/Object/ObjectFile.h"
1416 #include "llvm/Support/ErrorHandling.h"
1517 #include "llvm/Support/FileSystem.h"
4446 return section_iterator(SectionRef(Sec, this));
4547 }
4648
47 ErrorOr<ObjectFile *>
49 ErrorOr<std::unique_ptr>
4850 ObjectFile::createObjectFile(std::unique_ptr &Object,
4951 sys::fs::file_magic Type) {
5052 if (Type == sys::fs::file_magic::unknown)
8183 llvm_unreachable("Unexpected Object File Type");
8284 }
8385
84 ErrorOr<ObjectFile *> ObjectFile::createObjectFile(StringRef ObjectPath) {
86 ErrorOr<std::unique_ptr>
87 ObjectFile::createObjectFile(StringRef ObjectPath) {
8588 ErrorOr> FileOrErr =
8689 MemoryBuffer::getFile(ObjectPath);
8790 if (std::error_code EC = FileOrErr.getError())
2424
2525 SymbolicFile::~SymbolicFile() {}
2626
27 ErrorOr<SymbolicFile *>
27 ErrorOr<std::unique_ptr>
2828 SymbolicFile::createSymbolicFile(std::unique_ptr &Object,
2929 sys::fs::file_magic Type,
3030 LLVMContext *Context) {
281281 if (!File)
282282 error(File.getError());
283283 else
284 Object.reset(File.get());
284 Object = std::move(File.get());
285285 }
286286
287287 ObjectFileCoverageMappingReader::ObjectFileCoverageMappingReader(
291291 if (!File)
292292 error(File.getError());
293293 else
294 Object.reset(File.get());
294 Object = std::move(File.get());
295295 }
296296
297297 namespace {
527527 }
528528
529529 for (unsigned i = 0, e = ExtraObjects.size(); i != e; ++i) {
530 ErrorOr<object::ObjectFile *> Obj =
530 ErrorOr<std::unique_ptr> Obj =
531531 object::ObjectFile::createObjectFile(ExtraObjects[i]);
532532 if (!Obj) {
533533 Err.print(argv[0], errs());
534534 return 1;
535535 }
536 EE->addObjectFile(std::unique_ptr(Obj.get()));
536 EE->addObjectFile(std::move(Obj.get()));
537537 }
538538
539539 for (unsigned i = 0, e = ExtraArchives.size(); i != e; ++i) {
697697 E = Members.end();
698698 I != E; ++I, ++MemberNum) {
699699 std::unique_ptr &MemberBuffer = Buffers[MemberNum];
700 ErrorOr<object::SymbolicFile *> ObjOrErr =
700 ErrorOr<std::unique_ptr> ObjOrErr =
701701 object::SymbolicFile::createSymbolicFile(
702702 MemberBuffer, sys::fs::file_magic::unknown, &Context);
703703 if (!ObjOrErr)
704704 continue; // FIXME: check only for "not an object file" errors.
705 std::unique_ptr Obj(ObjOrErr.get());
705 std::unique_ptr Obj = std::move(ObjOrErr.get());
706706
707707 if (!StartOffset) {
708708 printMemberHeader(Out, "", sys::TimeValue::now(), 0, 0, 0, 0);
7373 return;
7474 }
7575
76 ErrorOr<ObjectFile *> ObjOrErr(ObjectFile::createObjectFile(Buff.get()));
76 ErrorOr<std::unique_ptr> ObjOrErr =
77 ObjectFile::createObjectFile(Buff.get());
7778 if (std::error_code EC = ObjOrErr.getError()) {
7879 errs() << Filename << ": " << EC.message() << '\n';
7980 return;
8081 }
81 std::unique_ptr Obj(ObjOrErr.get());
82 std::unique_ptr Obj = std::move(ObjOrErr.get());
8283
8384 std::unique_ptr DICtx(DIContext::getDWARFContext(Obj.get()));
8485
10081008 return;
10091009
10101010 LLVMContext &Context = getGlobalContext();
1011 ErrorOr<Binary *> BinaryOrErr =
1011 ErrorOr<std::unique_ptr> BinaryOrErr =
10121012 createBinary(std::move(*BufferOrErr), &Context);
10131013 if (error(BinaryOrErr.getError(), Filename))
10141014 return;
1015 std::unique_ptr Bin(BinaryOrErr.get());
1015 std::unique_ptr Bin = std::move(BinaryOrErr.get());
10161016
10171017 if (Archive *A = dyn_cast(Bin.get())) {
10181018 if (ArchiveMap) {
201201 return;
202202 }
203203
204 std::unique_ptr MachOOF(static_cast(
205 ObjectFile::createMachOObjectFile(Buff.get()).get()));
204 std::unique_ptr MachOOF =
205 std::move(ObjectFile::createMachOObjectFile(Buff.get()).get());
206206
207207 DisassembleInputMachO2(Filename, MachOOF.get());
208208 }
293293 errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n';
294294 return;
295295 }
296 DbgObj = ObjectFile::createMachOObjectFile(Buf.get()).get();
296 DbgObj = ObjectFile::createMachOObjectFile(Buf.get()).get().release();
297297 }
298298
299299 // Setup the DIContext
883883 }
884884
885885 // Attempt to open the binary.
886 ErrorOr<Binary *> BinaryOrErr = createBinary(file);
886 ErrorOr<std::unique_ptr> BinaryOrErr = createBinary(file);
887887 if (std::error_code EC = BinaryOrErr.getError()) {
888888 errs() << ToolName << ": '" << file << "': " << EC.message() << ".\n";
889889 return;
890890 }
891 std::unique_ptr binary(BinaryOrErr.get());
891 std::unique_ptr binary = std::move(BinaryOrErr.get());
892892
893893 if (Archive *a = dyn_cast(binary.get()))
894894 DumpArchive(a);
286286 }
287287
288288 // Attempt to open the binary.
289 ErrorOr<Binary *> BinaryOrErr = createBinary(File);
289 ErrorOr<std::unique_ptr> BinaryOrErr = createBinary(File);
290290 if (std::error_code EC = BinaryOrErr.getError()) {
291291 reportError(File, EC);
292292 return;
293293 }
294 std::unique_ptr Binary(BinaryOrErr.get());
294 std::unique_ptr Binary = std::move(BinaryOrErr.get());
295295
296296 if (Archive *Arc = dyn_cast(Binary.get()))
297297 dumpArchive(Arc);
452452 }
453453
454454 // Attempt to open the binary.
455 ErrorOr<Binary *> BinaryOrErr = createBinary(file);
455 ErrorOr<std::unique_ptr> BinaryOrErr = createBinary(file);
456456 if (std::error_code EC = BinaryOrErr.getError()) {
457457 errs() << ToolName << ": " << file << ": " << EC.message() << ".\n";
458458 return;
459459 }
460 std::unique_ptr binary(BinaryOrErr.get());
460 std::unique_ptr binary = std::move(BinaryOrErr.get());
461461
462462 if (Archive *a = dyn_cast(binary.get())) {
463463 // This is an archive. Iterate over each member and display its sizes.
299299 return I->second;
300300 Binary *Bin = nullptr;
301301 Binary *DbgBin = nullptr;
302 ErrorOr<Binary *> BinaryOrErr = createBinary(Path);
302 ErrorOr<std::unique_ptr> BinaryOrErr = createBinary(Path);
303303 if (!error(BinaryOrErr.getError())) {
304 std::unique_ptr ParsedBinary(BinaryOrErr.get());
304 std::unique_ptr ParsedBinary = std::move(BinaryOrErr.get());
305305 // Check if it's a universal binary.
306306 Bin = ParsedBinary.get();
307307 ParsedBinariesAndObjects.push_back(std::move(ParsedBinary));
313313 BinaryOrErr = createBinary(ResourcePath);
314314 std::error_code EC = BinaryOrErr.getError();
315315 if (EC != errc::no_such_file_or_directory && !error(EC)) {
316 DbgBin = BinaryOrErr.get();
317 ParsedBinariesAndObjects.push_back(std::unique_ptr(DbgBin));
316 DbgBin = BinaryOrErr.get().get();
317 ParsedBinariesAndObjects.push_back(std::move(BinaryOrErr.get()));
318318 }
319319 }
320320 // Try to locate the debug binary using .gnu_debuglink section.
326326 findDebugBinary(Path, DebuglinkName, CRCHash, DebugBinaryPath)) {
327327 BinaryOrErr = createBinary(DebugBinaryPath);
328328 if (!error(BinaryOrErr.getError())) {
329 DbgBin = BinaryOrErr.get();
330 ParsedBinariesAndObjects.push_back(std::unique_ptr(DbgBin));
329 DbgBin = BinaryOrErr.get().get();
330 ParsedBinariesAndObjects.push_back(std::move(BinaryOrErr.get()));
331331 }
332332 }
333333 }
163163 }
164164
165165 // Attempt to open the binary.
166 ErrorOr<Binary *> BinaryOrErr = createBinary(File);
166 ErrorOr<std::unique_ptr> BinaryOrErr = createBinary(File);
167167 if (std::error_code EC = BinaryOrErr.getError()) {
168168 reportError(File, EC);
169169 return;
170170 }
171 std::unique_ptr Binary(BinaryOrErr.get());
171 std::unique_ptr Binary = std::move(BinaryOrErr.get());
172172
173173 if (Archive *Arc = dyn_cast(Binary.get()))
174174 dumpArchive(Arc);
402402
403403 cl::ParseCommandLineOptions(argc, argv, "llvm Mach-O dumping tool\n");
404404
405 ErrorOr<Binary *> BinaryOrErr = createBinary(InputFile);
405 ErrorOr<std::unique_ptr> BinaryOrErr = createBinary(InputFile);
406406 if (std::error_code EC = BinaryOrErr.getError())
407407 return Error("unable to read input: '" + EC.message() + "'");
408 std::unique_ptr Binary(BinaryOrErr.get());
408 std::unique_ptr Binary = std::move((BinaryOrErr.get()));
409409
410410 const MachOObjectFile *InputObject = dyn_cast(Binary.get());
411411 if (!InputObject)
3131 if (File != "-" && !sys::fs::exists(File))
3232 return obj2yaml_error::file_not_found;
3333
34 ErrorOr<Binary *> BinaryOrErr = createBinary(File);
34 ErrorOr<std::unique_ptr> BinaryOrErr = createBinary(File);
3535 if (std::error_code EC = BinaryOrErr.getError())
3636 return EC;
3737
38 std::unique_ptr Binary(BinaryOrErr.get());
38 std::unique_ptr Binary = std::move(BinaryOrErr.get());
3939 // TODO: If this is an archive, then burst it and dump each entry
4040 if (ObjectFile *Obj = dyn_cast(Binary.get()))
4141 return dumpObject(*Obj);