2018-08-21 05:27:52 +00:00
|
|
|
/* Copyright 2017-2018 ccls Authors
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
==============================================================================*/
|
|
|
|
|
2018-10-29 04:21:21 +00:00
|
|
|
#include "utils.hh"
|
2017-02-17 09:57:44 +00:00
|
|
|
|
2018-05-27 19:24:56 +00:00
|
|
|
#include "log.hh"
|
2018-11-27 06:22:27 +00:00
|
|
|
#include "message_handler.hh"
|
|
|
|
#include "pipeline.hh"
|
2018-10-29 04:21:21 +00:00
|
|
|
#include "platform.hh"
|
2017-05-23 07:24:14 +00:00
|
|
|
|
2018-01-30 00:27:43 +00:00
|
|
|
#include <siphash.h>
|
2017-05-23 07:24:14 +00:00
|
|
|
|
2018-09-20 07:31:23 +00:00
|
|
|
#include <llvm/ADT/StringRef.h>
|
|
|
|
#include <llvm/Support/FileSystem.h>
|
|
|
|
#include <llvm/Support/Path.h>
|
2018-09-08 17:37:48 +00:00
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
#include <algorithm>
|
2018-03-31 18:32:28 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
2018-04-07 17:43:56 +00:00
|
|
|
#include <errno.h>
|
2017-05-09 01:21:21 +00:00
|
|
|
#include <functional>
|
2018-11-27 06:22:27 +00:00
|
|
|
#include <regex>
|
2018-08-09 17:08:14 +00:00
|
|
|
#include <string.h>
|
2017-04-08 22:54:36 +00:00
|
|
|
#include <unordered_map>
|
2017-02-17 09:57:44 +00:00
|
|
|
|
2019-03-10 08:27:50 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2018-10-28 17:49:31 +00:00
|
|
|
namespace ccls {
|
2018-11-27 06:22:27 +00:00
|
|
|
struct Matcher::Impl {
|
|
|
|
std::regex regex;
|
|
|
|
};
|
|
|
|
|
|
|
|
Matcher::Matcher(const std::string &pattern)
|
|
|
|
: impl(std::make_unique<Impl>()), pattern(pattern) {
|
|
|
|
impl->regex = std::regex(pattern, std::regex_constants::ECMAScript |
|
|
|
|
std::regex_constants::icase |
|
|
|
|
std::regex_constants::optimize);
|
|
|
|
}
|
|
|
|
|
|
|
|
Matcher::~Matcher() {}
|
|
|
|
|
|
|
|
bool Matcher::Matches(const std::string &text) const {
|
|
|
|
return std::regex_search(text, impl->regex, std::regex_constants::match_any);
|
|
|
|
}
|
|
|
|
|
|
|
|
GroupMatch::GroupMatch(const std::vector<std::string> &whitelist,
|
|
|
|
const std::vector<std::string> &blacklist) {
|
|
|
|
auto err = [](const std::string &pattern, const char *what) {
|
|
|
|
ShowMessageParam params;
|
|
|
|
params.type = MessageType::Error;
|
|
|
|
params.message =
|
|
|
|
"failed to parse EMCAScript regex " + pattern + " : " + what;
|
|
|
|
pipeline::Notify(window_showMessage, params);
|
|
|
|
};
|
|
|
|
for (const std::string &pattern : whitelist) {
|
|
|
|
try {
|
|
|
|
this->whitelist.emplace_back(pattern);
|
|
|
|
} catch (const std::exception &e) {
|
|
|
|
err(pattern, e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const std::string &pattern : blacklist) {
|
|
|
|
try {
|
|
|
|
this->blacklist.emplace_back(pattern);
|
|
|
|
} catch (const std::exception &e) {
|
|
|
|
err(pattern, e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GroupMatch::Matches(const std::string &text,
|
|
|
|
std::string *blacklist_pattern) const {
|
|
|
|
for (const Matcher &m : whitelist)
|
|
|
|
if (m.Matches(text))
|
|
|
|
return true;
|
|
|
|
for (const Matcher &m : blacklist)
|
|
|
|
if (m.Matches(text)) {
|
|
|
|
if (blacklist_pattern)
|
|
|
|
*blacklist_pattern = m.pattern;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-17 18:23:12 +00:00
|
|
|
uint64_t HashUsr(llvm::StringRef s) {
|
2018-01-13 19:39:06 +00:00
|
|
|
union {
|
|
|
|
uint64_t ret;
|
|
|
|
uint8_t out[8];
|
|
|
|
};
|
2018-01-14 21:18:12 +00:00
|
|
|
// k is an arbitrary key. Don't change it.
|
2018-01-13 19:39:06 +00:00
|
|
|
const uint8_t k[16] = {0xd0, 0xe5, 0x4d, 0x61, 0x74, 0x63, 0x68, 0x52,
|
|
|
|
0x61, 0x79, 0xea, 0x70, 0xca, 0x70, 0xf0, 0x0d};
|
2018-08-09 17:08:14 +00:00
|
|
|
(void)siphash(reinterpret_cast<const uint8_t *>(s.data()), s.size(), k, out,
|
|
|
|
8);
|
2018-01-13 19:39:06 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
std::string LowerPathIfInsensitive(const std::string &path) {
|
2017-05-22 07:14:11 +00:00
|
|
|
#if defined(_WIN32)
|
2018-03-31 18:32:28 +00:00
|
|
|
std::string ret = path;
|
2018-08-09 17:08:14 +00:00
|
|
|
for (char &c : ret)
|
2018-03-31 18:32:28 +00:00
|
|
|
c = tolower(c);
|
|
|
|
return ret;
|
|
|
|
#else
|
|
|
|
return path;
|
2017-05-22 07:14:11 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
void EnsureEndsInSlash(std::string &path) {
|
2017-05-21 19:51:15 +00:00
|
|
|
if (path.empty() || path[path.size() - 1] != '/')
|
|
|
|
path += '/';
|
|
|
|
}
|
|
|
|
|
2017-12-02 05:07:30 +00:00
|
|
|
std::string EscapeFileName(std::string path) {
|
2018-03-31 18:32:28 +00:00
|
|
|
bool slash = path.size() && path.back() == '/';
|
2019-02-21 15:46:20 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
std::replace(path.begin(), path.end(), ':', '@');
|
|
|
|
#endif
|
|
|
|
std::replace(path.begin(), path.end(), '/', '@');
|
2018-03-31 18:32:28 +00:00
|
|
|
if (slash)
|
|
|
|
path += '/';
|
2017-12-02 05:07:30 +00:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2018-09-22 08:37:00 +00:00
|
|
|
std::string ResolveIfRelative(const std::string &directory,
|
|
|
|
const std::string &path) {
|
|
|
|
if (sys::path::is_absolute(path))
|
|
|
|
return path;
|
|
|
|
SmallString<256> Ret;
|
|
|
|
sys::path::append(Ret, directory, path);
|
|
|
|
return NormalizePath(Ret.str());
|
|
|
|
}
|
|
|
|
|
2019-03-10 08:27:50 +00:00
|
|
|
std::string RealPath(const std::string &path) {
|
|
|
|
SmallString<256> buf;
|
|
|
|
sys::fs::real_path(path, buf);
|
|
|
|
return buf.empty() ? path : llvm::sys::path::convert_to_slash(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NormalizeFolder(std::string &path) {
|
|
|
|
for (auto &[root, real] : g_config->workspaceFolders)
|
|
|
|
if (real.size() && llvm::StringRef(path).startswith(real)) {
|
|
|
|
path = root + path.substr(real.size());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-20 07:31:23 +00:00
|
|
|
std::optional<int64_t> LastWriteTime(const std::string &path) {
|
|
|
|
sys::fs::file_status Status;
|
|
|
|
if (sys::fs::status(path, Status))
|
|
|
|
return {};
|
|
|
|
return sys::toTimeT(Status.getLastModificationTime());
|
|
|
|
}
|
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
std::optional<std::string> ReadContent(const std::string &filename) {
|
2018-03-31 18:32:28 +00:00
|
|
|
char buf[4096];
|
|
|
|
std::string ret;
|
2018-08-09 17:08:14 +00:00
|
|
|
FILE *f = fopen(filename.c_str(), "rb");
|
|
|
|
if (!f)
|
|
|
|
return {};
|
2018-03-31 18:32:28 +00:00
|
|
|
size_t n;
|
|
|
|
while ((n = fread(buf, 1, sizeof buf, f)) > 0)
|
|
|
|
ret.append(buf, n);
|
2018-04-22 17:01:44 +00:00
|
|
|
fclose(f);
|
2018-03-31 18:32:28 +00:00
|
|
|
return ret;
|
2017-04-17 07:06:01 +00:00
|
|
|
}
|
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
void WriteToFile(const std::string &filename, const std::string &content) {
|
|
|
|
FILE *f = fopen(filename.c_str(), "wb");
|
2018-05-13 16:52:19 +00:00
|
|
|
if (!f ||
|
|
|
|
(content.size() && fwrite(content.c_str(), content.size(), 1, f) != 1)) {
|
|
|
|
LOG_S(ERROR) << "failed to write to " << filename << ' ' << strerror(errno);
|
2018-01-04 17:33:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-03-31 18:32:28 +00:00
|
|
|
fclose(f);
|
2017-03-09 18:02:55 +00:00
|
|
|
}
|
2017-05-25 02:04:19 +00:00
|
|
|
|
2018-06-01 03:06:09 +00:00
|
|
|
// Find discontinous |search| in |content|.
|
|
|
|
// Return |found| and the count of skipped chars before found.
|
2018-08-09 17:08:14 +00:00
|
|
|
int ReverseSubseqMatch(std::string_view pat, std::string_view text,
|
2018-06-01 03:06:09 +00:00
|
|
|
int case_sensitivity) {
|
|
|
|
if (case_sensitivity == 1)
|
|
|
|
case_sensitivity = std::any_of(pat.begin(), pat.end(), isupper) ? 2 : 0;
|
|
|
|
int j = pat.size();
|
|
|
|
if (!j)
|
|
|
|
return text.size();
|
|
|
|
for (int i = text.size(); i--;)
|
|
|
|
if ((case_sensitivity ? text[i] == pat[j - 1]
|
|
|
|
: tolower(text[i]) == tolower(pat[j - 1])) &&
|
|
|
|
!--j)
|
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-03-10 03:45:37 +00:00
|
|
|
std::string GetDefaultResourceDirectory() { return CLANG_RESOURCE_DIRECTORY; }
|
|
|
|
} // namespace ccls
|