//===- lib/ReaderWriter/PECOFF/PDBPass.h ----------------------------------===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef LLD_READER_WRITER_PE_COFF_PDB_PASS_H
#define LLD_READER_WRITER_PE_COFF_PDB_PASS_H

#include "lld/Core/Pass.h"
#include "llvm/ADT/StringRef.h"

#if !defined(_MSC_VER) && !defined(__MINGW32__)
#include <unistd.h>
#else
#include <io.h>
#endif

namespace lld {
namespace pecoff {

class PDBPass : public lld::Pass {
public:
  PDBPass(PECOFFLinkingContext &ctx) : _ctx(ctx) {}

  void perform(std::unique_ptr<MutableFile> &file) override {
    if (_ctx.getDebug())
      touch(_ctx.getPDBFilePath());
  }

private:
  void touch(StringRef path) {
    int fd;
    if (llvm::sys::fs::openFileForWrite(path, fd, llvm::sys::fs::F_Append))
      llvm::report_fatal_error("failed to create a PDB file");
    ::close(fd);
  }

  PECOFFLinkingContext &_ctx;
};

} // namespace pecoff
} // namespace lld

#endif

//===- lib/ReaderWriter/PECOFF/PDBPass.h ----------------------------------===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef LLD_READER_WRITER_PE_COFF_PDB_PASS_H
#define LLD_READER_WRITER_PE_COFF_PDB_PASS_H

#include "lld/Core/Pass.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Process.h"

namespace lld {
namespace pecoff {

class PDBPass : public lld::Pass {
public:
  PDBPass(PECOFFLinkingContext &ctx) : _ctx(ctx) {}

  void perform(std::unique_ptr<MutableFile> &file) override {
    if (_ctx.getDebug())
      touch(_ctx.getPDBFilePath());
  }

private:
  void touch(StringRef path) {
    int fd;
    if (llvm::sys::fs::openFileForWrite(path, fd, llvm::sys::fs::F_Append))
      llvm::report_fatal_error("failed to create a PDB file");
    llvm::sys::Process::SafelyCloseFileDescriptor(fd);
  }

  PECOFFLinkingContext &_ctx;
};

} // namespace pecoff
} // namespace lld

#endif

Use SafelyCloseFileDescriptor instead of close.
Use SafelyCloseFileDescriptor instead of close.

Opening a file using openFileForWrite and closing it using close
was asymmetric. It also had a subtle portability problem (details are
described in the commit message for r219189).

git-svn-id: f6089bf0e6284f307027cef4f64114ee9ebb0424@222802 91177308-0d34-0410-b5e6-96231b3b80d8
