work save

This commit is contained in:
Roy Qu 2022-03-23 13:56:41 +08:00
parent e80a92c30a
commit f228717eb7
2 changed files with 130 additions and 248 deletions

View File

@ -138,16 +138,6 @@ void CppPreprocessor::addDefineByLine(const QString &line, bool hardCoded)
addDefineByParts(name, args, value, hardCoded); addDefineByParts(name, args, value, hardCoded);
} }
PDefine CppPreprocessor::getDefine(const QString &name)
{
return mDefines.value(name,PDefine());
}
PDefine CppPreprocessor::getHardDefine(const QString &name)
{
return mHardDefines.value(name,PDefine());
}
void CppPreprocessor::reset() void CppPreprocessor::reset()
{ {
mResult.clear(); mResult.clear();
@ -160,14 +150,6 @@ void CppPreprocessor::reset()
resetDefines(); // do not throw away hardcoded resetDefines(); // do not throw away hardcoded
} }
void CppPreprocessor::resetDefines()
{
mDefines = mHardDefines;
// mDefines.clear();
// mDefines.insert(mHardDefines);
}
void CppPreprocessor::setScanOptions(bool parseSystem, bool parseLocal) void CppPreprocessor::setScanOptions(bool parseSystem, bool parseLocal)
{ {
mParseSystem = parseSystem; mParseSystem = parseSystem;
@ -302,22 +284,9 @@ QString CppPreprocessor::getNextPreprocessor()
return result; return result;
} }
void CppPreprocessor::simplify(QString &output)
{
// Remove #
output = output.mid(1).trimmed();
}
void CppPreprocessor::handleBranch(const QString &line) void CppPreprocessor::handleBranch(const QString &line)
{ {
if (line.startsWith("ifdef")) { if (line.startsWith("ifdef")) {
// // if a branch that is not at our level is false, current branch is false too;
// for (int i=0;i<=mBranchResults.count()-2;i++) {
// if (!mBranchResults[i]) {
// setCurrentBranch(false);
// return;
// }
// }
if (!getCurrentBranch()) { if (!getCurrentBranch()) {
setCurrentBranch(false); setCurrentBranch(false);
} else { } else {
@ -327,13 +296,6 @@ void CppPreprocessor::handleBranch(const QString &line)
} }
} else if (line.startsWith("ifndef")) { } else if (line.startsWith("ifndef")) {
// // if a branch that is not at our level is false, current branch is false too;
// for (int i=0;i<=mBranchResults.count()-2;i++) {
// if (!mBranchResults[i]) {
// setCurrentBranch(false);
// return;
// }
// }
if (!getCurrentBranch()) { if (!getCurrentBranch()) {
setCurrentBranch(false); setCurrentBranch(false);
} else { } else {
@ -342,13 +304,6 @@ void CppPreprocessor::handleBranch(const QString &line)
setCurrentBranch( getDefine(name)==nullptr ); setCurrentBranch( getDefine(name)==nullptr );
} }
} else if (line.startsWith("if")) { } else if (line.startsWith("if")) {
// // if a branch that is not at our level is false, current branch is false too;
// for (int i=0;i<=mBranchResults.count()-2;i++) {
// if (!mBranchResults[i]) {
// setCurrentBranch(false);
// return;
// }
// }
if (!getCurrentBranch()) {// we are already inside an if that is NOT being taken if (!getCurrentBranch()) {// we are already inside an if that is NOT being taken
setCurrentBranch(false);// so don't take this one either setCurrentBranch(false);// so don't take this one either
} else { } else {
@ -378,14 +333,6 @@ void CppPreprocessor::handleBranch(const QString &line)
} }
} }
void CppPreprocessor::handleDefine(const QString &line)
{
if (getCurrentBranch()) {
addDefineByLine(line, false);
mResult[mPreProcIndex] = '#' + line; // add define to result file so the parser can handle it
}
}
void CppPreprocessor::handleInclude(const QString &line, bool fromNext) void CppPreprocessor::handleInclude(const QString &line, bool fromNext)
{ {
if (!getCurrentBranch()) // we're skipping due to a branch failure if (!getCurrentBranch()) // we're skipping due to a branch failure
@ -620,11 +567,6 @@ void CppPreprocessor::removeGCCAttribute(const QString &line, QString &newLine,
} }
} }
PParsedFile CppPreprocessor::getInclude(int index)
{
return mIncludes[index];
}
void CppPreprocessor::openInclude(const QString &fileName, QStringList bufferedText) void CppPreprocessor::openInclude(const QString &fileName, QStringList bufferedText)
{ {
if (mIncludes.size()>0) { if (mIncludes.size()>0) {
@ -764,35 +706,6 @@ void CppPreprocessor::closeInclude()
.arg(parsedFile->index+1)); .arg(parsedFile->index+1));
} }
bool CppPreprocessor::getCurrentBranch()
{
if (!mBranchResults.isEmpty())
return mBranchResults.last();
else
return true;
}
void CppPreprocessor::setCurrentBranch(bool value)
{
mBranchResults.append(value);
}
void CppPreprocessor::removeCurrentBranch()
{
if (mBranchResults.size()>0)
mBranchResults.pop_back();
}
QStringList CppPreprocessor::result() const
{
return mResult;
}
PFileIncludes CppPreprocessor::getFileIncludesEntry(const QString &fileName)
{
return mIncludesList.value(fileName,PFileIncludes());
}
void CppPreprocessor::addDefinesInFile(const QString &fileName) void CppPreprocessor::addDefinesInFile(const QString &fileName)
{ {
if (mProcessed.contains(fileName)) if (mProcessed.contains(fileName))
@ -1104,103 +1017,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')
return true;
if (ch>='a' && ch<='f')
return true;
if (ch>='A' && ch<='F')
return true;
switch(ch.unicode()) {
case 'x':
case 'X':
case 'u':
case 'U':
case 'l':
case 'L':
return true;
default:
return false;
}
}
QString CppPreprocessor::lineBreak()
{
return "\n";
}
bool CppPreprocessor::evaluateIf(const QString &line)
{
QString newLine = expandDefines(line); // replace FOO by numerical value of FOO
return evaluateExpression(newLine);
}
QString CppPreprocessor::expandDefines(QString line) QString CppPreprocessor::expandDefines(QString line)
{ {
int searchPos = 0; int searchPos = 0;
@ -1784,39 +1600,3 @@ int CppPreprocessor::evaluateExpression(QString line)
return -1; return -1;
return result; return result;
} }
const QList<QString> &CppPreprocessor::projectIncludePathList() const
{
return mProjectIncludePathList;
}
const QList<QString> &CppPreprocessor::includePathList() const
{
return mIncludePathList;
}
const DefineMap &CppPreprocessor::hardDefines() const
{
return mHardDefines;
}
const QSet<QString> &CppPreprocessor::projectIncludePaths()
{
return mProjectIncludePaths;
}
const QSet<QString> &CppPreprocessor::includePaths()
{
return mIncludePaths;
}
QSet<QString> &CppPreprocessor::scannedFiles()
{
return mScannedFiles;
}
QHash<QString, PFileIncludes> &CppPreprocessor::includesList()
{
return mIncludesList;
}

View File

@ -75,29 +75,54 @@ public:
void addProjectIncludePath(const QString& fileName); void addProjectIncludePath(const QString& fileName);
void clearIncludePaths(); void clearIncludePaths();
void clearProjectIncludePaths(); void clearProjectIncludePaths();
QStringList result() const;
QHash<QString, PFileIncludes> &includesList(); const QStringList &result() const {
return mResult;
}
QSet<QString> &scannedFiles(); QHash<QString, PFileIncludes> &includesList() {
return mIncludesList;
}
const QSet<QString> &includePaths(); QSet<QString> &scannedFiles() {
return mScannedFiles;
}
const QSet<QString> &projectIncludePaths(); const QSet<QString> &includePaths() {
return mIncludePaths;
}
const DefineMap &hardDefines() const; const QSet<QString> &projectIncludePaths() {
return mProjectIncludePaths;
}
const QList<QString> &includePathList() const; const DefineMap &hardDefines() const {
return mHardDefines;
}
const QList<QString> &projectIncludePathList() const; const QList<QString> &includePathList() const {
return mIncludePathList;
}
const QList<QString> &projectIncludePathList() const {
return mProjectIncludePathList;
}
private: private:
void preprocessBuffer(); void preprocessBuffer();
void skipToEndOfPreprocessor(); void skipToEndOfPreprocessor();
void skipToPreprocessor(); void skipToPreprocessor();
QString getNextPreprocessor(); QString getNextPreprocessor();
void simplify(QString& output); void simplify(QString& output) {
// Remove #
output = output.mid(1).trimmed();
}
void handleBranch(const QString& line); void handleBranch(const QString& line);
void handleDefine(const QString& line); void handleDefine(const QString& line) {
if (getCurrentBranch()) {
addDefineByLine(line, false);
mResult[mPreProcIndex] = '#' + line; // add define to result file so the parser can handle it
}
}
void handleInclude(const QString& line, bool fromNext=false); void handleInclude(const QString& line, bool fromNext=false);
void handlePreprocessor(const QString& value); void handlePreprocessor(const QString& value);
void handleUndefine(const QString& line); void handleUndefine(const QString& line);
@ -105,24 +130,44 @@ private:
void expandMacro(const QString& line, QString& newLine, QString& word, int& i, int depth); void expandMacro(const QString& line, QString& newLine, QString& word, int& i, int depth);
QString removeGCCAttributes(const QString& line); QString removeGCCAttributes(const QString& line);
void removeGCCAttribute(const QString&line, QString& newLine, int &i, const QString& word); void removeGCCAttribute(const QString&line, QString& newLine, int &i, const QString& word);
PDefine getDefine(const QString& name); PDefine getDefine(const QString& name) {
return mDefines.value(name,PDefine());
}
// current file stuff // current file stuff
PParsedFile getInclude(int index); PParsedFile getInclude(int index) {
return mIncludes[index];
}
void openInclude(const QString& fileName, QStringList bufferedText=QStringList()); void openInclude(const QString& fileName, QStringList bufferedText=QStringList());
void closeInclude(); void closeInclude();
// branch stuff // branch stuff
bool getCurrentBranch(); bool getCurrentBranch() const{
void setCurrentBranch(bool value); if (!mBranchResults.isEmpty())
void removeCurrentBranch(); return mBranchResults.last();
else
return true;
}
void setCurrentBranch(bool value) {
mBranchResults.append(value);
}
void removeCurrentBranch() {
if (mBranchResults.size()>0)
mBranchResults.pop_back();
}
// include stuff // include stuff
PFileIncludes getFileIncludesEntry(const QString& FileName); PFileIncludes getFileIncludesEntry(const QString& fileName) {
return mIncludesList.value(fileName,PFileIncludes());
}
void addDefinesInFile(const QString& fileName); void addDefinesInFile(const QString& fileName);
void resetDefines(); void resetDefines() {
mDefines = mHardDefines;
}
void addDefineByParts(const QString& name, const QString& args, void addDefineByParts(const QString& name, const QString& args,
const QString& value, bool hardCoded); const QString& value, bool hardCoded);
void addDefineByLine(const QString& line, bool hardCoded); void addDefineByLine(const QString& line, bool hardCoded);
PDefine getHardDefine(const QString& name); PDefine getHardDefine(const QString& name) {
return mHardDefines.value(name,PDefine());
}
void invalidDefinesInFile(const QString& fileName); void invalidDefinesInFile(const QString& fileName);
void parseArgs(PDefine define); void parseArgs(PDefine define);
@ -132,42 +177,99 @@ private:
/* /*
* '_','a'..'z','A'..'Z','0'..'9' * '_','a'..'z','A'..'Z','0'..'9'
*/ */
bool isWordChar(const QChar& ch); bool isWordChar(const QChar& ch) const{
return (ch=='_' || (ch>='0' && ch<='9')
|| ch.isLetter()
);
}
/* /*
* 'A'..'Z', '0'..'9', 'a'..'z', '_', '*', '&', '~' * 'A'..'Z', '0'..'9', 'a'..'z', '_', '*', '&', '~'
*/ */
bool isIdentChar(const QChar& ch); bool isIdentChar(const QChar& ch) const {
return (ch=='_' || ch == '*' || ch == '&' || ch == '~' ||
ch.isLetter()
|| (ch>='0' && ch<='9'));
}
/* /*
* '\r','\n' * '\r','\n'
*/ */
bool isLineChar(const QChar& ch); bool isLineChar(const QChar& ch) const {
return ch=='\r' || ch == '\n';
}
/* /*
* '\t' ' ' * '\t' ' '
*/ */
bool isSpaceChar(const QChar& ch); bool isSpaceChar(const QChar& ch) const {
return ch == ' ' || ch == '\t';
}
/* /*
* '+', '-', '*', '/', '!', '=', '<', '>', '&', '|', '^' * '+', '-', '*', '/', '!', '=', '<', '>', '&', '|', '^'
*/ */
bool isOperatorChar(const QChar& ch); bool isOperatorChar(const QChar& ch) const {
switch(ch.unicode()) {
case '+':
case '-':
case '*':
case '/':
case '!':
case '=':
case '<':
case '>':
case '&':
case '|':
case '^':
return true;
default:
return false;
}
}
/* /*
* 'A'..'Z', 'a'..'z', '_' * 'A'..'Z', 'a'..'z', '_'
*/ */
bool isMacroIdentChar(const QChar& ch); bool isMacroIdentChar(const QChar& ch) const {
return ch.isLetter()
|| ch == '_';
}
/* /*
* '0'..'9' * '0'..'9'
*/ */
bool isDigit(const QChar& ch); bool isDigit(const QChar& ch) const {
return (ch>='0' && ch<='9');
}
/* /*
* '0'..'9','x',X','a'..'f','A'..'F','u','U','l','L' * '0'..'9','x',X','a'..'f','A'..'F','u','U','l','L'
*/ */
bool isNumberChar(const QChar& ch); bool isNumberChar(const QChar& ch) const {
if (ch>='0' && ch<='9')
return true;
if (ch>='a' && ch<='f')
return true;
if (ch>='A' && ch<='F')
return true;
switch(ch.unicode()) {
case 'x':
case 'X':
case 'u':
case 'U':
case 'l':
case 'L':
return true;
default:
return false;
}
}
QString lineBreak(); QString lineBreak() const{
return "\n";
}
bool evaluateIf(const QString& line); bool evaluateIf(const QString& line) {
QString newLine = expandDefines(line); // replace FOO by numerical value of FOO
return evaluateExpression(newLine);
}
QString expandDefines(QString line); QString expandDefines(QString line);
bool skipBraces(const QString&line, int& index, int step = 1); bool skipBraces(const QString&line, int& index, int step = 1);
QString expandFunction(PDefine define,QString args); QString expandFunction(PDefine define,QString args);