Refactoring: Added helper function 'setindentlevel'
This commit is contained in:
parent
ede402a5ec
commit
0d0f562e90
|
@ -16,11 +16,9 @@ static const TOKEN *findfunction(const TOKEN *tok)
|
|||
int indentlevel = 0, parlevel = 0;
|
||||
for (; tok; tok = tok->next)
|
||||
{
|
||||
if (tok->str[0] == '{')
|
||||
indentlevel++;
|
||||
else if (tok->str[0] == '}')
|
||||
indentlevel--;
|
||||
else if (tok->str[0] == '(')
|
||||
setindentlevel( tok, indentlevel );
|
||||
|
||||
if (tok->str[0] == '(')
|
||||
parlevel++;
|
||||
else if (tok->str[0] == ')')
|
||||
parlevel--;
|
||||
|
@ -113,11 +111,8 @@ static void CheckBufferOverrun_DynamicData()
|
|||
int indentlevel = 0;
|
||||
for (const TOKEN *tok = ftok; tok; tok = tok->next)
|
||||
{
|
||||
if (tok->str[0] == '{')
|
||||
indentlevel++;
|
||||
else if (tok->str[0] == '}')
|
||||
if (setindentlevel(tok, indentlevel))
|
||||
{
|
||||
indentlevel--;
|
||||
if (indentlevel <= 0)
|
||||
break;
|
||||
}
|
||||
|
@ -160,37 +155,27 @@ static void CheckBufferOverrun_LocalVariable()
|
|||
int indentlevel = 0;
|
||||
for (const TOKEN *tok = tokens; tok; tok = tok->next)
|
||||
{
|
||||
if (tok->str[0]=='{')
|
||||
indentlevel++;
|
||||
setindentlevel( tok, indentlevel );
|
||||
if (indentlevel < 0)
|
||||
break;
|
||||
|
||||
else if (tok->str[0]=='}')
|
||||
indentlevel--;
|
||||
|
||||
else if (indentlevel > 0)
|
||||
{
|
||||
// Declaring array..
|
||||
if (match(tok, "type var [ num ] ;"))
|
||||
{
|
||||
if ( ! match(tok, "type var [ num ] ;") )
|
||||
continue;
|
||||
|
||||
const char *varname = getstr(tok,1);
|
||||
unsigned int size = strtoul(getstr(tok,3), NULL, 10);
|
||||
int total_size = size * SizeOfType(tok->str);
|
||||
if (total_size == 0)
|
||||
continue;
|
||||
int _indentlevel = indentlevel;
|
||||
|
||||
int _indentlevel = 0;
|
||||
for (const TOKEN *tok2 = gettok(tok,5); tok2; tok2 = tok2->next)
|
||||
{
|
||||
if (tok2->str[0]=='{')
|
||||
{
|
||||
_indentlevel++;
|
||||
}
|
||||
else if (tok2->str[0]=='}')
|
||||
{
|
||||
_indentlevel--;
|
||||
if (_indentlevel < indentlevel)
|
||||
setindentlevel(tok2, _indentlevel);
|
||||
if ( _indentlevel < 0 )
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
// Array index..
|
||||
if (strcmp(tok2->str,varname)==0 &&
|
||||
strcmp(getstr(tok2,1),"[")==0 &&
|
||||
|
@ -314,9 +299,6 @@ static void CheckBufferOverrun_LocalVariable()
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
|
|
|
@ -58,3 +58,14 @@ bool IsStandardType(const char str[])
|
|||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
bool setindentlevel( const TOKEN *tok, int &indentlevel )
|
||||
{
|
||||
if ( tok->str[0] == '{' )
|
||||
indentlevel++;
|
||||
|
||||
else if ( tok->str[0] == '}' )
|
||||
indentlevel--;
|
||||
|
||||
return bool(tok->str[0] == '}');
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
|
|
@ -6,6 +6,10 @@
|
|||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
// Report errors..
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
struct TOKEN;
|
||||
|
||||
std::string FileLine(const TOKEN *tok);
|
||||
|
@ -16,10 +20,22 @@ void ReportErr(const std::string &errmsg);
|
|||
extern std::ostringstream errout;
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
// Classify tokens..
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
bool IsName(const char str[]);
|
||||
bool IsNumber(const char str[]);
|
||||
|
||||
bool IsStandardType(const char str[]);
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
// Iterating through tokens..
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
bool setindentlevel( const TOKEN *tok, int &indentlevel );
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue