optimization: make parser methods inline

This commit is contained in:
Roy Qu 2024-04-09 22:12:03 +08:00
parent 262ca6536c
commit a9295caff2
6 changed files with 82 additions and 313 deletions

View File

@ -138,11 +138,6 @@ void CppPreprocessor::getDefineParts(const QString &input, QString &name, QStrin
args.squeeze();
}
void CppPreprocessor::addHardDefineByLine(const QString &line)
{
addDefineByLine(line,true);
}
void CppPreprocessor::addDefineByLine(const QString &line, bool hardCoded)
{
// Remove define
@ -157,22 +152,13 @@ void CppPreprocessor::addDefineByLine(const QString &line, bool hardCoded)
addDefineByParts(name, args, value, hardCoded);
}
void CppPreprocessor::setScanOptions(bool parseSystem, bool parseLocal)
{
mParseSystem = parseSystem;
mParseLocal=parseLocal;
}
void CppPreprocessor::preprocess(const QString &fileName)
{
clearTempResults();
mFileName = fileName;
//mDefines = mHardDefines;
openInclude(fileName);
// StringsToFile(mBuffer,"f:\\buffer.txt");
preprocessBuffer();
// StringsToFile(mBuffer,"f:\\buffer.txt");
// StringsToFile(mResult,"f:\\log.txt");
}
void CppPreprocessor::invalidDefinesInFile(const QString &fileName)
@ -265,18 +251,6 @@ void CppPreprocessor::addProjectIncludePath(const QString &fileName)
}
}
void CppPreprocessor::clearIncludePaths()
{
mIncludePaths.clear();
mIncludePathList.clear();
}
void CppPreprocessor::clearProjectIncludePaths()
{
mProjectIncludePaths.clear();
mProjectIncludePathList.clear();
}
void CppPreprocessor::removeScannedFile(const QString &filename)
{
invalidDefinesInFile(filename);
@ -1204,71 +1178,6 @@ void CppPreprocessor::skipToPreprocessor()
}
}
bool CppPreprocessor::isWordChar(const QChar &ch)
{
if (ch=='_'
// || (ch>='a' && ch<='z') || (ch>='A' && ch<='Z')
|| ch.isLetter()
|| (ch>='0' && ch<='9')) {
return true;
}
return false;
}
bool CppPreprocessor::isIdentChar(const QChar &ch)
{
if (ch=='_' || ch == '*' || ch == '&' || ch == '~' ||
ch.isLetter()
//(ch>='a' && ch<='z') || (ch>='A' && ch<='Z')
|| (ch>='0' && ch<='9')) {
return true;
}
return false;
}
bool CppPreprocessor::isLineChar(const QChar &ch)
{
return ch=='\r' || ch == '\n';
}
bool CppPreprocessor::isSpaceChar(const QChar &ch)
{
return ch == ' ' || ch == '\t';
}
//bool CppPreprocessor::isOperatorChar(const QChar &ch)
//{
// switch(ch.unicode()) {
// case '+':
// case '-':
// case '*':
// case '/':
// case '!':
// case '=':
// case '<':
// case '>':
// case '&':
// case '|':
// case '^':
// return true;
// default:
// return false;
// }
//}
bool CppPreprocessor::isMacroIdentChar(const QChar &ch)
{
//return (ch>='A' && ch<='Z') || (ch>='a' && ch<='z')
return ch.isLetter()
|| ch == '_';
}
bool CppPreprocessor::isDigit(const QChar &ch)
{
return (ch>='0' && ch<='9');
}
bool CppPreprocessor::isNumberChar(const QChar &ch)
{
if (ch>='0' && ch<='9')
@ -1290,11 +1199,6 @@ bool CppPreprocessor::isNumberChar(const QChar &ch)
}
}
QString CppPreprocessor::lineBreak()
{
return "\n";
}
bool CppPreprocessor::evaluateIf(const QString &line)
{
QString newLine = expandDefines(line); // replace FOO by numerical value of FOO
@ -1961,22 +1865,4 @@ int CppPreprocessor::evaluateExpression(QString line)
return result;
}
void CppPreprocessor::setOnGetFileStream(const GetFileStreamCallBack &newOnGetFileStream)
{
mOnGetFileStream = newOnGetFileStream;
}
const QList<QString> &CppPreprocessor::projectIncludePathList() const
{
return mProjectIncludePathList;
}
const QList<QString> &CppPreprocessor::includePathList() const
{
return mIncludePathList;
}
const DefineMap &CppPreprocessor::hardDefines() const
{
return mHardDefines;
}

View File

@ -69,16 +69,25 @@ public:
void clearTempResults();
void getDefineParts(const QString& input, QString &name, QString &args, QString &value);
void addHardDefineByLine(const QString& line);
void setScanOptions(bool parseSystem, bool parseLocal);
void addHardDefineByLine(const QString& line) { addDefineByLine(line,true); }
void setScanOptions(bool parseSystem, bool parseLocal) {
mParseSystem = parseSystem;
mParseLocal=parseLocal;
}
void preprocess(const QString& fileName);
void dumpDefinesTo(const QString& fileName) const;
void dumpIncludesListTo(const QString& fileName) const;
void addIncludePath(const QString& fileName);
void addProjectIncludePath(const QString& fileName);
void clearIncludePaths();
void clearProjectIncludePaths();
void clearIncludePaths() {
mIncludePaths.clear();
mIncludePathList.clear();
}
void clearProjectIncludePaths() {
mProjectIncludePaths.clear();
mProjectIncludePathList.clear();
}
void removeScannedFile(const QString& filename);
PDefine getDefine(const QString& name) const{
@ -116,12 +125,12 @@ public:
return mProjectIncludePaths;
}
const DefineMap &hardDefines() const;
const DefineMap &hardDefines() const { return mHardDefines; }
const QList<QString> &includePathList() const;
const QList<QString> &includePathList() const { return mIncludePathList; }
const QList<QString> &projectIncludePathList() const;
void setOnGetFileStream(const GetFileStreamCallBack &newOnGetFileStream);
const QList<QString> &projectIncludePathList() const { return mProjectIncludePathList; }
void setOnGetFileStream(const GetFileStreamCallBack &newOnGetFileStream) { mOnGetFileStream = newOnGetFileStream; }
static QList<PDefineArgToken> tokenizeValue(const QString& value);
@ -198,40 +207,46 @@ private:
/*
* '_','a'..'z','A'..'Z','0'..'9'
*/
static bool isWordChar(const QChar& ch);
static bool isWordChar(const QChar& ch) {
return (ch=='_'
|| ch.isLetter()
|| (ch>='0' && ch<='9'));
}
/*
* 'A'..'Z', '0'..'9', 'a'..'z', '_', '*', '&', '~'
*/
static bool isIdentChar(const QChar& ch);
static bool isIdentChar(const QChar& ch) {
return (ch=='_' || ch == '*' || ch == '&' || ch == '~' ||
ch.isLetter()
|| (ch>='0' && ch<='9'));
}
/*
* '\r','\n'
*/
static bool isLineChar(const QChar& ch);
static bool isLineChar(const QChar& ch) { return ch=='\r' || ch == '\n'; }
/*
* '\t' ' '
*/
static bool isSpaceChar(const QChar& ch);
/*
* '+', '-', '*', '/', '!', '=', '<', '>', '&', '|', '^'
*/
//static bool isOperatorChar(const QChar& ch);
static bool isSpaceChar(const QChar& ch) { return ch == ' ' || ch == '\t'; }
/*
* 'A'..'Z', 'a'..'z', '_'
*/
static bool isMacroIdentChar(const QChar& ch);
static bool isMacroIdentChar(const QChar& ch) { return ch.isLetter() || ch == '_'; }
/*
* '0'..'9'
*/
static bool isDigit(const QChar& ch);
static bool isDigit(const QChar& ch) { return (ch>='0' && ch<='9'); }
/*
* '0'..'9','x',X','a'..'f','A'..'F','u','U','l','L'
*/
static bool isNumberChar(const QChar& ch);
QString lineBreak();
QString lineBreak() { return "\n"; }
bool evaluateIf(const QString& line);
QString expandDefines(QString line);

View File

@ -529,7 +529,6 @@ bool isHFile(const QString& filename)
{
if (filename.isEmpty())
return false;
QFileInfo fileInfo(filename);
return CppHeaderExts->contains(fileInfo.suffix().toLower());
@ -576,28 +575,12 @@ void CppScopes::addScope(int line, PStatement scopeStatement)
scope->startLine = line;
scope->statement = scopeStatement;
mScopes.append(scope);
#ifdef QT_DEBUG
if (!mScopes.isEmpty() && mScopes.back()->startLine>line) {
qDebug()<<QString("Error: new scope %1 at %2 which is less that last scope %3")
.arg(scopeStatement->fullName, line,mScopes.back()->startLine>line);
}
}
PStatement CppScopes::lastScope() const
{
if (mScopes.isEmpty())
return PStatement();
return mScopes.back()->statement;
}
void CppScopes::removeLastScope()
{
if (!mScopes.isEmpty())
mScopes.pop_back();
}
void CppScopes::clear()
{
mScopes.clear();
#endif
}
MemberOperatorType getOperatorType(const QString &phrase, int index)
@ -767,17 +750,6 @@ bool isTypeKind(StatementKind kind)
}
}
ParsedFileInfo::ParsedFileInfo(const QString &fileName):
mFileName { fileName }
{
}
void ParsedFileInfo::insertBranch(int level, bool branchTrue)
{
mBranches.insert(level, branchTrue);
}
bool ParsedFileInfo::isLineVisible(int line) const
{
int lastI=-1;
@ -789,93 +761,3 @@ bool ParsedFileInfo::isLineVisible(int line) const
}
return lastI<0?true:mBranches[lastI];
}
void ParsedFileInfo::addInclude(const QString &fileName)
{
mIncludes.insert(fileName);
}
void ParsedFileInfo::addDirectInclude(const QString &fileName)
{
mDirectIncludes.append(fileName);
}
bool ParsedFileInfo::including(const QString &fileName) const
{
return mIncludes.contains(fileName);
}
const QSet<QString>& ParsedFileInfo::includes() const
{
return mIncludes;
}
const QList<std::weak_ptr<ClassInheritanceInfo> >& ParsedFileInfo::handledInheritances() const
{
return mHandledInheritances;
}
QString ParsedFileInfo::fileName() const
{
return mFileName;
}
PStatement ParsedFileInfo::findScopeAtLine(int line) const
{
return mScopes.findScopeAtLine(line);
}
void ParsedFileInfo::addStatement(const PStatement &statement)
{
mStatements.insert(statement->fullName,statement);
}
void ParsedFileInfo::clearStatements()
{
mStatements.clear();
}
void ParsedFileInfo::addScope(int line, const PStatement &scope)
{
mScopes.addScope(line,scope);
}
void ParsedFileInfo::removeLastScope()
{
mScopes.removeLastScope();
}
PStatement ParsedFileInfo::lastScope() const
{
return mScopes.lastScope();
}
void ParsedFileInfo::addUsing(const QString &usingSymbol)
{
mUsings.insert(usingSymbol);
}
void ParsedFileInfo::addHandledInheritances(std::weak_ptr<ClassInheritanceInfo> classInheritanceInfo)
{
mHandledInheritances.append(classInheritanceInfo);
}
void ParsedFileInfo::clearHandledInheritances()
{
mHandledInheritances.clear();
}
const StatementMap& ParsedFileInfo::statements() const
{
return mStatements;
}
const QSet<QString>& ParsedFileInfo::usings() const
{
return mUsings;
}
const QStringList& ParsedFileInfo::directIncludes() const
{
return mDirectIncludes;
}

View File

