title: How to integrate clang-tidy to CMake and GCC?
body: <p>I want to integrate clang-tidy to our C and C++, CMake based project which is compiled using a custom GCC toolchain.</p>
  
  <p>I've tried following <a href="https://ortogonal.github.io/cmake-clang-tidy" rel="noreferrer">this tutorial</a>, setting <code>CMAKE_CXX_CLANG_TIDY</code>. I've also tried generating a compilation database by setting <code>CMAKE_EXPORT_COMPILE_COMMANDS</code> to <code>ON</code> and pointing <code>run-clang-tidy.py</code> to its directory.</p>
  
  <p>In both cases, I've encountered (the same) few errors that are probably related to differences between Clang and GCC:</p>
  
  <ol>
  <li>Some warning flags that are enabled in the CMake files are not supported in Clang but are supported in GCC (like <code>-Wlogical-op</code>). As the compiler is GCC, the file builds correctly, and the flag is written to the compilation database, but <code>clang-tidy</code> complains about it.</li>
  <li>clang-tidy complains some defines and functions are unavailable, even though the code compiles just fine. As an example, the <a href="https://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-open.html" rel="noreferrer">android-cloexec-open check</a> suggested using <code>O_CLOEXEC</code> to improve security and force the closing of files, but trying to use this define leads to an undefined identifier error (even though our GCC compiles the code). 
  As an example to a function that is not found, there is <code>clock_gettime</code>.</li>
  </ol>
  
  <p>Our code compiles with the C11 standard and C++14 standard, without GNU extensions:</p>
  
  <pre><code>set(CMAKE_C_STANDARD 11)
  set(CMAKE_CXX_STANDARD 14)
  
  set(CMAKE_C_EXTENSIONS OFF)
  set(CMAKE_CXX_EXTENSIONS OFF)
  </code></pre>
  
  <p>The custom toolchain is a cross-compilation toolchain which runs on Linux and compiles to FreeBSD.</p>
  
  <ol>
  <li>Is there a way to disable the passing of some flags by CMake to clang-tidy? Am I using clang-tidy wrong?</li>
  <li>I suspect this issue is related to disabling GNU extensions, using a cross-compilation toolchain, and some feature-test-macro which is not defined by default in Clang but is defined with GCC (e.g. <code>_GNU_SOURCE</code>/<code>_POSIX_SOURCE</code>). If this is the case, how can I check it? If not, should I use clang-tidy differently?</li>
  </ol>
  
  <p><strong>EDIT</strong></p>
  
  <p>As @pablo285 asked, here are 2 warnings I get for a single file, and then as I added <code>--warnings-as-errors=*</code>, the build stops:</p>
  
  <pre><code>error: unknown warning option '-Wlogical-op' ; did you mean '-Wlong-long'? [clang-diagnostic-error]
  
  &lt;file path&gt;: error: use of undeclared identifier 'O_CLOEXEC' [clang-diagnostic-error]
  O_WRONLY | O_CLOEXEC
             ^
  </code></pre>
  
  <p>I decided to write a python script that will replace clang-tidy, receive the commandline from CMake and edit it to fix various errors. Here are the modification to the commandline I tried:</p>
  
  <ol>
  <li>Remove none clang compile flags
  
  <ul>
  <li>This helps with things like the first warning, as now I don't pass flags that clang doesn't know. It seems like I can't configure CMake to pass different set of flags to GCC and to clang-tidy, so if anyone is familiar with some solution to this problem, I'll be happy to hear!</li>
  </ul></li>
  <li>I changed the include directories that are passed to clang-tidy
  
  <ul>
  <li>As mentioned in the post, I use a custom toolchain (which cross-compiles). I used <a href="https://stackoverflow.com/questions/4980819/what-are-the-gcc-default-include-directories">this post</a> and Python to extract the list of standard include directories, and added them to the flag list as a list of <code>-isystem &lt;dir&gt;</code>. I also added <code>-nostdinc</code> so that clang-tidy won't try to look on his own headers instead of mine
  
  <ul>
  <li>This helped with the issue above, as now various defines such as <code>O_CLOEXEC</code> is defined in the toolchain's headers, but as my toolchain is based on GCC, clang couldn't parse the <code>&lt;type_traits&gt;</code> header which includes calls to many compiler intrinsics</li>
  </ul></li>
  <li>I'm not sure what's the best approach in this case</li>
  </ul></li>
  </ol>
  
  <p>@shycha: Thanks for the tip, I'll try disabling this specific check and I'll edit this post again</p>