Repository: opencv
Issue: CV_ADD Clang compilation error
Labels: category: build/install

<!--
If you have a question rather than reporting a bug please go to https://forum.opencv.org where you get much faster responses.
If you need further assistance please read [How To Contribute](https://github.com/opencv/opencv/wiki/How_to_contribute).

This is a template helping you to create an issue which can be processed as quickly as possible. This is the bug reporting section for the OpenCV library.
-->

##### System information (version)
<!-- Example
- OpenCV => 4.2
- Operating System / Platform => Windows 64 Bit
- Compiler => Visual Studio 2017
-->

- OpenCV => 4.6.0
- Operating System / Platform => x86-linux cross-compile to Mac
- Compiler => Clang 13.0.1

##### Detailed description

When compiling OpenCV, the following error is generated:

```bash
[21:30:16] [100%] Built target opencv_gapi
[21:31:41] In file included from /workspace/srcdir/build/modules/julia/gen/autogen_cpp/cv_core.cpp:12:
[21:31:41] In file included from /workspace/srcdir/build/modules/julia/gen/autogen_cpp/jlcv.hpp:16:
[21:31:41] In file included from /workspace/srcdir/opencv/modules/core/include/opencv2/core.hpp:58:
[21:31:41] In file included from /workspace/srcdir/opencv/modules/core/include/opencv2/core/mat.hpp:3773:
[21:31:41] /workspace/srcdir/opencv/modules/core/include/opencv2/core/mat.inl.hpp:2116:9: error: address argument to atomic operation must be a pointer to _Atomic type ('std::atomic<int> *' invalid)
[21:31:41]         CV_XADD(&this->hdr->refcount, 1);
[21:31:41]         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[21:31:41] /workspace/srcdir/opencv/modules/core/include/opencv2/core/cvdef.h:670:36: note: expanded from macro 'CV_XADD'
[21:31:41] #      define CV_XADD(addr, delta) __c11_atomic_fetch_add((_Atomic(int)*)(addr), delta, __ATOMIC_ACQ_REL)
[21:31:41]                                    ^                      ~~~~~~~~~~~~~~~~~~~~~
[21:31:41] In file included from /workspace/srcdir/build/modules/julia/gen/autogen_cpp/cv_core.cpp:12:
[21:31:41] In file included from /workspace/srcdir/build/modules/julia/gen/autogen_cpp/jlcv.hpp:16:
[21:31:41] In file included from /workspace/srcdir/opencv/modules/core/include/opencv2/core.hpp:58:
[21:31:41] In file included from /workspace/srcdir/opencv/modules/core/include/opencv2/core/mat.hpp:3773:
[21:31:41] /workspace/srcdir/opencv/modules/core/include/opencv2/core/mat.inl.hpp:2131:21: error: address argument to atomic operation must be a pointer to _Atomic type ('std::atomic<int> *' invalid)
[21:31:41]         if( m.hdr ) CV_XADD(&m.hdr->refcount, 1);
[21:31:41]                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
[21:31:41] /workspace/srcdir/opencv/modules/core/include/opencv2/core/cvdef.h:670:36: note: expanded from macro 'CV_XADD'
[21:31:41] #      define CV_XADD(addr, delta) __c11_atomic_fetch_add((_Atomic(int)*)(addr), delta, __ATOMIC_ACQ_REL)
[21:31:41]                                    ^                      ~~~~~~~~~~~~~~~~~~~~~
[21:31:41] 2 errors generated.
[21:31:41] make[2]: *** [modules/julia/CMakeFiles/opencv_julia.dir/build.make:90: modules/julia/CMakeFiles/opencv_julia.dir/gen/autogen_cpp/cv_core.cpp.o] Error 1
[21:31:41] make[2]: Leaving directory '/workspace/srcdir/build'
[21:31:41] make[1]: *** [CMakeFiles/Makefile2:3097: modules/julia/CMakeFiles/opencv_julia.dir/all] Error 2
[21:31:41] make[1]: Leaving directory '/workspace/srcdir/build'
[21:31:41] make: *** [Makefile:166: all] Error 2
[21:31:41]  ---> make -j${nproc}
[21:31:41]  ---> make -j${nproc}
[21:31:41] Previous command exited with 2
[21:31:41] Child Process exited, exit code 2
```

This error goes away with the following patch: https://github.com/JuliaPackaging/Yggdrasil/blob/c3fe8fa2b85ac49da49a5cd171068e0570598d83/O/OpenCV/bundled/patches/atomic_fix.patch (which in its current formulation likely creates issues for other platforms)

Full build output: https://gist.github.com/jeremiahpslewis/acac3d9029f7a8611232ba1c79283c9f

##### Steps to reproduce

Build script is here: https://github.com/JuliaPackaging/Yggdrasil/blob/c3fe8fa2b85ac49da49a5cd171068e0570598d83/O/OpenCV/build_tarballs.jl

##### Issue submission checklist

 - [x] I report the issue, it's not a question
   <!--
   OpenCV team works with forum.opencv.org, Stack Overflow and other communities
   to discuss problems. Tickets with questions without a real issue statement will be
   closed.
   -->
 - [x] I checked the problem with documentation, FAQ, open issues,
       forum.opencv.org, Stack Overflow, etc and have not found any solution
   <!--
   Places to check:
   * OpenCV documentation: https://docs.opencv.org
   * FAQ page: https://github.com/opencv/opencv/wiki/FAQ
   * OpenCV forum: https://forum.opencv.org
   * OpenCV issue tracker: https://github.com/opencv/opencv/issues?q=is%3Aissue
   * Stack Overflow branch: https://stackoverflow.com/questions/tagged/opencv
   -->
 - [x] I updated to the latest OpenCV version and the issue is still there
   <!--
   master branch for OpenCV 4.x and 3.4 branch for OpenCV 3.x releases.
   OpenCV team supports only the latest release for each branch.
   The ticket is closed if the problem is not reproduced with the modern version.
   -->
 - [x] There is reproducer code and related data files: videos, images, onnx, etc
   <!--
   The best reproducer -- test case for OpenCV that we can add to the library.
   Recommendations for media files and binary files:
   * Try to reproduce the issue with images and videos in opencv_extra repository
     to reduce attachment size
   * Use PNG for images, if you report some CV related bug, but not image reader
     issue
   * Attach the image as an archive to the ticket, if you report some reader issue.
     Image hosting services compress images and it breaks the repro code.
   * Provide ONNX file for some public model or ONNX file with random weights,
     if you report ONNX parsing or handling issue. Architecture details diagram
     from netron tool can be very useful too. See https://lutzroeder.github.io/netron/
   -->