From fda997b8bf3185907f61e6b11c54fc4558f21b88 Mon Sep 17 00:00:00 2001 From: yuanbo Date: Fri, 18 Feb 2022 10:11:39 +0800 Subject: [PATCH] fix: add missed '-o' option and reformat hc-gen code Signed-off-by: yuanbo --- tools/hc-gen/src/ast.cpp | 61 ++++++++++-------------- tools/hc-gen/src/ast.h | 28 +++++------ tools/hc-gen/src/bytecode_gen.cpp | 42 ++++++++--------- tools/hc-gen/src/bytecode_gen.h | 16 +++---- tools/hc-gen/src/decompile.cpp | 33 +++++++------ tools/hc-gen/src/decompile.h | 5 +- tools/hc-gen/src/decompile_gen.cpp | 22 ++++----- tools/hc-gen/src/decompile_gen.h | 13 +++--- tools/hc-gen/src/file.cpp | 7 +-- tools/hc-gen/src/file.h | 10 ++-- tools/hc-gen/src/generator.h | 5 +- tools/hc-gen/src/lexer.cpp | 27 +++++------ tools/hc-gen/src/lexer.h | 13 +++--- tools/hc-gen/src/logger.h | 18 ++++---- tools/hc-gen/src/macro_gen.cpp | 34 +++++++------- tools/hc-gen/src/macro_gen.h | 7 +-- tools/hc-gen/src/opcode.h | 5 +- tools/hc-gen/src/option.cpp | 20 ++++---- tools/hc-gen/src/option.h | 7 +-- tools/hc-gen/src/parser.cpp | 10 ++-- tools/hc-gen/src/parser.h | 6 +-- tools/hc-gen/src/text_gen.cpp | 74 +++++++++++++++--------------- tools/hc-gen/src/text_gen.h | 21 +++++---- tools/hc-gen/src/token.cpp | 23 +++++----- tools/hc-gen/src/token.h | 8 ++-- tools/hc-gen/src/types.h | 32 ++++++------- 26 files changed, 266 insertions(+), 281 deletions(-) diff --git a/tools/hc-gen/src/ast.cpp b/tools/hc-gen/src/ast.cpp index d9f6b8b5..ee1d4632 100644 --- a/tools/hc-gen/src/ast.cpp +++ b/tools/hc-gen/src/ast.cpp @@ -6,16 +6,17 @@ * See the LICENSE file in the root of this repository for complete details. */ -#include -#include -#include #include "ast.h" + +#include +#include +#include + #include "logger.h" using namespace OHOS::Hardware; -AstObject::AstObject(AstObject &obj) : - AstObject(obj.name_, obj.type_, obj.stringValue_) +AstObject::AstObject(AstObject &obj) : AstObject(obj.name_, obj.type_, obj.stringValue_) { integerValue_ = obj.integerValue_; } @@ -150,19 +151,19 @@ std::ostream &OHOS::Hardware::operator<<(std::ostream &stream, const AstObject & } switch (t.type_) { case PARSEROP_UINT8: - stream << "uint8 " << "0x" << std::hex << t.integerValue_; + stream << "uint8 0x" << std::hex << t.integerValue_; break; case PARSEROP_UINT16: - stream << "uint16 " << "0x" << std::hex << t.integerValue_; + stream << "uint16 0x" << std::hex << t.integerValue_; break; case PARSEROP_UINT32: - stream << "uint32 " << "0x" << std::hex << t.integerValue_; + stream << "uint32 0x" << std::hex << t.integerValue_; break; case PARSEROP_UINT64: - stream << "uint64 " << "0x" << std::hex << t.integerValue_; + stream << "uint64 0x" << std::hex << t.integerValue_; break; case PARSEROP_STRING: - stream << "string " << "\"" << t.stringValue_ << "\""; + stream << "string \"" << t.stringValue_ << "\""; break; case PARSEROP_ARRAY: stream << "array"; @@ -254,7 +255,7 @@ std::shared_ptr AstObject::Lookup(const std::string &name, uint32_t t bool AstObject::IsNumber() const { - return type_ >= PARSEROP_UINT8 && type_ <= PARSEROP_UINT64;; + return type_ >= PARSEROP_UINT8 && type_ <= PARSEROP_UINT64; } bool AstObject::IsNode() const @@ -409,7 +410,6 @@ ConfigNode::ConfigNode(std::string name, uint32_t nodeType, std::string refName) inheritCount_(0), templateSignNum_(0) { - } ConfigNode::ConfigNode(Token &name, uint32_t nodeType, std::string refName) : @@ -425,12 +425,12 @@ ConfigNode::ConfigNode(Token &name, uint32_t nodeType, std::string refName) : const std::string &ConfigNode::NodeTypeToStr(uint32_t type) { static std::map type2StringMap = { - {NODE_NOREF, ""}, - {NODE_COPY, "NodeCopy"}, - {NODE_REF, "NodeReference"}, - {NODE_DELETE, "NodeDelete"}, - {NODE_INHERIT, "NodeInherit"}, - {NODE_TEMPLATE, "NodeTemplate"}, + {NODE_NOREF, "" }, + { NODE_COPY, "NodeCopy" }, + { NODE_REF, "NodeReference"}, + { NODE_DELETE, "NodeDelete" }, + { NODE_INHERIT, "NodeInherit" }, + { NODE_TEMPLATE, "NodeTemplate" }, }; return type2StringMap[type]; } @@ -592,7 +592,7 @@ bool ConfigNode::NodeRefExpand(const std::shared_ptr &ref) Logger().Error() << SourceInfo() << "reference node '" << refNodePath_ << "' not exist"; return false; } - return ref->Move(std::shared_ptr(this, [](AstObject *p) { (void) p; })); + return ref->Move(std::shared_ptr(this, [](AstObject *p) { (void)p; })); } bool ConfigNode::NodeCopyExpand(const std::shared_ptr &ref) @@ -654,8 +654,7 @@ ConfigTerm::ConfigTerm(ConfigTerm &term) : ConfigTerm(term.name_, nullptr) } ConfigTerm::ConfigTerm(std::string name, const std::shared_ptr &value) : - AstObject(std::move(name), PARSEROP_CONFTERM, 0), - signNum_(0) + AstObject(std::move(name), PARSEROP_CONFTERM, 0), signNum_(0) { if (value != nullptr) { child_ = value; @@ -664,8 +663,7 @@ ConfigTerm::ConfigTerm(std::string name, const std::shared_ptr &value } ConfigTerm::ConfigTerm(Token &name, const std::shared_ptr &value) : - AstObject(name.strval, PARSEROP_CONFTERM, 0, name), - signNum_(0) + AstObject(name.strval, PARSEROP_CONFTERM, 0, name), signNum_(0) { if (value != nullptr) { child_ = value; @@ -709,8 +707,7 @@ bool ConfigTerm::RefExpand(std::shared_ptr refObj) return true; } - if (refObj == nullptr || !refObj->IsNode() || - ConfigNode::CastFrom(refObj)->GetNodeType() == NODE_REF || + if (refObj == nullptr || !refObj->IsNode() || ConfigNode::CastFrom(refObj)->GetNodeType() == NODE_REF || ConfigNode::CastFrom(refObj)->GetNodeType() == NODE_TEMPLATE || ConfigNode::CastFrom(refObj)->GetNodeType() == NODE_DELETE) { Logger().Error() << SourceInfo() << "reference invalid node '" << child_->StringValue() << '\''; @@ -753,10 +750,7 @@ uint32_t ConfigTerm::SigNum() return signNum_; } -ConfigArray::ConfigArray() : - AstObject("", PARSEROP_ARRAY, 0), - arrayType_(0), - arraySize_(0) +ConfigArray::ConfigArray() : AstObject("", PARSEROP_ARRAY, 0), arrayType_(0), arraySize_(0) { } @@ -772,11 +766,7 @@ ConfigArray::ConfigArray(ConfigArray &array) : ConfigArray() } ConfigArray::ConfigArray(const Token &bindToken) : - AstObject("", PARSEROP_ARRAY, 0, bindToken), - arrayType_(0), - arraySize_(0) -{ -}; + AstObject("", PARSEROP_ARRAY, 0, bindToken), arrayType_(0), arraySize_(0) {}; bool ConfigArray::AddChild(const std::shared_ptr &childObj) { @@ -908,7 +898,7 @@ bool Ast::Expand() bool Ast::NodeExpand() { return WalkBackward([this](const std::shared_ptr ¤t, int32_t walkDepth) -> int32_t { - (void) walkDepth; + (void)walkDepth; if (current->IsNode()) { auto node = ConfigNode::CastFrom(current); if (node->GetNodeType() == NODE_DELETE) { @@ -958,7 +948,6 @@ bool Ast::InheritExpand() }); } - bool Ast::RedefineCheck() { if (redefineChecked_) { diff --git a/tools/hc-gen/src/ast.h b/tools/hc-gen/src/ast.h index 54348339..8a509f3b 100644 --- a/tools/hc-gen/src/ast.h +++ b/tools/hc-gen/src/ast.h @@ -10,11 +10,12 @@ #define HC_GEN_AST_H #include -#include -#include #include +#include +#include #include #include + #include "token.h" #include "types.h" @@ -283,7 +284,7 @@ public: std::shared_ptr Lookup(const std::shared_ptr &startObj, const std::string &path); - template + template static bool WalkForward(const std::shared_ptr &startObject, T callback) { std::shared_ptr forwardWalkObj = startObject; @@ -319,7 +320,7 @@ public: return true; } - template + template static bool WalkBackward(const std::shared_ptr &startObject, T callback) { std::shared_ptr backWalkObj = startObject; @@ -360,7 +361,7 @@ public: return true; } - template + template static bool WalkRound(const std::shared_ptr &startObject, T1 forwardCallback, T2 backwardCallback) { std::shared_ptr roundWalkObj = startObject; @@ -400,20 +401,19 @@ public: return true; } - - template + template bool WalkForward(T callback) { return WalkForward(astRoot_, callback); } - template + template bool WalkBackward(T callback) { return WalkBackward(astRoot_, callback); } - template + template bool WalkRound(T1 forwardCallback, T2 backwardCallback) { return WalkRound(astRoot_, forwardCallback, backwardCallback); @@ -434,11 +434,11 @@ private: bool redefineChecked_; }; -std::ostream& operator<<(std::ostream &s, const AstObject &obj); -std::ostream& operator<<(std::ostream &s, const ConfigNode &obj); -std::ostream& operator<<(std::ostream &s, const ConfigTerm &obj); +std::ostream &operator<<(std::ostream &s, const AstObject &obj); +std::ostream &operator<<(std::ostream &s, const ConfigNode &obj); +std::ostream &operator<<(std::ostream &s, const ConfigTerm &obj); -} // Hardware -} // OHOS +} // namespace Hardware +} // namespace OHOS #endif // HC_GEN_AST_H diff --git a/tools/hc-gen/src/bytecode_gen.cpp b/tools/hc-gen/src/bytecode_gen.cpp index bc7d5232..acf8d5f8 100644 --- a/tools/hc-gen/src/bytecode_gen.cpp +++ b/tools/hc-gen/src/bytecode_gen.cpp @@ -7,15 +7,15 @@ */ #include "bytecode_gen.h" -#include #include "file.h" #include "logger.h" #include "opcode.h" +#include using namespace OHOS::Hardware; -ByteCodeGen::ByteCodeGen(std::shared_ptr ast) : Generator(ast), - needAlign_(false), dummyOutput_(false), writeSize_(0) +ByteCodeGen::ByteCodeGen(std::shared_ptr ast) : + Generator(ast), needAlign_(false), dummyOutput_(false), writeSize_(0) { } @@ -94,14 +94,14 @@ uint32_t ByteCodeGen::Align(uint32_t size) const const OpCode &ByteCodeGen::ToOpCode(uint32_t objectType) { static std::map byteCodeMap = { - {PARSEROP_UINT8, {HCS_BYTE_OP, BYTE_SIZE, "Uint8"}}, - {PARSEROP_UINT16, {HCS_WORD_OP, WORD_SIZE, "Uint16"}}, - {PARSEROP_UINT32, {HCS_DWORD_OP, DWORD_SIZE, "Uint32"}}, - {PARSEROP_UINT64, {HCS_QWORD_OP, QWORD_SIZE, "Uint64"}}, - {PARSEROP_STRING, {HCS_STRING_OP, 0, "String"}}, - {PARSEROP_ARRAY, {HCS_ARRAY_OP, WORD_SIZE, "Array"}}, /* ElementCount - WORD */ - {PARSEROP_CONFNODE, {HCS_NODE_OP, DWORD_SIZE, "ConfigNode"}}, /* SubSize - DWORD */ - {PARSEROP_CONFTERM, {HCS_TERM_OP, 0, "ConfigTerm"}}, + {PARSEROP_UINT8, {HCS_BYTE_OP, BYTE_SIZE, "Uint8"} }, + {PARSEROP_UINT16, {HCS_WORD_OP, WORD_SIZE, "Uint16"} }, + {PARSEROP_UINT32, {HCS_DWORD_OP, DWORD_SIZE, "Uint32"} }, + {PARSEROP_UINT64, {HCS_QWORD_OP, QWORD_SIZE, "Uint64"} }, + {PARSEROP_STRING, {HCS_STRING_OP, 0, "String"} }, + {PARSEROP_ARRAY, {HCS_ARRAY_OP, WORD_SIZE, "Array"} }, /* ElementCount - WORD */ + {PARSEROP_CONFNODE, {HCS_NODE_OP, DWORD_SIZE, "ConfigNode"}}, /* SubSize - DWORD */ + {PARSEROP_CONFTERM, {HCS_TERM_OP, 0, "ConfigTerm"} }, {PARSEROP_NODEREF, {HCS_NODEREF_OP, DWORD_SIZE, "NodeRef"}}, /* RefHashCode - DWORD */ }; return byteCodeMap[objectType]; @@ -112,7 +112,7 @@ void ByteCodeGen::Write(const std::string &data) Write(data.c_str(), static_cast(data.size() + 1)); } -template +template void ByteCodeGen::Write(T &data) { auto p = &data; @@ -172,8 +172,8 @@ bool ByteCodeGen::ByteCodeWrite(bool dummy) .versionMajor = 0, .versionMinor = 0, .checkSum = 0, - .totalSize = static_cast(Option::Instance().ShouldAlign() ? -ast_->GetAstRoot()->GetSize() - : ast_->GetAstRoot()->GetSize()), + .totalSize = static_cast( + Option::Instance().ShouldAlign() ? -ast_->GetAstRoot()->GetSize() : ast_->GetAstRoot()->GetSize()), }; Option::Instance().GetVersion(header.versionMinor, header.versionMajor); Write(header); @@ -299,7 +299,7 @@ bool ByteCodeGen::HexdumpOutput(FILE *in, FILE *out) int32_t byte; while ((byte = getc(in)) != EOF) { if (fprintf(out, "%s0x%02x", (writeCount % NUMS_PER_LINE) ? ", " : &",\n "[PRINT_SKIP_STEP * !writeCount], - byte) < 0) { + byte) < 0) { return false; } writeCount++; @@ -312,12 +312,12 @@ bool ByteCodeGen::HexdumpOutput(FILE *in, FILE *out) return false; } if (fprintf(out, - "void HdfGetBuildInConfigData(const unsigned char** data, unsigned int* size)\n" - "{\n" - " *data = g_%s%s;\n" - " *size = g_%s%sLen;\n" - "}", - prefix.data(), HCS_HEXDUMP_ENTRY_SYMBOL, prefix.data(), HCS_HEXDUMP_ENTRY_SYMBOL) < 0) { + "void HdfGetBuildInConfigData(const unsigned char** data, unsigned int* size)\n" + "{\n" + " *data = g_%s%s;\n" + " *size = g_%s%sLen;\n" + "}", + prefix.data(), HCS_HEXDUMP_ENTRY_SYMBOL, prefix.data(), HCS_HEXDUMP_ENTRY_SYMBOL) < 0) { return false; } return true; diff --git a/tools/hc-gen/src/bytecode_gen.h b/tools/hc-gen/src/bytecode_gen.h index 638f503e..bdad6fed 100644 --- a/tools/hc-gen/src/bytecode_gen.h +++ b/tools/hc-gen/src/bytecode_gen.h @@ -6,24 +6,20 @@ * See the LICENSE file in the root of this repository for complete details. */ - #ifndef HC_GEN_BYTECODE_GEN_H #define HC_GEN_BYTECODE_GEN_H +#include "generator.h" #include #include -#include "generator.h" namespace OHOS { namespace Hardware { struct OpCode { - OpCode() : opCode(0), - size(0) {} + OpCode() : opCode(0), size(0) {} - OpCode(uint8_t code, uint32_t s, std::string str) : opCode(code), - size(s), - opStr(std::move(str)) {} + OpCode(uint8_t code, uint32_t s, std::string str) : opCode(code), size(s), opStr(std::move(str)) {} ~OpCode() = default; @@ -53,7 +49,7 @@ private: bool ByteCodeWriteWalk(); - template + template void Write(T &data); void Write(const char *data, uint32_t size); @@ -78,6 +74,6 @@ private: bool dummyOutput_; uint32_t writeSize_; }; -} // Hardware -} // OHOS +} // namespace Hardware +} // namespace OHOS #endif // HC_GEN_BYTECODE_GEN_H diff --git a/tools/hc-gen/src/decompile.cpp b/tools/hc-gen/src/decompile.cpp index 916e4cc4..0fc529ec 100644 --- a/tools/hc-gen/src/decompile.cpp +++ b/tools/hc-gen/src/decompile.cpp @@ -7,21 +7,19 @@ */ #include "decompile.h" -#include #include "decompile_gen.h" #include "logger.h" #include "opcode.h" +#include using namespace OHOS::Hardware; -Decompile::Decompile(std::string fileName) : isAlign_(false), fileName_(std::move(fileName)) -{ -} +Decompile::Decompile(std::string fileName) : isAlign_(false), fileName_(std::move(fileName)) {} bool Decompile::InitDecompileFile() { file_.open(fileName_.data(), std::ios::binary); - if(!file_.is_open()) { + if (!file_.is_open()) { Logger().Error() << "Failed to open decompile file: " << fileName_; return false; } @@ -30,7 +28,7 @@ bool Decompile::InitDecompileFile() bool Decompile::ReadFile(char *buffer, size_t readSize) { - if(!file_.read(buffer, static_cast(readSize))) { + if (!file_.read(buffer, static_cast(readSize))) { Logger().Error() << "read file failed, read size is: " << readSize; return false; } @@ -44,15 +42,16 @@ void Decompile::SetAlign(bool isAlign) bool Decompile::VerifyDecompileFile() { - HcbHeader header{0}; + HcbHeader header {0}; if (!ReadFile(reinterpret_cast(&header), sizeof(header))) { Logger().Error() << "read header failed"; return false; } - Logger().Debug() << "read Header: magic is: " << header.magicNumber << " version major: " << header.versionMajor << - " version minor: " << header.versionMinor << " checksum: " << header.checkSum << " totalSize: " << header.totalSize; + Logger().Debug() << "read Header: magic is: " << header.magicNumber << " version major: " << header.versionMajor + << " version minor: " << header.versionMinor << " checksum: " << header.checkSum + << " totalSize: " << header.totalSize; if (header.magicNumber != HCB_MAGIC_NUM) { - Logger().Error() << "magic number is: " << header.magicNumber << ", check failed!"; + Logger().Error() << "magic number is: " << header.magicNumber << ", check failed!"; return false; } if (header.totalSize < 0) { @@ -102,7 +101,7 @@ bool Decompile::ReadString(std::string &value) { value.clear(); char c; - while(ReadFile(&c, sizeof(c))) { + while (ReadFile(&c, sizeof(c))) { if (c == '\0') { break; } @@ -124,10 +123,10 @@ bool Decompile::ReadString(std::string &value) bool Decompile::GetNextByteCode(uint32_t &byteCode) { if (GetAlignSize(OPCODE_BYTE_WIDTH) == OPCODE_BYTE_WIDTH) { - uint8_t value = 0; - bool ret = ReadUint8(value); - byteCode = value; - return ret; + uint8_t value = 0; + bool ret = ReadUint8(value); + byteCode = value; + return ret; } else { return ReadUint32(byteCode); } @@ -143,7 +142,7 @@ std::shared_ptr Decompile::RebuildNode() auto node = std::make_shared(nodeName, NODE_NOREF, ""); uint32_t nodeSize = 0; - if(!ReadUint32(nodeSize)) { + if (!ReadUint32(nodeSize)) { return nullptr; } node->SetSize(nodeSize); @@ -309,7 +308,7 @@ std::shared_ptr Decompile::RebuildAst() bool Decompile::DoDecompile() { - if(!InitDecompileFile()) { + if (!InitDecompileFile()) { return false; } diff --git a/tools/hc-gen/src/decompile.h b/tools/hc-gen/src/decompile.h index cec3fe06..c691f7a6 100644 --- a/tools/hc-gen/src/decompile.h +++ b/tools/hc-gen/src/decompile.h @@ -11,6 +11,7 @@ #include #include + #include "ast.h" namespace OHOS { @@ -74,6 +75,6 @@ private: std::ifstream file_; }; -} // OHOS -} // Hardware +} // namespace Hardware +} // namespace OHOS #endif // HC_GEN_DECOMPILE_H diff --git a/tools/hc-gen/src/decompile_gen.cpp b/tools/hc-gen/src/decompile_gen.cpp index b8b82e99..586e78b6 100644 --- a/tools/hc-gen/src/decompile_gen.cpp +++ b/tools/hc-gen/src/decompile_gen.cpp @@ -7,9 +7,9 @@ */ #include "decompile_gen.h" -#include #include "file.h" #include "logger.h" +#include using namespace OHOS::Hardware; @@ -68,7 +68,7 @@ std::string DecompileGen::GetNodeRefPath(uint32_t value) return std::string(); } -int32_t DecompileGen::PrintArrayType(const std::shared_ptr& astObj) +int32_t DecompileGen::PrintArrayType(const std::shared_ptr &astObj) { WriteFile("["); auto arrayElement = astObj->Child(); @@ -88,7 +88,7 @@ int32_t DecompileGen::PrintArrayType(const std::shared_ptr& astObj) return NOERR; } -int32_t DecompileGen::PrintBaseType(const std::shared_ptr& astObj) +int32_t DecompileGen::PrintBaseType(const std::shared_ptr &astObj) { std::stringstream outStr; std::string refPath; @@ -120,7 +120,7 @@ int32_t DecompileGen::PrintBaseType(const std::shared_ptr& astObj) return NOERR; } -int32_t DecompileGen::OutPutWalk(const std::shared_ptr& astObj, int32_t walkDepth) +int32_t DecompileGen::OutPutWalk(const std::shared_ptr &astObj, int32_t walkDepth) { if (astObj->Type() != PARSEROP_CONFNODE && astObj->Type() != PARSEROP_CONFTERM) { return NOERR; @@ -155,7 +155,7 @@ int32_t DecompileGen::OutPutWalk(const std::shared_ptr& astObj, int32 return 0; } -int32_t DecompileGen::CloseBrace(const std::shared_ptr& astObj, int32_t walkDepth) +int32_t DecompileGen::CloseBrace(const std::shared_ptr &astObj, int32_t walkDepth) { if (astObj->Type() != PARSEROP_CONFNODE) { return NOERR; @@ -176,12 +176,12 @@ bool DecompileGen::OutPut() } WriteFile(fileHeader_); if (!ast_->WalkRound( - [this](std::shared_ptr ¤t, int32_t walkDepth) -> int32_t { - return OutPutWalk(current, walkDepth); - }, - [this](std::shared_ptr ¤t, int32_t walkDepth) -> int32_t { - return CloseBrace(current, walkDepth); - })) { + [this](std::shared_ptr ¤t, int32_t walkDepth) -> int32_t { + return OutPutWalk(current, walkDepth); + }, + [this](std::shared_ptr ¤t, int32_t walkDepth) -> int32_t { + return CloseBrace(current, walkDepth); + })) { return false; } diff --git a/tools/hc-gen/src/decompile_gen.h b/tools/hc-gen/src/decompile_gen.h index 3f3ae0af..d107854f 100644 --- a/tools/hc-gen/src/decompile_gen.h +++ b/tools/hc-gen/src/decompile_gen.h @@ -11,6 +11,7 @@ #include #include + #include "ast.h" namespace OHOS { @@ -29,15 +30,15 @@ private: void WriteFile(const std::string &str); - int32_t PrintBaseType(const std::shared_ptr& astObj); + int32_t PrintBaseType(const std::shared_ptr &astObj); std::string GetNodeRefPath(uint32_t hash); - int32_t PrintArrayType(const std::shared_ptr& astObj); + int32_t PrintArrayType(const std::shared_ptr &astObj); - int32_t OutPutWalk(const std::shared_ptr& astObj, int32_t walkDepth); + int32_t OutPutWalk(const std::shared_ptr &astObj, int32_t walkDepth); - int32_t CloseBrace(const std::shared_ptr& astObj, int32_t walkDepth); + int32_t CloseBrace(const std::shared_ptr &astObj, int32_t walkDepth); const std::string fileHeader_ = "/*\n * HDF decompile hcs file\n */\n\n"; std::string outPutFileName_; @@ -45,6 +46,6 @@ private: std::shared_ptr ast_; }; -} // OHOS -} // Hardware +} // namespace Hardware +} // namespace OHOS #endif // HC_GEN_DECOMPILE_GEN_H diff --git a/tools/hc-gen/src/file.cpp b/tools/hc-gen/src/file.cpp index 79fe1a18..b696f782 100644 --- a/tools/hc-gen/src/file.cpp +++ b/tools/hc-gen/src/file.cpp @@ -6,15 +6,16 @@ * See the LICENSE file in the root of this repository for complete details. */ -#include "file.h" #include #include #include + +#include "file.h" #include "types.h" using namespace OHOS::Hardware::Util; -std::string File::AbsPath(const std::string& path) +std::string File::AbsPath(const std::string &path) { char realPath[PATH_MAX]; #ifdef MINGW32 @@ -48,7 +49,7 @@ std::string File::GetDir(std::string path) return path.substr(0, separatorPos + 1); } -std::string File::FileNameBase(const std::string& path) +std::string File::FileNameBase(const std::string &path) { auto sepPos = path.rfind(OS_SEPARATOR); auto dotPos = path.rfind('.'); diff --git a/tools/hc-gen/src/file.h b/tools/hc-gen/src/file.h index 6bfca906..d74ec2d9 100644 --- a/tools/hc-gen/src/file.h +++ b/tools/hc-gen/src/file.h @@ -16,12 +16,12 @@ namespace Hardware { namespace Util { class File { public: - static std::string AbsPath(const std::string& path); + static std::string AbsPath(const std::string &path); static std::string StripSuffix(std::string path); static std::string GetDir(std::string path); - static std::string FileNameBase(const std::string& path); + static std::string FileNameBase(const std::string &path); }; -} // Util -} //Hardware -} //OHOS +} // namespace Util +} // namespace Hardware +} // namespace OHOS #endif // HC_GEN_FILE_H diff --git a/tools/hc-gen/src/generator.h b/tools/hc-gen/src/generator.h index 60c6fb74..14e159d0 100644 --- a/tools/hc-gen/src/generator.h +++ b/tools/hc-gen/src/generator.h @@ -11,6 +11,7 @@ #include #include + #include "ast.h" namespace OHOS { @@ -26,7 +27,7 @@ public: protected: std::shared_ptr ast_; }; -} // Hardware -} // OHOS +} // namespace Hardware +} // namespace OHOS #endif // HC_GEN_GENERATOR_H diff --git a/tools/hc-gen/src/lexer.cpp b/tools/hc-gen/src/lexer.cpp index 25a8e395..e9769d1f 100644 --- a/tools/hc-gen/src/lexer.cpp +++ b/tools/hc-gen/src/lexer.cpp @@ -6,21 +6,20 @@ * See the LICENSE file in the root of this repository for complete details. */ -#include "lexer.h" #include #include + +#include "lexer.h" #include "logger.h" using namespace OHOS::Hardware; -Lexer::Lexer() : lineno_(0), lineLoc_(0) -{ -} +Lexer::Lexer() : lineno_(0), lineLoc_(0) {} std::map Lexer::keyWords_ = { - {"#include", INCLUDE}, - {"root", ROOT}, - {"delete", DELETE}, + {"#include", INCLUDE }, + {"root", ROOT }, + {"delete", DELETE }, {"template", TEMPLATE}, }; @@ -172,15 +171,14 @@ bool Lexer::FillBuffer() bool Lexer::ProcessComment() { char c = 0; - ConsumeChar();// skip first '/' + ConsumeChar(); // skip first '/' if (!GetChar(c)) { Logger().Error() << *this << "unterminated comment"; return false; } if (c == '/') { - while (c != '\n' && GetChar(c)) { - } + while (c != '\n' && GetChar(c)) {} if (c != '\n' && c != EOF) { Logger().Error() << *this << "unterminated signal line comment"; return false; @@ -277,8 +275,7 @@ bool Lexer::LexFromNumber(Token &token) case 'x': // fall-through case 'X': // hex number ConsumeChar(); - while (PeekChar(c, false) && (IsNum(c) || (c >= 'a' && c <= 'f') - || (c >= 'A' && c <= 'F'))) { + while (PeekChar(c, false) && (IsNum(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) { value.push_back(c); ConsumeChar(); } @@ -297,7 +294,7 @@ bool Lexer::LexFromNumber(Token &token) break; case '+': // fall-through case '-': // fall-through, signed decimal number - default: // unsigned decimal number + default: // unsigned decimal number value.push_back(c); while (PeekChar(c, true) && IsNum(c)) { ConsumeChar(); @@ -353,7 +350,6 @@ void Lexer::LexFromLiteral(Token &token) } } while (false); - token.strval = std::move(value); token.lineNo = lineno_; } @@ -361,7 +357,7 @@ void Lexer::LexFromLiteral(Token &token) void Lexer::ConsumeChar() { char c; - (void) GetChar(c, false); + (void)GetChar(c, false); } bool Lexer::IsNum(char c) @@ -380,4 +376,3 @@ bool Lexer::LexInclude(Token &token) token.type = INCLUDE; return true; } - diff --git a/tools/hc-gen/src/lexer.h b/tools/hc-gen/src/lexer.h index adbc063f..e6ab185a 100644 --- a/tools/hc-gen/src/lexer.h +++ b/tools/hc-gen/src/lexer.h @@ -13,6 +13,7 @@ #include #include #include + #include "token.h" namespace OHOS { @@ -69,16 +70,16 @@ private: std::ifstream src_; std::shared_ptr srcName_; - char buffer_[BUFFER_SIZE]{0}; - const char *bufferStart_{nullptr}; - const char *bufferEnd_{nullptr}; + char buffer_[BUFFER_SIZE] {0}; + const char *bufferStart_ {nullptr}; + const char *bufferEnd_ {nullptr}; int32_t lineno_; int32_t lineLoc_; }; -std::ostream& operator<<(std::ostream &s, const Lexer &lexer); +std::ostream &operator<<(std::ostream &s, const Lexer &lexer); -} // Hardware -} // OHOS +} // namespace Hardware +} // namespace OHOS #endif // HC_GEN_LEXER_H diff --git a/tools/hc-gen/src/logger.h b/tools/hc-gen/src/logger.h index 93678e5d..67d4d3f9 100644 --- a/tools/hc-gen/src/logger.h +++ b/tools/hc-gen/src/logger.h @@ -6,13 +6,13 @@ * See the LICENSE file in the root of this repository for complete details. */ - #ifndef HC_GEN_LOG_H #define HC_GEN_LOG_H #include #include #include + #include "option.h" namespace OHOS { @@ -33,7 +33,7 @@ public: ::std::cout << ::std::endl; } - template + template inline Logger &operator<<(const T &v) { if (level_ <= DEBUG && !Option::Instance().VerboseLog()) { @@ -93,12 +93,12 @@ private: void ShowLevel() { static ::std::map levelStrMap = { - {NONE, ""}, - {DEBUG, "Debug"}, - {INFO, "Info"}, + {NONE, "" }, + {DEBUG, "Debug" }, + {INFO, "Info" }, {WARNING, "Warning"}, - {ERROR, "Error"}, - {FATAL, "Fatal"} + {ERROR, "Error" }, + {FATAL, "Fatal" } }; if (level_ > INFO) { ::std::cout << ERROR_COLOR_PREFIX; @@ -115,6 +115,6 @@ private: #endif }; -} // OHOS -} // Hardware +} // namespace Hardware +} // namespace OHOS #endif // HC_GEN_LOG_H diff --git a/tools/hc-gen/src/macro_gen.cpp b/tools/hc-gen/src/macro_gen.cpp index f77eaba8..ce2a60a3 100755 --- a/tools/hc-gen/src/macro_gen.cpp +++ b/tools/hc-gen/src/macro_gen.cpp @@ -6,21 +6,20 @@ * See the LICENSE file in the root of this repository for complete details. */ -#include "macro_gen.h" #include + #include "file.h" #include "logger.h" +#include "macro_gen.h" using namespace OHOS::Hardware; -constexpr static const char *FILE_HEAD_COMMENT = \ - "/*\n" \ - " * This is an automatically generated HDF config file. Do not modify it manually.\n" \ +constexpr static const char *FILE_HEAD_COMMENT = + "/*\n" + " * This is an automatically generated HDF config file. Do not modify it manually.\n" " */\n\n"; -MacroGen::MacroGen(std::shared_ptr ast) : Generator(ast) -{ -} +MacroGen::MacroGen(std::shared_ptr ast) : Generator(ast) {} const std::string &MacroGen::ToUpperString(std::string &str) { @@ -115,8 +114,8 @@ std::string MacroGen::GenFullName(int32_t depth, const std::shared_ptr &node) +bool MacroGen::GenArray( + const std::string &arrName, uint32_t &arrSize, uint32_t type, const std::shared_ptr &node) { static uint32_t index = 0; const uint32_t ELEMENT_PER_LINE = 8; @@ -157,10 +156,11 @@ bool MacroGen::GenNodeForeach(int32_t depth, const std::shared_ptr &n auto child = node->Child(); uint32_t count = 0; - Logger().Debug() << "node:" << node->Name() << " child:" << " depth:" << depth; + Logger().Debug() << "node:" << node->Name() << " child:" + << " depth:" << depth; while (child != nullptr) { if (child->IsNode()) { - Logger().Debug() << " " << child->Name(); + Logger().Debug() << " " << child->Name(); subList.push_back(GenFullName(depth + 1, child, "_")); count++; } @@ -221,8 +221,7 @@ bool MacroGen::NodeWalk() auto type = current->Type(); static uint32_t arraySize = 0; - Logger().Debug() << "name,type:[" << current->Name() << "," << type \ - << "] depth:" << depth \ + Logger().Debug() << "name,type:[" << current->Name() << "," << type << "] depth:" << depth << " arraySize:" << std::dec << arraySize << '\n'; SetTypeData(type, current, arraySize, depth); @@ -230,8 +229,7 @@ bool MacroGen::NodeWalk() }); } -void MacroGen::SetTypeData(uint32_t type, const std::shared_ptr ¤t, - uint32_t &arraySize, int32_t depth) +void MacroGen::SetTypeData(uint32_t type, const std::shared_ptr ¤t, uint32_t &arraySize, int32_t depth) { static std::string nodeName; static std::string arrayName; @@ -248,7 +246,7 @@ void MacroGen::SetTypeData(uint32_t type, const std::shared_ptr &curr if (arraySize != 0) { GenArray(arrayName, arraySize, arrayType, current); } else { - ofs_ << " " << '"' << current->StringValue() << '"' << std::endl; + ofs_ << " " << '"' << current->StringValue() << '"' << std::endl; } break; case PARSEROP_CONFTERM: @@ -281,8 +279,8 @@ void MacroGen::SetTypeData(uint32_t type, const std::shared_ptr &curr } } -void MacroGen::SetTypeDataUinit64(const std::string &arrayName, uint32_t &arraySize, uint32_t arrayType, - const std::shared_ptr ¤t) +void MacroGen::SetTypeDataUinit64( + const std::string &arrayName, uint32_t &arraySize, uint32_t arrayType, const std::shared_ptr ¤t) { if (arraySize != 0) { GenArray(arrayName, arraySize, arrayType, current); diff --git a/tools/hc-gen/src/macro_gen.h b/tools/hc-gen/src/macro_gen.h index 527f52fc..3f3a489b 100755 --- a/tools/hc-gen/src/macro_gen.h +++ b/tools/hc-gen/src/macro_gen.h @@ -9,10 +9,11 @@ #ifndef HC_GEN_MACRO_GEN_H #define HC_GEN_MACRO_GEN_H -#include "generator.h" #include #include +#include "generator.h" + namespace OHOS { namespace Hardware { class MacroGen : public Generator { @@ -53,6 +54,6 @@ private: std::string outFileName_; std::map nodeNameMap_; }; -} // Hardware -} // OHOS +} // namespace Hardware +} // namespace OHOS #endif // HC_GEN_MACRO_GEN_H diff --git a/tools/hc-gen/src/opcode.h b/tools/hc-gen/src/opcode.h index d082d190..b22d7731 100644 --- a/tools/hc-gen/src/opcode.h +++ b/tools/hc-gen/src/opcode.h @@ -11,6 +11,7 @@ #include #include + #include "ast.h" namespace OHOS { @@ -37,7 +38,7 @@ struct HcbHeader { int32_t totalSize; }; -} // OHOS -} // Hardware +} // namespace Hardware +} // namespace OHOS #endif // HC_GEN_OPCODE_H diff --git a/tools/hc-gen/src/option.cpp b/tools/hc-gen/src/option.cpp index 08aaa57a..c654fb56 100644 --- a/tools/hc-gen/src/option.cpp +++ b/tools/hc-gen/src/option.cpp @@ -6,13 +6,14 @@ * See the LICENSE file in the root of this repository for complete details. */ -#include "option.h" #include #include -#include #include -#include "logger.h" +#include + #include "file.h" +#include "logger.h" +#include "option.h" using namespace OHOS::Hardware; static constexpr int HCS_COMPILER_VERSION_MAJOR = 00; @@ -59,6 +60,9 @@ bool Option::ParseOptions(int argc, char **argv) while (op != OPTION_END) { op = getopt(argc, argv, HCS_SUPPORT_ARGS); SetOptionData(op); + if (ShouldShowUsage() || ShouldShowVersion()) { + return false; + } } return true; @@ -105,8 +109,6 @@ void Option::SetOptionData(char op) showUsage_ = true; break; case '?': - showUsage_ = true; - optionError_ = true; SetOptionError(); break; default: @@ -116,9 +118,9 @@ void Option::SetOptionData(char op) void Option::ShowUsage() { - Logger() << - "Usage: hc-gen [Options] [File]\n" << - "options:"; + Logger() << "Usage: hc-gen [Options] [File]\n" + << "options:"; + ShowOption("-o ", "output file name, default same as input"); ShowOption("-a", "hcb align with four bytes"); ShowOption("-b", "output binary output, default enable"); ShowOption("-t", "output config in C language source file style"); @@ -234,7 +236,7 @@ bool Option::SetSourceOption(const char *srcName) } sourceName_ = srcAbsPath; - sourceNameBase_ = Util::File::FileNameBase(srcAbsPath) ; + sourceNameBase_ = Util::File::FileNameBase(srcAbsPath); return true; } diff --git a/tools/hc-gen/src/option.h b/tools/hc-gen/src/option.h index 1446abda..fdebae5d 100644 --- a/tools/hc-gen/src/option.h +++ b/tools/hc-gen/src/option.h @@ -62,11 +62,12 @@ public: std::string GetSourceDir(); static std::string RealPathSourcePath(const char *path); + private: static void ShowOption(const std::string &option, const std::string &helpInfo); bool ParseOptions(int argc, char *argv[]); void SetOptionError(bool shouldShowUsage = true); - bool SetSourceOption(const char* srcName); + bool SetSourceOption(const char *srcName); bool showUsage_ = false; bool showVersion_ = false; @@ -86,6 +87,6 @@ private: void SetOptionData(char op); }; -} // Hardware -} // OHOS +} // namespace Hardware +} // namespace OHOS #endif // HC_GEN_OPTION_H diff --git a/tools/hc-gen/src/parser.cpp b/tools/hc-gen/src/parser.cpp index 2679cd8c..df6ff84b 100644 --- a/tools/hc-gen/src/parser.cpp +++ b/tools/hc-gen/src/parser.cpp @@ -6,10 +6,11 @@ * See the LICENSE file in the root of this repository for complete details. */ -#include "parser.h" #include + #include "file.h" #include "logger.h" +#include "parser.h" using namespace OHOS::Hardware; @@ -54,7 +55,6 @@ bool Parser::Parse() return true; } - std::shared_ptr Parser::ParseOne(const std::string &src, std::list &includeList) { if (!lexer_.Initialize(src)) { @@ -156,8 +156,8 @@ std::shared_ptr Parser::ParseNode(Token &name, bool bracesStart) child = ParseNodeAndTerm(); break; default: - Logger().Error() << lexer_ - << "syntax error, except '}' or TEMPLATE or LITERAL for node '" << name.strval << '\''; + Logger().Error() << lexer_ << "syntax error, except '}' or TEMPLATE or LITERAL for node '" + << name.strval << '\''; return nullptr; } if (child == nullptr) { @@ -180,7 +180,7 @@ std::shared_ptr Parser::ParseTerm(Token &name) Logger().Error() << lexer_ << "syntax error, miss value of config term"; return nullptr; } - auto term = std::shared_ptr(new(std::nothrow) ConfigTerm(name, nullptr)); + auto term = std::shared_ptr(new (std::nothrow) ConfigTerm(name, nullptr)); if (term == nullptr) { return nullptr; } diff --git a/tools/hc-gen/src/parser.h b/tools/hc-gen/src/parser.h index a3b055c8..c49e4373 100644 --- a/tools/hc-gen/src/parser.h +++ b/tools/hc-gen/src/parser.h @@ -10,13 +10,13 @@ #define HC_GEN_PARSER_H #include + #include "ast.h" #include "lexer.h" namespace OHOS { namespace Hardware { - class Parser { public: Parser() = default; @@ -58,6 +58,6 @@ private: std::list srcQueue_; }; -} // OHOS -} // Hardware +} // namespace Hardware +} // namespace OHOS #endif // HC_GEN_PARSER_H diff --git a/tools/hc-gen/src/text_gen.cpp b/tools/hc-gen/src/text_gen.cpp index cc39e4b8..349fecc3 100644 --- a/tools/hc-gen/src/text_gen.cpp +++ b/tools/hc-gen/src/text_gen.cpp @@ -7,6 +7,7 @@ */ #include + #include "file.h" #include "logger.h" #include "opcode.h" @@ -14,14 +15,12 @@ using namespace OHOS::Hardware; -constexpr static const char *FILE_HEAD_COMMENT = \ - "/*\n" \ - " * This is an automatically generated HDF config file. Do not modify it manually.\n" \ +constexpr static const char *FILE_HEAD_COMMENT = + "/*\n" + " * This is an automatically generated HDF config file. Do not modify it manually.\n" " */\n\n"; -TextGen::TextGen(std::shared_ptr ast) : Generator(ast) -{ -} +TextGen::TextGen(std::shared_ptr ast) : Generator(ast) {} bool TextGen::Output() { @@ -99,8 +98,8 @@ bool TextGen::HeaderOutputTraversal() return false; } - ofs_ << "const struct " << ToUpperCamelString(prefix_) << moduleName_ << "Root* HdfGet" - << moduleName_ << "ModuleConfigRoot(void);\n"; + ofs_ << "const struct " << ToUpperCamelString(prefix_) << moduleName_ << "Root* HdfGet" << moduleName_ + << "ModuleConfigRoot(void);\n"; return ofs_.good(); } @@ -122,8 +121,9 @@ bool TextGen::ImplOutput() return ret; } - ofs_ << "\nconst struct " << ToUpperCamelString(prefix_) << moduleName_ << "Root* HdfGet" << moduleName_ << "ModuleConfigRoot(void)\n" - << "{\n" + ofs_ << "\nconst struct " << ToUpperCamelString(prefix_) << moduleName_ << "Root* HdfGet" << moduleName_ + << "ModuleConfigRoot(void)\n" + << "{\n" << Indent() << "return &" << rootVariableName_ << ";\n" << "}\n"; @@ -248,7 +248,7 @@ std::string TextGen::GenConfigStructName(const std::shared_ptr &node) return ToUpperCamelString(prefix_).append(ToUpperCamelString(moduleName_)).append(ToUpperCamelString(node->Name())); } -bool TextGen::GenObjectDefinitionGen(const std::shared_ptr& object) +bool TextGen::GenObjectDefinitionGen(const std::shared_ptr &object) { if (!object->IsNode() && !object->IsTerm()) { return true; @@ -289,8 +289,8 @@ bool TextGen::GenTermDefinition(const std::shared_ptr &term) ofs_ << TAB << "const " << TypeToStr(array->ArrayType()) << "* " << term->Name() << ";\n"; ofs_ << TAB << "uint32_t " << term->Name() << "Size;\n"; } else { - ofs_ << TAB << TypeToStr(array->ArrayType()) << " " << term->Name() << "[" - << array->ArraySize() << "];\n"; + ofs_ << TAB << TypeToStr(array->ArrayType()) << " " << term->Name() << "[" << array->ArraySize() + << "];\n"; } break; } @@ -304,8 +304,7 @@ bool TextGen::GenTermDefinition(const std::shared_ptr &term) case PARSEROP_NODEREF: { auto structName = GenConfigStructName(ConfigTerm::CastFrom(term)->RefNode().lock()); ofs_ << TAB << "const struct " << structName << "* " << term->Name() << ";\n"; - } - break; + } break; default: break; } @@ -328,10 +327,10 @@ bool TextGen::IsInTemplate(const std::shared_ptr &object) const std::string &TextGen::TypeToStr(uint32_t type) { static std::map typeMap = { - {PARSEROP_UINT8, "uint8_t"}, - {PARSEROP_UINT16, "uint16_t"}, - {PARSEROP_UINT32, "uint32_t"}, - {PARSEROP_UINT64, "uint64_t"}, + {PARSEROP_UINT8, "uint8_t" }, + {PARSEROP_UINT16, "uint16_t" }, + {PARSEROP_UINT32, "uint32_t" }, + {PARSEROP_UINT64, "uint64_t" }, {PARSEROP_STRING, "const char*"}, }; return typeMap[type]; @@ -339,11 +338,11 @@ const std::string &TextGen::TypeToStr(uint32_t type) bool TextGen::OutputImplGlobalVariables() { - auto forwardWalkFunc = [this](const std::shared_ptr& current, int32_t depth) -> uint32_t { + auto forwardWalkFunc = [this](const std::shared_ptr ¤t, int32_t depth) -> uint32_t { return ImplementGenTraversal(current, depth); }; - auto backwardWalkFunc = [this](const std::shared_ptr& current, int32_t depth) -> uint32_t { + auto backwardWalkFunc = [this](const std::shared_ptr ¤t, int32_t depth) -> uint32_t { return ImplementCloseBraceGen(current, depth); }; @@ -468,8 +467,8 @@ std::string TextGen::GenTemplateVariableName(const std::shared_ptr &o } return node->TemplateSignNum() != 0 ? - std::string("g_").append(prefix_).append(name).append(std::to_string(node->TemplateSignNum())) : - std::string("g_").append(prefix_).append(name); + std::string("g_").append(prefix_).append(name).append(std::to_string(node->TemplateSignNum())) : + std::string("g_").append(prefix_).append(name); } std::shared_ptr TextGen::SymbolFind(const std::string &name) @@ -612,8 +611,7 @@ bool TextGen::OutputTemplateImpl() } return ast_->WalkBackward([this](const std::shared_ptr &object, int32_t) -> uint32_t { - if (!object->IsNode() || - (object->IsNode() && ConfigNode::CastFrom(object)->GetNodeType() != NODE_TEMPLATE)) { + if (!object->IsNode() || (object->IsNode() && ConfigNode::CastFrom(object)->GetNodeType() != NODE_TEMPLATE)) { return NOERR; } auto node = ConfigNode::CastFrom(object); @@ -621,8 +619,8 @@ bool TextGen::OutputTemplateImpl() return NOERR; } - ofs_ << "static const struct " << GenConfigStructName(object) << ' ' - << GenTemplateVariableName(object) << "[] = {\n"; + ofs_ << "static const struct " << GenConfigStructName(object) << ' ' << GenTemplateVariableName(object) + << "[] = {\n"; auto subClass = node->SubClasses(); for (auto nodeObj : subClass) { std::shared_ptr obj = std::shared_ptr(nodeObj, [](auto p) {}); @@ -644,7 +642,7 @@ bool TextGen::OutputTemplateVariablesDeclare() if (object->IsTerm() && object->Child()->IsArray()) { return ArrayVariablesDeclareGen(object); } else if (!object->IsNode() || - (object->IsNode() && ConfigNode::CastFrom(object)->GetNodeType() != NODE_TEMPLATE)) { + (object->IsNode() && ConfigNode::CastFrom(object)->GetNodeType() != NODE_TEMPLATE)) { return NOERR; } auto node = ConfigNode::CastFrom(object); @@ -669,7 +667,8 @@ uint32_t TextGen::ArrayVariablesDeclareGen(const std::shared_ptr &obj auto arrayName = GenArrayName(object); auto array = ConfigArray::CastFrom(object->Child()); ofs_ << "static const " << TypeToStr(array->ArrayType()) << ' ' << arrayName << '[' << array->ArraySize() - << "] = {\n" << Indent(); + << "] = {\n" + << Indent(); HcsPrintArrayContent(object->Child(), 1); ofs_ << "\n};\n\n"; @@ -685,7 +684,7 @@ std::string TextGen::GenArrayName(const std::shared_ptr &term) if (sym == nullptr) { SymbolAdd(arrayName, term); t->SetSigNum(1); - } else if (t->SigNum() == 0){ + } else if (t->SigNum() == 0) { t->SetSigNum(sym->duplicateCount + 1); sym->duplicateCount++; } @@ -698,12 +697,13 @@ uint32_t TextGen::TemplateVariableGen(const std::shared_ptr &nodeObje { auto child = nodeObject->Child(); while (child != nullptr) { - auto res = Ast::WalkRound(child, - [this](const std::shared_ptr& object, uint32_t depth) -> uint32_t { - return ObjectImplementGen(object, depth + 2); + auto res = Ast::WalkRound( + child, + [this](const std::shared_ptr &object, uint32_t depth) -> uint32_t { + return ObjectImplementGen(object, depth + 2); }, - [this](const std::shared_ptr& object, uint32_t depth) -> uint32_t { - return ImplementCloseBraceGen(object, depth + 2); + [this](const std::shared_ptr &object, uint32_t depth) -> uint32_t { + return ImplementCloseBraceGen(object, depth + 2); }); if (!res) { return false; @@ -717,7 +717,7 @@ bool TextGen::DuplicateNodeNameCheck() { std::map> nodeMap; - return ast_->WalkForward([&](const std::shared_ptr& current, uint32_t) -> uint32_t { + return ast_->WalkForward([&](const std::shared_ptr ¤t, uint32_t) -> uint32_t { if (!current->IsNode() || IsInSubClassNode(current)) { return NOERR; } @@ -729,7 +729,7 @@ bool TextGen::DuplicateNodeNameCheck() } Logger().Error() << current->SourceInfo() << "duplicate node name at " << node->second->SourceInfo() << "\n" - << "To avoid redefining structures, not allow duplicate node name at text config mode"; + << "To avoid redefining structures, not allow duplicate node name at text config mode"; return EFAIL; }); } diff --git a/tools/hc-gen/src/text_gen.h b/tools/hc-gen/src/text_gen.h index f0ed649d..e405f23c 100644 --- a/tools/hc-gen/src/text_gen.h +++ b/tools/hc-gen/src/text_gen.h @@ -12,6 +12,7 @@ #include #include #include + #include "generator.h" namespace OHOS { @@ -51,7 +52,7 @@ private: std::string GenConfigStructName(const std::shared_ptr &node); - bool GenObjectDefinitionGen(const std::shared_ptr& object); + bool GenObjectDefinitionGen(const std::shared_ptr &object); bool GenTermDefinition(const std::shared_ptr &term); @@ -65,7 +66,7 @@ private: uint32_t ImplementCloseBraceGen(const std::shared_ptr &object, int32_t depth); - static const std::string & Indent(uint32_t times = 1); + static const std::string &Indent(uint32_t times = 1); static bool IsInSubClassNode(const std::shared_ptr &object); @@ -84,7 +85,7 @@ private: std::shared_ptr SymbolFind(const std::string &name); - void SymbolAdd(const std::string &name, const std::shared_ptr& object); + void SymbolAdd(const std::string &name, const std::shared_ptr &object); std::ofstream ofs_; std::string outputFileName_; @@ -96,13 +97,13 @@ private: uint32_t PrintTermImplement(const std::shared_ptr &object, int32_t depth); - bool PrintBaseTypeValue(const std::shared_ptr& object); + bool PrintBaseTypeValue(const std::shared_ptr &object); uint32_t PrintArrayImplement(const std::shared_ptr &object, int32_t depth); bool PrintArrayImplInSubClass(const std::shared_ptr &object, int32_t depth); - bool HcsPrintArrayContent(const std::shared_ptr& object, uint32_t indent); + bool HcsPrintArrayContent(const std::shared_ptr &object, uint32_t indent); std::string HcsBuildObjectPath(std::shared_ptr object); @@ -110,13 +111,13 @@ private: bool OutputTemplateVariablesDeclare(); - uint32_t ArrayVariablesDeclareGen(const std::shared_ptr& object); + uint32_t ArrayVariablesDeclareGen(const std::shared_ptr &object); - std::string GenArrayName(const std::shared_ptr& term); + std::string GenArrayName(const std::shared_ptr &term); - uint32_t TemplateVariableGen(const std::shared_ptr& nodeObject); + uint32_t TemplateVariableGen(const std::shared_ptr &nodeObject); }; -} // Hardware -} // OHOS +} // namespace Hardware +} // namespace OHOS #endif // HC_GEN_TEXT_GENERATOR_H diff --git a/tools/hc-gen/src/token.cpp b/tools/hc-gen/src/token.cpp index 134444ff..6192cfd2 100644 --- a/tools/hc-gen/src/token.cpp +++ b/tools/hc-gen/src/token.cpp @@ -6,24 +6,25 @@ * See the LICENSE file in the root of this repository for complete details. */ -#include "token.h" #include #include +#include "token.h" + using namespace OHOS::Hardware; std::string OHOS::Hardware::TokenType2String(int32_t type) { static std::map tokenTypeMap = { - {NUMBER, "NUMBER"}, - {TEMPLATE, "TEMPLATE"}, - {LITERAL, "LITERAL"}, - {ROOT, "ROOT"}, - {INCLUDE, "INCLUDE"}, - {DELETE, "DELETE"}, - {STRING, "STRING"}, - {REF_PATH, "REF_PATH"}, - {FILE_PATH, "FILE_PATH"} + {NUMBER, "NUMBER" }, + {TEMPLATE, "TEMPLATE" }, + {LITERAL, "LITERAL" }, + {ROOT, "ROOT" }, + {INCLUDE, "INCLUDE" }, + {DELETE, "DELETE" }, + {STRING, "STRING" }, + {REF_PATH, "REF_PATH" }, + {FILE_PATH, "FILE_PATH"} }; std::string str; @@ -65,5 +66,3 @@ bool Token::operator!=(Token &t) const { return t.type != type || t.numval != numval || t.strval != strval; } - - diff --git a/tools/hc-gen/src/token.h b/tools/hc-gen/src/token.h index cd27c22f..3dbd772d 100644 --- a/tools/hc-gen/src/token.h +++ b/tools/hc-gen/src/token.h @@ -42,13 +42,11 @@ struct Token { friend std::ostream &operator<<(std::ostream &stream, const Token &t); }; -std::ostream& operator<<(std::ostream &s, const Token &t); +std::ostream &operator<<(std::ostream &s, const Token &t); std::string TokenType2String(int32_t type); -} // Hardware -} // OHOS - - +} // namespace Hardware +} // namespace OHOS #endif // HC_GEN_TOKEN_H diff --git a/tools/hc-gen/src/types.h b/tools/hc-gen/src/types.h index 5cdfc79a..031b5d08 100644 --- a/tools/hc-gen/src/types.h +++ b/tools/hc-gen/src/types.h @@ -9,18 +9,18 @@ #ifndef HC_GEN_TYPES_H #define HC_GEN_TYPES_H -#define ALIGN_SIZE 4 -#define TAB " " -#define TAB_SIZE 4 +#define ALIGN_SIZE 4 +#define TAB " " +#define TAB_SIZE 4 #define OPCODE_BYTE_WIDTH 1 -#define BYTE_SIZE 1 -#define WORD_SIZE 2 +#define BYTE_SIZE 1 +#define WORD_SIZE 2 #define DWORD_SIZE 4 #define QWORD_SIZE 8 #define UNIX_SEPARATOR '/' -#define WIN_SEPARATOR '\\' +#define WIN_SEPARATOR '\\' #ifdef OS_WIN #define OS_SEPARATOR WIN_SEPARATOR @@ -29,16 +29,16 @@ #endif enum HcsErrorNo { - NOERR = 0, /* No error */ - EFAIL, /* Process fail */ - EOOM, /* Out of memory */ - EOPTION, /* Option error */ - EREOPENF, /* Reopen argument */ - EINVALF, /* Invalid file */ - EINVALARG, /* Invalid argument */ - EDECOMP, /* Decompile error */ - EOUTPUT, /* Output error */ - EASTWALKBREAK, /* Break ast walk */ + NOERR = 0, /* No error */ + EFAIL, /* Process fail */ + EOOM, /* Out of memory */ + EOPTION, /* Option error */ + EREOPENF, /* Reopen argument */ + EINVALF, /* Invalid file */ + EINVALARG, /* Invalid argument */ + EDECOMP, /* Decompile error */ + EOUTPUT, /* Output error */ + EASTWALKBREAK, /* Break ast walk */ }; #endif // HC_GEN_TYPES_H