[libc-commits] [PATCH] D71094: [libc] Add implementation of errno and define the other macros of errno.h.

Fangrui Song via Phabricator via libc-commits libc-commits at lists.llvm.org
Fri Dec 6 10:04:24 PST 2019


MaskRay requested changes to this revision.
MaskRay added a comment.
This revision now requires changes to proceed.

For posterity, `#define errno *__errno_location()` breaks C11 7.1.2p5:

> Any definition of an object-like macro described in this clause shall expand to code that is fully protected by parentheses where necessary, so that it groups in an arbitrary expression as if it were a single identifier.

It will fail to diagnose code like `3 errno[0]` (false positive), and incorrectly flag (false negative):

  const char *blah[] = { [EILSEQ] = "asdf" };
  errno = EILSEQ;
  puts(errno[blah]);



================
Comment at: libc/config/linux/api.td:34
+  let Defn = [{
+    int *__errno_location() __attribute__((const));
+    #define errno *__errno_location()
----------------
sivachandra wrote:
> abrachet wrote:
> > I don't have a Windows machine to test this, but from godbolt, `__attribute__`'s seem to be unrecognized by MSVC or at least const was not a known one. I see other libcs conditionally defining (on `__GNUC__` according to @MaskRay) an `__attribute_const` or something like that.
> > 
> > FWIW I don't favor adding this attribute. How useful will this even be really?
> We are adding the attribute in a Linux only file so should not be a problem because Clang and GCC on Linux support on this attribute?
Using unprotected `__attribute__` will break cross compilability. MSVC does not accept it. Or you can say llvm-libc doesn't support MSVC. I'm totally fine with it.

> FWIW I don't favor adding this attribute. How useful will this even be really?

This matters when you run lots of functions that may potentially modify `errno` in a function.

```
foo();
if (errno)
  ...
foo();
if (errno)
  ...
foo();
if (errno)
  ...
```


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D71094/new/

https://reviews.llvm.org/D71094





More information about the libc-commits mailing list