Blame lib/tokenlist.h

Packit 2035a7
/*
Packit 2035a7
 * Cppcheck - A tool for static C/C++ code analysis
Packit 2035a7
 * Copyright (C) 2007-2018 Cppcheck team.
Packit 2035a7
 *
Packit 2035a7
 * This program is free software: you can redistribute it and/or modify
Packit 2035a7
 * it under the terms of the GNU General Public License as published by
Packit 2035a7
 * the Free Software Foundation, either version 3 of the License, or
Packit 2035a7
 * (at your option) any later version.
Packit 2035a7
 *
Packit 2035a7
 * This program is distributed in the hope that it will be useful,
Packit 2035a7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 2035a7
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 2035a7
 * GNU General Public License for more details.
Packit 2035a7
 *
Packit 2035a7
 * You should have received a copy of the GNU General Public License
Packit 2035a7
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit 2035a7
 */
Packit 2035a7
Packit 2035a7
//---------------------------------------------------------------------------
Packit 2035a7
#ifndef tokenlistH
Packit 2035a7
#define tokenlistH
Packit 2035a7
//---------------------------------------------------------------------------
Packit 2035a7
Packit 2035a7
#include "config.h"
Packit 2035a7
Packit 2035a7
#include <string>
Packit 2035a7
#include <vector>
Packit 2035a7
Packit 2035a7
class Settings;
Packit 2035a7
class Token;
Packit 2035a7
Packit 2035a7
namespace simplecpp {
Packit 2035a7
    class TokenList;
Packit 2035a7
}
Packit 2035a7
Packit 2035a7
/// @addtogroup Core
Packit 2035a7
/// @{
Packit 2035a7
Packit 2035a7
class CPPCHECKLIB TokenList {
Packit 2035a7
public:
Packit 2035a7
    explicit TokenList(const Settings* settings);
Packit 2035a7
    ~TokenList();
Packit 2035a7
Packit 2035a7
    void setSettings(const Settings *settings) {
Packit 2035a7
        _settings = settings;
Packit 2035a7
    }
Packit 2035a7
Packit 2035a7
    const Settings *getSettings() const {
Packit 2035a7
        return _settings;
Packit 2035a7
    }
Packit 2035a7
Packit 2035a7
    /** @return the source file path. e.g. "file.cpp" */
Packit 2035a7
    const std::string& getSourceFilePath() const;
Packit 2035a7
Packit 2035a7
    /** Is the code C. Used for bailouts */
Packit 2035a7
    bool isC() const {
Packit 2035a7
        return _isC;
Packit 2035a7
    }
Packit 2035a7
Packit 2035a7
    /** Is the code CPP. Used for bailouts */
Packit 2035a7
    bool isCPP() const {
Packit 2035a7
        return _isCPP;
Packit 2035a7
    }
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * Delete all tokens in given token list
Packit 2035a7
     * @param tok token list to delete
Packit 2035a7
     */
Packit 2035a7
    static void deleteTokens(Token *tok);
Packit 2035a7
Packit 2035a7
    void addtoken(std::string str, const unsigned int lineno, const unsigned int fileno, bool split = false);
Packit 2035a7
    void addtoken(const Token *tok, const unsigned int lineno, const unsigned int fileno);
Packit 2035a7
Packit 2035a7
    static void insertTokens(Token *dest, const Token *src, unsigned int n);
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * Copy tokens.
Packit 2035a7
     * @param dest destination token where copied tokens will be inserted after
Packit 2035a7
     * @param first first token to copy
Packit 2035a7
     * @param last last token to copy
Packit 2035a7
     * @param one_line true=>copy all tokens to the same line as dest. false=>copy all tokens to dest while keeping the 'line breaks'
Packit 2035a7
     * @return new location of last token copied
Packit 2035a7
     */
Packit 2035a7
    static Token *copyTokens(Token *dest, const Token *first, const Token *last, bool one_line = true);
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * Create tokens from code.
Packit 2035a7
     * The code must be preprocessed first:
Packit 2035a7
     * - multiline strings are not handled.
Packit 2035a7
     * - UTF in the code are not handled.
Packit 2035a7
     * - comments are not handled.
Packit 2035a7
     * @param code input stream for code
Packit 2035a7
     * @param file0 source file name
Packit 2035a7
     */
Packit 2035a7
    bool createTokens(std::istream &code, const std::string& file0 = emptyString);
Packit 2035a7
Packit 2035a7
    void createTokens(const simplecpp::TokenList *tokenList);
Packit 2035a7
Packit 2035a7
    /** Deallocate list */
Packit 2035a7
    void deallocateTokens();
Packit 2035a7
Packit 2035a7
    /** append file name if seen the first time; return its index in any case */
Packit 2035a7
    unsigned int appendFileIfNew(const std::string &fileName);
Packit 2035a7
Packit 2035a7
    /** get first token of list */
Packit 2035a7
    const Token *front() const {
Packit 2035a7
        return _front;
Packit 2035a7
    }
Packit 2035a7
    Token *front() {
Packit 2035a7
        return _front;
Packit 2035a7
    }
Packit 2035a7
Packit 2035a7
    /** get last token of list */
Packit 2035a7
    const Token *back() const {
Packit 2035a7
        return _back;
Packit 2035a7
    }
Packit 2035a7
    Token *back() {
Packit 2035a7
        return _back;
Packit 2035a7
    }
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * Get filenames (the sourcefile + the files it include).
Packit 2035a7
     * The first filename is the filename for the sourcefile
Packit 2035a7
     * @return vector with filenames
Packit 2035a7
     */
Packit 2035a7
    const std::vector<std::string>& getFiles() const {
Packit 2035a7
        return _files;
Packit 2035a7
    }
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * get filename for given token
Packit 2035a7
     * @param tok The given token
Packit 2035a7
     * @return filename for the given token
Packit 2035a7
     */
Packit 2035a7
    const std::string& file(const Token *tok) const;
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * Get file:line for a given token
Packit 2035a7
     * @param tok given token
Packit 2035a7
     * @return location for given token
Packit 2035a7
     */
Packit 2035a7
    std::string fileLine(const Token *tok) const;
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
    * Calculates a 64-bit checksum of the token list used to compare
Packit 2035a7
    * multiple token lists with each other as quickly as possible.
Packit 2035a7
    */
Packit 2035a7
    unsigned long long calculateChecksum() const;
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * Create abstract syntax tree.
Packit 2035a7
     */
Packit 2035a7
    void createAst();
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * Check abstract syntax tree.
Packit 2035a7
     * Throws InternalError on failure
Packit 2035a7
     */
Packit 2035a7
    void validateAst() const;
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * Verify that the given token is an element of the tokenlist.
Packit 2035a7
     * That method is implemented for debugging purposes.
Packit 2035a7
     * @param[in] tok token to be checked
Packit 2035a7
     * \return true if token was found in tokenlist, false else. In case of nullptr true is returned.
Packit 2035a7
     */
Packit 2035a7
    bool validateToken(const Token* tok) const;
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * Collapse compound standard types into a single token.
Packit 2035a7
     * unsigned long long int => long _isUnsigned=true,_isLong=true
Packit 2035a7
     */
Packit 2035a7
    void simplifyStdType();
Packit 2035a7
Packit 2035a7
private:
Packit 2035a7
Packit 2035a7
    /** Disable copy constructor, no implementation */
Packit 2035a7
    TokenList(const TokenList &);
Packit 2035a7
Packit 2035a7
    /** Disable assignment operator, no implementation */
Packit 2035a7
    TokenList &operator=(const TokenList &);
Packit 2035a7
Packit 2035a7
    /** Token list */
Packit 2035a7
    Token *_front, *_back;
Packit 2035a7
Packit 2035a7
    /** filenames for the tokenized source code (source + included) */
Packit 2035a7
    std::vector<std::string> _files;
Packit 2035a7
Packit 2035a7
    /** settings */
Packit 2035a7
    const Settings* _settings;
Packit 2035a7
Packit 2035a7
    /** File is known to be C/C++ code */
Packit 2035a7
    bool _isC, _isCPP;
Packit 2035a7
};
Packit 2035a7
Packit 2035a7
/// @}
Packit 2035a7
Packit 2035a7
//---------------------------------------------------------------------------
Packit 2035a7
#endif // tokenlistH