Skip to content

Commit fb8ec70

Browse files
committed
refs #13914 - apply enforced language for non-project inputs in GUI
1 parent 129055d commit fb8ec70

12 files changed

Lines changed: 152 additions & 79 deletions

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: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -661,9 +661,11 @@ void MainWindow::doAnalyzeFiles(const QStringList &files, const bool checkLibrar
661661
return;
662662
}
663663

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

666-
mThread->setFiles(fileNames);
666+
// TODO: lock UI here?
667+
mUI->mResults->checkingStarted(fdetails.size());
668+
mThread->setFiles(std::move(fdetails));
667669
if (mProjectFile && !checkConfiguration)
668670
mThread->setAddonsAndTools(mProjectFile->getAddonsAndTools());
669671
mThread->setSuppressions(mProjectFile ? mProjectFile->getCheckingSuppressions() : QList<SuppressionList::Suppression>());
@@ -701,9 +703,9 @@ void MainWindow::analyzeCode(const QString& code, const QString& filename)
701703
if (!getCppcheckSettings(checkSettings, supprs))
702704
return;
703705

706+
// TODO: split ErrorLogger from ThreadResult
704707
// Initialize dummy ThreadResult as ErrorLogger
705708
ThreadResult result;
706-
result.setFiles(QStringList(filename));
707709
connect(&result, SIGNAL(progress(int,QString)),
708710
mUI->mResults, SLOT(progress(int,QString)));
709711
connect(&result, SIGNAL(error(ErrorItem)),
@@ -720,7 +722,7 @@ void MainWindow::analyzeCode(const QString& code, const QString& filename)
720722
checkLockDownUI();
721723
clearResults();
722724
mUI->mResults->checkingStarted(1);
723-
// TODO: apply enforcedLanguage
725+
// TODO: apply enforcedLanguage?
724726
cppcheck.check(FileWithDetails(filename.toStdString(), Path::identify(filename.toStdString(), false), 0), code.toStdString());
725727
analysisDone();
726728

@@ -1380,10 +1382,12 @@ void MainWindow::reAnalyzeSelected(const QStringList& files)
13801382
pathList.addPathList(files);
13811383
if (mProjectFile)
13821384
pathList.addExcludeList(mProjectFile->getExcludedPaths());
1383-
QStringList fileNames = pathList.getFileList();
1385+
1386+
std::list<FileWithDetails> fdetails = enrichFilesForAnalysis(pathList.getFileList(), checkSettings);
1387+
13841388
checkLockDownUI(); // lock UI while checking
1385-
mUI->mResults->checkingStarted(fileNames.size());
1386-
mThread->setCheckFiles(fileNames);
1389+
mUI->mResults->checkingStarted(fdetails.size());
1390+
mThread->setCheckFiles(std::move(fdetails));
13871391

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

13971401
void MainWindow::reAnalyze(bool all)
13981402
{
1399-
const QStringList files = mThread->getReCheckFiles(all);
1403+
const std::list<FileWithDetails> files = mThread->getReCheckFiles(all);
14001404
if (files.empty())
14011405
return;
14021406

@@ -1409,8 +1413,8 @@ void MainWindow::reAnalyze(bool all)
14091413
mUI->mResults->clear(all);
14101414

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

14151419
checkLockDownUI(); // lock UI while checking
14161420
mUI->mResults->checkingStarted(files.size());
@@ -2349,3 +2353,12 @@ void MainWindow::changeReportType() {
23492353
}
23502354
}
23512355

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

gui/mainwindow.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,9 @@ private slots:
423423
*/
424424
void removeProjectMRU(const QString &project);
425425

426+
/** @brief Generate list of detailed files from list of filenames. */
427+
std::list<FileWithDetails> enrichFilesForAnalysis(const QStringList& fileNames, const Settings& settings) const;
428+
426429
/** @brief Program settings */
427430
QSettings *mSettings;
428431

gui/threadhandler.cpp

Lines changed: 12 additions & 10 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

@@ -57,10 +58,10 @@ void ThreadHandler::clearFiles()
5758
mSuppressions.clear();
5859
}
5960

