1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "Utils.h"
18
19 #include <err.h>
20 #include <fts.h>
21 #include <string.h>
22 #include <unistd.h>
23
24 #include <sstream>
25 #include <string>
26 #include <vector>
27
28 #include <android-base/strings.h>
29
30 #include "DeclarationDatabase.h"
31
getWorkingDir()32 std::string getWorkingDir() {
33 char buf[PATH_MAX];
34 if (!getcwd(buf, sizeof(buf))) {
35 err(1, "getcwd failed");
36 }
37 return buf;
38 }
39
collectHeaders(const std::string & directory)40 std::vector<std::string> collectHeaders(const std::string& directory) {
41 std::vector<std::string> headers;
42
43 char* dir_argv[2] = { const_cast<char*>(directory.c_str()), nullptr };
44 std::unique_ptr<FTS, decltype(&fts_close)> fts(
45 fts_open(dir_argv, FTS_LOGICAL | FTS_NOCHDIR, nullptr), fts_close);
46
47 if (!fts) {
48 err(1, "failed to open directory '%s'", directory.c_str());
49 }
50
51 while (FTSENT* ent = fts_read(fts.get())) {
52 if (ent->fts_info & (FTS_D | FTS_DP)) {
53 continue;
54 }
55
56 if (!android::base::EndsWith(ent->fts_path, ".h")) {
57 continue;
58 }
59
60 headers.push_back(ent->fts_path);
61 }
62
63 return headers;
64 }
65
StripPrefix(llvm::StringRef string,llvm::StringRef prefix)66 llvm::StringRef StripPrefix(llvm::StringRef string, llvm::StringRef prefix) {
67 if (string.startswith(prefix)) {
68 return string.drop_front(prefix.size());
69 }
70 return string;
71 }
72