Skip to content

Commit

Permalink
ctypesparser: borland ast to ctypes parser can parse named types
Browse files Browse the repository at this point in the history
  • Loading branch information
xvenge00 committed Mar 12, 2019
1 parent 72b935b commit a3657e7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
2 changes: 2 additions & 0 deletions include/retdec/ctypesparser/borland_ast_ctypes_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "retdec/ctypes/function.h"
#include "retdec/ctypes/type.h"
#include "retdec/ctypes/integral_type.h"
#include "retdec/ctypes/named_type.h"
#include "retdec/ctypes/pointer_type.h"
#include "retdec/ctypes/floating_point_type.h"
#include "retdec/ctypesparser/ctypes_parser.h"
Expand Down Expand Up @@ -64,6 +65,7 @@ class BorlandToCtypesParser: public CTypesParser
std::shared_ptr<ctypes::PointerType> parsePointerType(std::shared_ptr<demangler::borland::PointerTypeNode> pointerNode);
std::shared_ptr<ctypes::Type> parseReferenceType(std::shared_ptr<demangler::borland::ReferenceTypeNode> referenceNode);
std::shared_ptr<ctypes::Type> parseRReferenceType(std::shared_ptr<demangler::borland::RReferenceTypeNode> referenceNode);
std::shared_ptr<ctypes::NamedType> parseNamedType(std::shared_ptr<demangler::borland::NamedTypeNode> namedTypeNode);
ctypes::Function::Parameters parseFuncParameters(std::shared_ptr<demangler::borland::NodeArray> paramsNode);
ctypes::CallConvention parseCallConvention(demangler::borland::CallConv callConv);
ctypes::FunctionType::VarArgness parseVarArgness(bool isVarArg);
Expand Down
11 changes: 11 additions & 0 deletions src/ctypesparser/borland_ast_ctypes_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ std::shared_ptr<ctypes::Type> BorlandToCtypesParser::parseType(std::shared_ptr<r
parseRReferenceType(std::static_pointer_cast<demangler::borland::RReferenceTypeNode>(typeNode));
return std::static_pointer_cast<ctypes::Type>(referenceType);
}
case Kind::KNamedType: {
auto namedType = parseNamedType(std::static_pointer_cast<demangler::borland::NamedTypeNode>(typeNode));
return std::static_pointer_cast<ctypes::Type>(namedType);
}
default:
break;
}
Expand Down Expand Up @@ -208,6 +212,13 @@ std::shared_ptr<ctypes::Type> BorlandToCtypesParser::parseRReferenceType(
return std::static_pointer_cast<ctypes::Type>(ctypes::UnknownType::create()); // TODO rreference
}

std::shared_ptr<ctypes::NamedType> BorlandToCtypesParser::parseNamedType(
std::shared_ptr<retdec::demangler::borland::NamedTypeNode> namedTypeNode)
{
std::string name = namedTypeNode->name()->str();
return ctypes::NamedType::create(context, name);
}

ctypes::Function::Parameters BorlandToCtypesParser::parseFuncParameters(
std::shared_ptr<retdec::demangler::borland::NodeArray> paramsNode)
{
Expand Down
44 changes: 39 additions & 5 deletions tests/ctypesparser/borland_ast_to_ctypes_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
#include <gtest/gtest.h>

#include "retdec/demangler/demangler.h"
#include "retdec/ctypes/module.h"
#include "retdec/demangler/context.h"
#include "retdec/ctypes/module.h"
#include "retdec/ctypes/context.h"
#include "retdec/ctypes/function.h"
#include "retdec/ctypes/parameter.h"
#include "retdec/ctypes/unknown_type.h"

using namespace ::testing;

Expand All @@ -23,16 +26,47 @@ class BorlandCtypesTests : public Test
using status = retdec::demangler::Demangler::Status;

BorlandCtypesTests() :
demangler(retdec::demangler::DemanglerFactory::getDemangler("borland")) {}
demangler(retdec::demangler::DemanglerFactory::getDemangler("borland")),
context(std::make_shared<retdec::ctypes::Context>()),
module(std::make_unique<ctypes::Module>(context)) {}
protected:
void mangledToCtypes(
const std::string &mangled)
{
demangler->demangleToModule(mangled, module);
}

std::unique_ptr<retdec::demangler::Demangler> demangler;
std::shared_ptr<retdec::ctypes::Context> context;
std::unique_ptr<retdec::ctypes::Module> module;
};

TEST_F(BorlandCtypesTests, basic)
{
auto context = std::make_shared<retdec::ctypes::Context>();
auto module = std::make_unique<ctypes::Module>(context);
demangler->demangleToModule("@myFunc_int_$qi", module);
mangledToCtypes("@myFunc_int_$qi");

EXPECT_TRUE(module->hasFunctionWithName("myFunc_int_"));

auto func = module->getFunctionWithName("myFunc_int_");
EXPECT_TRUE(func->getReturnType()->isUnknown());

EXPECT_EQ(func->getParameterCount(), 1);
EXPECT_FALSE(func->isVarArg());
EXPECT_TRUE(func->getParameter(1).getType()->isIntegral());
}

TEST_F(BorlandCtypesTests, templateTypes)
{
mangledToCtypes("@%foo$60std@%basic_string$c19std@%char_traits$c%17std@%allocator$c%%%$q60std@%basic_string$c19std@%char_traits$c%17std@%allocator$c%%$v");

EXPECT_TRUE(module->hasFunctionWithName("foo<std::basic_string<char, std::char_traits<char>, std::allocator<char>>>"));

auto func = module->getFunctionWithName("foo<std::basic_string<char, std::char_traits<char>, std::allocator<char>>>");
EXPECT_TRUE(func->getReturnType()->isVoid());

EXPECT_EQ(func->getParameterCount(), 1);
EXPECT_FALSE(func->isVarArg());
EXPECT_TRUE(func->getParameter(1).getType()->isNamed()); // TODO
}

}
Expand Down

0 comments on commit a3657e7

Please sign in to comment.