Cppcheck
threadhandler.h
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 
20 #ifndef THREADHANDLER_H
21 #define THREADHANDLER_H
22 
23 #include "suppressions.h"
24 #include "threadresult.h"
25 
26 #include <set>
27 
28 #include <QDateTime>
29 #include <QElapsedTimer>
30 #include <QList>
31 #include <QObject>
32 #include <QString>
33 #include <QStringList>
34 
35 class ResultsView;
36 class CheckThread;
37 class QSettings;
38 class Settings;
39 class ImportProject;
40 class ErrorItem;
41 
42 /// @addtogroup GUI
43 /// @{
44 
45 
46 /**
47  * @brief This class handles creating threadresult and starting threads
48  *
49  */
50 class ThreadHandler : public QObject {
51  Q_OBJECT
52 public:
53  explicit ThreadHandler(QObject *parent = nullptr);
54  ~ThreadHandler() override;
55 
56  /**
57  * @brief Set the number of threads to use
58  * @param count The number of threads to use
59  */
60  void setThreadCount(const int count);
61 
62  /**
63  * @brief Initialize the threads (connect all signals to resultsview's slots)
64  *
65  * @param view View to show error results
66  */
67  void initialize(const ResultsView *view);
68 
69  /**
70  * @brief Load settings
71  * @param settings QSettings to load settings from
72  */
73  void loadSettings(const QSettings &settings);
74 
75  /**
76  * @brief Save settings
77  * @param settings QSettings to save settings to
78  */
79  void saveSettings(QSettings &settings) const;
80 
81  void setAddonsAndTools(const QStringList &addonsAndTools) {
82  mAddonsAndTools = addonsAndTools;
83  }
84 
85  void setSuppressions(const QList<SuppressionList::Suppression> &s) {
86  mSuppressions = s;
87  }
88 
89  void setClangIncludePaths(const QStringList &s) {
91  }
92 
93  /**
94  * @brief Clear all files from cppcheck
95  *
96  */
97  void clearFiles();
98 
99  /**
100  * @brief Set files to check
101  *
102  * @param files files to check
103  */
104  void setFiles(const QStringList &files);
105 
106  /**
107  * @brief Set project to check
108  *
109  * @param prj project to check
110  */
111  void setProject(const ImportProject &prj);
112 
113  /**
114  * @brief Start the threads to check the files
115  *
116  * @param settings Settings for checking
117  */
118  void check(const Settings &settings);
119 
120  /**
121  * @brief Set files to check
122  *
123  * @param all true if all files, false if modified files
124  */
125  void setCheckFiles(bool all);
126 
127  /**
128  * @brief Set selected files to check
129  *
130  * @param files list of files to be checked
131  */
132  void setCheckFiles(const QStringList& files);
133 
134  /**
135  * @brief Is checking running?
136  *
137  * @return true if check is running, false otherwise.
138  */
139  bool isChecking() const;
140 
141  /**
142  * @brief Have we checked files already?
143  *
144  * @return true check has been previously run and recheck can be done
145  */
146  bool hasPreviousFiles() const;
147 
148  /**
149  * @brief Return count of files we checked last time.
150  *
151  * @return count of files that were checked last time.
152  */
153  int getPreviousFilesCount() const;
154 
155  /**
156  * @brief Return the time elapsed while scanning the previous time.
157  *
158  * @return the time elapsed in milliseconds.
159  */
160  int getPreviousScanDuration() const;
161 
162  /**
163  * @brief Get files that should be rechecked because they have been
164  * changed.
165  */
166  QStringList getReCheckFiles(bool all) const;
167 
168  /**
169  * @brief Get start time of last check
170  *
171  * @return start time of last check
172  */
173  QDateTime getCheckStartTime() const;
174 
175  /**
176  * @brief Set start time of check
177  *
178  * @param checkStartTime saved start time of the last check
179  */
180  void setCheckStartTime(QDateTime checkStartTime);
181 
182 signals:
183  /**
184  * @brief Signal that all threads are done
185  *
186  */
187  void done();
188 
189  // NOLINTNEXTLINE(readability-inconsistent-declaration-parameter-name) - caused by generated MOC code
190  void log(const QString &msg);
191 
192  // NOLINTNEXTLINE(readability-inconsistent-declaration-parameter-name) - caused by generated MOC code
193  void debugError(const ErrorItem &item);
194 
195 public slots:
196 
197  /**
198  * @brief Slot to stop all threads
199  *
200  */
201  void stop();
202 protected slots:
203  /**
204  * @brief Slot that a single thread is done
205  *
206  */
207  void threadDone();
208 protected:
209  /**
210  * @brief List of files checked last time (used when rechecking)
211  *
212  */
213  QStringList mLastFiles;
214 
215  /** @brief date and time when current checking started */
216  QDateTime mCheckStartTime;
217 
218  /**
219  * @brief when was the files checked the last time (used when rechecking)
220  */
221  QDateTime mLastCheckTime;
222 
223  /**
224  * @brief Timer used for measuring scan duration
225  *
226  */
227  QElapsedTimer mTimer;
228 
229  /**
230  * @brief The previous scan duration in milliseconds.
231  *
232  */
234 
235  /**
236  * @brief Function to delete all threads
237  *
238  */
239  void removeThreads();
240 
241  /**
242  * @brief Thread results are stored here
243  *
244  */
246 
247  /**
248  * @brief List of threads currently in use
249  *
250  */
251  QList<CheckThread *> mThreads;
252 
253  /**
254  * @brief The amount of threads currently running
255  *
256  */
258 
260 
261  QStringList mAddonsAndTools;
262  QList<SuppressionList::Suppression> mSuppressions;
263  QStringList mClangIncludePaths;
264 private:
265 
266  /**
267  * @brief Check if a file needs to be rechecked. Recursively checks
268  * included headers. Used by GetReCheckFiles()
269  */
270  bool needsReCheck(const QString &filename, std::set<QString> &modified, std::set<QString> &unmodified) const;
271 };
272 /// @}
273 #endif // THREADHANDLER_H
Thread to run cppcheck.
Definition: checkthread.h:47
A class containing error data for one error.
Definition: erroritem.h:72
Importing project settings.
Definition: importproject.h:52
Widget to show cppcheck progressbar and result.
Definition: resultsview.h:51
This is just a container for general settings so that we don't need to pass individual values to func...
Definition: settings.h:95
This class handles creating threadresult and starting threads.
Definition: threadhandler.h:50
bool hasPreviousFiles() const
Have we checked files already?
void done()
Signal that all threads are done.
void check(const Settings &settings)
Start the threads to check the files.
QStringList mClangIncludePaths
bool needsReCheck(const QString &filename, std::set< QString > &modified, std::set< QString > &unmodified) const
Check if a file needs to be rechecked.
int mScanDuration
The previous scan duration in milliseconds.
QStringList mLastFiles
List of files checked last time (used when rechecking)
void removeThreads()
Function to delete all threads.
bool mAnalyseWholeProgram
void threadDone()
Slot that a single thread is done.
QDateTime getCheckStartTime() const
Get start time of last check.
QList< SuppressionList::Suppression > mSuppressions
QStringList mAddonsAndTools
void saveSettings(QSettings &settings) const
Save settings.
QDateTime mCheckStartTime
date and time when current checking started
ThreadResult mResults
Thread results are stored here.
void setThreadCount(const int count)
Set the number of threads to use.
ThreadHandler(QObject *parent=nullptr)
void setAddonsAndTools(const QStringList &addonsAndTools)
Definition: threadhandler.h:81
QDateTime mLastCheckTime
when was the files checked the last time (used when rechecking)
QStringList getReCheckFiles(bool all) const
Get files that should be rechecked because they have been changed.
~ThreadHandler() override
void stop()
Slot to stop all threads.
void setClangIncludePaths(const QStringList &s)
Definition: threadhandler.h:89
void debugError(const ErrorItem &item)
int mRunningThreadCount
The amount of threads currently running.
bool isChecking() const
Is checking running?
void log(const QString &msg)
QElapsedTimer mTimer
Timer used for measuring scan duration.
void setProject(const ImportProject &prj)
Set project to check.
int getPreviousFilesCount() const
Return count of files we checked last time.
void setCheckStartTime(QDateTime checkStartTime)
Set start time of check.
void clearFiles()
Clear all files from cppcheck.
int getPreviousScanDuration() const
Return the time elapsed while scanning the previous time.
void setCheckFiles(bool all)
Set files to check.
void initialize(const ResultsView *view)
Initialize the threads (connect all signals to resultsview's slots)
void setFiles(const QStringList &files)
Set files to check.
QList< CheckThread * > mThreads
List of threads currently in use.
void setSuppressions(const QList< SuppressionList::Suppression > &s)
Definition: threadhandler.h:85
void loadSettings(const QSettings &settings)
Load settings.
Threads use this class to obtain new files to process and to publish results.
Definition: threadresult.h:46