Uploaded image for project: 'C Driver'
  1. C Driver
  2. CDRIVER-4163

clang version checks for __builtin_bswap are incorrect

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      Background and Motivation

      The compiler check in bson-endian.h for clang support of __builtin_bswap is as follows:

      #elif defined(__clang__) && defined(__clang_major__) &&  \
         defined(__clang_minor__) && (__clang_major__ >= 3) && \
         (__clang_minor__ >= 1)
      #if __has_builtin(__builtin_bswap16)
      #define BSON_UINT16_SWAP_LE_BE(v) __builtin_bswap16 (v)
      #endif
      

      The intent appears to check that the version of clang is >= 3.1. But this asserts that both major version is >= 3 and minor version is >= 1. This excludes clang versions with a 0 minor version, like 9.0.1.

      Checking the version does not seem necessary. I think it is sufficient to only use __has_builtin for clang. A caveat is that the Clang Language Extensions notes:

      Prior to Clang 10, __has_builtin could not be used to detect most builtin pseudo-functions.

      I believe __builtin_bswap16, _builtin_bswap32, and _builtin_bswap64 _classify as pseudo-functions since they accept an argument. But this test with Compiler Explorer shows that _has_builtin detects support back to clang 3.0.0.

      Impact

      The impact of this bug is that byte swapping may be slower when clang is used to compile.

      Scope
      Update the check for _builtin_bswap support on clang in bson-endian.h to only use _has_builtin.

            Assignee:
            Unassigned Unassigned
            Reporter:
            kevin.albertson@mongodb.com Kevin Albertson
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: