From 0ecdc3c6b5acb191f5208d085c4bec43228e4134 Mon Sep 17 00:00:00 2001 From: jiangkaiwen Date: Wed, 4 Jan 2023 10:58:39 +0800 Subject: [PATCH] Fix codecheck warnings of security compliance in master branch 1. Const decorated function and parameter 2. Names of parameter and argument are consistent 3. Use operators to complete string formatting Issue: I68OA8 Signed-off-by: jiangkaiwen Change-Id: Ic83ecfeb0be2fa391fecb99d37f668244d5e3216 --- es2panda/binder/variable.h | 2 +- es2panda/compiler/core/regAllocator.cpp | 4 +- es2panda/compiler/core/regAllocator.h | 4 +- es2panda/es2panda.cpp | 41 +++++++++------- es2panda/es2panda.h | 4 ++ .../ir/expressions/literals/bigIntLiteral.cpp | 2 +- es2panda/parser/parserImpl.cpp | 8 ++-- es2panda/parser/parserImpl.h | 4 +- es2panda/parser/transformer/transformer.h | 2 +- test262/run_sunspider.py | 47 +++++++++---------- test262/run_test262.py | 4 +- ts2panda/ts2abc/ts2abc.cpp | 9 ++-- ts2panda/ts2abc/ts2abc.h | 2 +- 13 files changed, 72 insertions(+), 61 deletions(-) diff --git a/es2panda/binder/variable.h b/es2panda/binder/variable.h index 65eb6a31fe..6ddcf184f2 100644 --- a/es2panda/binder/variable.h +++ b/es2panda/binder/variable.h @@ -49,7 +49,7 @@ public: NO_COPY_SEMANTIC(Variable); NO_MOVE_SEMANTIC(Variable); - VariableType virtual Type() const = 0; + virtual VariableType Type() const = 0; #define DECLARE_CHECKS_CASTS(variableType, className) \ bool Is##className() const \ diff --git a/es2panda/compiler/core/regAllocator.cpp b/es2panda/compiler/core/regAllocator.cpp index c6d9ac3537..871178f6fd 100644 --- a/es2panda/compiler/core/regAllocator.cpp +++ b/es2panda/compiler/core/regAllocator.cpp @@ -148,8 +148,8 @@ void RegAllocator::AdjustInsRegWhenHasSpill() pg_->SetInsns(newInsns); } -void RegAllocator::AdjustInsSpill(Span ®isters, IRNode *ins, ArenaList &newInsns, - std::vector ®sKind) +void RegAllocator::AdjustInsSpill(const Span ®isters, IRNode *ins, ArenaList &newInsns, + const std::vector ®sKind) { ASSERT(spillIndex_ == 0); ASSERT(!regsKind.empty()); diff --git a/es2panda/compiler/core/regAllocator.h b/es2panda/compiler/core/regAllocator.h index dd0f440c5f..984bab01e7 100644 --- a/es2panda/compiler/core/regAllocator.h +++ b/es2panda/compiler/core/regAllocator.h @@ -121,8 +121,8 @@ private: void Run(IRNode *ins); void Run(IRNode *ins, size_t argCount); void Run(IRNode *ins, int64_t typeIndex); - void AdjustInsSpill(Span ®isters, IRNode *ins, ArenaList &newInsns, - std::vector ®sKind); + void AdjustInsSpill(const Span ®isters, IRNode *ins, ArenaList &newInsns, + const std::vector ®sKind); void AdjustRangeInsSpill(Span ®isters, IRNode *ins, ArenaList &newInsns); template diff --git a/es2panda/es2panda.cpp b/es2panda/es2panda.cpp index 7c42974334..59a7576a15 100644 --- a/es2panda/es2panda.cpp +++ b/es2panda/es2panda.cpp @@ -24,7 +24,6 @@ #include #include #include -#include #include @@ -74,14 +73,7 @@ panda::pandasm::Program *Compiler::Compile(const SourceFile &input, const Compil return createJsonContentProgram(src, rname); } - bool needDumpSymbolFile = !options.hotfixOptions.dumpSymbolTable.empty(); - bool needGeneratePatch = options.hotfixOptions.generatePatch && !options.hotfixOptions.symbolTable.empty(); - util::Hotfix *hotfixHelper = nullptr; - if (symbolTable && (needDumpSymbolFile || needGeneratePatch)) { - hotfixHelper = new util::Hotfix(needDumpSymbolFile, needGeneratePatch, input.recordName, symbolTable); - parser_->AddHotfixHelper(hotfixHelper); - compiler_->AddHotfixHelper(hotfixHelper); - } + auto *hotfixHelper = InitHotfixHelper(input, options, symbolTable); try { auto ast = parser_->Parse(fname, src, rname, kind); @@ -110,22 +102,37 @@ panda::pandasm::Program *Compiler::Compile(const SourceFile &input, const Compil sourcefile : options.debugInfoSourceFile; auto *prog = compiler_->Compile(&ast, options, debugInfoSourceFile, pkgName); - if (hotfixHelper) { - delete hotfixHelper; - hotfixHelper = nullptr; - } + CleanHotfixHelper(hotfixHelper); return prog; } catch (const class Error &e) { error_ = e; - if (hotfixHelper) { - delete hotfixHelper; - hotfixHelper = nullptr; - } + CleanHotfixHelper(hotfixHelper); return nullptr; } } +util::Hotfix *Compiler::InitHotfixHelper(const SourceFile &input, const CompilerOptions &options, util::SymbolTable *symbolTable) +{ + bool needDumpSymbolFile = !options.hotfixOptions.dumpSymbolTable.empty(); + bool needGeneratePatch = options.hotfixOptions.generatePatch && !options.hotfixOptions.symbolTable.empty(); + util::Hotfix *hotfixHelper = nullptr; + if (symbolTable && (needDumpSymbolFile || needGeneratePatch)) { + hotfixHelper = new util::Hotfix(needDumpSymbolFile, needGeneratePatch, input.recordName, symbolTable); + parser_->AddHotfixHelper(hotfixHelper); + compiler_->AddHotfixHelper(hotfixHelper); + } + return hotfixHelper; +} + +void Compiler::CleanHotfixHelper(const util::Hotfix *hotfixHelper) +{ + if (hotfixHelper) { + delete hotfixHelper; + hotfixHelper = nullptr; + } +} + void Compiler::DumpAsm(const panda::pandasm::Program *prog) { compiler::CompilerImpl::DumpAsm(prog); diff --git a/es2panda/es2panda.h b/es2panda/es2panda.h index 8ac8084008..614bb3f2af 100644 --- a/es2panda/es2panda.h +++ b/es2panda/es2panda.h @@ -18,6 +18,7 @@ #include #include +#include #include #include @@ -197,6 +198,9 @@ public: } private: + util::Hotfix *InitHotfixHelper(const SourceFile &input, const CompilerOptions &options, util::SymbolTable *symbolTable); + static void CleanHotfixHelper(const util::Hotfix *hotfixHelper); + parser::ParserImpl *parser_; compiler::CompilerImpl *compiler_; std::unique_ptr transformer_ {nullptr}; diff --git a/es2panda/ir/expressions/literals/bigIntLiteral.cpp b/es2panda/ir/expressions/literals/bigIntLiteral.cpp index 0a274fa6a2..47251cae62 100644 --- a/es2panda/ir/expressions/literals/bigIntLiteral.cpp +++ b/es2panda/ir/expressions/literals/bigIntLiteral.cpp @@ -30,7 +30,7 @@ void BigIntLiteral::Dump(ir::AstDumper *dumper) const void BigIntLiteral::Compile(compiler::PandaGen *pg) const { - util::StringView bigIntValue = src_.Substr(0, src_.Length()-1); + util::StringView bigIntValue = src_.Substr(0, src_.Length() - 1); pg->LoadAccumulatorBigInt(this, bigIntValue); } diff --git a/es2panda/parser/parserImpl.cpp b/es2panda/parser/parserImpl.cpp index cce58af4b9..a245d55f62 100644 --- a/es2panda/parser/parserImpl.cpp +++ b/es2panda/parser/parserImpl.cpp @@ -866,7 +866,7 @@ bool ParserImpl::IsTSNamedTupleMember() return isNamedMember; } -void ParserImpl::HandleRestType(ir::AstNodeType elementType, bool *hasRestType) +void ParserImpl::HandleRestType(ir::AstNodeType elementType, bool *hasRestType) const { if (elementType == ir::AstNodeType::TS_ARRAY_TYPE && *hasRestType) { ThrowSyntaxError("A rest element cannot follow another rest element"); @@ -878,7 +878,6 @@ ir::Expression *ParserImpl::ParseTsTupleElement(ir::TSTupleKind *kind, bool *see { lexer::SourcePosition startPos = lexer_->GetToken().Start(); ir::Expression *element = nullptr; - bool isOptional = false; bool isRestType = false; TypeAnnotationParsingOptions options = TypeAnnotationParsingOptions::THROW_ERROR; @@ -897,6 +896,7 @@ ir::Expression *ParserImpl::ParseTsTupleElement(ir::TSTupleKind *kind, bool *see elementIdent->SetRange(lexer_->GetToken().Loc()); lexer_->NextToken(); // eat identifier + bool isOptional = false; if (lexer_->GetToken().Type() == lexer::TokenType::PUNCTUATOR_QUESTION_MARK) { lexer_->NextToken(); // eat '?' isOptional = true; @@ -939,7 +939,6 @@ ir::Expression *ParserImpl::ParseTsTupleElement(ir::TSTupleKind *kind, bool *see element = AllocNode(std::move(element)); element->SetRange({elementStartPos, lexer_->GetToken().End()}); lexer_->NextToken(); // eat '?' - isOptional = true; *seenOptional = true; } else if (*seenOptional && !isRestType) { ThrowSyntaxError("A required element cannot follow an optional element"); @@ -3101,6 +3100,7 @@ ArenaVector ParserImpl::ParseFunctionParams(bool isDeclare, if (context_.Status() & ParserStatus::IN_METHOD_DEFINITION) { auto decorators = ParseDecorators(); if (!decorators.empty()) { + ASSERT(paramDecorators != nullptr); paramDecorators->push_back({index, std::move(decorators)}); } } @@ -3520,7 +3520,7 @@ ir::Expression *ParserImpl::ParseFunctionParameter(bool isDeclare) return functionParameter; } -void ParserImpl::ValidateLvalueAssignmentTarget(ir::Expression *node) +void ParserImpl::ValidateLvalueAssignmentTarget(ir::Expression *node) const { switch (node->Type()) { case ir::AstNodeType::IDENTIFIER: { diff --git a/es2panda/parser/parserImpl.h b/es2panda/parser/parserImpl.h index 03288da0b3..cd12f818eb 100644 --- a/es2panda/parser/parserImpl.h +++ b/es2panda/parser/parserImpl.h @@ -261,7 +261,7 @@ private: ir::Expression *ParseTsQualifiedReference(ir::Expression *typeName); ir::Expression *ParseTsTypeReferenceOrQuery(bool parseQuery = false); bool IsTSNamedTupleMember(); - void HandleRestType(ir::AstNodeType elementType, bool *hasRestType); + void HandleRestType(ir::AstNodeType elementType, bool *hasRestType) const; ir::Expression *ParseTsTupleElement(ir::TSTupleKind *kind, bool *seenOptional, bool *hasRestType); ir::TSTupleType *ParseTsTupleType(); ir::TSImportType *ParseTsImportType(const lexer::SourcePosition &startLoc, bool isTypeof = false); @@ -404,7 +404,7 @@ private: ir::AstNode *ParseImportDefaultSpecifier(ArenaVector *specifiers); ir::AstNode *ParseImportSpecifiers(ArenaVector *specifiers); void ValidateAssignmentTarget(ExpressionParseFlags flags, ir::Expression *node); - void ValidateLvalueAssignmentTarget(ir::Expression *node); + void ValidateLvalueAssignmentTarget(ir::Expression *node) const; void ValidateArrowParameterBindings(const ir::Expression *node); ir::ExportDefaultDeclaration *ParseExportDefaultDeclaration(const lexer::SourcePosition &startLoc, diff --git a/es2panda/parser/transformer/transformer.h b/es2panda/parser/transformer/transformer.h index 9e90beb7f2..69ab3c21f7 100644 --- a/es2panda/parser/transformer/transformer.h +++ b/es2panda/parser/transformer/transformer.h @@ -115,7 +115,7 @@ private: ir::Expression *init = nullptr, bool needBinding = true); ir::CallExpression *CreateCallExpressionForTsModule(ir::TSModuleDeclaration *node, - util::StringView paramName, + util::StringView name, bool isExport = false); ir::Expression *CreateTsModuleParam(util::StringView paramName, bool isExport); ir::ExpressionStatement *CreateTsModuleAssignment(util::StringView name); diff --git a/test262/run_sunspider.py b/test262/run_sunspider.py index 77cdf53747..d3acda909b 100755 --- a/test262/run_sunspider.py +++ b/test262/run_sunspider.py @@ -227,19 +227,20 @@ class ArkProgram(): self.arch_root = self.args.ark_arch_root - def check_compile_mod(self, dependency): - with open(dependency, 'r', encoding='utf-8') as f: - context_file = f.read() + def check_compile_mode(self, file): + with open(file, 'r', encoding='utf-8') as check_file: + content_file = check_file.read() module_pattern = '((?:export|import)\s+(?:{[\s\S]+}|\*))|' module_pattern += '(export\s+(?:let|const|var|function|class|default))|' module_pattern += '(import\s+[\'\"].+[\'\"])' - module_mode_list = re.findall(module_pattern, context_file) + module_mode_list = re.findall(module_pattern, content_file) - for module_mode in list(set(module_mode_list)): - if len(module_mode[0]) != 0 or len(module_mode[1]) != 0 or \ - len(module_mode[2]) != 0: - return True - if "flags: [module]" in context_file or "/language/module-code/" in self.js_file: + for module_mode in list(set(module_mode_list)): + if len(module_mode[0]) != 0 or len(module_mode[1]) != 0 or \ + len(module_mode[2]) != 0: + return True + + if "flags: [module]" in content_file or "/language/module-code/" in self.js_file: return True return False @@ -260,7 +261,7 @@ class ArkProgram(): frontend_tool = self.ark_frontend_binary merge_abc_binary = self.args.merge_abc_binary merge_abc_mode = self.merge_abc_mode - compile_as_module = self.check_compile_mod(dependency) + compile_as_module = self.check_compile_mode(dependency) if self.ark_frontend == ARK_FRONTEND_LIST[0]: if merge_abc_mode != "0": @@ -297,9 +298,8 @@ class ArkProgram(): file_dir = os.path.split(self.js_file)[0] is_apart_abc_existed = os.path.exists(file_dir + "/" + output_abc) dependency_file_prefix = os.path.basename(dependency)[:-3] - dependency_bin_file = file_dir + "/" + \ - ".".join([dependency_file_prefix, - PROTO_BIN_SUFFIX]) + dependency_bin_file = '%s/%s.%s' % (file_dir, + dependency_file_prefix, PROTO_BIN_SUFFIX) cmd_args = [merge_abc_binary, '--input', dependency_bin_file, '--suffix', PROTO_BIN_SUFFIX, '--outputFilePath', file_dir, '--output', output_abc] @@ -320,9 +320,8 @@ class ArkProgram(): for dependency in list(set(dependencies)): dependency_file_prefix = os.path.basename(dependency)[:-3] - dependency_bin_file = file_dir + "/" + \ - ".".join([dependency_file_prefix, - PROTO_BIN_SUFFIX]) + dependency_bin_file = '%s/%s.%s' % (file_dir, + dependency_file_prefix, PROTO_BIN_SUFFIX) # test262 report syntax error cases if not os.path.exists(dependency_bin_file): generate_merged_abc = False @@ -372,13 +371,13 @@ class ArkProgram(): if (file_name in self.module_list or file_name in self.dynamicImport_list): search_dir = os.path.dirname(js_file.replace(BASE_OUT_DIR, DATA_DIR)) dependencies = collect_module_dependencies(js_file, search_dir, []) - compile_as_module = self.check_compile_mod(js_file) + compile_as_module = self.check_compile_mode(js_file) if (self.ark_frontend == ARK_FRONTEND_LIST[1]): if list(set(dependencies)): for dependency in list(set(dependencies)): dependency_file = os.path.basename(dependency) dependency_name = os.path.splitext(dependency_file)[0] - out_dependency_pre = file_dir + "/" + dependency_name + out_dependency_pre = '%s/%s' % (file_dir, dependency_name) out_dependency_proto = f"{out_dependency_pre}.protoBin" is_dependency_proto_existed = os.path.exists(out_dependency_proto) if not is_dependency_proto_existed: @@ -476,7 +475,7 @@ class ArkProgram(): print_command(cmd_args) def execute_aot(self): - unForceGc = False + unforce_gc = False os.environ["LD_LIBRARY_PATH"] = self.libs_dir file_name_pre = os.path.splitext(self.js_file)[0] cmd_args = [] @@ -498,9 +497,9 @@ class ArkProgram(): f'{file_name_pre}.abc'] elif self.arch == ARK_ARCH_LIST[0]: if file_name_pre in FORCE_GC_SKIP_TESTS: - unForceGc = True + unforce_gc = True asm_arg1 = "--enable-force-gc=true" - if unForceGc: + if unforce_gc: asm_arg1 = "--enable-force-gc=false" cmd_args = [self.ark_tool, ICU_PATH, asm_arg1, f'--aot-file={file_name_pre}', @@ -514,7 +513,7 @@ class ArkProgram(): return retcode def execute(self): - unForceGc = False + unforce_gc = False if platform.system() == "Windows" : #add env path for cmd/powershell execute libs_dir = self.libs_dir.replace(":", ";") @@ -542,9 +541,9 @@ class ArkProgram(): f'{file_name_pre}.abc'] elif self.arch == ARK_ARCH_LIST[0]: if file_name_pre in FORCE_GC_SKIP_TESTS: - unForceGc = True + unforce_gc = True asm_arg1 = "--enable-force-gc=true" - if unForceGc: + if unforce_gc: asm_arg1 = "--enable-force-gc=false" cmd_args = [self.ark_tool, ICU_PATH, asm_arg1, f'{file_name_pre}.abc'] diff --git a/test262/run_test262.py b/test262/run_test262.py index 39569b3c9f..58dfc7d03c 100755 --- a/test262/run_test262.py +++ b/test262/run_test262.py @@ -318,8 +318,8 @@ class TestPrepare(): for file_name in file_names: with open(file_name, 'r', encoding='utf-8') as file: - content = file.read() - if esid in content: + file_content = file.read() + if esid in file_content: files.append(file_name.split(origin_dir)[1]) return files diff --git a/ts2panda/ts2abc/ts2abc.cpp b/ts2panda/ts2abc/ts2abc.cpp index 81b6348936..340d8a1b51 100644 --- a/ts2panda/ts2abc/ts2abc.cpp +++ b/ts2panda/ts2abc/ts2abc.cpp @@ -1099,7 +1099,8 @@ static void ParseInputJsonFileContent(const Json::Value &rootValue, panda::panda auto inputJsonFileContentField = panda::pandasm::Field(LANG_EXT); inputJsonFileContentField.name = "jsonFileContent"; inputJsonFileContentField.type = panda::pandasm::Type("u32", 0); - inputJsonFileContentField.metadata->SetValue(panda::pandasm::ScalarValue::Create( + inputJsonFileContentField.metadata->SetValue( + panda::pandasm::ScalarValue::Create( static_cast(rootValue["ijfc"].asString()))); rec.field_list.emplace_back(std::move(inputJsonFileContentField)); } @@ -1490,7 +1491,7 @@ static bool EmitProgram(const std::string &output, int optLevel, std::string opt return true; } -static bool EmitAndRestoreProgram(panda::pandasm::Program &prog, panda::ts2abc::Options options) +static bool EmitAndRestoreProgram(panda::pandasm::Program &prog, const panda::ts2abc::Options &options) { if (!EmitProgram(g_outputFileName, options.GetOptLevelArg(), options.GetOptLogLevelArg(), prog)) { std::cerr << "fail to emit porgram " << g_outputFileName << " in HandleBuffer" << std::endl; @@ -1502,7 +1503,7 @@ static bool EmitAndRestoreProgram(panda::pandasm::Program &prog, panda::ts2abc:: } static bool HandleBuffer(const int &ret, char *buff, std::string &data, panda::pandasm::Program &prog, - panda::ts2abc::Options options) + const panda::ts2abc::Options &options) { uint32_t startPos = 0; if (options.IsMultiProgramsPipe() && ((buff[0] == '*' && data.back() != '#') || @@ -1576,7 +1577,7 @@ static bool ReadFromPipe(panda::pandasm::Program &prog, panda::ts2abc::Options o return true; } -bool GenerateProgramsFromPipe(panda::ts2abc::Options options) +bool GenerateProgramsFromPipe(const panda::ts2abc::Options &options) { panda::pandasm::Program prog = panda::pandasm::Program(); prog.lang = panda::pandasm::extensions::Language::ECMASCRIPT; diff --git a/ts2panda/ts2abc/ts2abc.h b/ts2panda/ts2abc/ts2abc.h index e1d9bf08e6..c64d63fb98 100644 --- a/ts2panda/ts2abc/ts2abc.h +++ b/ts2panda/ts2abc/ts2abc.h @@ -53,7 +53,7 @@ enum class OptLevel { bool HandleJsonFile(const std::string &input, std::string &data); bool GenerateProgram(const std::string &data, const std::string &output, panda::ts2abc::Options options); -bool GenerateProgramsFromPipe(panda::ts2abc::Options options); +bool GenerateProgramsFromPipe(const panda::ts2abc::Options &options); bool CompileNpmEntries(const std::string &input, const std::string &output); bool GetDebugLog(); void ParseLogEnable(const Json::Value &rootValue); -- Gitee