From a9295caff209c64f6b22c18182fd8e5c7377e68c Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Tue, 9 Apr 2024 22:12:03 +0800 Subject: [PATCH] optimization: make parser methods inline --- RedPandaIDE/parser/cpppreprocessor.cpp | 114 ----------------------- RedPandaIDE/parser/cpppreprocessor.h | 55 +++++++---- RedPandaIDE/parser/parserutils.cpp | 122 +------------------------ RedPandaIDE/parser/parserutils.h | 60 ++++++------ RedPandaIDE/parser/statementmodel.cpp | 24 ----- RedPandaIDE/parser/statementmodel.h | 20 +++- 6 files changed, 82 insertions(+), 313 deletions(-) diff --git a/RedPandaIDE/parser/cpppreprocessor.cpp b/RedPandaIDE/parser/cpppreprocessor.cpp index e9f18d5c..46164dbe 100644 --- a/RedPandaIDE/parser/cpppreprocessor.cpp +++ b/RedPandaIDE/parser/cpppreprocessor.cpp @@ -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 &CppPreprocessor::projectIncludePathList() const -{ - return mProjectIncludePathList; -} - -const QList &CppPreprocessor::includePathList() const -{ - return mIncludePathList; -} - -const DefineMap &CppPreprocessor::hardDefines() const -{ - return mHardDefines; -} diff --git a/RedPandaIDE/parser/cpppreprocessor.h b/RedPandaIDE/parser/cpppreprocessor.h index afd0230b..1d2a0962 100644 --- a/RedPandaIDE/parser/cpppreprocessor.h +++ b/RedPandaIDE/parser/cpppreprocessor.h @@ -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 &includePathList() const; + const QList &includePathList() const { return mIncludePathList; } - const QList &projectIncludePathList() const; - void setOnGetFileStream(const GetFileStreamCallBack &newOnGetFileStream); + const QList &projectIncludePathList() const { return mProjectIncludePathList; } + void setOnGetFileStream(const GetFileStreamCallBack &newOnGetFileStream) { mOnGetFileStream = newOnGetFileStream; } static QList 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); + static bool isNumberChar(const QChar& ch); - QString lineBreak(); + QString lineBreak() { return "\n"; } bool evaluateIf(const QString& line); QString expandDefines(QString line); diff --git a/RedPandaIDE/parser/parserutils.cpp b/RedPandaIDE/parser/parserutils.cpp index 6bcafd08..c372eecb 100644 --- a/RedPandaIDE/parser/parserutils.cpp +++ b/RedPandaIDE/parser/parserutils.cpp @@ -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()<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& ParsedFileInfo::includes() const -{ - return mIncludes; -} - -const QList >& 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) -{ - mHandledInheritances.append(classInheritanceInfo); -} - -void ParsedFileInfo::clearHandledInheritances() -{ - mHandledInheritances.clear(); -} - -const StatementMap& ParsedFileInfo::statements() const -{ - return mStatements; -} - -const QSet& ParsedFileInfo::usings() const -{ - return mUsings; -} - -const QStringList& ParsedFileInfo::directIncludes() const -{ - return mDirectIncludes; -} diff --git a/RedPandaIDE/parser/parserutils.h b/RedPandaIDE/parser/parserutils.h index a832a23e..ee328c7c 100644 --- a/RedPandaIDE/parser/parserutils.h +++ b/RedPandaIDE/parser/parserutils.h @@ -174,8 +174,6 @@ Q_DECLARE_FLAGS(StatementProperties, StatementProperty) Q_DECLARE_OPERATORS_FOR_FLAGS(StatementProperties) - - using PStatementMathPosition = std::shared_ptr; struct Statement; @@ -184,8 +182,6 @@ using StatementList = QList; using PStatementList = std::shared_ptr; using StatementMap = QMultiMap; struct Statement { -// Statement(); -// ~Statement(); std::weak_ptr 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; 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 mScopes; }; @@ -319,32 +321,30 @@ using PClassInheritanceInfo = std::shared_ptr; 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); - 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) { mHandledInheritances.append(classInheritanceInfo); } + void clearHandledInheritances() { mHandledInheritances.clear(); } - QString fileName() const; - const StatementMap& statements() const; - const QSet& usings() const; - const QStringList& directIncludes() const; - const QSet& includes() const; - const QList >& handledInheritances() const; - const QSet &includedBySet() const; - int includedByCount(const QString &fileName) const; + QString fileName() const { return mFileName; } + const StatementMap& statements() const { return mStatements; } + const QSet& usings() const { return mUsings; } + const QStringList& directIncludes() const { return mDirectIncludes; } + const QSet& includes() const { return mIncludes; } + const QList >& handledInheritances() const { return mHandledInheritances; } private: QString mFileName; diff --git a/RedPandaIDE/parser/statementmodel.cpp b/RedPandaIDE/parser/statementmodel.cpp index c564c1cb..04a5ecfa 100644 --- a/RedPandaIDE/parser/statementmodel.cpp +++ b/RedPandaIDE/parser/statementmodel.cpp @@ -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) 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) { diff --git a/RedPandaIDE/parser/statementmodel.h b/RedPandaIDE/parser/statementmodel.h index 7bc26a7c..c7f7e819 100644 --- a/RedPandaIDE/parser/statementmodel.h +++ b/RedPandaIDE/parser/statementmodel.h @@ -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) 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) 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);