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 <set>
22 #include <string>
23 #include <vector>
24
25
26 namespace header_checker {
27 namespace utils {
28
29
ShouldSkipFile(llvm::StringRef & file_name)30 static bool ShouldSkipFile(llvm::StringRef &file_name) {
31 // Ignore swap files, hidden files, and hidden directories. Do not recurse
32 // into hidden directories either. We should also not look at source files.
33 // Many projects include source files in their exports.
34 return (file_name.empty() || file_name.startswith(".") ||
35 file_name.endswith(".swp") || file_name.endswith(".swo") ||
36 file_name.endswith("#") || file_name.endswith(".cpp") ||
37 file_name.endswith(".cc") || file_name.endswith(".c"));
38 }
39
GetCwd()40 static std::string GetCwd() {
41 llvm::SmallString<256> cwd;
42 if (llvm::sys::fs::current_path(cwd)) {
43 llvm::errs() << "ERROR: Failed to get current working directory\n";
44 ::exit(1);
45 }
46 return std::string(cwd);
47 }
48
ParseRootDirs(const std::vector<std::string> & args)49 RootDirs ParseRootDirs(const std::vector<std::string> &args) {
50 RootDirs root_dirs;
51 for (const std::string_view arg : args) {
52 std::string_view path;
53 std::string_view replacement;
54 size_t colon_index = arg.find(":");
55 if (colon_index != std::string_view::npos) {
56 path = arg.substr(0, colon_index);
57 replacement = arg.substr(colon_index + 1);
58 } else {
59 path = arg;
60 replacement = "";
61 }
62 llvm::SmallString<256> norm_replacement(replacement.begin(),
63 replacement.end());
64 llvm::sys::path::remove_dots(norm_replacement, /* remove_dot_dot = */ true);
65 root_dirs.emplace_back(NormalizePath(path, {}),
66 std::string(norm_replacement));
67 }
68 if (root_dirs.empty()) {
69 root_dirs.emplace_back(GetCwd(), "");
70 }
71 // Sort by length in descending order so that NormalizePath finds the longest
72 // matching root dir.
73 std::sort(root_dirs.begin(), root_dirs.end(),
74 [](RootDir &first, RootDir &second) {
75 return first.path.size() > second.path.size();
76 });
77 for (size_t index = 1; index < root_dirs.size(); index++) {
78 if (root_dirs[index - 1].path == root_dirs[index].path) {
79 llvm::errs() << "Duplicate root dir: " << root_dirs[index].path << "\n";
80 ::exit(1);
81 }
82 }
83 return root_dirs;
84 }
85
NormalizePath(std::string_view path,const RootDirs & root_dirs)86 std::string NormalizePath(std::string_view path, const RootDirs &root_dirs) {
87 llvm::SmallString<256> norm_path(path.begin(), path.end());
88 if (llvm::sys::fs::make_absolute(norm_path)) {
89 return "";
90 }
91 llvm::sys::path::remove_dots(norm_path, /* remove_dot_dot = */ true);
92 llvm::StringRef separator = llvm::sys::path::get_separator();
93 // Convert /root/dir/path to path.
94 for (const RootDir &root_dir : root_dirs) {
95 // llvm::sys::path::replace_path_prefix("AB", "A", "") returns "B", so do
96 // not use it.
97 if (!norm_path.startswith(root_dir.path)) {
98 continue;
99 }
100 if (norm_path.size() == root_dir.path.size()) {
101 return root_dir.replacement;
102 }
103 llvm::StringRef suffix = norm_path.substr(root_dir.path.size());
104 if (suffix.startswith(separator)) {
105 if (root_dir.replacement.empty()) {
106 return suffix.substr(separator.size()).str();
107 }
108 // replacement == "/"
109 if (llvm::StringRef(root_dir.replacement).endswith(separator)) {
110 return root_dir.replacement + suffix.substr(separator.size()).str();
111 }
112 return root_dir.replacement + suffix.str();
113 }
114 }
115 return std::string(norm_path);
116 }
117
CollectExportedHeaderSet(const std::string & dir_name,std::set<std::string> * exported_headers,const RootDirs & root_dirs)118 static bool CollectExportedHeaderSet(const std::string &dir_name,
119 std::set<std::string> *exported_headers,
120 const RootDirs &root_dirs) {
121 std::error_code ec;
122 llvm::sys::fs::recursive_directory_iterator walker(dir_name, ec);
123 // Default construction - end of directory.
124 llvm::sys::fs::recursive_directory_iterator end;
125 for ( ; walker != end; walker.increment(ec)) {
126 if (ec) {
127 llvm::errs() << "Failed to walk directory: " << dir_name << ": "
128 << ec.message() << "\n";
129 return false;
130 }
131
132 const std::string &file_path = walker->path();
133
134 llvm::StringRef file_name(llvm::sys::path::filename(file_path));
135 // Ignore swap files and hidden files / dirs. Do not recurse into them too.
136 // We should also not look at source files. Many projects include source
137 // files in their exports.
138 if (ShouldSkipFile(file_name)) {
139 walker.no_push();
140 continue;
141 }
142
143 llvm::ErrorOr<llvm::sys::fs::basic_file_status> status = walker->status();
144 if (!status) {
145 llvm::errs() << "Failed to stat file: " << file_path << "\n";
146 return false;
147 }
148
149 if ((status->type() != llvm::sys::fs::file_type::symlink_file) &&
150 (status->type() != llvm::sys::fs::file_type::regular_file)) {
151 // Ignore non regular files, except symlinks.
152 continue;
153 }
154
155 exported_headers->insert(NormalizePath(file_path, root_dirs));
156 }
157 return true;
158 }
159
160 std::set<std::string>
CollectAllExportedHeaders(const std::vector<std::string> & exported_header_dirs,const RootDirs & root_dirs)161 CollectAllExportedHeaders(const std::vector<std::string> &exported_header_dirs,
162 const RootDirs &root_dirs) {
163 std::set<std::string> exported_headers;
164 for (auto &&dir : exported_header_dirs) {
165 if (!CollectExportedHeaderSet(dir, &exported_headers, root_dirs)) {
166 llvm::errs() << "Couldn't collect exported headers\n";
167 ::exit(1);
168 }
169 }
170 return exported_headers;
171 }
172
173 } // namespace utils
174 } // namespace header_checker
175