Defined in header <cparse/Generator.hh>
virtual std::string generateHeaderGuard( std::string const& header ) const noexcept;
Composes a header-guard identifier from getNamespacePath() and header, in five steps:
- Start with
getNamespacePath(). - If non-empty, append
'/'thenheader; otherwise justheader. - Upper-case the result.
- Replace every non-alphanumeric with
'_'. - Collapse any run of consecutive underscores to a single
_, then strip leading underscores.
The result is a valid identifier regardless of what characters appeared in the inputs.
Parameters
| Parameter | Description |
|---|---|
header | Header filename or path. Typically ends in .hh / .h; the extension is preserved by upper-casing the letters and mapping the dot to _. |
Return value
An all-caps identifier suitable for #ifndef / #define guards.
For a subclass whose getNamespacePath() returns "fedem::parser":
generateHeaderGuard("Cursor.hh")
// → "FEDEM_PARSER_CURSOR_HH"
Exceptions
noexcept.
Notes
The consecutive-underscore collapse handles the case where :: in the namespace path becomes __ after step 4: FEDEM::PARSER/CURSOR.HH → FEDEM__PARSER_CURSOR_HH → FEDEM_PARSER_CURSOR_HH. Leading-underscore strip handles a starting / if the namespace path was empty but the header started with a slash.
Example
Typical use in an emitter:
void MyGen::emitHeaderGuardOpen(std::string const& fileName)
{
auto const guard = generateHeaderGuard(fileName);
getStream() << "#ifndef " << guard << "\n"
<< "#define " << guard << "\n\n";
}
See also
- Generator::getNamespacePath — the namespace-path source.
- Generator::convertUpperCase — the case transformation used.
- Generator::replaceNonAlphanums — the sanitisation used.

