Skip to content

Commit 10c065d

Browse files
authored
Adding support for multiple variables of the same type to a plugin for vscode (#585)
1 parent 2f6f07d commit 10c065d

27 files changed

+258
-47
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "different_variables.h"
2+
3+
void swap_two_int_pointers(int *a, int *b) {
4+
int t = *a;
5+
*a = *b;
6+
*b = t;
7+
}
8+
9+
float max_of_two_float(float a, float b) {
10+
if (a > b)
11+
return a;
12+
return b;
13+
}
14+
15+
int struct_test(struct easy_str a, struct easy_str b) {
16+
if (a.a == b.a) {
17+
return 1;
18+
}
19+
return -1;
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef SIMPLE_TEST_PROJECT_DIFFERENT_VARIABLES_H
2+
#define SIMPLE_TEST_PROJECT_DIFFERENT_VARIABLES_H
3+
4+
struct easy_str {
5+
int a;
6+
char b;
7+
};
8+
9+
void swap_two_int_pointers(int *a, int *b);
10+
11+
float max_of_two_float(float a, float b);
12+
13+
int struct_test(struct easy_str a, struct easy_str b);
14+
15+
#endif // SIMPLE_TEST_PROJECT_DIFFERENT_VARIABLES_H

server/proto/testgen.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ message SettingsContext {
105105
bool useDeterministicSearcher = 5;
106106
bool useStubs = 6;
107107
ErrorMode errorMode = 7;
108+
bool differentVariablesOfTheSameType = 8;
108109
}
109110

110111
message SnippetRequest {

server/src/KleeGenerator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ std::vector<fs::path> KleeGenerator::buildKleeFiles(const tests::TestsMap &tests
244244
const std::shared_ptr<LineInfo> &lineInfo) {
245245
std::vector<fs::path> outFiles;
246246
LOG_S(DEBUG) << "Building generated klee files...";
247-
printer::KleePrinter kleePrinter(&typesHandler, testGen->getTargetBuildDatabase(), utbot::Language::UNKNOWN);
247+
printer::KleePrinter kleePrinter(&typesHandler, testGen->getTargetBuildDatabase(), utbot::Language::UNKNOWN, testGen);
248248
ExecUtils::doWorkWithProgress(
249249
testsMap, testGen->progressWriter, "Building generated klee files",
250250
[&](auto const &it) {

server/src/Server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ Status Server::TestsGenServiceImpl::ProcessBaseTestRequest(BaseTestGen &testGen,
249249
if (lineTestGen->needToAddPathFlag()) {
250250
LOG_S(DEBUG) << "Added test line flag for file " << lineInfo->filePath;
251251
fs::path flagFilePath =
252-
printer::KleePrinter(&typesHandler, nullptr, Paths::getSourceLanguage(lineInfo->filePath))
252+
printer::KleePrinter(&typesHandler, nullptr, Paths::getSourceLanguage(lineInfo->filePath), &testGen)
253253
.addTestLineFlag(lineInfo, lineInfo->forAssert, testGen.projectContext);
254254
pathSubstitution = {lineTestGen->filePath, flagFilePath};
255255
}

server/src/SettingsContext.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ namespace utbot {
99
int32_t timeoutPerTest,
1010
bool useDeterministicSearcher,
1111
bool useStubs,
12-
testsgen::ErrorMode errorMode)
12+
testsgen::ErrorMode errorMode,
13+
bool differentVariablesOfTheSameType)
1314
: generateForStaticFunctions(generateForStaticFunctions),
1415
verbose(verbose),
1516
timeoutPerFunction(timeoutPerFunction > 0
@@ -19,7 +20,8 @@ namespace utbot {
1920
? std::make_optional(std::chrono::seconds{ timeoutPerTest })
2021
: std::nullopt),
2122
useDeterministicSearcher(useDeterministicSearcher), useStubs(useStubs),
22-
errorMode(errorMode) {
23+
errorMode(errorMode),
24+
differentVariablesOfTheSameType (differentVariablesOfTheSameType) {
2325
}
2426
SettingsContext::SettingsContext(const testsgen::SettingsContext &settingsContext)
2527
: SettingsContext(settingsContext.generateforstaticfunctions(),
@@ -28,6 +30,7 @@ namespace utbot {
2830
settingsContext.timeoutpertest(),
2931
settingsContext.usedeterministicsearcher(),
3032
settingsContext.usestubs(),
31-
settingsContext.errormode()) {
33+
settingsContext.errormode(),
34+
settingsContext.differentvariablesofthesametype()) {
3235
}
3336
}

server/src/SettingsContext.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@ namespace utbot {
2020
int32_t timeoutPerTest,
2121
bool useDeterministicSearcher,
2222
bool useStubs,
23-
testsgen::ErrorMode errorMode);
23+
testsgen::ErrorMode errorMode,
24+
bool differentVariablesOfTheSameType);
2425

2526
const bool generateForStaticFunctions;
2627
const bool verbose;
2728
const std::optional<std::chrono::seconds> timeoutPerFunction, timeoutPerTest;
2829
const bool useDeterministicSearcher;
2930
const bool useStubs;
3031
testsgen::ErrorMode errorMode;
32+
const bool differentVariablesOfTheSameType;
3133
};
3234
}
3335

server/src/commands/Commands.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,10 @@ ErrorMode Commands::SettingsContextOptionGroup::getErrorMode() const {
412412
return errorMode;
413413
}
414414

415+
bool Commands::SettingsContextOptionGroup::doDifferentVariablesOfTheSameType() const {
416+
return differentVariablesOfTheSameType;
417+
}
418+
415419
Commands::RunTestsCommands::RunTestsCommands(Commands::MainCommands &commands) {
416420
runCommand = commands.getRunTestsCommand();
417421

server/src/commands/Commands.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ namespace Commands {
245245

246246
[[nodiscard]] ErrorMode getErrorMode() const;
247247

248+
[[nodiscard]] bool doDifferentVariablesOfTheSameType() const;
249+
248250
private:
249251
CLI::Option_group *settingsContextOptions;
250252
bool generateForStaticFunctions = true;
@@ -254,6 +256,7 @@ namespace Commands {
254256
bool noDeterministicSearcher = false;
255257
bool noStubs = false;
256258
ErrorMode errorMode = ErrorMode::FAILING;
259+
bool differentVariablesOfTheSameType = false;
257260
};
258261
};
259262

server/src/printers/KleeConstraintsPrinter.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ printer::KleeConstraintsPrinter::KleeConstraintsPrinter(const types::TypesHandle
1515
: Printer(srcLanguage), typesHandler(typesHandler) {}
1616

1717
printer::KleeConstraintsPrinter::Stream
18-
KleeConstraintsPrinter::genConstraints(const std::string &name, const types::Type& type) {
18+
KleeConstraintsPrinter::genConstraints(const std::string &name, const types::Type& type, const std::vector<std::string>& names) {
1919
ConstraintsState state = { "&" + name, name, type, true };
2020
auto paramType = type;
2121
if (type.maybeJustPointer()) {
@@ -34,19 +34,19 @@ KleeConstraintsPrinter::genConstraints(const std::string &name, const types::Typ
3434
genConstraintsForEnum(state);
3535
break;
3636
default:
37-
genConstraintsForPrimitive(state);
37+
genConstraintsForPrimitive(state, names);
3838
}
3939

4040
return ss;
4141
}
4242

4343
printer::KleeConstraintsPrinter::Stream
44-
KleeConstraintsPrinter::genConstraints(const Tests::MethodParam &param) {
45-
return genConstraints(param.name, param.type);
44+
KleeConstraintsPrinter::genConstraints(const Tests::MethodParam &param, const std::vector<std::string>& names) {
45+
return genConstraints(param.name, param.type, names);
4646
}
4747

48-
void KleeConstraintsPrinter::genConstraintsForPrimitive(const ConstraintsState &state) {
49-
const auto &cons = cexConstraints(state.curElement, state.curType);
48+
void KleeConstraintsPrinter::genConstraintsForPrimitive(const ConstraintsState &state, const std::vector<std::string>& names) {
49+
const auto &cons = cexConstraints(state.curElement, state.curType, names);
5050
if (!cons.empty()) {
5151
strFunctionCall(PrinterUtils::KLEE_PREFER_CEX, { state.paramName, cons });
5252
} else {
@@ -164,7 +164,7 @@ void KleeConstraintsPrinter::genConstraintsForStruct(const ConstraintsState &sta
164164
}
165165
}
166166

167-
std::string KleeConstraintsPrinter::cexConstraints(const std::string &name, const types::Type &type) {
167+
std::string KleeConstraintsPrinter::cexConstraints(const std::string &name, const types::Type &type, const std::vector<std::string>& names) {
168168
if (!CollectionUtils::containsKey(TypesHandler::preferredConstraints(), type.baseType())) {
169169
return "";
170170
}
@@ -176,6 +176,11 @@ std::string KleeConstraintsPrinter::cexConstraints(const std::string &name, cons
176176
ssCex << " & ";
177177
}
178178
}
179+
for (const std::string& currentName: names){
180+
if(name != currentName){
181+
ssCex << " & " << name << " != " << currentName;
182+
}
183+
}
179184
return ssCex.str();
180185
}
181186

0 commit comments

Comments
 (0)