Skip to content

Commit 1cff895

Browse files
authored
refs #13914 - apply enforced language for non-project inputs in GUI (danmar#7627)
1 parent bc2755d commit 1cff895

File tree

12 files changed

+156
-80
lines changed

12 files changed

+156
-80
lines changed

cli/cmdlineparser.cpp

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -293,40 +293,7 @@ bool CmdLineParser::fillSettingsFromArgs(int argc, const char* const argv[])
293293
files = std::move(filesResolved);
294294
}
295295

296-
if (mEnforcedLang != Standards::Language::None)
297-
{
298-
// apply enforced language
299-
for (auto& f : files)
300-
{
301-
if (mSettings.library.markupFile(f.path()))
302-
continue;
303-
f.setLang(mEnforcedLang);
304-
}
305-
}
306-
else
307-
{
308-
// identify remaining files
309-
for (auto& f : files)
310-
{
311-
if (f.lang() != Standards::Language::None)
312-
continue;
313-
if (mSettings.library.markupFile(f.path()))
314-
continue;
315-
bool header = false;
316-
f.setLang(Path::identify(f.path(), mSettings.cppHeaderProbe, &header));
317-
// unknown extensions default to C++
318-
if (!header && f.lang() == Standards::Language::None)
319-
f.setLang(Standards::Language::CPP);
320-
}
321-
}
322-
323-
// enforce the language since markup files are special and do not adhere to the enforced language
324-
for (auto& f : files)
325-
{
326-
if (mSettings.library.markupFile(f.path())) {
327-
f.setLang(Standards::Language::C);
328-
}
329-
}
296+
frontend::applyLang(files, mSettings, mEnforcedLang);
330297

331298
// sort the markup last
332299
std::copy_if(files.cbegin(), files.cend(), std::inserter(mFiles, mFiles.end()), [&](const FileWithDetails& entry) {

frontend/frontend.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,42 @@ namespace frontend {
6363
}
6464
}
6565
}
66+
67+
void applyLang(std::list<FileWithDetails>& files, const Settings& settings, Standards::Language enforcedLang)
68+
{
69+
if (enforcedLang != Standards::Language::None)
70+
{
71+
// apply enforced language
72+
for (auto& f : files)
73+
{
74+
if (settings.library.markupFile(f.path()))
75+
continue;
76+
f.setLang(enforcedLang);
77+
}
78+
}
79+
else
80+
{
81+
// identify remaining files
82+
for (auto& f : files)
83+
{
84+
if (f.lang() != Standards::Language::None)
85+
continue;
86+
if (settings.library.markupFile(f.path()))
87+
continue;
88+
bool header = false;
89+
f.setLang(Path::identify(f.path(), settings.cppHeaderProbe, &header));
90+
// unknown extensions default to C++
91+
if (!header && f.lang() == Standards::Language::None)
92+
f.setLang(Standards::Language::CPP);
93+
}
94+
}
95+
96+
// enforce the language since markup files are special and do not adhere to the enforced language
97+
for (auto& f : files)
98+
{
99+
if (settings.library.markupFile(f.path())) {
100+
f.setLang(Standards::Language::C);
101+
}
102+
}
103+
}
66104
}

frontend/frontend.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@
2525

2626
struct FileSettings;
2727
class Settings;
28+
class FileWithDetails;
2829

2930
namespace frontend
3031
{
3132
/**
3233
Applies the enforced language as all as identifying remaining files - also taking markup files into consideration.
3334
*/
3435
void applyLang(std::list<FileSettings> &fileSettings, const Settings &settings, Standards::Language enforcedLang);
36+
void applyLang(std::list<FileWithDetails> &files, const Settings &settings, Standards::Language enforcedLang);
3537
}
3638

3739
#endif // FRONTEND_H

gui/checkthread.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "errorlogger.h"
2626
#include "errortypes.h"
2727
#include "filesettings.h"
28-
#include "path.h"
2928
#include "settings.h"
3029
#include "standards.h"
3130
#include "threadresult.h"
@@ -121,7 +120,7 @@ void CheckThread::setSettings(const Settings &settings, std::shared_ptr<Suppress
121120
mSuppressions = std::move(supprs);
122121
}
123122

