From 83a81ec65a9cb5f51cd1070af82ce907aedd75b2 Mon Sep 17 00:00:00 2001 From: IDKWNTCMF <71064019+IDKWNTCMF@users.noreply.github.com> Date: Tue, 18 Jul 2023 15:28:55 +0300 Subject: [PATCH] Resolve the issue with unnecessary enum specifier #600 (#602) * Resolve issue with unnecessary enum specifier --- server/src/Tests.cpp | 3 ++- server/src/types/Types.h | 2 ++ server/src/types/TypesResolver.cpp | 5 ++++ server/test/framework/Regression_Tests.cpp | 26 ++++++++++++++++++++ server/test/suites/regression/CMakeLists.txt | 1 + server/test/suites/regression/issue-600.c | 26 ++++++++++++++++++++ 6 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 server/test/suites/regression/issue-600.c diff --git a/server/src/Tests.cpp b/server/src/Tests.cpp index 0bcdaca05..2e692e9eb 100644 --- a/server/src/Tests.cpp +++ b/server/src/Tests.cpp @@ -121,7 +121,8 @@ std::shared_ptr KTestObjectParser::enumView(const std::vector(value); } diff --git a/server/src/types/Types.h b/server/src/types/Types.h index 8ebcf6409..634d2db8f 100644 --- a/server/src/types/Types.h +++ b/server/src/types/Types.h @@ -327,6 +327,8 @@ namespace types { std::optional access; + bool isSpecifierNeeded = true; + std::string getEntryName(std::string const& value, utbot::Language language) const; }; diff --git a/server/src/types/TypesResolver.cpp b/server/src/types/TypesResolver.cpp index 8b7e1b64b..79d4049cf 100644 --- a/server/src/types/TypesResolver.cpp +++ b/server/src/types/TypesResolver.cpp @@ -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 getAccess(const clang::Decl *decl) { const clang::DeclContext *pContext = decl->getDeclContext(); std::vector result; @@ -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; diff --git a/server/test/framework/Regression_Tests.cpp b/server/test/framework/Regression_Tests.cpp index 9090add43..7bb7cc5ae 100644 --- a/server/test/framework/Regression_Tests.cpp +++ b/server/test/framework/Regression_Tests.cpp @@ -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( + {[](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" + ); + } + } diff --git a/server/test/suites/regression/CMakeLists.txt b/server/test/suites/regression/CMakeLists.txt index 3cab09150..e1df728ea 100644 --- a/server/test/suites/regression/CMakeLists.txt +++ b/server/test/suites/regression/CMakeLists.txt @@ -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) diff --git a/server/test/suites/regression/issue-600.c b/server/test/suites/regression/issue-600.c new file mode 100644 index 000000000..f6f172188 --- /dev/null +++ b/server/test/suites/regression/issue-600.c @@ -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; +} \ No newline at end of file