[llvm-dev] Dealing with llvm::errs() and friends in dynamic libs on Windows

Janus Lynggaard Thorborg via llvm-dev llvm-dev at lists.llvm.org
Sun Sep 9 09:30:30 PDT 2018


Hello,

outs() and errs() are routinely used around llvm and cannot really be
disabled. It would seem that in certain types of Windows applications,
stderr and stdout are not opened by default, which is an assumption of llvm.

This leaves errs() and outs() with an error code, because writing calls
will fail due to invalid fd handles. Thus, when an llvm lib is unloaded, it
will terminate the entire process due to a non-zero EC in ~raw_fd_ostream()
that calls report_fatal_error().

It doesn't help that outs(), errs() etc. are not returning raw_fd_ostream,
so you cannot clear the error codes manually (without dynamic_cast):

struct LLVMCleanup
{
    ~LLVMCleanup()
    {
        for (auto stream : {&llvm::errs(), &llvm::outs()})
        {
            if (auto fdStream = dynamic_cast<llvm::raw_fd_ostream*>(stream))
                fdStream->clear_error();
        }
    }
};

Above is my currently working fix (if I sprinkle it in enough places) but
it is a pretty fragile solution. Is there some special initialization or
handling that I'm missing? My program is itself a dynamic lib, so I don't
control state of stderr or stdout. I would rather not terminate my host
process because diagnostic prints failed.

Thanks - and regards,
Janus
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180909/998c39b1/attachment.html>


More information about the llvm-dev mailing list