RedPanda-CPP/RedPandaIDE/parser/cpppreprocessor.h

148 lines
5.4 KiB
C
Raw Normal View History

2021-08-05 23:13:21 +08:00
#ifndef CPPPREPROCESSOR_H
#define CPPPREPROCESSOR_H
#include <QObject>
2021-08-07 18:02:57 +08:00
#include <QTextStream>
#include "utils.h"
struct ParsedFile {
int index; // 0-based for programming convenience
QString fileName; // Record filename, but not used now
QStringList buffer; // do not concat them all
int branches; //branch levels;
PFileIncludes fileIncludes; // includes of this file
};
using PParsedFile = std::shared_ptr<ParsedFile>;
2021-08-05 23:13:21 +08:00
class CppPreprocessor : public QObject
{
Q_OBJECT
public:
2021-08-07 18:02:57 +08:00
2021-08-05 23:13:21 +08:00
explicit CppPreprocessor(QObject *parent = nullptr);
2021-08-08 17:22:37 +08:00
void clear();
void addDefineByParts(const QString& name, const QString& args,
const QString& value, bool hardCoded);
void getDefineParts(const QString& Input, QString &name, QString &args, QString &value);
void addDefineByLine(const QString& line, bool hardCoded);
PDefine getDefine(const QString& name, int &index);
PDefine getHardDefine(const QString& name, int &index);
void reset(); //reset but don't clear generated defines
void resetDefines();
void setScanOptions(bool parseSystem, bool parseLocal);
void setIncludePaths(QStringList &list);
void setProjectIncludePaths(QStringList& list);
void setScannedFileList(QStringList &list);
void setIncludesList(QString& list);
void preprocessStream(const QString& fileName, QTextStream stream = QTextStream());
void preprocessFile(const QString& fileName);
void invalidDefinesInFile(const QString& fileName);
2021-08-05 23:13:21 +08:00
signals:
2021-08-07 18:02:57 +08:00
private:
void preprocessBuffer();
void skipToEndOfPreprocessor();
void skipToPreprocessor();
QString getNextPreprocessor();
void simplify(QString& output);
void handleBranch(const QString& line);
2021-08-08 17:22:37 +08:00
void handleDefine(const QString& line);
2021-08-07 18:02:57 +08:00
void handleInclude(const QString& line);
2021-08-08 17:22:37 +08:00
void handlePreprocessor(const QString& value);
void handleUndefine(const QString& line);
2021-08-07 18:02:57 +08:00
QString expandMacros(const QString& line, int depth);
2021-08-07 23:30:01 +08:00
void expandMacro(const QString& line, QString& newLine, QString& word, int& i, int depth);
2021-08-07 18:02:57 +08:00
QString removeGCCAttributes(const QString& line);
QString removeSuffixes(const QString& input);
// current file stuff
PParsedFile getInclude(int index);
void openInclude(const QString& fileName, QTextStream stream = QTextStream());
void closeInclude();
2021-08-07 23:30:01 +08:00
2021-08-07 18:02:57 +08:00
// branch stuff
bool getCurrentBranch();
void setCurrentBranch(bool value);
void removeCurrentBranch();
QString getResult();
// include stuff
PFileIncludes getFileIncludesEntry(const QString& FileName);
void addDefinesInFile(const QString& fileName);
2021-08-07 23:30:01 +08:00
2021-08-09 09:11:10 +08:00
/*
* '_','a'..'z','A'..'Z','0'..'9'
*/
bool isWordChar(const QChar& ch);
/*
* 'A'..'Z', '0'..'9', 'a'..'z', '_', '*', '&', '~'
*/
2021-08-07 23:30:01 +08:00
bool isIdentChar(const QChar& ch);
2021-08-09 09:47:36 +08:00
/*
* '\r','\n'
*/
bool isLineChar(const QChar& ch);
/*
* '\t' ' '
*/
bool isSpaceChar(const QChar& ch);
/*
* '+', '-', '*', '/', '!', '=', '<', '>', '&', '|', '^'
*/
bool isOperatorChar(const QChar& ch);
/*
* 'A'..'Z', 'a'..'z', '_'
*/
bool isMacroIdentChar(const QChar& ch);
2021-08-08 17:22:37 +08:00
2021-08-10 12:09:48 +08:00
/*
* '0'..'9'
*/
bool isDigit(const QChar& ch);
2021-08-08 17:22:37 +08:00
QString lineBreak();
2021-08-09 09:11:10 +08:00
bool evaluateIf(const QString& line);
2021-08-10 12:09:48 +08:00
QString expandDefines(QString line);
bool skipBraces(const QString&line, int& index, int step = 1);
QString expandFunction(PDefine define,QString args);
bool skipSpaces(const QString &expr, int& pos);
bool evalTerm(const QString &expr, int& result, int& pos);
bool evalUnaryExpr(const QString &expr, int& result, int& pos);
bool evalMulExpr(const QString &expr, int& result, int& pos);
bool evalAddExpr(const QString &expr, int& result, int& pos);
bool evalShiftExpr(const QString &expr, int& result, int& pos);
bool evalRelationExpr(const QString &expr, int& result, int& pos);
bool evalEqualExpr(const QString &expr, int& result, int& pos);
bool evalBitAndExpr(const QString &expr, int& result, int& pos);
bool evalBitXorExpr(const QString &expr, int& result, int& pos);
bool evalBitOrExpr(const QString &expr, int& result, int& pos);
bool evalLogicAndExpr(const QString &expr, int& result, int& pos);
bool evalLogicOrExpr(const QString &expr, int& result, int& pos);
bool evalExpr(const QString &expr, int& result, int& pos);
QString evaluateExpression(QString line);
2021-08-07 18:02:57 +08:00
private:
int mIndex; // points to current file buffer. do not free
QString mFileName; // idem
QStringList mBuffer; // idem
QStringList mResult;
PFileIncludes mCurrentIncludes;
int mPreProcIndex;
2021-08-08 17:22:37 +08:00
QHash<QString,PFileIncludes> mIncludesList;
2021-08-07 18:02:57 +08:00
DefineMap mHardDefines; // set by "cpp -dM -E -xc NUL"
DefineMap mDefines; // working set, editable
QHash<QString, PDefineMap> mFileDefines; //dictionary to save defines for each headerfile;
QList<PParsedFile> mIncludes; // stack of files we've stepped into. last one is current file, first one is source file
2021-08-07 23:30:01 +08:00
QList<bool> mBranchResults;// stack of branch results (boolean). last one is current branch, first one is outermost branch
2021-08-07 18:02:57 +08:00
QStringList mIncludePaths; // path to include folders
QStringList mProjectIncludePaths;
bool mParseSystem;
bool mParseLocal;
2021-08-08 17:22:37 +08:00
QSet<QString> mScannedFiles;
2021-08-07 18:02:57 +08:00
QSet<QString> mProcessed; // dictionary to save filename already processed
2021-08-09 09:47:36 +08:00
QStringList mOperators;
2021-08-05 23:13:21 +08:00
};
#endif // CPPPREPROCESSOR_H