Skip to content

Commit

Permalink
cleanup: Add const to local variables where possible.
Browse files Browse the repository at this point in the history
To reproduce:
```
run-clang-tidy \
  -checks="-*,misc-const-correctness" \
  -p _build \
  -fix \
  $(find . -name "*.h" -or -name "*.cpp" | grep -v "/_build/")
```
  • Loading branch information
iphydf committed Nov 17, 2024
1 parent b699420 commit 2289ced
Show file tree
Hide file tree
Showing 114 changed files with 1,106 additions and 1,091 deletions.
14 changes: 7 additions & 7 deletions audio/src/backend/alsink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

AlSink::~AlSink()
{
QMutexLocker<QRecursiveMutex> locker{&killLock};
const QMutexLocker<QRecursiveMutex> locker{&killLock};

// unsubscribe only if not already killed
if (!killed) {
Expand All @@ -27,7 +27,7 @@ AlSink::~AlSink()

void AlSink::playAudioBuffer(const int16_t* data, int samples, unsigned channels, int sampleRate) const
{
QMutexLocker<QRecursiveMutex> locker{&killLock};
const QMutexLocker<QRecursiveMutex> locker{&killLock};

if (killed) {
qCritical() << "Trying to play audio on an invalid sink";
Expand All @@ -38,7 +38,7 @@ void AlSink::playAudioBuffer(const int16_t* data, int samples, unsigned channels

void AlSink::playMono16Sound(const IAudioSink::Sound& sound)
{
QMutexLocker<QRecursiveMutex> locker{&killLock};
const QMutexLocker<QRecursiveMutex> locker{&killLock};

if (killed) {
qCritical() << "Trying to play sound on an invalid sink";
Expand All @@ -49,7 +49,7 @@ void AlSink::playMono16Sound(const IAudioSink::Sound& sound)

void AlSink::startLoop()
{
QMutexLocker<QRecursiveMutex> locker{&killLock};
const QMutexLocker<QRecursiveMutex> locker{&killLock};

if (killed) {
qCritical() << "Trying to start loop on an invalid sink";
Expand All @@ -60,7 +60,7 @@ void AlSink::startLoop()

void AlSink::stopLoop()
{
QMutexLocker<QRecursiveMutex> locker{&killLock};
const QMutexLocker<QRecursiveMutex> locker{&killLock};

if (killed) {
qCritical() << "Trying to stop loop on an invalid sink";
Expand All @@ -71,7 +71,7 @@ void AlSink::stopLoop()

uint AlSink::getSourceId() const
{
uint tmp = sourceId;
const uint tmp = sourceId;
return tmp;
}

Expand All @@ -92,7 +92,7 @@ AlSink::AlSink(OpenAL& al, uint sourceId_)

AlSink::operator bool() const
{
QMutexLocker<QRecursiveMutex> locker{&killLock};
const QMutexLocker<QRecursiveMutex> locker{&killLock};

return !killed;
}
4 changes: 2 additions & 2 deletions audio/src/backend/alsource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ AlSource::AlSource(OpenAL& al)

AlSource::~AlSource()
{
QMutexLocker<QRecursiveMutex> locker{&killLock};
const QMutexLocker<QRecursiveMutex> locker{&killLock};

// unsubscribe only if not already killed
if (!killed) {
Expand All @@ -32,7 +32,7 @@ AlSource::~AlSource()

AlSource::operator bool() const
{
QMutexLocker<QRecursiveMutex> locker{&killLock};
const QMutexLocker<QRecursiveMutex> locker{&killLock};
return !killed;
}

Expand Down
36 changes: 18 additions & 18 deletions audio/src/backend/openal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ void OpenAL::checkAlcError(ALCdevice* device) noexcept
*/
void OpenAL::setOutputVolume(qreal volume)
{
QMutexLocker<QRecursiveMutex> locker(&audioLock);
const QMutexLocker<QRecursiveMutex> locker(&audioLock);

volume = std::max(0.0, std::min(volume, 1.0));

Expand All @@ -126,7 +126,7 @@ void OpenAL::setOutputVolume(qreal volume)
*/
qreal OpenAL::minInputGain() const
{
QMutexLocker<QRecursiveMutex> locker(&audioLock);
const QMutexLocker<QRecursiveMutex> locker(&audioLock);
return minInGain;
}

Expand All @@ -137,7 +137,7 @@ qreal OpenAL::minInputGain() const
*/
qreal OpenAL::maxInputGain() const
{
QMutexLocker<QRecursiveMutex> locker(&audioLock);
const QMutexLocker<QRecursiveMutex> locker(&audioLock);
return maxInGain;
}

Expand All @@ -148,7 +148,7 @@ qreal OpenAL::maxInputGain() const
*/
qreal OpenAL::minInputThreshold() const
{
QMutexLocker<QRecursiveMutex> locker(&audioLock);
const QMutexLocker<QRecursiveMutex> locker(&audioLock);
return minInThreshold;
}

Expand All @@ -159,7 +159,7 @@ qreal OpenAL::minInputThreshold() const
*/
qreal OpenAL::maxInputThreshold() const
{
QMutexLocker<QRecursiveMutex> locker(&audioLock);
const QMutexLocker<QRecursiveMutex> locker(&audioLock);
return maxInThreshold;
}

Expand Down Expand Up @@ -208,7 +208,7 @@ bool OpenAL::reinitOutput(const QString& outDevDesc)
*/
std::unique_ptr<IAudioSink> OpenAL::makeSink()
{
QMutexLocker<QRecursiveMutex> locker(&audioLock);
const QMutexLocker<QRecursiveMutex> locker(&audioLock);

if (!autoInitOutput()) {
qWarning("Failed to subscribe to audio output device.");
Expand Down Expand Up @@ -236,7 +236,7 @@ std::unique_ptr<IAudioSink> OpenAL::makeSink()
*/
void OpenAL::destroySink(AlSink& sink)
{
QMutexLocker<QRecursiveMutex> locker(&audioLock);
const QMutexLocker<QRecursiveMutex> locker(&audioLock);

const auto sinksErased = sinks.erase(&sink);
const auto soundSinksErased = soundSinks.erase(&sink);
Expand Down Expand Up @@ -269,7 +269,7 @@ void OpenAL::destroySink(AlSink& sink)
*/
std::unique_ptr<IAudioSource> OpenAL::makeSource()
{
QMutexLocker<QRecursiveMutex> locker(&audioLock);
const QMutexLocker<QRecursiveMutex> locker(&audioLock);

if (!autoInitInput()) {
qWarning("Failed to subscribe to audio input device.");
Expand All @@ -295,7 +295,7 @@ std::unique_ptr<IAudioSource> OpenAL::makeSource()
*/
void OpenAL::destroySource(AlSource& source)
{
QMutexLocker<QRecursiveMutex> locker(&audioLock);
const QMutexLocker<QRecursiveMutex> locker(&audioLock);

const auto s = sources.find(&source);
if (s == sources.end()) {
Expand Down Expand Up @@ -348,7 +348,7 @@ bool OpenAL::initInput(const QString& deviceName, uint32_t channels)

// TODO: Try to actually detect if our audio source is stereo
inputChannels = channels;
int stereoFlag = inputChannels == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
const int stereoFlag = inputChannels == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
const int bytesPerSample = 2;
const int safetyFactor = 2; // internal OpenAL ring buffer. must be larger than our inputBuffer
// to avoid the ring from overwriting itself between captures.
Expand Down Expand Up @@ -435,7 +435,7 @@ void OpenAL::playMono16Sound(AlSink& sink, const IAudioSink::Sound& sound)
return;
}

QMutexLocker<QRecursiveMutex> locker(&audioLock);
const QMutexLocker<QRecursiveMutex> locker(&audioLock);

// interrupt possibly playing sound, we don't buffer here
alSourceStop(sourceId);
Expand Down Expand Up @@ -463,12 +463,12 @@ void OpenAL::playMono16Sound(AlSink& sink, const IAudioSink::Sound& sound)

void OpenAL::cleanupSound()
{
QMutexLocker<QRecursiveMutex> locker(&audioLock);
const QMutexLocker<QRecursiveMutex> locker(&audioLock);

auto sinkIt = soundSinks.begin();
while (sinkIt != soundSinks.end()) {
auto sink = *sinkIt;
ALuint sourceId = sink->getSourceId();
const ALuint sourceId = sink->getSourceId();
ALint state = 0;

alGetSourcei(sourceId, AL_SOURCE_STATE, &state);
Expand All @@ -485,7 +485,7 @@ void OpenAL::playAudioBuffer(uint sourceId, const int16_t* data, int samples, un
int sampleRate)
{
assert(channels == 1 || channels == 2);
QMutexLocker<QRecursiveMutex> locker(&audioLock);
const QMutexLocker<QRecursiveMutex> locker(&audioLock);

if (!(alOutDev && outputInitialized))
return;
Expand Down Expand Up @@ -643,7 +643,7 @@ void OpenAL::doOutput()
*/
void OpenAL::doAudio()
{
QMutexLocker<QRecursiveMutex> lock(&audioLock);
const QMutexLocker<QRecursiveMutex> lock(&audioLock);

// Output section does nothing

Expand All @@ -663,7 +663,7 @@ void OpenAL::captureSamples(ALCdevice* device, int16_t* buffer, ALCsizei samples
*/
bool OpenAL::isOutputReady() const
{
QMutexLocker<QRecursiveMutex> locker(&audioLock);
const QMutexLocker<QRecursiveMutex> locker(&audioLock);
return alOutDev && outputInitialized;
}

Expand Down Expand Up @@ -721,13 +721,13 @@ void OpenAL::cleanupBuffers(uint sourceId)

void OpenAL::startLoop(uint sourceId)
{
QMutexLocker<QRecursiveMutex> locker(&audioLock);
const QMutexLocker<QRecursiveMutex> locker(&audioLock);
alSourcei(sourceId, AL_LOOPING, AL_TRUE);
}

void OpenAL::stopLoop(uint sourceId)
{
QMutexLocker<QRecursiveMutex> locker(&audioLock);
const QMutexLocker<QRecursiveMutex> locker(&audioLock);
alSourcei(sourceId, AL_LOOPING, AL_FALSE);
alSourceStop(sourceId);
cleanupBuffers(sourceId);
Expand Down
16 changes: 8 additions & 8 deletions src/appmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void logMessageHandler(QtMsgType type, const QMessageLogContext& ctxt, const QSt
return;
}

QRegularExpression snoreFilter{QStringLiteral("Snore::Notification.*was already closed")};
const QRegularExpression snoreFilter{QStringLiteral("Snore::Notification.*was already closed")};
if (type == QtWarningMsg && msg.contains(snoreFilter)) {
// snorenotify logs this when we call requestCloseNotification correctly. The behaviour
// still works, so we'll just mask the warning for now. The issue has been reported
Expand All @@ -75,7 +75,7 @@ void logMessageHandler(QtMsgType type, const QMessageLogContext& ctxt, const QSt
}

// Time should be in UTC to save user privacy on log sharing
QTime time = QDateTime::currentDateTime().toUTC().time();
const QTime time = QDateTime::currentDateTime().toUTC().time();
QString LogMsg =
QString("[%1 UTC] %2:%3 : ").arg(time.toString("HH:mm:ss.zzz")).arg(file).arg(ctxt.line);
switch (type) {
Expand All @@ -99,7 +99,7 @@ void logMessageHandler(QtMsgType type, const QMessageLogContext& ctxt, const QSt
}

LogMsg += ": " + msg + "\n";
QByteArray LogMsgBytes = LogMsg.toUtf8();
const QByteArray LogMsgBytes = LogMsg.toUtf8();
fwrite(LogMsgBytes.constData(), 1, LogMsgBytes.size(), stderr);

#ifdef LOG_TO_FILE
Expand Down Expand Up @@ -178,7 +178,7 @@ int AppManager::run()
qWarning() << "Couldn't load font";
}

QString locale = settings->getTranslation();
const QString locale = settings->getTranslation();
// We need to init the resources in the translations_library explicitely.
// See https://doc.qt.io/qt-5/resources.html#using-resources-in-a-library
Q_INIT_RESOURCE(translations);
Expand Down Expand Up @@ -222,10 +222,10 @@ int AppManager::run()
}

#ifdef LOG_TO_FILE
QString logFileDir = settings->getPaths().getAppCacheDirPath();
QString const logFileDir = settings->getPaths().getAppCacheDirPath();
QDir(logFileDir).mkpath(".");

QString logfile = logFileDir + "qtox.log";
const QString logfile = logFileDir + "qtox.log";
FILE* mainLogFilePtr = fopen(logfile.toLocal8Bit().constData(), "a");

// Trim log file if over 1MB
Expand Down Expand Up @@ -304,7 +304,7 @@ int AppManager::run()
}

if (doIpc && !ipc->isCurrentOwner()) {
time_t event = ipc->postEvent(eventType, firstParam.toUtf8(), ipcDest);
const time_t event = ipc->postEvent(eventType, firstParam.toUtf8(), ipcDest);
// If someone else processed it, we're done here, no need to actually start qTox
if (ipc->waitUntilAccepted(event, 2)) {
if (eventType == "activate") {
Expand Down Expand Up @@ -344,7 +344,7 @@ int AppManager::run()
nexus->bootstrapWithProfile(profile);
} else {
nexus->setParser(&parser);
int returnval = nexus->showLogin(profileName);
const int returnval = nexus->showLogin(profileName);
if (returnval == QDialog::Rejected) {
return -1;
}
Expand Down
4 changes: 2 additions & 2 deletions src/chatlog/chatline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ void ChatLine::layout(qreal w, QPointF scenePos)
if (varWidth == 0.0)
varWidth = 1.0;

qreal leftover = qMax(0.0, width - fixedWidth);
const qreal leftover = qMax(0.0, width - fixedWidth);

qreal maxVOffset = 0.0;
qreal xOffset = 0.0;
Expand Down Expand Up @@ -207,7 +207,7 @@ void ChatLine::layout(qreal w, QPointF scenePos)
for (int i = 0; i < content.size(); ++i) {
// calculate vertical alignment
// vertical alignment may depend on width, so we do it in a second pass
qreal yOffset = maxVOffset - content[i]->getAscent();
const qreal yOffset = maxVOffset - content[i]->getAscent();

// reposition
content[i]->setPos(xPos[i], scenePos.y() + yOffset);
Expand Down
17 changes: 9 additions & 8 deletions src/chatlog/chatmessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ ChatMessage::Ptr ChatMessage::createChatMessage(const QString& sender, const QSt
text = TextFormatter::highlightURI(text);

// text styling
Settings::StyleType styleType = settings.getStylePreference();
const Settings::StyleType styleType = settings.getStylePreference();
if (styleType != Settings::StyleType::NONE) {
text = TextFormatter::applyMarkdown(text, styleType == Settings::StyleType::WITH_CHARS);
}
Expand All @@ -79,14 +79,15 @@ ChatMessage::Ptr ChatMessage::createChatMessage(const QString& sender, const QSt
}

// Note: Eliding cannot be enabled for RichText items. (QTBUG-17207)
QFont baseFont = settings.getChatMessageFont();
const QFont baseFont = settings.getChatMessageFont();
QFont authorFont = baseFont;
if (isMe)
authorFont.setBold(true);

QColor color = style.getColor(Style::ColorPalette::MainText);
if (colorizeName) {
QByteArray hash = QCryptographicHash::hash((sender.toUtf8()), QCryptographicHash::Sha256);
const QByteArray hash =
QCryptographicHash::hash((sender.toUtf8()), QCryptographicHash::Sha256);
auto lightness = color.lightnessF();
// Adapt as good as possible to Light/Dark themes
lightness = lightness * 0.5f + 0.3f;
Expand Down Expand Up @@ -133,7 +134,7 @@ ChatMessage::Ptr ChatMessage::createChatInfoMessage(const QString& rawMessage,
Settings& settings, Style& style)
{
ChatMessage::Ptr msg = ChatMessage::Ptr(new ChatMessage(documentCache, settings, style));
QString text = rawMessage.toHtmlEscaped();
const QString text = rawMessage.toHtmlEscaped();

QString img;
switch (type) {
Expand All @@ -148,7 +149,7 @@ ChatMessage::Ptr ChatMessage::createChatInfoMessage(const QString& rawMessage,
break;
}

QFont baseFont = settings.getChatMessageFont();
const QFont baseFont = settings.getChatMessageFont();

msg->addColumn(new Image(QSize(18, 18), img),
ColumnFormat(NAME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));
Expand All @@ -169,7 +170,7 @@ ChatMessage::Ptr ChatMessage::createFileTransferMessage(const QString& sender, C
{
ChatMessage::Ptr msg = ChatMessage::Ptr(new ChatMessage(documentCache, settings, style));

QFont baseFont = settings.getChatMessageFont();
const QFont baseFont = settings.getChatMessageFont();
QFont authorFont = baseFont;
if (isMe) {
authorFont.setBold(true);
Expand All @@ -193,7 +194,7 @@ ChatMessage::Ptr ChatMessage::createTypingNotification(DocumentCache& documentCa
{
ChatMessage::Ptr msg = ChatMessage::Ptr(new ChatMessage(documentCache, settings, style));

QFont baseFont = settings.getChatMessageFont();
const QFont baseFont = settings.getChatMessageFont();

// Note: "[user]..." is just a placeholder. The actual text is set in
// ChatForm::setFriendTyping()
Expand Down Expand Up @@ -237,7 +238,7 @@ ChatMessage::Ptr ChatMessage::createBusyNotification(DocumentCache& documentCach

void ChatMessage::markAsDelivered(const QDateTime& time)
{
QFont baseFont = settings.getChatMessageFont();
const QFont baseFont = settings.getChatMessageFont();

// remove the spinner and replace it by $time
replaceContent(2, new Timestamp(time, settings.getTimestampFormat(), baseFont, documentCache,
Expand Down
Loading

0 comments on commit 2289ced

Please sign in to comment.