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-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 #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 class FileWithDetails;
35 
36 /**
37  * This class works as an example of how CppCheck can be used in external
38  * programs without very little knowledge of the internal parts of the
39  * program itself. If you wish to use cppcheck e.g. as a part of IDE,
40  * just rewrite this class for your needs and possibly use other methods
41  * from CppCheck class instead the ones used here.
42  */
44 public:
45  friend class TestSuppressions;
46 
47  /**
48  * Constructor
49  */
50  CppCheckExecutor() = default;
53 
54  /**
55  * Starts the checking.
56  *
57  * @param argc from main()
58  * @param argv from main()
59  * @return EXIT_FAILURE if arguments are invalid or no input files
60  * were found.
61  * If errors are found and --error-exitcode is used,
62  * given value is returned instead of default 0.
63  * If no errors are found, 0 is returned.
64  */
65  int check(int argc, const char* const argv[]);
66 
67  /**
68  * @param exceptionOutput Output file
69  */
70  static void setExceptionOutput(FILE* exceptionOutput);
71  /**
72  * @return file name to be used for output from exception handler. Has to be either "stdout" or "stderr".
73  */
74  static FILE* getExceptionOutput();
75 
76 private:
77 
78  /**
79  * Execute a shell command and read the output from it. Returns exitcode of the executed command,.
80  */
81  static int executeCommand(std::string exe, std::vector<std::string> args, std::string redirect, std::string &output_);
82 
83 protected:
84 
85  static bool reportSuppressions(const Settings &settings, const SuppressionList& suppressions, bool unusedFunctionCheckEnabled, const std::list<FileWithDetails> &files, const std::list<FileSettings>& fileSettings, ErrorLogger& errorLogger);
86 
87  /**
88  * Wrapper around check_internal
89  * - installs optional platform dependent signal handling
90  *
91  * @param settings the settings
92  **/
93  int check_wrapper(const Settings& settings);
94 
95  /**
96  * Starts the checking.
97  *
98  * @param settings the settings
99  * @return EXIT_FAILURE if arguments are invalid or no input files
100  * were found.
101  * If errors are found and --error-exitcode is used,
102  * given value is returned instead of default 0.
103  * If no errors are found, 0 is returned.
104  */
105  int check_internal(const Settings& settings) const;
106 
107  /**
108  * Filename associated with size of file
109  */
110  std::list<FileWithDetails> mFiles;
111 
112  std::list<FileSettings> mFileSettings;
113 
114 #if defined(USE_WINDOWS_SEH) || defined(USE_UNIX_SIGNAL_HANDLING)
115  /**
116  * Output file name for exception handler
117  */
118  static FILE* mExceptionOutput;
119 #endif
120 };
121 
122 #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 FILE * getExceptionOutput()
static bool reportSuppressions(const Settings &settings, const SuppressionList &suppressions, bool unusedFunctionCheckEnabled, const std::list< FileWithDetails > &files, const std::list< FileSettings > &fileSettings, ErrorLogger &errorLogger)
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
std::list< FileWithDetails > mFiles
Filename associated with size of file.
int check_internal(const Settings &settings) const
Starts the checking.
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