initial atdna commit

This commit is contained in:
Jack Andersen 2015-06-14 16:49:02 -10:00
parent 1ea07c20fb
commit dde8ac1a1c
5 changed files with 265 additions and 1 deletions

View File

@ -62,7 +62,8 @@ HEADERS += \
$$PWD/include/utf8/unchecked.h \ $$PWD/include/utf8/unchecked.h \
$$PWD/include/Athena/FileInfo.hpp \ $$PWD/include/Athena/FileInfo.hpp \
$$PWD/include/Athena/Dir.hpp \ $$PWD/include/Athena/Dir.hpp \
$$PWD/include/gekko_support.h $$PWD/include/gekko_support.h \
$$PWD/include/Athena/DNA.hpp
win32:HEADERS += \ win32:HEADERS += \
$$PWD/include/win32_largefilewrapper.h $$PWD/include/win32_largefilewrapper.h

47
atdna/atdna.pro Normal file
View File

@ -0,0 +1,47 @@
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
QT =
DEFINES += __STDC_LIMIT_MACROS=1 __STDC_CONSTANT_MACROS=1
QMAKE_CXXFLAGS += -fno-rtti
QMAKE_CXXFLAGS_WARN_ON = -Wno-unused-parameter
INCLUDEPATH += ../include /run/media/jacko/Extra/llvm-build/usr/include
SOURCES += \
main.cpp
LIBS += -L/run/media/jacko/Extra/llvm-build/usr/lib \
-lclangFrontendTool -lclangFrontend -lclangTooling -lclangDriver \
-lclangSerialization -lclangCodeGen -lclangParse -lclangSema \
-lclangRewriteFrontend -lclangRewrite -lclangAnalysis -lclangEdit \
-lclangAST -lclangLex -lclangBasic \
-lLLVMLTO -lLLVMObjCARCOpts -lLLVMLinker -lLLVMBitWriter -lLLVMIRReader \
-lLLVMXCoreDisassembler -lLLVMXCoreCodeGen -lLLVMXCoreDesc -lLLVMXCoreInfo \
-lLLVMXCoreAsmPrinter -lLLVMSystemZDisassembler -lLLVMSystemZCodeGen \
-lLLVMSystemZAsmParser -lLLVMSystemZDesc -lLLVMSystemZInfo -lLLVMSystemZAsmPrinter \
-lLLVMSparcDisassembler -lLLVMSparcCodeGen -lLLVMSparcAsmParser -lLLVMSparcDesc \
-lLLVMSparcInfo -lLLVMSparcAsmPrinter -lLLVMPowerPCDisassembler -lLLVMPowerPCCodeGen \
-lLLVMPowerPCAsmParser -lLLVMPowerPCDesc -lLLVMPowerPCInfo -lLLVMPowerPCAsmPrinter \
-lLLVMNVPTXCodeGen -lLLVMNVPTXDesc -lLLVMNVPTXInfo -lLLVMNVPTXAsmPrinter \
-lLLVMMSP430CodeGen -lLLVMMSP430Desc -lLLVMMSP430Info -lLLVMMSP430AsmPrinter \
-lLLVMMipsDisassembler -lLLVMMipsCodeGen -lLLVMMipsAsmParser -lLLVMMipsDesc \
-lLLVMMipsInfo -lLLVMMipsAsmPrinter -lLLVMHexagonDisassembler -lLLVMHexagonCodeGen \
-lLLVMHexagonDesc -lLLVMHexagonInfo -lLLVMCppBackendCodeGen -lLLVMCppBackendInfo \
-lLLVMBPFCodeGen -lLLVMBPFDesc -lLLVMBPFInfo -lLLVMBPFAsmPrinter -lLLVMARMDisassembler \
-lLLVMARMCodeGen -lLLVMARMAsmParser -lLLVMARMDesc -lLLVMARMInfo -lLLVMARMAsmPrinter \
-lLLVMAMDGPUCodeGen -lLLVMAMDGPUAsmParser -lLLVMAMDGPUDesc -lLLVMAMDGPUInfo \
-lLLVMAMDGPUAsmPrinter -lLLVMAArch64Disassembler -lLLVMAArch64CodeGen -lLLVMAArch64AsmParser \
-lLLVMAArch64Desc -lLLVMAArch64Info -lLLVMAArch64AsmPrinter -lLLVMAArch64Utils \
-lLLVMMIRParser -lLLVMAsmParser -lLLVMLibDriver -lLLVMOption -lLLVMDebugInfoPDB -lLLVMTableGen \
-lLLVMOrcJIT -lLLVMLineEditor -lLLVMInstrumentation -lLLVMX86Disassembler -lLLVMX86AsmParser \
-lLLVMX86CodeGen -lLLVMSelectionDAG -lLLVMAsmPrinter -lLLVMX86Desc -lLLVMMCDisassembler \
-lLLVMX86Info -lLLVMX86AsmPrinter -lLLVMX86Utils -lLLVMMCJIT -lLLVMDebugInfoDWARF \
-lLLVMPasses -lLLVMipo -lLLVMVectorize -lLLVMInterpreter -lLLVMExecutionEngine \
-lLLVMRuntimeDyld -lLLVMCodeGen -lLLVMTarget -lLLVMScalarOpts -lLLVMProfileData \
-lLLVMObject -lLLVMMCParser -lLLVMBitReader -lLLVMInstCombine -lLLVMTransformUtils \
-lLLVMipa -lLLVMMC -lLLVMAnalysis -lLLVMCore -lLLVMSupport -lz -lpthread -lcurses -ldl -g
HEADERS += \
test.hpp

