Kaydet (Commit) cb2c7428 authored tarafından Noel Grandin's avatar Noel Grandin

use std::vector in AstStack

instead of a hand-coded equivalent. Note that I can't use
std::unique_ptr, because the parsing code appears to just randomly leak
AstScope

Change-Id: Idc56dfe1f084db55c9d5a7558ac44ddab53b98e3
Reviewed-on: https://gerrit.libreoffice.org/59771
Tested-by: Jenkins
Reviewed-by: 's avatarNoel Grandin <noel.grandin@collabora.co.uk>
üst e040d8de
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#define INCLUDED_IDLC_INC_ASTSTACK_HXX #define INCLUDED_IDLC_INC_ASTSTACK_HXX
#include <sal/types.h> #include <sal/types.h>
#include <vector>
class AstScope; class AstScope;
...@@ -29,7 +30,7 @@ public: ...@@ -29,7 +30,7 @@ public:
AstStack(); AstStack();
~AstStack(); ~AstStack();
sal_uInt32 depth() const { return m_top;} sal_uInt32 depth() const { return m_stack.size();}
AstScope* top(); AstScope* top();
AstScope* bottom(); AstScope* bottom();
AstScope* nextToTop(); AstScope* nextToTop();
...@@ -39,9 +40,7 @@ public: ...@@ -39,9 +40,7 @@ public:
void clear(); void clear();
private: private:
AstScope** m_stack; std::vector<AstScope*> m_stack;
sal_uInt32 m_size;
sal_uInt32 m_top;
}; };
#endif // INCLUDED_IDLC_INC_ASTSTACK_HXX #endif // INCLUDED_IDLC_INC_ASTSTACK_HXX
......
...@@ -21,102 +21,65 @@ ...@@ -21,102 +21,65 @@
#include <aststack.hxx> #include <aststack.hxx>
#include <astscope.hxx> #include <astscope.hxx>
#define STACKSIZE_INCREMENT 64
AstStack::AstStack() AstStack::AstStack()
: m_stack(static_cast<AstScope**>(rtl_allocateZeroMemory(sizeof(AstScope*) * STACKSIZE_INCREMENT)))
, m_size(STACKSIZE_INCREMENT)
, m_top(0)
{ {
} }
AstStack::~AstStack() AstStack::~AstStack()
{ {
for(sal_uInt32 i=0; i < m_top; i++) for (AstScope* p : m_stack)
{ delete p;
if (m_stack[i])
delete m_stack[i];
}
std::free(m_stack);
} }
AstScope* AstStack::top() AstScope* AstStack::top()
{ {
if (m_top < 1) if (m_stack.empty())
return nullptr; return nullptr;
return m_stack[m_top - 1]; return m_stack.back();
} }
AstScope* AstStack::bottom() AstScope* AstStack::bottom()
{ {
if (m_top == 0) if (m_stack.empty())
return nullptr; return nullptr;
return m_stack[0]; return m_stack.front();
} }
AstScope* AstStack::nextToTop() AstScope* AstStack::nextToTop()
{ {
AstScope *tmp, *retval; if (m_stack.size() < 2)
if (depth() < 2)
return nullptr; return nullptr;
tmp = top(); // Save top return m_stack[m_stack.size() - 2];
pop(); // Pop it
retval = top(); // Get next one down
(void) push(tmp); // Push top back
return retval; // Return next one down
} }
AstScope* AstStack::topNonNull() AstScope* AstStack::topNonNull()
{ {
for (sal_uInt32 i = m_top; i > 0; i--) for (sal_uInt32 i = m_stack.size(); i > 0; i--)
{ {
if ( m_stack[i - 1] ) if ( m_stack[i - 1] )
return m_stack[i - 1]; return m_stack[i - 1];
} }
return nullptr; return nullptr;
} }
AstStack* AstStack::push(AstScope* pScope) AstStack* AstStack::push(AstScope* pScope)
{ {
AstScope **tmp; m_stack.push_back(pScope);
// AstDeclaration *pDecl = ScopeAsDecl(pScope);
sal_uInt32 newSize;
sal_uInt32 i;
// Make sure there's space for one more
if (m_size == m_top)
{
newSize = m_size;
newSize += STACKSIZE_INCREMENT;
tmp = static_cast<AstScope**>(rtl_allocateZeroMemory(sizeof(AstScope*) * newSize));
for(i=0; i < m_size; i++)
tmp[i] = m_stack[i];
std::free(m_stack);
m_stack = tmp;
}
// Insert new scope
m_stack[m_top++] = pScope;
return this; return this;
} }
void AstStack::pop() void AstStack::pop()
{ {
if (m_top < 1) if (m_stack.empty())
return; return;
--m_top; m_stack.pop_back();
} }
void AstStack::clear() void AstStack::clear()
{ {
m_top = 0; m_stack.clear();
} }
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment