Blame lib/preprocessor.h

Packit 2035a7
/*
Packit 2035a7
 * Cppcheck - A tool for static C/C++ code analysis
Packit 2035a7
 * Copyright (C) 2007-2017 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 preprocessorH
Packit 2035a7
#define preprocessorH
Packit 2035a7
//---------------------------------------------------------------------------
Packit 2035a7
Packit 2035a7
#include "config.h"
Packit 2035a7
Packit 2035a7
#include <simplecpp.h>
Packit 2035a7
#include <istream>
Packit 2035a7
#include <list>
Packit 2035a7
#include <map>
Packit 2035a7
#include <set>
Packit 2035a7
#include <string>
Packit 2035a7
#include <vector>
Packit 2035a7
Packit 2035a7
class ErrorLogger;
Packit 2035a7
class Settings;
Packit 2035a7
Packit 2035a7
/**
Packit 2035a7
 * @brief A preprocessor directive
Packit 2035a7
 * Each preprocessor directive (\#include, \#define, \#undef, \#if, \#ifdef, \#else, \#endif)
Packit 2035a7
 * will be recorded as an instance of this class.
Packit 2035a7
 *
Packit 2035a7
 * file and linenr denote the location where where the directive is defined.
Packit 2035a7
 *
Packit 2035a7
 */
Packit 2035a7
Packit 2035a7
class CPPCHECKLIB Directive {
Packit 2035a7
public:
Packit 2035a7
    /** name of (possibly included) file where directive is defined */
Packit 2035a7
    std::string file;
Packit 2035a7
Packit 2035a7
    /** line number in (possibly included) file where directive is defined */
Packit 2035a7
    unsigned int linenr;
Packit 2035a7
Packit 2035a7
    /** the actual directive text */
Packit 2035a7
    std::string str;
Packit 2035a7
Packit 2035a7
    /** record a directive (possibly filtering src) */
Packit 2035a7
    Directive(const std::string &_file, const int _linenr, const std::string &_str);
Packit 2035a7
};
Packit 2035a7
Packit 2035a7
/// @addtogroup Core
Packit 2035a7
/// @{
Packit 2035a7
Packit 2035a7
/**
Packit 2035a7
 * @brief The cppcheck preprocessor.
Packit 2035a7
 * The preprocessor has special functionality for extracting the various ifdef
Packit 2035a7
 * configurations that exist in a source file.
Packit 2035a7
 */
