- fix: Can't parse enum values.

- fix: Can't correctly show enum values in the class browser.
  - fix: Can't correctly create project, if template's encoding setting is not valid.
  - enhancement: Add "embed assembly" template.
This commit is contained in:
Roy Qu 2023-02-15 16:24:24 +08:00
parent 1b8ff37a60
commit 25d0f5b782
23 changed files with 501 additions and 375 deletions

View File

@ -20,6 +20,10 @@ Red Panda C++ Version 2.12
- fix: If current editor is empty, parser will parse the file's content on the disk instead from the editor.
- fix: Can't show completion when cursor is after "char["
- change: Don't confirm rebuild/recompile when run/debug.
- fix: Can't parse enum values.
- fix: Can't correctly show enum values in the class browser.
- fix: Can't correctly create project, if template's encoding setting is not valid.
- enhancement: Add "embed assembly" template.
Red Panda C++ Version 2.11

View File

@ -9456,3 +9456,9 @@ void MainWindow::on_actionNew_GAS_File_triggered()
newEditor("s");
}
void MainWindow::on_actionGNU_Assembler_Manual_triggered()
{
QDesktopServices::openUrl(QUrl("https://sourceware.org/binutils/docs/as/index.html"));
}

View File

@ -777,6 +777,8 @@ private slots:
void on_actionNew_GAS_File_triggered();
void on_actionGNU_Assembler_Manual_triggered();
private:
Ui::MainWindow *ui;
bool mFullInitialized;

View File

@ -270,6 +270,7 @@
<addaction name="actionDocument"/>
<addaction name="actionC_Reference"/>
<addaction name="actionC_C_Reference"/>
<addaction name="actionGNU_Assembler_Manual"/>
<addaction name="actionRaylib_Manual"/>
<addaction name="actionEGE_Manual"/>
<addaction name="separator"/>
@ -3332,6 +3333,11 @@
<string>New GAS File</string>
</property>
</action>
<action name="actionGNU_Assembler_Manual">
<property name="text">
<string>GNU Assembler Manual</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>

View File

