Skip to content

fixed #13909 / refs #13914 - some (enforced) language handling fixes for the GUI #7570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ $(libcppdir)/vf_settokenvalue.o: lib/vf_settokenvalue.cpp lib/addoninfo.h lib/as
$(libcppdir)/vfvalue.o: lib/vfvalue.cpp lib/config.h lib/errortypes.h lib/mathlib.h lib/templatesimplifier.h lib/token.h lib/utils.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vfvalue.cpp

frontend/frontend.o: frontend/frontend.cpp frontend/frontend.h
frontend/frontend.o: frontend/frontend.cpp frontend/frontend.h lib/addoninfo.h lib/checkers.h lib/config.h lib/errortypes.h lib/filesettings.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/standards.h lib/utils.h
$(CXX) ${INCLUDE_FOR_FE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ frontend/frontend.cpp

cli/cmdlineparser.o: cli/cmdlineparser.cpp cli/cmdlinelogger.h cli/cmdlineparser.h cli/filelister.h externals/tinyxml2/tinyxml2.h lib/addoninfo.h lib/check.h lib/checkers.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/importproject.h lib/library.h lib/mathlib.h lib/path.h lib/pathmatch.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/utils.h lib/xml.h
Expand Down
45 changes: 7 additions & 38 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
#include "timer.h"
#include "utils.h"

#include "frontend.h"

#include <algorithm>
#include <cassert>
#include <climits>
Expand Down Expand Up @@ -221,40 +223,7 @@ bool CmdLineParser::fillSettingsFromArgs(int argc, const char* const argv[])

mFileSettings.clear();

if (mSettings.enforcedLang != Standards::Language::None)
{
// apply enforced language
for (auto& fs : fileSettings)
{
if (mSettings.library.markupFile(fs.filename()))
continue;
fs.file.setLang(mSettings.enforcedLang);
}
}
else
{
// identify files
for (auto& fs : fileSettings)
{
if (mSettings.library.markupFile(fs.filename()))
continue;
assert(fs.file.lang() == Standards::Language::None);
bool header = false;
fs.file.setLang(Path::identify(fs.filename(), mSettings.cppHeaderProbe, &header));
// unknown extensions default to C++
if (!header && fs.file.lang() == Standards::Language::None)
fs.file.setLang(Standards::Language::CPP);
}
}

// enforce the language since markup files are special and do not adhere to the enforced language
for (auto& fs : fileSettings)
{
if (mSettings.library.markupFile(fs.filename())) {
assert(fs.file.lang() == Standards::Language::None);
fs.file.setLang(Standards::Language::C);
}
}
frontend::applyLang(fileSettings, mSettings, mEnforcedLang);

// sort the markup last
std::copy_if(fileSettings.cbegin(), fileSettings.cend(), std::back_inserter(mFileSettings), [&](const FileSettings &fs) {
Expand Down Expand Up @@ -324,14 +293,14 @@ bool CmdLineParser::fillSettingsFromArgs(int argc, const char* const argv[])
files = std::move(filesResolved);
}

if (mSettings.enforcedLang != Standards::Language::None)
if (mEnforcedLang != Standards::Language::None)
{
// apply enforced language
for (auto& f : files)
{
if (mSettings.library.markupFile(f.path()))
continue;
f.setLang(mSettings.enforcedLang);
f.setLang(mEnforcedLang);
}
}
else
Expand Down Expand Up @@ -983,9 +952,9 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
}

if (str == "c")
mSettings.enforcedLang = Standards::Language::C;
mEnforcedLang = Standards::Language::C;
else if (str == "c++")
mSettings.enforcedLang = Standards::Language::CPP;
mEnforcedLang = Standards::Language::CPP;
else {
mLogger.printError("unknown language '" + str + "' enforced.");
return Result::Fail;
Expand Down
4 changes: 4 additions & 0 deletions cli/cmdlineparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include "cmdlinelogger.h"
#include "filesettings.h"
#include "standards.h"
#include "utils.h"

class Settings;
Expand All @@ -46,6 +47,7 @@ class Library;
* class internal options.
*/
class CmdLineParser {
friend class TestCmdlineParser;
public:
/**
* The constructor.
Expand Down Expand Up @@ -174,6 +176,8 @@ class CmdLineParser {
std::vector<std::string> mIgnoredPaths;
Settings &mSettings;
Suppressions &mSuppressions;
/** @brief Name of the language that is enforced. Empty per default. */
Standards::Language mEnforcedLang{Standards::Language::None};
};

/// @}
Expand Down
44 changes: 42 additions & 2 deletions frontend/frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,45 @@

#include "frontend.h"

namespace frontend
{}
#include "filesettings.h"
#include "settings.h"

namespace frontend {
void applyLang(std::list<FileSettings>& fileSettings, const Settings& settings, Standards::Language enforcedLang)
{
if (enforcedLang != Standards::Language::None)
{
// apply enforced language
for (auto& fs : fileSettings)
{
if (settings.library.markupFile(fs.filename()))
continue;
fs.file.setLang(enforcedLang);
}
}
else
{
// identify files
for (auto& fs : fileSettings)
{
if (settings.library.markupFile(fs.filename()))
continue;
assert(fs.file.lang() == Standards::Language::None);
bool header = false;
fs.file.setLang(Path::identify(fs.filename(), settings.cppHeaderProbe, &header));
// unknown extensions default to C++
if (!header && fs.file.lang() == Standards::Language::None)
fs.file.setLang(Standards::Language::CPP);
}
}

// enforce the language since markup files are special and do not adhere to the enforced language
for (auto& fs : fileSettings)
{
if (settings.library.markupFile(fs.filename())) {
assert(fs.file.lang() == Standards::Language::None);
fs.file.setLang(Standards::Language::C);
}
}
}
}
11 changes: 10 additions & 1 deletion frontend/frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@
#ifndef FRONTEND_H
#define FRONTEND_H

#include "standards.h"

#include <list>

struct FileSettings;
class Settings;

namespace frontend
{}
{
void applyLang(std::list<FileSettings> &fileSettings, const Settings &settings, Standards::Language enforcedLang);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still needs documentation - and unit test.

}

#endif // FRONTEND_H
2 changes: 2 additions & 0 deletions gui/checkthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ void CheckThread::run()
qDebug() << "Whole program analysis";
std::list<FileWithDetails> files2;
std::transform(mFiles.cbegin(), mFiles.cend(), std::back_inserter(files2), [&](const QString& file) {
// TODO: apply enforcedLanguage
return FileWithDetails{file.toStdString(), Path::identify(file.toStdString(), mSettings.cppHeaderProbe), 0};
});
cppcheck.analyseWholeProgram(mSettings.buildDir, files2, {}, ctuInfo);
Expand All @@ -154,6 +155,7 @@ void CheckThread::run()
QString file = mResult.getNextFile();
while (!file.isEmpty() && mState == Running) {
qDebug() << "Checking file" << file;
// TODO: apply enforcedLanguage
cppcheck.check(FileWithDetails(file.toStdString(), Path::identify(file.toStdString(), mSettings.cppHeaderProbe), 0));
runAddonsAndTools(mSettings, nullptr, file);
emit fileChecked(file);
Expand Down
12 changes: 9 additions & 3 deletions gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@

#include "ui_mainwindow.h"

#include "frontend.h"

#include <algorithm>
#include <iterator>
#include <list>
Expand Down Expand Up @@ -604,6 +606,10 @@ void MainWindow::doAnalyzeProject(ImportProject p, const bool checkLibrary, cons
mThread->setClangIncludePaths(clangHeaders.split(";"));
mThread->setSuppressions(mProjectFile->getSuppressions());
}

const Standards::Language enforcedLang = static_cast<Standards::Language>(mSettings->value(SETTINGS_ENFORCED_LANGUAGE, 0).toInt());
frontend::applyLang(p.fileSettings, checkSettings, enforcedLang);

mThread->setProject(p);
mThread->check(checkSettings, supprs);
mUI->mResults->setCheckSettings(checkSettings);
Expand Down Expand Up @@ -703,6 +709,7 @@ void MainWindow::analyzeCode(const QString& code, const QString& filename)
checkLockDownUI();
clearResults();
mUI->mResults->checkingStarted(1);
// TODO: apply enforcedLanguage
cppcheck.check(FileWithDetails(filename.toStdString(), Path::identify(filename.toStdString(), false), 0), code.toStdString());
analysisDone();

Expand Down Expand Up @@ -797,7 +804,7 @@ void MainWindow::analyzeFiles()
p.ignoreOtherConfigs(cfg.toStdString());
}

doAnalyzeProject(p);
doAnalyzeProject(p); // TODO: avoid copy
return;
}

Expand Down Expand Up @@ -1208,7 +1215,6 @@ bool MainWindow::getCppcheckSettings(Settings& settings, Suppressions& supprs)
settings.platform.set(static_cast<Platform::Type>(mSettings->value(SETTINGS_CHECKED_PLATFORM, 0).toInt()));
settings.standards.setCPP(mSettings->value(SETTINGS_STD_CPP, QString()).toString().toStdString());
settings.standards.setC(mSettings->value(SETTINGS_STD_C, QString()).toString().toStdString());
settings.enforcedLang = static_cast<Standards::Language>(mSettings->value(SETTINGS_ENFORCED_LANGUAGE, 0).toInt());

settings.jobs = std::max(settings.jobs, 1u);

Expand Down Expand Up @@ -1960,7 +1966,7 @@ void MainWindow::analyzeProject(const ProjectFile *projectFile, const QStringLis
msg.exec();
return;
}
doAnalyzeProject(p, checkLibrary, checkConfiguration);
doAnalyzeProject(p, checkLibrary, checkConfiguration); // TODO: avoid copy
return;
}

