Files
drivers_framework/tools/hdi-gen/parser/module_parser.cpp
T
yue edde6b3659 代码告警清理
Signed-off-by: yue <yueyan4@huawei.com>
2021-09-19 17:56:08 +08:00

141 lines
3.8 KiB
C++
Executable File

/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
#include "parser/module_parser.h"
#include <queue>
#include "parser/parser.h"
#include "util/logger.h"
namespace OHOS {
namespace HDI {
AutoPtr<ASTModule> ModuleParser::Parse()
{
if (!ParserDependencies()) {
return nullptr;
}
if (!CompileFiles()) {
return nullptr;
}
return module_;
}
bool ModuleParser::ParserDependencies()
{
if (!ParserAllImports(option_.GetSourceFile())) {
Logger::E(g_tab, "Parsing all idl file failed.");
return false;
}
if (!CheckCircularReference()) {
Logger::E(g_tab, "has circle reference.");
return false;
}
return true;
}
bool ModuleParser::CompileFiles()
{
std::unique_ptr<Parser> parserPtr = std::make_unique<Parser>(option_, module_);
for (const auto& filePath : compileFiles_) {
if (!parserPtr->Parse(filePath)) {
Logger::E(g_tab, "parse %s failed", filePath.string());
return false;
}
}
return true;
}
bool ModuleParser::ParserAllImports(const String& rootFilePath)
{
std::unique_ptr<Parser> parserPtr = std::make_unique<Parser>(option_);
std::shared_ptr<FileDetail> fileInfo = nullptr;
if (!parserPtr->Parse(rootFilePath, fileInfo)) {
return false;
}
if (fileInfo == nullptr) {
return false;
}
sourceFiles_[fileInfo->GetFullName()] = fileInfo;
return ParserAllImportsRecursion(fileInfo);
}
bool ModuleParser::ParserAllImportsRecursion(const std::shared_ptr<FileDetail>& fileInfo)
{
for (const auto& importName : fileInfo->GetImports()) {
if (sourceFiles_.find(importName) != sourceFiles_.end()) {
continue;
}
String filePath = FileDetail::ImportsToPath(importName);
std::unique_ptr<Parser> parserPtr = std::make_unique<Parser>(option_);
std::shared_ptr<FileDetail> file = nullptr;
if (!parserPtr->Parse(filePath, file)) {
Logger::E(g_tab, "Parsing %s failed.", filePath.string());
return false;
}
if (file == nullptr) {
Logger::E(g_tab, "Parsing %s failed, generator filedetail is nullptr.", filePath.string());
return false;
}
sourceFiles_[file->GetFullName()] = file;
if (!ParserAllImportsRecursion(file)) {
Logger::E(g_tab, "Parsing %s file's import failed.", file->GetFilePath().string());
return false;
}
}
return true;
}
bool ModuleParser::CheckCircularReference()
{
std::queue<std::shared_ptr<FileDetail>> fileQueue;
for (const auto& filePair : sourceFiles_) {
std::shared_ptr<FileDetail> fileNode = filePair.second;
if (fileNode->GetImportSize() == 0) {
fileQueue.push(fileNode);
}
}
compileFiles_.clear();
while (!fileQueue.empty()) {
std::shared_ptr<FileDetail> importFile = fileQueue.front();
fileQueue.pop();
compileFiles_.push_back(importFile->GetFilePath());
for (const auto& filePair : sourceFiles_) {
std::shared_ptr<FileDetail> fileNode = filePair.second;
if (fileNode->GetImportSize() > 0) {
fileNode->DelImport(importFile->GetFullName());
if (fileNode->GetImportSize() == 0) {
fileQueue.push(fileNode);
}
}
}
}
if (compileFiles_.size() < sourceFiles_.size()) {
return false;
}
return true;
}
} // namespace HDI
} // namespace OHOS