- enhancement: Code Completion now respect compiler set's language standard settings.

This commit is contained in:
Roy Qu 2023-01-19 11:08:32 +08:00
parent 8603fa10ee
commit 4456772fa7
7 changed files with 63 additions and 74 deletions

View File

@ -5,6 +5,7 @@ Red Panda C++ Version 2.10
- enhancement: Don't create temp backup for readonly files.
- enhancement: Add "Help"/"Submit Iusses".
- enhancement: Add "Help"/"Document" for Simplified Chinese users.
- enhancement: Code Completion now respect compiler set's language standard settings.
Red Panda C++ Version 2.9

View File

@ -136,7 +136,7 @@ bool FileCompiler::prepareForCompile()
bool FileCompiler::prepareForRebuild()
{
QString exeName=compilerSet()->getCompileOptionValue(mFilename);
QString exeName=compilerSet()->getOutputFilename(mFilename);
QFile file(exeName);

View File

@ -220,6 +220,9 @@ public:
const PCppParser &parser() const;
void tab() override;
static PCppParser sharedParser(ParserLanguage language);
signals:
void renamed(const QString& oldName, const QString& newName, bool firstSave);
void fileSaved(const QString& filename, bool inProject);
@ -286,7 +289,6 @@ private:
void onExportedFormatToken(QSynedit::PSyntaxer syntaxer, int Line, int column, const QString& token,
QSynedit::PTokenAttribute &attr);
void onScrollBarValueChanged();
static PCppParser sharedParser(ParserLanguage language);
private:
QDateTime mBackupTime;
QFile* mBackupFile;

View File

@ -1571,14 +1571,19 @@ void MainWindow::changeOptions(const QString &widgetName, const QString &groupNa
return;
}
Editor *e = mEditorList->getEditor();
if (mProject && !e) {
if (mProject) {
scanActiveProject(true);
} else if (mProject && e && e->inProject()) {
scanActiveProject(true);
} else if (e) {
reparseNonProjectEditors();
}
reparseNonProjectEditors();
// Editor *e = mEditorList->getEditor();
// if (mProject && !e) {
// scanActiveProject(true);
// } else if (mProject && e && e->inProject()) {
// scanActiveProject(true);
// } else if (e) {
// reparseNonProjectEditors();
// }
}
@ -7188,6 +7193,30 @@ void MainWindow::setProjectViewCurrentUnit(std::shared_ptr<ProjectUnit> unit) {
void MainWindow::reparseNonProjectEditors()
{
if (pSettings->codeCompletion().shareParser()) {
bool hasC=false;
bool hasCpp=false;
for(int i=0;i<mEditorList->pageCount();i++) {
Editor* e=(*mEditorList)[i];
if (!e->inProject() && e->parser()) {
if (e->parser()->language()==ParserLanguage::C) {
hasC=true;
} else if (e->parser()->language()==ParserLanguage::CPlusPlus) {
hasCpp=true;
}
}
}
if (hasC) {
PCppParser parser{Editor::sharedParser(ParserLanguage::C)};
if (parser)
resetCppParser(parser);
}
if (hasCpp) {
PCppParser parser{Editor::sharedParser(ParserLanguage::CPlusPlus)};
if (parser)
resetCppParser(parser);
}
}
for (int i=0;i<mEditorList->pageCount();i++) {
Editor* e=(*mEditorList)[i];
if (!e->inProject()) {

View File

@ -1677,8 +1677,6 @@ Settings::CompilerSet::CompilerSet(const QString& compilerFolder, const QString&
setUserInput();
setDefines();
mFullLoaded = true;
} else {
mFullLoaded = false;
@ -1707,8 +1705,6 @@ Settings::CompilerSet::CompilerSet(const Settings::CompilerSet &set):
mVersion(set.mVersion),
mType(set.mType),
mName(set.mName),
mCppDefines(set.mCppDefines),
mCDefines(set.mCDefines),
mTarget(set.mTarget),
mCompilerType(set.mCompilerType),
mCompilerSetType(set.mCompilerSetType),
@ -1994,7 +1990,6 @@ QStringList &Settings::CompilerSet::defaultCIncludeDirs()
if (!mFullLoaded && !binDirs().isEmpty()) {
mFullLoaded=true;
setDirectories(binDirs()[0],mCompilerType);
setDefines();
}
return mDefaultCIncludeDirs;
}
@ -2004,7 +1999,6 @@ QStringList &Settings::CompilerSet::defaultCppIncludeDirs()
if (!mFullLoaded && !binDirs().isEmpty()) {
mFullLoaded=true;
setDirectories(binDirs()[0],mCompilerType);
setDefines();
}
return mDefaultCppIncludeDirs;
}
@ -2014,7 +2008,6 @@ QStringList &Settings::CompilerSet::defaultLibDirs()
if (!mFullLoaded && !binDirs().isEmpty()) {
mFullLoaded=true;
setDirectories(binDirs()[0],mCompilerType);
setDefines();
}
return mLibDirs;
}
@ -2059,27 +2052,6 @@ void Settings::CompilerSet::setName(const QString &value)
mName = value;
}
const QStringList& Settings::CompilerSet::CDefines()
{
if (!mFullLoaded && !binDirs().isEmpty()) {
mFullLoaded=true;
setDirectories(binDirs()[0],mCompilerType);
setDefines();
}
return mCDefines;
}
const QStringList &Settings::CompilerSet::CppDefines()
{
if (!mFullLoaded && !binDirs().isEmpty()) {
mFullLoaded=true;
setDirectories(binDirs()[0],mCompilerType);
setDefines();
}
return mCppDefines;
}
const QString &Settings::CompilerSet::target() const
{
return mTarget;
@ -2290,46 +2262,42 @@ void Settings::CompilerSet::setProperties(const QString& binDir, const QString&
}
}
void Settings::CompilerSet::setDefines() {
QStringList Settings::CompilerSet::defines(bool isCpp) {
// get default defines
QStringList arguments;
arguments.append("-dM");
arguments.append("-E");
arguments.append("-x");
arguments.append("c++");
arguments.append("-std=c++17");
QString key;
if (isCpp) {
arguments.append("c++");
key=CC_CMD_OPT_STD;
} else {
arguments.append("c");
key=C_CMD_OPT_STD;
}
//language standard
PCompilerOption pOption = CompilerInfoManager::getCompilerOption(compilerType(), key);
if (pOption) {
if (!mCompileOptions[key].isEmpty())
arguments.append(pOption->setting + mCompileOptions[key]);
}
arguments.append(NULL_FILE);
qDebug()<<arguments;
QFileInfo ccompiler(mCCompiler);
QByteArray output = getCompilerOutput(ccompiler.absolutePath(),ccompiler.fileName(),arguments);
// 'cpp.exe -dM -E -x c++ -std=c++17 NUL'
mCppDefines.clear();
QStringList result;
QList<QByteArray> lines = output.split('\n');
for (QByteArray& line:lines) {
QByteArray trimmedLine = line.trimmed();
if (!trimmedLine.isEmpty()) {
mCppDefines.append(trimmedLine);
result.append(trimmedLine);
}
}
arguments.clear();
arguments.append("-dM");
arguments.append("-E");
arguments.append("-x");
arguments.append("c");
arguments.append(NULL_FILE);
output = getCompilerOutput(ccompiler.absolutePath(),ccompiler.fileName(),arguments);
// 'cpp.exe -dM -E -x c NUL'
mCDefines.clear();
lines = output.split('\n');
for (QByteArray& line:lines) {
QByteArray trimmedLine = line.trimmed();
if (!trimmedLine.isEmpty()) {
mCDefines.append(trimmedLine);
}
}
return result;
}
void Settings::CompilerSet::setExecutables()

View File

@ -1334,8 +1334,7 @@ public:
void setType(const QString& value);
const QString& name() const;
void setName(const QString& value);
const QStringList& CppDefines();
const QStringList& CDefines();
QStringList defines(bool isCpp);
const QString& target() const;
void setTarget(const QString& value);
@ -1393,7 +1392,6 @@ public:
private:
void setDirectories(const QString& binDir, CompilerType mCompilerType);
//load hard defines
void setDefines();
void setExecutables();
void setUserInput();
@ -1426,8 +1424,6 @@ public:
QString mVersion; // "4.7.1"
QString mType; // "TDM-GCC", "MinGW"
QString mName; // "TDM-GCC 4.7.1 Release"
QStringList mCppDefines; // list of predefined constants
QStringList mCDefines; // list of predefined constants
QString mTarget; // 'X86_64' / 'i686'
CompilerType mCompilerType; // 'Clang' / 'GCC'
CompilerSetType mCompilerSetType; // RELEASE/ DEBUG/ Profile

View File

@ -310,16 +310,9 @@ void resetCppParser(std::shared_ptr<CppParser> parser, int compilerSetIndex)
foreach (const QString& file,compilerSet->defaultCIncludeDirs()) {
parser->addIncludePath(file);
}
//TODO: Add default include dirs last, just like gcc does
// Set defines
if (parser->language()==ParserLanguage::C) {
for (QString define:compilerSet->CDefines()) {
parser->addHardDefineByLine(define); // predefined constants from -dM -E
}
} else {
for (QString define:compilerSet->CppDefines()) {
parser->addHardDefineByLine(define); // predefined constants from -dM -E
}
for (QString define:compilerSet->defines(parser->language()==ParserLanguage::CPlusPlus)) {
parser->addHardDefineByLine(define);
}
// add a Red Pand C++ 's own macro
parser->addHardDefineByLine("#define EGE_FOR_AUTO_CODE_COMPLETETION_ONLY");