optimize parser

This commit is contained in:
Roy Qu 2023-03-13 00:13:23 +08:00
parent 586e6a6185
commit dab6a331b9
2 changed files with 22 additions and 10 deletions

View File

@ -3925,10 +3925,12 @@ void CppParser::internalParse(const QString &fileName)
// if (!isCfile(fileName) && !isHfile(fileName)) // support only known C/C++ files
// return;
//QElapsedTimer timer;
// Preprocess the file...
auto action = finally([this]{
mTokenizer.clear();
});
//timer.start();
// Let the preprocessor augment the include records
mPreprocessor.setScanOptions(mParseGlobalHeaders, mParseLocalHeaders);
mPreprocessor.preprocess(fileName);
@ -3939,13 +3941,18 @@ void CppParser::internalParse(const QString &fileName)
// mPreprocessor.dumpDefinesTo("r:\\defines.txt");
// mPreprocessor.dumpIncludesListTo("r:\\includes.txt");
#endif
//qDebug()<<"preprocess"<<timer.elapsed();
//reduce memory usage
//timer.restart();
mPreprocessor.clearTempResults();
//qDebug()<<"preprocess clean"<<timer.elapsed();
//timer.restart();
// Tokenize the preprocessed buffer file
mTokenizer.tokenize(preprocessResult);
//reduce memory usage
preprocessResult.clear();
//qDebug()<<"tokenize"<<timer.elapsed();
if (mTokenizer.tokenCount() == 0)
return;
#ifdef QT_DEBUG
@ -3954,11 +3961,13 @@ void CppParser::internalParse(const QString &fileName)
#ifdef QT_DEBUG
mLastIndex = -1;
#endif
// timer.restart();
// Process the token list
while(true) {
if (!handleStatement())
break;
}
// qDebug()<<"parse"<<timer.elapsed();
#ifdef QT_DEBUG
// mStatementList.dumpAll(QString("r:\\all-stats-%1.txt").arg(extractFileName(fileName)));
// mStatementList.dump(QString("r:\\stats-%1.txt").arg(extractFileName(fileName)));

View File

@ -323,7 +323,7 @@ QString CppPreprocessor::getNextPreprocessor()
QString result;
for (int i=preProcFrom;i<=preProcTo;i++) {
if (mBuffer[i].endsWith('\\')) {
result+=mBuffer[i].mid(0,mBuffer[i].size()-1)+' ';
result+=mBuffer[i].leftRef(mBuffer[i].size()-1)+' ';
} else {
result+=mBuffer[i]+' ';
}
@ -937,10 +937,12 @@ QStringList CppPreprocessor::removeComments(const QStringList &text)
QString s;
int pos = 0;
bool stopProcess=false;
while (pos<line.length()) {
int lineLen=line.length();
s.reserve(line.length());
while (pos<lineLen) {
QChar ch =line[pos];
if (currentType == ContentType::AnsiCComment) {
if (ch=='*' && (pos+1<line.length()) && line[pos+1]=='/') {
if (ch=='*' && (pos+1<lineLen) && line[pos+1]=='/') {
pos+=2;
currentType = ContentType::Other;
} else {
@ -986,7 +988,7 @@ QStringList CppPreprocessor::removeComments(const QStringList &text)
s+=ch;
break;
case 'R':
if (currentType == ContentType::Other && pos+1<line.length() && line[pos+1]=='"') {
if (currentType == ContentType::Other && pos+1<lineLen && line[pos+1]=='"') {
s+=ch;
pos++;
ch = line[pos];
@ -1010,11 +1012,11 @@ QStringList CppPreprocessor::removeComments(const QStringList &text)
break;
case '/':
if (currentType == ContentType::Other) {
if (pos+1<line.length() && line[pos+1]=='/') {
if (pos+1<lineLen && line[pos+1]=='/') {
// line comment , skip all remainings of the current line
stopProcess = true;
break;
} else if (pos+1<line.length() && line[pos+1]=='*') {
} else if (pos+1<lineLen && line[pos+1]=='*') {
/* ansi c comment */
pos++;
currentType = ContentType::AnsiCComment;
@ -1029,7 +1031,7 @@ QStringList CppPreprocessor::removeComments(const QStringList &text)
case ContentType::Character:
pos++;
s+=ch;
if (pos<line.length()) {
if (pos<lineLen) {
ch = line[pos];
s+=ch;
}
@ -1069,16 +1071,17 @@ void CppPreprocessor::preprocessBuffer()
void CppPreprocessor::skipToEndOfPreprocessor()
{
int bufferCount = mBuffer.count();
// Skip until last char of line is NOT \ anymore
while ((mIndex < mBuffer.count()) && mBuffer[mIndex].endsWith('\\'))
while ((mIndex < bufferCount) && mBuffer[mIndex].endsWith('\\'))
mIndex++;
}
void CppPreprocessor::skipToPreprocessor()
{
int bufferCount = mBuffer.count();
// Increment until a line begins with a #
while ((mIndex < mBuffer.count()) && !mBuffer[mIndex].startsWith('#')) {
while ((mIndex < bufferCount) && !mBuffer[mIndex].startsWith('#')) {
if (getCurrentBranch()) // if not skipping, expand current macros
mResult.append(expandMacros(mBuffer[mIndex],1));
else // If skipping due to a failed branch, clear line