Skip to content

Commit d57a2bf

Browse files
committed
Improve compiler compatibility for early gcc versions
1 parent c95478c commit d57a2bf

File tree

10 files changed

+104
-43
lines changed

10 files changed

+104
-43
lines changed

src/ast.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,23 @@ namespace Sass {
2828
dynamic_cast<Supports_Operator*>(cond);
2929
}
3030

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

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

4541
void String_Constant::rtrim()
4642
{
47-
value_ = str_rtrim(value_);
43+
str_rtrim(value_);
4844
}
4945
void String_Constant::ltrim()
5046
{
51-
value_ = str_ltrim(value_);
47+
str_ltrim(value_);
5248
}
5349
void String_Constant::trim()
5450
{
@@ -151,7 +147,7 @@ namespace Sass {
151147

152148
bool Compound_Selector::has_parent_ref()
153149
{
154-
for (Simple_Selector* s : *this) {
150+
for (Simple_Selector* s : elements()) {
155151
if (s && s->has_parent_ref()) return true;
156152
}
157153
return false;
@@ -1183,7 +1179,7 @@ namespace Sass {
11831179
retval = this->tails(ctx, tails);
11841180
}
11851181

1186-
for (Simple_Selector* ss : *head) {
1182+
for (Simple_Selector* ss : head->elements()) {
11871183
if (Wrapped_Selector* ws = dynamic_cast<Wrapped_Selector*>(ss)) {
11881184
if (Selector_List* sl = dynamic_cast<Selector_List*>(ws->selector())) {
11891185
if (parents) ws->selector(sl->parentize(parents, ctx));
@@ -1384,7 +1380,7 @@ namespace Sass {
13841380

13851381
bool Selector_List::has_parent_ref()
13861382
{
1387-
for (Complex_Selector* s : *this) {
1383+
for (Complex_Selector* s : elements()) {
13881384
if (s && s->has_parent_ref()) return true;
13891385
}
13901386
return false;

src/ast.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ namespace Sass {
309309
virtual void adjust_after_pushing(std::pair<Expression*, Expression*> p) { }
310310
public:
311311
Hashed(size_t s = 0) : elements_(ExpressionMap(s)), list_(std::vector<Expression*>())
312-
{ elements_.reserve(s); list_.reserve(s); reset_duplicate_key(); }
312+
{ /* elements_.reserve(s); */ list_.reserve(s); reset_duplicate_key(); }
313313
virtual ~Hashed();
314314
size_t length() const { return list_.size(); }
315315
bool empty() const { return list_.empty(); }
@@ -1231,7 +1231,7 @@ namespace Sass {
12311231
if (hash_ == 0) {
12321232
hash_ = std::hash<std::string>()(name());
12331233
for (auto argument : arguments()->elements())
1234-
hash_combine(hash_, argument->hash());
1234+
{ hash_combine(hash_, argument->hash()); }
12351235
}
12361236
return hash_;
12371237
}
@@ -1359,10 +1359,12 @@ namespace Sass {
13591359
{
13601360
if (hash_ == 0) {
13611361
hash_ = std::hash<double>()(value_);
1362-
for (const auto numerator : numerator_units())
1362+
for (const auto numerator : numerator_units()) {
13631363
hash_combine(hash_, std::hash<std::string>()(numerator));
1364-
for (const auto denominator : denominator_units())
1364+
}
1365+
for (const auto denominator : denominator_units()) {
13651366
hash_combine(hash_, std::hash<std::string>()(denominator));
1367+
}
13661368
}
13671369
return hash_;
13681370
}
@@ -1512,7 +1514,7 @@ namespace Sass {
15121514
{
15131515
if (hash_ == 0) {
15141516
for (auto string : elements())
1515-
hash_combine(hash_, string->hash());
1517+
{ hash_combine(hash_, string->hash()); }
15161518
}
15171519
return hash_;
15181520
}

src/context.cpp

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

9999
// load plugins and register custom behaviors
100-
for(auto plug : plugin_paths) plugins.load_plugins(plug);
101-
for(auto fn : plugins.get_headers()) c_headers.push_back(fn);
102-
for(auto fn : plugins.get_importers()) c_importers.push_back(fn);
103-
for(auto fn : plugins.get_functions()) c_functions.push_back(fn);
100+
for(auto plug : plugin_paths) { plugins.load_plugins(plug); }
101+
for(auto fn : plugins.get_headers()) { c_headers.push_back(fn); }
102+
for(auto fn : plugins.get_importers()) { c_importers.push_back(fn); }
103+
for(auto fn : plugins.get_functions()) { c_functions.push_back(fn); }
104104

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

src/debugger.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ inline void debug_ast(AST_Node* node, std::string ind, Env* env)
367367
std::cerr << " (" << pstate_source_position(node) << ")";
368368
std::cerr << " " << block->tabs() << std::endl;
369369
// std::vector<std::string> files_;
370-
for (auto imp : block->urls()) debug_ast(imp, ind + "@: ", env);
370+
for (auto imp : block->urls()) { debug_ast(imp, ind + "@: ", env); }
371371
debug_ast(block->media_queries(), ind + "@@ ");
372372
} else if (dynamic_cast<Assignment*>(node)) {
373373
Assignment* block = dynamic_cast<Assignment*>(node);

src/eval.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,7 @@ namespace Sass {
10361036

10371037
if (Arguments* args = dynamic_cast<Arguments*>(ex)) {
10381038
List* ll = SASS_MEMORY_NEW(ctx.mem, List, args->pstate(), 0, SASS_COMMA);
1039-
for(auto arg : *args) {
1039+
for(auto arg : args->elements()) {
10401040
*ll << arg->value();
10411041
}
10421042
ll->is_interpolant(args->is_interpolant());
@@ -1072,7 +1072,7 @@ namespace Sass {
10721072
List* ll = SASS_MEMORY_NEW(ctx.mem, List, l->pstate(), 0, l->separator());
10731073
// this fixes an issue with bourbon sample, not really sure why
10741074
// if (l->size() && dynamic_cast<Null*>((*l)[0])) { res += ""; }
1075-
for(auto item : *l) {
1075+
for(auto item : l->elements()) {
10761076
item->is_interpolant(l->is_interpolant());
10771077
std::string rl(""); interpolation(ctx, rl, item, into_quotes, l->is_interpolant());
10781078
bool is_null = dynamic_cast<Null*>(item) != 0; // rl != ""
@@ -1324,11 +1324,11 @@ namespace Sass {
13241324
true);
13251325

13261326
if (ls && ls->is_arglist()) {
1327-
for (auto as : *ls) *arglist << as;
1327+
for (auto as : ls->elements()) { *arglist << as; }
13281328
} else if (ms) {
13291329
*aa << SASS_MEMORY_NEW(ctx.mem, Argument, splat->pstate(), ms, "", false, true);
13301330
} else if (ls) {
1331-
for (auto as : *ls) *arglist << as;
1331+
for (auto as : ls->elements()) { *arglist << as; }
13321332
} else {
13331333
*arglist << splat;
13341334
}

src/expand.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ namespace Sass {
636636
if (schema->has_parent_ref()) s = eval(schema);
637637
}
638638
if (Selector_List* sl = dynamic_cast<Selector_List*>(s)) {
639-
for (Complex_Selector* cs : *sl) {
639+
for (Complex_Selector* cs : sl->elements()) {
640640
if (cs != NULL && cs->head() != NULL) {
641641
cs->head()->media_block(media_block_stack.back());
642642
}

src/extend.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,7 +1723,7 @@ namespace Sass {
17231723

17241724
if (pHead) {
17251725
if (seen.find(*pHead) == seen.end()) {
1726-
for (Simple_Selector* pSimple : *pHead) {
1726+
for (Simple_Selector* pSimple : pHead->elements()) {
17271727
if (Wrapped_Selector* ws = dynamic_cast<Wrapped_Selector*>(pSimple)) {
17281728
if (Selector_List* sl = dynamic_cast<Selector_List*>(ws->selector())) {
17291729
for (Complex_Selector* cs : sl->elements()) {
@@ -1961,7 +1961,7 @@ namespace Sass {
19611961
pNewSelectors = remove_placeholders.remove_placeholders(pNewSelectors);
19621962

19631963
// unwrap all wrapped selectors with inner lists
1964-
for (Complex_Selector* cur : *pNewSelectors) {
1964+
for (Complex_Selector* cur : pNewSelectors->elements()) {
19651965
// process tails
19661966
while (cur) {
19671967
// process header
@@ -1970,7 +1970,7 @@ namespace Sass {
19701970
recseen.insert(*cur->head());
19711971
// create a copy since we add multiple items if stuff get unwrapped
19721972
Compound_Selector* cpy_head = SASS_MEMORY_NEW(ctx.mem, Compound_Selector, cur->pstate());
1973-
for (Simple_Selector* hs : *cur->head()) {
1973+
for (Simple_Selector* hs : cur->head()->elements()) {
19741974
if (Wrapped_Selector* ws = dynamic_cast<Wrapped_Selector*>(hs)) {
19751975
if (Selector_List* sl = dynamic_cast<Selector_List*>(ws->selector())) {
19761976
// special case for ruby ass
@@ -2092,7 +2092,8 @@ namespace Sass {
20922092
// we set `extended` flag on extended selectors
20932093
if (b->is_root()) {
20942094
// debug_subset_map(subset_map);
2095-
for(auto const &it : subset_map.values()) {
2095+
auto values = subset_map.values();
2096+
for(auto it : values) {
20962097
Complex_Selector* sel = it.first ? it.first->first() : NULL;
20972098
Compound_Selector* ext = it.second ? it.second : NULL;
20982099
if (ext && (ext->extended() || ext->is_optional())) continue;

src/functions.cpp

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <cstdlib>
1919
#include <cmath>
2020
#include <cctype>
21+
#include <climits>
2122
#include <sstream>
2223
#include <string>
2324
#include <iomanip>
@@ -30,6 +31,24 @@
3031
#include "wincrypt.h"
3132
#endif
3233

34+
#if defined __GNUC__ && ! defined __llvm__
35+
#define GCC_VERSION (__GNUC__ * 10000 \
36+
+ __GNUC_MINOR__ * 100 \
37+
+ __GNUC_PATCHLEVEL__)
38+
#if GCC_VERSION < 40500
39+
#include <tr1/random>
40+
#define IMPLEMENT_TR1
41+
#define tr1ns std::tr1
42+
#define uniform_real_distribution uniform_real
43+
#else
44+
#include <random>
45+
#define tr1ns std
46+
#endif
47+
#else
48+
#include <random>
49+
#define tr1ns std
50+
#endif
51+
3352
#define ARG(argname, argtype) get_arg<argtype>(argname, env, sig, pstate, backtrace)
3453
#define ARGR(argname, argtype, lo, hi) get_arg_r(argname, env, sig, pstate, lo, hi, backtrace)
3554
#define ARGM(argname, argtype, ctx) get_arg_m(argname, env, sig, pstate, backtrace, ctx)
@@ -229,9 +248,43 @@ namespace Sass {
229248
// random_device degrades sharply once the entropy pool
230249
// is exhausted. For practical use, random_device is
231250
// generally only used to seed a PRNG such as mt19937.
232-
static std::mt19937 rand(static_cast<unsigned int>(GetSeed()));
251+
static tr1ns::mt19937 rand(static_cast<unsigned int>(GetSeed()));
252+
253+
tr1ns::uniform_real_distribution<> std_dist(0, 1);
254+
#ifdef IMPLEMENT_TR1
255+
tr1ns::variate_generator <
256+
tr1ns::mt19937,
257+
tr1ns::uniform_real_distribution <double>
258+
> gen_std_dist(rand, std_dist);
259+
#endif
260+
261+
// Using ULONG_MAX here seems to fail on Mac OSX Clang!?
262+
tr1ns::uniform_real_distribution<> full_dist(0, 4294967296);
263+
#ifdef IMPLEMENT_TR1
264+
tr1ns::variate_generator <
265+
tr1ns::mt19937,
266+
tr1ns::uniform_real_distribution <double>
267+
> gen_full_dist(rand, full_dist);
268+
#endif
269+
270+
// helper function to retrieve a random number in interval
271+
// works around some compiler issues with older gcc versions
272+
static double random(double min, double max)
273+
{
274+
tr1ns::uniform_real_distribution<> distributor(min, max);
275+
#ifdef IMPLEMENT_TR1
276+
tr1ns::variate_generator <
277+
tr1ns::mt19937,
278+
tr1ns::uniform_real_distribution <>
279+
> gen(rand, distributor);
280+
distributor(rand);
281+
return gen();
282+
#else
283+
return distributor(rand);
284+
#endif
285+
}
233286

234-
// features
287+
// supported features lookup table
235288
static std::set<std::string> features {
236289
"global-variable-shadowing",
237290
"extend-selector-pseudoclass",
@@ -1188,13 +1241,19 @@ namespace Sass {
11881241
err << "Expected $limit to be an integer but got `" << v << "` for `random`";
11891242
error(err.str(), pstate);
11901243
}
1191-
std::uniform_real_distribution<> distributor(1, v + 1);
1192-
uint_fast32_t distributed = static_cast<uint_fast32_t>(distributor(rand));
1244+
// std::uniform_real_distribution<> distributor(1, v + 1);
1245+
// uint_fast32_t distributed = static_cast<uint_fast32_t>(distributor(rand));
1246+
uint_fast32_t distributed = random(1, v + 1);
11931247
return SASS_MEMORY_NEW(ctx.mem, Number, pstate, (double)distributed);
11941248
}
11951249
else if (b) {
1196-
std::uniform_real_distribution<> distributor(0, 1);
1197-
double distributed = static_cast<double>(distributor(rand));
1250+
// std::uniform_real_distribution<> distributor(0, 1);
1251+
// double distributed = static_cast<double>(distributor(rand));
1252+
#ifdef IMPLEMENT_TR1
1253+
double distributed = gen_std_dist();
1254+
#else
1255+
double distributed = std_dist(rand);
1256+
#endif
11981257
return SASS_MEMORY_NEW(ctx.mem, Number, pstate, distributed);
11991258
} else if (v) {
12001259
throw Exception::InvalidArgumentType(pstate, "random", "$limit", "number", v);
@@ -1932,8 +1991,11 @@ namespace Sass {
19321991
BUILT_IN(unique_id)
19331992
{
19341993
std::stringstream ss;
1935-
std::uniform_real_distribution<> distributor(0, 4294967296); // 16^8
1936-
uint_fast32_t distributed = static_cast<uint_fast32_t>(distributor(rand));
1994+
#ifdef IMPLEMENT_TR1
1995+
uint_fast32_t distributed = gen_full_dist();
1996+
#else
1997+
uint_fast32_t distributed = full_dist(rand);
1998+
#endif
19371999
ss << "u" << std::setfill('0') << std::setw(8) << std::hex << distributed;
19382000
return SASS_MEMORY_NEW(ctx.mem, String_Quoted, pstate, ss.str());
19392001
}

src/parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ namespace Sass {
303303
do {
304304
while (lex< block_comment >());
305305
if (lex< quoted_string >()) {
306-
to_import.push_back(std::pair<std::string,Function_Call*>(std::string(lexed), 0));
306+
to_import.push_back(std::pair<std::string,Function_Call*>(std::string(lexed), (Function_Call*)0));
307307
}
308308
else if (lex< uri_prefix >()) {
309309
Arguments* args = SASS_MEMORY_NEW(ctx.mem, Arguments, pstate);

src/remove_placeholders.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ namespace Sass {
3939
// Set the new placeholder selector list
4040
r->selector(remove_placeholders(sl));
4141
// Remove placeholders in wrapped selectors
42-
for (Complex_Selector* cs : *sl) {
42+
for (Complex_Selector* cs : sl->elements()) {
4343
while (cs) {
4444
if (cs->head()) {
45-
for (Simple_Selector* ss : *cs->head()) {
45+
for (Simple_Selector* ss : cs->head()->elements()) {
4646
if (Wrapped_Selector* ws = dynamic_cast<Wrapped_Selector*>(ss)) {
4747
if (Selector_List* sl = dynamic_cast<Selector_List*>(ws->selector())) {
4848
Selector_List* clean = remove_placeholders(sl);

0 commit comments

Comments
 (0)