Skip to content

Commit 84e6163

Browse files
committed
GUI: Add cppcheck build dir
1 parent 1235815 commit 84e6163

11 files changed

Lines changed: 239 additions & 93 deletions

gui/checkthread.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,35 @@ CheckThread::~CheckThread()
3737

3838
void CheckThread::Check(const Settings &settings)
3939
{
40+
mFiles.clear();
4041
mCppcheck.settings() = settings;
4142
start();
4243
}
4344

45+
void CheckThread::AnalyseWholeProgram(const QStringList &files)
46+
{
47+
mFiles = files;
48+
start();
49+
}
50+
4451
void CheckThread::run()
4552
{
4653
mState = Running;
4754

55+
if (!mFiles.isEmpty()) {
56+
qDebug() << "Whole program analysis";
57+
const std::string &buildDir = mCppcheck.settings().buildDir;
58+
if (!buildDir.empty()) {
59+
std::map<std::string,std::size_t> files2;
60+
for (QString file : mFiles)
61+
files2[file.toStdString()] = 0;
62+
mCppcheck.analyseWholeProgram(buildDir, files2);
63+
}
64+
mFiles.clear();
65+
emit Done();
66+
return;
67+
}
68+
4869
QString file = mResult.GetNextFile();
4970
while (!file.isEmpty() && mState == Running) {
5071
qDebug() << "Checking file" << file;

gui/checkthread.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ class CheckThread : public QThread {
4646
*/
4747
void Check(const Settings &settings);
4848

49+
/**
50+
* @brief Run whole program analysis
51+
* @param files All files
52+
*/
53+
void AnalyseWholeProgram(const QStringList &files);
54+
4955
/**
5056
* @brief method that is run in a thread
5157
*
@@ -91,7 +97,9 @@ class CheckThread : public QThread {
9197
*
9298
*/
9399
CppCheck mCppcheck;
100+
94101
private:
102+
QStringList mFiles;
95103
};
96104
/// @}
97105
#endif // CHECKTHREAD_H

gui/mainwindow.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,20 @@ void MainWindow::DoCheckFiles(const QStringList &files)
438438
if (mProject)
439439
qDebug() << "Checking project file" << mProject->GetProjectFile()->GetFilename();
440440

441+
if (!checkSettings.buildDir.empty()) {
442+
QString s = QString::fromStdString(checkSettings.buildDir);
443+
if (!s.endsWith('/'))
444+
s += '/';
445+
s += "files.txt";
446+
447+
std::ofstream fout(s.toStdString());
448+
if (fout.is_open()) {
449+
foreach (QString f, fileNames) {
450+
fout << f.toStdString() << '\n';
451+
}
452+
}
453+
}
454+
441455
mThread->SetCheckFiles(true);
442456
mThread->Check(checkSettings, true);
443457
}
@@ -742,6 +756,12 @@ Settings MainWindow::GetCppcheckSettings()
742756
// Only check the given -D configuration
743757
if (!defines.isEmpty())
744758
result.maxConfigs = 1;
759+
760+
QString buildDir = pfile->GetBuildDir();
761+
if (!buildDir.isEmpty()) {
762+
QString prjpath = QFileInfo(pfile->GetFilename()).absolutePath();
763+
result.buildDir = (prjpath + '/' + buildDir).toStdString();
764+
}
745765
}
746766

747767
// Include directories (and files) are searched in listed order.
@@ -759,6 +779,8 @@ Settings MainWindow::GetCppcheckSettings()
759779
result.addEnabled("portability");
760780
result.addEnabled("information");
761781
result.addEnabled("missingInclude");
782+
if (!result.buildDir.empty())
783+
result.addEnabled("unusedFunction");
762784
result.debug = false;
763785
result.debugwarnings = mSettings->value(SETTINGS_SHOW_DEBUG_WARNINGS, false).toBool();
764786
result.quiet = false;

gui/project.cpp

