Skip to content

Commit

Permalink
Improve compiler compatibility for early gcc versions
Browse files Browse the repository at this point in the history
  • Loading branch information
mgreter committed May 1, 2016
1 parent e2c5a03 commit d918d31
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 41 deletions.
22 changes: 9 additions & 13 deletions src/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,23 @@ namespace Sass {
dynamic_cast<Supports_Operator*>(cond);
}

std::string & str_ltrim(std::string & str)
void str_rtrim(std::string& s, const std::string& delimiters = " \f\n\r\t\v" )
{
auto it2 = std::find_if( str.begin() , str.end() , [](char ch){ return !std::isspace<char>(ch , std::locale::classic() ) ; } );
str.erase( str.begin() , it2);
return str;
s.erase( s.find_last_not_of( delimiters ) + 1 );
}

std::string & str_rtrim(std::string & str)
void str_ltrim(std::string& s, const std::string& delimiters = " \f\n\r\t\v" )
{
auto it1 = std::find_if( str.rbegin() , str.rend() , [](char ch){ return !std::isspace<char>(ch , std::locale::classic() ) ; } );
str.erase( it1.base() , str.end() );
return str;
s.erase( 0, s.find_first_not_of( delimiters ) );
}

void String_Constant::rtrim()
{
value_ = str_rtrim(value_);
str_rtrim(value_);
}
void String_Constant::ltrim()
{
value_ = str_ltrim(value_);
str_ltrim(value_);
}
void String_Constant::trim()
{
Expand Down Expand Up @@ -151,7 +147,7 @@ namespace Sass {

bool Compound_Selector::has_parent_ref()
{
for (Simple_Selector* s : *this) {
for (Simple_Selector* s : elements()) {
if (s && s->has_parent_ref()) return true;
}
return false;
Expand Down Expand Up @@ -1183,7 +1179,7 @@ namespace Sass {
retval = this->tails(ctx, tails);
}

for (Simple_Selector* ss : *head) {
for (Simple_Selector* ss : head->elements()) {
if (Wrapped_Selector* ws = dynamic_cast<Wrapped_Selector*>(ss)) {
if (Selector_List* sl = dynamic_cast<Selector_List*>(ws->selector())) {
if (parents) ws->selector(sl->parentize(parents, ctx));
Expand Down Expand Up @@ -1384,7 +1380,7 @@ namespace Sass {

bool Selector_List::has_parent_ref()
{
for (Complex_Selector* s : *this) {
for (Complex_Selector* s : elements()) {
if (s && s->has_parent_ref()) return true;
}
return false;
Expand Down
12 changes: 7 additions & 5 deletions src/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ namespace Sass {
virtual void adjust_after_pushing(std::pair<Expression*, Expression*> p) { }
public:
Hashed(size_t s = 0) : elements_(ExpressionMap(s)), list_(std::vector<Expression*>())
{ elements_.reserve(s); list_.reserve(s); reset_duplicate_key(); }
{ /* elements_.reserve(s); */ list_.reserve(s); reset_duplicate_key(); }
virtual ~Hashed();
size_t length() const { return list_.size(); }
bool empty() const { return list_.empty(); }
Expand Down Expand Up @@ -1231,7 +1231,7 @@ namespace Sass {
if (hash_ == 0) {
hash_ = std::hash<std::string>()(name());
for (auto argument : arguments()->elements())
hash_combine(hash_, argument->hash());
{ hash_combine(hash_, argument->hash()); }
}
return hash_;
}
Expand Down Expand Up @@ -1359,10 +1359,12 @@ namespace Sass {
{
if (hash_ == 0) {
hash_ = std::hash<double>()(value_);
for (const auto numerator : numerator_units())
for (const auto numerator : numerator_units()) {
hash_combine(hash_, std::hash<std::string>()(numerator));
for (const auto denominator : denominator_units())
}
for (const auto denominator : denominator_units()) {
hash_combine(hash_, std::hash<std::string>()(denominator));
}
}
return hash_;
}
Expand Down Expand Up @@ -1512,7 +1514,7 @@ namespace Sass {
{
if (hash_ == 0) {
for (auto string : elements())
hash_combine(hash_, string->hash());
{ hash_combine(hash_, string->hash()); }
}
return hash_;
}
Expand Down
8 changes: 4 additions & 4 deletions src/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ namespace Sass {
collect_plugin_paths(c_options.plugin_paths);

// load plugins and register custom behaviors
for(auto plug : plugin_paths) plugins.load_plugins(plug);
for(auto fn : plugins.get_headers()) c_headers.push_back(fn);
for(auto fn : plugins.get_importers()) c_importers.push_back(fn);
for(auto fn : plugins.get_functions()) c_functions.push_back(fn);
for(auto plug : plugin_paths) { plugins.load_plugins(plug); }
for(auto fn : plugins.get_headers()) { c_headers.push_back(fn); }
for(auto fn : plugins.get_importers()) { c_importers.push_back(fn); }
for(auto fn : plugins.get_functions()) { c_functions.push_back(fn); }

// sort the items by priority (lowest first)
sort (c_headers.begin(), c_headers.end(), sort_importers);
Expand Down
2 changes: 1 addition & 1 deletion src/debugger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ inline void debug_ast(AST_Node* node, std::string ind, Env* env)
std::cerr << " (" << pstate_source_position(node) << ")";
std::cerr << " " << block->tabs() << std::endl;
// std::vector<std::string> files_;
for (auto imp : block->urls()) debug_ast(imp, ind + "@: ", env);
for (auto imp : block->urls()) { debug_ast(imp, ind + "@: ", env); }
debug_ast(block->media_queries(), ind + "@@ ");
} else if (dynamic_cast<Assignment*>(node)) {
Assignment* block = dynamic_cast<Assignment*>(node);
Expand Down
8 changes: 4 additions & 4 deletions src/eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,7 @@ namespace Sass {

if (Arguments* args = dynamic_cast<Arguments*>(ex)) {
List* ll = SASS_MEMORY_NEW(ctx.mem, List, args->pstate(), 0, SASS_COMMA);
for(auto arg : *args) {
for(auto arg : args->elements()) {
*ll << arg->value();
}
ll->is_interpolant(args->is_interpolant());
Expand Down Expand Up @@ -1072,7 +1072,7 @@ namespace Sass {
List* ll = SASS_MEMORY_NEW(ctx.mem, List, l->pstate(), 0, l->separator());
// this fixes an issue with bourbon sample, not really sure why
// if (l->size() && dynamic_cast<Null*>((*l)[0])) { res += ""; }
for(auto item : *l) {
for(auto item : l->elements()) {
item->is_interpolant(l->is_interpolant());
std::string rl(""); interpolation(ctx, rl, item, into_quotes, l->is_interpolant());
bool is_null = dynamic_cast<Null*>(item) != 0; // rl != ""
Expand Down Expand Up @@ -1324,11 +1324,11 @@ namespace Sass {
true);

if (ls && ls->is_arglist()) {
for (auto as : *ls) *arglist << as;
for (auto as : ls->elements()) { *arglist << as; }
} else if (ms) {
*aa << SASS_MEMORY_NEW(ctx.mem, Argument, splat->pstate(), ms, "", false, true);
} else if (ls) {
for (auto as : *ls) *arglist << as;
for (auto as : ls->elements()) { *arglist << as; }
} else {
*arglist << splat;
}
Expand Down
2 changes: 1 addition & 1 deletion src/expand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ namespace Sass {
if (schema->has_parent_ref()) s = eval(schema);
}
if (Selector_List* sl = dynamic_cast<Selector_List*>(s)) {
for (Complex_Selector* cs : *sl) {
for (Complex_Selector* cs : sl->elements()) {
if (cs != NULL && cs->head() != NULL) {
cs->head()->media_block(media_block_stack.back());
}
Expand Down
9 changes: 5 additions & 4 deletions src/extend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1723,7 +1723,7 @@ namespace Sass {

if (pHead) {
if (seen.find(*pHead) == seen.end()) {
for (Simple_Selector* pSimple : *pHead) {
for (Simple_Selector* pSimple : pHead->elements()) {
if (Wrapped_Selector* ws = dynamic_cast<Wrapped_Selector*>(pSimple)) {
if (Selector_List* sl = dynamic_cast<Selector_List*>(ws->selector())) {
for (Complex_Selector* cs : sl->elements()) {
Expand Down Expand Up @@ -1961,7 +1961,7 @@ namespace Sass {
pNewSelectors = remove_placeholders.remove_placeholders(pNewSelectors);

// unwrap all wrapped selectors with inner lists
for (Complex_Selector* cur : *pNewSelectors) {
for (Complex_Selector* cur : pNewSelectors->elements()) {
// process tails
while (cur) {
// process header
Expand All @@ -1970,7 +1970,7 @@ namespace Sass {
recseen.insert(*cur->head());
// create a copy since we add multiple items if stuff get unwrapped
Compound_Selector* cpy_head = SASS_MEMORY_NEW(ctx.mem, Compound_Selector, cur->pstate());
for (Simple_Selector* hs : *cur->head()) {
for (Simple_Selector* hs : cur->head()->elements()) {
if (Wrapped_Selector* ws = dynamic_cast<Wrapped_Selector*>(hs)) {
if (Selector_List* sl = dynamic_cast<Selector_List*>(ws->selector())) {
// special case for ruby ass
Expand Down Expand Up @@ -2092,7 +2092,8 @@ namespace Sass {
// we set `extended` flag on extended selectors
if (b->is_root()) {
// debug_subset_map(subset_map);
for(auto const &it : subset_map.values()) {
auto values = subset_map.values();
for(auto it : values) {
Complex_Selector* sel = it.first ? it.first->first() : NULL;
Compound_Selector* ext = it.second ? it.second : NULL;
if (ext && (ext->extended() || ext->is_optional())) continue;
Expand Down
70 changes: 64 additions & 6 deletions src/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <climits>
#include <sstream>
#include <string>
#include <iomanip>
Expand All @@ -30,6 +31,24 @@
#include "wincrypt.h"
#endif

#if defined __GNUC__ && ! defined __llvm__
#define GCC_VERSION (__GNUC__ * 10000 \
+ __GNUC_MINOR__ * 100 \
+ __GNUC_PATCHLEVEL__)
#if GCC_VERSION < 40500
#include <tr1/random>
#define IMPLEMENT_TR1
#define tr1ns std::tr1
#define uniform_real_distribution uniform_real
#else
#include <random>
#define tr1ns std
#endif
#else
#include <random>
#define tr1ns std
#endif

#define ARG(argname, argtype) get_arg<argtype>(argname, env, sig, pstate, backtrace)
#define ARGR(argname, argtype, lo, hi) get_arg_r(argname, env, sig, pstate, lo, hi, backtrace)
#define ARGM(argname, argtype, ctx) get_arg_m(argname, env, sig, pstate, backtrace, ctx)
Expand Down Expand Up @@ -229,9 +248,42 @@ namespace Sass {
// random_device degrades sharply once the entropy pool
// is exhausted. For practical use, random_device is
// generally only used to seed a PRNG such as mt19937.
static std::mt19937 rand(static_cast<unsigned int>(GetSeed()));
static tr1ns::mt19937 rand(static_cast<unsigned int>(GetSeed()));

tr1ns::uniform_real_distribution<> std_dist(0, 1);
#ifdef IMPLEMENT_TR1
tr1ns::variate_generator <
tr1ns::mt19937,
tr1ns::uniform_real_distribution <double>
> gen_std_dist(rand, std_dist);
#endif

tr1ns::uniform_real_distribution<> full_dist(0, ULONG_MAX);
#ifdef IMPLEMENT_TR1
tr1ns::variate_generator <
tr1ns::mt19937,
tr1ns::uniform_real_distribution <double>
> gen_full_dist(rand, full_dist);
#endif

// helper function to retrieve a random number in interval
// works around some compiler issues with older gcc versions
static double random(double min, double max)
{
tr1ns::uniform_real_distribution<> distributor(min, max);
#ifdef IMPLEMENT_TR1
tr1ns::variate_generator <
tr1ns::mt19937,
tr1ns::uniform_real_distribution <>
> gen(rand, distributor);
distributor(rand);
return gen();
#else
return distributor(rand);
#endif
}

// features
// supported features lookup table
static std::set<std::string> features {
"global-variable-shadowing",
"extend-selector-pseudoclass",
Expand Down Expand Up @@ -1188,13 +1240,19 @@ namespace Sass {
err << "Expected $limit to be an integer but got `" << v << "` for `random`";
error(err.str(), pstate);
}
std::uniform_real_distribution<> distributor(1, v + 1);
uint_fast32_t distributed = static_cast<uint_fast32_t>(distributor(rand));
// std::uniform_real_distribution<> distributor(1, v + 1);
// uint_fast32_t distributed = static_cast<uint_fast32_t>(distributor(rand));
uint_fast32_t distributed = random(1, v + 1);
return SASS_MEMORY_NEW(ctx.mem, Number, pstate, (double)distributed);
}
else if (b) {
std::uniform_real_distribution<> distributor(0, 1);
double distributed = static_cast<double>(distributor(rand));
// std::uniform_real_distribution<> distributor(0, 1);
// double distributed = static_cast<double>(distributor(rand));
#ifdef IMPLEMENT_TR1
double distributed = gen_std_dist();
#else
double distributed = std_dist(rand);
#endif
return SASS_MEMORY_NEW(ctx.mem, Number, pstate, distributed);
} else if (v) {
throw Exception::InvalidArgumentType(pstate, "random", "$limit", "number", v);
Expand Down
2 changes: 1 addition & 1 deletion src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ namespace Sass {
do {
while (lex< block_comment >());
if (lex< quoted_string >()) {
to_import.push_back(std::pair<std::string,Function_Call*>(std::string(lexed), 0));
to_import.push_back(std::pair<std::string,Function_Call*>(std::string(lexed), (Function_Call*)0));
}
else if (lex< uri_prefix >()) {
Arguments* args = SASS_MEMORY_NEW(ctx.mem, Arguments, pstate);
Expand Down
4 changes: 2 additions & 2 deletions src/remove_placeholders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ namespace Sass {
// Set the new placeholder selector list
r->selector(remove_placeholders(sl));
// Remove placeholders in wrapped selectors
for (Complex_Selector* cs : *sl) {
for (Complex_Selector* cs : sl->elements()) {
while (cs) {
if (cs->head()) {
for (Simple_Selector* ss : *cs->head()) {
for (Simple_Selector* ss : cs->head()->elements()) {
if (Wrapped_Selector* ws = dynamic_cast<Wrapped_Selector*>(ss)) {
if (Selector_List* sl = dynamic_cast<Selector_List*>(ws->selector())) {
Selector_List* clean = remove_placeholders(sl);
Expand Down

0 comments on commit d918d31

Please sign in to comment.