1 /* 2 * Copyright (c) 2024 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 OHOS_IDL_PREPROCESSOR_H 17 #define OHOS_IDL_PREPROCESSOR_H 18 19 #include <map> 20 #include <set> 21 #include <unordered_set> 22 #include <vector> 23 24 #include "lexer/lexer.h" 25 #include "util/options.h" 26 #include "util/string_helper.h" 27 28 namespace OHOS { 29 namespace Idl { 30 class FileDetail { 31 public: GetFullName()32 inline std::string GetFullName() const 33 { 34 Options &option = Options::GetInstance(); 35 if (option.GetInterfaceType() == InterfaceType::SA && option.GetLanguage() == Language::CPP) { 36 return filePath_; 37 } 38 return StringHelper::Format("%s.%s", packageName_.c_str(), fileName_.c_str()); 39 } 40 41 std::string Dump() const; 42 43 public: 44 std::string filePath_; 45 std::string fileName_; 46 std::string packageName_; 47 std::unordered_set<std::string> imports_; 48 }; 49 50 using FileDetailMap = std::map<std::string, FileDetail>; 51 52 class Preprocessor { 53 public: 54 // analyze idl files and return sorted ids files 55 static bool Preprocess(std::vector<FileDetail> &fileDetails); 56 57 static bool UnitPreprocess(FileDetailMap &fileDetails); 58 59 private: 60 static bool CheckAllFilesPath(const std::set<std::string> &sourceFiles); 61 62 static bool AnalyseImportInfo(std::set<std::string> sourceFiles, FileDetailMap &allFileDetails); 63 64 static bool ParseFileDetail(const std::string &sourceFile, FileDetail &info); 65 66 static bool ParsePackage(Lexer &lexer, FileDetail &info); 67 68 static bool ParseImports(Lexer &lexer, FileDetail &info); 69 70 static bool LoadOtherIdlFiles( 71 const FileDetail &ownerFileDetail, FileDetailMap &allFileDetails, std::set<std::string> &sourceFiles); 72 73 static bool CheckCircularReference(const FileDetailMap &allFileDetails, 74 std::vector<FileDetail> &compileSourceFiles); 75 76 static void PrintCyclefInfo(FileDetailMap &allFileDetails); 77 78 static void FindCycle(const std::string &curNode, FileDetailMap &allFiles, std::vector<std::string> &trace); 79 80 // check if the file path matches the package name 81 static bool CheckPackageName(const std::string &filePath, const std::string &packageName); 82 }; 83 } // namespace Idl 84 } // namespace OHOS 85 86 #endif // OHOS_IDL_PREPROCESSOR_H