|
| 1 | +/* |
| 2 | + * Copyright (c) 2015, Charlie Curtsinger and Emery Berger, |
| 3 | + * University of Massachusetts Amherst |
| 4 | + * This file is part of the Coz project. See LICENSE.md file at the top-level |
| 5 | + * directory of this distribution and at http://github.com/plasma-umass/coz. |
| 6 | + */ |
| 7 | + |
| 8 | +#ifndef COZ_PATH_FILTER_H |
| 9 | +#define COZ_PATH_FILTER_H |
| 10 | + |
| 11 | +#include <cstdlib> |
| 12 | +#include <string> |
| 13 | +#include <vector> |
| 14 | + |
| 15 | +inline bool path_has_prefix(const std::string& path, const std::string& prefix) { |
| 16 | + if(prefix.empty()) |
| 17 | + return false; |
| 18 | + if(prefix.size() > path.size()) |
| 19 | + return false; |
| 20 | + if(path.compare(0, prefix.size(), prefix) != 0) |
| 21 | + return false; |
| 22 | + return path.size() == prefix.size() || path[prefix.size()] == '/'; |
| 23 | +} |
| 24 | + |
| 25 | +inline bool is_system_path(const std::string& normalized) { |
| 26 | + static const std::vector<std::string> prefixes = { |
| 27 | + "/usr/include", |
| 28 | + "/usr/lib", |
| 29 | + "/usr/local/include", |
| 30 | + "/usr/local/lib", |
| 31 | + "/lib", |
| 32 | + "/lib64" |
| 33 | + }; |
| 34 | + for(const auto& prefix : prefixes) { |
| 35 | + if(path_has_prefix(normalized, prefix)) |
| 36 | + return true; |
| 37 | + } |
| 38 | + return false; |
| 39 | +} |
| 40 | + |
| 41 | +inline bool is_rust_path(const std::string& normalized) { |
| 42 | + // /rustc/<hash>/... paths are synthetic, always start with /rustc |
| 43 | + if(path_has_prefix(normalized, "/rustc")) |
| 44 | + return true; |
| 45 | + // .rustup and .cargo are only Rust toolchain paths when under $HOME |
| 46 | + const char* home = std::getenv("HOME"); |
| 47 | + if(home && home[0] != '\0') { |
| 48 | + std::string h(home); |
| 49 | + if(path_has_prefix(normalized, h + "/.rustup")) |
| 50 | + return true; |
| 51 | + if(path_has_prefix(normalized, h + "/.cargo/registry")) |
| 52 | + return true; |
| 53 | + if(path_has_prefix(normalized, h + "/.cargo/git")) |
| 54 | + return true; |
| 55 | + } |
| 56 | + return false; |
| 57 | +} |
| 58 | + |
| 59 | +inline bool is_coz_header(const std::string& path) { |
| 60 | + const std::string suffix = "/coz.h"; |
| 61 | + return path.size() >= suffix.size() && |
| 62 | + path.compare(path.size() - suffix.size(), suffix.size(), suffix) == 0; |
| 63 | +} |
| 64 | + |
| 65 | +#endif // COZ_PATH_FILTER_H |
0 commit comments