Skip to content

Resolve the issue with unnecessary enum specifier #600 #602

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
Jul 18, 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
3 changes: 2 additions & 1 deletion server/src/Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ std::shared_ptr<EnumValueView> KTestObjectParser::enumView(const std::vector<cha
value = NameDecorator::decorate(name);
} else {
LOG_S(WARNING) << "Enum value for '" << enumInfo.name << "' is out of range: " << value;
value = StringUtils::stringFormat("(enum %s)(%d)", enumInfo.name, value);
std::string format = enumInfo.isSpecifierNeeded ? "(enum %s)(%d)" : "(%s) %d";
value = StringUtils::stringFormat(format, enumInfo.name, value);
}
return std::make_shared<EnumValueView>(value);
}
Expand Down
2 changes: 2 additions & 0 deletions server/src/types/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ namespace types {

std::optional<std::string> access;

bool isSpecifierNeeded = true;

std::string getEntryName(std::string const& value, utbot::Language language) const;
};

Expand Down
5 changes: 5 additions & 0 deletions server/src/types/TypesResolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ void TypesResolver::resolveStructEx(const clang::RecordDecl *D, const std::strin
LOG_S(DEBUG) << ss.str();
}

bool isSpecifierNeeded(const clang::TagDecl *tagDecl) {
return tagDecl->getTypedefNameForAnonDecl() == nullptr;
}

static std::optional<std::string> getAccess(const clang::Decl *decl) {
const clang::DeclContext *pContext = decl->getDeclContext();
std::vector<std::string> result;
Expand Down Expand Up @@ -233,6 +237,7 @@ void TypesResolver::resolveEnum(const clang::EnumDecl *EN, const std::string &na
enumInfo.size = context.getTypeSize(promotionType);

enumInfo.access = getAccess(EN);
enumInfo.isSpecifierNeeded = isSpecifierNeeded(EN);

for (auto it = EN->enumerator_begin(); it != EN->enumerator_end(); ++it) {
types::EnumInfo::EnumEntry enumEntry;
Expand Down
26 changes: 26 additions & 0 deletions server/test/framework/Regression_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,30 @@ namespace {
testUtils::checkMinNumberOfTests(testGen.tests, 2);
}

TEST_F(Regression_Test, Unnecessary_Enum_Specifier) {
fs::path source = getTestFilePath("issue-600.c");
auto [testGen, status] = createTestForFunction(source, 14);

ASSERT_TRUE(status.ok()) << status.error_message();

checkTestCasePredicates(
testGen.tests.at(source).methods.begin().value().testCases,
std::vector<TestCasePredicate>(
{[](const tests::Tests::MethodTestCase &testCase) {
return testCase.returnValue.view->getEntryValue(nullptr) == "-2";
},
[](const tests::Tests::MethodTestCase &testCase) {
return testCase.returnValue.view->getEntryValue(nullptr) == "-1";
},
[](const tests::Tests::MethodTestCase &testCase) {
return testCase.returnValue.view->getEntryValue(nullptr) == "0";
},
[](const tests::Tests::MethodTestCase &testCase) {
return testCase.returnValue.view->getEntryValue(nullptr) == "1";
}
}),
"isCorrectPointerStruct"
);
}

}
1 change: 1 addition & 0 deletions server/test/suites/regression/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ target_compile_definitions(issue-276 PUBLIC EXPORT4="4")

add_library(issue-195 issue-195.c)
add_library(issue-514 issue-514.c)
add_library(issue-600 issue-600.c)

set_target_properties(regression PROPERTIES LINK_WHAT_YOU_USE TRUE)
26 changes: 26 additions & 0 deletions server/test/suites/regression/issue-600.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
typedef enum {
EVEN,
ODD
} Parity;

struct WrapperStruct {
Parity p;
};

struct PointerStruct {
struct WrapperStruct* wrapperStruct;
};

int isCorrectPointerStruct(struct PointerStruct* s) {
if (!s || !s->wrapperStruct) {
return -1;
}
Parity parity = s->wrapperStruct->p;
if (parity == EVEN) {
return 0;
}
if (parity == ODD) {
return 1;
}
return -2;
}