Skip to content

Commit

Permalink
Format fix and infinite recursion fix (21.10 22:04)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexlord committed Oct 21, 2020
1 parent 06c8a4c commit 31dc895
Show file tree
Hide file tree
Showing 30 changed files with 528 additions and 530 deletions.
4 changes: 3 additions & 1 deletion CMake/MetaPrebuild.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ macro(meta_generate PROJECT_NAME IN_SOURCE OUT_HEADER OUT_SOURCE ADDITIONAL_FLAG

set(CMD "${META_CPP_EXE} \"${IN_SOURCE_PATH}\" -out-header \"${OUT_HEADER_PATH}\" -out-source \"${OUT_SOURCE_PATH}\" ${FLAGS}")
separate_arguments(CMD)


# Useful for debugging
message("MetaCPP Command: ${CMD}")

add_custom_command(
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/MetaCPP"
Expand Down
8 changes: 4 additions & 4 deletions Example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
#include <MetaCPP/JsonSerializer.hpp>

int main() {
metacpp::Storage *storage = metacpp::Runtime::GetStorage();
metacpp::Storage* storage = metacpp::Runtime::GetStorage();
metacpp::generated::Load(storage);

Player *player = new Player();
Player* player = new Player();
player->health = 255;
player->position = {5, 5};
player->velocity = {1, 1};
player->name = "mlomb";

Monster *monster = new Monster();
Monster* monster = new Monster();
monster->health = 255;
monster->position = {10, 10};
monster->velocity = {-1, -1};
Expand All @@ -43,7 +43,7 @@ int main() {
std::cout << json << std::endl;

// deserialize
Map *deserialized_map = serializer.DeSerialize<Map>(json);
Map* deserialized_map = serializer.DeSerialize<Map>(json);

// serialize again and compare the jsons
if (serializer.Serialize(deserialized_map, true) == json) {
Expand Down
5 changes: 3 additions & 2 deletions Example/objects.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Player : public Entity {
public:
std::string name;

void Attack(Entity *entity) {
void Attack(Entity* entity) {
--entity->health;
}
};
Expand All @@ -37,6 +37,7 @@ class Map {
char magic_numbers[2][2];
std::vector<std::vector<int>> map;
//std::vector<Entity*> entities;
std::array<Entity *, 16> entities;
std::array<Entity*, 16> entities;
};

#pragma pack(pop)
57 changes: 28 additions & 29 deletions MetaCPP-CLI/ASTScraper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
#include "MetaCPP/Type.hpp"

namespace metacpp {
ASTScraper::ASTScraper(Storage *storage, const Configuration &config)
ASTScraper::ASTScraper(Storage* storage, const Configuration& config)
: m_Config(config), m_Storage(storage) {
}

void ASTScraper::ScrapeTranslationUnit(const clang::TranslationUnitDecl *tuDecl) {
void ASTScraper::ScrapeTranslationUnit(const clang::TranslationUnitDecl* tuDecl) {
ScrapeDeclContext(tuDecl, nullptr);
}

void ASTScraper::ScrapeDeclContext(const clang::DeclContext *ctxDecl, Type *parent) {
void ASTScraper::ScrapeDeclContext(const clang::DeclContext* ctxDecl, Type* parent) {
for (clang::DeclContext::decl_iterator it = ctxDecl->decls_begin(); it != ctxDecl->decls_end(); ++it) {
const clang::Decl *decl = *it;
const clang::Decl* decl = *it;
if (decl->isInvalidDecl())
continue;

Expand All @@ -31,7 +31,7 @@ namespace metacpp {
}
}

void ASTScraper::ScrapeNamedDecl(const clang::NamedDecl *namedDecl, Type *parent) {
void ASTScraper::ScrapeNamedDecl(const clang::NamedDecl* namedDecl, Type* parent) {
clang::Decl::Kind kind = namedDecl->getKind();
switch (kind) {
case clang::Decl::Kind::ClassTemplate:
Expand All @@ -56,25 +56,25 @@ namespace metacpp {
}
}

Type *ASTScraper::ScrapeCXXRecordDecl(const clang::CXXRecordDecl *cxxRecordDecl, Type *parent) {
Type* ASTScraper::ScrapeCXXRecordDecl(const clang::CXXRecordDecl* cxxRecordDecl, Type* parent) {
if (cxxRecordDecl->isAnonymousStructOrUnion()) {
ScrapeDeclContext(cxxRecordDecl, parent);
return 0;
}

const clang::Type *cType = cxxRecordDecl->getTypeForDecl();
metacpp::Type *type = ScrapeType(cType, 1);
const clang::Type* cType = cxxRecordDecl->getTypeForDecl();
metacpp::Type* type = ScrapeType(cType, 1);

if (type) {
const clang::CXXRecordDecl *typeCxxRecordDecl = cType->getAsCXXRecordDecl();
const clang::CXXRecordDecl* typeCxxRecordDecl = cType->getAsCXXRecordDecl();

type->SetAccess(TransformAccess(cxxRecordDecl->getAccess()));
type->SetHasDefaultConstructor(!typeCxxRecordDecl->hasUserProvidedDefaultConstructor() && typeCxxRecordDecl->needsImplicitDefaultConstructor());
type->SetHasDefaultDestructor(typeCxxRecordDecl->needsImplicitDestructor());

// methods
for (auto it = cxxRecordDecl->method_begin(); it != cxxRecordDecl->method_end(); it++) {
clang::CXXMethodDecl *method = *it;
clang::CXXMethodDecl* method = *it;
if (method != 0) {
ScrapeMethodDecl(method, type);
}
Expand All @@ -90,7 +90,7 @@ namespace metacpp {
}

std::vector<TemplateArgument>
ASTScraper::ResolveCXXRecordTemplate(const clang::CXXRecordDecl *cxxRecordDecl, QualifiedName &qualifiedName) {
ASTScraper::ResolveCXXRecordTemplate(const clang::CXXRecordDecl* cxxRecordDecl, QualifiedName& qualifiedName) {
std::string typeName = cxxRecordDecl->getQualifiedNameAsString();
std::vector<TemplateArgument> templateArgs;

Expand All @@ -99,9 +99,9 @@ namespace metacpp {
if (templateDecl) {
typeName += "<";

const clang::TemplateArgumentList &args = templateDecl->getTemplateArgs();
const clang::TemplateArgumentList& args = templateDecl->getTemplateArgs();
for (int i = 0, end = (int) args.size(); i < end; i++) {
const clang::TemplateArgument &arg = args[i];
const clang::TemplateArgument& arg = args[i];
if (i > 0)
typeName += ",";

Expand Down Expand Up @@ -140,7 +140,7 @@ namespace metacpp {
return templateArgs;
}

Type *ASTScraper::ScrapeType(const clang::Type *cType, size_t arraySize) {
Type* ASTScraper::ScrapeType(const clang::Type* cType, size_t arraySize) {
QualifiedName qualifiedName;
metacpp::TypeKind kind;
std::vector<TemplateArgument> templateArgs;
Expand Down Expand Up @@ -205,13 +205,14 @@ namespace metacpp {
if (m_Storage->HasType(typeId))
return m_Storage->GetType(typeId);

metacpp::Type *type = new metacpp::Type(typeId, qualifiedName);
metacpp::Type* type = new metacpp::Type(typeId, qualifiedName);

type->SetArraySize(arraySize);
type->SetKind(kind);
type->SetHasDefaultConstructor(cType->getTypeClass() == clang::Type::TypeClass::Builtin && qualifiedName.GetName() != "void");
type->SetHasDefaultDestructor(cType->getTypeClass() == clang::Type::TypeClass::Builtin && qualifiedName.GetName() != "void");
type->SetPolymorphic(false);
m_Storage->AddType(type);

if (auto cxxRecordDecl = cType->getAsCXXRecordDecl()) {
if (cxxRecordDecl->getDeclKind() == clang::Decl::ClassTemplateSpecialization
Expand All @@ -235,18 +236,16 @@ namespace metacpp {
}
}

for (const TemplateArgument &argument : templateArgs)
for (const TemplateArgument& argument : templateArgs)
type->AddTemplateArgument(argument);

m_Storage->AddType(type);

if (auto declCtx = cType->getAsTagDecl()->castToDeclContext(cType->getAsTagDecl()))
ScrapeDeclContext(declCtx, type);

return type;
}

void ASTScraper::ScrapeFieldDecl(const clang::FieldDecl *fieldDecl, Type *parent) {
void ASTScraper::ScrapeFieldDecl(const clang::FieldDecl* fieldDecl, Type* parent) {
if (!parent)
return;
if (fieldDecl->isAnonymousStructOrUnion())
Expand All @@ -261,7 +260,7 @@ namespace metacpp {
parent->AddField(field);
}

void ASTScraper::ScrapeMethodDecl(const clang::CXXMethodDecl *cxxMethodDecl, Type *parent) {
void ASTScraper::ScrapeMethodDecl(const clang::CXXMethodDecl* cxxMethodDecl, Type* parent) {
auto constructor = clang::dyn_cast<clang::CXXConstructorDecl>(cxxMethodDecl);
auto destructor = clang::dyn_cast<clang::CXXDestructorDecl>(cxxMethodDecl);

Expand All @@ -288,7 +287,7 @@ namespace metacpp {

// params
for (auto it = cxxMethodDecl->param_begin(); it != cxxMethodDecl->param_end(); it++) {
clang::ParmVarDecl *param = *it;
clang::ParmVarDecl* param = *it;
std::string name = param->getName().str();
QualifiedType qtype = ResolveQualType(param->getType(), 1);

Expand Down Expand Up @@ -322,14 +321,14 @@ namespace metacpp {

MakeCanonical(qualType);

const clang::Type *cType = qualType.split().Ty;
Type *type = ScrapeType(cType, arraySize);
const clang::Type* cType = qualType.split().Ty;
Type* type = ScrapeType(cType, arraySize);
qualifiedType.SetTypeID(type ? type->GetTypeID() : 0);

return qualifiedType;
}

void ASTScraper::MakeCanonical(clang::QualType &qualType) {
void ASTScraper::MakeCanonical(clang::QualType& qualType) {
if (!qualType.isCanonical())
qualType = qualType.getCanonicalType();
}
Expand All @@ -346,7 +345,7 @@ namespace metacpp {
return QualifiedName(qualifiedName);
}

void ASTScraper::RemoveAll(std::string &source, const std::string &search) {
void ASTScraper::RemoveAll(std::string& source, const std::string& search) {
std::string::size_type n = search.length();
for (std::string::size_type i = source.find(search); i != std::string::npos; i = source.find(search))
source.erase(i, n);
Expand All @@ -365,7 +364,7 @@ namespace metacpp {
}
}

std::vector<std::string> ASTScraper::ScrapeAnnotations(const clang::Decl *decl) {
std::vector<std::string> ASTScraper::ScrapeAnnotations(const clang::Decl* decl) {
std::vector<std::string> annotations;

if (decl && decl->hasAttrs()) {
Expand All @@ -374,7 +373,7 @@ namespace metacpp {

// __attribute__((annotate("...")))
for (; it != itEnd; it++) {
const clang::AnnotateAttr *attr = clang::dyn_cast<clang::AnnotateAttr>(*it);
const clang::AnnotateAttr* attr = clang::dyn_cast<clang::AnnotateAttr>(*it);
if (attr)
annotations.emplace_back(attr->getAnnotation());
}
Expand All @@ -383,13 +382,13 @@ namespace metacpp {
return annotations;
}

bool ASTScraper::IsReflected(const std::vector<std::string> &attrs) {
bool ASTScraper::IsReflected(const std::vector<std::string>& attrs) {
if (m_Config.AnnotationRequired.size() == 0)
return true;
return std::find(attrs.begin(), attrs.end(), m_Config.AnnotationRequired) != attrs.end();
}

void ASTScraper::SetContext(clang::ASTContext *context) {
void ASTScraper::SetContext(clang::ASTContext* context) {
m_Context = context;
}
}
32 changes: 16 additions & 16 deletions MetaCPP-CLI/ASTScraper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,35 @@ namespace metacpp {
std::string AnnotationRequired;
};

ASTScraper(Storage *storage, const Configuration &config);
ASTScraper(Storage* storage, const Configuration& config);

void ScrapeTranslationUnit(const clang::TranslationUnitDecl *tuDecl);
void ScrapeDeclContext(const clang::DeclContext *ctxDecl, Type *parent);
void ScrapeNamedDecl(const clang::NamedDecl *namedDecl, Type *parent);
void ScrapeTranslationUnit(const clang::TranslationUnitDecl* tuDecl);
void ScrapeDeclContext(const clang::DeclContext* ctxDecl, Type* parent);
void ScrapeNamedDecl(const clang::NamedDecl* namedDecl, Type* parent);

Type *ScrapeCXXRecordDecl(const clang::CXXRecordDecl *cxxRecordDecl, Type *parent);
std::vector<TemplateArgument> ResolveCXXRecordTemplate(const clang::CXXRecordDecl *cxxRecordDecl, QualifiedName &qualifiedName);
Type *ScrapeType(const clang::Type *cType, size_t arraySize);
void ScrapeFieldDecl(const clang::FieldDecl *fieldDecl, Type *parent);
void ScrapeMethodDecl(const clang::CXXMethodDecl *cxxMethodDecl, Type *parent);
Type* ScrapeCXXRecordDecl(const clang::CXXRecordDecl* cxxRecordDecl, Type* parent);
std::vector<TemplateArgument> ResolveCXXRecordTemplate(const clang::CXXRecordDecl* cxxRecordDecl, QualifiedName& qualifiedName);
Type* ScrapeType(const clang::Type* cType, size_t arraySize);
void ScrapeFieldDecl(const clang::FieldDecl* fieldDecl, Type* parent);
void ScrapeMethodDecl(const clang::CXXMethodDecl* cxxMethodDecl, Type* parent);

QualifiedType ResolveQualType(clang::QualType qualType, size_t arraySize);
std::vector<std::string> ScrapeAnnotations(const clang::Decl *decl);
std::vector<std::string> ScrapeAnnotations(const clang::Decl* decl);

/* Utility */
void MakeCanonical(clang::QualType &qualType);
void MakeCanonical(clang::QualType& qualType);
QualifiedName ResolveQualifiedName(std::string qualifiedName);
void RemoveAll(std::string &source, const std::string &search);
void RemoveAll(std::string& source, const std::string& search);
AccessSpecifier TransformAccess(const clang::AccessSpecifier as);

bool IsReflected(const std::vector<std::string> &attrs);
bool IsReflected(const std::vector<std::string>& attrs);

void SetContext(clang::ASTContext *context);
void SetContext(clang::ASTContext* context);

private:
clang::ASTContext *m_Context;
clang::ASTContext* m_Context;
Configuration m_Config;
Storage *m_Storage;
Storage* m_Storage;
};
}

Expand Down
13 changes: 6 additions & 7 deletions MetaCPP-CLI/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required (VERSION 3.1)
cmake_minimum_required(VERSION 3.1)
project(MetaCPP-CLI)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../CMake")
Expand All @@ -13,8 +13,7 @@ link_directories(${LLVM_LIB_DIR})
add_definitions(${LLVM_COMPILE_FLAGS})

set(MetaCPPCLI_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR})
file(GLOB_RECURSE MetaCPPCLI_SRC ${MetaCPPCLI_SRC_DIR}/*.hpp
${MetaCPPCLI_SRC_DIR}/*.cpp)
file(GLOB_RECURSE MetaCPPCLI_SRC ${MetaCPPCLI_SRC_DIR}/*.hpp ${MetaCPPCLI_SRC_DIR}/*.cpp)

add_executable(MetaCPPCLI ${MetaCPPCLI_SRC})

Expand All @@ -24,7 +23,7 @@ target_link_libraries(MetaCPPCLI PUBLIC MetaCPP)
target_link_libraries(MetaCPPCLI PUBLIC ${CLANG_LIBS} ${LLVM_LIBS_CORE} ${LLVM_LDFLAGS})
target_include_directories(MetaCPPCLI PRIVATE ${LLVM_INCLUDE_DIR} ${CLANG_INCLUDE_DIRS})
target_include_directories(MetaCPPCLI PUBLIC ../External/Mustache)
if(WIN32)
# Annoying lib in Windows
target_link_libraries(MetaCPPCLI PRIVATE version.lib)
endif()
if (WIN32)
# Annoying lib in Windows
target_link_libraries(MetaCPPCLI PRIVATE version.lib)
endif ()
Loading

0 comments on commit 31dc895

Please sign in to comment.