Lines changed: 33 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -86,53 +86,40 @@ bool Project::Open()
8686
bool Project::Edit()
8787
{
8888
ProjectFileDialog dlg(mFilename, mParentWidget);
89-
QString root = mPFile->GetRootPath();
90-
dlg.SetRootPath(root);
91-
QStringList includes = mPFile->GetIncludeDirs();
92-
dlg.SetIncludepaths(includes);
93-
QStringList defines = mPFile->GetDefines();
94-
dlg.SetDefines(defines);
95-
QStringList paths = mPFile->GetCheckPaths();
96-
dlg.SetPaths(paths);
97-
QString importProject = mPFile->GetImportProject();
98-
dlg.SetImportProject(importProject);
99-
QStringList ignorepaths = mPFile->GetExcludedPaths();
100-
dlg.SetExcludedPaths(ignorepaths);
101-
QStringList libraries = mPFile->GetLibraries();
102-
dlg.SetLibraries(libraries);
103-
QStringList suppressions = mPFile->GetSuppressions();
104-
dlg.SetSuppressions(suppressions);
105-
106-
int rv = dlg.exec();
107-
if (rv == QDialog::Accepted) {
108-
QString root = dlg.GetRootPath();
109-
mPFile->SetRootPath(root);
110-
mPFile->SetImportProject(dlg.GetImportProject());
111-
QStringList includes = dlg.GetIncludePaths();
112-
mPFile->SetIncludes(includes);
113-
QStringList defines = dlg.GetDefines();
114-
mPFile->SetDefines(defines);
115-
QStringList paths = dlg.GetPaths();
116-
mPFile->SetCheckPaths(paths);
117-
QStringList excludedpaths = dlg.GetExcludedPaths();
118-
mPFile->SetExcludedPaths(excludedpaths);
119-
QStringList libraries = dlg.GetLibraries();
120-
mPFile->SetLibraries(libraries);
121-
QStringList suppressions = dlg.GetSuppressions();
122-
mPFile->SetSuppressions(suppressions);
123-
124-
bool writeSuccess = mPFile->Write();
125-
if (!writeSuccess) {
126-
QMessageBox msg(QMessageBox::Critical,
127-
tr("Cppcheck"),
128-
tr("Could not write the project file."),
129-
QMessageBox::Ok,
130-
mParentWidget);
131-
msg.exec();
132-
}
133-
return writeSuccess;
89+
dlg.SetRootPath(mPFile->GetRootPath());
90+
dlg.SetBuildDir(mPFile->GetBuildDir());
91+
dlg.SetIncludepaths(mPFile->GetIncludeDirs());
92+
dlg.SetDefines(mPFile->GetDefines());
93+
dlg.SetPaths(mPFile->GetCheckPaths());
94+
dlg.SetImportProject(mPFile->GetImportProject());
95+
dlg.SetExcludedPaths(mPFile->GetExcludedPaths());
96+
dlg.SetLibraries(mPFile->GetLibraries());
97+
dlg.SetSuppressions(mPFile->GetSuppressions());
98+
99+
if (dlg.exec() != QDialog::Accepted)
100+
return false;
101+
102+
mPFile->SetRootPath(dlg.GetRootPath());
103+
mPFile->SetBuildDir(dlg.GetBuildDir());
104+
mPFile->SetImportProject(dlg.GetImportProject());
105+
mPFile->SetIncludes(dlg.GetIncludePaths());
106+
mPFile->SetDefines(dlg.GetDefines());
107+
mPFile->SetCheckPaths(dlg.GetPaths());
108+
mPFile->SetExcludedPaths(dlg.GetExcludedPaths());
109+
mPFile->SetLibraries(dlg.GetLibraries());
110+
mPFile->SetSuppressions(dlg.GetSuppressions());
111+
112+
if (!mPFile->Write()) {
113+
QMessageBox msg(QMessageBox::Critical,
114+
tr("Cppcheck"),
115+
tr("Could not write the project file."),
116+
QMessageBox::Ok,
117+
mParentWidget);
118+
msg.exec();
119+
return false;
134120
}
135-
return false;
121+
122+
return true;
136123
}
137124

138125
void Project::Create()

gui/projectfile.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
static const char ProjectElementName[] = "project";
2828
static const char ProjectVersionAttrib[] = "version";
2929
static const char ProjectFileVersion[] = "1";
30+
static const char BuildDirElementName[] = "builddir";
3031
static const char ImportProjectElementName[] = "importproject";
3132
static const char IncludeDirElementName[] = "includedir";
3233
static const char DirElementName[] = "dir";
@@ -84,6 +85,10 @@ bool ProjectFile::Read(const QString &filename)
8485
if (insideProject && xmlReader.name() == RootPathName)
8586
ReadRootPath(xmlReader);
8687

88+
// Read root path from inside project element
89+
if (insideProject && xmlReader.name() == BuildDirElementName)
90+
ReadBuildDir(xmlReader);
91+
8792
// Find paths to check from inside project element
8893
if (insideProject && xmlReader.name() == PathsElementName)
8994
ReadCheckPaths(xmlReader);
@@ -152,6 +157,31 @@ void ProjectFile::ReadRootPath(QXmlStreamReader &reader)
152157
mRootPath = name;
153158
}
154159