124-
void CheckThread::analyseWholeProgram(const QStringList &files, const std::string& ctuInfo)
123+
void CheckThread::analyseWholeProgram(const std::list<FileWithDetails> &files, const std::string& ctuInfo)
125124
{
126125
mFiles = files;
127126
mAnalyseWholeProgram = true;
@@ -136,17 +135,12 @@ void CheckThread::run()
136135

137136
CppCheck cppcheck(mSettings, *mSuppressions, mResult, true, executeCommand);
138137

139-
if (!mFiles.isEmpty() || mAnalyseWholeProgram) {
138+
if (!mFiles.empty() || mAnalyseWholeProgram) {
140139
mAnalyseWholeProgram = false;
141140
std::string ctuInfo;
142141
ctuInfo.swap(mCtuInfo);
143142
qDebug() << "Whole program analysis";
144-
std::list<FileWithDetails> files2;
145-
std::transform(mFiles.cbegin(), mFiles.cend(), std::back_inserter(files2), [&](const QString& file) {
146-
// TODO: apply enforcedLanguage
147-
return FileWithDetails{file.toStdString(), Path::identify(file.toStdString(), mSettings.cppHeaderProbe), 0};
148-
});
149-
cppcheck.analyseWholeProgram(mSettings.buildDir, files2, {}, ctuInfo);
143+
cppcheck.analyseWholeProgram(mSettings.buildDir, mFiles, {}, ctuInfo);
150144
mFiles.clear();
151145
emit done();
152146
return;

gui/checkthread.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
#ifndef CHECKTHREAD_H
2121
#define CHECKTHREAD_H
2222

23+
#include "filesettings.h"
2324
#include "settings.h"
2425
#include "suppressions.h"
2526

2627
#include <atomic>
2728
#include <cstdint>
29+
#include <list>
2830
#include <memory>
2931
#include <string>
3032
#include <vector>
@@ -36,7 +38,6 @@
3638
#include <QThread>
3739

3840
class ThreadResult;
39-
struct FileSettings;
4041

4142
/// @addtogroup GUI
4243
/// @{
@@ -63,7 +64,7 @@ class CheckThread : public QThread {
6364
* @param files All files
6465
* @param ctuInfo Ctu info for addons
6566
*/
66-
void analyseWholeProgram(const QStringList &files, const std::string& ctuInfo);
67+
void analyseWholeProgram(const std::list<FileWithDetails> &files, const std::string& ctuInfo);
6768

6869
void setAddonsAndTools(const QStringList &addonsAndTools) {
6970
mAddonsAndTools = addonsAndTools;
@@ -142,7 +143,7 @@ class CheckThread : public QThread {
142143

143144
bool isSuppressed(const SuppressionList::ErrorMessage &errorMessage) const;
144145

145-
QStringList mFiles;
146+
std::list<FileWithDetails> mFiles;
146147
bool mAnalyseWholeProgram{};
147148
std::string mCtuInfo;
148149
QStringList mAddonsAndTools;

gui/mainwindow.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
#include "frontend.h"
5858

5959
#include <algorithm>
60+
#include <cstddef>
61+
#include <fstream>
6062
#include <iterator>
6163
#include <list>
6264
#include <memory>
@@ -661,9 +663,11 @@ void MainWindow::doAnalyzeFiles(const QStringList &files, const bool checkLibrar
661663
return;
662664
}
663665

664-
mUI->mResults->checkingStarted(fileNames.count());
666+
std::list<FileWithDetails> fdetails = enrichFilesForAnalysis(fileNames, checkSettings);
665667

666-
mThread->setFiles(fileNames);
668+
// TODO: lock UI here?
669+
mUI->mResults->checkingStarted(fdetails.size());
670+
mThread->setFiles(std::move(fdetails));
667671
if (mProjectFile && !checkConfiguration)
668672
mThread->setAddonsAndTools(mProjectFile->getAddonsAndTools());
669673
mThread->setSuppressions(mProjectFile ? mProjectFile->getCheckingSuppressions() : QList<SuppressionList::Suppression>());
@@ -701,9 +705,9 @@ void MainWindow::analyzeCode(const QString& code, const QString& filename)
701705
if (!getCppcheckSettings(checkSettings, supprs))
702706
return;
703707

708+
// TODO: split ErrorLogger from ThreadResult
704709
// Initialize dummy ThreadResult as ErrorLogger
705710
ThreadResult result;
706-
result.setFiles(QStringList(filename));
707711
connect(&result, SIGNAL(progress(int,QString)),
708712
mUI->mResults, SLOT(progress(int,QString)));
709713
connect(&result, SIGNAL(error(ErrorItem)),
@@ -720,7 +724,7 @@ void MainWindow::analyzeCode(const QString& code, const QString& filename)
720724
checkLockDownUI();
721725
clearResults();
722726
mUI->mResults->checkingStarted(1);
723-
// TODO: apply enforcedLanguage
727+
// TODO: apply enforcedLanguage?
724728
cppcheck.check(FileWithDetails(filename.toStdString(), Path::identify(filename.toStdString(), false), 0), code.toStdString());
725729
analysisDone();
726730

@@ -1380,10 +1384,12 @@ void MainWindow::reAnalyzeSelected(const QStringList& files)
13801384
pathList.addPathList(files);
13811385
if (mProjectFile)
13821386
pathList.addExcludeList(mProjectFile->getExcludedPaths());
1383-
QStringList fileNames = pathList.getFileList();
1387+
1388+
std::list<FileWithDetails> fdetails = enrichFilesForAnalysis(pathList.getFileList(), checkSettings);
1389+
13841390
checkLockDownUI(); // lock UI while checking
1385-
mUI->mResults->checkingStarted(fileNames.size());
1386-
mThread->setCheckFiles(fileNames);
1391+
mUI->mResults->checkingStarted(fdetails.size());
1392+
mThread->setCheckFiles(std::move(fdetails));
13871393

13881394
// Saving last check start time, otherwise unchecked modified files will not be
13891395
// considered in "Modified Files Check" performed after "Selected Files Check"
@@ -1396,7 +1402,7 @@ void MainWindow::reAnalyzeSelected(const QStringList& files)
13961402

13971403
void MainWindow::reAnalyze(bool all)
13981404
{
1399-
const QStringList files = mThread->getReCheckFiles(all);
1405+
const std::list<FileWithDetails> files = mThread->getReCheckFiles(all);
14001406
if (files.empty())
14011407
return;
14021408

@@ -1409,8 +1415,8 @@ void MainWindow::reAnalyze(bool all)
14091415
mUI->mResults->clear(all);
14101416

14111417
// Clear results for changed files
1412-
for (int i = 0; i < files.size(); ++i)
1413-
mUI->mResults->clear(files[i]);
1418+
for (const auto& f : files)
1419+
mUI->mResults->clear(QString::fromStdString(f.path()));
14141420

14151421
checkLockDownUI(); // lock UI while checking
14161422
mUI->mResults->checkingStarted(files.size());
@@ -2349,3 +2355,12 @@ void MainWindow::changeReportType() {
23492355
}
23502356
}
23512357

2358+
std::list<FileWithDetails> MainWindow::enrichFilesForAnalysis(const QStringList& fileNames, const Settings& settings) const {
2359+
std::list<FileWithDetails> fdetails;
2360+
std::transform(fileNames.cbegin(), fileNames.cend(), std::back_inserter(fdetails), [](const QString& f) {
2361+
return FileWithDetails{f.toStdString(), Standards::Language::None, static_cast<std::size_t>(QFile(f).size())};
2362+
});
2363+
const Standards::Language enforcedLang = static_cast<Standards::Language>(mSettings->value(SETTINGS_ENFORCED_LANGUAGE, 0).toInt());
2364+
frontend::applyLang(fdetails, settings, enforcedLang);
2365+
return fdetails;
2366+
}

gui/mainwindow.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "platforms.h"
2424

2525
#include <cstdint>
26+
#include <list>
2627

2728
#include <QFileDialog>
2829
#include <QMainWindow>
@@ -45,6 +46,7 @@ class QNetworkAccessManager;
4546
class QNetworkReply;
4647
class Settings;
4748
struct Suppressions;
49+
class FileWithDetails;
4850
namespace Ui {
4951
class MainWindow;
5052
}
@@ -423,6 +425,9 @@ private slots:
423425
*/
424426
void removeProjectMRU(const QString &project);
425427

428+
/** @brief Generate list of detailed files from list of filenames. */
429+
std::list<FileWithDetails> enrichFilesForAnalysis(const QStringList& fileNames, const Settings& settings) const;
430+
426431
/** @brief Program settings */
427432
QSettings *mSettings;
428433

gui/threadhandler.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "checkthread.h"
2222
#include "common.h"
23+
#include "filesettings.h"
2324
#include "resultsview.h"
2425
#include "settings.h"
2526

@@ -55,10 +56,10 @@ void ThreadHandler::clearFiles()
5556
mSuppressionsUI.clear();
5657
}
5758

58-
void ThreadHandler::setFiles(const QStringList &files)
59+
void ThreadHandler::setFiles(std::list<FileWithDetails> files)
5960
{
60-
mResults.setFiles(files);
6161
mLastFiles = files;
62+
mResults.setFiles(std::move(files));
6263
}
6364

6465
void ThreadHandler::setProject(const ImportProject &prj)
@@ -74,10 +75,10 @@ void ThreadHandler::setCheckFiles(bool all)
7475
}
7576
}
7677

77-
void ThreadHandler::setCheckFiles(const QStringList& files)
78+
void ThreadHandler::setCheckFiles(std::list<FileWithDetails> files)
7879
{
7980
if (mRunningThreadCount == 0) {
80-
mResults.setFiles(files);
81+
mResults.setFiles(std::move(files));
8182
}
8283
}
8384

@@ -172,6 +173,7 @@ void ThreadHandler::threadDone()
172173
{
173174
mRunningThreadCount--;
174175

176+
// TODO: also run with projects?
175177
if (mRunningThreadCount == 0 && mAnalyseWholeProgram) {
176178
createThreads(1);
177179
mRunningThreadCount = 1;
@@ -235,7 +237,7 @@ void ThreadHandler::saveSettings(QSettings &settings) const
235237

236238
bool ThreadHandler::hasPreviousFiles() const
237239
{
238-
return !mLastFiles.isEmpty();
240+
return !mLastFiles.empty();
239241
}
240242

241243
int ThreadHandler::getPreviousFilesCount() const
@@ -248,19 +250,18 @@ int ThreadHandler::getPreviousScanDuration() const
248250
return mScanDuration;
249251
}
250252

251-
QStringList ThreadHandler::getReCheckFiles(bool all) const
253+
std::list<FileWithDetails> ThreadHandler::getReCheckFiles(bool all) const
252254
{
253255
if (mLastCheckTime.isNull() || all)
254256
return mLastFiles;
255257

256258
std::set<QString> modified;
257259
std::set<QString> unmodified;
258260

259-
QStringList files;
260-
for (int i = 0; i < mLastFiles.size(); ++i) {
261-
if (needsReCheck(mLastFiles[i], modified, unmodified))
262-
files.push_back(mLastFiles[i]);
263-
}
261+
std::list<FileWithDetails> files;
262+
std::copy_if(mLastFiles.cbegin(), mLastFiles.cend(), std::back_inserter(files), [&](const FileWithDetails &f) {
263+
return needsReCheck(QString::fromStdString(f.path()), modified, unmodified);
264+
});
264265
return files;
265266
}
266267

0 commit comments

Comments
 (0)