Skip to content

Commit

Permalink
uic: Fix some clang-tidy warnings
Browse files Browse the repository at this point in the history
- Repeated return types
- Make member functions static
- narrowing int conversions
- Use auto *
- Use list.isEmpty()
- Use modern includes
- Remove redundant access specifiers
- minor fixes

Pick-to: 6.8
Change-Id: I14ddf1add667536739fbb5fabb357dbaa7ef35c3
Reviewed-by: Jarek Kobus <[email protected]>
  • Loading branch information
FriedemannKleint committed Jul 6, 2024
1 parent d1a5e60 commit 6346c5b
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 68 deletions.
12 changes: 6 additions & 6 deletions src/tools/uic/cpp/cppwritedeclaration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void WriteDeclaration::acceptUI(DomUI *node)
exportMacro.append(u' ');

QStringList namespaceList = qualifiedClassName.split("::"_L1);
if (namespaceList.size()) {
if (!namespaceList.isEmpty()) {
className = namespaceList.last();
namespaceList.removeLast();
}
Expand All @@ -66,14 +66,14 @@ void WriteDeclaration::acceptUI(DomUI *node)
// In this case the generated Ui helper classes will also end up in
// the Qt namespace (which is harmless, but not "pretty")
const bool needsMacro = m_option.qtNamespace &&
(namespaceList.size() == 0 || namespaceList[0] == "qdesigner_internal"_L1);
(namespaceList.isEmpty() || namespaceList.at(0) == "qdesigner_internal"_L1);

if (needsMacro)
m_output << "QT_BEGIN_NAMESPACE\n\n";

openNameSpaces(namespaceList, m_output);

if (namespaceList.size())
if (!namespaceList.isEmpty())
m_output << "\n";

m_output << "class " << exportMacro << m_option.prefix << className << "\n"
Expand All @@ -98,7 +98,7 @@ void WriteDeclaration::acceptUI(DomUI *node)

closeNameSpaces(namespaceList, m_output);

if (namespaceList.size())
if (!namespaceList.isEmpty())
m_output << "\n";

if (m_option.generateNamespace && !m_option.prefix.isEmpty()) {
Expand All @@ -110,7 +110,7 @@ void WriteDeclaration::acceptUI(DomUI *node)

closeNameSpaces(namespaceList, m_output);

if (namespaceList.size())
if (!namespaceList.isEmpty())
m_output << "\n";
}

Expand All @@ -124,7 +124,7 @@ void WriteDeclaration::acceptWidget(DomWidget *node)
if (node->hasAttributeClass())
className = node->attributeClass();

m_output << m_option.indent << m_uic->customWidgetsInfo()->realClassName(className) << " *" << m_driver->findOrInsertWidget(node) << ";\n";
m_output << m_option.indent << CustomWidgetsInfo::realClassName(className) << " *" << m_driver->findOrInsertWidget(node) << ";\n";

TreeWalker::acceptWidget(node);
}
Expand Down
8 changes: 4 additions & 4 deletions src/tools/uic/cpp/cppwriteincludes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <qfileinfo.h>
#include <qtextstream.h>

#include <stdio.h>
#include <cstdio>

QT_BEGIN_NAMESPACE

