Skip to content

Commit d42700a

Browse files
committed
enable QT_NO_CAST_FROM_ASCII
To facilitate that, sprinkle a bunch of QStringLiteral() around string literals and QLatin1Char() around char literals. In some places, I used QLatin1String() instead of QStringLiteral() when the expression is used as an argument to a function that has a QLatin1String overload (this is advised by the Qt docs). I also replaced empty strings like QString x = ""; QString x { "" }; funcWithQStringArg(""); funcWithQStringArg(QString("")); by the QString() default constructor, which yields an empty string more efficiently: QString x; funcWithQStringArg(QString()); QString::fromLocal8Bit() was used whenever strings were read from C libraries (e.g. PAM). For SDDM's own configuration files, I used QString::fromUtf8(), under the assumption that most text editors today default to UTF-8. In some places, I also used the chance to optimize single-char string literals to char literals, e.g. str << list.join(","); //before str << list.join(QLatin1Char(',')); //after Testing done: It compiles and the ConfigurationTest passes. I don't have a test setup for actually running a compiled SDDM, so if someone could check that I didn't break anything, that would be highly appreciated. Closes: #469
1 parent 5c0c703 commit d42700a

36 files changed

+297
-294
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ option(BUILD_MAN_PAGES "Build man pages" OFF)
3333
option(ENABLE_JOURNALD "Enable logging to journald" ON)
3434

3535
# Definitions
36-
add_definitions(-Wall -std=c++11)
36+
add_definitions(-Wall -std=c++11 -DQT_NO_CAST_FROM_ASCII)
3737

3838
# Default build type
3939
if(NOT CMAKE_BUILD_TYPE)

