1 // Copyright (C) 2017 The Android Open Source Project
2 //
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 #include <regex>
16 #include <set>
17 #include <string>
18 #include <vector>
19 
20 namespace abi_util {
21 
22 std::set<std::string> CollectAllExportedHeaders(
23     const std::vector<std::string> &exported_header_dirs);
24 
25 class VersionScriptParser {
26  public:
27 
28   enum LineScope {
29     global,
30     local,
31   };
32 
33   VersionScriptParser(const std::string &version_script,
34                       const std::string &arch,
35                       const std::string &api);
36   bool Parse();
37 
38   const std::set<std::string> &GetFunctions();
39 
40   const std::set<std::string> &GetGlobVars();
41 
42   const std::set<std::string> &GetFunctionRegexs();
43 
44   const std::set<std::string> &GetGlobVarRegexs();
45 
46  private:
47 
48   bool ParseInnerBlock(std::ifstream &symbol_ifstream);
49 
50   LineScope GetLineScope(std::string &line, LineScope scope);
51 
52   bool ParseSymbolLine(const std::string &line);
53 
54   bool SymbolInArchAndApiVersion(const std::string &line,
55                                  const std::string &arch, int api);
56 
57   bool SymbolExported(const std::string &line, const std::string &arch,
58                       int api);
59 
60   int ApiStrToInt(const std::string &api);
61 
62   void AddToVars(std::string &symbol);
63 
64   void AddToFunctions(std::string &symbol);
65 
66  private:
67   const std::string &version_script_;
68   const std::string &arch_;
69   std::set<std::string> functions_;
70   std::set<std::string> globvars_;
71   // Added to speed up version script parsing and linking.
72   std::set<std::string> function_regexs_;
73   std::set<std::string> globvar_regexs_;
74   int api_;
75 };
76 
FindAndReplace(const std::string & candidate_str,const std::string & find_str,const std::string & replace_str)77 inline std::string FindAndReplace(const std::string &candidate_str,
78                                   const std::string &find_str,
79                                   const std::string &replace_str) {
80   // Find all matches of find_str in candidate_str and return a new string with
81   // all the matches replaced with replace_str
82   std::regex match_expr(find_str);
83   return std::regex_replace(candidate_str, match_expr, replace_str);
84 }
85 
86 } // namespace abi_util
87