Skip to content

Add necessary const qualifier in generated tests #622

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 3 commits into from
Sep 4, 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
9 changes: 6 additions & 3 deletions server/src/Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,8 @@ void KTestObjectParser::assignAllLazyPointers(
testCase.lazyReferences.emplace_back(
fromPtr.varName, toPtrName,
PrinterUtils::initializePointerToVar(fromPtr.type.baseType(), toPtrName,
fromPtr.type.getDimension()));
fromPtr.type.getDimension(),
fromPtr.type.isConstQualifiedValue()));
}
}
}
Expand Down Expand Up @@ -1233,13 +1234,15 @@ KTestObjectParser::getLazyPointerView(const std::vector<UTBotKTestObject> &objec
initReferences.emplace_back(
name, ptr_element->name,
PrinterUtils::initializePointerToVar(paramType.baseType(), ptr_element->name,
paramType.getDimension()));
paramType.getDimension(),
paramType.isConstQualifiedValue()));
}
if (lazyPointer || ptr_element != objects.end()) {
res = PrinterUtils::C_NULL;
}
return std::make_shared<JustValueView>(
PrinterUtils::initializePointer(paramType.baseType(), res, paramType.getDimension()));
PrinterUtils::initializePointer(paramType.baseType(), res, paramType.getDimension(),
paramType.isConstQualifiedValue()));
}

bool Tests::MethodDescription::operator==(const Tests::MethodDescription &other) const {
Expand Down
2 changes: 1 addition & 1 deletion server/src/Tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ namespace tests {

[[nodiscard]] std::string getFunctionParamDecl() const {
if (type.isTwoDimensionalPointer() && types::TypesHandler::isVoid(type.baseTypeObj())) {
std::string qualifier = type.isConstQualified() ? "const " : "";
std::string qualifier = PrinterUtils::getConstQualifier(type.isConstQualifiedValue());
return StringUtils::stringFormat("(%svoid **) %s", qualifier, name);
} else if (type.isRValueReference()) {
return "std::move(" + name + ")";
Expand Down
18 changes: 13 additions & 5 deletions server/src/utils/PrinterUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace PrinterUtils {
}

std::string getterDecl(const std::string &returnTypeName,
const std::string &wrapperName) {
const std::string &wrapperName) {
std::string gName = getterName(wrapperName);
return StringUtils::stringFormat("%s %s()", returnTypeName, gName);
}
Expand All @@ -46,6 +46,10 @@ namespace PrinterUtils {
return StringUtils::stringFormat("access_private::%s(%s)", fieldName, objectName);
}

std::string getConstQualifier(bool constQualifiedValue) {
return constQualifiedValue ? "const " : "";
}

std::string fillVarName(std::string const &access, std::string const &varName) {
return StringUtils::stringFormat(access, varName);
}
Expand All @@ -66,21 +70,25 @@ namespace PrinterUtils {

std::string initializePointer(const std::string &type,
const std::string &value,
size_t additionalPointersCount) {
size_t additionalPointersCount,
bool pointerToConstQualifiedValue) {
if (value == C_NULL || std::stoull(value) == 0) {
return C_NULL;
} else {
std::string additionalPointers = StringUtils::repeat("*", additionalPointersCount);
return StringUtils::stringFormat("(%s%s) 0x%lx", type, additionalPointers,
std::string qualifier = getConstQualifier(pointerToConstQualifiedValue);
return StringUtils::stringFormat("(%s%s%s) 0x%lx", qualifier, type, additionalPointers,
std::stoull(value));
}
}

std::string initializePointerToVar(const std::string &type,
const std::string &varName,
size_t additionalPointersCount) {
size_t additionalPointersCount,
bool pointerToConstQualifiedValue) {
std::string additionalPointers = StringUtils::repeat("*", additionalPointersCount);
return StringUtils::stringFormat("(%s%s) &%s", type, additionalPointers, varName);
std::string qualifier = getConstQualifier(pointerToConstQualifiedValue);
return StringUtils::stringFormat("(%s%s%s) &%s", qualifier, type, additionalPointers, varName);
}

std::string generateNewVar(int cnt) {
Expand Down
8 changes: 6 additions & 2 deletions server/src/utils/PrinterUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ namespace PrinterUtils {

std::string getFieldAccess(const std::string &objectName, const types::Field &field);

std::string getConstQualifier(bool constQualifiedValue);

std::string fillVarName(std::string const &temp, std::string const &varName);

void appendIndicesToVarName(std::string &varName, const std::vector<size_t> &sizes, size_t offset);
Expand All @@ -95,11 +97,13 @@ namespace PrinterUtils {

std::string initializePointer(const std::string &type,
const std::string &value,
size_t additionalPointersCount);
size_t additionalPointersCount,
bool pointerToConstQualifiedValue);

std::string initializePointerToVar(const std::string &type,
const std::string &varName,
size_t additionalPointersCount);
size_t additionalPointersCount,
bool pointerToConstQualifiedValue);

std::string generateNewVar(int cnt);

Expand Down
3 changes: 3 additions & 0 deletions server/test/suites/server/multi_dim_pointers.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ int func_with_multi_dim_pointer(struct MainStruct **str) {
struct MainStruct *ptr = *str;
int sz = 0;
if (ptr) {
if (!ptr->name) {
return -1;
}
struct ElementStruct *e = ptr->list.head;
struct ElementStruct *n;
for (int i = 0; i < 5; i++) {
Expand Down
1 change: 1 addition & 0 deletions server/test/suites/server/multi_dim_pointers.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct ListStruct {

struct MainStruct {
struct ListStruct list;
const char **name;
};

int func_with_multi_dim_pointer(struct MainStruct **str);
Expand Down