Skip to content

Commit 83a81ec

Browse files
authored
Resolve the issue with unnecessary enum specifier #600 (#602)
* Resolve issue with unnecessary enum specifier
1 parent e052321 commit 83a81ec

File tree

6 files changed

+62
-1
lines changed

6 files changed

+62
-1
lines changed

server/src/Tests.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ std::shared_ptr<EnumValueView> KTestObjectParser::enumView(const std::vector<cha
121121
value = NameDecorator::decorate(name);
122122
} else {
123123
LOG_S(WARNING) << "Enum value for '" << enumInfo.name << "' is out of range: " << value;
124-
value = StringUtils::stringFormat("(enum %s)(%d)", enumInfo.name, value);
124+
std::string format = enumInfo.isSpecifierNeeded ? "(enum %s)(%d)" : "(%s) %d";
125+
value = StringUtils::stringFormat(format, enumInfo.name, value);
125126
}
126127
return std::make_shared<EnumValueView>(value);
127128
}

server/src/types/Types.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,8 @@ namespace types {
327327

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

330+
bool isSpecifierNeeded = true;
331+
330332
std::string getEntryName(std::string const& value, utbot::Language language) const;
331333
};
332334

server/src/types/TypesResolver.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ void TypesResolver::resolveStructEx(const clang::RecordDecl *D, const std::strin
194194
LOG_S(DEBUG) << ss.str();
195195
}
196196

197+
bool isSpecifierNeeded(const clang::TagDecl *tagDecl) {
198+
return tagDecl->getTypedefNameForAnonDecl() == nullptr;
199+
}
200+
197201
static std::optional<std::string> getAccess(const clang::Decl *decl) {
198202
const clang::DeclContext *pContext = decl->getDeclContext();
199203
std::vector<std::string> result;
@@ -233,6 +237,7 @@ void TypesResolver::resolveEnum(const clang::EnumDecl *EN, const std::string &na
233237
enumInfo.size = context.getTypeSize(promotionType);
234238

235239
enumInfo.access = getAccess(EN);
240+
enumInfo.isSpecifierNeeded = isSpecifierNeeded(EN);
236241

237242
for (auto it = EN->enumerator_begin(); it != EN->enumerator_end(); ++it) {
238243
types::EnumInfo::EnumEntry enumEntry;

server/test/framework/Regression_Tests.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,4 +375,30 @@ namespace {
375375
testUtils::checkMinNumberOfTests(testGen.tests, 2);
376376
}
377377

378+
TEST_F(Regression_Test, Unnecessary_Enum_Specifier) {
379+
fs::path source = getTestFilePath("issue-600.c");
380+
auto [testGen, status] = createTestForFunction(source, 14);
381+
382+
ASSERT_TRUE(status.ok()) << status.error_message();
383+
384+
checkTestCasePredicates(
385+
testGen.tests.at(source).methods.begin().value().testCases,
386+
std::vector<TestCasePredicate>(
387+
{[](const tests::Tests::MethodTestCase &testCase) {
388+
return testCase.returnValue.view->getEntryValue(nullptr) == "-2";
389+
},
390+
[](const tests::Tests::MethodTestCase &testCase) {
391+
return testCase.returnValue.view->getEntryValue(nullptr) == "-1";
392+
},
393+
[](const tests::Tests::MethodTestCase &testCase) {
394+
return testCase.returnValue.view->getEntryValue(nullptr) == "0";
395+
},
396+
[](const tests::Tests::MethodTestCase &testCase) {
397+
return testCase.returnValue.view->getEntryValue(nullptr) == "1";
398+
}
399+
}),
400+
"isCorrectPointerStruct"
401+
);
402+
}
403+
378404
}

server/test/suites/regression/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,6 @@ target_compile_definitions(issue-276 PUBLIC EXPORT4="4")
4949

5050
add_library(issue-195 issue-195.c)
5151
add_library(issue-514 issue-514.c)
52+
add_library(issue-600 issue-600.c)
5253

5354
set_target_properties(regression PROPERTIES LINK_WHAT_YOU_USE TRUE)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
typedef enum {
2+
EVEN,
3+
ODD
4+
} Parity;
5+
6+
struct WrapperStruct {
7+
Parity p;
8+
};
9+
10+
struct PointerStruct {
11+
struct WrapperStruct* wrapperStruct;
12+
};
13+
14+
int isCorrectPointerStruct(struct PointerStruct* s) {
15+
if (!s || !s->wrapperStruct) {
16+
return -1;
17+
}
18+
Parity parity = s->wrapperStruct->p;
19+
if (parity == EVEN) {
20+
return 0;
21+
}
22+
if (parity == ODD) {
23+
return 1;
24+
}
25+
return -2;
26+
}

0 commit comments

Comments
 (0)