Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way of providing header-filter for clang-tidy in Cmake?

I have projects that sets Clang-tidy configuration as following

set(CMAKE_CXX_CLANG_TIDY "clang-tidy;-checks=google-*,cppcoreguidelines-*;")

However, I have noticed that it was checking all the files that are not even in the current repo like

/opt/ros/melodic/include/ros/console.h

and all the .h/.hpp files of submodules...

I tried to add a regex to filter the target .h files but couldn't get it working... I have given absolute path for a single .hpp file but it was still evaluating /opt/ros/melodic/include files...

Can I have an example on header-filter??

I assume clang-tidy will check the corresponding cpp file if hpp is in the filter. am I correct?

like image 705
Brandon Lee Avatar asked Apr 02 '20 21:04

Brandon Lee


2 Answers

You can look at this example. That's my commit. https://github.com/cocos2d/cocos2d-x/pull/19928

This is how I disabled clang-tidy checks on two directories with regular expressions.

'^((?!/cocos2d-x/external/|/cocos/scripting/).)*$'

It disables clang-tidy checks on external directory and cocos/scripting directory.

I create a python script to test whether the regular expression is working as intended.

#!/usr/bin/env python
import re

files = [ 
"/home/john/cocos2d-x/external/openssl/include/linux/openssl/bio.h",
"/home/john/cocos2d-x/external/tiff/include/linux/tiff.h",
"/home/john/git/cocos/cocos2d-x/cocos/scripting/lua-bindings/auto/lua_cocos2dx_3d_auto.cpp"
"/home/john/cocos2d-x/external/json/stringbuffer.h",
"/home/john/cocos2d-x/cocos/base/ccUtils.h",
"/home/john/git/cocos/cocos2d-x/cocos/scripting/js-bindings/precheader.cpp",
"/home/john/cocos2d-x/cocos/physics/CCPhysicsBody.cpp",
"/home/john/cocos2d-x/tests/cpp-tests/Classes/ActionsEaseTest/ActionsEaseTest.cpp",
"/home/john/cocos2d-x/templates/cpp-template-default/Classes/AppDelegate.cpp",
"/home/john/git/cocos/cocos2d-x/cocos/scripting/js-bindings/proj.android/CMakeLists.txt",
]

pattern = '^((?!/cocos2d-x/external/|/cocos/scripting/).)*$'

for file in files:
    m = re.search(pattern, file)
    if m:
        print m.group(0)

Running this python file and the output is

/home/john/cocos2d-x/cocos/base/ccUtils.h
/home/john/cocos2d-x/cocos/physics/CCPhysicsBody.cpp
/home/john/cocos2d-x/tests/cpp-tests/Classes/ActionsEaseTest/ActionsEaseTest.cpp
/home/john/cocos2d-x/templates/cpp-template-default/Classes/AppDelegate.cpp

You can modify the regular expression and python test script to see if it works.

like image 179
JohnKoch Avatar answered Nov 09 '22 20:11

JohnKoch


The following is a minimal C++ code doing an equivalent of what is going on in clang-tidy in terms of headers filter processing.

#include <llvm/Support/Regex.h>
#include <llvm/ADT/StringRef.h>
#include <iostream>

int main(int argc, char**)
{
  llvm::StringRef re_str = R"(my_fancy_regex_string_goes_here)";
  llvm::Regex re(re_str);
  if(not re.isValid())
  {
    std::cout << "invalid regex" << std::endl;
    return -1;
  }

  llvm::StringRef file_str = "path_to_the_problematic_file";
  if(not re.match(file_str)){
    std::cout << "does not match" << std::endl;
  } else {
    std::cout << "matches" << std::endl;
  }
}

I'm compiling it as follows (MacOS 12.0.1):

clang++  llvm_re_test.cpp  $(llvm-config --cxxflags --ldflags --libs --system-libs)

Once your regex works fine with such minimalistic code it will work fine with clang-tidy.

like image 1
tomaszmi Avatar answered Nov 09 '22 19:11

tomaszmi