Skip to content

Commit

Permalink
Merge pull request #59230 from m-kuhn/mocdefs
Browse files Browse the repository at this point in the history
Add includemocs.py and create one moc file per cpp file
m-kuhn authored Oct 30, 2024
2 parents b167311 + 3a7182b commit f436bfa
Showing 1,668 changed files with 1,930 additions and 142 deletions.
2 changes: 1 addition & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Checks: 'bugprone-*,-bugprone-easily-swappable-parameters,-bugprone-virtual-near-miss'
Checks: 'bugprone-*,-bugprone-easily-swappable-parameters,-bugprone-virtual-near-miss,-bugprone-suspicious-include'
12 changes: 12 additions & 0 deletions .github/workflows/code_layout.yml
Original file line number Diff line number Diff line change
@@ -222,3 +222,15 @@ jobs:
- name: Run cppcheck test
run: ./scripts/cppcheck.sh

moc_check:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Run Check
run: python3 scripts/includemocs.py src --dry-run
Original file line number Diff line number Diff line change
@@ -7,7 +7,6 @@
************************************************************************/



class QgsCalloutWidget : QWidget, protected QgsExpressionContextGenerator
{
%Docstring(signature="appended")
2 changes: 2 additions & 0 deletions python/PyQt6/gui/auto_generated/qgsdial.sip.in
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@




class QgsDial : QDial
{

@@ -38,6 +39,7 @@ Constructor for QgsDial


};

/************************************************************************
* This file has been generated automatically from *
* *
2 changes: 2 additions & 0 deletions python/PyQt6/gui/auto_generated/qgsslider.sip.in
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@




class QgsSlider : QSlider
{

@@ -41,6 +42,7 @@ Constructor for QgsSlider


};

/************************************************************************
* This file has been generated automatically from *
* *
1 change: 0 additions & 1 deletion python/gui/auto_generated/callouts/qgscalloutwidget.sip.in
Original file line number Diff line number Diff line change
@@ -7,7 +7,6 @@
************************************************************************/



class QgsCalloutWidget : QWidget, protected QgsExpressionContextGenerator
{
%Docstring(signature="appended")
2 changes: 2 additions & 0 deletions python/gui/auto_generated/qgsdial.sip.in
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@




class QgsDial : QDial
{

@@ -38,6 +39,7 @@ Constructor for QgsDial


};

/************************************************************************
* This file has been generated automatically from *
* *
2 changes: 2 additions & 0 deletions python/gui/auto_generated/qgsslider.sip.in
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@




class QgsSlider : QSlider
{

@@ -41,6 +42,7 @@ Constructor for QgsSlider


};

/************************************************************************
* This file has been generated automatically from *
* *
259 changes: 259 additions & 0 deletions scripts/includemocs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
#!/usr/bin/env python3

#
# This file is part of KDToolBox.
#
# SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
# Author: Jesper K. Pedersen <jesper.pedersen@kdab.com>
#
# SPDX-License-Identifier: MIT
#

"""
Script to add inclusion of mocs to files recursively.
"""

# pylint: disable=redefined-outer-name

import os
import re
import argparse
import sys

dirty = False


def stripInitialSlash(path):
if path and path.startswith("/"):
path = path[1:]
return path


# Returns true if the path is to be excluded from the search


def shouldExclude(root, path):
# pylint: disable=used-before-assignment,possibly-used-before-assignment
if not args.excludes:
return False # No excludes provided

assert root.startswith(args.root)
root = stripInitialSlash(root[len(args.root):])

if args.headerPrefix:
assert root.startswith(args.headerPrefix)
root = stripInitialSlash(root[len(args.headerPrefix):])

return (path in args.excludes) or (root + "/" + path in args.excludes)


regexp = re.compile("\\s*(Q_OBJECT|Q_GADGET|Q_NAMESPACE)\\s*")
# Returns true if the header file provides contains a Q_OBJECT, Q_GADGET or Q_NAMESPACE macro


def hasMacro(fileName):
with open(fileName, "r", encoding="ISO-8859-1") as fileHandle:
for line in fileHandle:
if regexp.match(line):
return True
return False


# returns the matching .cpp file for the given .h file


def matchingCPPFile(root, fileName):
assert root.startswith(args.root)
root = stripInitialSlash(root[len(args.root):])

if args.headerPrefix:
assert root.startswith(args.headerPrefix)
root = stripInitialSlash(root[len(args.headerPrefix):])

if args.sourcePrefix:
root = args.sourcePrefix + "/" + root

return (
args.root
+ "/"
+ root
+ ("/" if root != "" else "")
+ fileNameWithoutExtension(fileName)
+ ".cpp"
)


def fileNameWithoutExtension(fileName):
return os.path.splitext(os.path.basename(fileName))[0]


# returns true if the specifies .cpp file already has the proper include


def cppHasMOCInclude(fileName):
includeStatement = '#include "moc_%s.cpp"' % fileNameWithoutExtension(fileName)
with open(fileName, encoding="utf8") as fileHandle:
return includeStatement in fileHandle.read()


def getMocInsertionLocation(filename, content):
headerIncludeRegex = re.compile(
r'#include "%s\.h".*\n' % fileNameWithoutExtension(filename), re.M
)
match = headerIncludeRegex.search(content)
if match:
return match.end()
return 0


def trimExistingMocInclude(content, cppFileName):
mocStrRegex = re.compile(
r'#include "moc_%s\.cpp"\n' % fileNameWithoutExtension(cppFileName)
)
match = mocStrRegex.search(content)
if match:
return content[: match.start()] + content[match.end():]
return content


def processFile(root, fileName):
# pylint: disable=global-statement
global dirty
macroFound = hasMacro(root + "/" + fileName)
logVerbose(
"Inspecting %s %s"
% (
root + "/" + fileName,
"[Has Q_OBJECT / Q_GADGET / Q_NAMESPACE]" if macroFound else "",
)
)

if macroFound:
cppFileName = matchingCPPFile(root, fileName)
logVerbose(" -> %s" % cppFileName)

if not os.path.exists(cppFileName):
log("file %s didn't exist (which might not be an error)" % cppFileName)
return

if args.replaceExisting or not cppHasMOCInclude(cppFileName):
dirty = True
if args.dryRun:
log("Missing moc include file: %s" % cppFileName)
else:
log("Updating %s" % cppFileName)

with open(cppFileName, "r", encoding="utf8") as f:
content = f.read()

if args.replaceExisting:
content = trimExistingMocInclude(content, cppFileName)

loc = getMocInsertionLocation(cppFileName, content)
if args.insertAtEnd:
with open(cppFileName, "a", encoding="utf8") as f:
f.write(
'\n#include "moc_%s.cpp"\n'
% fileNameWithoutExtension(cppFileName)
)
else:
with open(cppFileName, "w", encoding="utf8") as f:
f.write(
content[:loc]
+ (
'#include "moc_%s.cpp"\n'
% fileNameWithoutExtension(cppFileName)
)
+ content[loc:]
)


def log(content):
if not args.quiet:
print(content)


def logVerbose(content):
if args.verbose:
print(content)


# MAIN
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="""Script to add inclusion of mocs to files recursively.
The source files either need to be in the same directories as the header files or in parallel directories,
where the root of the headers are specified using --header-prefix and the root of the sources are specified using --source-prefix.
If either header-prefix or source-prefix is the current directory, then they may be omitted."""
)
parser.add_argument(
"--dry-run",
"-n",
dest="dryRun",
action="store_true",
help="only report files to be updated",
)
parser.add_argument(
"--quiet", "-q", dest="quiet", action="store_true", help="suppress output"
)
parser.add_argument("--verbose", "-v", dest="verbose", action="store_true")
parser.add_argument(
"--header-prefix",
metavar="directory",
dest="headerPrefix",
help="This directory will be replaced with source-prefix when "
"searching for matching source files",
)
parser.add_argument(
"--source-prefix",
metavar="directory",
dest="sourcePrefix",
help="see --header-prefix",
)
parser.add_argument(
"--excludes",
metavar="directory",
dest="excludes",
nargs="*",
help="directories to be excluded, might either be in the form of a directory name, "
"e.g. 3rdparty or a partial directory prefix from the root, e.g 3rdparty/parser",
)
parser.add_argument(
"--insert-at-end",
dest="insertAtEnd",
action="store_true",
help="insert the moc include at the end of the file instead of the beginning",
)
parser.add_argument(
"--replace-existing",
dest="replaceExisting",
action="store_true",
help="delete and readd existing MOC include statements",
)
parser.add_argument(
dest="root",
default=".",
metavar="directory",
nargs="?",
help="root directory for the operation",
)

args = parser.parse_args()

root = args.root
if args.headerPrefix:
root += "/" + args.headerPrefix

path = os.walk(root)
for root, directories, files in path:
# Filter out directories specified in --exclude
directories[:] = [d for d in directories if not shouldExclude(root, d)]

for file in files:
if file.endswith(".h") or file.endswith(".hpp"):
processFile(root, file)

if not dirty:
log("No changes needed")

sys.exit(-1 if dirty else 0)
1 change: 1 addition & 0 deletions src/3d/chunks/qgschunkboundsentity_p.cpp
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@
***************************************************************************/

#include "qgschunkboundsentity_p.h"
#include "moc_qgschunkboundsentity_p.cpp"

#include <Qt3DExtras/QPhongMaterial>

1 change: 1 addition & 0 deletions src/3d/chunks/qgschunkedentity.cpp
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@
***************************************************************************/

#include "qgschunkedentity.h"
#include "moc_qgschunkedentity.cpp"

#include <QElapsedTimer>
#include <QVector4D>
1 change: 1 addition & 0 deletions src/3d/chunks/qgschunkloader.cpp
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@
***************************************************************************/

#include "qgschunkloader.h"
#include "moc_qgschunkloader.cpp"
#include "qgschunknode.h"

#include <QVector>
1 change: 1 addition & 0 deletions src/3d/chunks/qgschunkqueuejob.cpp
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@
***************************************************************************/

#include "qgschunkqueuejob.h"
#include "moc_qgschunkqueuejob.cpp"

///@cond PRIVATE

1 change: 1 addition & 0 deletions src/3d/materials/qgsmaterial.cpp
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@
***************************************************************************/

#include "qgsmaterial.h"
#include "moc_qgsmaterial.cpp"
#include "qgs3dutils.h"

#include <Qt3DRender/QEffect>
Loading

0 comments on commit f436bfa

Please sign in to comment.