147
atdna/main.cpp Normal file
View File

@ -0,0 +1,147 @@
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Tooling/Tooling.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Sema/Sema.h"
#include "clang/AST/RecordLayout.h"
#include "clang/AST/DeclCXX.h"
class FindNamedClassVisitor : public clang::RecursiveASTVisitor<FindNamedClassVisitor>
{
clang::ASTContext& context;
public:
explicit FindNamedClassVisitor(clang::ASTContext& ctxin)
: context(ctxin) {}
bool VisitCXXRecordDecl(clang::CXXRecordDecl* decl)
{
if (decl->isInvalidDecl() || !decl->hasDefinition())
return true;
if (!decl->getNumBases())
return true;
bool foundDNA = false;
for (const clang::CXXBaseSpecifier& base : decl->bases())
{
llvm::outs() << "BASE " << base.getType().getCanonicalType().getAsString() << "\n";
if (!base.getType().getCanonicalType().getAsString().compare("struct Athena::io::DNA"))
{
foundDNA = true;
break;
}
}
if (!foundDNA)
return true;
llvm::outs() << "DECL name " << decl->getQualifiedNameAsString() << "\n";
llvm::outs() << "DECL kind " << decl->getKindName() << "\n";
const clang::ASTRecordLayout& layout = context.getASTRecordLayout(decl);
for (const clang::FieldDecl* field : decl->fields())
{
llvm::outs() << " Field " << field->getName();
clang::QualType qualType = field->getType();
const clang::Type* regType = qualType.getTypePtrOrNull();
if (regType->getTypeClass() == clang::Type::Typedef)
{
const clang::TypedefType* tdType = (const clang::TypedefType*)regType;
llvm::outs() << " Typedef " << tdType->getDecl()->getNameAsString();
}
else if (regType->getTypeClass() == clang::Type::TemplateSpecialization)
{
const clang::TemplateSpecializationType* tsType = (const clang::TemplateSpecializationType*)regType;
llvm::outs() << " Alias " << tsType->getTemplateName().getAsTemplateDecl()->getNameAsString();
llvm::outs() << " " << tsType->getNumArgs() << " ";
for (const clang::TemplateArgument& arg : *tsType)
{
if (arg.getKind() == clang::TemplateArgument::Expression)
{
llvm::APSInt value;
if (arg.getAsExpr()->isIntegerConstantExpr(value, context))
{
llvm::outs() << " Expr " << value;
}
}
else if (arg.getKind() == clang::TemplateArgument::Type)
{
llvm::outs() << " Type " << arg.getAsType().getAsString();
}
}
}
else if (regType->getTypeClass() == clang::Type::Builtin)
{
const clang::BuiltinType* bType = (const clang::BuiltinType*)regType;
llvm::outs() << " Builtin " << bType->getName(clang::PrintingPolicy(clang::LangOptions()));
}
clang::TypeInfo regTypeInfo = context.getTypeInfo(qualType);
llvm::outs() << " Width " << regTypeInfo.Width;
llvm::outs() << " Off " << layout.getFieldOffset(field->getFieldIndex()) << "\n";
}
/*
if (decl->getQualifiedNameAsString() == "n::m::C")
{
clang::FullSourceLoc fullLoc = context.getFullLoc(decl->getLocStart());
if (fullLoc.isValid())
llvm::outs() << "Found declaration at "
<< fullLoc.getSpellingLineNumber() << ":"
<< fullLoc.getSpellingColumnNumber() << "\n";
}
*/
return true;
}
};
class FindNamedClassConsumer : public clang::ASTConsumer
{
FindNamedClassVisitor visitor;
public:
explicit FindNamedClassConsumer(clang::ASTContext& context)
: visitor(context) {}
void HandleTranslationUnit(clang::ASTContext& context)
{visitor.TraverseDecl(context.getTranslationUnitDecl());}
};
static const char ATDNA_PREAMBLE[] =
"#include <stdint.h>\n"
"typedef uint16_t atUint16;\n"
"typedef int16_t atInt16;\n"
"typedef uint32_t atUint32;\n"
"typedef int32_t atInt32;\n"
"typedef uint64_t atUint64;\n"
"typedef int64_t atInt64;\n"
"#define TYPES_HPP\n";
class FindNamedClassAction : public clang::ASTFrontendAction
{
std::unique_ptr<llvm::MemoryBuffer> preambleBuf;
public:
explicit FindNamedClassAction()
: preambleBuf(llvm::MemoryBuffer::getMemBuffer(ATDNA_PREAMBLE, "ATDNA_PREAMBLE.hpp")) {}
std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(clang::CompilerInstance& compiler,
llvm::StringRef /*filename*/)
{
//compiler.getDiagnostics().setSuppressAllDiagnostics();
compiler.getPreprocessorOpts().addRemappedFile("ATDNA_PREAMBLE.hpp", preambleBuf.get());
compiler.getPreprocessorOpts().ChainedIncludes.push_back("ATDNA_PREAMBLE.hpp");
compiler.getPreprocessorOpts().RetainRemappedFileBuffers = true;
return std::unique_ptr<clang::ASTConsumer>(new FindNamedClassConsumer(compiler.getASTContext()));
}
};
int main(int argc, const char** argv)
{
if (argc > 1)
{
clang::FileManager fman((clang::FileSystemOptions()));
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> buf = fman.getBufferForFile(argv[1]);
std::error_code ec;
if ((ec = buf.getError()))
throw ec;
llvm::Twine buft(buf->get()->getBuffer());
clang::tooling::runToolOnCodeWithArgs(new FindNamedClassAction, buft, {"-std=c++11"}, argv[1]);
}
return 0;
}

