Coverage Report

Created: 2025-01-24 06:31

/src/cppcheck/lib/tokenlist.h
Line
Count
Source
1
/* -*- C++ -*-
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
//---------------------------------------------------------------------------
20
#ifndef tokenlistH
21
#define tokenlistH
22
//---------------------------------------------------------------------------
23
24
#include "config.h"
25
#include "standards.h"
26
27
#include <cstddef>
28
#include <iosfwd>
29
#include <string>
30
#include <vector>
31
32
class Token;
33
class TokenList;
34
class Settings;
35
36
namespace simplecpp {
37
    class TokenList;
38
}
39
40
/// @addtogroup Core
41
/// @{
42
43
/**
44
 * @brief This struct stores pointers to the front and back tokens of the list this token is in.
45
 */
46
struct TokensFrontBack {
47
868
    explicit TokensFrontBack(const TokenList& list) : list(list) {}
48
    Token *front{};
49
    Token* back{};
50
    const TokenList& list;
51
};
52
53
class CPPCHECKLIB TokenList {
54
public:
55
    // TODO: pass settings as reference
56
    explicit TokenList(const Settings* settings);
57
    ~TokenList();
58
59
    TokenList(const TokenList &) = delete;
60
    TokenList &operator=(const TokenList &) = delete;
61
62
    /** @return the source file path. e.g. "file.cpp" */
63
    const std::string& getSourceFilePath() const;
64
65
    /** @return true if the code is C */
66
    bool isC() const;
67
68
    /** @return true if the code is C++ */
69
    bool isCPP() const;
70
71
    // TODO: get rid of this
72
    void setLang(Standards::Language lang, bool force = false);
73
74
    /**
75
     * Delete all tokens in given token list
76
     * @param tok token list to delete
77
     */
78
    static void deleteTokens(Token *tok);
79
80
    void addtoken(const std::string& str, nonneg int lineno, nonneg int column, nonneg int fileno, bool split = false);
81
    void addtoken(const std::string& str, const Token *locationTok);
82
83
    void addtoken(const Token *tok, nonneg int lineno, nonneg int column, nonneg int fileno);
84
    void addtoken(const Token *tok, const Token *locationTok);
85
    void addtoken(const Token *tok);
86
87
    static void insertTokens(Token *dest, const Token *src, nonneg int n);
88
89
    /**
90
     * Copy tokens.
91
     * @param dest destination token where copied tokens will be inserted after
92
     * @param first first token to copy
93
     * @param last last token to copy
94
     * @param one_line true=>copy all tokens to the same line as dest. false=>copy all tokens to dest while keeping the 'line breaks'
95
     * @return new location of last token copied
96
     */
97
    RET_NONNULL static Token *copyTokens(Token *dest, const Token *first, const Token *last, bool one_line = true);
98
99
    /**
100
     * Create tokens from code.
101
     * The code must be preprocessed first:
102
     * - multiline strings are not handled.
103
     * - UTF in the code are not handled.
104
     * - comments are not handled.
105
     * @param code input stream for code
106
     * @param file0 source file name
107
     */
108
    bool createTokens(std::istream &code, const std::string& file0);
109
    bool createTokens(std::istream &code, Standards::Language lang);
110
111
    void createTokens(simplecpp::TokenList&& tokenList);
112
113
    /** Deallocate list */
114
    void deallocateTokens();
115
116
    /** append file name if seen the first time; return its index in any case */
117
    int appendFileIfNew(std::string fileName);
118
119
    /** get first token of list */
120
43.9k
    const Token *front() const {
121
43.9k
        return mTokensFrontBack.front;
122
43.9k
    }
123
    // NOLINTNEXTLINE(readability-make-member-function-const) - do not allow usage of mutable pointer from const object
124
283k
    Token *front() {
125
283k
        return mTokensFrontBack.front;
126
283k
    }
127
128
    /** get last token of list */
129
11.9k
    const Token *back() const {
130
11.9k
        return mTokensFrontBack.back;
131
11.9k
    }
132
    // NOLINTNEXTLINE(readability-make-member-function-const) - do not allow usage of mutable pointer from const object
133
770k
    Token *back() {
134
770k
        return mTokensFrontBack.back;
135
770k
    }
136
137
    /**
138
     * Get filenames (the sourcefile + the files it include).
139
     * The first filename is the filename for the sourcefile
140
     * @return vector with filenames
141
     */
142
604k
    const std::vector<std::string>& getFiles() const {
143
604k
        return mFiles;
144
604k
    }
145
146
    std::string getOrigFile(const Token *tok) const;
147
148
    /**
149
     * get filename for given token
150
     * @param tok The given token
151
     * @return filename for the given token
152
     */
153
    const std::string& file(const Token *tok) const;
154
155
    /**
156
     * Get file:line for a given token
157
     * @param tok given token
158
     * @return location for given token
159
     */
160
    std::string fileLine(const Token *tok) const;
161
162
    /**
163
     * Calculates a hash of the token list used to compare multiple
164
     * token lists with each other as quickly as possible.
165
     */
166
    std::size_t calculateHash() const;
167
168
    /**
169
     * Create abstract syntax tree.
170
     */
171
    void createAst() const;
172
173
    /**
174
     * Check abstract syntax tree.
175
     * Throws InternalError on failure
176
     */
177
    void validateAst(bool print) const;
178
179
    /**
180
     * Verify that the given token is an element of the tokenlist.
181
     * That method is implemented for debugging purposes.
182
     * @param[in] tok token to be checked
183
     * \return true if token was found in tokenlist, false else. In case of nullptr true is returned.
184
     */
185
    bool validateToken(const Token* tok) const;
186
187
    /**
188
     * Convert platform dependent types to standard types.
189
     * 32 bits: size_t -> unsigned long
190
     * 64 bits: size_t -> unsigned long long
191
     */
192
    void simplifyPlatformTypes();
193
194
    /**
195
     * Collapse compound standard types into a single token.
196
     * unsigned long long int => long _isUnsigned=true,_isLong=true
197
     */
198
    void simplifyStdType();
199
200
    void clangSetOrigFiles();
201
202
    bool isKeyword(const std::string &str) const;
203
204
private:
205
    void determineCppC();
206
207
    bool createTokensInternal(std::istream &code, const std::string& file0);
208
209
    /** Token list */
210
    TokensFrontBack mTokensFrontBack;
211
212
    /** filenames for the tokenized source code (source + included) */
213
    std::vector<std::string> mFiles;
214
215
    /** Original filenames for the tokenized source code (source + included) */
216
    std::vector<std::string> mOrigFiles;
217
218
    /** settings */
219
    const Settings* const mSettings{};
220
221
    /** File is known to be C/C++ code */
222
    Standards::Language mLang{Standards::Language::None};
223
};
224
225
/// @}
226
227
const Token* isLambdaCaptureList(const Token* tok);
228
const Token* findLambdaEndTokenWithoutAST(const Token* tok);
229
230
//---------------------------------------------------------------------------
231
#endif // tokenlistH