Skip to content
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

Fix issue with anonymous enums #599

Merged
merged 10 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 17 additions & 0 deletions server/src/clang-utils/SourceToHeaderMatchCallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ void SourceToHeaderMatchCallback::generateInternal(const FunctionDecl *decl) con
auto policy = getDefaultPrintingPolicy(decl, true);
policy.TerseOutput = 1;
policy.PolishForDeclaration = 1;
policy.AnonymousTagLocations = 0;

std::string name = decl->getNameAsString();
std::string decoratedName = decorate(name);
Expand All @@ -209,6 +210,17 @@ void SourceToHeaderMatchCallback::generateInternal(const FunctionDecl *decl) con
wrapperDecl = getOldStyleDeclarationAsString(decl, wrapperName);
}

auto returnType = decl->getReturnType();
if (returnType->hasUnnamedOrLocalType()) {
if (auto EN = llvm::dyn_cast<clang::EnumDecl>(returnType->getAsTagDecl())) {
kichunya marked this conversation as resolved.
Show resolved Hide resolved
std::string returnTypeName = name + "_return_type";
renameDecl(EN, returnTypeName);
print(EN);
StringUtils::replaceFirst(wrapperDecl, "(anonymous)", returnTypeName);
StringUtils::replaceFirst(curDecl, "(anonymous)", returnTypeName);
}
}

*internalStream << "extern \"C\" " << wrapperDecl << ";\n";
*internalStream << "static " << curDecl << " {\n";
printReturn(decl, wrapperName, internalStream);
Expand Down Expand Up @@ -251,6 +263,7 @@ void SourceToHeaderMatchCallback::generateWrapper(const FunctionDecl *decl) cons
auto policy = getDefaultPrintingPolicy(decl, false);
policy.TerseOutput = 1;
policy.PolishForDeclaration = 1;
policy.AnonymousTagLocations = 0;

/*
* fun_wrapper {
Expand All @@ -264,6 +277,10 @@ void SourceToHeaderMatchCallback::generateWrapper(const FunctionDecl *decl) cons
wrapperDecl = getOldStyleDeclarationAsString(decl, wrapperName);
}

auto returnType = decl->getReturnType();
if (returnType->hasUnnamedOrLocalType() && returnType->isEnumeralType()) {
StringUtils::replaceFirst(wrapperDecl, "enum (anonymous)", "int");
ladisgin marked this conversation as resolved.
Show resolved Hide resolved
}
*wrapperStream << wrapperDecl << " {\n";
printReturn(decl, name, wrapperStream);
*wrapperStream << "}\n";
Expand Down
2 changes: 2 additions & 0 deletions server/src/fetchers/FunctionDeclsMatchCallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ void FunctionDeclsMatchCallback::run(const MatchFinder::MatchResult &Result) {
}
clang::QualType realReturnType = ClangUtils::getReturnType(FS, Result);
methodDescription.returnType = ParamsHandler::getType(realReturnType, realReturnType, sourceManager);
methodDescription.returnType.replaceTypeNameIfUnnamed("enum " + methodName + "_return_type");
ladisgin marked this conversation as resolved.
Show resolved Hide resolved
methodDescription.accessSpecifier = types::AS_pubic;
if (onlyReturnTypes) {
addMethod(sourceFilePath, methodDescription);
Expand Down Expand Up @@ -72,6 +73,7 @@ void FunctionDeclsMatchCallback::run(const MatchFinder::MatchResult &Result) {
methodDescription.accessSpecifier = getAcessSpecifier(FS);
}
methodDescription.returnType = ParamsHandler::getType(realReturnType, realReturnType, sourceManager);
methodDescription.returnType.replaceTypeNameIfUnnamed("enum " + methodName + "_return_type");
ladisgin marked this conversation as resolved.
Show resolved Hide resolved
methodDescription.hasIncompleteReturnType = ClangUtils::isIncomplete(realReturnType);
if (toResolveReturnTypes) {
typesResolver.resolve(realReturnType);
Expand Down
5 changes: 4 additions & 1 deletion server/src/printers/KleePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,10 @@ void KleePrinter::genReturnDeclaration(const Tests::MethodDescription &testMetho
: testMethod.returnType;
bool maybeArray = returnType.maybeReturnArray();
bool isPointer = testMethod.returnType.isObjectPointer();
strDeclareVar(returnType.baseType(), KleeUtils::RESULT_VARIABLE_NAME, std::nullopt, std::nullopt, false);
std::string type = (returnType.isUnnamed() && typesHandler->isEnum(returnType))
? "int"
: returnType.baseType();
strDeclareVar(type, KleeUtils::RESULT_VARIABLE_NAME, std::nullopt, std::nullopt, false);
makeBracketsForStrPredicate(predicateInfo);
if (maybeArray) {
size_t size = types::TypesHandler::getElementsNumberInPointerOneDim(PointerUsage::RETURN);
Expand Down
7 changes: 7 additions & 0 deletions server/src/types/Types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,13 @@ void types::Type::replaceUsedType(const types::TypeName &newUsedType) {
mUsedType = newUsedType;
}

void types::Type::replaceTypeNameIfUnnamed(const TypeName &newTypeName) {
if (isUnnamed()) {
mBaseType = newTypeName;
mUsedType = newTypeName;
}
}

/*
* Integer types
*/
Expand Down
7 changes: 7 additions & 0 deletions server/src/types/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,13 @@ namespace types {
*/
void replaceUsedType(const TypeName &newUsedType);

/**
* Replace current baseType and usedType with a new one if unnamed.
* @param newTypeName - name of type
* @return
*/
void replaceTypeNameIfUnnamed(const TypeName &newTypeName);

/**
* Creates type from its name. Created type satisfies following:
* typeName() == "const " + type && baseType() == type && usedType() == typeName();
Expand Down
5 changes: 4 additions & 1 deletion server/src/visitors/KleeAssumeReturnValueVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ namespace visitor {
functionCall = printer->constrFunctionCall(methodDescription, 0, "", false);
additionalPointersCount = methodDescription.returnType.countReturnPointers();
auto returnType = methodDescription.returnType.baseTypeObj();
printer->strDeclareVar(getActualTmpVarType(returnType).baseType(),
std::string type = (returnType.isUnnamed() && typesHandler->isEnum(returnType))
ladisgin marked this conversation as resolved.
Show resolved Hide resolved
? "int"
: getActualTmpVarType(returnType).baseType();
printer->strDeclareVar(type,
KleeUtils::TEMP_VARIABLE_NAME, functionCall,
std::nullopt, true, additionalPointersCount);
checkNotNullBefore();
Expand Down