Expand Down
3 changes: 0 additions & 3 deletions lib/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,6 @@ class CPPCHECKLIB WARN_UNUSED Settings {
/** @brief Do not filter duplicated errors. */
bool emitDuplicates{};

/** @brief Name of the language that is enforced. Empty per default. */
Standards::Language enforcedLang{};

#if defined(USE_WINDOWS_SEH) || defined(USE_UNIX_SIGNAL_HANDLING)
/** @brief Is --exception-handling given */
bool exceptionHandling{};
Expand Down
8 changes: 4 additions & 4 deletions test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -765,14 +765,14 @@ class TestCmdlineParser : public TestFixture {
REDIRECT;
const char * const argv[] = {"cppcheck", "file.cpp"};
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
ASSERT_EQUALS(Standards::Language::None, settings->enforcedLang);
ASSERT_EQUALS(Standards::Language::None, parser->mEnforcedLang);
}

void enforceLanguage2() {
REDIRECT;
const char * const argv[] = {"cppcheck", "-x", "c++", "file.cpp"};
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
ASSERT_EQUALS(Standards::Language::CPP, settings->enforcedLang);
ASSERT_EQUALS(Standards::Language::CPP, parser->mEnforcedLang);
}

void enforceLanguage3() {
Expand All @@ -793,14 +793,14 @@ class TestCmdlineParser : public TestFixture {
REDIRECT;
const char * const argv[] = {"cppcheck", "--language=c++", "file.cpp"};
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
ASSERT_EQUALS(Standards::Language::CPP, settings->enforcedLang);
ASSERT_EQUALS(Standards::Language::CPP, parser->mEnforcedLang);
}

void enforceLanguage6() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--language=c", "file.cpp"};
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
ASSERT_EQUALS(Standards::Language::C, settings->enforcedLang);
ASSERT_EQUALS(Standards::Language::C, parser->mEnforcedLang);
}

void enforceLanguage7() {
Expand Down