@ -1535,16 +1535,9 @@ QStringList CppParser::sortFilesByIncludeRelations(const QSet<QString> &files)
continue;
//already removed in interalInvalidateFiles
//mPreprocessor.removeScannedFile(file);
QStringList buffer;
if (mOnGetFileStream) {
mOnGetFileStream(file,buffer);
//prevent preprocessor to read file
if (buffer.isEmpty())
buffer.append(QString());
}
//we only use local include relations
mPreprocessor.setScanOptions(false, true);
mPreprocessor.preprocess(file,buffer);
mPreprocessor.preprocess(file);
mPreprocessor.clearTempResults();
}
@ -2267,8 +2260,11 @@ void CppParser::handleEnum(bool isTypedef)
QString args;
if (mTokenizer[mIndex]->text!='}') {
while ((mIndex < mTokenizer.tokenCount()) &&
!isblockChar(mTokenizer[mIndex]->text[0])) {
if (!mTokenizer[mIndex]->text.startsWith(',')) {
mTokenizer[mIndex]->text!='}') {
if (mTokenizer[mIndex]->text=="=") {
mIndex=indexOfNextPeriodOrSemicolon(mIndex);
continue;
} else if (tokenIsIdentifier(mTokenizer[mIndex]->text)) {
cmd = mTokenizer[mIndex]->text;
args = "";
if (isEnumClass) {
@ -3793,26 +3789,13 @@ void CppParser::internalParse(const QString &fileName)
// if (!isCfile(fileName) && !isHfile(fileName)) // support only known C/C++ files
// return;
QStringList buffer;
if (mOnGetFileStream) {
mOnGetFileStream(fileName,buffer);
//prevent preprocessor to read file
if (buffer.isEmpty())
buffer.append(QString());
}
// Preprocess the file...
{
auto action = finally([this]{
mTokenizer.clear();
});
// Let the preprocessor augment the include records
// mPreprocessor.setIncludesList(mIncludesList);
// mPreprocessor.setScannedFileList(mScannedFiles);
// mPreprocessor.setIncludePaths(mIncludePaths);
// mPreprocessor.setProjectIncludePaths(mProjectIncludePaths);
mPreprocessor.setScanOptions(mParseGlobalHeaders, mParseLocalHeaders);
mPreprocessor.preprocess(fileName, buffer);
mPreprocessor.preprocess(fileName);
QStringList preprocessResult = mPreprocessor.result();
#ifdef QT_DEBUG
@ -3846,7 +3829,6 @@ void CppParser::internalParse(const QString &fileName)
#endif
//reduce memory usage
internalClear();
}
}
void CppParser::inheritClassStatement(const PStatement& derived, bool isStruct,
@ -5732,7 +5714,6 @@ int CppParser::parserId() const
void CppParser::setOnGetFileStream(const GetFileStreamCallBack &newOnGetFileStream)
{
mOnGetFileStream = newOnGetFileStream;
mPreprocessor.setOnGetFileStream(newOnGetFileStream);
}

View File

@ -671,7 +671,6 @@ private:
#else
QMutex mMutex;
#endif
GetFileStreamCallBack mOnGetFileStream;
QMap<QString,KeywordType> mCppKeywords;
QSet<QString> mCppTypeKeywords;
};

View File

@ -155,12 +155,12 @@ void CppPreprocessor::setScanOptions(bool parseSystem, bool parseLocal)
mParseLocal=parseLocal;
}
void CppPreprocessor::preprocess(const QString &fileName, QStringList buffer)
void CppPreprocessor::preprocess(const QString &fileName)
{
clearTempResults();
mFileName = fileName;
mDefines = mHardDefines;
openInclude(fileName, buffer);
openInclude(fileName);
// StringsToFile(mBuffer,"f:\\buffer.txt");
preprocessBuffer();
// StringsToFile(mBuffer,"f:\\buffer.txt");
@ -456,11 +456,7 @@ void CppPreprocessor::handleInclude(const QString &line, bool fromNext)
return;
PFileIncludes oldCurrentIncludes = mCurrentIncludes;
QStringList buffer;
if (mOnGetFileStream) {
mOnGetFileStream(fileName,buffer);
}
openInclude(fileName, buffer);
openInclude(fileName);
}
void CppPreprocessor::handlePreprocessor(const QString &value)
@ -659,7 +655,7 @@ void CppPreprocessor::removeGCCAttribute(const QString &line, QString &newLine,
}
}
void CppPreprocessor::openInclude(const QString &fileName, QStringList bufferedText)
void CppPreprocessor::openInclude(const QString &fileName)
{
if (mIncludes.size()>0) {
PParsedFile topFile = mIncludes.front();
@ -719,7 +715,8 @@ void CppPreprocessor::openInclude(const QString &fileName, QStringList bufferedT
// Only load up the file if we are allowed to parse it
bool isSystemFile = isSystemHeaderFile(fileName, mIncludePaths) || isSystemHeaderFile(fileName, mProjectIncludePaths);
if ((mParseSystem && isSystemFile) || (mParseLocal && !isSystemFile)) {
if (!bufferedText.isEmpty()) {
QStringList bufferedText;
if (mOnGetFileStream && mOnGetFileStream(fileName,bufferedText)) {
parsedFile->buffer = bufferedText;
} else {
parsedFile->buffer = readFileToLines(fileName);

View File

@ -70,7 +70,7 @@ public:
void getDefineParts(const QString& input, QString &name, QString &args, QString &value);
void addHardDefineByLine(const QString& line);
void setScanOptions(bool parseSystem, bool parseLocal);
void preprocess(const QString& fileName, QStringList buffer = QStringList());
void preprocess(const QString& fileName);
void dumpDefinesTo(const QString& fileName) const;
void dumpIncludesListTo(const QString& fileName) const;
@ -122,7 +122,7 @@ private:
PParsedFile getInclude(int index) const {
return mIncludes[index];
}
void openInclude(const QString& fileName, QStringList bufferedText=QStringList());
void openInclude(const QString& fileName);
void closeInclude();
// branch stuff

View File

@ -921,6 +921,13 @@ bool Project::assignTemplate(const std::shared_ptr<ProjectTemplate> aTemplate, b
updateCompilerSetType();
mOptions.icon = aTemplate->icon();
QTextCodec* codec=QTextCodec::codecForName(mOptions.encoding);
if (!codec)
mOptions.encoding=ENCODING_SYSTEM_DEFAULT;
codec=QTextCodec::codecForName(mOptions.execEncoding);
if (!codec)
mOptions.execEncoding=ENCODING_SYSTEM_DEFAULT;
// Copy icon to project directory
if (!mOptions.icon.isEmpty()) {
QString originIcon = cleanPath(QFileInfo(aTemplate->fileName()).absoluteDir().absoluteFilePath(mOptions.icon));

View File

@ -4992,6 +4992,10 @@
<source>Please check detail info in &quot;Tools Output&quot; panel.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>GNU Assembler Manual</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>NewClassDialog</name>

File diff suppressed because it is too large Load Diff

View File

@ -4781,6 +4781,10 @@
<source>Please check detail info in &quot;Tools Output&quot; panel.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>GNU Assembler Manual</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>NewClassDialog</name>

View File

@ -260,14 +260,13 @@ PClassBrowserNode ClassBrowserModel::addChild(ClassBrowserNode *node, const PSta
.arg(statement->noNameArgs)
.arg((int)statement->kind),newNode);
mProcessedStatements.insert(statement.get());
if (statement->kind == StatementKind::skClass
|| statement->kind == StatementKind::skNamespace) {
if (isScopeStatement(statement)) {
mScopeNodes.insert(statement->fullName,newNode);
}
//don't show enum type's children values (they are displayed in parent scope)
if (statement->kind != StatementKind::skEnumType) {
// if (statement->kind != StatementKind::skEnumType) {
filterChildren(newNode.get(), statement->children);
}
// }
return newNode;
}
@ -373,7 +372,7 @@ void ClassBrowserModel::filterChildren(ClassBrowserNode *node, const StatementMa
if (dummyNode)
parentNode = dummyNode;
}
if (statement->kind == StatementKind::skNamespace || statement->kind == StatementKind::skClass) {
if (isScopeStatement(statement)) {
//PStatement dummy = mDummyStatements.value(statement->fullName,PStatement());
PClassBrowserNode scopeNode = mScopeNodes.value(statement->fullName,PClassBrowserNode());
if (!scopeNode) {
@ -415,8 +414,7 @@ ClassBrowserNode* ClassBrowserModel::getParentNode(const PStatement &parentState
return nullptr;
if (!parentStatement)
return mRoot;
if (parentStatement->kind!=skClass
&& parentStatement->kind!=skNamespace) {
if (!isScopeStatement(parentStatement)) {
return mRoot;
}
PClassBrowserNode parentNode = mScopeNodes.value(parentStatement->fullName,PClassBrowserNode());
@ -428,6 +426,19 @@ ClassBrowserNode* ClassBrowserModel::getParentNode(const PStatement &parentState
return parentNode.get();
}
bool ClassBrowserModel::isScopeStatement(const PStatement &statement)
{
switch(statement->kind) {
case StatementKind::skClass:
case StatementKind::skNamespace:
case StatementKind::skEnumClassType:
case StatementKind::skEnumType:
return true;
default:
return false;
}
}
QModelIndex ClassBrowserModel::modelIndexForStatement(const QString &key)
{
QMutexLocker locker(&mMutex);

View File

@ -75,6 +75,7 @@ private:
void filterChildren(ClassBrowserNode * node, const StatementMap& statements);
PStatement createDummy(const PStatement& statement);
ClassBrowserNode* getParentNode(const PStatement &parentStatement, int depth);
bool isScopeStatement(const PStatement& statement);
private:
ClassBrowserNode * mRoot;
QHash<QString,PStatement> mDummyStatements;

View File

@ -1615,14 +1615,13 @@ int QSynEdit::calcIndentSpaces(int line, const QString& lineText, bool addIndent
QString firstToken = mSyntaxer->getToken();
PTokenAttribute attr = mSyntaxer->getTokenAttribute();
if (
( (attr->tokenType() == TokenType::Keyword
(attr->tokenType() == TokenType::Keyword
&& (
firstToken == "public" || firstToken == "private"
|| firstToken == "protected" || firstToken == "case"
|| firstToken == "default"
)
)
|| (attr->tokenType() == TokenType::Identifier))
&& lineText.endsWith(':')
) {
// public: private: protecte: case: should indents like it's parent statement

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

@ -0,0 +1,22 @@
[Template]
Ver = 3
Name = Inline ASM
Category = Assembly
Description = A simple c program with inline assembly instructions
Name[zh_CN] = 内联汇编
Category[zh_CN] = 汇编
Description[zh_CN] = 简单的C内联汇编程序示例
Icon = app.ico
[Project]
Type = 1
IsCpp = 0
Encoding = SYSTEM
ClassBrowserType = 0
UnitCount = 1
[Unit0]
CName=main.c
C=main.c

View File

@ -0,0 +1,22 @@
#include <stdio.h>
int add(int x,int y) {
int result;
asm (
"movl %%eax, %1 \n"
"addl %%eax, %2 \n"
"movl %0, %%eax \n"
:"=m"(result) //output operands used in the instructions
:"m"(x),"m"(y) //input operands used in the instructions
:"eax" //registers changed by the assembler instructions
);
return x+y;
}
int main() {
int a,b,c;
scanf("%d,%d",&a,&b);
c=a+b;
printf("%d\n",c);
return 0;
}

View File

@ -2,13 +2,19 @@
.text:
main:
# the x64 calling convention requires you to allocate 32 bytes of shadow space before each call
# https://stackoverflow.com/questions/30190132/what-is-the-shadow-space-in-x64-assembly/
sub $32, %rsp # allocate shadow space
# Microsoft X86_64 Calling convention:
# - The first four integer or pointer parameters are passed in the rcx, rdx, r8, and r9 registers.
# - The first four floating-point parameters are passed in the first four SSE registers, xmm0-xmm3.
# - The caller reserves space on the stack for arguments passed in registers. The called function can use this space to spill the contents of registers to the stack.
# - Any additional arguments are passed on the stack.
# - An integer or pointer return value is returned in the rax register, while a floating-point return value is returned in xmm0.
# see https://learn.microsoft.com/en-us/windows-hardware/drivers/debugger/x64-architecture
sub $32, %rsp # reserve stack space for the call
leaq fmt(%rip), %rcx # first parameter
leaq msg(%rip), %rdx # second parameter
call printf
add $32, %rsp # remove shadow space
add $32, %rsp # restore stack space
xor %eax,%eax # set 0 as exit code
ret

View File

@ -8,13 +8,19 @@ fmt: db "%s", 10, 0 ; The printf format, "\n",'0'
section .text:
main:
; the x64 calling convention requires you to allocate 32 bytes of shadow space before each call
; https://stackoverflow.com/questions/30190132/what-is-the-shadow-space-in-x64-assembly/
sub rsp, 32 ; allocate shadow space
; Microsoft X86_64 Calling convention:
; - The first four integer or pointer parameters are passed in the rcx, rdx, r8, and r9 registers.
; - The first four floating-point parameters are passed in the first four SSE registers, xmm0-xmm3.
; - The caller reserves space on the stack for arguments passed in registers. The called function can use this space to spill the contents of registers to the stack.
; - Any additional arguments are passed on the stack.
; - An integer or pointer return value is returned in the rax register, while a floating-point return value is returned in xmm0.
; see https://learn.microsoft.com/en-us/windows-hardware/drivers/debugger/x64-architecture
sub rsp, 32 ; reserve stack space for call
lea rcx, [rel fmt] ; first parameter
lea rdx, [rel msg] ; secodng parameter
call printf
add rsp,32 ; restore stack
add rsp,32 ; remove shadow space
mov eax,0 ; exit code
ret

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

@ -0,0 +1,22 @@
[Template]
Ver = 3
Name = Inline ASM
Category = Assembly
Description = A simple c program with inline assembly instructions
Name[zh_CN] = 内联汇编
Category[zh_CN] = 汇编
Description[zh_CN] = 简单的C内联汇编程序示例
Icon = app.ico
[Project]
Type = 1
IsCpp = 0
Encoding = SYSTEM
ClassBrowserType = 0
UnitCount = 1
[Unit0]
CName=main.c
C=main.c

View File

@ -0,0 +1,22 @@
#include <stdio.h>
int add(int x,int y) {
int result;
asm (
"movl %%eax, %1 \n"
"addl %%eax, %2 \n"
"movl %0, %%eax \n"
:"=m"(result) //output operands used in the instructions
:"m"(x),"m"(y) //input operands used in the instructions
:"eax" //registers changed by the assembler instructions
);
return x+y;
}
int main() {
int a,b,c;
scanf("%d,%d",&a,&b);
c=a+b;
printf("%d\n",c);
return 0;
}