Skip to content

Commit

Permalink
[format] re-format code on google cpp style
Browse files Browse the repository at this point in the history
  • Loading branch information
lvntky committed Jun 22, 2024
1 parent e0747b9 commit 97a0776
Show file tree
Hide file tree
Showing 16 changed files with 274 additions and 295 deletions.
103 changes: 81 additions & 22 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,24 +1,83 @@
---
# Google C/C++ Code Style settings
# https://clang.llvm.org/docs/ClangFormatStyleOptions.html
# Author: Kehan Xue, kehan.xue (at) gmail.com

Language: Cpp
BasedOnStyle: Google
AlignAfterOpenBracket: 'AlwaysBreak'
AllowAllConstructorInitializersOnNextLine: 'false'
AllowAllParametersOfDeclarationOnNextLine: 'false'
AlignConsecutiveMacros: 'true'
AllowShortCaseLabelsOnASingleLine: 'true'
AllowShortFunctionsOnASingleLine: 'None'
AllowShortIfStatementsOnASingleLine: 'Never'
AllowShortLoopsOnASingleLine: 'false'
BreakBeforeBraces: Allman
BinPackArguments: 'false'
BinPackParameters: 'false'
Cpp11BracedListStyle: 'false'
ColumnLimit: 125
NamespaceIndentation: All
SpaceAfterTemplateKeyword: 'false'
SpaceBeforeCtorInitializerColon: 'true'
SpaceBeforeInheritanceColon: 'true'
AccessModifierOffset: -1
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: None
AlignOperands: Align
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: Never # To avoid conflict, set this "Never" and each "if statement" should include brace when coding
AllowShortLambdasOnASingleLine: Inline
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterReturnType: None
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: true
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterStruct: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
ColumnLimit: 80
CompactNamespaces: false
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false # Make sure the * or & align on the left
EmptyLineBeforeAccessModifier: LogicalBlock
FixNamespaceComments: true
IncludeBlocks: Preserve
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth: 2
KeepEmptyLinesAtTheStartOfBlocks: true
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PointerAlignment: Left
ReflowComments: false
# SeparateDefinitionBlocks: Always # Only support since clang-format 14
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: 'true'
SpaceInEmptyBlock: true
Standard: 'Latest'
...
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: c++11
TabWidth: 4
UseTab: Never
3 changes: 1 addition & 2 deletions include/cvm/banner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

#define CVM_VERSION "2.0.0"

