2010-08-31 20:32:26 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2023-01-28 10:16:34 +01:00
|
|
|
* Copyright (C) 2007-2023 Cppcheck team.
|
2010-08-31 20:32:26 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CMDLINE_PARSER_H
|
|
|
|
#define CMDLINE_PARSER_H
|
|
|
|
|
2023-08-02 10:36:17 +02:00
|
|
|
#include <cstddef>
|
2023-11-03 23:24:04 +01:00
|
|
|
#include <list>
|
2010-08-31 20:32:26 +02:00
|
|
|
#include <string>
|
2023-11-19 19:45:10 +01:00
|
|
|
#include <utility>
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <vector>
|
2010-08-31 20:32:26 +02:00
|
|
|
|
2023-09-25 13:43:18 +02:00
|
|
|
#include "cmdlinelogger.h"
|
2023-11-03 23:24:04 +01:00
|
|
|
#include "filesettings.h"
|
2023-04-08 22:29:09 +02:00
|
|
|
#include "utils.h"
|
|
|
|
|
2010-08-31 20:32:26 +02:00
|
|
|
class Settings;
|
2023-04-30 20:56:54 +02:00
|
|
|
class Suppressions;
|
2023-11-19 19:45:10 +01:00
|
|
|
class Library;
|
2010-08-31 20:32:26 +02:00
|
|
|
|
2010-09-06 19:50:48 +02:00
|
|
|
/// @addtogroup CLI
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The command line parser.
|
|
|
|
* The command line parser parses options and parameters user gives to
|
|
|
|
* cppcheck command line.
|
|
|
|
*
|
|
|
|
* The parser takes a pointer to Settings instance which it will update
|
|
|
|
* based on options user has given. Couple of options are handled as
|
|
|
|
* class internal options.
|
|
|
|
*/
|
2011-10-13 20:53:06 +02:00
|
|
|
class CmdLineParser {
|
2010-08-31 20:32:26 +02:00
|
|
|
public:
|
2010-09-06 19:50:48 +02:00
|
|
|
/**
|
|
|
|
* The constructor.
|
2023-09-25 13:43:18 +02:00
|
|
|
* @param logger The logger instance to log messages through
|
2010-12-15 18:45:53 +01:00
|
|
|
* @param settings Settings instance that will be modified according to
|
2010-09-06 19:50:48 +02:00
|
|
|
* options user has given.
|
2023-04-30 20:56:54 +02:00
|
|
|
* @param suppressions Suppressions instance that keeps the suppressions
|
|
|
|
* @param suppressionsNoFail Suppressions instance that keeps the "do not fail" suppressions
|
2010-09-06 19:50:48 +02:00
|
|
|
*/
|
2023-09-25 13:43:18 +02:00
|
|
|
CmdLineParser(CmdLineLogger &logger, Settings &settings, Suppressions &suppressions, Suppressions &suppressionsNoFail);
|
2010-09-06 19:50:48 +02:00
|
|
|
|
2023-11-27 12:06:03 +01:00
|
|
|
enum class Result { Success, Exit, Fail };
|
|
|
|
|
2023-11-19 19:45:10 +01:00
|
|
|
/**
|
|
|
|
* @brief Parse command line args and fill settings and file lists
|
|
|
|
* from there.
|
|
|
|
*
|
|
|
|
* @param argc argc from main()
|
|
|
|
* @param argv argv from main()
|
|
|
|
* @return false when errors are found in the input
|
|
|
|
*/
|
|
|
|
bool fillSettingsFromArgs(int argc, const char* const argv[]);
|
|
|
|
|
2010-09-06 19:50:48 +02:00
|
|
|
/**
|
|
|
|
* Parse given command line.
|
|
|
|
* @return true if command line was ok, false if there was an error.
|
|
|
|
*/
|
2023-11-27 12:06:03 +01:00
|
|
|
Result parseFromArgs(int argc, const char* const argv[]);
|
2010-09-06 19:50:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the path names user gave to command line.
|
|
|
|
*/
|
2018-04-23 12:39:47 +02:00
|
|
|
const std::vector<std::string>& getPathNames() const {
|
2018-08-18 15:45:42 +02:00
|
|
|
return mPathNames;
|
2010-08-31 20:32:26 +02:00
|
|
|
}
|
|
|
|
|
2023-11-19 19:45:10 +01:00
|
|
|
/**
|
|
|
|
* Return the files user gave to command line.
|
|
|
|
*/
|
|
|
|
const std::list<std::pair<std::string, std::size_t>>& getFiles() const {
|
|
|
|
return mFiles;
|
|
|
|
}
|
|
|
|
|
2023-11-03 23:24:04 +01:00
|
|
|
/**
|
|
|
|
* Return the file settings read from command line.
|
|
|
|
*/
|
|
|
|
const std::list<FileSettings>& getFileSettings() const {
|
|
|
|
return mFileSettings;
|
|
|
|
}
|
|
|
|
|
2011-01-31 14:25:51 +01:00
|
|
|
/**
|
|
|
|
* Return a list of paths user wants to ignore.
|
|
|
|
*/
|
2018-04-23 12:39:47 +02:00
|
|
|
const std::vector<std::string>& getIgnoredPaths() const {
|
2018-08-18 15:45:42 +02:00
|
|
|
return mIgnoredPaths;
|
2011-01-31 14:25:51 +01:00
|
|
|
}
|
|
|
|
|
2010-08-31 20:32:26 +02:00
|
|
|
protected:
|
2010-09-06 19:50:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Print help text to the console.
|
|
|
|
*/
|
2023-10-21 09:12:59 +02:00
|
|
|
void printHelp() const;
|
2010-08-31 20:32:26 +02:00
|
|
|
|
|
|
|
private:
|
2023-12-13 06:53:03 +01:00
|
|
|
bool isCppcheckPremium() const;
|
2022-08-24 08:46:35 +02:00
|
|
|
|
2023-04-08 22:29:09 +02:00
|
|
|
template<typename T>
|
2023-09-25 13:43:18 +02:00
|
|
|
bool parseNumberArg(const char* const arg, std::size_t offset, T& num, bool mustBePositive = false)
|
2023-04-08 22:29:09 +02:00
|
|
|
{
|
|
|
|
T tmp;
|
|
|
|
std::string err;
|
|
|
|
if (!strToInt(arg + offset, tmp, &err)) {
|
2023-09-25 13:43:18 +02:00
|
|
|
mLogger.printError("argument to '" + std::string(arg, offset) + "' is not valid - " + err + ".");
|
2023-04-08 22:29:09 +02:00
|
|
|
return false;
|
|
|
|
}
|
2023-06-28 19:52:33 +02:00
|
|
|
if (mustBePositive && tmp < 0) {
|
2023-09-25 13:43:18 +02:00
|
|
|
mLogger.printError("argument to '" + std::string(arg, offset) + "' needs to be a positive integer.");
|
2023-04-08 22:29:09 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
num = tmp;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-11-19 19:45:10 +01:00
|
|
|
/**
|
|
|
|
* Tries to load a library and prints warning/error messages
|
|
|
|
* @return false, if an error occurred (except unknown XML elements)
|
|
|
|
*/
|
2023-11-25 21:12:24 +01:00
|
|
|
bool tryLoadLibrary(Library& destination, const std::string& basepath, const char* filename);
|
2023-11-19 19:45:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Load libraries
|
|
|
|
* @param settings Settings
|
|
|
|
* @return Returns true if successful
|
|
|
|
*/
|
2023-11-25 21:12:24 +01:00
|
|
|
bool loadLibraries(Settings& settings);
|
2023-11-19 19:45:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Load addons
|
|
|
|
* @param settings Settings
|
|
|
|
* @return Returns true if successful
|
|
|
|
*/
|
2023-11-25 21:12:24 +01:00
|
|
|
bool loadAddons(Settings& settings);
|
2023-11-19 19:45:10 +01:00
|
|
|
|
2023-12-01 16:34:14 +01:00
|
|
|
bool loadCppcheckCfg();
|
|
|
|
|
2023-09-25 13:43:18 +02:00
|
|
|
CmdLineLogger &mLogger;
|
|
|
|
|
2018-08-18 15:45:42 +02:00
|
|
|
std::vector<std::string> mPathNames;
|
2023-11-19 19:45:10 +01:00
|
|
|
std::list<std::pair<std::string, std::size_t>> mFiles;
|
2023-11-03 23:24:04 +01:00
|
|
|
std::list<FileSettings> mFileSettings;
|
2018-08-18 15:45:42 +02:00
|
|
|
std::vector<std::string> mIgnoredPaths;
|
2023-03-07 12:11:21 +01:00
|
|
|
Settings &mSettings;
|
2023-04-30 20:56:54 +02:00
|
|
|
Suppressions &mSuppressions;
|
|
|
|
Suppressions &mSuppressionsNoFail;
|
2020-04-19 18:19:28 +02:00
|
|
|
std::string mVSConfig;
|
2010-08-31 20:32:26 +02:00
|
|
|
};
|
|
|
|
|
2010-09-06 19:50:48 +02:00
|
|
|
/// @}
|
|
|
|
|
2010-08-31 20:32:26 +02:00
|
|
|
#endif // CMDLINE_PARSER_H
|