160+
void ProjectFile::ReadBuildDir(QXmlStreamReader &reader)
161+
{
162+
mBuildDir.clear();
163+
do {
164+
const QXmlStreamReader::TokenType type = reader.readNext();
165+
switch (type) {
166+
case QXmlStreamReader::Characters:
167+
mBuildDir = reader.text().toString();
168+
case QXmlStreamReader::EndElement:
169+
return;
170+
// Not handled
171+
case QXmlStreamReader::StartElement:
172+
case QXmlStreamReader::NoToken:
173+
case QXmlStreamReader::Invalid:
174+
case QXmlStreamReader::StartDocument:
175+
case QXmlStreamReader::EndDocument:
176+
case QXmlStreamReader::Comment:
177+
case QXmlStreamReader::DTD:
178+
case QXmlStreamReader::EntityReference:
179+
case QXmlStreamReader::ProcessingInstruction:
180+
break;
181+
}
182+
} while (1);
183+
}
184+
155185
void ProjectFile::ReadImportProject(QXmlStreamReader &reader)
156186
{
157187
mImportProject.clear();
@@ -426,6 +456,12 @@ bool ProjectFile::Write(const QString &filename)
426456
xmlWriter.writeEndElement();
427457
}
428458

459+
if (!mBuildDir.isEmpty()) {
460+
xmlWriter.writeStartElement(BuildDirElementName);
461+
xmlWriter.writeCharacters(mBuildDir);
462+
xmlWriter.writeEndElement();
463+
}
464+
429465
if (!mImportProject.isEmpty()) {
430466
xmlWriter.writeStartElement(ImportProjectElementName);
431467
xmlWriter.writeCharacters(mImportProject);

gui/projectfile.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ class ProjectFile : public QObject {
5454
return mRootPath;
5555
}
5656

57+
QString GetBuildDir() const {
58+
return mBuildDir;
59+
}
60+
5761
QString GetImportProject() const {
5862
return mImportProject;
5963
}
@@ -122,6 +126,11 @@ class ProjectFile : public QObject {
122126
mRootPath = rootpath;
123127
}
124128

129+
130+
void SetBuildDir(const QString &buildDir) {
131+
mBuildDir = buildDir;
132+
}
133+
125134
void SetImportProject(const QString &importProject) {
126135
mImportProject = importProject;
127136
}
@@ -193,6 +202,8 @@ class ProjectFile : public QObject {
193202
*/
194203
void ReadRootPath(QXmlStreamReader &reader);
195204

205+
void ReadBuildDir(QXmlStreamReader &reader);
206+
196207
/**
197208
* @brief Read importproject from XML.
198209
* @param reader XML stream reader.
@@ -251,6 +262,10 @@ class ProjectFile : public QObject {
251262
*/
252263
QString mRootPath;
253264

265+
/** Cppcheck build dir */
266+
QString mBuildDir;
267+
268+
/** Visual studio project/solution , compile database */
254269
QString mImportProject;
255270

256271
/**

gui/projectfile.ui

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>615</width>
10-
<height>357</height>
9+
<width>635</width>
10+
<height>368</height>
1111
</rect>
1212
</property>
1313
<property name="windowTitle">
@@ -41,6 +41,27 @@
4141
</item>
4242
</layout>
4343
</item>
44+
<item>
45+
<layout class="QHBoxLayout" name="horizontalLayout_9">
46+
<item>
47+
<widget class="QLabel" name="buildDirLabel">
48+
<property name="text">
49+
<string>Cppcheck build dir (optional)</string>
50+
</property>
51+
</widget>
52+
</item>
53+
<item>
54+
<widget class="QLineEdit" name="buildDirEdit"/>
55+
</item>
56+
<item>
57+
<widget class="QPushButton" name="buildDirBrowse">
58+
<property name="text">
59+
<string>...</string>
60+
</property>
61+
</widget>
62+
</item>
63+
</layout>
64+
</item>
4465
<item>
4566
<spacer name="verticalSpacer_10">
4667
<property name="orientation">

0 commit comments

Comments
 (0)