16

Is there a way to remove the suggested computer specific path on the suggested llvm-header-guard string when running static analysis with clang-tidy?

For example the suggested header guard for the file (cls/math/matrix.hpp) is: _USERS_NAME_DESKTOP_PROJECT_SRC_CLS_MATH_MATRIX_HPP

But to avoid making this specific to my computer it should only be: _PROJECT_SRC_CLS_MATH_MATRIX_HPP

Is there a way to configure clang-tidy such that it suggests header guards starting from the /project/ directory rather than the /users/ directory?

I am using clang 5.0.0, running from cmake.

user7119460
  • 1,451
  • 10
  • 20

1 Answers1

6

I've also had this problem for ages and finally figured it out, at least for public includes: clang-tidy assumes that headers are within a directory called include.

So, if your headers are like /home/me/myproject/include/myproject/header.h then it will work properly and recommend MYPROJECT_HEADER_H. If it doesn't find an include directory to treat as the root, then it falls back to the absolute path.

So the solution for libraries is to use that structure (which is probably a good idea anyway). I am still not sure about headers inside a project's source though.

drobilla
  • 211
  • 3
  • 6
  • 1
    "which is probably a good idea anyway" not if you have headers which you want to keep internal to your project, surely. It's often preferable to keep *MyApi.cpp* and *MyApi.h* together in a *src* folder away from externally-facing interfaces. – John McFarlane Mar 11 '20 at 18:11
  • 1
    Sure, I just meant for public includes, obviously that doesn't apply for side-by-side headers and implementations. I'm guessing this is a symptom of the check being implemented specifically for LLVM which doesn't do that. It would be nice if it were more general, or at least better documented. – drobilla Mar 11 '20 at 19:41
  • 3
    Yes, I agree. This test is almost very useful but instead is not much use at all. – John McFarlane Mar 12 '20 at 22:44