@ -174,8 +174,6 @@ Q_DECLARE_FLAGS(StatementProperties, StatementProperty)
Q_DECLARE_OPERATORS_FOR_FLAGS(StatementProperties)
using PStatementMathPosition = std::shared_ptr<StatementMatchPosition>;
struct Statement;
@ -184,8 +182,6 @@ using StatementList = QList<PStatement>;
using PStatementList = std::shared_ptr<StatementList>;
using StatementMap = QMultiMap<QString, PStatement>;
struct Statement {
// Statement();
// ~Statement();
std::weak_ptr<Statement> parentScope; // parent class/struct/namespace scope, use weak pointer to prevent circular reference
QString type; // type "int"
QString command; // identifier/name of statement "foo"
@ -293,13 +289,19 @@ struct CppScope {
using PCppScope = std::shared_ptr<CppScope>;
class CppScopes {
public:
PStatement findScopeAtLine(int line) const;
void addScope(int line, PStatement scopeStatement);
PStatement lastScope() const;
void removeLastScope();
void clear();
PStatement lastScope() const {
if (mScopes.isEmpty())
return PStatement();
return mScopes.back()->statement;
}
void removeLastScope() {
if (!mScopes.isEmpty())
mScopes.pop_back();
}
void clear() { mScopes.clear(); }
private:
QVector<PCppScope> mScopes;
};
@ -319,32 +321,30 @@ using PClassInheritanceInfo = std::shared_ptr<ClassInheritanceInfo>;
class ParsedFileInfo {
public:
ParsedFileInfo(const QString& fileName);
ParsedFileInfo(const QString& fileName): mFileName {fileName} { }
ParsedFileInfo(const ParsedFileInfo&)=delete;
ParsedFileInfo& operator=(const ParsedFileInfo&)=delete;
void insertBranch(int level, bool branchTrue);
void insertBranch(int level, bool branchTrue) { mBranches.insert(level, branchTrue); }
bool isLineVisible(int line) const;
void addInclude(const QString &fileName);
void addDirectInclude(const QString &fileName);
bool including(const QString &fileName) const;
PStatement findScopeAtLine(int line) const;
void addStatement(const PStatement &statement);
void clearStatements();
void addScope(int line, const PStatement &scope);
void removeLastScope();
PStatement lastScope() const;
void addUsing(const QString &usingSymbol);
void addHandledInheritances(std::weak_ptr<ClassInheritanceInfo> classInheritanceInfo);
void clearHandledInheritances();
void addInclude(const QString &fileName) { mIncludes.insert(fileName); }
void addDirectInclude(const QString &fileName) { mDirectIncludes.append(fileName); }
bool including(const QString &fileName) const { return mIncludes.contains(fileName); }
PStatement findScopeAtLine(int line) const { return mScopes.findScopeAtLine(line); }
void addStatement(const PStatement &statement) { mStatements.insert(statement->fullName,statement); }
void clearStatements() { mStatements.clear(); }
void addScope(int line, const PStatement &scope) { mScopes.addScope(line,scope); }
void removeLastScope() { mScopes.removeLastScope(); }
PStatement lastScope() const { return mScopes.lastScope(); }
void addUsing(const QString &usingSymbol) { mUsings.insert(usingSymbol); }
void addHandledInheritances(std::weak_ptr<ClassInheritanceInfo> classInheritanceInfo) { mHandledInheritances.append(classInheritanceInfo); }
void clearHandledInheritances() { mHandledInheritances.clear(); }
QString fileName() const;
const StatementMap& statements() const;
const QSet<QString>& usings() const;
const QStringList& directIncludes() const;
const QSet<QString>& includes() const;
const QList<std::weak_ptr<ClassInheritanceInfo> >& handledInheritances() const;
const QSet<QString> &includedBySet() const;
int includedByCount(const QString &fileName) const;
QString fileName() const { return mFileName; }
const StatementMap& statements() const { return mStatements; }
const QSet<QString>& usings() const { return mUsings; }
const QStringList& directIncludes() const { return mDirectIncludes; }
const QSet<QString>& includes() const { return mIncludes; }
const QList<std::weak_ptr<ClassInheritanceInfo> >& handledInheritances() const { return mHandledInheritances; }
private:
QString mFileName;

View File

@ -39,7 +39,6 @@ void StatementModel::add(const PStatement& statement)
#ifdef QT_DEBUG
mAllStatements.append(statement);
#endif
}
void StatementModel::deleteStatement(const PStatement& statement)
@ -61,29 +60,6 @@ void StatementModel::deleteStatement(const PStatement& statement)
}
const StatementMap &StatementModel::childrenStatements(const PStatement& statement) const
{
if (!statement) {
return mGlobalStatements;
} else {
return statement->children;
}
}
const StatementMap &StatementModel::childrenStatements(std::weak_ptr<Statement> statement) const
{
PStatement s = statement.lock();
return childrenStatements(s);
}
void StatementModel::clear() {
mCount=0;
mGlobalStatements.clear();
#ifdef QT_DEBUG
mAllStatements.clear();
#endif
}
#ifdef QT_DEBUG
void StatementModel::dump(const QString &logFile)
{

View File

@ -30,12 +30,22 @@ public:
StatementModel& operator=(const StatementModel&)=delete;
void add(const PStatement& statement);
// function DeleteFirst: Integer;
// function DeleteLast: Integer;
void deleteStatement(const PStatement& statement);
const StatementMap& childrenStatements(const PStatement& statement = PStatement()) const;
const StatementMap& childrenStatements(std::weak_ptr<Statement> statement) const;
void clear();
const StatementMap& childrenStatements(const PStatement& statement = PStatement()) const {
if (!statement) {
return mGlobalStatements;
} else {
return statement->children;
}
}
const StatementMap& childrenStatements(std::weak_ptr<Statement> statement) const { return childrenStatements(statement.lock()); }
void clear() {
mCount=0;
mGlobalStatements.clear();
#ifdef QT_DEBUG
mAllStatements.clear();
#endif
}
int count() const { return mCount; }
#ifdef QT_DEBUG
void dump(const QString& logFile);