Skip to content

Commit 5e58c35

Browse files
glebmxzyfer
authored andcommitted
AST: Fix clang warnings, add const, remove virtual
Mechanical changes: * Fixes all clang warnings in ast.hpp and removes warnings suppression. * Adds `const` to a bunch of methods. * Removes `virtual` where unnecessary. * Adds `override` where necessary (as part of fixing clang warnings). Non-mechanical changes: * Removes unused `Thunk` class. * Makes `hash()` const by marking `hash_` as `mutable` (implementation detail). * Makes `first()` and `last()` const, and adds `mutable_first()` and `mutable_first()` for the rare cases where mutability is necessary. * `Complex_Selector` internally now uses `tail_` and `head_` directly instead of going through accessors (these accessors create a `SharedImpl` on every call).
1 parent fa93c5a commit 5e58c35

14 files changed

+337
-342
lines changed

src/ast.cpp

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ namespace Sass {
807807
return ns() < rhs.ns();
808808
}
809809

810-
bool Wrapped_Selector::is_superselector_of(Wrapped_Selector_Obj sub)
810+
bool Wrapped_Selector::is_superselector_of(Wrapped_Selector_Obj sub) const
811811
{
812812
if (this->name() != sub->name()) return false;
813813
if (this->name() == ":current") return false;
@@ -820,23 +820,23 @@ namespace Sass {
820820
return false;
821821
}
822822

823-
bool Compound_Selector::is_superselector_of(Selector_List_Obj rhs, std::string wrapped)
823+
bool Compound_Selector::is_superselector_of(Selector_List_Obj rhs, std::string wrapped) const
824824
{
825825
for (Complex_Selector_Obj item : rhs->elements()) {
826826
if (is_superselector_of(item, wrapped)) return true;
827827
}
828828
return false;
829829
}
830830

831-
bool Compound_Selector::is_superselector_of(Complex_Selector_Obj rhs, std::string wrapped)
831+
bool Compound_Selector::is_superselector_of(Complex_Selector_Obj rhs, std::string wrapped) const
832832
{
833833
if (rhs->head()) return is_superselector_of(rhs->head(), wrapped);
834834
return false;
835835
}
836836

837-
bool Compound_Selector::is_superselector_of(Compound_Selector_Obj rhs, std::string wrapping)
837+
bool Compound_Selector::is_superselector_of(Compound_Selector_Obj rhs, std::string wrapping) const
838838
{
839-
Compound_Selector_Ptr lhs = this;
839+
Compound_Selector_Ptr_Const lhs = this;
840840
Simple_Selector_Ptr lbase = lhs->base();
841841
Simple_Selector_Ptr rbase = rhs->base();
842842

@@ -963,8 +963,8 @@ namespace Sass {
963963
{
964964

965965
// get last tails (on the right side)
966-
Complex_Selector_Obj l_last = this->last();
967-
Complex_Selector_Obj r_last = other->last();
966+
Complex_Selector_Obj l_last = this->mutable_last();
967+
Complex_Selector_Obj r_last = other->mutable_last();
968968

969969
// check valid pointers (assertion)
970970
SASS_ASSERT(l_last, "lhs is null");
@@ -1059,21 +1059,21 @@ namespace Sass {
10591059
// there is no break?!
10601060
}
10611061

1062-
bool Complex_Selector::is_superselector_of(Compound_Selector_Obj rhs, std::string wrapping)
1062+
bool Complex_Selector::is_superselector_of(Compound_Selector_Obj rhs, std::string wrapping) const
10631063
{
10641064
return last()->head() && last()->head()->is_superselector_of(rhs, wrapping);
10651065
}
10661066

1067-
bool Complex_Selector::is_superselector_of(Complex_Selector_Obj rhs, std::string wrapping)
1067+
bool Complex_Selector::is_superselector_of(Complex_Selector_Obj rhs, std::string wrapping) const
10681068
{
1069-
Complex_Selector_Ptr lhs = this;
1069+
Complex_Selector_Ptr_Const lhs = this;
10701070
// check for selectors with leading or trailing combinators
10711071
if (!lhs->head() || !rhs->head())
10721072
{ return false; }
1073-
Complex_Selector_Obj l_innermost = lhs->innermost();
1073+
Complex_Selector_Ptr_Const l_innermost = lhs->last();
10741074
if (l_innermost->combinator() != Complex_Selector::ANCESTOR_OF)
10751075
{ return false; }
1076-
Complex_Selector_Obj r_innermost = rhs->innermost();
1076+
Complex_Selector_Ptr_Const r_innermost = rhs->last();
10771077
if (r_innermost->combinator() != Complex_Selector::ANCESTOR_OF)
10781078
{ return false; }
10791079
// more complex (i.e., longer) selectors are always more specific
@@ -1212,19 +1212,20 @@ namespace Sass {
12121212
// std::cerr << "has no or empty head\n";
12131213
}
12141214

1215-
if (last()) {
1216-
if (last()->combinator() != ANCESTOR_OF && c != ANCESTOR_OF) {
1215+
Complex_Selector_Ptr last = mutable_last();
1216+
if (last) {
1217+
if (last->combinator() != ANCESTOR_OF && c != ANCESTOR_OF) {
12171218
Complex_Selector_Ptr inter = SASS_MEMORY_NEW(Complex_Selector, pstate());
12181219
inter->reference(r);
12191220
inter->combinator(c);
12201221
inter->tail(t);
1221-
last()->tail(inter);
1222+
last->tail(inter);
12221223
} else {
1223-
if (last()->combinator() == ANCESTOR_OF) {
1224-
last()->combinator(c);
1225-
last()->reference(r);
1224+
if (last->combinator() == ANCESTOR_OF) {
1225+
last->combinator(c);
1226+
last->reference(r);
12261227
}
1227-
last()->tail(t);
1228+
last->tail(t);
12281229
}
12291230
}
12301231

@@ -1413,16 +1414,16 @@ namespace Sass {
14131414
}
14141415

14151416
// return the last tail that is defined
1416-
Complex_Selector_Obj Complex_Selector::first()
1417+
Complex_Selector_Ptr_Const Complex_Selector::first() const
14171418
{
14181419
// declare variables used in loop
1419-
Complex_Selector_Obj cur = this;
1420-
Compound_Selector_Obj head;
1420+
Complex_Selector_Ptr_Const cur = this;
1421+
Compound_Selector_Ptr_Const head;
14211422
// processing loop
14221423
while (cur)
14231424
{
14241425
// get the head
1425-
head = cur->head_;
1426+
head = cur->head_.ptr();
14261427
// abort (and return) if it is not a parent selector
14271428
if (!head || head->length() != 1 || !Cast<Parent_Selector>((*head)[0])) {
14281429
break;
@@ -1434,35 +1435,45 @@ namespace Sass {
14341435
return cur;
14351436
}
14361437

1438+
Complex_Selector_Ptr Complex_Selector::mutable_first()
1439+
{
1440+
return const_cast<Complex_Selector_Ptr>(first());
1441+
}
1442+
14371443
// return the last tail that is defined
1438-
Complex_Selector_Obj Complex_Selector::last()
1444+
Complex_Selector_Ptr_Const Complex_Selector::last() const
14391445
{
1440-
Complex_Selector_Ptr cur = this;
1441-
Complex_Selector_Ptr nxt = cur;
1446+
Complex_Selector_Ptr_Const cur = this;
1447+
Complex_Selector_Ptr_Const nxt = cur;
14421448
// loop until last
14431449
while (nxt) {
14441450
cur = nxt;
1445-
nxt = cur->tail();
1451+
nxt = cur->tail_.ptr();
14461452
}
14471453
return cur;
14481454
}
14491455

1456+
Complex_Selector_Ptr Complex_Selector::mutable_last()
1457+
{
1458+
return const_cast<Complex_Selector_Ptr>(last());
1459+
}
1460+
14501461
Complex_Selector::Combinator Complex_Selector::clear_innermost()
14511462
{
14521463
Combinator c;
14531464
if (!tail() || tail()->tail() == 0)
14541465
{ c = combinator(); combinator(ANCESTOR_OF); tail(0); }
14551466
else
1456-
{ c = tail()->clear_innermost(); }
1467+
{ c = tail_->clear_innermost(); }
14571468
return c;
14581469
}
14591470

14601471
void Complex_Selector::set_innermost(Complex_Selector_Obj val, Combinator c)
14611472
{
1462-
if (!tail())
1463-
{ tail(val); combinator(c); }
1473+
if (!tail_)
1474+
{ tail_ = val; combinator(c); }
14641475
else
1465-
{ tail()->set_innermost(val, c); }
1476+
{ tail_->set_innermost(val, c); }
14661477
}
14671478

14681479
void Complex_Selector::cloneChildren()
@@ -1515,7 +1526,7 @@ namespace Sass {
15151526
}
15161527
}
15171528

1518-
size_t Wrapped_Selector::hash()
1529+
size_t Wrapped_Selector::hash() const
15191530
{
15201531
if (hash_ == 0) {
15211532
hash_combine(hash_, Simple_Selector::hash());
@@ -1579,7 +1590,7 @@ namespace Sass {
15791590

15801591
// it's a superselector if every selector of the right side
15811592
// list is a superselector of the given left side selector
1582-
bool Complex_Selector::is_superselector_of(Selector_List_Obj sub, std::string wrapping)
1593+
bool Complex_Selector::is_superselector_of(Selector_List_Obj sub, std::string wrapping) const
15831594
{
15841595
// Check every rhs selector against left hand list
15851596
for(size_t i = 0, L = sub->length(); i < L; ++i) {
@@ -1590,7 +1601,7 @@ namespace Sass {
15901601

15911602
// it's a superselector if every selector of the right side
15921603
// list is a superselector of the given left side selector
1593-
bool Selector_List::is_superselector_of(Selector_List_Obj sub, std::string wrapping)
1604+
bool Selector_List::is_superselector_of(Selector_List_Obj sub, std::string wrapping) const
15941605
{
15951606
// Check every rhs selector against left hand list
15961607
for(size_t i = 0, L = sub->length(); i < L; ++i) {
@@ -1601,7 +1612,7 @@ namespace Sass {
16011612

16021613
// it's a superselector if every selector on the right side
16031614
// is a superselector of any one of the left side selectors
1604-
bool Selector_List::is_superselector_of(Compound_Selector_Obj sub, std::string wrapping)
1615+
bool Selector_List::is_superselector_of(Compound_Selector_Obj sub, std::string wrapping) const
16051616
{
16061617
// Check every lhs selector against right hand
16071618
for(size_t i = 0, L = length(); i < L; ++i) {
@@ -1612,7 +1623,7 @@ namespace Sass {
16121623

16131624
// it's a superselector if every selector on the right side
16141625
// is a superselector of any one of the left side selectors
1615-
bool Selector_List::is_superselector_of(Complex_Selector_Obj sub, std::string wrapping)
1626+
bool Selector_List::is_superselector_of(Complex_Selector_Obj sub, std::string wrapping) const
16161627
{
16171628
// Check every lhs selector against right hand
16181629
for(size_t i = 0, L = length(); i < L; ++i) {
@@ -1679,7 +1690,7 @@ namespace Sass {
16791690
}
16801691
};
16811692

1682-
void Compound_Selector::append(Simple_Selector_Ptr element)
1693+
void Compound_Selector::append(Simple_Selector_Obj element)
16831694
{
16841695
Vectorized<Simple_Selector_Obj>::append(element);
16851696
pstate_.offset += element->pstate().offset;
@@ -2112,7 +2123,7 @@ namespace Sass {
21122123
catch (...) { throw; }
21132124
}
21142125

2115-
size_t Function_Call::hash()
2126+
size_t Function_Call::hash() const
21162127
{
21172128
if (hash_ == 0) {
21182129
hash_ = std::hash<std::string>()(name());

0 commit comments

Comments
 (0)