1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * 4 * HDF is dual licensed: you can use it either under the terms of 5 * the GPL, or the BSD license, at your option. 6 * See the LICENSE file in the root of this repository for complete details. 7 */ 8 9 #ifndef OHOS_HDI_PREPROCESSOR_H 10 #define OHOS_HDI_PREPROCESSOR_H 11 12 #include <map> 13 #include <set> 14 #include <unordered_set> 15 #include <vector> 16 17 #include "lexer/lexer.h" 18 #include "util/string_helper.h" 19 20 namespace OHOS { 21 namespace HDI { 22 class FileDetail { 23 public: GetFullName()24 inline std::string GetFullName() const 25 { 26 return StringHelper::Format("%s.%s", packageName_.c_str(), fileName_.c_str()); 27 } 28 29 std::string Dump() const; 30 31 public: 32 std::string filePath_; 33 std::string fileName_; 34 std::string packageName_; 35 std::unordered_set<std::string> imports_; 36 }; 37 38 using FileDetailMap = std::map<std::string, FileDetail>; 39 40 class Preprocessor { 41 public: 42 // analyze idl files and return sorted ids files 43 static bool Preprocess(std::vector<FileDetail> &fileDetails); 44 45 static bool UnitPreprocess(FileDetailMap &fileDetails); 46 47 private: 48 static bool CheckAllFilesPath(const std::set<std::string> &sourceFiles); 49 50 static bool AnalyseImportInfo(std::set<std::string> sourceFiles, FileDetailMap &allFileDetails); 51 52 static bool ParseFileDetail(const std::string &sourceFile, FileDetail &info); 53 54 static bool ParsePackage(Lexer &lexer, FileDetail &info); 55 56 static bool ParseImports(Lexer &lexer, FileDetail &info); 57 58 static bool LoadOtherIdlFiles( 59 const FileDetail &ownerFileDetail, FileDetailMap &allFileDetails, std::set<std::string> &sourceFiles); 60 61 static bool CheckCircularReference(const FileDetailMap &allFileDetails, 62 std::vector<FileDetail> &compileSourceFiles); 63 64 static void PrintCyclefInfo(FileDetailMap &allFileDetails); 65 66 static void FindCycle(const std::string &curNode, FileDetailMap &allFiles, std::vector<std::string> &trace); 67 68 // check if the file path matches the package name 69 static bool CheckPackageName(const std::string &filePath, const std::string &packageName); 70 }; 71 } // namespace HDI 72 } // namespace OHOS 73 74 #endif // OHOS_HDI_PREPROCESSOR_H