Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 43 additions & 41 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <set>
#include <sstream>
#include <stack>
Expand All @@ -48,6 +49,7 @@
#ifdef SIMPLECPP_WINDOWS
# include <mutex>
#endif
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -1486,9 +1488,9 @@ namespace simplecpp {

class Macro {
public:
explicit Macro(std::vector<std::string> &f) : nameTokDef(nullptr), valueToken(nullptr), endToken(nullptr), files(f), tokenListDefine(f), variadic(false), variadicOpt(false), valueDefinedInCode_(false) {}
explicit Macro(std::vector<std::string> &f) : valueToken(nullptr), endToken(nullptr), files(f), variadic(false), variadicOpt(false), valueDefinedInCode_(false) {}

Macro(const Token *tok, std::vector<std::string> &f) : nameTokDef(nullptr), files(f), tokenListDefine(f), valueDefinedInCode_(true) {
Macro(const Token *tok, std::vector<std::string> &f) : files(f), valueDefinedInCode_(true) {
if (sameline(tok->previousSkipComments(), tok))
throw std::runtime_error("bad macro syntax");
if (tok->op != '#')
Expand All @@ -1504,15 +1506,15 @@ namespace simplecpp {
throw std::runtime_error("bad macro syntax");
}

Macro(const std::string &name, const std::string &value, std::vector<std::string> &f) : nameTokDef(nullptr), files(f), tokenListDefine(f), valueDefinedInCode_(false) {
Macro(const std::string &name, const std::string &value, std::vector<std::string> &f) : files(f), tokenListDefine(new TokenList(f)), valueDefinedInCode_(false) {
const std::string def(name + ' ' + value);
StdCharBufStream stream(reinterpret_cast<const unsigned char*>(def.data()), def.size());
tokenListDefine.readfile(stream);
if (!parseDefine(tokenListDefine.cfront()))
tokenListDefine->readfile(stream);
if (!parseDefine(tokenListDefine->cfront()))
throw std::runtime_error("bad macro syntax. macroname=" + name + " value=" + value);
}

Macro(const Macro &other) : nameTokDef(nullptr), files(other.files), tokenListDefine(other.files), valueDefinedInCode_(other.valueDefinedInCode_) {
Macro(const Macro &other) : files(other.files), valueDefinedInCode_(other.valueDefinedInCode_) {
// TODO: remove the try-catch - see #537
// avoid bugprone-exception-escape clang-tidy warning
try {
Expand All @@ -1530,11 +1532,11 @@ namespace simplecpp {
if (this != &other) {
files = other.files;
valueDefinedInCode_ = other.valueDefinedInCode_;
if (other.tokenListDefine.empty())
tokenListDefine = other.tokenListDefine;
if (!tokenListDefine || tokenListDefine->empty())
parseDefine(other.nameTokDef);
else {
tokenListDefine = other.tokenListDefine;
parseDefine(tokenListDefine.cfront());
parseDefine(tokenListDefine->cfront());
}
usageList = other.usageList;
}
Expand Down Expand Up @@ -2375,7 +2377,7 @@ namespace simplecpp {
}

/** name token in definition */
const Token *nameTokDef;
const Token *nameTokDef{};

/** arguments for macro */
std::vector<TokenString> args;
Expand All @@ -2390,7 +2392,7 @@ namespace simplecpp {
std::vector<std::string> &files;

/** this is used for -D where the definition is not seen anywhere in code */
TokenList tokenListDefine;
std::shared_ptr<TokenList> tokenListDefine;

/** usage of this macro */
mutable std::list<Location> usageList;
Expand Down Expand Up @@ -3304,26 +3306,26 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
#endif

std::map<std::string, std::size_t> sizeOfType(rawtokens.sizeOfType);
sizeOfType.insert(std::make_pair("char", sizeof(char)));
sizeOfType.insert(std::make_pair("short", sizeof(short)));
sizeOfType.insert(std::make_pair("short int", sizeOfType["short"]));
sizeOfType.insert(std::make_pair("int", sizeof(int)));
sizeOfType.insert(std::make_pair("long", sizeof(long)));
sizeOfType.insert(std::make_pair("long int", sizeOfType["long"]));
sizeOfType.insert(std::make_pair("long long", sizeof(long long)));
sizeOfType.insert(std::make_pair("float", sizeof(float)));
sizeOfType.insert(std::make_pair("double", sizeof(double)));
sizeOfType.insert(std::make_pair("long double", sizeof(long double)));
sizeOfType.insert(std::make_pair("char *", sizeof(char *)));
sizeOfType.insert(std::make_pair("short *", sizeof(short *)));
sizeOfType.insert(std::make_pair("short int *", sizeOfType["short *"]));
sizeOfType.insert(std::make_pair("int *", sizeof(int *)));
sizeOfType.insert(std::make_pair("long *", sizeof(long *)));
sizeOfType.insert(std::make_pair("long int *", sizeOfType["long *"]));
sizeOfType.insert(std::make_pair("long long *", sizeof(long long *)));
sizeOfType.insert(std::make_pair("float *", sizeof(float *)));
sizeOfType.insert(std::make_pair("double *", sizeof(double *)));
sizeOfType.insert(std::make_pair("long double *", sizeof(long double *)));
sizeOfType.emplace("char", sizeof(char));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block could be an init list, followed by sizeOfType.insert(rawtokens.sizeOfType.begin(), rawtokens.sizeOfType.end()).

sizeOfType.emplace("short", sizeof(short));
sizeOfType.emplace("short int", sizeOfType["short"]);
sizeOfType.emplace("int", sizeof(int));
sizeOfType.emplace("long", sizeof(long));
sizeOfType.emplace("long int", sizeOfType["long"]);
sizeOfType.emplace("long long", sizeof(long long));
sizeOfType.emplace("float", sizeof(float));
sizeOfType.emplace("double", sizeof(double));
sizeOfType.emplace("long double", sizeof(long double));
sizeOfType.emplace("char *", sizeof(char *));
sizeOfType.emplace("short *", sizeof(short *));
sizeOfType.emplace("short int *", sizeOfType["short *"]);
sizeOfType.emplace("int *", sizeof(int *));
sizeOfType.emplace("long *", sizeof(long *));
sizeOfType.emplace("long int *", sizeOfType["long *"]);
sizeOfType.emplace("long long *", sizeof(long long *));
sizeOfType.emplace("float *", sizeof(float *));
sizeOfType.emplace("double *", sizeof(double *));
sizeOfType.emplace("long double *", sizeof(long double *));

// use a dummy vector for the macros because as this is not part of the file and would add an empty entry - e.g. /usr/include/poll.h
std::vector<std::string> dummy;
Expand All @@ -3343,27 +3345,27 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
const std::string lhs(macrostr.substr(0,eq));
const std::string rhs(eq==std::string::npos ? std::string("1") : macrostr.substr(eq+1));
const Macro macro(lhs, rhs, dummy);
macros.insert(std::pair<TokenString,Macro>(macro.name(), macro));
macros.emplace(macro.name(), macro);
}

const bool strictAnsiUndefined = dui.undefined.find("__STRICT_ANSI__") != dui.undefined.cend();
if (!isGnu(dui) && !strictAnsiDefined && !strictAnsiUndefined)
macros.insert(std::pair<TokenString, Macro>("__STRICT_ANSI__", Macro("__STRICT_ANSI__", "1", dummy)));
macros.emplace(std::piecewise_construct, std::forward_as_tuple("__STRICT_ANSI__"), std::forward_as_tuple("__STRICT_ANSI__", "1", dummy));

macros.insert(std::make_pair("__FILE__", Macro("__FILE__", "__FILE__", dummy)));
macros.insert(std::make_pair("__LINE__", Macro("__LINE__", "__LINE__", dummy)));
macros.insert(std::make_pair("__COUNTER__", Macro("__COUNTER__", "__COUNTER__", dummy)));
macros.emplace(std::piecewise_construct, std::forward_as_tuple("__FILE__"), std::forward_as_tuple("__FILE__", "__FILE__", dummy));
macros.emplace(std::piecewise_construct, std::forward_as_tuple("__LINE__"), std::forward_as_tuple("__LINE__", "__LINE__", dummy));
macros.emplace(std::piecewise_construct, std::forward_as_tuple("__COUNTER__"), std::forward_as_tuple("__COUNTER__", "__COUNTER__", dummy));
struct tm ltime = {};
getLocaltime(ltime);
macros.insert(std::make_pair("__DATE__", Macro("__DATE__", getDateDefine(&ltime), dummy)));
macros.insert(std::make_pair("__TIME__", Macro("__TIME__", getTimeDefine(&ltime), dummy)));
macros.emplace(std::piecewise_construct, std::forward_as_tuple("__DATE__"), std::forward_as_tuple("__DATE__", getDateDefine(&ltime), dummy));
macros.emplace(std::piecewise_construct, std::forward_as_tuple("__TIME__"), std::forward_as_tuple("__TIME__", getTimeDefine(&ltime), dummy));

if (!dui.std.empty()) {
const cstd_t c_std = simplecpp::getCStd(dui.std);
if (c_std != CUnknown) {
const std::string std_def = simplecpp::getCStdString(c_std);
if (!std_def.empty())
macros.insert(std::make_pair("__STDC_VERSION__", Macro("__STDC_VERSION__", std_def, dummy)));
macros.emplace(std::piecewise_construct, std::forward_as_tuple("__STDC_VERSION__"), std::forward_as_tuple("__STDC_VERSION__", std_def, dummy));
} else {
const cppstd_t cpp_std = simplecpp::getCppStd(dui.std);
if (cpp_std == CPPUnknown) {
Expand All @@ -3380,7 +3382,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
}
const std::string std_def = simplecpp::getCppStdString(cpp_std);
if (!std_def.empty())
macros.insert(std::make_pair("__cplusplus", Macro("__cplusplus", std_def, dummy)));
macros.emplace(std::piecewise_construct, std::forward_as_tuple("__cplusplus"), std::forward_as_tuple("__cplusplus", std_def, dummy));
}
}

Expand Down Expand Up @@ -3467,7 +3469,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
if (dui.undefined.find(macro.name()) == dui.undefined.end()) {
const MacroMap::iterator it = macros.find(macro.name());
if (it == macros.end())
macros.insert(std::pair<TokenString, Macro>(macro.name(), macro));
macros.emplace(macro.name(), macro);
else
it->second = macro;
}
Expand Down
Loading