Skip to content

Adding support for multiple variables of the same type to a plugin for vscode #585

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

Merged
merged 2 commits into from
Mar 15, 2023
Merged
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
20 changes: 20 additions & 0 deletions integration-tests/c-example/lib/different_variables.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "different_variables.h"

void swap_two_int_pointers(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}

float max_of_two_float(float a, float b) {
if (a > b)
return a;
return b;
}

int struct_test(struct easy_str a, struct easy_str b) {
if (a.a == b.a) {
return 1;
}
return -1;
}
15 changes: 15 additions & 0 deletions integration-tests/c-example/lib/different_variables.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef SIMPLE_TEST_PROJECT_DIFFERENT_VARIABLES_H
#define SIMPLE_TEST_PROJECT_DIFFERENT_VARIABLES_H

struct easy_str {
int a;
char b;
};

void swap_two_int_pointers(int *a, int *b);

float max_of_two_float(float a, float b);

int struct_test(struct easy_str a, struct easy_str b);

#endif // SIMPLE_TEST_PROJECT_DIFFERENT_VARIABLES_H
1 change: 1 addition & 0 deletions server/proto/testgen.proto
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ message SettingsContext {
bool useDeterministicSearcher = 5;
bool useStubs = 6;
ErrorMode errorMode = 7;
bool differentVariablesOfTheSameType = 8;
}

