• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2024-2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ES2PANDA_TEST_UTILS_CHECKER_TEST_H
17 #define ES2PANDA_TEST_UTILS_CHECKER_TEST_H
18 
19 #include "compiler/core/compilerImpl.h"
20 #include "compiler/lowering/phase.h"
21 #include "panda_executable_path_getter.h"
22 #include "compiler/core/regSpiller.h"
23 #include "compiler/core/ETSemitter.h"
24 #include "checker/ETSAnalyzer.h"
25 #include "util/options.h"
26 #include "util/diagnosticEngine.h"
27 #include <gtest/gtest.h>
28 
29 namespace ir_alias = ark::es2panda::ir;
30 namespace checker_alias = ark::es2panda::checker;
31 namespace varbinder_alias = ark::es2panda::varbinder;
32 namespace plib_alias = ark::es2panda::public_lib;
33 namespace parser_alias = ark::es2panda::parser;
34 namespace compiler_alias = ark::es2panda::compiler;
35 namespace util_alias = ark::es2panda::util;
36 namespace test::utils {
37 
38 class CheckerTest : public testing::Test {
39 public:
CheckerTest()40     CheckerTest()
41         : allocator_(std::make_unique<ark::ArenaAllocator>(ark::SpaceType::SPACE_TYPE_COMPILER)),
42           publicContext_ {std::make_unique<plib_alias::Context>()},
43           program_ {parser_alias::Program::NewProgram<varbinder_alias::ETSBinder>(allocator_.get())},
44           es2pandaPath_ {PandaExecutablePathGetter::Get()[0]},
45           checker_(diagnosticEngine_)
46     {
47     }
48     ~CheckerTest() override = default;
SetUpTestCase()49     static void SetUpTestCase()
50     {
51         ark::mem::MemConfig::Initialize(0, 0, ark::es2panda::COMPILER_SIZE, 0, 0, 0);
52         ark::PoolManager::Initialize();
53     }
54 
Checker()55     checker_alias::ETSChecker *Checker()
56     {
57         return &checker_;
58     }
59 
Allocator()60     ark::ArenaAllocator *Allocator()
61     {
62         return allocator_.get();
63     }
64 
Program()65     parser_alias::Program *Program()
66     {
67         return &program_;
68     }
69     checker_alias::Type *FindClassType(varbinder_alias::ETSBinder *varbinder, std::string_view className);
70 
71     checker_alias::Type *FindTypeAlias(checker_alias::ETSChecker *checker, std::string_view aliasName);
72 
InitializeChecker(std::string_view fileName,std::string_view src)73     void InitializeChecker(std::string_view fileName, std::string_view src)
74     {
75         auto es2pandaPathPtr = es2pandaPath_.c_str();
76         ASSERT(es2pandaPathPtr);
77 
78         InitializeChecker<parser_alias::ETSParser, varbinder_alias::ETSBinder, checker_alias::ETSChecker,
79                           checker_alias::ETSAnalyzer, compiler_alias::ETSCompiler, compiler_alias::ETSGen,
80                           compiler_alias::StaticRegSpiller, compiler_alias::ETSFunctionEmitter,
81                           compiler_alias::ETSEmitter>(&es2pandaPathPtr, fileName, src, &checker_, &program_);
82     }
83 
84     template <typename Parser, typename VarBinder, typename Checker, typename Analyzer, typename AstCompiler,
85               typename CodeGen, typename RegSpiller, typename FunctionEmitter, typename Emitter>
InitializeChecker(char const * const * argv,std::string_view fileName,std::string_view src,checker_alias::ETSChecker * checker,parser_alias::Program * program)86     void InitializeChecker(char const *const *argv, std::string_view fileName, std::string_view src,
87                            checker_alias::ETSChecker *checker, parser_alias::Program *program)
88     {
89         // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
90         auto options = std::make_unique<util_alias::Options>(argv[0], diagnosticEngine_);
91         if (!options->Parse(ark::Span(argv, 1))) {
92             return;
93         }
94 
95         ark::Logger::ComponentMask mask {};
96         mask.set(ark::Logger::Component::ES2PANDA);
97         ark::Logger::InitializeStdLogging(options->LogLevel(), mask);
98 
99         ark::es2panda::Compiler compiler(options->GetExtension(), options->GetThread());
100         ark::es2panda::SourceFile input(fileName, src, options->IsModule());
101         compiler_alias::CompilationUnit unit {input, *options, 0, options->GetExtension(), diagnosticEngine_};
102         auto parser = Parser(program, unit.options, diagnosticEngine_,
103                              static_cast<parser_alias::ParserStatus>(unit.rawParserStatus));
104         parser.SetContext(publicContext_.get());
105         auto analyzer = Analyzer(checker);
106         checker->SetAnalyzer(&analyzer);
107 
108         auto *varbinder = program->VarBinder();
109         varbinder->SetProgram(program);
110 
111         varbinder->SetContext(publicContext_.get());
112 
113         auto emitter = Emitter(publicContext_.get());
114         auto phaseManager = compiler_alias::PhaseManager(unit.ext, allocator_.get());
115 
116         auto config = plib_alias::ConfigImpl {};
117         publicContext_->config = &config;
118         publicContext_->config->options = &unit.options;
119         publicContext_->sourceFile = &unit.input;
120         publicContext_->allocator = allocator_.get();
121         publicContext_->parser = &parser;
122         publicContext_->checker = checker;
123         publicContext_->analyzer = publicContext_->checker->GetAnalyzer();
124         publicContext_->emitter = &emitter;
125         publicContext_->parserProgram = program;
126         publicContext_->diagnosticEngine = &diagnosticEngine_;
127         publicContext_->phaseManager = &phaseManager;
128         parser.ParseScript(unit.input,
129                            unit.options.GetCompilationMode() == ark::es2panda::CompilationMode::GEN_STD_LIB);
130         while (auto phase = publicContext_->phaseManager->NextPhase()) {
131             if (!phase->Apply(publicContext_.get(), program)) {
132                 return;
133             }
134         }
135     }
136     NO_COPY_SEMANTIC(CheckerTest);
137     NO_MOVE_SEMANTIC(CheckerTest);
138 
139 private:
140     std::unique_ptr<ark::ArenaAllocator> allocator_;
141     std::unique_ptr<plib_alias::Context> publicContext_;
142     parser_alias::Program program_;
143     std::string es2pandaPath_;
144     util_alias::DiagnosticEngine diagnosticEngine_;
145     checker_alias::ETSChecker checker_;
146 };
147 
148 }  // namespace test::utils
149 
150 #endif  // ES2PANDA_TEST_UTILS_CHECKER_TEST_H
151