Cppcheck
checkthread.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 CHECKTHREAD_H
21 #define CHECKTHREAD_H
22 
23 #include "cppcheck.h"
24 #include "suppressions.h"
25 
26 #include <atomic>
27 #include <string>
28 #include <vector>
29 
30 #include <QList>
31 #include <QObject>
32 #include <QString>
33 #include <QStringList>
34 #include <QThread>
35 
36 class Settings;
37 class ThreadResult;
38 struct FileSettings;
39 
40 /// @addtogroup GUI
41 /// @{
42 
43 /**
44  * @brief Thread to run cppcheck
45  *
46  */
47 class CheckThread : public QThread {
48  Q_OBJECT
49 public:
50  explicit CheckThread(ThreadResult &result);
51 
52  /**
53  * @brief Set settings for cppcheck
54  *
55  * @param settings settings for cppcheck
56  */
57  void check(const Settings &settings);
58 
59  /**
60  * @brief Run whole program analysis
61  * @param files All files
62  */
63  void analyseWholeProgram(const QStringList &files);
64 
65  void setAddonsAndTools(const QStringList &addonsAndTools) {
66  mAddonsAndTools = addonsAndTools;
67  }
68 
69  void setClangIncludePaths(const QStringList &s) {
71  }
72 
73  void setSuppressions(const QList<SuppressionList::Suppression> &s) {
74  mSuppressions = s;
75  }
76 
77  /**
78  * @brief method that is run in a thread
79  *
80  */
81  void run() override;
82 
83  void stop();
84 
85  /**
86  * Determine command to run clang
87  * \return Command to run clang, empty if it is not found
88  */
89  static QString clangCmd();
90 
91  /**
92  * Determine command to run clang-tidy
93  * \return Command to run clang-tidy, empty if it is not found
94  */
95  static QString clangTidyCmd();
96 
97  static int executeCommand(std::string exe, std::vector<std::string> args, std::string redirect, std::string &output);
98 
99 signals:
100 
101  /**
102  * @brief cpp checking is done
103  *
104  */
105  void done();
106 
107  // NOLINTNEXTLINE(readability-inconsistent-declaration-parameter-name) - caused by generated MOC code
108  void fileChecked(const QString &file);
109 protected:
110 
111  /**
112  * @brief States for the check thread.
113  * Whole purpose of these states is to allow stopping of the checking. When
114  * stopping we say for the thread (Stopping) that stop when current check
115  * has been completed. Thread must be stopped cleanly, just terminating thread
116  * likely causes unpredictable side-effects.
117  */
118  enum State {
119  Running, /**< The thread is checking. */
120  Stopping, /**< The thread will stop after current work. */
121  Stopped, /**< The thread has been stopped. */
122  Ready, /**< The thread is ready. */
123  };
124 
125  /**
126  * @brief Thread's current execution state. Can be changed from outside
127  */
128  std::atomic<State> mState{Ready};
129 
131  /**
132  * @brief Cppcheck itself
133  */
135 
136 private:
137  void runAddonsAndTools(const FileSettings *fileSettings, const QString &fileName);
138 
139  void parseClangErrors(const QString &tool, const QString &file0, QString err);
140 
141  bool isSuppressed(const SuppressionList::ErrorMessage &errorMessage) const;
142 
143  QStringList mFiles;
145  QStringList mAddonsAndTools;
146  QStringList mClangIncludePaths;
147  QList<SuppressionList::Suppression> mSuppressions;
148 };
149 /// @}
150 #endif // CHECKTHREAD_H
Thread to run cppcheck.
Definition: checkthread.h:47
bool mAnalyseWholeProgram
Definition: checkthread.h:144
static QString clangTidyCmd()
Determine command to run clang-tidy.
void parseClangErrors(const QString &tool, const QString &file0, QString err)
QStringList mFiles
Definition: checkthread.h:143
State
States for the check thread.
Definition: checkthread.h:118
@ Running
The thread is checking.
Definition: checkthread.h:119
@ Stopping
The thread will stop after current work.
Definition: checkthread.h:120
@ Stopped
The thread has been stopped.
Definition: checkthread.h:121
@ Ready
The thread is ready.
Definition: checkthread.h:122
void run() override
method that is run in a thread
void fileChecked(const QString &file)
void analyseWholeProgram(const QStringList &files)
Run whole program analysis.
Definition: checkthread.cpp:99
CheckThread(ThreadResult &result)
Definition: checkthread.cpp:87
QStringList mAddonsAndTools
Definition: checkthread.h:145
CppCheck mCppcheck
Cppcheck itself.
Definition: checkthread.h:134
void check(const Settings &settings)
Set settings for cppcheck.
Definition: checkthread.cpp:92
void setClangIncludePaths(const QStringList &s)
Definition: checkthread.h:69
void setSuppressions(const QList< SuppressionList::Suppression > &s)
Definition: checkthread.h:73
void done()
cpp checking is done
static int executeCommand(std::string exe, std::vector< std::string > args, std::string redirect, std::string &output)
Definition: checkthread.cpp:60
void runAddonsAndTools(const FileSettings *fileSettings, const QString &fileName)
QStringList mClangIncludePaths
Definition: checkthread.h:146
QList< SuppressionList::Suppression > mSuppressions
Definition: checkthread.h:147
bool isSuppressed(const SuppressionList::ErrorMessage &errorMessage) const
std::atomic< State > mState
Thread's current execution state.
Definition: checkthread.h:128
static QString clangCmd()
Determine command to run clang.
void setAddonsAndTools(const QStringList &addonsAndTools)
Definition: checkthread.h:65
ThreadResult & mResult
Definition: checkthread.h:130
This is the base class which will use other classes to do static code analysis for C and C++ code to ...
Definition: cppcheck.h:60
This is just a container for general settings so that we don't need to pass individual values to func...
Definition: settings.h:95
Threads use this class to obtain new files to process and to publish results.
Definition: threadresult.h:46
File settings.
Definition: filesettings.h:57