1 // Copyright (C) 2021 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 "utils/source_path_utils.h"
16 
17 #include <llvm/Support/FileSystem.h>
18 #include <llvm/Support/Path.h>
19 #include <llvm/Support/raw_ostream.h>
20 
21 #include <algorithm>
22 #include <set>
23 #include <string>
24 #include <vector>
25 
26 namespace header_checker {
27 namespace utils {
28 
29 static const std::vector<std::string> header_extensions{
30     ".h", ".hh", ".hpp", ".hxx", ".h++", ".inl", ".inc", ".ipp", ".h.generic"};
31 
32 static const std::vector<std::string> libcxx_include_dir{"libcxx", "include"};
33 
HasHeaderExtension(llvm::StringRef file_name)34 static bool HasHeaderExtension(llvm::StringRef file_name) {
35   return std::find_if(header_extensions.begin(), header_extensions.end(),
36                       [file_name](const std::string &e) {
37                         return file_name.endswith(e);
38                       }) != header_extensions.end();
39 }
40 
PathEndsWith(llvm::StringRef path,const std::vector<std::string> & suffix)41 static bool PathEndsWith(llvm::StringRef path,
42                          const std::vector<std::string> &suffix) {
43   auto path_it = llvm::sys::path::rbegin(path);
44   auto suffix_it = suffix.rbegin();
45   while (suffix_it != suffix.rend()) {
46     if (path_it == llvm::sys::path::rend(path)) {
47       return false;
48     }
49     if (*path_it != *suffix_it) {
50       return false;
51     }
52     ++path_it;
53     ++suffix_it;
54   }
55   return true;
56 }
57 
GetCwd()58 static std::string GetCwd() {
59   llvm::SmallString<256> cwd;
60   if (llvm::sys::fs::current_path(cwd)) {
61     llvm::errs() << "ERROR: Failed to get current working directory\n";
62     ::exit(1);
63   }
64   return std::string(cwd);
65 }
66 
ParseRootDirs(const std::vector<std::string> & args)67 RootDirs ParseRootDirs(const std::vector<std::string> &args) {
68   RootDirs root_dirs;
69   for (const std::string_view arg : args) {
70     std::string_view path;
71     std::string_view replacement;
72     size_t colon_index = arg.find(":");
73     if (colon_index != std::string_view::npos) {
74       path = arg.substr(0, colon_index);
75       replacement = arg.substr(colon_index + 1);
76     } else {
77       path = arg;
78       replacement = "";
79     }
80     llvm::SmallString<256> norm_replacement(replacement.begin(),
81                                             replacement.end());
82     llvm::sys::path::remove_dots(norm_replacement, /* remove_dot_dot = */ true);
83     root_dirs.emplace_back(NormalizePath(path, {}),
84                            std::string(norm_replacement));
85   }
86   if (root_dirs.empty()) {
87     root_dirs.emplace_back(GetCwd(), "");
88   }
89   // Sort by length in descending order so that NormalizePath finds the longest
90   // matching root dir.
91   std::sort(root_dirs.begin(), root_dirs.end(),
92             [](RootDir &first, RootDir &second) {
93               return first.path.size() > second.path.size();
94             });
95   for (size_t index = 1; index < root_dirs.size(); index++) {
96     if (root_dirs[index - 1].path == root_dirs[index].path) {
97       llvm::errs() << "Duplicate root dir: " << root_dirs[index].path << "\n";
98       ::exit(1);
99     }
100   }
101   return root_dirs;
102 }
103 
NormalizePath(std::string_view path,const RootDirs & root_dirs)104 std::string NormalizePath(std::string_view path, const RootDirs &root_dirs) {
105   llvm::SmallString<256> norm_path(path.begin(), path.end());
106   if (llvm::sys::fs::make_absolute(norm_path)) {
107     return "";
108   }
109   llvm::sys::path::remove_dots(norm_path, /* remove_dot_dot = */ true);
110   llvm::StringRef separator = llvm::sys::path::get_separator();
111   // Convert /root/dir/path to path.
112   for (const RootDir &root_dir : root_dirs) {
113     // llvm::sys::path::replace_path_prefix("AB", "A", "") returns "B", so do
114     // not use it.
115     if (!norm_path.startswith(root_dir.path)) {
116       continue;
117     }
118     if (norm_path.size() == root_dir.path.size()) {
119       return root_dir.replacement;
120     }
121     llvm::StringRef suffix = norm_path.substr(root_dir.path.size());
122     if (suffix.startswith(separator)) {
123       if (root_dir.replacement.empty()) {
124         return suffix.substr(separator.size()).str();
125       }
126       // replacement == "/"
127       if (llvm::StringRef(root_dir.replacement).endswith(separator)) {
128         return root_dir.replacement + suffix.substr(separator.size()).str();
129       }
130       return root_dir.replacement + suffix.str();
131     }
132   }
133   return std::string(norm_path);
134 }
135 
CollectExportedHeaderSet(const std::string & dir_name,std::set<std::string> * exported_headers,const RootDirs & root_dirs)136 static bool CollectExportedHeaderSet(const std::string &dir_name,
137                                      std::set<std::string> *exported_headers,
138                                      const RootDirs &root_dirs) {
139   // Bazel creates temporary files in header directories. To avoid race
140   // condition, this function filters headers by name extensions.
141   // An exception is that libc++ headers do not have extensions.
142   bool collect_headers_without_extensions =
143       PathEndsWith(dir_name, libcxx_include_dir);
144   std::error_code ec;
145   llvm::sys::fs::recursive_directory_iterator walker(dir_name, ec);
146   // Default construction - end of directory.
147   llvm::sys::fs::recursive_directory_iterator end;
148   for ( ; walker != end; walker.increment(ec)) {
149     if (ec) {
150       llvm::errs() << "Failed to walk directory: " << dir_name << ": "
151                    << ec.message() << "\n";
152       return false;
153     }
154 
155     const std::string &file_path = walker->path();
156 
157     llvm::StringRef file_name(llvm::sys::path::filename(file_path));
158     if (file_name.empty() || file_name.startswith(".")) {
159       // Ignore hidden files and directories.
160       walker.no_push();
161       continue;
162     }
163     if (!file_name.contains(".")) {
164       if (!collect_headers_without_extensions) {
165         continue;
166       }
167     } else if (!HasHeaderExtension(file_name)) {
168       continue;
169     }
170 
171     llvm::ErrorOr<llvm::sys::fs::basic_file_status> status = walker->status();
172     if (!status) {
173       llvm::errs() << "Failed to stat file: " << file_path << "\n";
174       return false;
175     }
176 
177     if ((status->type() != llvm::sys::fs::file_type::symlink_file) &&
178         (status->type() != llvm::sys::fs::file_type::regular_file)) {
179       // Ignore non regular files, except symlinks.
180       continue;
181     }
182 
183     exported_headers->insert(NormalizePath(file_path, root_dirs));
184   }
185   return true;
186 }
187 
188 std::set<std::string>
CollectAllExportedHeaders(const std::vector<std::string> & exported_header_dirs,const RootDirs & root_dirs)189 CollectAllExportedHeaders(const std::vector<std::string> &exported_header_dirs,
190                           const RootDirs &root_dirs) {
191   std::set<std::string> exported_headers;
192   for (auto &&dir : exported_header_dirs) {
193     if (!CollectExportedHeaderSet(dir, &exported_headers, root_dirs)) {
194       llvm::errs() << "Couldn't collect exported headers\n";
195       ::exit(1);
196     }
197   }
198   return exported_headers;
199 }
200 
201 }  // namespace utils
202 }  // namespace header_checker
203