keepassxc icon indicating copy to clipboard operation
keepassxc copied to clipboard

Replace #include guards with #pragma once

Open c4rlo opened this issue 2 years ago • 10 comments

  • ~Make the names of #include guards consistent across the project (with a special category for src/fdosecrets/*/*.h based on prevailing usage). I have left out headers that clearly originate from other projects.~
  • ~Also make the format of the comment after the #endif consistent.~
  • ~Remove one case of #pragma once, replacing it with an #include guard.~

Per discussion below, this PR now replaces #include guards with #pragma once. This was done using the following script:

#!/usr/bin/python

import pathlib
import re


def replace(match, path, is_found):
    is_found[0] = True
    begin_newline = match.group(1)
    body = match.group(2)
    print(f"{path}: fixing")
    num_end_newlines = 0
    for c in reversed(body):
        if c == "\n":
            num_end_newlines += 1
        else:
            break
    if num_end_newlines > 1:
        body = body[:(1-num_end_newlines)]
    return (f"{begin_newline}#pragma once\n{body}")


def process(path):
    content = path.read_text()
    if re.search(r"^#pragma once", content, flags=re.MULTILINE):
        print(f"{path}: ok")
        return

    is_found = [False]
    processed = re.sub(
        r"(^|\n)#ifndef .+?\n#define .+?\n(.*\n)#endif.+?\n?$",
        lambda m: replace(m, path, is_found),
        content,
        count=1,
        flags=re.DOTALL,
    )
    if not is_found[0]:
        print(f"{path}: missing")
    else:
        path.write_text(processed)


def relevant_files():
    yield from pathlib.Path("src").walk()
    yield from pathlib.Path("tests").walk()


for dirpath, dirnames, filenames in relevant_files():
    if str(dirpath) == "src":
        dirnames.remove("thirdparty")
    for name in filenames:
        path = dirpath / name
        if path.suffix in (".h", ".h.cmake") and str(path) != "src/streams/qtiocompressor.h":
            process(path)

Testing strategy

TeamCity

Type of change

  • ✅ Refactor (significant modification to existing code)

c4rlo avatar Feb 05 '23 17:02 c4rlo