Cppcheck
threadresult.cpp
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2024 Cppcheck team.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "threadresult.h"
20 
21 #include "common.h"
22 #include "erroritem.h"
23 #include "errorlogger.h"
24 #include "errortypes.h"
25 #include "importproject.h"
26 
27 #include <numeric>
28 
29 #include <QFile>
30 
31 void ThreadResult::reportOut(const std::string &outmsg, Color /*c*/)
32 {
33  emit log(QString::fromStdString(outmsg));
34 }
35 
36 void ThreadResult::fileChecked(const QString &file)
37 {
38  std::lock_guard<std::mutex> locker(mutex);
39 
40  mProgress += QFile(file).size();
41  mFilesChecked++;
42 
43  if (mMaxProgress > 0) {
44  const int value = static_cast<int>(PROGRESS_MAX * mProgress / mMaxProgress);
45  const QString description = tr("%1 of %2 files checked").arg(mFilesChecked).arg(mTotalFiles);
46 
47  emit progress(value, description);
48  }
49 }
50 
52 {
53  std::lock_guard<std::mutex> locker(mutex);
54  const ErrorItem item(msg);
55  if (msg.severity != Severity::debug)
56  emit error(item);
57  else
58  emit debugError(item);
59 }
60 
62 {
63  std::lock_guard<std::mutex> locker(mutex);
64  if (mFiles.isEmpty()) {
65  return QString();
66  }
67 
68  return mFiles.takeFirst();
69 }
70 
72 {
73  std::lock_guard<std::mutex> locker(mutex);
74  if (mFileSettings.empty()) {
75  return FileSettings("");
76  }
77  const FileSettings fs = mFileSettings.front();
78  mFileSettings.pop_front();
79  return fs;
80 }
81 
82 void ThreadResult::setFiles(const QStringList &files)
83 {
84  std::lock_guard<std::mutex> locker(mutex);
85  mFiles = files;
86  mProgress = 0;
87  mFilesChecked = 0;
88  mTotalFiles = files.size();
89 
90  // Determine the total size of all of the files to check, so that we can
91  // show an accurate progress estimate
92  quint64 sizeOfFiles = std::accumulate(files.begin(), files.end(), 0, [](quint64 total, const QString& file) {
93  return total + QFile(file).size();
94  });
95  mMaxProgress = sizeOfFiles;
96 }
97 
99 {
100  std::lock_guard<std::mutex> locker(mutex);
101  mFiles.clear();
103  mProgress = 0;
104  mFilesChecked = 0;
105  mTotalFiles = prj.fileSettings.size();
106 
107  // Determine the total size of all of the files to check, so that we can
108  // show an accurate progress estimate
109  mMaxProgress = std::accumulate(prj.fileSettings.begin(), prj.fileSettings.end(), quint64{ 0 }, [](quint64 v, const FileSettings& fs) {
110  return v + QFile(QString::fromStdString(fs.filename())).size();
111  });
112 }
113 
115 {
116  std::lock_guard<std::mutex> locker(mutex);
117  mFiles.clear();
118  mFileSettings.clear();
119  mFilesChecked = 0;
120  mTotalFiles = 0;
121 }
122 
124 {
125  std::lock_guard<std::mutex> locker(mutex);
126  return mFiles.size() + mFileSettings.size();
127 }
A class containing error data for one error.
Definition: erroritem.h:72
Wrapper for error messages, provided by reportErr()
Definition: errorlogger.h:48
Severity severity
Definition: errorlogger.h:170
Importing project settings.
Definition: importproject.h:52
std::list< FileSettings > fileSettings
Definition: importproject.h:70
quint64 mMaxProgress
Max progress.
Definition: threadresult.h:145
QStringList mFiles
List of files to check.
Definition: threadresult.h:137
void clearFiles()
Clear files to check.
std::mutex mutex
Mutex.
Definition: threadresult.h:131
unsigned long mTotalFiles
Total number of files.
Definition: threadresult.h:163
FileSettings getNextFileSettings()
void error(const ErrorItem &item)
Signal of a new error.
quint64 mProgress
Current progress.
Definition: threadresult.h:151
void setProject(const ImportProject &prj)
QString getNextFile()
Get next unprocessed file.
void debugError(const ErrorItem &item)
Signal of a debug error.
void reportErr(const ErrorMessage &msg) override
Information about found errors and warnings is directed here.
void log(const QString &logline)
Signal of a new log message.
void fileChecked(const QString &file)
Slot threads use to signal this class that a specific file is checked.
unsigned long mFilesChecked
Current number of files checked.
Definition: threadresult.h:157
std::list< FileSettings > mFileSettings
Definition: threadresult.h:139
void progress(int value, const QString &description)
Progress signal.
void reportOut(const std::string &outmsg, Color c=Color::Reset) override
ErrorLogger methods.
void setFiles(const QStringList &files)
Set list of files to check.
int getFileCount() const
Get the number of files to check.
Color
Definition: color.h:27
@ debug
Debug message.
#define PROGRESS_MAX
Definition: common.h:94
File settings.
Definition: filesettings.h:57