Expand Down Expand Up @@ -41,7 +41,7 @@ WriteIncludes::WriteIncludes(Uic *uic) : WriteIncludesBase(uic),
for (const auto &e : classInfoEntries()) {
const QString klass = QLatin1StringView(e.klass);
const QString module = QLatin1StringView(e.module);
QLatin1StringView header = QLatin1StringView(e.header);
QLatin1StringView header(e.header);
if (klass.contains(namespaceDelimiter)) {
m_classToHeader.insert(klass, moduleHeader(module, header));
} else {
Expand Down Expand Up @@ -73,7 +73,7 @@ void WriteIncludes::acceptUI(DomUI *node)
void WriteIncludes::insertIncludeForClass(const QString &className, QString header, bool global)
{
if (debugWriteIncludes)
fprintf(stderr, "%s %s '%s' %d\n", Q_FUNC_INFO, qPrintable(className), qPrintable(header), global);
std::fprintf(stderr, "%s %s '%s' %d\n", Q_FUNC_INFO, qPrintable(className), qPrintable(header), global);

do {
if (!header.isEmpty())
Expand All @@ -91,7 +91,7 @@ void WriteIncludes::insertIncludeForClass(const QString &className, QString head
// Remove namespaces
QString lowerClassName = className.toLower();
static const auto namespaceSeparator = "::"_L1;
const int namespaceIndex = lowerClassName.lastIndexOf(namespaceSeparator);
const auto namespaceIndex = lowerClassName.lastIndexOf(namespaceSeparator);
if (namespaceIndex != -1)
lowerClassName.remove(0, namespaceIndex + namespaceSeparator.size());
if (m_includeBaseNames.contains(lowerClassName)) {
Expand Down
66 changes: 34 additions & 32 deletions src/tools/uic/cpp/cppwriteinitialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

#include <algorithm>

#include <ctype.h>

QT_BEGIN_NAMESPACE

using namespace Qt::StringLiterals;
Expand Down Expand Up @@ -416,7 +414,7 @@ int SizePolicyHandle::compare(const SizePolicyHandle &rhs) const

WriteInitialization::LayoutDefaultHandler::LayoutDefaultHandler()
{
std::fill_n(m_state, int(NumProperties), 0u);
std::fill_n(m_state, int(NumProperties), 0U);
std::fill_n(m_defaultValues, int(NumProperties), 0);
}

Expand Down Expand Up @@ -499,7 +497,6 @@ void WriteInitialization::LayoutDefaultHandler::writeProperty(int p, const QStri
writeSetter(indent, objectName, setter, m_defaultValues[p], str);
}
}
return;
}


Expand Down Expand Up @@ -547,7 +544,7 @@ void WriteInitialization::acceptUI(DomUI *node)
if (node->hasAttributeConnectslotsbyname())
m_connectSlotsByName = node->attributeConnectslotsbyname();

if (auto customSlots = node->elementSlots()) {
if (auto *customSlots = node->elementSlots()) {
m_customSlots = customSlots->elementSlot();
m_customSignals = customSlots->elementSignal();
}
Expand Down Expand Up @@ -683,7 +680,8 @@ void WriteInitialization::acceptWidget(DomWidget *node)
const QString className = node->attributeClass();
const QString varName = m_driver->findOrInsertWidget(node);

QString parentWidget, parentClass;
QString parentWidget;
QString parentClass;
if (m_widgetChain.top()) {
parentWidget = m_driver->findOrInsertWidget(m_widgetChain.top());
parentClass = m_widgetChain.top()->attributeClass();
Expand All @@ -698,7 +696,7 @@ void WriteInitialization::acceptWidget(DomWidget *node)

if (m_widgetChain.size() != 1) {
m_output << m_indent << varName << " = " << language::operatorNew
<< language::fixClassName(cwi->realClassName(className))
<< language::fixClassName(CustomWidgetsInfo::realClassName(className))
<< '(' << parentWidget << ')' << language::eol;
}

Expand Down Expand Up @@ -932,7 +930,7 @@ void WriteInitialization::addButtonGroup(const DomWidget *buttonNode, const QStr
// was present before the actual Designer support (4.5)
const bool createGroupOnTheFly = group == nullptr;
if (createGroupOnTheFly) {
DomButtonGroup *newGroup = new DomButtonGroup;
auto *newGroup = new DomButtonGroup;
newGroup->setAttributeName(attributeName);
group = newGroup;
fprintf(stderr, "%s: Warning: Creating button group `%s'\n",
Expand Down Expand Up @@ -984,8 +982,10 @@ void WriteInitialization::acceptLayout(DomLayout *node)
DomPropertyList propList = node->elementProperty();
DomPropertyList newPropList;
if (m_layoutWidget) {
bool left, top, right, bottom;
left = top = right = bottom = false;
bool left = false;
bool top = false;
bool right = false;
bool bottom = false;
for (const DomProperty *p : propList) {
const QString propertyName = p->attributeName();
if (propertyName == "leftMargin"_L1 && p->kind() == DomProperty::Number)
Expand All @@ -998,25 +998,25 @@ void WriteInitialization::acceptLayout(DomLayout *node)
bottom = true;
}
if (!left) {
DomProperty *p = new DomProperty();
auto *p = new DomProperty();
p->setAttributeName("leftMargin"_L1);
p->setElementNumber(0);
newPropList.append(p);
}
if (!top) {
DomProperty *p = new DomProperty();
auto *p = new DomProperty();
p->setAttributeName("topMargin"_L1);
p->setElementNumber(0);
newPropList.append(p);
}
if (!right) {
DomProperty *p = new DomProperty();
auto *p = new DomProperty();
p->setAttributeName("rightMargin"_L1);
p->setElementNumber(0);
newPropList.append(p);
}
if (!bottom) {
DomProperty *p = new DomProperty();
auto *p = new DomProperty();
p->setAttributeName("bottomMargin"_L1);
p->setElementNumber(0);
newPropList.append(p);
Expand Down Expand Up @@ -1054,9 +1054,8 @@ void WriteInitialization::writePropertyList(const QString &varName,
{
if (value.isEmpty())
return;
const QStringList list = value.split(u',');
const int count = list.size();
for (int i = 0; i < count; i++) {
const auto list = QStringView{value}.split(u',');
for (qsizetype i = 0, count = list.size(); i < count; i++) {
if (list.at(i) != defaultValue) {
m_output << m_indent << varName << language::derefPointer << setFunction
<< '(' << i << ", " << list.at(i) << ')' << language::eol;
Expand Down Expand Up @@ -1250,7 +1249,7 @@ static QString configKeyForProperty(const QString &propertyName)
return shortcutConfigKey();
if (propertyName == "accessibleName"_L1 || propertyName == "accessibleDescription"_L1)
return accessibilityConfigKey();
return QString();
return {};
}

void WriteInitialization::writeProperties(const QString &varName,
Expand Down Expand Up @@ -1290,8 +1289,10 @@ void WriteInitialization::writeProperties(const QString &varName,
<< language::charliteral(objectName, m_dindent) << ')' << language::eol;
}

int leftMargin, topMargin, rightMargin, bottomMargin;
leftMargin = topMargin = rightMargin = bottomMargin = -1;
int leftMargin = -1;
int topMargin = -1;
int rightMargin = -1;
int bottomMargin = -1;
bool frameShadowEncountered = false;

for (const DomProperty *p : lst) {
Expand Down Expand Up @@ -2006,8 +2007,8 @@ QString WriteInitialization::domColor2QString(const DomColor *c)
static inline QVersionNumber colorRoleVersionAdded(const QString &roleName)
{
if (roleName == "PlaceholderText"_L1)
return QVersionNumber(5, 12, 0);
return QVersionNumber();
return {5, 12, 0};
return {};
}

void WriteInitialization::writeColorGroup(DomColorGroup *colorGroup, const QString &group, const QString &paletteName)
Expand Down Expand Up @@ -2308,8 +2309,9 @@ void WriteInitialization::enableSorting(DomWidget *w, const QString &varName, co
the initializer is omitted.
See above for other parameters.
*/
void WriteInitialization::addInitializer(Item *item,
const QString &name, int column, const QString &value, const QString &directive, bool translatable) const
void WriteInitialization::addInitializer(Item *item, const QString &name,
int column, const QString &value,
const QString &directive, bool translatable)
{
if (!value.isEmpty()) {
QString setter;
Expand Down Expand Up @@ -2360,8 +2362,8 @@ void WriteInitialization::addBrushInitializer(Item *item,
Create inititializer for a flag value in the Qt namespace.
If the named property is not in the map, the initializer is omitted.
*/
void WriteInitialization::addQtFlagsInitializer(Item *item,
const DomPropertyMap &properties, const QString &name, int column) const
void WriteInitialization::addQtFlagsInitializer(Item *item, const DomPropertyMap &properties,
const QString &name, int column)
{
if (const DomProperty *p = properties.value(name)) {
const QString orOperator = u'|' + language::qtQualifier;
Expand Down Expand Up @@ -2500,10 +2502,10 @@ WriteInitialization::Items WriteInitialization::initializeTreeWidgetItems(const
{
// items
Items items;
const int numDomItems = domItems.size();
const qsizetype numDomItems = domItems.size();
items.reserve(numDomItems);

for (int i = 0; i < numDomItems; ++i) {
for (qsizetype i = 0; i < numDomItems; ++i) {
const DomItem *domItem = domItems.at(i);

Item *item = new Item("QTreeWidgetItem"_L1, m_indent, m_output, m_refreshOut, m_driver);
Expand Down Expand Up @@ -2687,7 +2689,7 @@ QString WriteInitialization::noTrCall(DomString *str, const QString &defaultStri
{
QString value = defaultString;
if (!str && defaultString.isEmpty())
return QString();
return {};
if (str)
value = str->text();
QString ret;
Expand Down Expand Up @@ -2815,7 +2817,7 @@ static void generateMultiDirectiveBegin(QTextStream &outputStream, const QSet<QS
std::sort(list.begin(), list.end());

outputStream << "#if " << language::qtConfig(list.constFirst());
for (int i = 1, size = list.size(); i < size; ++i)
for (qsizetype i = 1, size = list.size(); i < size; ++i)
outputStream << " || " << language::qtConfig(list.at(i));
outputStream << Qt::endl;
}
Expand Down Expand Up @@ -2847,14 +2849,14 @@ WriteInitialization::Item::~Item()
QString WriteInitialization::Item::writeSetupUi(const QString &parent, Item::EmptyItemPolicy emptyItemPolicy)
{
if (emptyItemPolicy == Item::DontConstruct && m_setupUiData.policy == ItemData::DontGenerate)
return QString();
return {};

bool generateMultiDirective = false;
if (emptyItemPolicy == Item::ConstructItemOnly && m_children.isEmpty()) {
if (m_setupUiData.policy == ItemData::DontGenerate) {
m_setupUiStream << m_indent << language::operatorNew << m_itemClassName
<< '(' << parent << ')' << language::eol;
return QString();
return {};
}
if (m_setupUiData.policy == ItemData::GenerateWithMultiDirective)
generateMultiDirective = true;
Expand Down
11 changes: 5 additions & 6 deletions src/tools/uic/cpp/cppwriteinitialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ struct WriteInitialization : public TreeWalker
void writeRetranslateUi(const QString &parentPath);
void addSetter(const QString &setter, const QString &directive = QString(), bool translatable = false); // don't call it if you already added *this as a child of another Item
void addChild(Item *child); // all setters should already been added
private:

struct ItemData
{
QMultiMap<QString, QString> setters; // directive to setter
Expand All @@ -175,10 +175,10 @@ struct WriteInitialization : public TreeWalker
};
using Items = QList<Item *>;

void addInitializer(Item *item,
const QString &name, int column, const QString &value, const QString &directive = QString(), bool translatable = false) const;
void addQtFlagsInitializer(Item *item, const DomPropertyMap &properties,
const QString &name, int column = -1) const;
static void addInitializer(Item *item, const QString &name, int column,
const QString &value, const QString &directive = QString(), bool translatable = false);
static void addQtFlagsInitializer(Item *item, const DomPropertyMap &properties,
const QString &name, int column = -1);
void addQtEnumInitializer(Item *item,
const DomPropertyMap &properties, const QString &name, int column = -1) const;
void addBrushInitializer(Item *item,
Expand Down Expand Up @@ -206,7 +206,6 @@ struct WriteInitialization : public TreeWalker

Declaration findDeclaration(const QString &name);

private:
QString writeFontProperties(const DomFont *f);
QString writeIconProperties(const DomResourceIcon *i);
void writeThemeIconCheckAssignment(const QString &themeValue, const QString &iconName,
Expand Down
6 changes: 3 additions & 3 deletions src/tools/uic/customwidgetsinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ bool CustomWidgetsInfo::isAmbiguousSlot(const QString &className,
return isAmbiguous(className, signalSignature, QMetaMethod::Slot);
}

QString CustomWidgetsInfo::realClassName(const QString &className) const
QString CustomWidgetsInfo::realClassName(const QString &className)
{
if (className == "Line"_L1)
return u"QFrame"_s;
Expand All @@ -175,7 +175,7 @@ QString CustomWidgetsInfo::customWidgetAddPageMethod(const QString &name) const
{
if (DomCustomWidget *dcw = m_customWidgets.value(name, nullptr))
return dcw->elementAddPageMethod();
return QString();
return {};
}

// add page methods for simple containers taking only the widget parameter
Expand All @@ -195,7 +195,7 @@ QString CustomWidgetsInfo::simpleContainerAddPageMethod(const QString &name) con
if (extends(name, m.first))
return m.second;
}
return QString();
return {};
}

QT_END_NAMESPACE
2 changes: 1 addition & 1 deletion src/tools/uic/customwidgetsinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class CustomWidgetsInfo : public TreeWalker
QString customWidgetAddPageMethod(const QString &name) const;
QString simpleContainerAddPageMethod(const QString &name) const;

QString realClassName(const QString &className) const;
static QString realClassName(const QString &className);

bool extends(const QString &className, QAnyStringView baseClassName) const;
bool extendsOneOf(const QString &className, const QStringList &baseClassNames) const;
Expand Down
2 changes: 1 addition & 1 deletion src/tools/uic/databaseinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void DatabaseInfo::acceptWidget(DomWidget *node)
QHash<QString, DomProperty*> properties = propertyMap(node->elementProperty());

DomProperty *frameworkCode = properties.value("frameworkCode"_L1);
if (frameworkCode && toBool(frameworkCode->elementBool()) == false)
if (frameworkCode && !toBool(frameworkCode->elementBool()))
return;

DomProperty *db = properties.value("database"_L1);
Expand Down
Loading

0 comments on commit 6346c5b

Please sign in to comment.