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
44 changes: 23 additions & 21 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <set>
#include <sstream>
#include <stack>
Expand All @@ -47,6 +48,7 @@
#ifdef SIMPLECPP_WINDOWS
# include <mutex>
#endif
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -1498,12 +1500,12 @@ 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) {}

/**
* @throws std::runtime_error thrown on bad macro syntax
*/
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 @@ -1522,15 +1524,15 @@ namespace simplecpp {
/**
* @throws std::runtime_error thrown on 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 @@ -1548,12 +1550,12 @@ 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 @@ -2408,7 +2410,7 @@ namespace simplecpp {
}

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

/** arguments for macro */
std::vector<TokenString> args;
Expand All @@ -2423,7 +2425,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 @@ -3367,7 +3369,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
const std::string rhs(eq==std::string::npos ? std::string("1") : macrostr.substr(eq+1));
try {
const Macro macro(lhs, rhs, dummy);
macros.insert(std::pair<TokenString,Macro>(macro.name(), macro));
macros.emplace(macro.name(), macro);
} catch (const std::runtime_error& e) {
if (outputList) {
simplecpp::Output err{
Expand All @@ -3384,22 +3386,22 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL

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 @@ -3416,7 +3418,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 @@ -3503,7 +3505,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