message SnippetRequest {
Expand Down
2 changes: 1 addition & 1 deletion server/src/KleeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ std::vector<fs::path> KleeGenerator::buildKleeFiles(const tests::TestsMap &tests
const std::shared_ptr<LineInfo> &lineInfo) {
std::vector<fs::path> outFiles;
LOG_S(DEBUG) << "Building generated klee files...";
printer::KleePrinter kleePrinter(&typesHandler, testGen->getTargetBuildDatabase(), utbot::Language::UNKNOWN);
printer::KleePrinter kleePrinter(&typesHandler, testGen->getTargetBuildDatabase(), utbot::Language::UNKNOWN, testGen);
ExecUtils::doWorkWithProgress(
testsMap, testGen->progressWriter, "Building generated klee files",
[&](auto const &it) {
Expand Down
2 changes: 1 addition & 1 deletion server/src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ Status Server::TestsGenServiceImpl::ProcessBaseTestRequest(BaseTestGen &testGen,
if (lineTestGen->needToAddPathFlag()) {
LOG_S(DEBUG) << "Added test line flag for file " << lineInfo->filePath;
fs::path flagFilePath =
printer::KleePrinter(&typesHandler, nullptr, Paths::getSourceLanguage(lineInfo->filePath))
printer::KleePrinter(&typesHandler, nullptr, Paths::getSourceLanguage(lineInfo->filePath), &testGen)
.addTestLineFlag(lineInfo, lineInfo->forAssert, testGen.projectContext);
pathSubstitution = {lineTestGen->filePath, flagFilePath};
}
Expand Down
9 changes: 6 additions & 3 deletions server/src/SettingsContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ namespace utbot {
int32_t timeoutPerTest,
bool useDeterministicSearcher,
bool useStubs,
testsgen::ErrorMode errorMode)
testsgen::ErrorMode errorMode,
bool differentVariablesOfTheSameType)
: generateForStaticFunctions(generateForStaticFunctions),
verbose(verbose),
timeoutPerFunction(timeoutPerFunction > 0
Expand All @@ -19,7 +20,8 @@ namespace utbot {
? std::make_optional(std::chrono::seconds{ timeoutPerTest })
: std::nullopt),
useDeterministicSearcher(useDeterministicSearcher), useStubs(useStubs),
errorMode(errorMode) {
errorMode(errorMode),
differentVariablesOfTheSameType (differentVariablesOfTheSameType) {
}
SettingsContext::SettingsContext(const testsgen::SettingsContext &settingsContext)
: SettingsContext(settingsContext.generateforstaticfunctions(),
Expand All @@ -28,6 +30,7 @@ namespace utbot {
settingsContext.timeoutpertest(),
settingsContext.usedeterministicsearcher(),
settingsContext.usestubs(),
settingsContext.errormode()) {
settingsContext.errormode(),
settingsContext.differentvariablesofthesametype()) {
}
}
4 changes: 3 additions & 1 deletion server/src/SettingsContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ namespace utbot {
int32_t timeoutPerTest,
bool useDeterministicSearcher,
bool useStubs,
testsgen::ErrorMode errorMode);
testsgen::ErrorMode errorMode,
bool differentVariablesOfTheSameType);

const bool generateForStaticFunctions;
const bool verbose;
const std::optional<std::chrono::seconds> timeoutPerFunction, timeoutPerTest;
const bool useDeterministicSearcher;
const bool useStubs;
testsgen::ErrorMode errorMode;
const bool differentVariablesOfTheSameType;
};
}

Expand Down
4 changes: 4 additions & 0 deletions server/src/commands/Commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,10 @@ ErrorMode Commands::SettingsContextOptionGroup::getErrorMode() const {
return errorMode;
}

bool Commands::SettingsContextOptionGroup::doDifferentVariablesOfTheSameType() const {
return differentVariablesOfTheSameType;
}

Commands::RunTestsCommands::RunTestsCommands(Commands::MainCommands &commands) {
runCommand = commands.getRunTestsCommand();

Expand Down
3 changes: 3 additions & 0 deletions server/src/commands/Commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ namespace Commands {

[[nodiscard]] ErrorMode getErrorMode() const;

[[nodiscard]] bool doDifferentVariablesOfTheSameType() const;

private:
CLI::Option_group *settingsContextOptions;
bool generateForStaticFunctions = true;
Expand All @@ -254,6 +256,7 @@ namespace Commands {
bool noDeterministicSearcher = false;
bool noStubs = false;
ErrorMode errorMode = ErrorMode::FAILING;
bool differentVariablesOfTheSameType = false;
};
};

Expand Down
19 changes: 12 additions & 7 deletions server/src/printers/KleeConstraintsPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ printer::KleeConstraintsPrinter::KleeConstraintsPrinter(const types::TypesHandle
: Printer(srcLanguage), typesHandler(typesHandler) {}

printer::KleeConstraintsPrinter::Stream
KleeConstraintsPrinter::genConstraints(const std::string &name, const types::Type& type) {
KleeConstraintsPrinter::genConstraints(const std::string &name, const types::Type& type, const std::vector<std::string>& names) {
ConstraintsState state = { "&" + name, name, type, true };
auto paramType = type;
if (type.maybeJustPointer()) {
Expand All @@ -34,19 +34,19 @@ KleeConstraintsPrinter::genConstraints(const std::string &name, const types::Typ
genConstraintsForEnum(state);
break;
default:
genConstraintsForPrimitive(state);
genConstraintsForPrimitive(state, names);
}

return ss;
}

printer::KleeConstraintsPrinter::Stream
KleeConstraintsPrinter::genConstraints(const Tests::MethodParam &param) {
return genConstraints(param.name, param.type);
KleeConstraintsPrinter::genConstraints(const Tests::MethodParam &param, const std::vector<std::string>& names) {
return genConstraints(param.name, param.type, names);
}

void KleeConstraintsPrinter::genConstraintsForPrimitive(const ConstraintsState &state) {
const auto &cons = cexConstraints(state.curElement, state.curType);
void KleeConstraintsPrinter::genConstraintsForPrimitive(const ConstraintsState &state, const std::vector<std::string>& names) {
const auto &cons = cexConstraints(state.curElement, state.curType, names);
if (!cons.empty()) {
strFunctionCall(PrinterUtils::KLEE_PREFER_CEX, { state.paramName, cons });
} else {
Expand Down Expand Up @@ -164,7 +164,7 @@ void KleeConstraintsPrinter::genConstraintsForStruct(const ConstraintsState &sta
}
}

std::string KleeConstraintsPrinter::cexConstraints(const std::string &name, const types::Type &type) {
std::string KleeConstraintsPrinter::cexConstraints(const std::string &name, const types::Type &type, const std::vector<std::string>& names) {
if (!CollectionUtils::containsKey(TypesHandler::preferredConstraints(), type.baseType())) {
return "";
}
Expand All @@ -176,6 +176,11 @@ std::string KleeConstraintsPrinter::cexConstraints(const std::string &name, cons
ssCex << " & ";
}
}
for (const std::string& currentName: names){
if(name != currentName){
ssCex << " & " << name << " != " << currentName;
}
}
return ssCex.str();
}

Expand Down
8 changes: 4 additions & 4 deletions server/src/printers/KleeConstraintsPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ namespace printer {

utbot::Language getLanguage() const override;

Stream genConstraints(const std::string &name, const types::Type& type);
Stream genConstraints(const std::string &name, const types::Type& type, const std::vector<std::string>& names={});

Stream genConstraints(const Tests::MethodParam &param);
Stream genConstraints(const Tests::MethodParam &param, const std::vector<std::string>& names={});

void setTabsDepth(const size_t depth) {
tabsDepth = depth;
Expand All @@ -31,7 +31,7 @@ namespace printer {
int depth = 0;
};

void genConstraintsForPrimitive(const ConstraintsState &state);
void genConstraintsForPrimitive(const ConstraintsState &state, const std::vector<std::string>& names={});

void genConstraintsForPointerOrArray(const ConstraintsState &state);

Expand All @@ -44,7 +44,7 @@ namespace printer {

void genConstraintsForPointerInStruct(const ConstraintsState &state);

static std::string cexConstraints(const std::string &name, const types::Type &type);
static std::string cexConstraints(const std::string &name, const types::Type &type, const std::vector<std::string>& names={});

void noConstraints(const std::string &cause);
};
Expand Down
18 changes: 13 additions & 5 deletions server/src/printers/KleePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Paths.h"
#include "exceptions/NoSuchTypeException.h"
#include "exceptions/UnImplementedException.h"
#include "testgens/BaseTestGen.h"
#include "utils/CollectionUtils.h"
#include "utils/FileSystemUtils.h"
#include "utils/KleeUtils.h"
Expand All @@ -29,8 +30,9 @@ static const std::string CALLOC_DECLARATION = "extern\n"

printer::KleePrinter::KleePrinter(const types::TypesHandler *typesHandler,
std::shared_ptr<BuildDatabase> buildDatabase,
utbot::Language srcLanguage)
: Printer(srcLanguage), typesHandler(typesHandler), buildDatabase(std::move(buildDatabase)) {
utbot::Language srcLanguage,
const BaseTestGen *testGen)
: Printer(srcLanguage), typesHandler(typesHandler), buildDatabase(std::move(buildDatabase)), testGen(testGen) {
}

void KleePrinter::writePosixWrapper(const Tests &tests,
Expand Down Expand Up @@ -376,7 +378,11 @@ void KleePrinter::genParamsDeclarations(
.str();
ss << constraintsBlock;
}
std::unordered_map<std::string , std::vector<std::string>>typesToNames;
for (const auto &param : testMethod.params) {
if(testGen->settingsContext.differentVariablesOfTheSameType){
typesToNames[param.type.typeName()].push_back(param.name);
}
if (!filter(param)) {
continue;
}
Expand All @@ -388,7 +394,9 @@ void KleePrinter::genParamsDeclarations(
auto paramType =
kleeParam.type.maybeJustPointer() ? kleeParam.type.baseTypeObj() : kleeParam.type;
strKleeMakeSymbolic(paramType, kleeParam.name, param.name, !isArray);
genConstraints(kleeParam, testMethod.name);
if(testGen->settingsContext.differentVariablesOfTheSameType && typesToNames[param.type.typeName()].size() <= 3){
genConstraints(kleeParam, testMethod.name, typesToNames[param.type.typeName()]);}
else {genConstraints(kleeParam, testMethod.name);}
genTwoDimPointers(param, true);
commentBlockSeparator();
}
Expand Down Expand Up @@ -479,10 +487,10 @@ void KleePrinter::genParamsKleeAssumes(
}
}

void KleePrinter::genConstraints(const Tests::MethodParam &param, const std::string &methodName) {
void KleePrinter::genConstraints(const Tests::MethodParam &param, const std::string &methodName, const std::vector<std::string>& names) {
KleeConstraintsPrinter constraintsPrinter(typesHandler, srcLanguage);
constraintsPrinter.setTabsDepth(tabsDepth);
const auto constraintsBlock = constraintsPrinter.genConstraints(param).str();
const auto constraintsBlock = constraintsPrinter.genConstraints(param, names).str();
ss << constraintsBlock;
}

Expand Down
6 changes: 4 additions & 2 deletions server/src/printers/KleePrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "LineInfo.h"
#include "building/BuildDatabase.h"
#include "types/Types.h"
#include "testgens/BaseTestGen.h"
#include "utils/path/FileSystemPath.h"

#include <cstdio>
Expand All @@ -24,7 +25,7 @@ namespace printer {
public:
KleePrinter(const types::TypesHandler *typesHandler,
std::shared_ptr<BuildDatabase> buildDatabase,
utbot::Language srcLanguage);
utbot::Language srcLanguage, const BaseTestGen *testGen);

utbot::Language getLanguage() const override;

Expand All @@ -46,6 +47,7 @@ namespace printer {
[[nodiscard]] std::vector<std::string> getIncludePaths(const Tests &tests, const PathSubstitution &substitution) const;
private:
types::TypesHandler const *typesHandler;
BaseTestGen const *testGen;
std::shared_ptr<BuildDatabase> buildDatabase;

using PredInfo = LineInfo::PredicateInfo;
Expand Down Expand Up @@ -85,7 +87,7 @@ namespace printer {
/*
* Functions for constraints generation.
*/
void genConstraints(const Tests::MethodParam &param, const std::string& methodName = "");
void genConstraints(const Tests::MethodParam &param, const std::string& methodName = "", const std::vector<std::string>& names = {});

void genTwoDimPointers(const Tests::MethodParam &param, bool needDeclare);

Expand Down
3 changes: 2 additions & 1 deletion server/src/utils/CLIUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ createSettingsContextByOptions(const SettingsContextOptionGroup &settingsContext
settingsContextOptionGroup.getTimeoutPerTest(),
settingsContextOptionGroup.isDeterministicSearcherUsed(),
settingsContextOptionGroup.withStubs(),
settingsContextOptionGroup.getErrorMode());
settingsContextOptionGroup.getErrorMode(),
settingsContextOptionGroup.doDifferentVariablesOfTheSameType());
}

std::vector<fs::path> getSourcePaths(const ProjectContextOptionGroup &projectContextOptions,
Expand Down
4 changes: 3 additions & 1 deletion server/src/utils/GrpcUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ namespace GrpcUtils {
int32_t timeoutPerTest,
bool useDeterministicSearcher,
bool useStubs,
ErrorMode errorMode) {
ErrorMode errorMode,
bool differentVariablesOfTheSameType) {
auto result = std::make_unique<testsgen::SettingsContext>();
result->set_generateforstaticfunctions(generateForStaticFunctions);
result->set_verbose(verbose);
Expand All @@ -45,6 +46,7 @@ namespace GrpcUtils {
result->set_usedeterministicsearcher(useDeterministicSearcher);
result->set_usestubs(useStubs);
result->set_errormode(errorMode);
result->set_differentvariablesofthesametype(differentVariablesOfTheSameType);
return result;
}

Expand Down
3 changes: 2 additions & 1 deletion server/src/utils/GrpcUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ namespace GrpcUtils {
int32_t timeoutPerTest,
bool useDeterministicSearcher,
bool useStubs,
ErrorMode errorMode);
ErrorMode errorMode,
bool differentVariablesOfTheSameType);

std::unique_ptr<testsgen::SnippetRequest>
createSnippetRequest(std::unique_ptr<testsgen::ProjectContext> projectContext,
Expand Down
Loading