Skip to content

Commit 531a5c0

Browse files
committed
Improve compiler compatibility for early gcc versions
1 parent 348ff6f commit 531a5c0

File tree

10 files changed

+109
-40
lines changed

10 files changed

+109
-40
lines changed

src/ast.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,15 @@ namespace Sass {
168168

169169
bool Compound_Selector::has_parent_ref() const
170170
{
171-
for (Simple_Selector_Obj s : *this) {
171+
for (Simple_Selector_Obj s : elements()) {
172172
if (s && s->has_parent_ref()) return true;
173173
}
174174
return false;
175175
}
176176

177177
bool Compound_Selector::has_real_parent_ref() const
178178
{
179-
for (Simple_Selector_Obj s : *this) {
179+
for (Simple_Selector_Obj s : elements()) {
180180
if (s && s->has_real_parent_ref()) return true;
181181
}
182182
return false;

src/ast.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ namespace Sass {
366366
: elements_(ExpressionMap(s)),
367367
list_(std::vector<Expression_Obj>()),
368368
hash_(0), duplicate_key_(NULL)
369-
{ elements_.reserve(s); list_.reserve(s); }
369+
{ list_.reserve(s); reset_duplicate_key(); }
370370
virtual ~Hashed();
371371
size_t length() const { return list_.size(); }
372372
bool empty() const { return list_.empty(); }
@@ -1478,7 +1478,7 @@ namespace Sass {
14781478
if (hash_ == 0) {
14791479
hash_ = std::hash<std::string>()(name());
14801480
for (auto argument : arguments()->elements())
1481-
hash_combine(hash_, argument->hash());
1481+
{ hash_combine(hash_, argument->hash()); }
14821482
}
14831483
return hash_;
14841484
}
@@ -1583,9 +1583,9 @@ namespace Sass {
15831583
if (hash_ == 0) {
15841584
hash_ = std::hash<double>()(value_);
15851585
for (const auto numerator : numerator_units())
1586-
hash_combine(hash_, std::hash<std::string>()(numerator));
1586+
{ hash_combine(hash_, std::hash<std::string>()(numerator)); }
15871587
for (const auto denominator : denominator_units())
1588-
hash_combine(hash_, std::hash<std::string>()(denominator));
1588+
{ hash_combine(hash_, std::hash<std::string>()(denominator)); }
15891589
}
15901590
return hash_;
15911591
}
@@ -1768,7 +1768,7 @@ namespace Sass {
17681768
{
17691769
if (hash_ == 0) {
17701770
for (auto string : elements())
1771-
hash_combine(hash_, string->hash());
1771+
{ hash_combine(hash_, string->hash()); }
17721772
}
17731773
return hash_;
17741774
}

src/color_maps.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ namespace Sass {
613613
Color_Ptr_Const name_to_color(const std::string& key)
614614
{
615615
// case insensitive lookup. See #2462
616-
std::string lower{key};
616+
std::string lower(key);
617617
std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
618618

619619
auto p = names_to_colors.find(lower.c_str());

src/context.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ namespace Sass {
100100
collect_plugin_paths(c_options.plugin_paths);
101101

102102
// load plugins and register custom behaviors
103-
for(auto plug : plugin_paths) plugins.load_plugins(plug);
104-
for(auto fn : plugins.get_headers()) c_headers.push_back(fn);
105-
for(auto fn : plugins.get_importers()) c_importers.push_back(fn);
106-
for(auto fn : plugins.get_functions()) c_functions.push_back(fn);
103+
for(auto plug : plugin_paths) { plugins.load_plugins(plug); }
104+
for(auto fn : plugins.get_headers()) { c_headers.push_back(fn); }
105+
for(auto fn : plugins.get_importers()) { c_importers.push_back(fn); }
106+
for(auto fn : plugins.get_functions()) { c_functions.push_back(fn); }
107107

108108
// sort the items by priority (lowest first)
109109
sort (c_headers.begin(), c_headers.end(), sort_importers);

src/eval.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -337,15 +337,16 @@ namespace Sass {
337337
// try to use generic function
338338
if (env->has("@warn[f]")) {
339339

340-
// add call stack entry
341-
ctx.callee_stack.push_back({
340+
struct Sass_Callee callee = {
342341
"@warn",
343342
w->pstate().path,
344343
w->pstate().line + 1,
345344
w->pstate().column + 1,
346345
SASS_CALLEE_FUNCTION,
347346
{ env }
348-
});
347+
};
348+
// add call stack entry
349+
ctx.callee_stack.push_back(callee);
349350

350351
Definition_Ptr def = Cast<Definition>((*env)["@warn[f]"]);
351352
// Block_Obj body = def->block();
@@ -384,15 +385,16 @@ namespace Sass {
384385
// try to use generic function
385386
if (env->has("@error[f]")) {
386387

387-
// add call stack entry
388-
ctx.callee_stack.push_back({
388+
struct Sass_Callee callee = {
389389
"@error",
390390
e->pstate().path,
391391
e->pstate().line + 1,
392392
e->pstate().column + 1,
393393
SASS_CALLEE_FUNCTION,
394394
{ env }
395-
});
395+
};
396+
// add call stack entry
397+
ctx.callee_stack.push_back(callee);
396398

397399
Definition_Ptr def = Cast<Definition>((*env)["@error[f]"]);
398400
// Block_Obj body = def->block();
@@ -428,15 +430,16 @@ namespace Sass {
428430
// try to use generic function
429431
if (env->has("@debug[f]")) {
430432

431-
// add call stack entry
432-
ctx.callee_stack.push_back({
433+
struct Sass_Callee callee = {
433434
"@debug",
434435
d->pstate().path,
435436
d->pstate().line + 1,
436437
d->pstate().column + 1,
437438
SASS_CALLEE_FUNCTION,
438439
{ env }
439-
});
440+
};
441+
// add call stack entry
442+
ctx.callee_stack.push_back(callee);
440443

441444
Definition_Ptr def = Cast<Definition>((*env)["@debug[f]"]);
442445
// Block_Obj body = def->block();
@@ -1007,14 +1010,15 @@ namespace Sass {
10071010
bind(std::string("Function"), c->name(), params, args, &ctx, &fn_env, this);
10081011
Backtrace here(backtrace(), c->pstate(), ", in function `" + c->name() + "`");
10091012
exp.backtrace_stack.push_back(&here);
1010-
ctx.callee_stack.push_back({
1013+
struct Sass_Callee callee = {
10111014
c->name().c_str(),
10121015
c->pstate().path,
10131016
c->pstate().line + 1,
10141017
c->pstate().column + 1,
10151018
SASS_CALLEE_FUNCTION,
10161019
{ env }
1017-
});
1020+
};
1021+
ctx.callee_stack.push_back(callee);
10181022

10191023
// eval the body if user-defined or special, invoke underlying CPP function if native
10201024
if (body /* && !Prelexer::re_special_fun(name.c_str()) */) {
@@ -1048,14 +1052,15 @@ namespace Sass {
10481052

10491053
Backtrace here(backtrace(), c->pstate(), ", in function `" + c->name() + "`");
10501054
exp.backtrace_stack.push_back(&here);
1051-
ctx.callee_stack.push_back({
1055+
struct Sass_Callee callee = {
10521056
c->name().c_str(),
10531057
c->pstate().path,
10541058
c->pstate().line + 1,
10551059
c->pstate().column + 1,
10561060
SASS_CALLEE_C_FUNCTION,
10571061
{ env }
1058-
});
1062+
};
1063+
ctx.callee_stack.push_back(callee);
10591064

10601065
To_C to_c;
10611066
union Sass_Value* c_args = sass_make_list(params->length(), SASS_COMMA);
@@ -1177,7 +1182,7 @@ namespace Sass {
11771182
List_Obj ll = SASS_MEMORY_NEW(List, l->pstate(), 0, l->separator());
11781183
// this fixes an issue with bourbon sample, not really sure why
11791184
// if (l->size() && Cast<Null>((*l)[0])) { res += ""; }
1180-
for(Expression_Obj item : *l) {
1185+
for(Expression_Obj item : l->elements()) {
11811186
item->is_interpolant(l->is_interpolant());
11821187
std::string rl(""); interpolation(ctx, rl, item, into_quotes, l->is_interpolant());
11831188
bool is_null = Cast<Null>(item) != 0; // rl != ""

src/expand.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -713,14 +713,15 @@ namespace Sass {
713713
Arguments_Obj args = Cast<Arguments>(rv);
714714
Backtrace new_bt(backtrace(), c->pstate(), ", in mixin `" + c->name() + "`");
715715
backtrace_stack.push_back(&new_bt);
716-
ctx.callee_stack.push_back({
716+
struct Sass_Callee callee = {
717717
c->name().c_str(),
718718
c->pstate().path,
719719
c->pstate().line + 1,
720720
c->pstate().column + 1,
721721
SASS_CALLEE_MIXIN,
722722
{ env }
723-
});
723+
};
724+
ctx.callee_stack.push_back(callee);
724725

725726
Env new_env(def->environment());
726727
env_stack.push_back(&new_env);

src/extend.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,7 +1928,7 @@ namespace Sass {
19281928
recseen.insert(cur->head());
19291929
// create a copy since we add multiple items if stuff get unwrapped
19301930
Compound_Selector_Obj cpy_head = SASS_MEMORY_NEW(Compound_Selector, cur->pstate());
1931-
for (Simple_Selector_Obj hs : *cur->head()) {
1931+
for (Simple_Selector_Obj hs : cur->head()->elements()) {
19321932
if (Wrapped_Selector_Obj ws = Cast<Wrapped_Selector>(hs)) {
19331933
ws->selector(SASS_MEMORY_CLONE(ws->selector()));
19341934
if (Selector_List_Obj sl = Cast<Selector_List>(ws->selector())) {
@@ -2072,7 +2072,8 @@ namespace Sass {
20722072
// we set `extended` flag on extended selectors
20732073
if (b->is_root()) {
20742074
// debug_subset_map(subset_map);
2075-
for(auto const &it : subset_map.values()) {
2075+
auto values = subset_map.values();
2076+
for(auto it : values) {
20762077
Complex_Selector_Ptr sel = NULL;
20772078
Compound_Selector_Ptr ext = NULL;
20782079
if (it.first) sel = it.first->first();

src/functions.cpp

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@
3030
#include "wincrypt.h"
3131
#endif
3232

33+
#if defined __GNUC__ && ! defined __llvm__
34+
#define GCC_VERSION (__GNUC__ * 10000 \
35+
+ __GNUC_MINOR__ * 100 \
36+
+ __GNUC_PATCHLEVEL__)
37+
#if GCC_VERSION < 40500
38+
#include <tr1/random>
39+
#define IMPLEMENT_TR1
40+
#define tr1ns std::tr1
41+
#define uniform_real_distribution uniform_real
42+
#else
43+
#include <random>
44+
#define tr1ns std
45+
#endif
46+
#else
47+
#include <random>
48+
#define tr1ns std
49+
#endif
50+
3351
#define ARG(argname, argtype) get_arg<argtype>(argname, env, sig, pstate, backtrace)
3452
#define ARGR(argname, argtype, lo, hi) get_arg_r(argname, env, sig, pstate, lo, hi, backtrace)
3553
#define ARGM(argname, argtype, ctx) get_arg_m(argname, env, sig, pstate, backtrace, ctx)
@@ -215,9 +233,43 @@ namespace Sass {
215233
// random_device degrades sharply once the entropy pool
216234
// is exhausted. For practical use, random_device is
217235
// generally only used to seed a PRNG such as mt19937.
218-
static std::mt19937 rand(static_cast<unsigned int>(GetSeed()));
236+
static tr1ns::mt19937 rand(static_cast<unsigned int>(GetSeed()));
237+
238+
tr1ns::uniform_real_distribution<> std_dist(0, 1);
239+
#ifdef IMPLEMENT_TR1
240+
tr1ns::variate_generator <
241+
tr1ns::mt19937,
242+
tr1ns::uniform_real_distribution <double>
243+
> gen_std_dist(rand, std_dist);
244+
#endif
245+
246+
// Using ULONG_MAX here seems to fail on Mac OSX Clang!?
247+
tr1ns::uniform_real_distribution<> full_dist(0, 4294967296);
248+
#ifdef IMPLEMENT_TR1
249+
tr1ns::variate_generator <
250+
tr1ns::mt19937,
251+
tr1ns::uniform_real_distribution <double>
252+
> gen_full_dist(rand, full_dist);
253+
#endif
254+
255+
// helper function to retrieve a random number in interval
256+
// works around some compiler issues with older gcc versions
257+
static double random(double min, double max)
258+
{
259+
tr1ns::uniform_real_distribution<> distributor(min, max);
260+
#ifdef IMPLEMENT_TR1
261+
tr1ns::variate_generator <
262+
tr1ns::mt19937,
263+
tr1ns::uniform_real_distribution <>
264+
> gen(rand, distributor);
265+
distributor(rand);
266+
return gen();
267+
#else
268+
return distributor(rand);
269+
#endif
270+
}
219271

220-
// features
272+
// supported features lookup table
221273
static std::set<std::string> features {
222274
"global-variable-shadowing",
223275
"extend-selector-pseudoclass",
@@ -1199,13 +1251,19 @@ namespace Sass {
11991251
err << "Expected $limit to be an integer but got " << lv << " for `random'";
12001252
error(err.str(), pstate);
12011253
}
1202-
std::uniform_real_distribution<> distributor(1, lv + 1);
1203-
uint_fast32_t distributed = static_cast<uint_fast32_t>(distributor(rand));
1254+
// std::uniform_real_distribution<> distributor(1, lv + 1);
1255+
// uint_fast32_t distributed = static_cast<uint_fast32_t>(distributor(rand));
1256+
uint_fast32_t distributed = random(1, lv + 1);
12041257
return SASS_MEMORY_NEW(Number, pstate, (double)distributed);
12051258
}
12061259
else if (b) {
1207-
std::uniform_real_distribution<> distributor(0, 1);
1208-
double distributed = static_cast<double>(distributor(rand));
1260+
// std::uniform_real_distribution<> distributor(0, 1);
1261+
// double distributed = static_cast<double>(distributor(rand));
1262+
#ifdef IMPLEMENT_TR1
1263+
double distributed = gen_std_dist();
1264+
#else
1265+
double distributed = std_dist(rand);
1266+
#endif
12091267
return SASS_MEMORY_NEW(Number, pstate, distributed);
12101268
} else if (v) {
12111269
throw Exception::InvalidArgumentType(pstate, "random", "$limit", "number", v);
@@ -1973,8 +2031,11 @@ namespace Sass {
19732031
BUILT_IN(unique_id)
19742032
{
19752033
std::stringstream ss;
1976-
std::uniform_real_distribution<> distributor(0, 4294967296); // 16^8
1977-
uint_fast32_t distributed = static_cast<uint_fast32_t>(distributor(rand));
2034+
#ifdef IMPLEMENT_TR1
2035+
uint_fast32_t distributed = gen_full_dist();
2036+
#else
2037+
uint_fast32_t distributed = full_dist(rand);
2038+
#endif
19782039
ss << "u" << std::setfill('0') << std::setw(8) << std::hex << distributed;
19792040
return SASS_MEMORY_NEW(String_Quoted, pstate, ss.str());
19802041
}

src/parser.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ namespace Sass {
318318
do {
319319
while (lex< block_comment >());
320320
if (lex< quoted_string >()) {
321-
to_import.push_back(std::pair<std::string,Function_Call_Obj>(std::string(lexed), 0));
321+
to_import.push_back(std::pair<std::string,Function_Call_Obj>(std::string(lexed), (Function_Call*) NULL));
322322
}
323323
else if (lex< uri_prefix >()) {
324324
Arguments_Obj args = SASS_MEMORY_NEW(Arguments, pstate);
@@ -2938,6 +2938,7 @@ namespace Sass {
29382938
if (left_subpos && ellipsis_left) left = ellipsis + left.substr(left_subpos);
29392939
if (right_subpos && ellipsis_right) right = right.substr(right_subpos) + ellipsis;
29402940
// now pass new message to the more generic error function
2941+
if (source == NULL || *source == 0) source = pstate.src;
29412942
error(msg + prefix + quote(left) + middle + quote(right), pstate);
29422943
}
29432944

src/subset_map.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace Sass {
2424
continue;
2525
}
2626
const std::vector<std::pair<Compound_Selector_Obj, size_t> >& subsets = hash_[(*sel)[i]];
27-
for (const std::pair<Compound_Selector_Obj, size_t>& item : subsets) {
27+
for (const auto& item : subsets) {
2828
bool include = true;
2929
for (const Simple_Selector_Obj& it : item.first->elements()) {
3030
auto found = dict.find(it);

0 commit comments

Comments
 (0)