File: cmdlineparser.h

package info (click to toggle)
cppcheck 2.18.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,132 kB
  • sloc: cpp: 268,935; python: 20,890; ansic: 8,090; sh: 1,045; makefile: 1,008; xml: 1,005; cs: 291
file content (186 lines) | stat: -rw-r--r-- 5,385 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* -*- C++ -*-
 * Cppcheck - A tool for static C/C++ code analysis
 * Copyright (C) 2007-2025 Cppcheck team.
 *
 * 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

#include <cstddef>
#include <cstdint>
#include <list>
#include <string>
#include <vector>

#include "cmdlinelogger.h"
#include "filesettings.h"
#include "standards.h"
#include "utils.h"

class Settings;
struct Suppressions;
class Library;

/// @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.
 */
class CmdLineParser {
    friend class TestCmdlineParser;
public:
    /**
     * The constructor.
     * @param logger The logger instance to log messages through
     * @param settings Settings instance that will be modified according to
     * options user has given.
     * @param suppressions Suppressions instance that keeps the suppressions
     */
    CmdLineParser(CmdLineLogger &logger, Settings &settings, Suppressions &suppressions);

    enum class Result : std::uint8_t { Success, Exit, Fail };

    /**
     * @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[]);

    /**
     * @brief Filter files
     *
     * @param fileFilters file filters
     * @param filesResolved all the files in project
     * @return the files in filesResolved that match filters
     */
    static std::list<FileWithDetails> filterFiles(const std::vector<std::string>& fileFilters,
                                                  const std::list<FileWithDetails>& filesResolved);

    /**
     * Parse given command line.
     * @return true if command line was ok, false if there was an error.
     */
    Result parseFromArgs(int argc, const char* const argv[]);

    /**
     * Return the path names user gave to command line.
     */
    const std::vector<std::string>& getPathNames() const {
        return mPathNames;
    }

    /**
     * Return the files user gave to command line.
     */
    const std::list<FileWithDetails>& getFiles() const {
        return mFiles;
    }

    /**
     * Return the file settings read from command line.
     */
    const std::list<FileSettings>& getFileSettings() const {
        return mFileSettings;
    }

    /**
     * Return a list of paths user wants to ignore.
     */
    const std::vector<std::string>& getIgnoredPaths() const {
        return mIgnoredPaths;
    }

    /**
     * Get Cppcheck version
     */
    std::string getVersion() const;

protected:

    /**
     * Print help text to the console.
     */
    void printHelp() const;

private:
    bool isCppcheckPremium() const;

    template<typename T>
    bool parseNumberArg(const char* const arg, std::size_t offset, T& num, bool mustBePositive = false)
    {
        T tmp;
        std::string err;
        if (!strToInt(arg + offset, tmp, &err)) {
            mLogger.printError("argument to '" + std::string(arg, offset) + "' is not valid - " + err + ".");
            return false;
        }
        if (mustBePositive && tmp < 0) {
            mLogger.printError("argument to '" + std::string(arg, offset) + "' needs to be a positive integer.");
            return false;
        }
        num = tmp;
        return true;
    }

    /**
     * Tries to load a library and prints warning/error messages
     * @return false, if an error occurred (except unknown XML elements)
     */
    bool tryLoadLibrary(Library& destination, const std::string& basepath, const char* filename, bool debug);

    /**
     * @brief Load libraries
     * @param settings Settings
     * @return Returns true if successful
     */
    bool loadLibraries(Settings& settings);

    /**
     * @brief Load addons
     * @param settings Settings
     * @return Returns true if successful
     */
    bool loadAddons(Settings& settings);

    bool loadCppcheckCfg();

    CmdLineLogger &mLogger;

    std::vector<std::string> mPathNames;
    std::list<FileWithDetails> mFiles;
    std::list<FileSettings> mFileSettings;
    std::vector<std::string> mIgnoredPaths;
    Settings &mSettings;
    Suppressions &mSuppressions;
    bool mAnalyzeAllVsConfigsSetOnCmdLine = false;
    /** @brief Name of the language that is enforced. Empty per default. */
    Standards::Language mEnforcedLang{Standards::Language::None};
};

/// @}

#endif // CMDLINE_PARSER_H