void printBanner()
{
void printBanner() {
const std::string green = "\033[32m";
const std::string reset = "\033[0m";

Expand Down
14 changes: 4 additions & 10 deletions include/cvm/classfile/attribute_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,20 @@

#include "../fmt_commons.hpp"

typedef struct attribute_info
{
typedef struct attribute_info {
uint16_t attribute_name_index;
uint32_t attribute_length;
uint8_t* info;

~attribute_info()
{
delete[] info;
}
~attribute_info() { delete[] info; }

std::string to_string() const
{
std::string to_string() const {
return fmt::format(
"Attribute:\n"
" attribute_name_index: {}\n"
" attribute_length: {}\n"
" info: [{}]",
attribute_name_index,
attribute_length,
attribute_name_index, attribute_length,
fmt::join(info, info + attribute_length, ", "));
}
} attribute_info;
Expand Down
13 changes: 6 additions & 7 deletions include/cvm/classfile/classfile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
#include "field_info.hpp"
#include "method_info.hpp"

class Classfile
{
class Classfile {
public:
uint32_t magic;
uint16_t minor_version;
Expand All @@ -32,11 +31,11 @@ class Classfile
uint16_t attributes_count;
std::vector<attribute_info> attributes;

uint16_t readShort(const uint8_t *bytes, size_t &offset);
uint32_t readInt(const uint8_t *bytes, size_t &offset);
uint64_t readLong(const uint8_t *bytes, size_t &offset);
double readDouble(const uint8_t *bytes, size_t &offset);
Classfile parseClassfile(const uint8_t *classBytes, size_t fileSize);
uint16_t readShort(const uint8_t* bytes, size_t& offset);
uint32_t readInt(const uint8_t* bytes, size_t& offset);
uint64_t readLong(const uint8_t* bytes, size_t& offset);
double readDouble(const uint8_t* bytes, size_t& offset);
Classfile parseClassfile(const uint8_t* classBytes, size_t fileSize);
std::string to_string() const;
};

Expand Down
43 changes: 17 additions & 26 deletions include/cvm/classfile/cp_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,40 @@
#include "../fmt_commons.hpp"

// Constants for constant pool tags
#define CONSTANT_Class 7
#define CONSTANT_Fieldref 9
#define CONSTANT_Methodref 10
#define CONSTANT_Class 7
#define CONSTANT_Fieldref 9
#define CONSTANT_Methodref 10
#define CONSTANT_InterfaceMethodref 11
#define CONSTANT_String 8
#define CONSTANT_Integer 3
#define CONSTANT_NameAndType 12
#define CONSTANT_Double 6
#define CONSTANT_Utf8 1
#define CONSTANT_String 8
#define CONSTANT_Integer 3
#define CONSTANT_NameAndType 12
#define CONSTANT_Double 6
#define CONSTANT_Utf8 1

typedef struct cpinfo
{
typedef struct cpinfo {
uint8_t tag;
union
{
struct
{
union {
struct {
uint16_t name_index;
} Class;
struct
{
struct {
uint16_t string_index;
} String;
struct
{
struct {
uint16_t class_index;
uint16_t name_and_type_index;
} Fieldref, Methodref, InterfaceMethodref;
struct
{
struct {
int32_t bytes;
} Integer;
struct
{
struct {
uint16_t name_index;
uint16_t descriptor_index;
} NameAndType;
struct
{
struct {
uint64_t bytes;
} Double;
struct
{
struct {
uint16_t length;
uint8_t* bytes;
} Utf8;
Expand Down
14 changes: 4 additions & 10 deletions include/cvm/classfile/field_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@
#include "../fmt_commons.hpp"
#include "attribute_info.hpp"

typedef struct field_info
{
typedef struct field_info {
uint16_t access_flags;
uint16_t name_index;
uint16_t descriptor_index;
uint16_t attributes_count;
std::vector<attribute_info> attributes;

std::string to_string() const
{
std::string to_string() const {
std::string attributes_str;
for (const auto& attr : attributes)
{
for (const auto& attr : attributes) {
attributes_str += attr.to_string() + "\n";
}
return fmt::format(
Expand All @@ -29,10 +26,7 @@ typedef struct field_info
" descriptor_index: {}\n"
" attributes_count: {}\n"
" attributes: [\n{}\n ]",
access_flags,
name_index,
descriptor_index,
attributes_count,
access_flags, name_index, descriptor_index, attributes_count,
attributes_str);
}
} field_info;
Expand Down
14 changes: 4 additions & 10 deletions include/cvm/classfile/method_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@
#include "../fmt_commons.hpp"
#include "attribute_info.hpp"

typedef struct method_info
{
typedef struct method_info {
uint16_t access_flags;
uint16_t name_index;
uint16_t descriptor_index;
uint16_t attributes_count;
std::vector<attribute_info> attributes;

std::string to_string() const
{
std::string to_string() const {
std::string attributes_str;
for (const auto& attr : attributes)
{
for (const auto& attr : attributes) {
attributes_str += attr.to_string() + "\n";
}
return fmt::format(
Expand All @@ -29,10 +26,7 @@ typedef struct method_info
" descriptor_index: {}\n"
" attributes_count: {}\n"
" attributes: [\n{}\n ]",
access_flags,
name_index,
descriptor_index,
attributes_count,
access_flags, name_index, descriptor_index, attributes_count,
attributes_str);
}
} method_info;
Expand Down
9 changes: 5 additions & 4 deletions include/cvm/execute_engine/cvm_execute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@
#include "../classfile/classfile.hpp"
#include "../log.hpp"

class CVM
{
class CVM {
public:
CVM() = default;
void execute(const Classfile& cf, const std::string& methodName);

private:
std::string getUtf8FromConstantPool(const Classfile& cf, uint16_t index);
const method_info* findMehodByName(const Classfile& cf, const std::string& methodName);
const uint8_t* getByteCode(const Classfile& cf, const method_info* methodInfo);
const method_info* findMehodByName(const Classfile& cf,
const std::string& methodName);
const uint8_t* getByteCode(const Classfile& cf,
const method_info* methodInfo);
void interprete(const uint8_t* byteCode, const Classfile& cf);
};

Expand Down
4 changes: 2 additions & 2 deletions include/cvm/log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

#include <memory>

#define LOG_OK "OK"
#define LOG_NOK "NOK"
#define LOG_OK "OK"
#define LOG_NOK "NOK"
#define DEBUG_ENABLED 0

void setLevel();
Expand Down
18 changes: 4 additions & 14 deletions include/cvm/stack/cvm_stack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,13 @@

#include "frame.hpp"

class CVMStack
{
class CVMStack {
public:
void pushFrame(Frame frame)
{
frames.push(frame);
}
void pushFrame(Frame frame) { frames.push(frame); }

Frame& topFrame()
{
return frames.top();
}
Frame& topFrame() { return frames.top(); }

void popFrame()
{
frames.pop();
}
void popFrame() { frames.pop(); }

private:
std::stack<Frame> frames;
Expand Down
10 changes: 5 additions & 5 deletions include/cvm/stack/frame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
#include "../fmt_commons.hpp"
// #include "../log.hpp"

class Frame
{
class Frame {
public:
Frame(size_t maxLocals, size_t maxStack) : localVariables(maxLocals), operandStack(maxStack), operandStackSize(maxStack)
{
}
Frame(size_t maxLocals, size_t maxStack)
: localVariables(maxLocals),
operandStack(maxStack),
operandStackSize(maxStack) {}
int32_t getLocalVariable(size_t index) const;
void setLocalVariable(size_t index, int32_t value);
void pushOperand(int32_t value);
Expand Down
Loading

0 comments on commit 97a0776

Please sign in to comment.