39
atdna/test.hpp Normal file
View File

@ -0,0 +1,39 @@
#include <stdint.h>
#include <vector>
typedef uint32_t atUint32;
typedef uint16_t atUint16;
enum Endian
{
LITTLE,
BIG
};
template <typename T, Endian endian>
using Value = T;
template <typename T, Endian endian>
using Vector = std::vector<T>;
namespace Athena
{
namespace io
{
class IStreamReader;
class IStreamWriter;
struct DNA
{
void read(IStreamReader&);
void write(IStreamWriter&) const;
};
}
}
using namespace Athena;
struct ANCSFile : public io::DNA
{
Value<atUint32, BIG> var32;
Value<atUint16, BIG> var16;
Vector<atUint32, BIG> vec;
};

30
include/Athena/DNA.hpp Normal file
View File

@ -0,0 +1,30 @@
#ifndef DNA_HPP
#define DNA_HPP
#include <vector>
namespace Athena
{
namespace io
{
class IStreamReader;
class IStreamWriter;
template <bool bigEndian=true>
struct DNA
{
virtual bool read(IStreamReader& reader)=0;
virtual bool write(IStreamWriter& writer) const=0;
};
template <typename T, bool bigEndian=true>
using DNAValue = T;
template <typename T, bool bigEndian=true>
using DNAVector = std::vector<T>;
}
}
#endif // DNA_HPP