Skip to content

Commit

Permalink
Refactoring: replace QCString with std::string in constexp
Browse files Browse the repository at this point in the history
  • Loading branch information
doxygen committed Feb 6, 2021
1 parent 0c612fe commit 38fdb5c
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 92 deletions.
24 changes: 11 additions & 13 deletions src/bufstr.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/******************************************************************************
*
*
*
*
*
* Copyright (C) 1997-2015 by Dimitri van Heesch.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation under the terms of the GNU General Public License is hereby
* granted. No representations are made about the suitability of this software
* documentation under the terms of the GNU General Public License is hereby
* granted. No representations are made about the suitability of this software
* for any purpose. It is provided "as is" without express or implied warranty.
* See the GNU General Public License for more details.
*
Expand All @@ -18,20 +18,18 @@
#ifndef _BUFSTR_H
#define _BUFSTR_H

#include <qglobal.h>
#include <qcstring.h>
#include <stdlib.h>
#include <cstdlib>

/*! @brief Buffer used to store strings
*
*
* This buffer is used append characters and strings. It will automatically
* resize itself, yet provide efficient random access to the content.
*/
class BufStr
class BufStr
{
public:
BufStr(uint size)
: m_size(size), m_writeOffset(0), m_spareRoom(10240), m_buf(0)
BufStr(uint size)
: m_size(size), m_writeOffset(0), m_spareRoom(10240), m_buf(0)
{
m_buf = (char *)calloc(size,1);
}
Expand Down Expand Up @@ -95,8 +93,8 @@ class BufStr
return m_buf;
}
uint curPos() const
{
return m_writeOffset;
{
return m_writeOffset;
}
void dropFromStart(uint bytes)
{
Expand All @@ -108,7 +106,7 @@ class BufStr
private:
void makeRoomFor(uint size)
{
if (m_writeOffset+size>=m_size)
if (m_writeOffset+size>=m_size)
{
resize(m_size+size+m_spareRoom);
}
Expand Down
13 changes: 5 additions & 8 deletions src/constexp.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
/******************************************************************************
*
*
*
*
* Copyright (C) 1997-2015 by Dimitri van Heesch.
* Copyright (C) 1997-2021 by Dimitri van Heesch.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation under the terms of the GNU General Public License is hereby
* granted. No representations are made about the suitability of this software
* documentation under the terms of the GNU General Public License is hereby
* granted. No representations are made about the suitability of this software
* for any purpose. It is provided "as is" without express or implied warranty.
* See the GNU General Public License for more details.
*
Expand All @@ -19,14 +16,14 @@
#ifndef _CONSTEXP_H
#define _CONSTEXP_H

#include <qcstring.h>
#include <string>

class ConstExpressionParser
{
public:
ConstExpressionParser();
~ConstExpressionParser();
bool parse(const char *fileName,int line,const QCString &expression);
bool parse(const char *fileName,int line,const std::string &expression);
private:
struct Private;
Private *p;
Expand Down
6 changes: 3 additions & 3 deletions src/constexp.l
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ ConstExpressionParser::~ConstExpressionParser()
delete p;
}
bool ConstExpressionParser::parse(const char *fileName,int lineNr,const QCString &s)
bool ConstExpressionParser::parse(const char *fileName,int lineNr,const std::string &s)
{
struct yyguts_t *yyg = (struct yyguts_t*)p->yyscanner;
Expand All @@ -146,13 +146,13 @@ bool ConstExpressionParser::parse(const char *fileName,int lineNr,const QCString
yyextra->inputPosition = 0;
constexpYYrestart( yyin, p->yyscanner );
printlex(yy_flex_debug, TRUE, __FILE__, fileName);
printlex(yy_flex_debug, true, __FILE__, fileName);
//printf("Expression: '%s'\n",s.data());
constexpYYparse(p->yyscanner);
//printf("Result: %ld\n",(long)g_resultValue);
printlex(yy_flex_debug, FALSE, __FILE__, fileName);
printlex(yy_flex_debug, false, __FILE__, fileName);
bool result = (long)yyextra->resultValue!=0;
return result;
Expand Down
43 changes: 20 additions & 23 deletions src/constexp.y
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
/******************************************************************************
*
*
*
*
* Copyright (C) 1997-2015 by Dimitri van Heesch.
* Copyright (C) 1997-2021 by Dimitri van Heesch.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation under the terms of the GNU General Public License is hereby
* granted. No representations are made about the suitability of this software
* documentation under the terms of the GNU General Public License is hereby
* granted. No representations are made about the suitability of this software
* for any purpose. It is provided "as is" without express or implied warranty.
* See the GNU General Public License for more details.
*
Expand All @@ -28,8 +25,8 @@
int constexpYYerror(yyscan_t yyscanner, const char *s)
{
struct constexpYY_state* yyextra = constexpYYget_extra(yyscanner);
warn(yyextra->constExpFileName, yyextra->constExpLineNr,
"preprocessing issue while doing constant expression evaluation: %s: input='%s'",s,yyextra->inputString);
warn(yyextra->constExpFileName.c_str(), yyextra->constExpLineNr,
"preprocessing issue while doing constant expression evaluation: %s: input='%s'",s,yyextra->inputString.c_str());
return 0;
}

Expand Down Expand Up @@ -81,8 +78,8 @@ start: constant_expression

constant_expression: logical_or_expression
{ $$ = $1; }
| logical_or_expression
TOK_QUESTIONMARK logical_or_expression
| logical_or_expression
TOK_QUESTIONMARK logical_or_expression
TOK_COLON logical_or_expression
{
bool c = ($1.isInt() ? ((long)$1 != 0) : ((double)$1 != 0.0));
Expand All @@ -108,9 +105,9 @@ logical_and_expression: inclusive_or_expression

inclusive_or_expression: exclusive_or_expression
{ $$ = $1; }
| inclusive_or_expression TOK_BITWISEOR
| inclusive_or_expression TOK_BITWISEOR
exclusive_or_expression
{
{
$$ = CPPValue( (long)$1 | (long)$3 );
}
;
Expand All @@ -126,15 +123,15 @@ exclusive_or_expression: and_expression
and_expression: equality_expression
{ $$ = $1; }
| and_expression TOK_AMPERSAND equality_expression
{
{
$$ = CPPValue( (long)$1 & (long)$3 );
}
;

equality_expression: relational_expression
{ $$ = $1; }
| equality_expression TOK_EQUAL relational_expression
{
{
$$ = CPPValue( (long)((double)$1 == (double)$3) );
}
| equality_expression TOK_NOTEQUAL relational_expression
Expand All @@ -146,7 +143,7 @@ equality_expression: relational_expression
relational_expression: shift_expression
{ $$ = $1; }
| relational_expression TOK_LESSTHAN shift_expression
{
{
$$ = CPPValue( (long)((double)$1 < (double)$3) );
}
| relational_expression TOK_GREATERTHAN shift_expression
Expand All @@ -169,7 +166,7 @@ shift_expression: additive_expression
{ $$ = $1; }
| shift_expression TOK_SHIFTLEFT additive_expression
{
$$ = CPPValue( (long)$1 << (long)$3 );
$$ = CPPValue( (long)$1 << (long)$3 );
}
| shift_expression TOK_SHIFTRIGHT additive_expression
{
Expand All @@ -185,7 +182,7 @@ additive_expression: multiplicative_expression
{
$$ = CPPValue( (double)$1 + (double)$3 );
}
else
else
{
$$ = CPPValue( (long)$1 + (long)$3 );
}
Expand All @@ -196,7 +193,7 @@ additive_expression: multiplicative_expression
{
$$ = CPPValue( (double)$1 - (double)$3 );
}
else
else
{
$$ = CPPValue( (long)$1 - (long)$3 );
}
Expand All @@ -206,7 +203,7 @@ additive_expression: multiplicative_expression
multiplicative_expression: unary_expression
{ $$ = $1; }
| multiplicative_expression TOK_STAR unary_expression
{
{
if (!$1.isInt() || !$3.isInt())
{
$$ = CPPValue( (double)$1 * (double)$3 );
Expand All @@ -217,7 +214,7 @@ multiplicative_expression: unary_expression
}
}
| multiplicative_expression TOK_DIVIDE unary_expression
{
{
if (!$1.isInt() || !$3.isInt())
{
$$ = CPPValue( (double)$1 / (double)$3 );
Expand All @@ -230,7 +227,7 @@ multiplicative_expression: unary_expression
}
}
| multiplicative_expression TOK_MOD unary_expression
{
{
long value = $3;
if (value==0) value=1;
$$ = CPPValue( (long)$1 % value );
Expand All @@ -242,8 +239,8 @@ unary_expression: primary_expression
| TOK_PLUS unary_expression
{ $$ = $1; }
| TOK_MINUS unary_expression
{
if ($2.isInt())
{
if ($2.isInt())
$$ = CPPValue(-(long)$2);
else
$$ = CPPValue(-(double)$2);
Expand Down
18 changes: 9 additions & 9 deletions src/constexp_p.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/******************************************************************************
*
* Copyright (C) 1997-2019 by Dimitri van Heesch.
* Copyright (C) 1997-2021 by Dimitri van Heesch.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation under the terms of the GNU General Public License is hereby
* granted. No representations are made about the suitability of this software
* documentation under the terms of the GNU General Public License is hereby
* granted. No representations are made about the suitability of this software
* for any purpose. It is provided "as is" without express or implied warranty.
* See the GNU General Public License for more details.
*
Expand All @@ -16,7 +16,7 @@
#ifndef _CONSTEXP_P_H
#define _CONSTEXP_P_H

#include <qcstring.h>
#include <string>

//! @file
//! @brief Private interface between Parser (constexp.y) and Lexer (constexp.l)
Expand All @@ -27,12 +27,12 @@
typedef void* yyscan_t;
struct constexpYY_state
{
QCString strToken;
CPPValue resultValue;
int constExpLineNr;
QCString constExpFileName;
std::string strToken;
CPPValue resultValue;
int constExpLineNr;
std::string constExpFileName;

const char *inputString;
std::string inputString;
int inputPosition;
};
constexpYY_state* constexpYYget_extra(yyscan_t yyscanner );
Expand Down
33 changes: 15 additions & 18 deletions src/cppvalue.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
/******************************************************************************
*
*
*
*
* Copyright (C) 1997-2015 by Dimitri van Heesch.
* Copyright (C) 1997-2021 by Dimitri van Heesch.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation under the terms of the GNU General Public License is hereby
* granted. No representations are made about the suitability of this software
* documentation under the terms of the GNU General Public License is hereby
* granted. No representations are made about the suitability of this software
* for any purpose. It is provided "as is" without express or implied warranty.
* See the GNU General Public License for more details.
*
Expand All @@ -21,30 +18,30 @@
#include "cppvalue.h"
#include "constexp.h"

CPPValue parseOctal(const QCString& token)
CPPValue parseOctal(const std::string& token)
{
long val = 0;
for (const char *p = token.data(); *p != 0; p++)
for (const char *p = token.c_str(); *p != 0; p++)
{
if (*p >= '0' && *p <= '7') val = val * 8 + *p - '0';
}
return CPPValue(val);
}

CPPValue parseDecimal(const QCString& token)
CPPValue parseDecimal(const std::string& token)
{
long val = 0;
for (const char *p = token.data(); *p != 0; p++)
for (const char *p = token.c_str(); *p != 0; p++)
{
if (*p >= '0' && *p <= '9') val = val * 10 + *p - '0';
}
return CPPValue(val);
}

CPPValue parseHexadecimal(const QCString& token)
CPPValue parseHexadecimal(const std::string& token)
{
long val = 0;
for (const char *p = token.data(); *p != 0; p++)
for (const char *p = token.c_str(); *p != 0; p++)
{
if (*p >= '0' && *p <= '9') val = val * 16 + *p - '0';
else if (*p >= 'a' && *p <= 'f') val = val * 16 + *p - 'a' + 10;
Expand All @@ -54,7 +51,7 @@ CPPValue parseHexadecimal(const QCString& token)
return CPPValue(val);
}

CPPValue parseCharacter(const QCString& token) // does not work for '\n' and the alike
CPPValue parseCharacter(const std::string& token) // does not work for '\n' and the alike
{
if (token[1]=='\\')
{
Expand All @@ -80,16 +77,16 @@ CPPValue parseCharacter(const QCString& token) // does not work for '\n' and the
case '6': // fall through
case '7': // fall through
return parseOctal(token);
case 'x':
case 'x':
case 'X': return parseHexadecimal(token);
default: printf("Invalid escape sequence %s found!\n",token.data());
return CPPValue(0L);
default: printf("Invalid escape sequence %s found!\n",token.data());
return CPPValue(0L);
}
}
return CPPValue((long)token[1]);
}

CPPValue parseFloat(const QCString& token)
CPPValue parseFloat(const std::string& token)
{
return CPPValue(atof(token));
return CPPValue(std::stod(token));
}
Loading

0 comments on commit 38fdb5c

Please sign in to comment.