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 Jan 14, 2025
1 parent 187e7e3 commit 239afa0
Show file tree
Hide file tree
Showing 102 changed files with 973 additions and 954 deletions.
1 change: 1 addition & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Checks: "-*
, google-explicit-constructor
, misc-const-correctness
, modernize-avoid-bind
, modernize-deprecated-headers
, modernize-loop-convert
Expand Down
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 @@ -160,7 +160,7 @@ void OpenAL::checkAlcError(ALCdevice* device) noexcept
*/
void OpenAL::setOutputVolume(qreal volume)
{
QMutexLocker<QRecursiveMutex> locker(&audioLock);
const QMutexLocker<QRecursiveMutex> locker(&audioLock);

alListenerf(AL_GAIN, static_cast<ALfloat>(std::max(0.0, std::min(volume, 1.0))));
checkAlError();
Expand All @@ -173,7 +173,7 @@ void OpenAL::setOutputVolume(qreal volume)
*/
qreal OpenAL::minInputGain() const
{
QMutexLocker<QRecursiveMutex> locker(&audioLock);
const QMutexLocker<QRecursiveMutex> locker(&audioLock);
return minInGain;
}

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

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

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

Expand Down Expand Up @@ -255,7 +255,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 @@ -283,7 +283,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 @@ -316,7 +316,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 @@ -339,7 +339,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 @@ -393,7 +393,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 @@ -480,7 +480,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 @@ -508,12 +508,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 @@ -530,7 +530,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 @@ -688,7 +688,7 @@ void OpenAL::doOutput()
*/
void OpenAL::doAudio()
{
QMutexLocker<QRecursiveMutex> lock(&audioLock);
const QMutexLocker<QRecursiveMutex> lock(&audioLock);

// Output section does nothing

Expand All @@ -708,7 +708,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 @@ -767,13 +767,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
15 changes: 8 additions & 7 deletions src/appmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,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 logPrefix =
QStringLiteral("[%1 UTC] (%2) %3:%4 : ")
.arg(time.toString("HH:mm:ss.zzz"), category, file, QString::number(ctxt.line));
Expand Down Expand Up @@ -156,8 +156,9 @@ void logMessageHandler(QtMsgType type, const QMessageLogContext& ctxt, const QSt
logBufferMutex->lock();
if (logBuffer) {
// empty logBuffer to file
foreach (QByteArray bufferedMsg, *logBuffer)
for (const QByteArray& bufferedMsg : *logBuffer) {
fwrite(bufferedMsg.constData(), 1, bufferedMsg.size(), logFilePtr);
}

delete logBuffer; // no longer needed
logBuffer = nullptr;
Expand Down Expand Up @@ -209,10 +210,10 @@ int AppManager::startGui(QCommandLineParser& parser)
}

#ifdef LOG_TO_FILE
QString logFileDir = settings->getPaths().getAppCacheDirPath();
const QString 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 @@ -295,7 +296,7 @@ int AppManager::startGui(QCommandLineParser& parser)
}

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 @@ -335,7 +336,7 @@ int AppManager::startGui(QCommandLineParser& parser)
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 Expand Up @@ -394,7 +395,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 explicitly.
// See https://doc.qt.io/qt-5/resources.html#using-resources-in-a-library
Q_INIT_RESOURCE(translations);
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 @@ -58,7 +58,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 @@ -80,14 +80,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 @@ -134,7 +135,7 @@ ChatMessage::Ptr ChatMessage::createChatInfoMessage(const QString& rawMessage,
Settings& settings, Style& style)
{
ChatMessage::Ptr msg = std::make_shared<ChatMessage>(documentCache, settings, style);
QString text = rawMessage.toHtmlEscaped();
const QString text = rawMessage.toHtmlEscaped();

QString img;
switch (type) {
Expand All @@ -149,7 +150,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 @@ -170,7 +171,7 @@ ChatMessage::Ptr ChatMessage::createFileTransferMessage(const QString& sender, C
{
ChatMessage::Ptr msg = std::make_shared<ChatMessage>(documentCache, settings, style);

QFont baseFont = settings.getChatMessageFont();
const QFont baseFont = settings.getChatMessageFont();
QFont authorFont = baseFont;
if (isMe) {
authorFont.setBold(true);
Expand All @@ -194,7 +195,7 @@ ChatMessage::Ptr ChatMessage::createTypingNotification(DocumentCache& documentCa
{
ChatMessage::Ptr msg = std::make_shared<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 @@ -238,7 +239,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 239afa0

Please sign in to comment.