8

In clang tidy, the check [llvm-header-guard] looks for LLVM style header guards, but I can't find any examples of proper LLVM header guard style, specifically the structure of the name given to the define, the coding standards pages does not mention anything.

valiano
  • 16,433
  • 7
  • 64
  • 79
A-n-t-h-o-n-y
  • 410
  • 6
  • 14
  • 1
    According to the link, `llvm-header-guard` will fix one that do not adhere, so have you run it and looked at the results? You could also examine existing LLVM headers to see what they look like. – JAB May 09 '17 at 22:45
  • Last time I checked (today) `llvm-header-guard` asked me to precede the guard label with an uncerscore, it suggeted something like: `_HOME_USER_PRJ_DETAIL_FILENAME_HPP`. While other tidy rules told me not to (`bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp`), so I have to disable `llvm-header-guard`. – alfC Aug 21 '21 at 23:14

3 Answers3

6

Looking at the unit tests:

it seems to accept a few variations on the commonly used patterns. For a file named include/llvm/ADT/foo.h the convention seems to be:

#ifndef LLVM_ADT_FOO_H
#define LLVM_ADT_FOO_H
//...
#endif // LLVM_ADT_FOO_H
gavinb
  • 19,278
  • 3
  • 45
  • 60
  • 1
    You can also glean a little of the logic by looking at the macro checking loop in `EndOfMainFile` in [HeaderGuard.cpp](https://github.com/llvm-mirror/clang-tools-extra/blob/master/clang-tidy/utils/HeaderGuard.cpp). There, it parses the header guards, then auto-generates a guard and checks they are identical. If not, it's a warning. – paddy May 09 '17 at 23:00
2

Presumably the LLVM codebase adheres to the LLVM coding standards, so one can simply look at a few LLVM header files to get an idea of what the guard looks like. Here are some random LLVM header files I looked at:

https://github.com/llvm-mirror/llvm/blob/master/include/llvm/CodeGen/SelectionDAG.h

https://github.com/llvm-mirror/llvm/blob/master/include/llvm/Support/AlignOf.h

Based on those files, I think the header guard looks like this:

#ifndef LLVM_CODEGEN_SELECTIONDAG_H
#define LLVM_CODEGEN_SELECTIONDAG_H
...
#endif
David Grayson
  • 84,103
  • 24
  • 152
  • 189
  • I'm looking for the structure of the name for the define, from the links it looks like it is the path from the include directory. It'd be nice to see it spelled out by LLVM. – A-n-t-h-o-n-y May 09 '17 at 22:50
  • This is a very good answer. Not just for LLVM - it's a reasonable convention for *ANY* compiler, C or C++. I think it would be extremely unwise to do anything more complicated than this. IMO... – paulsm4 May 09 '17 at 22:55
2

The proper style for LLVM to detect and be happy with your header is to take the path used to include your header, convert it to uppercase, replace directory separators with underscores, and replace dots in file extensions with underscores.

For instance, if you use #include <dopelib/dopestuff/whatitisyo.h>, your header would be:

#ifndef DOPELIB_DOPESTUFF_WHATITISYO_H
#define DOPELIB_DOPESTUFF_WHATITISYO_H

/** Your code here. **/

#endif

Hope this helps!