[Libreoffice-bugs] [Bug 124176] Use pragma once instead of include guards

bugzilla-daemon at bugs.documentfoundation.org bugzilla-daemon at bugs.documentfoundation.org
Mon Aug 3 16:00:22 UTC 2020


https://bugs.documentfoundation.org/show_bug.cgi?id=124176

George Bateman <george.bateman16 at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|                            |george.bateman16 at gmail.com
           Assignee|libreoffice-bugs at lists.free |george.bateman16 at gmail.com
                   |desktop.org                 |

--- Comment #97 from George Bateman <george.bateman16 at gmail.com> ---
I am working on this as my first LO bug, using a suitable Python script to make
the changes en masse. Currently I've got the script to make the vast majority
of changes successfully and have had to edit just the following few files
manually:

chart2/source/inc/InternalData.hxx
include/svx/svdmodel.hxx
include/vcl/glxtestprocess.hxx

Unfortunately the tests are failing on my machine even with my changes removed
but the build at least is successful.

The script is:

#!/usr/bin/python3

import re
import subprocess
import sys

debug = len(sys.argv) > 1

if debug:
    files = sys.argv[1:]
else:
    files = subprocess.run(['git', 'grep', '--name-only', '#ifndef
INCLUDED_.*_HXX'],
        text=True, capture_output=True).stdout.splitlines()

for file_name in files:
    with open(file_name, 'r+') as f:
        lines = list(f)
        flag_name = None
        flag_start = None
        flag_def = None
        flag_end = None
        is_bad = False
        else_in_top_lvl = False
        if_lvl = 0

        if debug:
            print("Processing %s with %d lines" % (file_name, len(lines)))

        for i, line in enumerate(lines):
            if re.match('^\\s*#\\s*if', line):
                if_lvl += 1
                if if_lvl == 1 and flag_start is not None:
                    # There is a top-level if statement after the include
guard.
                    is_bad = True
            elif re.match('^\\s*#\\s*el', line):
                if if_lvl <= 1:
                    else_in_top_lvl = True
            elif re.match('^\\s*#\\s*endif', line):
                if_lvl -= 1

            if flag_name is None:
                if m := re.match('#ifndef (INCLUDED_.*_HXX)', line):
                    flag_name = m.group(1)
                    flag_start = i
            elif flag_def is None:
                if m := re.match('#define (INCLUDED_.*_HXX)', line):
                    if m.group(1) == flag_name and i == flag_start + 1:
                        flag_def = i
            elif flag_end is None:
                if re.match('^\\s*#\\s*endif', line) and if_lvl == 0:
                    flag_end = i
            else:
                if not (re.match('^\\s*$', line) or
re.match('^\\s*/\\*.*\\*/\\s*$', line)
                        or re.match('^\\s*//', line)):
                    is_bad = True

            if debug:
                print(f"Line {i}\tflags
{flag_start}\t{flag_def}\t{flag_end}\t{if_lvl}\t{else_in_top_lvl}\t{is_bad}")

        if flag_start is not None and (flag_end is None or else_in_top_lvl):
            is_bad = True
        if is_bad:
            print("%s is bad!" % file_name)
            continue
        if flag_start is None: continue
        if debug: continue

        lines[flag_start] = '#pragma once\n'
        lines.pop(flag_end)
        lines.pop(flag_def)
        f.seek(0)
        f.truncate()
        f.write(''.join(lines))

It parses preprocessor commands at a fairly basic level and makes the needed
changes if they seem safe (i.e. is_bad is false), or if passed file paths
prints debugging information to explain why they are bad. The only outstanding
file with a "weird" include guard is vcl/inc/unx/gtk/gtkgdi.hxx. This file has
an #else command half-way though the include guard which may not be correct.

-- 
You are receiving this mail because:
You are the assignee for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.freedesktop.org/archives/libreoffice-bugs/attachments/20200803/1ebfc127/attachment-0001.htm>


More information about the Libreoffice-bugs mailing list