Added support for -U option to the GUI.

This commit is contained in:
alex 2018-10-09 15:05:05 +02:00 committed by Daniel Marjamäki
parent cf44964f20
commit babafd75e3
12 changed files with 151 additions and 4 deletions

View File

@ -127,6 +127,9 @@ void CheckThread::runAddonsAndTools(const ImportProject::FileSettings *fileSetti
foreach (QString D, QString::fromStdString(fileSettings->defines).split(";")) { foreach (QString D, QString::fromStdString(fileSettings->defines).split(";")) {
args << ("-D" + D); args << ("-D" + D);
} }
foreach (const std::string& U, fileSettings->undefs) {
args << QString::fromStdString("-U" + U);
}
const QString clangPath = CheckThread::clangTidyCmd(); const QString clangPath = CheckThread::clangTidyCmd();
if (!clangPath.isEmpty()) { if (!clangPath.isEmpty()) {

View File

@ -91,6 +91,11 @@ static CppcheckLibraryData::Define loadDefine(const QXmlStreamReader &xmlReader)
return define; return define;
} }
static QString loadUndefine(const QXmlStreamReader &xmlReader)
{
return xmlReader.attributes().value("name").toString();
}
static CppcheckLibraryData::Function::Arg loadFunctionArg(QXmlStreamReader &xmlReader) static CppcheckLibraryData::Function::Arg loadFunctionArg(QXmlStreamReader &xmlReader)
{ {
CppcheckLibraryData::Function::Arg arg; CppcheckLibraryData::Function::Arg arg;
@ -237,6 +242,8 @@ QString CppcheckLibraryData::open(QIODevice &file)
containers.append(loadContainer(xmlReader)); containers.append(loadContainer(xmlReader));
else if (elementName == "define") else if (elementName == "define")
defines.append(loadDefine(xmlReader)); defines.append(loadDefine(xmlReader));
else if (elementName == "undefine")
undefines.append(loadUndefine(xmlReader));
else if (elementName == "function") else if (elementName == "function")
functions.append(loadFunction(xmlReader, comments)); functions.append(loadFunction(xmlReader, comments));
else if (elementName == "memory" || elementName == "resource") else if (elementName == "memory" || elementName == "resource")
@ -459,6 +466,12 @@ QString CppcheckLibraryData::toString() const
xmlWriter.writeEndElement(); xmlWriter.writeEndElement();
} }
foreach (const QString &undef, undefines) {
xmlWriter.writeStartElement("undefine");
xmlWriter.writeAttribute("name", undef);
xmlWriter.writeEndElement();
}
foreach (const Function &function, functions) { foreach (const Function &function, functions) {
writeFunction(xmlWriter, function); writeFunction(xmlWriter, function);
} }

View File

@ -154,6 +154,7 @@ public:
void clear() { void clear() {
containers.clear(); containers.clear();
defines.clear(); defines.clear();
undefines.clear();
functions.clear(); functions.clear();
memoryresource.clear(); memoryresource.clear();
podtypes.clear(); podtypes.clear();
@ -163,6 +164,7 @@ public:
void swap(CppcheckLibraryData &other) { void swap(CppcheckLibraryData &other) {
containers.swap(other.containers); containers.swap(other.containers);
defines.swap(other.defines); defines.swap(other.defines);
undefines.swap(other.undefines);
functions.swap(other.functions); functions.swap(other.functions);
memoryresource.swap(other.memoryresource); memoryresource.swap(other.memoryresource);
podtypes.swap(other.podtypes); podtypes.swap(other.podtypes);
@ -176,6 +178,7 @@ public:
QList<struct Function> functions; QList<struct Function> functions;
QList<struct MemoryResource> memoryresource; QList<struct MemoryResource> memoryresource;
QList<struct PodType> podtypes; QList<struct PodType> podtypes;
QStringList undefines;
}; };
#endif // LIBRARYDATA_H #endif // LIBRARYDATA_H

View File

@ -838,6 +838,10 @@ Settings MainWindow::getCppcheckSettings()
result.userDefines += define.toStdString(); result.userDefines += define.toStdString();
} }
const QStringList undefines = mProjectFile->getUndefines();
foreach (QString undefine, undefines)
result.userUndefs.insert(undefine.toStdString());
const QStringList libraries = mProjectFile->getLibraries(); const QStringList libraries = mProjectFile->getLibraries();
foreach (QString library, libraries) { foreach (QString library, libraries) {
const QString filename = library + ".cfg"; const QString filename = library + ".cfg";

View File

@ -37,6 +37,8 @@ static const char DirNameAttrib[] = "name";
static const char DefinesElementName[] = "defines"; static const char DefinesElementName[] = "defines";
static const char DefineName[] = "define"; static const char DefineName[] = "define";
static const char DefineNameAttrib[] = "name"; static const char DefineNameAttrib[] = "name";
static const char UndefinesElementName[] = "undefines";
static const char UndefineName[] = "undefine";
static const char PathsElementName[] = "paths"; static const char PathsElementName[] = "paths";
static const char PathName[] = "dir"; static const char PathName[] = "dir";
static const char PathNameAttrib[] = "name"; static const char PathNameAttrib[] = "name";
@ -82,6 +84,7 @@ void ProjectFile::clear()
mAnalyzeAllVsConfigs = true; mAnalyzeAllVsConfigs = true;
mIncludeDirs.clear(); mIncludeDirs.clear();
mDefines.clear(); mDefines.clear();
mUndefines.clear();
mPaths.clear(); mPaths.clear();
mExcludedPaths.clear(); mExcludedPaths.clear();
mLibraries.clear(); mLibraries.clear();
@ -138,6 +141,10 @@ bool ProjectFile::read(const QString &filename)
if (insideProject && xmlReader.name() == DefinesElementName) if (insideProject && xmlReader.name() == DefinesElementName)
readDefines(xmlReader); readDefines(xmlReader);
// Find preprocessor define from inside project element
if (insideProject && xmlReader.name() == UndefinesElementName)
readStringList(mUndefines, xmlReader, UndefineName);
// Find exclude list from inside project element // Find exclude list from inside project element
if (insideProject && xmlReader.name() == ExcludeElementName) if (insideProject && xmlReader.name() == ExcludeElementName)
readExcludes(xmlReader); readExcludes(xmlReader);
@ -557,6 +564,11 @@ void ProjectFile::setDefines(const QStringList &defines)
mDefines = defines; mDefines = defines;
} }
void ProjectFile::setUndefines(const QStringList &undefines)
{
mUndefines = undefines;
}
void ProjectFile::setCheckPaths(const QStringList &paths) void ProjectFile::setCheckPaths(const QStringList &paths)
{ {
mPaths = paths; mPaths = paths;
@ -650,6 +662,11 @@ bool ProjectFile::write(const QString &filename)
xmlWriter.writeEndElement(); xmlWriter.writeEndElement();
} }
writeStringList(xmlWriter,
mUndefines,
UndefinesElementName,
UndefineName);
if (!mPaths.isEmpty()) { if (!mPaths.isEmpty()) {
xmlWriter.writeStartElement(PathsElementName); xmlWriter.writeStartElement(PathsElementName);
foreach (QString path, mPaths) { foreach (QString path, mPaths) {

View File

@ -84,6 +84,14 @@ public:
return mDefines; return mDefines;
} }
/**
* @brief Get list of undefines.
* @return list of undefines.
*/
QStringList getUndefines() const {
return mUndefines;
}
/** /**
* @brief Get list of paths to check. * @brief Get list of paths to check.
* @return list of paths. * @return list of paths.
@ -198,6 +206,12 @@ public:
*/ */
void setDefines(const QStringList &defines); void setDefines(const QStringList &defines);
/**
* @brief Set list of undefines.
* @param defines List of undefines.
*/
void setUndefines(const QStringList &undefines);
/** /**
* @brief Set list of paths to check. * @brief Set list of paths to check.
* @param paths List of paths. * @param paths List of paths.
@ -372,6 +386,11 @@ private:
*/ */
QStringList mDefines; QStringList mDefines;
/**
* @brief List of undefines.
*/
QStringList mUndefines;
/** /**
* @brief List of paths to check. * @brief List of paths to check.
*/ */

View File

@ -134,6 +134,9 @@ ProjectFileDialog::ProjectFileDialog(ProjectFile *projectFile, QWidget *parent)
mUI.mEditTags->setValidator(new QRegExpValidator(QRegExp("[a-zA-Z0-9 ;]*"),this)); mUI.mEditTags->setValidator(new QRegExpValidator(QRegExp("[a-zA-Z0-9 ;]*"),this));
const QRegExp undefRegExp("\\s*([a-zA-Z_][a-zA-Z0-9_]*[; ]*)*");
mUI.mEditUndefines->setValidator(new QRegExpValidator(undefRegExp, this));
connect(mUI.mButtons, &QDialogButtonBox::accepted, this, &ProjectFileDialog::ok); connect(mUI.mButtons, &QDialogButtonBox::accepted, this, &ProjectFileDialog::ok);
connect(mUI.mBtnBrowseBuildDir, &QPushButton::clicked, this, &ProjectFileDialog::browseBuildDir); connect(mUI.mBtnBrowseBuildDir, &QPushButton::clicked, this, &ProjectFileDialog::browseBuildDir);
connect(mUI.mBtnClearImportProject, &QPushButton::clicked, this, &ProjectFileDialog::clearImportProject); connect(mUI.mBtnClearImportProject, &QPushButton::clicked, this, &ProjectFileDialog::clearImportProject);
@ -192,6 +195,7 @@ void ProjectFileDialog::loadFromProjectFile(const ProjectFile *projectFile)
setBuildDir(projectFile->getBuildDir()); setBuildDir(projectFile->getBuildDir());
setIncludepaths(projectFile->getIncludeDirs()); setIncludepaths(projectFile->getIncludeDirs());
setDefines(projectFile->getDefines()); setDefines(projectFile->getDefines());
setUndefines(projectFile->getUndefines());
setCheckPaths(projectFile->getCheckPaths()); setCheckPaths(projectFile->getCheckPaths());
setImportProject(projectFile->getImportProject()); setImportProject(projectFile->getImportProject());
mUI.mChkAllVsConfigs->setChecked(projectFile->getAnalyzeAllVsConfigs()); mUI.mChkAllVsConfigs->setChecked(projectFile->getAnalyzeAllVsConfigs());
@ -268,6 +272,7 @@ void ProjectFileDialog::saveToProjectFile(ProjectFile *projectFile) const
projectFile->setAnalyzeAllVsConfigs(mUI.mChkAllVsConfigs->isChecked()); projectFile->setAnalyzeAllVsConfigs(mUI.mChkAllVsConfigs->isChecked());
projectFile->setIncludes(getIncludePaths()); projectFile->setIncludes(getIncludePaths());
projectFile->setDefines(getDefines()); projectFile->setDefines(getDefines());
projectFile->setUndefines(getUndefines());
projectFile->setCheckPaths(getCheckPaths()); projectFile->setCheckPaths(getCheckPaths());
projectFile->setExcludedPaths(getExcludedPaths()); projectFile->setExcludedPaths(getExcludedPaths());
projectFile->setLibraries(getLibraries()); projectFile->setLibraries(getLibraries());
@ -348,6 +353,7 @@ void ProjectFileDialog::updatePathsAndDefines()
mUI.mBtnEditCheckPath->setEnabled(!importProject); mUI.mBtnEditCheckPath->setEnabled(!importProject);
mUI.mBtnRemoveCheckPath->setEnabled(!importProject); mUI.mBtnRemoveCheckPath->setEnabled(!importProject);
mUI.mEditDefines->setEnabled(!importProject); mUI.mEditDefines->setEnabled(!importProject);
mUI.mEditUndefines->setEnabled(!importProject);
mUI.mBtnAddInclude->setEnabled(!importProject); mUI.mBtnAddInclude->setEnabled(!importProject);
mUI.mBtnEditInclude->setEnabled(!importProject); mUI.mBtnEditInclude->setEnabled(!importProject);
mUI.mBtnRemoveInclude->setEnabled(!importProject); mUI.mBtnRemoveInclude->setEnabled(!importProject);
@ -456,6 +462,14 @@ QStringList ProjectFileDialog::getDefines() const
return defines; return defines;
} }
QStringList ProjectFileDialog::getUndefines() const
{
const QString undefine = mUI.mEditUndefines->text().trimmed();
QStringList undefines = undefine.split(QRegExp("\\s*;\\s*"), QString::SkipEmptyParts);
undefines.removeDuplicates();
return undefines;
}
QStringList ProjectFileDialog::getCheckPaths() const QStringList ProjectFileDialog::getCheckPaths() const
{ {
const int count = mUI.mListCheckPaths->count(); const int count = mUI.mListCheckPaths->count();
@ -524,6 +538,11 @@ void ProjectFileDialog::setDefines(const QStringList &defines)
mUI.mEditDefines->setText(definestr); mUI.mEditDefines->setText(definestr);
} }
void ProjectFileDialog::setUndefines(const QStringList &undefines)
{
mUI.mEditUndefines->setText(undefines.join(";"));
}
void ProjectFileDialog::setCheckPaths(const QStringList &paths) void ProjectFileDialog::setCheckPaths(const QStringList &paths)
{ {
foreach (QString path, paths) { foreach (QString path, paths) {

View File

@ -75,6 +75,12 @@ private:
*/ */
QStringList getDefines() const; QStringList getDefines() const;
/**
* @brief Return undefine names from the dialog control.
* @return List of undefine names.
*/
QStringList getUndefines() const;
/** /**
* @brief Return check paths from the dialog control. * @brief Return check paths from the dialog control.
* @return List of check paths. * @return List of check paths.
@ -124,6 +130,12 @@ private:
*/ */
void setDefines(const QStringList &defines); void setDefines(const QStringList &defines);
/**
* @brief Set undefine names to dialog control.
* @param defines List of undefine names to set to dialog control.
*/
void setUndefines(const QStringList &defines);
/** /**
* @brief Set check paths to dialog control. * @brief Set check paths to dialog control.
* @param paths List of path names to set to dialog control. * @param paths List of path names to set to dialog control.

View File

@ -166,6 +166,27 @@
</item> </item>
</layout> </layout>
</item> </item>
<item>
<layout class="QHBoxLayout" name="mLayoutUndefines">
<item>
<widget class="QLabel" name="mLabelUndefines">
<property name="text">
<string>Undefines:</string>
</property>
<property name="buddy">
<cstring>mEditUndefines</cstring>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="mEditUndefines">
<property name="toolTip">
<string>Undefines must be separated by a semicolon. Example: UNDEF1;UNDEF2;UNDEF3</string>
</property>
</widget>
</item>
</layout>
</item>
<item> <item>
<layout class="QHBoxLayout" name="mLayoutIncludePaths"> <layout class="QHBoxLayout" name="mLayoutIncludePaths">
<item> <item>

View File

@ -125,6 +125,26 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QLabel" name="mUndefinesLabel">
<property name="text">
<string>Undefines:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="mUndefines">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item> <item>
<spacer name="mVerticalSpacer"> <spacer name="mVerticalSpacer">
<property name="orientation"> <property name="orientation">

View File

@ -50,6 +50,7 @@ void StatsDialog::setProject(const ProjectFile* projectFile)
mUI.mPaths->setText(projectFile->getCheckPaths().join(";")); mUI.mPaths->setText(projectFile->getCheckPaths().join(";"));
mUI.mIncludePaths->setText(projectFile->getIncludeDirs().join(";")); mUI.mIncludePaths->setText(projectFile->getIncludeDirs().join(";"));
mUI.mDefines->setText(projectFile->getDefines().join(";")); mUI.mDefines->setText(projectFile->getDefines().join(";"));
mUI.mUndefines->setText(projectFile->getUndefines().join(";"));
#ifndef HAVE_QCHART #ifndef HAVE_QCHART
mUI.mTabHistory->setVisible(false); mUI.mTabHistory->setVisible(false);
#else #else
@ -81,6 +82,7 @@ void StatsDialog::setProject(const ProjectFile* projectFile)
mUI.mPaths->setText(QString()); mUI.mPaths->setText(QString());
mUI.mIncludePaths->setText(QString()); mUI.mIncludePaths->setText(QString());
mUI.mDefines->setText(QString()); mUI.mDefines->setText(QString());
mUI.mUndefines->setText(QString());
} }
} }
@ -174,6 +176,7 @@ void StatsDialog::copyToClipboard()
const QString paths(tr("Paths")); const QString paths(tr("Paths"));
const QString incPaths(tr("Include paths")); const QString incPaths(tr("Include paths"));
const QString defines(tr("Defines")); const QString defines(tr("Defines"));
const QString undefines(tr("Undefines"));
const QString prevScan(tr("Previous Scan")); const QString prevScan(tr("Previous Scan"));
const QString selPath(tr("Path selected")); const QString selPath(tr("Path selected"));
const QString numFiles(tr("Number of files scanned")); const QString numFiles(tr("Number of files scanned"));
@ -193,6 +196,7 @@ void StatsDialog::copyToClipboard()
"\t%4:\t%5\n" "\t%4:\t%5\n"
"\t%6:\t%7\n" "\t%6:\t%7\n"
"\t%8:\t%9\n" "\t%8:\t%9\n"
"\t%10:\t%11\n"
) )
.arg(projSettings) .arg(projSettings)
.arg(project) .arg(project)
@ -202,7 +206,9 @@ void StatsDialog::copyToClipboard()
.arg(incPaths) .arg(incPaths)
.arg(mUI.mIncludePaths->text()) .arg(mUI.mIncludePaths->text())
.arg(defines) .arg(defines)
.arg(mUI.mDefines->text()); .arg(mUI.mDefines->text())
.arg(undefines)
.arg(mUI.mUndefines->text());
const QString previous = QString( const QString previous = QString(
"%1\n" "%1\n"
@ -251,6 +257,7 @@ void StatsDialog::copyToClipboard()
" <tr><th>%4:</th><td>%5</td></tr>\n" " <tr><th>%4:</th><td>%5</td></tr>\n"
" <tr><th>%6:</th><td>%7</td></tr>\n" " <tr><th>%6:</th><td>%7</td></tr>\n"
" <tr><th>%8:</th><td>%9</td></tr>\n" " <tr><th>%8:</th><td>%9</td></tr>\n"
" <tr><th>%10:</th><td>%11</td></tr>\n"
"</table>\n" "</table>\n"
) )
.arg(projSettings) .arg(projSettings)
@ -261,7 +268,9 @@ void StatsDialog::copyToClipboard()
.arg(incPaths) .arg(incPaths)
.arg(mUI.mIncludePaths->text()) .arg(mUI.mIncludePaths->text())
.arg(defines) .arg(defines)
.arg(mUI.mDefines->text()); .arg(mUI.mDefines->text())
.arg(undefines)
.arg(mUI.mUndefines->text());
const QString htmlPrevious = QString( const QString htmlPrevious = QString(
"<h3>%1</h3>\n" "<h3>%1</h3>\n"

View File

@ -97,7 +97,7 @@ unsigned int CppCheck::check(const ImportProject::FileSettings &fs)
temp.mSettings.userDefines += ';'; temp.mSettings.userDefines += ';';
temp.mSettings.userDefines += fs.cppcheckDefines(); temp.mSettings.userDefines += fs.cppcheckDefines();
temp.mSettings.includePaths = fs.includePaths; temp.mSettings.includePaths = fs.includePaths;
// TODO: temp.mSettings.userUndefs = fs.undefs; temp.mSettings.userUndefs = fs.undefs;
if (fs.platformType != Settings::Unspecified) { if (fs.platformType != Settings::Unspecified) {
temp.mSettings.platform(fs.platformType); temp.mSettings.platform(fs.platformType);
} }
@ -123,7 +123,14 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
mErrorLogger.reportOut(std::string("Checking ") + fixedpath + ' ' + cfgname + std::string("...")); mErrorLogger.reportOut(std::string("Checking ") + fixedpath + ' ' + cfgname + std::string("..."));
if (mSettings.verbose) { if (mSettings.verbose) {
mErrorLogger.reportOut("Defines: " + mSettings.userDefines); mErrorLogger.reportOut("Defines:" + mSettings.userDefines);
std::string undefs;
for (const std::string& U : mSettings.userUndefs) {
if (!undefs.empty())
undefs += ';';
undefs += ' ' + U;
}
mErrorLogger.reportOut("Undefines:" + undefs);
std::string includePaths; std::string includePaths;
for (const std::string &I : mSettings.includePaths) for (const std::string &I : mSettings.includePaths)
includePaths += " -I" + I; includePaths += " -I" + I;