Cppcheck
cmdlineparser.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 CMDLINE_PARSER_H
20 #define CMDLINE_PARSER_H
21 
22 #include <cstddef>
23 #include <list>
24 #include <string>
25 #include <utility>
26 #include <vector>
27 
28 #include "cmdlinelogger.h"
29 #include "filesettings.h"
30 #include "utils.h"
31 
32 class Settings;
33 struct Suppressions;
34 class Library;
35 
36 /// @addtogroup CLI
37 /// @{
38 
39 /**
40  * @brief The command line parser.
41  * The command line parser parses options and parameters user gives to
42  * cppcheck command line.
43  *
44  * The parser takes a pointer to Settings instance which it will update
45  * based on options user has given. Couple of options are handled as
46  * class internal options.
47  */
49 public:
50  /**
51  * The constructor.
52  * @param logger The logger instance to log messages through
53  * @param settings Settings instance that will be modified according to
54  * options user has given.
55  * @param suppressions Suppressions instance that keeps the suppressions
56  */
57  CmdLineParser(CmdLineLogger &logger, Settings &settings, Suppressions &suppressions);
58 
59  enum class Result { Success, Exit, Fail };
60 
61  /**
62  * @brief Parse command line args and fill settings and file lists
63  * from there.
64  *
65  * @param argc argc from main()
66  * @param argv argv from main()
67  * @return false when errors are found in the input
68  */
69  bool fillSettingsFromArgs(int argc, const char* const argv[]);
70 
71  /**
72  * Parse given command line.
73  * @return true if command line was ok, false if there was an error.
74  */
75  Result parseFromArgs(int argc, const char* const argv[]);
76 
77  /**
78  * Return the path names user gave to command line.
79  */
80  const std::vector<std::string>& getPathNames() const {
81  return mPathNames;
82  }
83 
84  /**
85  * Return the files user gave to command line.
86  */
87  const std::list<FileWithDetails>& getFiles() const {
88  return mFiles;
89  }
90 
91  /**
92  * Return the file settings read from command line.
93  */
94  const std::list<FileSettings>& getFileSettings() const {
95  return mFileSettings;
96  }
97 
98  /**
99  * Return a list of paths user wants to ignore.
100  */
101  const std::vector<std::string>& getIgnoredPaths() const {
102  return mIgnoredPaths;
103  }
104 
105  /**
106  * Get Cppcheck version
107  */
108  std::string getVersion() const;
109 
110 protected:
111 
112  /**
113  * Print help text to the console.
114  */
115  void printHelp() const;
116 
117 private:
118  bool isCppcheckPremium() const;
119 
120  template<typename T>
121  bool parseNumberArg(const char* const arg, std::size_t offset, T& num, bool mustBePositive = false)
122  {
123  T tmp;
124  std::string err;
125  if (!strToInt(arg + offset, tmp, &err)) {
126  mLogger.printError("argument to '" + std::string(arg, offset) + "' is not valid - " + err + ".");
127  return false;
128  }
129  if (mustBePositive && tmp < 0) {
130  mLogger.printError("argument to '" + std::string(arg, offset) + "' needs to be a positive integer.");
131  return false;
132  }
133  num = tmp;
134  return true;
135  }
136 
137  /**
138  * Tries to load a library and prints warning/error messages
139  * @return false, if an error occurred (except unknown XML elements)
140  */
141  bool tryLoadLibrary(Library& destination, const std::string& basepath, const char* filename);
142 
143  /**
144  * @brief Load libraries
145  * @param settings Settings
146  * @return Returns true if successful
147  */
148  bool loadLibraries(Settings& settings);
149 
150  /**
151  * @brief Load addons
152  * @param settings Settings
153  * @return Returns true if successful
154  */
155  bool loadAddons(Settings& settings);
156 
157  bool loadCppcheckCfg();
158 
160 
161  std::vector<std::string> mPathNames;
162  std::list<FileWithDetails> mFiles;
163  std::list<FileSettings> mFileSettings;
164  std::vector<std::string> mIgnoredPaths;
167  std::string mVSConfig;
168 };
169 
170 /// @}
171 
172 #endif // CMDLINE_PARSER_H
virtual void printError(const std::string &message)=0
print an error message
The command line parser.
Definition: cmdlineparser.h:48
bool parseNumberArg(const char *const arg, std::size_t offset, T &num, bool mustBePositive=false)
CmdLineParser(CmdLineLogger &logger, Settings &settings, Suppressions &suppressions)
The constructor.
const std::vector< std::string > & getPathNames() const
Return the path names user gave to command line.
Definition: cmdlineparser.h:80
bool tryLoadLibrary(Library &destination, const std::string &basepath, const char *filename)
Tries to load a library and prints warning/error messages.
std::vector< std::string > mPathNames
const std::list< FileWithDetails > & getFiles() const
Return the files user gave to command line.
Definition: cmdlineparser.h:87
void printHelp() const
Print help text to the console.
bool loadLibraries(Settings &settings)
Load libraries.
std::string getVersion() const
Get Cppcheck version.
std::list< FileSettings > mFileSettings
bool fillSettingsFromArgs(int argc, const char *const argv[])
Parse command line args and fill settings and file lists from there.
Suppressions & mSuppressions
Settings & mSettings
std::list< FileWithDetails > mFiles
CmdLineLogger & mLogger
std::vector< std::string > mIgnoredPaths
bool isCppcheckPremium() const
Result parseFromArgs(int argc, const char *const argv[])
Parse given command line.
std::string mVSConfig
bool loadAddons(Settings &settings)
Load addons.
const std::vector< std::string > & getIgnoredPaths() const
Return a list of paths user wants to ignore.
const std::list< FileSettings > & getFileSettings() const
Return the file settings read from command line.
Definition: cmdlineparser.h:94
Library definitions handling.
Definition: library.h:52
This is just a container for general settings so that we don't need to pass individual values to func...
Definition: settings.h:95
bool strToInt(const std::string &str, T &num, std::string *err=nullptr)
Definition: utils.h:211