Cppcheck
cppcheckexecutor.h
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2023 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 #ifndef CPPCHECKEXECUTOR_H
20 #define CPPCHECKEXECUTOR_H
21 
22 #include "config.h"
23 #include "filesettings.h"
24 
25 #include <cstdio>
26 #include <list>
27 #include <string>
28 #include <utility>
29 #include <vector>
30 
31 class Settings;
32 class ErrorLogger;
33 class SuppressionList;
34 
35 /**
36  * This class works as an example of how CppCheck can be used in external
37  * programs without very little knowledge of the internal parts of the
38  * program itself. If you wish to use cppcheck e.g. as a part of IDE,
39  * just rewrite this class for your needs and possibly use other methods
40  * from CppCheck class instead the ones used here.
41  */
43 public:
44  friend class TestSuppressions;
45 
46  /**
47  * Constructor
48  */
49  CppCheckExecutor() = default;
52 
53  /**
54  * Starts the checking.
55  *
56  * @param argc from main()
57  * @param argv from main()
58  * @return EXIT_FAILURE if arguments are invalid or no input files
59  * were found.
60  * If errors are found and --error-exitcode is used,
61  * given value is returned instead of default 0.
62  * If no errors are found, 0 is returned.
63  */
64  int check(int argc, const char* const argv[]);
65 
66  /**
67  * @param exceptionOutput Output file
68  */
69  static void setExceptionOutput(FILE* exceptionOutput);
70  /**
71  * @return file name to be used for output from exception handler. Has to be either "stdout" or "stderr".
72  */
73  static FILE* getExceptionOutput();
74 
75 private:
76 
77  /**
78  * Execute a shell command and read the output from it. Returns exitcode of the executed command,.
79  */
80  static int executeCommand(std::string exe, std::vector<std::string> args, std::string redirect, std::string &output_);
81 
82 protected:
83 
84  static bool reportSuppressions(const Settings &settings, const SuppressionList& suppressions, bool unusedFunctionCheckEnabled, const std::list<std::pair<std::string, std::size_t>> &files, const std::list<FileSettings>& fileSettings, ErrorLogger& errorLogger);
85 
86  /**
87  * Wrapper around check_internal
88  * - installs optional platform dependent signal handling
89  *
90  * @param settings the settings
91  **/
92  int check_wrapper(const Settings& settings);
93 
94  /**
95  * Starts the checking.
96  *
97  * @param settings the settings
98  * @return EXIT_FAILURE if arguments are invalid or no input files
99  * were found.
100  * If errors are found and --error-exitcode is used,
101  * given value is returned instead of default 0.
102  * If no errors are found, 0 is returned.
103  */
104  int check_internal(const Settings& settings) const;
105 
106  /**
107  * Filename associated with size of file
108  */
109  std::list<std::pair<std::string, std::size_t>> mFiles;
110 
111  std::list<FileSettings> mFileSettings;
112 
113 #if defined(USE_WINDOWS_SEH) || defined(USE_UNIX_SIGNAL_HANDLING)
114  /**
115  * Output file name for exception handler
116  */
117  static FILE* mExceptionOutput;
118 #endif
119 };
120 
121 #endif // CPPCHECKEXECUTOR_H
This class works as an example of how CppCheck can be used in external programs without very little k...
static void setExceptionOutput(FILE *exceptionOutput)
static bool reportSuppressions(const Settings &settings, const SuppressionList &suppressions, bool unusedFunctionCheckEnabled, const std::list< std::pair< std::string, std::size_t >> &files, const std::list< FileSettings > &fileSettings, ErrorLogger &errorLogger)
static FILE * getExceptionOutput()
std::list< FileSettings > mFileSettings
int check_wrapper(const Settings &settings)
Wrapper around check_internal.
friend class TestSuppressions
CppCheckExecutor(const CppCheckExecutor &)=delete
int check(int argc, const char *const argv[])
Starts the checking.
static int executeCommand(std::string exe, std::vector< std::string > args, std::string redirect, std::string &output_)
Execute a shell command and read the output from it.
CppCheckExecutor()=default
Constructor.
CppCheckExecutor & operator=(const CppCheckExecutor &)=delete
int check_internal(const Settings &settings) const
Starts the checking.
std::list< std::pair< std::string, std::size_t > > mFiles
Filename associated with size of file.
This is an interface, which the class responsible of error logging should implement.
Definition: errorlogger.h:214
This is just a container for general settings so that we don't need to pass individual values to func...
Definition: settings.h:95
class for handling suppressions
Definition: suppressions.h:42