src/auth/Auth.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ namespace SDDM {
100100
Auth::SocketServer* Auth::SocketServer::instance() {
101101
if (!self) {
102102
self = new SocketServer();
103-
self->listen(QString("sddm-auth%1").arg(QUuid::createUuid().toString().replace(QRegExp("[{}]"),"")));
103+
self->listen(QStringLiteral("sddm-auth%1").arg(QUuid::createUuid().toString().replace(QRegExp(QStringLiteral("[{}]")), QString())));
104104
}
105105
return self;
106106
}
@@ -114,11 +114,11 @@ namespace SDDM {
114114
SocketServer::instance()->helpers[id] = this;
115115
QProcessEnvironment env = child->processEnvironment();
116116
bool langEmpty = true;
117-
QFile localeFile("/etc/locale.conf");
117+
QFile localeFile(QStringLiteral("/etc/locale.conf"));
118118
if (localeFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
119119
QTextStream in(&localeFile);
120120
while (!in.atEnd()) {
121-
QStringList parts = in.readLine().split('=');
121+
QStringList parts = in.readLine().split(QLatin1Char('='));
122122
if (parts.size() >= 2) {
123123
env.insert(parts[0], parts[1]);
124124
if (parts[0] == QStringLiteral("LANG"))
@@ -128,7 +128,7 @@ namespace SDDM {
128128
localeFile.close();
129129
}
130130
if (langEmpty)
131-
env.insert("LANG", "C");
131+
env.insert(QStringLiteral("LANG"), QStringLiteral("C"));
132132
child->setProcessEnvironment(env);
133133
connect(child, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(childExited(int,QProcess::ExitStatus)));
134134
connect(child, SIGNAL(error(QProcess::ProcessError)), this, SLOT(childError(QProcess::ProcessError)));
@@ -193,7 +193,7 @@ namespace SDDM {
193193
break;
194194
}
195195
default: {
196-
Q_EMIT auth->error(QString("Auth: Unexpected value received: %1").arg(m), ERROR_INTERNAL);
196+
Q_EMIT auth->error(QStringLiteral("Auth: Unexpected value received: %1").arg(m), ERROR_INTERNAL);
197197
}
198198
}
199199
}
@@ -335,17 +335,17 @@ namespace SDDM {
335335

336336
void Auth::start() {
337337
QStringList args;
338-
args << "--socket" << SocketServer::instance()->fullServerName();
339-
args << "--id" << QString("%1").arg(d->id);
338+
args << QStringLiteral("--socket") << SocketServer::instance()->fullServerName();
339+
args << QStringLiteral("--id") << QStringLiteral("%1").arg(d->id);
340340
if (!d->sessionPath.isEmpty())
341-
args << "--start" << d->sessionPath;
341+
args << QStringLiteral("--start") << d->sessionPath;
342342
if (!d->user.isEmpty())
343-
args << "--user" << d->user;
343+
args << QStringLiteral("--user") << d->user;
344344
if (d->autologin)
345-
args << "--autologin";
345+
args << QStringLiteral("--autologin");
346346
if (d->greeter)
347-
args << "--greeter";
348-
d->child->start(QString("%1/sddm-helper").arg(LIBEXEC_INSTALL_DIR), args);
347+
args << QStringLiteral("--greeter");
348+
d->child->start(QStringLiteral("%1/sddm-helper").arg(QStringLiteral(LIBEXEC_INSTALL_DIR)), args);
349349
}
350350
}
351351

src/auth/AuthMessages.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ namespace SDDM {
160160
QStringList l;
161161
s >> l;
162162
for (QString s : l) {
163-
int pos = s.indexOf('=');
163+
int pos = s.indexOf(QLatin1Char('='));
164164
m.insert(s.left(pos), s.mid(pos + 1));
165165
}
166166
return s;

src/common/ConfigReader.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include <QtCore/QFileInfo>
3030

3131
QTextStream &operator>>(QTextStream &str, QStringList &list) {
32-
QStringList tempList = str.readLine().split(",");
32+
QStringList tempList = str.readLine().split(QLatin1Char(','));
3333
list.clear();
3434
foreach(const QString &s, tempList)
3535
if (!s.trimmed().isEmpty())
@@ -38,12 +38,12 @@ QTextStream &operator>>(QTextStream &str, QStringList &list) {
3838
}
3939

4040
QTextStream &operator<<(QTextStream &str, const QStringList &list) {
41-
str << list.join(",");
41+
str << list.join(QLatin1Char(','));
4242
return str;
4343
}
4444

4545
QTextStream &operator>>(QTextStream &str, bool &val) {
46-
if (0 == str.readLine().trimmed().compare("true", Qt::CaseInsensitive))
46+
if (0 == str.readLine().trimmed().compare(QLatin1String("true"), Qt::CaseInsensitive))
4747
val = true;
4848
else
4949
val = false;
@@ -96,14 +96,14 @@ namespace SDDM {
9696
}
9797

9898
QString ConfigSection::toConfigFull() const {
99-
QString final = QString("[%1]\n").arg(m_name);
99+
QString final = QStringLiteral("[%1]\n").arg(m_name);
100100
for (const ConfigEntryBase *entry : m_entries)
101101
final.append(entry->toConfigFull());
102102
return final;
103103
}
104104

105105
QString ConfigSection::toConfigShort() const {
106-
return QString("[%1]").arg(name());
106+
return QStringLiteral("[%1]").arg(name());
107107
}
108108

109109

@@ -123,7 +123,7 @@ namespace SDDM {
123123
QString ret;
124124
for (ConfigSection *s : m_sections) {
125125
ret.append(s->toConfigFull());
126-
ret.append('\n');
126+
ret.append(QLatin1Char('\n'));
127127
}
128128
return ret;
129129
}
@@ -133,7 +133,7 @@ namespace SDDM {
133133
if (!QFile::exists(m_path))
134134
return;
135135

136-
QString currentSection = IMPLICIT_SECTION;
136+
QString currentSection = QStringLiteral(IMPLICIT_SECTION);
137137

138138
QFile in(m_path);
139139
QDateTime modificationTime = QFileInfo(in).lastModified();
@@ -144,12 +144,12 @@ namespace SDDM {
144144

145145
in.open(QIODevice::ReadOnly);
146146
while (!in.atEnd()) {
147-
QString line = in.readLine().trimmed();
147+
QString line = QString::fromUtf8(in.readLine().trimmed());
148148
// get rid of comments first
149-
line = line.left(line.indexOf('#')).trimmed();
149+
line = line.left(line.indexOf(QLatin1Char('#'))).trimmed();
150150

151151
// value assignment
152-
int separatorPosition = line.indexOf('=');
152+
int separatorPosition = line.indexOf(QLatin1Char('='));
153153
if (separatorPosition >= 0) {
154154
QString name = line.left(separatorPosition).trimmed();
155155
QString value = line.mid(separatorPosition + 1).trimmed();
@@ -161,7 +161,7 @@ namespace SDDM {
161161
m_unusedVariables = true;
162162
}
163163
// section start
164-
else if (line.startsWith('[') && line.endsWith(']'))
164+
else if (line.startsWith(QLatin1Char('[')) && line.endsWith(QLatin1Char(']')))
165165
currentSection = line.mid(1, line.length() - 2);
166166
}
167167
}
@@ -197,7 +197,7 @@ namespace SDDM {
197197
}
198198

199199
// initialize the current section - General, usually
200-
const ConfigSection *currentSection = m_sections[IMPLICIT_SECTION];
200+
const ConfigSection *currentSection = m_sections[QStringLiteral(IMPLICIT_SECTION)];
201201

202202
// stuff to store the pre-section stuff (comments) to the start of the right section, not the end of the previous one
203203
QByteArray junk;
@@ -221,15 +221,15 @@ namespace SDDM {
221221
QFile file(m_path);
222222
file.open(QIODevice::ReadOnly); // first just for reading
223223
while (!file.atEnd()) {
224-
QString line = file.readLine();
224+
QString line = QString::fromUtf8(file.readLine());
225225
// get rid of comments first
226-
QString trimmedLine = line.left(line.indexOf('#')).trimmed();
226+
QString trimmedLine = line.left(line.indexOf(QLatin1Char('#'))).trimmed();
227227
QString comment;
228-
if (line.indexOf('#') >= 0)
229-
comment = line.mid(line.indexOf('#')).trimmed();
228+
if (line.indexOf(QLatin1Char('#')) >= 0)
229+
comment = line.mid(line.indexOf(QLatin1Char('#'))).trimmed();
230230

231231
// value assignment
232-
int separatorPosition = trimmedLine.indexOf('=');
232+
int separatorPosition = trimmedLine.indexOf(QLatin1Char('='));
233233
if (separatorPosition >= 0) {
234234
QString name = trimmedLine.left(separatorPosition).trimmed();
235235
QString value = trimmedLine.mid(separatorPosition + 1).trimmed();
@@ -240,7 +240,7 @@ namespace SDDM {
240240
(!entry && section && section->name() == currentSection->name()) ||
241241
value != currentSection->entry(name)->value()) {
242242
changed = true;
243-
writeSectionData(QString("%1=%2 %3\n").arg(name).arg(currentSection->entry(name)->value()).arg(comment));
243+
writeSectionData(QStringLiteral("%1=%2 %3\n").arg(name).arg(currentSection->entry(name)->value()).arg(comment));
244244
}
245245
else
246246
writeSectionData(line);
@@ -249,12 +249,12 @@ namespace SDDM {
249249
else {
250250
if (currentSection)
251251
m_unusedVariables = true;
252-
writeSectionData(QString("%1 %2\n").arg(trimmedLine).arg(UNUSED_VARIABLE_COMMENT));
252+
writeSectionData(QStringLiteral("%1 %2\n").arg(trimmedLine).arg(QStringLiteral(UNUSED_VARIABLE_COMMENT)));
253253
}
254254
}
255255

256256
// section start
257-
else if (trimmedLine.startsWith('[') && trimmedLine.endsWith(']')) {
257+
else if (trimmedLine.startsWith(QLatin1Char('[')) && trimmedLine.endsWith(QLatin1Char(']'))) {
258258
QString name = trimmedLine.mid(1, trimmedLine.length() - 2);
259259
if (m_sections.contains(name)) {
260260
currentSection = m_sections[name];
@@ -270,7 +270,7 @@ namespace SDDM {
270270

271271
// other stuff, like comments and whatnot
272272
else {
273-
if (line != UNUSED_SECTION_COMMENT)
273+
if (line != QStringLiteral(UNUSED_SECTION_COMMENT))
274274
collectJunk(line);
275275
}
276276
}
@@ -281,7 +281,7 @@ namespace SDDM {
281281
currentSection = it.key();
282282
if (!sectionOrder.contains(currentSection))
283283
writeSectionData(currentSection->toConfigShort());
284-
writeSectionData("\n");
284+
writeSectionData(QStringLiteral("\n"));
285285
writeSectionData(it.value()->toConfigFull());
286286
}
287287

src/common/ConfigReader.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
#define Config(name, file, ...) \
4141
class name : public SDDM::ConfigBase, public SDDM::ConfigSection { \
4242
public: \
43-
name() : SDDM::ConfigBase(file), SDDM::ConfigSection(this, IMPLICIT_SECTION) { \
43+
name() : SDDM::ConfigBase(file), SDDM::ConfigSection(this, QStringLiteral(IMPLICIT_SECTION)) { \
4444
load(); \
4545
} \
4646
void save() { SDDM::ConfigBase::save(nullptr, nullptr); } \
@@ -52,14 +52,14 @@
5252
}
5353
// entry wrapper
5454
#define Entry(name, type, default, description) \
55-
SDDM::ConfigEntry<type> name { this, #name, (default), (description) }
55+
SDDM::ConfigEntry<type> name { this, QStringLiteral(#name), (default), (description) }
5656
// section wrapper
5757
#define Section(name, ...) \
5858
class name : public SDDM::ConfigSection { \
5959
public: \
6060
name (SDDM::ConfigBase *_parent, const QString &_name) : SDDM::ConfigSection(_parent, _name) { } \
6161
__VA_ARGS__ \
62-
} name { this, #name };
62+
} name { this, QStringLiteral(#name) };
6363

6464
QTextStream &operator>>(QTextStream &str, QStringList &list);
6565
QTextStream &operator<<(QTextStream &str, const QStringList &list);
@@ -153,14 +153,14 @@ namespace SDDM {
153153
}
154154

155155
QString toConfigShort() const {
156-
return QString("%1=%2").arg(m_name).arg(value());
156+
return QStringLiteral("%1=%2").arg(m_name).arg(value());
157157
}
158158

159159
QString toConfigFull() const {
160160
QString str;
161-
for (const QString &line : m_description.split('\n'))
162-
str.append(QString("# %1\n").arg(line));
163-
str.append(QString("%1=%2\n\n").arg(m_name).arg(value()));
161+
for (const QString &line : m_description.split(QLatin1Char('\n')))
162+
str.append(QStringLiteral("# %1\n").arg(line));
163+
str.append(QStringLiteral("%1=%2\n\n").arg(m_name).arg(value()));
164164
return str;
165165
}
166166
private:

src/common/Configuration.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
namespace SDDM {
3636
// Name File Sections and/or Entries (but anything else too, it's a class) - Entries in a Config are assumed to be in the General section
37-
Config(MainConfig, CONFIG_FILE,
37+
Config(MainConfig, QStringLiteral(CONFIG_FILE),
3838
enum NumState { NUM_NONE, NUM_SET_ON, NUM_SET_OFF };
3939

4040
// Name Type Default value Description
@@ -89,7 +89,7 @@ namespace SDDM {
8989
);
9090
);
9191

92-
Config(StateConfig, []()->QString{auto tmp = getpwnam("sddm"); return tmp ? tmp->pw_dir : STATE_DIR;}().append("/state.conf"),
92+
Config(StateConfig, []()->QString{auto tmp = getpwnam("sddm"); return tmp ? QString::fromLocal8Bit(tmp->pw_dir) : QStringLiteral(STATE_DIR);}().append(QStringLiteral("/state.conf")),
9393
Section(Last,
9494
Entry(Session, QString, QString(), _S("Name of the session file of the last session selected. This session will be preselected when the login screen shows up."));
9595
Entry(User, QString, QString(), _S("Name of the last logged-in user. This username will be preselected/shown when the login screen shows up"));
@@ -101,9 +101,9 @@ namespace SDDM {
101101

102102
inline QTextStream& operator>>(QTextStream &str, MainConfig::NumState &state) {
103103
QString text = str.readLine().trimmed();
104-
if (text.compare("on", Qt::CaseInsensitive) == 0)
104+
if (text.compare(QLatin1String("on"), Qt::CaseInsensitive) == 0)
105105
state = MainConfig::NUM_SET_ON;
106-
else if (text.compare("off", Qt::CaseInsensitive) == 0)
106+
else if (text.compare(QLatin1String("off"), Qt::CaseInsensitive) == 0)
107107
state = MainConfig::NUM_SET_OFF;
108108
else
109109
state = MainConfig::NUM_NONE;

src/common/MessageHandler.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ namespace SDDM {
6767
#endif
6868

6969
static void standardLogger(QtMsgType type, const QString &msg) {
70-
static QFile file(LOG_FILE);
70+
static QFile file(QStringLiteral(LOG_FILE));
7171

7272
// try to open file only if it's not already open
7373
if (!file.isOpen()) {
@@ -76,26 +76,26 @@ namespace SDDM {
7676
}
7777

7878
// create timestamp
79-
QString timestamp = QDateTime::currentDateTime().toString("hh:mm:ss.zzz");
79+
QString timestamp = QDateTime::currentDateTime().toString(QStringLiteral("hh:mm:ss.zzz"));
8080

8181
// set log priority
82-
QString logPriority = QString("(II)");
82+
QString logPriority = QStringLiteral("(II)");
8383
switch (type) {
8484
case QtDebugMsg:
8585
break;
8686
case QtWarningMsg:
87-
logPriority = QString("(WW)");
87+
logPriority = QStringLiteral("(WW)");
8888
break;
8989
case QtCriticalMsg:
9090
case QtFatalMsg:
91-
logPriority = QString("(EE)");
91+
logPriority = QStringLiteral("(EE)");
9292
break;
9393
default:
9494
break;
9595
}
9696

9797
// prepare log message
98-
QString logMessage = QString("[%1] %2 %3\n").arg(timestamp).arg(logPriority).arg(msg);
98+
QString logMessage = QStringLiteral("[%1] %2 %3\n").arg(timestamp).arg(logPriority).arg(msg);
9999

100100
// log message
101101
if (file.isOpen()) {
@@ -135,15 +135,15 @@ namespace SDDM {
135135
}
136136

137137
void DaemonMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) {
138-
messageHandler(type, context, "DAEMON: ", msg);
138+
messageHandler(type, context, QStringLiteral("DAEMON: "), msg);
139139
}
140140

141141
void HelperMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) {
142-
messageHandler(type, context, "HELPER: ", msg);
142+
messageHandler(type, context, QStringLiteral("HELPER: "), msg);
143143
}
144144

145145
void GreeterMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) {
146-
messageHandler(type, context, "GREETER: ", msg);
146+
messageHandler(type, context, QStringLiteral("GREETER: "), msg);
147147
}
148148
}
149149

0 commit comments

Comments
 (0)