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 #ifndef HEADER_ABI_UTIL_H_
16 #define HEADER_ABI_UTIL_H_
17
18 #include <map>
19 #include <regex>
20 #include <set>
21 #include <string>
22 #include <vector>
23
24
25 namespace header_checker {
26 namespace utils {
27
28
29 std::string GetCwd();
30
31 // Resolve '..' and '.'; if the path starts with root_dir, remove the prefix;
32 // don't resolve symbolic links.
33 std::string NormalizePath(const std::string &path, const std::string &root_dir);
34
35 std::set<std::string>
36 CollectAllExportedHeaders(const std::vector<std::string> &exported_header_dirs,
37 const std::string &root_dir);
38
FindAndReplace(const std::string & candidate_str,const std::string & find_str,const std::string & replace_str)39 inline std::string FindAndReplace(const std::string &candidate_str,
40 const std::string &find_str,
41 const std::string &replace_str) {
42 // Find all matches of find_str in candidate_str and return a new string with
43 // all the matches replaced with replace_str
44 std::regex match_expr(find_str);
45 return std::regex_replace(candidate_str, match_expr, replace_str);
46 }
47
48 template <typename T, typename K>
FindRemovedElements(const std::map<K,T> & old_elements_map,const std::map<K,T> & new_elements_map)49 std::vector<T> FindRemovedElements(
50 const std::map<K, T> &old_elements_map,
51 const std::map<K, T> &new_elements_map) {
52 std::vector<T> removed_elements;
53 for (auto &&map_element : old_elements_map) {
54 auto element_key = map_element.first;
55 auto new_element = new_elements_map.find(element_key);
56 if (new_element == new_elements_map.end()) {
57 removed_elements.emplace_back(map_element.second);
58 }
59 }
60 return removed_elements;
61 }
62
63 template <typename K, typename T, typename Iterable, typename KeyGetter,
64 typename ValueGetter>
AddToMap(std::map<K,T> * dst,Iterable & src,KeyGetter get_key,ValueGetter get_value)65 inline void AddToMap(std::map<K, T> *dst, Iterable &src, KeyGetter get_key,
66 ValueGetter get_value) {
67 for (auto &&element : src) {
68 dst->insert(std::make_pair(get_key(&element), get_value(&element)));
69 }
70 }
71
72 template <typename K, typename Iterable, typename KeyGetter>
AddToSet(std::set<K> * dst,Iterable & src,KeyGetter get_key)73 inline void AddToSet(std::set<K> *dst, Iterable &src, KeyGetter get_key) {
74 for (auto &&element : src) {
75 dst->insert(get_key(element));
76 }
77 }
78
79 template <typename K, typename T>
FindCommonElements(const std::map<K,T> & old_elements_map,const std::map<K,T> & new_elements_map)80 std::vector<std::pair<T, T>> FindCommonElements(
81 const std::map<K, T> &old_elements_map,
82 const std::map<K, T> &new_elements_map) {
83 std::vector<std::pair<T, T>> common_elements;
84 typename std::map<K, T>::const_iterator old_element =
85 old_elements_map.begin();
86 typename std::map<K, T>::const_iterator new_element =
87 new_elements_map.begin();
88 while (old_element != old_elements_map.end() &&
89 new_element != new_elements_map.end()) {
90 if (old_element->first == new_element->first) {
91 common_elements.emplace_back(std::make_pair(
92 old_element->second, new_element->second));
93 old_element++;
94 new_element++;
95 continue;
96 }
97 if (old_element->first < new_element->first) {
98 old_element++;
99 } else {
100 new_element++;
101 }
102 }
103 return common_elements;
104 }
105
106
107 } // namespace utils
108 } // namespace header_checker
109
110
111 #endif // HEADER_ABI_UTIL_H_
112