60-
void ThreadHandler::setFiles(const QStringList &files)
61+
void ThreadHandler::setFiles(std::list<FileWithDetails> files)
6162
{
62-
mResults.setFiles(files);
6363
mLastFiles = files;
64+
mResults.setFiles(std::move(files));
6465
}
6566

6667
void ThreadHandler::setProject(const ImportProject &prj)
@@ -76,10 +77,10 @@ void ThreadHandler::setCheckFiles(bool all)
7677
}
7778
}
7879

79-
void ThreadHandler::setCheckFiles(const QStringList& files)
80+
void ThreadHandler::setCheckFiles(std::list<FileWithDetails> files)
8081
{
8182
if (mRunningThreadCount == 0) {
82-
mResults.setFiles(files);
83+
mResults.setFiles(std::move(files));
8384
}
8485
}
8586

@@ -167,6 +168,7 @@ void ThreadHandler::removeThreads()
167168

168169
void ThreadHandler::threadDone()
169170
{
171+
// TODO: also run with projects?
170172
if (mRunningThreadCount == 1 && mAnalyseWholeProgram) {
171173
mThreads[0]->analyseWholeProgram(mLastFiles, mCtuInfo);
172174
mAnalyseWholeProgram = false;
@@ -225,7 +227,7 @@ void ThreadHandler::saveSettings(QSettings &settings) const
225227

226228
bool ThreadHandler::hasPreviousFiles() const
227229
{
228-
return !mLastFiles.isEmpty();
230+
return !mLastFiles.empty();
229231
}
230232

231233
int ThreadHandler::getPreviousFilesCount() const
@@ -238,18 +240,18 @@ int ThreadHandler::getPreviousScanDuration() const
238240
return mScanDuration;
239241
}
240242

241-
QStringList ThreadHandler::getReCheckFiles(bool all) const
243+
std::list<FileWithDetails> ThreadHandler::getReCheckFiles(bool all) const
242244
{
243245
if (mLastCheckTime.isNull() || all)
244246
return mLastFiles;
245247

246248
std::set<QString> modified;
247249
std::set<QString> unmodified;
248250

249-
QStringList files;
250-
for (int i = 0; i < mLastFiles.size(); ++i) {
251-
if (needsReCheck(mLastFiles[i], modified, unmodified))
252-
files.push_back(mLastFiles[i]);
251+
std::list<FileWithDetails> files;
252+
for (const auto& f : mLastFiles) {
253+
if (needsReCheck(QString::fromStdString(f.path()), modified, unmodified))
254+
files.push_back(f);
253255
}
254256
return files;
255257
}

gui/threadhandler.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "suppressions.h"
2424
#include "threadresult.h"
2525

26+
#include <list>
2627
#include <memory>
2728
#include <set>
2829
#include <string>
@@ -40,6 +41,7 @@ class QSettings;
4041
class Settings;
4142
class ImportProject;
4243
class ErrorItem;
44+
class FileWithDetails;
4345

4446
/// @addtogroup GUI
4547
/// @{
@@ -103,7 +105,7 @@ class ThreadHandler : public QObject {
103105
*
104106
* @param files files to check
105107
*/
106-
void setFiles(const QStringList &files);
108+
void setFiles(std::list<FileWithDetails> files);
107109

108110
/**
109111
* @brief Set project to check
@@ -132,7 +134,7 @@ class ThreadHandler : public QObject {
132134
*
133135
* @param files list of files to be checked
134136
*/
135-
void setCheckFiles(const QStringList& files);
137+
void setCheckFiles(std::list<FileWithDetails> files);
136138

137139
/**
138140
* @brief Is checking running?
@@ -166,7 +168,7 @@ class ThreadHandler : public QObject {
166168
* @brief Get files that should be rechecked because they have been
167169
* changed.
168170
*/
169-
QStringList getReCheckFiles(bool all) const;
171+
std::list<FileWithDetails> getReCheckFiles(bool all) const;
170172

171173
/**
172174
* @brief Get start time of last check
@@ -213,7 +215,7 @@ protected slots:
213215
* @brief List of files checked last time (used when rechecking)
214216
*
215217
*/
216-
QStringList mLastFiles;
218+
std::list<FileWithDetails> mLastFiles;
217219

218220
/** @brief date and time when current checking started */
219221
QDateTime mCheckStartTime;

0 commit comments

Comments
 (0)