1

I would like to make clang-tidy ignore preprocessor directives when checking through my code. I have code that looks like the following...

#ifdef NO_DEFINE
class wrongClass2 {
protected:
  int m_M;
public:
  wrongClass2(int M = 1): m_M(M) {};
  int wrong_setter{};
};
#endif

With the following .clang-tidy format file...

Checks: >
  -*,
  readability-identifier-naming,
  google-*

# Turn all the warnings from the checks above into errors.
WarningsAsErrors: "*"

CheckOptions:
  - { key: readability-identifier-naming.ClassCase,             value: CamelCase  }
  - { key: readability-identifier-naming.MethodCase,            value: CamelCase  }
  - { key: readability-identifier-naming.MemberPrefix,          value: m_         }
  - { key: readability-identifier-naming.MemberCase,            value: lower_case }

When clang-tidy is run...

clang-tidy /Users/interlooper/code-enforcer/test/test.cpp -- -std=c++11 | grep --color -A 5 "error:" && ret_code=1 || ret_code=0

It finds no errors if the preprocessor directive is not defined. If it is defined, I get the following correct issues...

/Users/interlooper/code-enforcer/test/test.cpp:48:7: error: invalid case style for class 'wrongClass2' [readability-identifier-naming,-warnings-as-errors]
class wrongClass2 {
      ^~~~~~~~~~~
      WrongClass2
/Users/interlooper/code-enforcer/test/test.cpp:50:7: error: invalid case style for member 'm_M' [readability-identifier-naming,-warnings-as-errors]
  int m_M;
      ^~~
      m_m_m
/Users/interlooper/code-enforcer/test/test.cpp:52:3: error: single-argument constructors must be marked explicit to avoid unintentional implicit conversions [google-explicit-constructor,-warnings-as-errors]
  wrongClass2(int M = 1): m_M(M) {};
  ^
  explicit 
Suppressed 383 warnings (383/Users/interlooper/code-enforcer/test/test.cpp:53:7: error: invalid case style for member 'wrong_setter' [readability-identifier-naming,-warnings-as-errors]
 in non-user code).
  int wrong_setter{};
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
      ^~~~~~~~~~~~
19 warning      m_wrong_setter
s treated as errors
Interlooper
  • 494
  • 2
  • 5
  • 14

0 Answers0