Packit 2035a7
class CPPCHECKLIB Preprocessor {
Packit 2035a7
public:
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * Include file types.
Packit 2035a7
     */
Packit 2035a7
    enum HeaderTypes {
Packit 2035a7
        NoHeader = 0,
Packit 2035a7
        UserHeader,
Packit 2035a7
        SystemHeader
Packit 2035a7
    };
Packit 2035a7
Packit 2035a7
    /** character that is inserted in expanded macros */
Packit 2035a7
    static char macroChar;
Packit 2035a7
Packit 2035a7
    explicit Preprocessor(Settings& settings, ErrorLogger *errorLogger = nullptr);
Packit 2035a7
    virtual ~Preprocessor();
Packit 2035a7
Packit 2035a7
    static bool missingIncludeFlag;
Packit 2035a7
    static bool missingSystemIncludeFlag;
Packit 2035a7
Packit 2035a7
    void inlineSuppressions(const simplecpp::TokenList &tokens);
Packit 2035a7
Packit 2035a7
    void setDirectives(const simplecpp::TokenList &tokens);
Packit 2035a7
Packit 2035a7
    /** list of all directives met while preprocessing file */
Packit 2035a7
    const std::list<Directive> &getDirectives() const {
Packit 2035a7
        return directives;
Packit 2035a7
    }
Packit 2035a7
Packit 2035a7
    std::set<std::string> getConfigs(const simplecpp::TokenList &tokens) const;
Packit 2035a7
Packit 2035a7
    void loadFiles(const simplecpp::TokenList &rawtokens, std::vector<std::string> &files);
Packit 2035a7
Packit 2035a7
    void removeComments();
Packit 2035a7
Packit 2035a7
    void setPlatformInfo(simplecpp::TokenList *tokens) const;
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * Extract the code for each configuration
Packit 2035a7
     * @param istr The (file/string) stream to read from.
Packit 2035a7
     * @param result The map that will get the results
Packit 2035a7
     * @param filename The name of the file to check e.g. "src/main.cpp"
Packit 2035a7
     * @param includePaths List of paths where include files should be searched from,
Packit 2035a7
     * single path can be e.g. in format "include/".
Packit 2035a7
     * There must be a path separator at the end. Default parameter is empty list.
Packit 2035a7
     * Note that if path from given filename is also extracted and that is used as
Packit 2035a7
     * a last include path if include file was not found from earlier paths.
Packit 2035a7
     */
Packit 2035a7
    void preprocess(std::istream &istr, std::map<std::string, std::string> &result, const std::string &filename, const std::list<std::string> &includePaths = std::list<std::string>());
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * Extract the code for each configuration. Use this with getcode() to get the
Packit 2035a7
     * file data for each individual configuration.
Packit 2035a7
     *
Packit 2035a7
     * @param srcCodeStream The (file/string) stream to read from.
Packit 2035a7
     * @param processedFile Give reference to empty string as a parameter,
Packit 2035a7
     * function will fill processed file here. Use this also as a filedata parameter
Packit 2035a7
     * to getcode() if you received more than once configurations.
Packit 2035a7
     * @param resultConfigurations List of configurations. Pass these one by one
Packit 2035a7
     * to getcode() with processedFile.
Packit 2035a7
     * @param filename The name of the file to check e.g. "src/main.cpp"
Packit 2035a7
     * @param includePaths List of paths where include files should be searched from,
Packit 2035a7
     * single path can be e.g. in format "include/".
Packit 2035a7
     * There must be a path separator at the end. Default parameter is empty list.
Packit 2035a7
     * Note that if path from given filename is also extracted and that is used as
Packit 2035a7
     * a last include path if include file was not found from earlier paths.
Packit 2035a7
     */
Packit 2035a7
    void preprocess(std::istream &srcCodeStream, std::string &processedFile, std::list<std::string> &resultConfigurations, const std::string &filename, const std::list<std::string> &includePaths);
Packit 2035a7
Packit 2035a7
    simplecpp::TokenList preprocess(const simplecpp::TokenList &tokens1, const std::string &cfg, std::vector<std::string> &files);
Packit 2035a7
Packit 2035a7
    std::string getcode(const simplecpp::TokenList &tokens1, const std::string &cfg, std::vector<std::string> &files, const bool writeLocations);
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * Get preprocessed code for a given configuration
Packit 2035a7
     * @param filedata file data including preprocessing 'if', 'define', etc
Packit 2035a7
     * @param cfg configuration to read out
Packit 2035a7
     * @param filename name of source file
Packit 2035a7
     */
Packit 2035a7
    std::string getcode(const std::string &filedata, const std::string &cfg, const std::string &filename);
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * preprocess all whitespaces
Packit 2035a7
     * @param processedFile The data to be processed
Packit 2035a7
     */
Packit 2035a7
    static void preprocessWhitespaces(std::string &processedFile);
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * make sure empty configuration macros are not used in code. the given code must be a single configuration
Packit 2035a7
     * @param cfg configuration
Packit 2035a7
     * @param macroUsageList macro usage list
Packit 2035a7
     * @return true => configuration is valid
Packit 2035a7
     */
Packit 2035a7
    bool validateCfg(const std::string &cfg, const std::list<simplecpp::MacroUsage> &macroUsageList);
Packit 2035a7
    void validateCfgError(const std::string &file, const unsigned int line, const std::string &cfg, const std::string ¯o;;
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * Calculate CRC32 checksum. Using toolinfo, tokens1, filedata.
Packit 2035a7
     *
Packit 2035a7
     * @param tokens1    Sourcefile tokens
Packit 2035a7
     * @param toolinfo   Arbitrary extra toolinfo
Packit 2035a7
     * @return CRC32 checksum
Packit 2035a7
     */
Packit 2035a7
    unsigned int calculateChecksum(const simplecpp::TokenList &tokens1, const std::string &toolinfo) const;
Packit 2035a7
Packit 2035a7
    void simplifyPragmaAsm(simplecpp::TokenList *tokenList);
Packit 2035a7
Packit 2035a7
private:
Packit 2035a7
Packit 2035a7
    static void simplifyPragmaAsmPrivate(simplecpp::TokenList *tokenList);
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * Remove space that has new line character on left or right side of it.
Packit 2035a7
     *
Packit 2035a7
     * @param str The string to be converted
Packit 2035a7
     * @return The string where space characters have been removed.
Packit 2035a7
     */
Packit 2035a7
    static std::string removeSpaceNearNL(const std::string &str);
Packit 2035a7
Packit 2035a7
public:
Packit 2035a7
Packit 2035a7
Packit 2035a7
    static void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings);
Packit 2035a7
Packit 2035a7
    void setFile0(const std::string &f) {
Packit 2035a7
        file0 = f;
Packit 2035a7
    }
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * dump all directives present in source file
Packit 2035a7
     */
Packit 2035a7
    void dump(std::ostream &out) const;
Packit 2035a7
Packit 2035a7
    void reportOutput(const simplecpp::OutputList &outputList, bool showerror);
Packit 2035a7
Packit 2035a7
private:
Packit 2035a7
    void missingInclude(const std::string &filename, unsigned int linenr, const std::string &header, HeaderTypes headerType);
Packit 2035a7
    void error(const std::string &filename, unsigned int linenr, const std::string &msg;;
Packit 2035a7
Packit 2035a7
    Settings& _settings;
Packit 2035a7
    ErrorLogger *_errorLogger;
Packit 2035a7
Packit 2035a7
    /** list of all directives met while preprocessing file */
Packit 2035a7
    std::list<Directive> directives;
Packit 2035a7
Packit 2035a7
    std::map<std::string, simplecpp::TokenList *> tokenlists;
Packit 2035a7
Packit 2035a7
    /** filename for cpp/c file - useful when reporting errors */
Packit 2035a7
    std::string file0;
Packit 2035a7
};
Packit 2035a7
Packit 2035a7
/// @}
Packit 2035a7
//---------------------------------------------------------------------------
Packit 2035a7
#endif // preprocessorH