Qt-Generic-MissingIncludeGuard

Include files need an include guard

Required inputs: IR

Every header file should have an include guard. This ensures that including the file multiple times into the same compilation unit (e.g. indirect inclusions through other headers) do not cause compiler errors due to duplicate type definitions.

Include guards also help reduce compilation time, as compilers that recognize the include guard can avoid repeatedly scanning the header file.

Example
#ifndef DIR_FILE_H
#define DIR_FILE_H

...

#endif

The identifiers used for include guards must be unique. If header file names are not necessarily unique in your project, include the directory name in the identifier.

Possible Messages

Key

Text

Severity

Disabled

include_guard_missing

Missing include guard.

None

False

include_guard_partial

Include guard does not cover complete file.

None

False

include_guard_with_wrong_define

Bad include guard: #define uses wrong macro, should be {}.

None

False

include_guard_without_define

Incomplete include guard: missing #define {}.

None

False

Options

macro_name_restrictions

macro_name_restrictions : list[typing.Callable[[bauhaus.ir.Node, scanner.Token, typing.Optional[str]], typing.Optional[str]]] = []

List of Python functions with parameters (file, define, macro) to perform additional checks on the macro used as include guard in the given file. Should return None if macro is accepted, else a message to print for the violation. The parameters are: PIR File node for the header, token for the macro #define or #pragma once, and the name of the macro or the string 'pragma once'.