[PATCH] D88518: Recognize setjmp and friends as builtins even if jmp_buf is not declared yet.

Martin Storsjö via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 2 01:48:35 PDT 2020


mstorsjo added a comment.

This broke use of setjmp for mingw on x86_64. IIRC, in MSVC environments, the `_setjmp` function is considered built-in, which is given an implicit second argument by the compiler. In mingw targets on the other hand, the compiler normally doesn't know of any such extra mechanisms, and it's hooked up e.g. like this:

  #define setjmp(BUF) _setjmp((BUF), __builtin_frame_address (0))
  int __cdecl __attribute__ ((__nothrow__,__returns_twice__)) _setjmp(jmp_buf _Buf, void *_Ctx);

After this commit, in C++, this errors out with "error: too many arguments to function call, expected 1, have 2", while in C it merely produces a warning "warning: incompatible redeclaration of library function '_setjmp'" (and that warning is produced by the SDK headers, where those warnings normally are silenced).

A full repro case here follows:

  typedef __attribute__ ((__aligned__ (16))) struct _SETJMP_FLOAT128 {
    __extension__ unsigned long long Part[2];
  } SETJMP_FLOAT128;
  #define _JBLEN 16
  typedef SETJMP_FLOAT128 _JBTYPE;
  typedef _JBTYPE jmp_buf[_JBLEN];
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  #define setjmp(BUF) _setjmp((BUF), __builtin_frame_address (0))
  int __cdecl __attribute__ ((__nothrow__,__returns_twice__)) _setjmp(jmp_buf _Buf, void *_Ctx);
  
  #ifdef __cplusplus
  }
  #endif
  
  void other(void);
  jmp_buf buf;
  int func(void) {
      if (setjmp(buf)) {
          return 1;
      }
      other();
      return 0;
  }

  $ clang++ -x c++ -target x86_64-w64-mingw32 -c setjmp.c
  setjmp.c:29:9: error: too many arguments to function call, expected 1, have 2
      if (setjmp(buf)) {
          ^~~~~~~~~~~
  setjmp.c:12:36: note: expanded from macro 'setjmp'
  #define setjmp(BUF) _setjmp((BUF), __builtin_frame_address (0))
                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~

(The exact functions that is used for setjmp in mingw configuration is a bit of a jungle, with different CRT setups on different architectures: https://github.com/mingw-w64/mingw-w64/blob/master/mingw-w64-headers/crt/setjmp.h#L213-L243)


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D88518



More information about the cfe-commits mailing list