Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ ifndef INCLUDE_FOR_CLI
endif

ifndef INCLUDE_FOR_TEST
INCLUDE_FOR_TEST=-Ilib -Icli -isystem externals/simplecpp -isystem externals/tinyxml2
INCLUDE_FOR_TEST=-Ilib -Ifrontend -Icli -isystem externals/simplecpp -isystem externals/tinyxml2
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I forgot to add this in #7579.

endif

BIN=$(DESTDIR)$(PREFIX)/bin
Expand Down Expand Up @@ -295,6 +295,7 @@ TESTOBJ = test/fixture.o \
test/testexecutor.o \
test/testfilelister.o \
test/testfilesettings.o \
test/testfrontend.o \
test/testfunctions.o \
test/testgarbage.o \
test/testimportproject.o \
Expand Down Expand Up @@ -659,7 +660,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 Expand Up @@ -770,6 +771,9 @@ test/testfilelister.o: test/testfilelister.cpp cli/filelister.h lib/addoninfo.h
test/testfilesettings.o: test/testfilesettings.cpp lib/addoninfo.h lib/check.h lib/checkers.h lib/color.h lib/config.h lib/errorlogger.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 test/fixture.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testfilesettings.cpp

test/testfrontend.o: test/testfrontend.cpp lib/addoninfo.h lib/check.h lib/checkers.h lib/color.h lib/config.h lib/errorlogger.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 test/fixture.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testfrontend.cpp

test/testfunctions.o: test/testfunctions.cpp lib/addoninfo.h lib/check.h lib/checkers.h lib/checkfunctions.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/standards.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h test/helpers.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testfunctions.cpp

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 @@ -985,9 +954,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
6 changes: 4 additions & 2 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 @@ -175,8 +177,8 @@ class CmdLineParser {
Settings &mSettings;
Suppressions &mSuppressions;
bool mAnalyzeAllVsConfigsSetOnCmdLine = false;

friend class TestCmdlineParser;
/** @brief Name of the language that is enforced. Empty per default. */
Standards::Language mEnforcedLang{Standards::Language::None};
};

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

#include "frontend.h"

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

#include <cassert>

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);
}
}
}
}
14 changes: 13 additions & 1 deletion frontend/frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,19 @@
#ifndef FRONTEND_H
#define FRONTEND_H

#include "standards.h"

#include <list>

struct FileSettings;
class Settings;

namespace frontend
{}
{
/**
Applies the enforced language as all as identifying remaining files - also taking markup files into consideration.
*/
void applyLang(std::list<FileSettings> &fileSettings, const Settings &settings, Standards::Language enforcedLang);
Comment thread
firewave marked this conversation as resolved.
}

#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 @@ -151,6 +152,7 @@ void CheckThread::run()
return;
}

// TODO: apply enforcedLanguage
const FileWithDetails* file = nullptr;
mResult.getNextFile(file);
while (file && mState == Running) {
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
Loading
Loading