Reformat to Chromium style.

This commit is contained in:
Jacob Dufault 2017-09-21 18:14:57 -07:00
parent 1b1be28be4
commit f5314b62b1
58 changed files with 3677 additions and 2975 deletions

View File

@ -1,8 +1,9 @@
#include "cache.h"
#include "indexer.h"
#include "platform.h"
#include "language_server_api.h"
#include "platform.h"
#include <loguru/loguru.hpp>
@ -43,15 +44,15 @@ optional<std::string> LoadCachedFileContents(Config* config,
return ReadContent(GetCachedBaseFileName(config->cacheDirectory, filename));
}
void WriteToCache(Config* config,
IndexFile& file) {
void WriteToCache(Config* config, IndexFile& file) {
if (!config->enableCacheWrite)
return;
std::string cache_basename =
GetCachedBaseFileName(config->cacheDirectory, file.path);
LOG_IF_S(ERROR, file.file_contents_.empty()) << "Writing " << file.path << " to cache but it has no contents";
LOG_IF_S(ERROR, file.file_contents_.empty())
<< "Writing " << file.path << " to cache but it has no contents";
assert(!file.file_contents_.empty());
std::ofstream cache_content;

View File

@ -4,8 +4,8 @@
#include <memory>
#include <string>
using std::experimental::optional;
using std::experimental::nullopt;
using std::experimental::optional;
struct Config;
struct IndexFile;
@ -16,5 +16,4 @@ std::unique_ptr<IndexFile> LoadCachedIndex(Config* config,
optional<std::string> LoadCachedFileContents(Config* config,
const std::string& filename);
void WriteToCache(Config* config,
IndexFile& file);
void WriteToCache(Config* config, IndexFile& file);

View File

@ -15,7 +15,6 @@
#include <unistd.h>
*/
namespace {
#if false
@ -45,34 +44,37 @@ void EmitBacktrace() {
unsigned Flags() {
// TODO: use clang_defaultEditingTranslationUnitOptions()?
return
CXTranslationUnit_Incomplete |
CXTranslationUnit_KeepGoing |
return CXTranslationUnit_Incomplete | CXTranslationUnit_KeepGoing |
CXTranslationUnit_CacheCompletionResults |
CXTranslationUnit_PrecompiledPreamble |
CXTranslationUnit_IncludeBriefCommentsInCodeCompletion
#if !defined(_WIN32)
// For whatever reason, CreatePreambleOnFirstParse causes clang to become
// very crashy on windows.
// For whatever reason, CreatePreambleOnFirstParse causes clang to
// become very crashy on windows.
// TODO: do more investigation, submit fixes to clang.
| CXTranslationUnit_CreatePreambleOnFirstParse
#endif
;
}
int GetCompletionPriority(const CXCompletionString& str, CXCursorKind result_kind, const std::string& label) {
int GetCompletionPriority(const CXCompletionString& str,
CXCursorKind result_kind,
const std::string& label) {
int priority = clang_getCompletionPriority(str);
if (result_kind == CXCursor_Destructor) {
priority *= 100;
//std::cerr << "Bumping[destructor] " << ls_completion_item.label << std::endl;
// std::cerr << "Bumping[destructor] " << ls_completion_item.label <<
// std::endl;
}
if (result_kind == CXCursor_ConversionFunction ||
(result_kind == CXCursor_CXXMethod && StartsWith(label, "operator"))) {
//std::cerr << "Bumping[conversion] " << ls_completion_item.label << std::endl;
// std::cerr << "Bumping[conversion] " << ls_completion_item.label <<
// std::endl;
priority *= 100;
}
if (clang_getCompletionAvailability(str) != CXAvailability_Available) {
//std::cerr << "Bumping[notavailable] " << ls_completion_item.label << std::endl;
// std::cerr << "Bumping[notavailable] " << ls_completion_item.label <<
// std::endl;
priority *= 100;
}
return priority;
@ -97,7 +99,6 @@ bool IsCallKind(CXCursorKind kind) {
lsCompletionItemKind GetCompletionKind(CXCursorKind cursor_kind) {
switch (cursor_kind) {
case CXCursor_ObjCInstanceMethodDecl:
case CXCursor_CXXMethod:
return lsCompletionItemKind::Method;
@ -158,25 +159,33 @@ lsCompletionItemKind GetCompletionKind(CXCursorKind cursor_kind) {
return lsCompletionItemKind::Text;
default:
std::cerr << "[complete] Unhandled completion kind " << cursor_kind << std::endl;
std::cerr << "[complete] Unhandled completion kind " << cursor_kind
<< std::endl;
return lsCompletionItemKind::Text;
}
}
void BuildDetailString(CXCompletionString completion_string, std::string& label, std::string& detail, std::string& insert, std::vector<std::string>* parameters) {
void BuildDetailString(CXCompletionString completion_string,
std::string& label,
std::string& detail,
std::string& insert,
std::vector<std::string>* parameters) {
int num_chunks = clang_getNumCompletionChunks(completion_string);
for (int i = 0; i < num_chunks; ++i) {
CXCompletionChunkKind kind = clang_getCompletionChunkKind(completion_string, i);
CXCompletionChunkKind kind =
clang_getCompletionChunkKind(completion_string, i);
switch (kind) {
case CXCompletionChunk_Optional: {
CXCompletionString nested = clang_getCompletionChunkCompletionString(completion_string, i);
CXCompletionString nested =
clang_getCompletionChunkCompletionString(completion_string, i);
BuildDetailString(nested, label, detail, insert, parameters);
break;
}
case CXCompletionChunk_Placeholder: {
std::string text = clang::ToString(clang_getCompletionChunkText(completion_string, i));
std::string text =
clang::ToString(clang_getCompletionChunkText(completion_string, i));
parameters->push_back(text);
detail += text;
insert += "${" + std::to_string(parameters->size()) + ":" + text + "}";
@ -189,7 +198,8 @@ void BuildDetailString(CXCompletionString completion_string, std::string& label,
break;
case CXCompletionChunk_TypedText: {
std::string text = clang::ToString(clang_getCompletionChunkText(completion_string, i));
std::string text =
clang::ToString(clang_getCompletionChunkText(completion_string, i));
label = text;
detail += text;
insert += text;
@ -197,14 +207,16 @@ void BuildDetailString(CXCompletionString completion_string, std::string& label,
}
case CXCompletionChunk_Text: {
std::string text = clang::ToString(clang_getCompletionChunkText(completion_string, i));
std::string text =
clang::ToString(clang_getCompletionChunkText(completion_string, i));
detail += text;
insert += text;
break;
}
case CXCompletionChunk_Informative: {
detail += clang::ToString(clang_getCompletionChunkText(completion_string, i));
detail +=
clang::ToString(clang_getCompletionChunkText(completion_string, i));
break;
}
@ -285,9 +297,12 @@ void EnsureDocumentParsed(ClangCompleteManager* manager,
std::vector<CXUnsavedFile> unsaved = session->working_files->AsUnsavedFiles();
std::cerr << "[complete] Creating completion session with arguments " << StringJoin(args) << std::endl;
*tu = MakeUnique<clang::TranslationUnit>(index, session->file.filename, args, unsaved, Flags());
std::cerr << "[complete] Done creating active; did_fail=" << (*tu)->did_fail << std::endl;
std::cerr << "[complete] Creating completion session with arguments "
<< StringJoin(args) << std::endl;
*tu = MakeUnique<clang::TranslationUnit>(index, session->file.filename, args,
unsaved, Flags());
std::cerr << "[complete] Done creating active; did_fail=" << (*tu)->did_fail
<< std::endl;
// Build diagnostics.
if (manager->config_->diagnosticsOnParse && !(*tu)->did_fail) {
@ -306,11 +321,14 @@ void EnsureDocumentParsed(ClangCompleteManager* manager,
void CompletionParseMain(ClangCompleteManager* completion_manager) {
while (true) {
// Fetching the completion request blocks until we have a request.
ClangCompleteManager::ParseRequest request = completion_manager->parse_requests_.Dequeue();
ClangCompleteManager::ParseRequest request =
completion_manager->parse_requests_.Dequeue();
// If we don't get a session then that means we don't care about the file
// anymore - abandon the request.
std::shared_ptr<CompletionSession> session = completion_manager->TryGetSession(request.path, false /*create_if_needed*/);
std::shared_ptr<CompletionSession> session =
completion_manager->TryGetSession(request.path,
false /*create_if_needed*/);
if (!session)
continue;
@ -322,24 +340,29 @@ void CompletionParseMain(ClangCompleteManager* completion_manager) {
}
std::unique_ptr<clang::TranslationUnit> parsing;
EnsureDocumentParsed(completion_manager, session, &parsing, &session->index);
EnsureDocumentParsed(completion_manager, session, &parsing,
&session->index);
// Activate new translation unit.
// tu_last_parsed_at is only read by this thread, so it doesn't need to be under the mutex.
// tu_last_parsed_at is only read by this thread, so it doesn't need to be
// under the mutex.
session->tu_last_parsed_at = std::chrono::high_resolution_clock::now();
std::lock_guard<std::mutex> lock(session->tu_lock);
std::cerr << "[completion] Swapping completion session for " << request.path << std::endl;
std::cerr << "[completion] Swapping completion session for " << request.path
<< std::endl;
session->tu = std::move(parsing);
}
}
void CompletionDiagnosticsDelayedRefreshMain(ClangCompleteManager* completion_manager) {
void CompletionDiagnosticsDelayedRefreshMain(
ClangCompleteManager* completion_manager) {
constexpr int kSecondsToWaitForDiagnosticsRefresh = 5;
// Refreshes diagnostics a few seconds after the final code completion, since
// we don't get a language server request.
while (true) {
std::unique_lock<std::mutex> l(completion_manager->delayed_diagnostic_wakeup_mtx_);
std::unique_lock<std::mutex> l(
completion_manager->delayed_diagnostic_wakeup_mtx_);
completion_manager->delayed_diagnostic_wakeup_cv_.wait(l);
// Check for spurious wakeup.
@ -350,15 +373,19 @@ void CompletionDiagnosticsDelayedRefreshMain(ClangCompleteManager* completion_ma
// Get completion request info.
if (!l.owns_lock())
l.lock();
lsTextDocumentPositionParams location = *completion_manager->delayed_diagnostic_last_completion_position_;
lsTextDocumentPositionParams location =
*completion_manager->delayed_diagnostic_last_completion_position_;
completion_manager->delayed_diagnostic_last_completion_position_.reset();
l.unlock();
// Wait five seconds. If there was another completion request, start the
// waiting process over again.
std::this_thread::sleep_for(std::chrono::seconds(kSecondsToWaitForDiagnosticsRefresh));
std::this_thread::sleep_for(
std::chrono::seconds(kSecondsToWaitForDiagnosticsRefresh));
l.lock();
bool has_completion_since_sleeping = completion_manager->delayed_diagnostic_last_completion_position_.has_value();
bool has_completion_since_sleeping =
completion_manager->delayed_diagnostic_last_completion_position_
.has_value();
l.unlock();
if (has_completion_since_sleeping)
continue;
@ -376,39 +403,42 @@ void CompletionDiagnosticsDelayedRefreshMain(ClangCompleteManager* completion_ma
completion_manager->completion_request_.SetIfEmpty(std::move(request));
break;
}
}
}
void CompletionQueryMain(ClangCompleteManager* completion_manager) {
while (true) {
// Fetching the completion request blocks until we have a request.
std::unique_ptr<ClangCompleteManager::CompletionRequest> request = completion_manager->completion_request_.Take();
std::unique_ptr<ClangCompleteManager::CompletionRequest> request =
completion_manager->completion_request_.Take();
std::string path = request->location.textDocument.uri.GetPath();
std::shared_ptr<CompletionSession> session = completion_manager->TryGetSession(path, true /*create_if_needed*/);
std::shared_ptr<CompletionSession> session =
completion_manager->TryGetSession(path, true /*create_if_needed*/);
std::lock_guard<std::mutex> lock(session->tu_lock);
EnsureDocumentParsed(completion_manager, session, &session->tu, &session->index);
EnsureDocumentParsed(completion_manager, session, &session->tu,
&session->index);
// Language server is 0-based, clang is 1-based.
unsigned line = request->location.position.line + 1;
unsigned column = request->location.position.character + 1;
std::cerr << "[complete] Completing at " << line << ":" << column << std::endl;
std::cerr << "[complete] Completing at " << line << ":" << column
<< std::endl;
Timer timer;
std::vector<CXUnsavedFile> unsaved = completion_manager->working_files_->AsUnsavedFiles();
std::vector<CXUnsavedFile> unsaved =
completion_manager->working_files_->AsUnsavedFiles();
timer.ResetAndPrint("[complete] Fetching unsaved files");
timer.Reset();
unsigned const kCompleteOptions = CXCodeComplete_IncludeMacros | CXCodeComplete_IncludeBriefComments;
unsigned const kCompleteOptions =
CXCodeComplete_IncludeMacros | CXCodeComplete_IncludeBriefComments;
CXCodeCompleteResults* cx_results = clang_codeCompleteAt(
session->tu->cx_tu,
session->file.filename.c_str(), line, column,
unsaved.data(), (unsigned)unsaved.size(),
kCompleteOptions);
session->tu->cx_tu, session->file.filename.c_str(), line, column,
unsaved.data(), (unsigned)unsaved.size(), kCompleteOptions);
if (!cx_results) {
timer.ResetAndPrint("[complete] Code completion failed");
if (request->on_complete)
@ -417,7 +447,8 @@ void CompletionQueryMain(ClangCompleteManager* completion_manager) {
}
timer.ResetAndPrint("[complete] clangCodeCompleteAt");
std::cerr << "[complete] Got " << cx_results->NumResults << " results" << std::endl;
std::cerr << "[complete] Got " << cx_results->NumResults << " results"
<< std::endl;
{
if (request->on_complete) {
@ -428,14 +459,15 @@ void CompletionQueryMain(ClangCompleteManager* completion_manager) {
for (unsigned i = 0; i < cx_results->NumResults; ++i) {
CXCompletionResult& result = cx_results->Results[i];
// TODO: Try to figure out how we can hide base method calls without also
// hiding method implementation assistance, ie,
// TODO: Try to figure out how we can hide base method calls without
// also hiding method implementation assistance, ie,
//
// void Foo::* {
// }
//
if (clang_getCompletionAvailability(result.CompletionString) == CXAvailability_NotAvailable)
if (clang_getCompletionAvailability(result.CompletionString) ==
CXAvailability_NotAvailable)
continue;
// TODO: fill in more data
@ -443,29 +475,40 @@ void CompletionQueryMain(ClangCompleteManager* completion_manager) {
// kind/label/detail/docs/sortText
ls_completion_item.kind = GetCompletionKind(result.CursorKind);
BuildDetailString(result.CompletionString, ls_completion_item.label, ls_completion_item.detail, ls_completion_item.insertText, &ls_completion_item.parameters_);
BuildDetailString(result.CompletionString, ls_completion_item.label,
ls_completion_item.detail,
ls_completion_item.insertText,
&ls_completion_item.parameters_);
ls_completion_item.insertText += "$0";
ls_completion_item.documentation = clang::ToString(clang_getCompletionBriefComment(result.CompletionString));
ls_completion_item.sortText = (const char)uint64_t(GetCompletionPriority(result.CompletionString, result.CursorKind, ls_completion_item.label));
ls_completion_item.documentation = clang::ToString(
clang_getCompletionBriefComment(result.CompletionString));
ls_completion_item.sortText = (const char)uint64_t(
GetCompletionPriority(result.CompletionString, result.CursorKind,
ls_completion_item.label));
ls_result.push_back(ls_completion_item);
}
timer.ResetAndPrint("[complete] Building " + std::to_string(ls_result.size()) + " completion results");
timer.ResetAndPrint("[complete] Building " +
std::to_string(ls_result.size()) +
" completion results");
request->on_complete(ls_result, false /*is_cached_result*/);
timer.ResetAndPrint("[complete] Running user-given completion func");
}
if (completion_manager->config_->diagnosticsOnCodeCompletion) {
unsigned num_diagnostics = clang_codeCompleteGetNumDiagnostics(cx_results);
unsigned num_diagnostics =
clang_codeCompleteGetNumDiagnostics(cx_results);
NonElidedVector<lsDiagnostic> ls_diagnostics;
for (unsigned i = 0; i < num_diagnostics; ++i) {
CXDiagnostic cx_diag = clang_codeCompleteGetDiagnostic(cx_results, i);
optional<lsDiagnostic> diagnostic = BuildAndDisposeDiagnostic(cx_diag, path);
optional<lsDiagnostic> diagnostic =
BuildAndDisposeDiagnostic(cx_diag, path);
if (diagnostic)
ls_diagnostics.push_back(*diagnostic);
}
completion_manager->on_diagnostic_(session->file.filename, ls_diagnostics);
completion_manager->on_diagnostic_(session->file.filename,
ls_diagnostics);
timer.ResetAndPrint("[complete] Build diagnostics");
}
}
@ -477,8 +520,10 @@ void CompletionQueryMain(ClangCompleteManager* completion_manager) {
if (completion_manager->config_->diagnosticsOnCodeCompletion &&
request->is_user_completion) {
{
std::lock_guard<std::mutex> lock(completion_manager->delayed_diagnostic_wakeup_mtx_);
completion_manager->delayed_diagnostic_last_completion_position_ = request->location;
std::lock_guard<std::mutex> lock(
completion_manager->delayed_diagnostic_wakeup_mtx_);
completion_manager->delayed_diagnostic_last_completion_position_ =
request->location;
}
completion_manager->delayed_diagnostic_wakeup_cv_.notify_one();
}
@ -489,19 +534,25 @@ void CompletionQueryMain(ClangCompleteManager* completion_manager) {
} // namespace
CompletionSession::CompletionSession(const Project::Entry& file, WorkingFiles* working_files)
: file(file), working_files(working_files), index(0 /*excludeDeclarationsFromPCH*/, 0 /*displayDiagnostics*/) {
std::cerr << "[complete] CompletionSession::CompletionSession() for " << file.filename << std::endl;
CompletionSession::CompletionSession(const Project::Entry& file,
WorkingFiles* working_files)
: file(file),
working_files(working_files),
index(0 /*excludeDeclarationsFromPCH*/, 0 /*displayDiagnostics*/) {
std::cerr << "[complete] CompletionSession::CompletionSession() for "
<< file.filename << std::endl;
}
CompletionSession::~CompletionSession() {
std::cerr << "[complete] CompletionSession::~CompletionSession() for " << file.filename << std::endl;
std::cerr << "[complete] CompletionSession::~CompletionSession() for "
<< file.filename << std::endl;
// EmitBacktrace();
}
LruSessionCache::LruSessionCache(int max_entries) : max_entries_(max_entries) {}
std::shared_ptr<CompletionSession> LruSessionCache::TryGetEntry(const std::string& filename) {
std::shared_ptr<CompletionSession> LruSessionCache::TryGetEntry(
const std::string& filename) {
for (int i = 0; i < entries_.size(); ++i) {
if (entries_[i]->file.filename == filename)
return entries_[i];
@ -509,7 +560,8 @@ std::shared_ptr<CompletionSession> LruSessionCache::TryGetEntry(const std::strin
return nullptr;
}
std::shared_ptr<CompletionSession> LruSessionCache::TryTakeEntry(const std::string& filename) {
std::shared_ptr<CompletionSession> LruSessionCache::TryTakeEntry(
const std::string& filename) {
for (int i = 0; i < entries_.size(); ++i) {
if (entries_[i]->file.filename == filename) {
std::shared_ptr<CompletionSession> result = entries_[i];
@ -529,9 +581,16 @@ void LruSessionCache::InsertEntry(std::shared_ptr<CompletionSession> session) {
ClangCompleteManager::ParseRequest::ParseRequest(const std::string& path)
: request_time(std::chrono::high_resolution_clock::now()), path(path) {}
ClangCompleteManager::ClangCompleteManager(Config* config, Project* project, WorkingFiles* working_files, OnDiagnostic on_diagnostic)
: config_(config), project_(project), working_files_(working_files), on_diagnostic_(on_diagnostic),
view_sessions_(kMaxViewSessions), edit_sessions_(kMaxEditSessions) {
ClangCompleteManager::ClangCompleteManager(Config* config,
Project* project,
WorkingFiles* working_files,
OnDiagnostic on_diagnostic)
: config_(config),
project_(project),
working_files_(working_files),
on_diagnostic_(on_diagnostic),
view_sessions_(kMaxViewSessions),
edit_sessions_(kMaxEditSessions) {
new std::thread([&]() {
SetCurrentThreadName("completequery");
CompletionQueryMain(this);
@ -550,7 +609,9 @@ ClangCompleteManager::ClangCompleteManager(Config* config, Project* project, Wor
ClangCompleteManager::~ClangCompleteManager() {}
void ClangCompleteManager::CodeComplete(const lsTextDocumentPositionParams& completion_location, const OnComplete& on_complete) {
void ClangCompleteManager::CodeComplete(
const lsTextDocumentPositionParams& completion_location,
const OnComplete& on_complete) {
// completion thread will create the CompletionSession if needed.
auto request = MakeUnique<CompletionRequest>();
@ -572,7 +633,8 @@ void ClangCompleteManager::NotifyView(const std::string& filename) {
if (view_sessions_.TryGetEntry(filename))
return;
std::cerr << "[complete] Creating new edit code completion session for " << filename << std::endl;
std::cerr << "[complete] Creating new edit code completion session for "
<< filename << std::endl;
view_sessions_.InsertEntry(std::make_shared<CompletionSession>(
project_->FindCompilationEntryForFile(filename), working_files_));
parse_requests_.Enqueue(ParseRequest(filename));
@ -590,12 +652,13 @@ void ClangCompleteManager::NotifyEdit(const std::string& filename) {
if (edit_sessions_.TryGetEntry(filename))
return;
std::shared_ptr<CompletionSession> session = view_sessions_.TryTakeEntry(filename);
std::shared_ptr<CompletionSession> session =
view_sessions_.TryTakeEntry(filename);
if (session) {
edit_sessions_.InsertEntry(session);
}
else {
std::cerr << "[complete] Creating new edit code completion session for " << filename << std::endl;
} else {
std::cerr << "[complete] Creating new edit code completion session for "
<< filename << std::endl;
edit_sessions_.InsertEntry(std::make_shared<CompletionSession>(
project_->FindCompilationEntryForFile(filename), working_files_));
parse_requests_.PriorityEnqueue(ParseRequest(filename));
@ -610,7 +673,8 @@ void ClangCompleteManager::NotifySave(const std::string& filename) {
std::lock_guard<std::mutex> lock(sessions_lock_);
if (!edit_sessions_.TryGetEntry(filename)) {
std::cerr << "[complete] Creating new edit code completion session for " << filename << std::endl;
std::cerr << "[complete] Creating new edit code completion session for "
<< filename << std::endl;
edit_sessions_.InsertEntry(std::make_shared<CompletionSession>(
project_->FindCompilationEntryForFile(filename), working_files_));
}
@ -618,10 +682,13 @@ void ClangCompleteManager::NotifySave(const std::string& filename) {
parse_requests_.PriorityEnqueue(ParseRequest(filename));
}
std::shared_ptr<CompletionSession> ClangCompleteManager::TryGetSession(const std::string& filename, bool create_if_needed) {
std::shared_ptr<CompletionSession> ClangCompleteManager::TryGetSession(
const std::string& filename,
bool create_if_needed) {
std::lock_guard<std::mutex> lock(sessions_lock_);
std::shared_ptr<CompletionSession> session = edit_sessions_.TryGetEntry(filename);
std::shared_ptr<CompletionSession> session =
edit_sessions_.TryGetEntry(filename);
if (!session)
session = view_sessions_.TryGetEntry(filename);

View File

@ -13,13 +13,15 @@
#include <mutex>
#include <string>
struct CompletionSession : public std::enable_shared_from_this<CompletionSession> {
struct CompletionSession
: public std::enable_shared_from_this<CompletionSession> {
Project::Entry file;
WorkingFiles* working_files;
clang::Index index;
// When |tu| was last parsed.
optional<std::chrono::time_point<std::chrono::high_resolution_clock>> tu_last_parsed_at;
optional<std::chrono::time_point<std::chrono::high_resolution_clock>>
tu_last_parsed_at;
// Acquired when |tu| is being used.
std::mutex tu_lock;
@ -47,8 +49,12 @@ struct LruSessionCache {
};
struct ClangCompleteManager {
using OnDiagnostic = std::function<void(std::string path, NonElidedVector<lsDiagnostic> diagnostics)>;
using OnComplete = std::function<void(const NonElidedVector<lsCompletionItem>& results, bool is_cached_result)>;
using OnDiagnostic =
std::function<void(std::string path,
NonElidedVector<lsDiagnostic> diagnostics)>;
using OnComplete =
std::function<void(const NonElidedVector<lsCompletionItem>& results,
bool is_cached_result)>;
struct ParseRequest {
ParseRequest(const std::string& path);
@ -62,7 +68,10 @@ struct ClangCompleteManager {
bool is_user_completion = false;
};
ClangCompleteManager(Config* config, Project* project, WorkingFiles* working_files, OnDiagnostic on_diagnostic);
ClangCompleteManager(Config* config,
Project* project,
WorkingFiles* working_files,
OnDiagnostic on_diagnostic);
~ClangCompleteManager();
// Start a code completion at the given location. |on_complete| will run when
@ -79,7 +88,8 @@ struct ClangCompleteManager {
// triggers a reparse.
void NotifySave(const std::string& filename);
std::shared_ptr<CompletionSession> TryGetSession(const std::string& filename, bool create_if_needed);
std::shared_ptr<CompletionSession> TryGetSession(const std::string& filename,
bool create_if_needed);
// TODO: make these configurable.
const int kMaxViewSessions = 1;
@ -108,5 +118,6 @@ struct ClangCompleteManager {
std::mutex delayed_diagnostic_wakeup_mtx_;
std::condition_variable delayed_diagnostic_wakeup_cv_;
// Access under |delayed_diagnostic_wakeup_mtx_|.
optional<lsTextDocumentPositionParams> delayed_diagnostic_last_completion_position_;
optional<lsTextDocumentPositionParams>
delayed_diagnostic_last_completion_position_;
};

View File

@ -10,24 +10,24 @@ lsRange GetLsRangeForFixIt(const CXSourceRange& range) {
CXSourceLocation end = clang_getRangeEnd(range);
unsigned int start_line, start_column;
clang_getSpellingLocation(start, nullptr, &start_line, &start_column, nullptr);
clang_getSpellingLocation(start, nullptr, &start_line, &start_column,
nullptr);
unsigned int end_line, end_column;
clang_getSpellingLocation(end, nullptr, &end_line, &end_column, nullptr);
return lsRange(
lsPosition(start_line - 1, start_column - 1) /*start*/,
return lsRange(lsPosition(start_line - 1, start_column - 1) /*start*/,
lsPosition(end_line - 1, end_column) /*end*/);
}
} // namespace
optional<lsDiagnostic> BuildAndDisposeDiagnostic(
CXDiagnostic diagnostic, const std::string& path) {
optional<lsDiagnostic> BuildAndDisposeDiagnostic(CXDiagnostic diagnostic,
const std::string& path) {
// Get diagnostic location.
CXFile file;
unsigned int line, column;
clang_getSpellingLocation(
clang_getDiagnosticLocation(diagnostic), &file, &line, &column, nullptr);
clang_getSpellingLocation(clang_getDiagnosticLocation(diagnostic), &file,
&line, &column, nullptr);
// Only report diagnostics in the same file. Using
// clang_Location_isInSystemHeader causes crashes for some reason.
@ -43,12 +43,15 @@ optional<lsDiagnostic> BuildAndDisposeDiagnostic(
// TODO: ls_diagnostic.range is lsRange, we have Range. We should only be
// storing Range types when inside the indexer so that index <-> buffer
// remapping logic is applied.
ls_diagnostic.range = lsRange(lsPosition(line - 1, column), lsPosition(line - 1, column));
ls_diagnostic.range =
lsRange(lsPosition(line - 1, column), lsPosition(line - 1, column));
ls_diagnostic.message = clang::ToString(clang_getDiagnosticSpelling(diagnostic));
ls_diagnostic.message =
clang::ToString(clang_getDiagnosticSpelling(diagnostic));
// Append the flag that enables this diagnostic, ie, [-Wswitch]
std::string enabling_flag = clang::ToString(clang_getDiagnosticOption(diagnostic, nullptr));
std::string enabling_flag =
clang::ToString(clang_getDiagnosticOption(diagnostic, nullptr));
if (!enabling_flag.empty())
ls_diagnostic.message += " [" + enabling_flag + "]";

View File

@ -4,13 +4,14 @@
#include <clang-c/Index.h>
#include <vector>
#include <optional.h>
#include <vector>
using namespace std::experimental;
optional<lsDiagnostic> BuildAndDisposeDiagnostic(
CXDiagnostic diagnostic, const std::string& path);
optional<lsDiagnostic> BuildAndDisposeDiagnostic(CXDiagnostic diagnostic,
const std::string& path);
// Returns the absolute path to |file|.
std::string FileName(CXFile file);

File diff suppressed because it is too large Load Diff

View File

@ -67,16 +67,20 @@ MAKE_REFLECT_STRUCT(Config,
extraClangArguments,
indexWhitelist, indexBlacklist,
indexWhitelist,
indexBlacklist,
logSkippedPathsForIndex,
maxWorkspaceSearchResults,
indexerCount,
enableIndexing, enableCacheWrite, enableCacheRead,
enableIndexing,
enableCacheWrite,
enableCacheRead,
includeCompletionMaximumPathLength,
includeCompletionWhitelistLiteralEnding,
includeCompletionWhitelist, includeCompletionBlacklist,
includeCompletionWhitelist,
includeCompletionBlacklist,
showDocumentLinksOnIncludes,

View File

@ -6,7 +6,8 @@
#include "utils.h"
bool operator==(const CXFileUniqueID& a, const CXFileUniqueID& b) {
return a.data[0] == b.data[0] && a.data[1] == b.data[1] && a.data[2] == b.data[2];
return a.data[0] == b.data[0] && a.data[1] == b.data[1] &&
a.data[2] == b.data[2];
}
bool FileConsumer::SharedState::Mark(const std::string& file) {
@ -21,7 +22,8 @@ void FileConsumer::SharedState::Reset(const std::string& file) {
files.erase(it);
}
FileConsumer::FileConsumer(SharedState* shared_state, const std::string& parse_file)
FileConsumer::FileConsumer(SharedState* shared_state,
const std::string& parse_file)
: shared_(shared_state), parse_file_(parse_file) {}
IndexFile* FileConsumer::TryConsumeFile(CXFile file, bool* is_first_ownership) {
@ -86,7 +88,8 @@ void FileConsumer::EmitError(CXFile file) const {
std::string file_name = clang::ToString(clang_getFileName(file));
// TODO: Investigate this more, why can we get an empty file name?
if (!file_name.empty()) {
std::string error_message = "Could not get unique file id for " + file_name + " when parsing " + parse_file_;
std::string error_message = "Could not get unique file id for " +
file_name + " when parsing " + parse_file_;
std::cerr << error_message << std::endl;
}
}

View File

@ -6,8 +6,9 @@
#include <functional>
#include <mutex>
#include <unordered_set>
#include <unordered_map>
#include <unordered_set>
struct IndexFile;

View File

@ -27,7 +27,8 @@ std::string ElideLongPath(Config* config, const std::string& path) {
return ".." + path.substr(start + 2);
}
size_t TrimCommonPathPrefix(const std::string& result, const std::string& trimmer) {
size_t TrimCommonPathPrefix(const std::string& result,
const std::string& trimmer) {
size_t i = 0;
while (i < result.size() && i < trimmer.size()) {
char a = result[i];
@ -47,7 +48,9 @@ size_t TrimCommonPathPrefix(const std::string& result, const std::string& trimme
}
// Returns true iff angle brackets should be used.
bool TrimPath(Project* project, const std::string& project_root, std::string* insert_path) {
bool TrimPath(Project* project,
const std::string& project_root,
std::string* insert_path) {
size_t start = 0;
bool angle = false;
@ -73,7 +76,10 @@ bool TrimPath(Project* project, const std::string& project_root, std::string* in
return angle;
}
lsCompletionItem BuildCompletionItem(Config* config, const std::string& path, bool use_angle_brackets, bool is_stl) {
lsCompletionItem BuildCompletionItem(Config* config,
const std::string& path,
bool use_angle_brackets,
bool is_stl) {
lsCompletionItem item;
if (use_angle_brackets)
item.label = "#include <" + ElideLongPath(config, path) + ">";
@ -111,15 +117,18 @@ void IncludeComplete::Rescan() {
completion_items.clear();
absolute_path_to_completion_item.clear();
if (!match_ && (!config_->includeCompletionWhitelist.empty() || !config_->includeCompletionBlacklist.empty()))
match_ = MakeUnique<GroupMatch>(config_->includeCompletionWhitelist, config_->includeCompletionBlacklist);
if (!match_ && (!config_->includeCompletionWhitelist.empty() ||
!config_->includeCompletionBlacklist.empty()))
match_ = MakeUnique<GroupMatch>(config_->includeCompletionWhitelist,
config_->includeCompletionBlacklist);
is_scanning = true;
WorkThread::StartThread("include_scanner", [this]() {
Timer timer;
InsertStlIncludes();
InsertIncludesFromDirectory(config_->projectRoot, false /*use_angle_brackets*/);
InsertIncludesFromDirectory(config_->projectRoot,
false /*use_angle_brackets*/);
for (const std::string& dir : project_->quote_include_directories)
InsertIncludesFromDirectory(dir, false /*use_angle_brackets*/);
for (const std::string& dir : project_->angle_include_directories)
@ -133,48 +142,60 @@ void IncludeComplete::Rescan() {
}
void IncludeComplete::AddFile(const std::string& absolute_path) {
if (!EndsWithAny(absolute_path, config_->includeCompletionWhitelistLiteralEnding))
if (!EndsWithAny(absolute_path,
config_->includeCompletionWhitelistLiteralEnding))
return;
if (match_ && !match_->IsMatch(absolute_path))
return;
std::string trimmed_path = absolute_path;
bool use_angle_brackets = TrimPath(project_, config_->projectRoot, &trimmed_path);
lsCompletionItem item = BuildCompletionItem(config_, trimmed_path, use_angle_brackets, false /*is_stl*/);
bool use_angle_brackets =
TrimPath(project_, config_->projectRoot, &trimmed_path);
lsCompletionItem item = BuildCompletionItem(
config_, trimmed_path, use_angle_brackets, false /*is_stl*/);
if (is_scanning) {
std::lock_guard<std::mutex> lock(completion_items_mutex);
if (absolute_path_to_completion_item.insert(std::make_pair(absolute_path, completion_items.size())).second)
if (absolute_path_to_completion_item
.insert(std::make_pair(absolute_path, completion_items.size()))
.second)
completion_items.push_back(item);
}
else {
if (absolute_path_to_completion_item.insert(std::make_pair(absolute_path, completion_items.size())).second)
} else {
if (absolute_path_to_completion_item
.insert(std::make_pair(absolute_path, completion_items.size()))
.second)
completion_items.push_back(item);
}
}
void IncludeComplete::InsertIncludesFromDirectory(
std::string directory,
void IncludeComplete::InsertIncludesFromDirectory(std::string directory,
bool use_angle_brackets) {
directory = NormalizePath(directory);
EnsureEndsInSlash(directory);
std::vector<CompletionCandidate> results;
GetFilesInFolder(directory, true /*recursive*/, false /*add_folder_to_path*/, [&](const std::string& path) {
if (!EndsWithAny(path, config_->includeCompletionWhitelistLiteralEnding))
GetFilesInFolder(
directory, true /*recursive*/, false /*add_folder_to_path*/,
[&](const std::string& path) {
if (!EndsWithAny(path,
config_->includeCompletionWhitelistLiteralEnding))
return;
if (match_ && !match_->IsMatch(directory + path))
return;
CompletionCandidate candidate;
candidate.absolute_path = directory + path;
candidate.completion_item = BuildCompletionItem(config_, path, use_angle_brackets, false /*is_stl*/);
candidate.completion_item = BuildCompletionItem(
config_, path, use_angle_brackets, false /*is_stl*/);
results.push_back(candidate);
});
std::lock_guard<std::mutex> lock(completion_items_mutex);
for (const CompletionCandidate& result : results) {
if (absolute_path_to_completion_item.insert(std::make_pair(result.absolute_path, completion_items.size())).second)
if (absolute_path_to_completion_item
.insert(
std::make_pair(result.absolute_path, completion_items.size()))
.second)
completion_items.push_back(result.completion_item);
}
}
@ -182,11 +203,13 @@ void IncludeComplete::InsertIncludesFromDirectory(
void IncludeComplete::InsertStlIncludes() {
std::lock_guard<std::mutex> lock(completion_items_mutex);
for (const char* stl_header : kStandardLibraryIncludes) {
completion_items.push_back(BuildCompletionItem(config_, stl_header, true /*use_angle_brackets*/, true /*is_stl*/));
completion_items.push_back(BuildCompletionItem(
config_, stl_header, true /*use_angle_brackets*/, true /*is_stl*/));
}
}
optional<lsCompletionItem> IncludeComplete::FindCompletionItemForAbsolutePath(const std::string& absolute_path) {
optional<lsCompletionItem> IncludeComplete::FindCompletionItemForAbsolutePath(
const std::string& absolute_path) {
std::lock_guard<std::mutex> lock(completion_items_mutex);
auto it = absolute_path_to_completion_item.find(absolute_path);

View File

@ -20,10 +20,12 @@ struct IncludeComplete {
// Scans the given directory and inserts all includes from this. This is a
// blocking function and should be run off the querydb thread.
void InsertIncludesFromDirectory(std::string directory, bool use_angle_brackets);
void InsertIncludesFromDirectory(std::string directory,
bool use_angle_brackets);
void InsertStlIncludes();
optional<lsCompletionItem> FindCompletionItemForAbsolutePath(const std::string& absolute_path);
optional<lsCompletionItem> FindCompletionItemForAbsolutePath(
const std::string& absolute_path);
// Guards |completion_items| when |is_scanning| is true.
std::mutex completion_items_mutex;
@ -42,4 +44,3 @@ struct IncludeComplete {
Project* project_;
std::unique_ptr<GroupMatch> match_;
};

View File

@ -14,7 +14,8 @@
#include <algorithm>
#include <chrono>
// TODO: See if we can use clang_indexLoc_getFileLocation to get a type ref on |Foobar| in DISALLOW_COPY(Foobar)
// TODO: See if we can use clang_indexLoc_getFileLocation to get a type ref on
// |Foobar| in DISALLOW_COPY(Foobar)
namespace {
@ -31,12 +32,12 @@ Range Resolve(const CXSourceRange& range, CXFile* cx_file = nullptr) {
CXSourceLocation end = clang_getRangeEnd(range);
unsigned int start_line, start_column;
clang_getSpellingLocation(start, cx_file, &start_line, &start_column, nullptr);
clang_getSpellingLocation(start, cx_file, &start_line, &start_column,
nullptr);
unsigned int end_line, end_column;
clang_getSpellingLocation(end, nullptr, &end_line, &end_column, nullptr);
return Range(
Position((int16_t)start_line, (int16_t)start_column) /*start*/,
return Range(Position((int16_t)start_line, (int16_t)start_column) /*start*/,
Position((int16_t)end_line, (int16_t)end_column) /*end*/);
}
@ -50,7 +51,6 @@ Range ResolveExtent(const CXCursor& cx_cursor, CXFile* cx_file = nullptr) {
return Resolve(cx_range, cx_file);
}
struct NamespaceHelper {
std::unordered_map<std::string, std::string> container_usr_to_qualified_name;
@ -109,12 +109,14 @@ struct IndexParam {
FileConsumer* file_consumer = nullptr;
NamespaceHelper ns;
IndexParam(clang::TranslationUnit* tu, FileConsumer* file_consumer) : tu(tu), file_consumer(file_consumer) {}
IndexParam(clang::TranslationUnit* tu, FileConsumer* file_consumer)
: tu(tu), file_consumer(file_consumer) {}
};
IndexFile* ConsumeFile(IndexParam* param, CXFile file) {
bool is_first_ownership = false;
IndexFile* db = param->file_consumer->TryConsumeFile(file, &is_first_ownership);
IndexFile* db =
param->file_consumer->TryConsumeFile(file, &is_first_ownership);
// If this is the first time we have seen the file (ignoring if we are
// generating an index for it):
@ -129,18 +131,21 @@ IndexFile* ConsumeFile(IndexParam* param, CXFile file) {
// Set modification time.
optional<int64_t> modification_time = GetLastModificationTime(file_name);
LOG_IF_S(ERROR, !modification_time) << "Failed fetching modification time for " << file_name;
LOG_IF_S(ERROR, !modification_time)
<< "Failed fetching modification time for " << file_name;
if (modification_time)
param->file_modification_times[file_name] = *modification_time;
// Capture file contents in |param->file_contents| if it was not specified
// at the start of indexing.
if (db && param->file_contents.find(file_name) == param->file_contents.end()) {
if (db &&
param->file_contents.find(file_name) == param->file_contents.end()) {
optional<std::string> content = ReadContent(file_name);
if (content)
param->file_contents[file_name] = *content;
else
LOG_S(ERROR) << "[indexer] Failed to read file content for " << file_name;
LOG_S(ERROR) << "[indexer] Failed to read file content for "
<< file_name;
}
}
}
@ -190,7 +195,9 @@ bool CanBeCalledImplicitly(CXIdxEntityKind kind) {
// Returns true if the cursor spelling contains the given string. This is
// useful to check for implicit function calls.
bool CursorSpellingContainsString(CXCursor cursor, CXTranslationUnit cx_tu, std::string scanning_for) {
bool CursorSpellingContainsString(CXCursor cursor,
CXTranslationUnit cx_tu,
std::string scanning_for) {
CXSourceRange range = clang_Cursor_getSpellingNameRange(cursor, 0, 0);
CXToken* tokens;
unsigned num_tokens;
@ -213,7 +220,8 @@ bool CursorSpellingContainsString(CXCursor cursor, CXTranslationUnit cx_tu, std:
// Returns the document content for the given range. May not work perfectly
// when there are tabs instead of spaces.
std::string GetDocumentContentInRange(CXTranslationUnit cx_tu, CXSourceRange range) {
std::string GetDocumentContentInRange(CXTranslationUnit cx_tu,
CXSourceRange range) {
std::string result;
CXToken* tokens;
@ -227,7 +235,8 @@ std::string GetDocumentContentInRange(CXTranslationUnit cx_tu, CXSourceRange ran
Range token_range = Resolve(clang_getTokenExtent(cx_tu, tokens[i]));
if (previous_token_range) {
// Insert newlines.
int16_t line_delta = token_range.start.line - previous_token_range->end.line;
int16_t line_delta =
token_range.start.line - previous_token_range->end.line;
assert(line_delta >= 0);
if (line_delta > 0) {
result.append((size_t)line_delta, '\n');
@ -235,7 +244,8 @@ std::string GetDocumentContentInRange(CXTranslationUnit cx_tu, CXSourceRange ran
previous_token_range->end.column = 0;
}
// Insert spaces.
int16_t column_delta = token_range.start.column - previous_token_range->end.column;
int16_t column_delta =
token_range.start.column - previous_token_range->end.column;
assert(column_delta >= 0);
result.append((size_t)column_delta, ' ');
}
@ -640,7 +650,6 @@ optional<IndexTypeId> AddDeclTypeUsages(
clang::Cursor decl_cursor,
const CXIdxContainerInfo* semantic_container,
const CXIdxContainerInfo* lexical_container) {
// std::cerr << std::endl << "AddDeclUsages " << decl_cursor.get_spelling() <<
// std::endl;
// Dump(decl_cursor);
@ -843,15 +852,15 @@ bool AreEqualLocations(CXIdxLoc loc, CXCursor cursor) {
clang_getRangeStart(clang_Cursor_getSpellingNameRange(cursor, 0, 0)));
}
clang::VisiterResult VisitMacroDefinitionAndExpansions(clang::Cursor cursor, clang::Cursor parent, IndexParam* param) {
clang::VisiterResult VisitMacroDefinitionAndExpansions(clang::Cursor cursor,
clang::Cursor parent,
IndexParam* param) {
switch (cursor.get_kind()) {
case CXCursor_MacroDefinition:
case CXCursor_MacroExpansion: {
// Resolve location, find IndexFile instance.
CXSourceRange cx_source_range = clang_Cursor_getSpellingNameRange(cursor.cx_cursor, 0, 0);
CXSourceRange cx_source_range =
clang_Cursor_getSpellingNameRange(cursor.cx_cursor, 0, 0);
CXSourceLocation start = clang_getRangeStart(cx_source_range);
if (clang_Location_isInSystemHeader(start))
break;
@ -881,7 +890,8 @@ clang::VisiterResult VisitMacroDefinitionAndExpansions(clang::Cursor cursor, cla
var_def->def.is_local = false;
var_def->def.is_macro = true;
var_def->def.definition_spelling = decl_loc_spelling;
var_def->def.definition_extent = ResolveExtent(cursor.cx_cursor);;
var_def->def.definition_extent = ResolveExtent(cursor.cx_cursor);
;
}
break;
@ -945,13 +955,16 @@ clang::VisiterResult VisitMacroDefinitionAndExpansions(clang::Cursor cursor, cla
void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
if (!kIndexStdDeclarations && clang_Location_isInSystemHeader(clang_indexLoc_getCXSourceLocation(decl->loc)))
if (!kIndexStdDeclarations &&
clang_Location_isInSystemHeader(
clang_indexLoc_getCXSourceLocation(decl->loc)))
return;
assert(AreEqualLocations(decl->loc, decl->cursor));
CXFile file;
clang_getSpellingLocation(clang_indexLoc_getCXSourceLocation(decl->loc), &file, nullptr, nullptr, nullptr);
clang_getSpellingLocation(clang_indexLoc_getCXSourceLocation(decl->loc),
&file, nullptr, nullptr, nullptr);
IndexParam* param = static_cast<IndexParam*>(client_data);
IndexFile* db = ConsumeFile(param, file);
if (!db)
@ -959,8 +972,9 @@ void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
NamespaceHelper* ns = &param->ns;
//std::cerr << "DECL kind=" << decl->entityInfo->kind << " at " << db->id_cache.Resolve(decl->cursor, false).ToPrettyString(&db->id_cache) << std::endl;
// std::cerr << "DECL kind=" << decl->entityInfo->kind << " at " <<
// db->id_cache.Resolve(decl->cursor, false).ToPrettyString(&db->id_cache) <<
// std::endl;
switch (decl->entityInfo->kind) {
case CXIdxEntity_CXXNamespace: {
@ -993,18 +1007,23 @@ void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
// if (!decl->isRedeclaration) {
var->def.short_name = decl->entityInfo->name;
std::string type_name = clang::ToString(clang_getTypeSpelling(clang_getCursorType(decl->cursor)));
var->def.detailed_name = type_name + " " + ns->QualifiedName(decl->semanticContainer, var->def.short_name);
std::string type_name = clang::ToString(
clang_getTypeSpelling(clang_getCursorType(decl->cursor)));
var->def.detailed_name =
type_name + " " +
ns->QualifiedName(decl->semanticContainer, var->def.short_name);
var->def.is_local = !decl->semanticContainer || IsLocalSemanticContainer(decl->semanticContainer->cursor.kind);
var->def.is_local =
!decl->semanticContainer ||
IsLocalSemanticContainer(decl->semanticContainer->cursor.kind);
//}
if (decl->isDefinition) {
var->def.definition_spelling = ResolveSpelling(decl->cursor);
var->def.definition_extent = ResolveExtent(decl->cursor);;
}
else {
var->def.definition_extent = ResolveExtent(decl->cursor);
;
} else {
var->def.declaration = ResolveSpelling(decl->cursor);
}
UniqueAdd(var->uses, decl_loc_spelling);
@ -1020,17 +1039,19 @@ void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
// the function declaration is encountered since we won't receive ParmDecl
// declarations for unnamed parameters.
// TODO: See if we can remove this function call.
AddDeclTypeUsages(
db, decl_cursor,
decl->semanticContainer, decl->lexicalContainer);
AddDeclTypeUsages(db, decl_cursor, decl->semanticContainer,
decl->lexicalContainer);
// We don't need to assign declaring type multiple times if this variable
// has already been seen.
if (!decl->isRedeclaration) {
optional<IndexTypeId> var_type = ResolveToDeclarationType(db, decl_cursor);
optional<IndexTypeId> var_type =
ResolveToDeclarationType(db, decl_cursor);
if (var_type.has_value()) {
// Don't treat enum definition variables as instantiations.
bool is_enum_member = decl->semanticContainer && decl->semanticContainer->cursor.kind == CXCursor_EnumDecl;
bool is_enum_member =
decl->semanticContainer &&
decl->semanticContainer->cursor.kind == CXCursor_EnumDecl;
if (!is_enum_member)
db->Resolve(var_type.value())->instances.push_back(var_id);
@ -1038,7 +1059,8 @@ void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
}
}
// TODO: Refactor handlers so more things are under 'if (!decl->isRedeclaration)'
// TODO: Refactor handlers so more things are under 'if
// (!decl->isRedeclaration)'
if (decl->isDefinition && IsTypeDefinition(decl->semanticContainer)) {
IndexTypeId declaring_type_id =
db->ToTypeId(decl->semanticContainer->cursor);
@ -1060,7 +1082,8 @@ void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
Range decl_extent = ResolveExtent(decl->cursor);
clang::Cursor decl_cursor = decl->cursor;
clang::Cursor decl_cursor_resolved = decl_cursor.template_specialization_to_template_definition();
clang::Cursor decl_cursor_resolved =
decl_cursor.template_specialization_to_template_definition();
bool is_template_specialization = decl_cursor != decl_cursor_resolved;
IndexFuncId func_id = db->ToFuncId(decl_cursor_resolved.cx_cursor);
@ -1068,8 +1091,8 @@ void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
// We don't actually need to know the return type, but we need to mark it
// as an interesting usage.
AddDeclTypeUsages(db, decl_cursor,
decl->semanticContainer, decl->lexicalContainer);
AddDeclTypeUsages(db, decl_cursor, decl->semanticContainer,
decl->lexicalContainer);
// Add definition or declaration. This is a bit tricky because we treat
// template specializations as declarations, even though they are
@ -1081,12 +1104,12 @@ void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
// assert(!func->def.definition_extent);
func->def.definition_spelling = decl_spelling;
func->def.definition_extent = decl_extent;
}
else {
} else {
IndexFunc::Declaration declaration;
declaration.spelling = decl_spelling;
declaration.extent = decl_extent;
declaration.content = GetDocumentContentInRange(param->tu->cx_tu, clang_getCursorExtent(decl->cursor));
declaration.content = GetDocumentContentInRange(
param->tu->cx_tu, clang_getCursorExtent(decl->cursor));
// Add parameters.
for (clang::Cursor arg : decl_cursor.get_arguments()) {
@ -1096,7 +1119,8 @@ void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
// If the name is empty (which is common for parameters), clang
// will report a range with length 1, which is not correct.
if (param_spelling.start.column == (param_spelling.end.column - 1) &&
if (param_spelling.start.column ==
(param_spelling.end.column - 1) &&
arg.get_display_name().empty()) {
param_spelling.end.column -= 1;
}
@ -1122,7 +1146,8 @@ void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
// Build detailed name. The type desc looks like void (void *). We
// insert the qualified name before the first '('.
std::string qualified_name = ns->QualifiedName(decl->semanticContainer, func->def.short_name);
std::string qualified_name =
ns->QualifiedName(decl->semanticContainer, func->def.short_name);
std::string type_desc = decl_cursor.get_type_description();
size_t offset = type_desc.find('(');
type_desc.insert(offset, qualified_name);
@ -1160,7 +1185,8 @@ void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
// FIXME if it ever shows up. Methods should only ever have 1 base
// type, though.
if (num_overridden > 1)
std::cerr << "[indexer]: warning: multiple base overrides for " << func->def.detailed_name << std::endl;
std::cerr << "[indexer]: warning: multiple base overrides for "
<< func->def.detailed_name << std::endl;
for (unsigned i = 0; i < num_overridden; ++i) {
clang::Cursor parent = overridden[i];
@ -1185,9 +1211,8 @@ void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
// Note we want to fetch the first TypeRef. Running
// ResolveCursorType(decl->cursor) would return
// the type of the typedef/using, not the type of the referenced type.
optional<IndexTypeId> alias_of =
AddDeclTypeUsages(db, decl->cursor,
decl->semanticContainer, decl->lexicalContainer);
optional<IndexTypeId> alias_of = AddDeclTypeUsages(
db, decl->cursor, decl->semanticContainer, decl->lexicalContainer);
IndexTypeId type_id = db->ToTypeId(decl->entityInfo->USR);
IndexType* type = db->Resolve(type_id);
@ -1254,15 +1279,14 @@ void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
for (unsigned int i = 0; i < class_info->numBases; ++i) {
const CXIdxBaseClassInfo* base_class = class_info->bases[i];
AddDeclTypeUsages(db, base_class->cursor,
decl->semanticContainer, decl->lexicalContainer);
AddDeclTypeUsages(db, base_class->cursor, decl->semanticContainer,
decl->lexicalContainer);
optional<IndexTypeId> parent_type_id =
ResolveToDeclarationType(db, base_class->cursor);
// type_def ptr could be invalidated by ResolveToDeclarationType.
type = db->Resolve(type_id);
if (parent_type_id) {
IndexType* parent_type_def =
db->Resolve(parent_type_id.value());
IndexType* parent_type_def = db->Resolve(parent_type_id.value());
parent_type_def->derived.push_back(type_id);
type->def.parents.push_back(parent_type_id.value());
}
@ -1272,11 +1296,9 @@ void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
}
default:
std::cerr
<< "!! Unhandled indexDeclaration: "
std::cerr << "!! Unhandled indexDeclaration: "
<< clang::Cursor(decl->cursor).ToString() << " at "
<< ResolveSpelling(decl->cursor).start.ToString()
<< std::endl;
<< ResolveSpelling(decl->cursor).start.ToString() << std::endl;
std::cerr << " entityInfo->kind = " << decl->entityInfo->kind
<< std::endl;
std::cerr << " entityInfo->USR = " << decl->entityInfo->USR
@ -1367,20 +1389,24 @@ bool IsFunctionCallContext(CXCursorKind kind) {
void indexEntityReference(CXClientData client_data,
const CXIdxEntityRefInfo* ref) {
// Don't index references from or to system headers.
if (clang_Location_isInSystemHeader(clang_indexLoc_getCXSourceLocation(ref->loc)) ||
clang_Location_isInSystemHeader(clang_getCursorLocation(ref->referencedEntity->cursor)))
if (clang_Location_isInSystemHeader(
clang_indexLoc_getCXSourceLocation(ref->loc)) ||
clang_Location_isInSystemHeader(
clang_getCursorLocation(ref->referencedEntity->cursor)))
return;
// assert(AreEqualLocations(ref->loc, ref->cursor));
// if (clang_Location_isInSystemHeader(clang_getCursorLocation(ref->cursor)) ||
// if (clang_Location_isInSystemHeader(clang_getCursorLocation(ref->cursor))
// ||
// clang_Location_isInSystemHeader(
// clang_getCursorLocation(ref->referencedEntity->cursor)))
// return;
// TODO: Use clang_getFileUniqueID
CXFile file;
clang_getSpellingLocation(clang_indexLoc_getCXSourceLocation(ref->loc), &file, nullptr, nullptr, nullptr);
clang_getSpellingLocation(clang_indexLoc_getCXSourceLocation(ref->loc), &file,
nullptr, nullptr, nullptr);
IndexParam* param = static_cast<IndexParam*>(client_data);
IndexFile* db = ConsumeFile(param, file);
if (!db)
@ -1390,17 +1416,18 @@ void indexEntityReference(CXClientData client_data,
// ref->loc mainFile=1
// ref->referencedEntity mainFile=1
//
// Regardless, we need to do more advanced location processing to handle multiple output IndexFile instances.
//bool mainFile = clang_Location_isFromMainFile(clang_indexLoc_getCXSourceLocation(ref->loc));
//Range loc_spelling = param->db->id_cache.ForceResolveSpelling(ref->cursor, false /*interesting*/);
//std::cerr << "mainFile: " << mainFile << ", loc: " << loc_spelling.ToString() << std::endl;
// Regardless, we need to do more advanced location processing to handle
// multiple output IndexFile instances.
// bool mainFile =
// clang_Location_isFromMainFile(clang_indexLoc_getCXSourceLocation(ref->loc));
// Range loc_spelling = param->db->id_cache.ForceResolveSpelling(ref->cursor,
// false /*interesting*/); std::cerr << "mainFile: " << mainFile << ", loc: "
// << loc_spelling.ToString() << std::endl;
// Don't index references that are not from the main file.
// if (!clang_Location_isFromMainFile(clang_getCursorLocation(ref->cursor)))
// return;
clang::Cursor cursor(ref->cursor);
// std::cerr << "REF kind=" << ref->referencedEntity->kind << " at " <<
@ -1453,8 +1480,10 @@ void indexEntityReference(CXClientData client_data,
// libclang doesn't provide a nice api to check if the given function
// call is implicit. ref->kind should probably work (it's either direct
// or implicit), but libclang only supports implicit for objective-c.
bool is_implicit = CanBeCalledImplicitly(ref->referencedEntity->kind) &&
!CursorSpellingContainsString(ref->cursor, param->tu->cx_tu, called->def.short_name);
bool is_implicit =
CanBeCalledImplicitly(ref->referencedEntity->kind) &&
!CursorSpellingContainsString(ref->cursor, param->tu->cx_tu,
called->def.short_name);
if (IsFunctionCallContext(ref->container->cursor.kind)) {
IndexFuncId caller_id = db->ToFuncId(ref->container->cursor);
@ -1462,8 +1491,10 @@ void indexEntityReference(CXClientData client_data,
// Calling db->ToFuncId invalidates the FuncDef* ptrs.
called = db->Resolve(called_id);
AddFuncRef(&caller->def.callees, IndexFuncRef(called_id, loc_spelling, is_implicit));
AddFuncRef(&called->callers, IndexFuncRef(caller_id, loc_spelling, is_implicit));
AddFuncRef(&caller->def.callees,
IndexFuncRef(called_id, loc_spelling, is_implicit));
AddFuncRef(&called->callers,
IndexFuncRef(caller_id, loc_spelling, is_implicit));
} else {
AddFuncRef(&called->callers, IndexFuncRef(loc_spelling, is_implicit));
}
@ -1478,7 +1509,8 @@ void indexEntityReference(CXClientData client_data,
case CXIdxEntity_Struct:
case CXIdxEntity_CXXClass: {
clang::Cursor referenced_cursor = ref->referencedEntity->cursor;
referenced_cursor = referenced_cursor.template_specialization_to_template_definition();
referenced_cursor =
referenced_cursor.template_specialization_to_template_definition();
IndexTypeId referenced_id = db->ToTypeId(referenced_cursor.get_usr());
IndexType* referenced = db->Resolve(referenced_id);
@ -1503,20 +1535,16 @@ void indexEntityReference(CXClientData client_data,
}
default:
std::cerr
<< "!! Unhandled indexEntityReference: " << cursor.ToString()
<< " at "
<< ResolveSpelling(ref->cursor).start.ToString()
std::cerr << "!! Unhandled indexEntityReference: " << cursor.ToString()
<< " at " << ResolveSpelling(ref->cursor).start.ToString()
<< std::endl;
std::cerr << " ref->referencedEntity->kind = "
<< ref->referencedEntity->kind << std::endl;
if (ref->parentEntity)
std::cerr << " ref->parentEntity->kind = "
<< ref->parentEntity->kind << std::endl;
std::cerr
<< " ref->loc = "
<< ResolveSpelling(ref->cursor).start.ToString()
<< std::endl;
std::cerr << " ref->loc = "
<< ResolveSpelling(ref->cursor).start.ToString() << std::endl;
std::cerr << " ref->kind = " << ref->kind << std::endl;
if (ref->parentEntity)
std::cerr << " parentEntity = "
@ -1563,17 +1591,18 @@ void indexEntityReference(CXClientData client_data,
FileContents::FileContents(const std::string& path, const std::string& content) : path(path), content(content) {}
FileContents::FileContents(const std::string& path, const std::string& content)
: path(path), content(content) {}
std::vector<std::unique_ptr<IndexFile>> Parse(
Config* config, FileConsumer::SharedState* file_consumer_shared,
Config* config,
FileConsumer::SharedState* file_consumer_shared,
std::string file,
std::vector<std::string> args,
std::vector<FileContents> file_contents,
PerformanceImportFile* perf,
clang::Index* index,
bool dump_ast) {
if (!config->enableIndexing)
return {};
@ -1590,9 +1619,12 @@ std::vector<std::unique_ptr<IndexFile>> Parse(
unsaved_files.push_back(unsaved);
}
clang::TranslationUnit tu(index, file, args, unsaved_files, CXTranslationUnit_KeepGoing | CXTranslationUnit_DetailedPreprocessingRecord);
clang::TranslationUnit tu(index, file, args, unsaved_files,
CXTranslationUnit_KeepGoing |
CXTranslationUnit_DetailedPreprocessingRecord);
if (tu.did_fail) {
std::cerr << "!! Failed creating translation unit for " << file << std::endl;
std::cerr << "!! Failed creating translation unit for " << file
<< std::endl;
return {};
}
@ -1601,12 +1633,10 @@ std::vector<std::unique_ptr<IndexFile>> Parse(
if (dump_ast)
Dump(tu.document_cursor());
IndexerCallbacks callbacks[] = {
{&abortQuery, &diagnostic, &enteredMainFile, &ppIncludedFile,
&importedASTFile, &startedTranslationUnit, &indexDeclaration,
&indexEntityReference}
};
IndexerCallbacks callbacks[] = {{&abortQuery, &diagnostic, &enteredMainFile,
&ppIncludedFile, &importedASTFile,
&startedTranslationUnit, &indexDeclaration,
&indexEntityReference}};
FileConsumer file_consumer(file_consumer_shared, file);
IndexParam param(&tu, &file_consumer);
@ -1620,13 +1650,16 @@ std::vector<std::unique_ptr<IndexFile>> Parse(
// std::cerr << "!! [START] Indexing " << file << std::endl;
CXIndexAction index_action = clang_IndexAction_create(index->cx_index);
clang_indexTranslationUnit(index_action, &param, callbacks, sizeof(callbacks),
CXIndexOpt_IndexFunctionLocalSymbols | CXIndexOpt_SkipParsedBodiesInSession | CXIndexOpt_IndexImplicitTemplateInstantiations,
CXIndexOpt_IndexFunctionLocalSymbols |
CXIndexOpt_SkipParsedBodiesInSession |
CXIndexOpt_IndexImplicitTemplateInstantiations,
tu.cx_tu);
clang_IndexAction_dispose(index_action);
// std::cerr << "!! [END] Indexing " << file << std::endl;
tu.document_cursor().VisitChildren(&VisitMacroDefinitionAndExpansions, &param);
tu.document_cursor().VisitChildren(&VisitMacroDefinitionAndExpansions,
&param);
perf->index_build = timer.ElapsedMicrosecondsAndReset();
@ -1655,7 +1688,6 @@ std::vector<std::unique_ptr<IndexFile>> Parse(
return result;
}
void IndexInit() {
clang_enableStackTraces();
clang_toggleCrashRecovery(1);

View File

@ -1,28 +1,31 @@
#pragma once
#include "file_consumer.h"
#include "position.h"
#include "serializer.h"
#include "utils.h"
#include "language_server_api.h"
#include "libclangmm/Index.h"
#include "libclangmm/Utility.h"
#include "performance.h"
#include "position.h"
#include "serializer.h"
#include "utils.h"
#include <optional.h>
#include <rapidjson/writer.h>
#include <rapidjson/document.h>
#include <rapidjson/prettywriter.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <algorithm>
#include <cstdint>
#include <cassert>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <unordered_map>
#include <vector>
struct IndexType;
struct IndexFunc;
struct IndexVar;
@ -49,7 +52,7 @@ template <typename T>
struct hash<Id<T>> {
size_t operator()(const Id<T>& k) const { return hash<size_t>()(k.id); }
};
}
} // namespace std
template <typename T>
bool operator==(const Id<T>& a, const Id<T>& b) {
@ -76,7 +79,6 @@ using IndexVarId = Id<IndexVar>;
struct IdCache;
struct IndexFuncRef {
// NOTE: id can be -1 if the function call is not coming from a function.
IndexFuncId id;
@ -85,19 +87,25 @@ struct IndexFuncRef {
IndexFuncRef() {} // For serialization.
IndexFuncRef(IndexFuncId id, Range loc, bool is_implicit) : id(id), loc(loc), is_implicit(is_implicit) {}
IndexFuncRef(Range loc, bool is_implicit) : id(IndexFuncId((size_t)-1)), loc(loc), is_implicit(is_implicit) {}
IndexFuncRef(IndexFuncId id, Range loc, bool is_implicit)
: id(id), loc(loc), is_implicit(is_implicit) {}
IndexFuncRef(Range loc, bool is_implicit)
: id(IndexFuncId((size_t)-1)), loc(loc), is_implicit(is_implicit) {}
inline bool operator==(const IndexFuncRef& other) {
return id == other.id && loc == other.loc && is_implicit == other.is_implicit;
return id == other.id && loc == other.loc &&
is_implicit == other.is_implicit;
}
inline bool operator!=(const IndexFuncRef& other) {
return !(*this == other);
}
inline bool operator!=(const IndexFuncRef& other) { return !(*this == other); }
inline bool operator<(const IndexFuncRef& other) const {
if (id < other.id)
return true;
if (id == other.id && loc < other.loc)
return true;
return id == other.id && loc == other.loc && is_implicit < other.is_implicit;
return id == other.id && loc == other.loc &&
is_implicit < other.is_implicit;
}
};
@ -127,8 +135,7 @@ inline void Reflect(Writer& visitor, IndexFuncRef& value) {
s += "~";
if (value.id.id == -1) { // id.id is unsigned, special case 0 value
s += "-1";
}
else {
} else {
s += std::to_string(value.id.id);
}
@ -136,10 +143,7 @@ inline void Reflect(Writer& visitor, IndexFuncRef& value) {
visitor.String(s.c_str());
}
template <typename TypeId,
typename FuncId,
typename VarId,
typename Range>
template <typename TypeId, typename FuncId, typename VarId, typename Range>
struct TypeDefDefinitionData {
// General metadata.
std::string usr;
@ -173,19 +177,18 @@ struct TypeDefDefinitionData {
TypeDefDefinitionData() {} // For reflection.
TypeDefDefinitionData(const std::string& usr) : usr(usr) {}
bool operator==(const TypeDefDefinitionData<TypeId, FuncId, VarId, Range>&
other) const {
bool operator==(
const TypeDefDefinitionData<TypeId, FuncId, VarId, Range>& other) const {
return usr == other.usr && short_name == other.short_name &&
detailed_name == other.detailed_name &&
definition_spelling == other.definition_spelling &&
definition_extent == other.definition_extent &&
alias_of == other.alias_of &&
parents == other.parents && types == other.types &&
funcs == other.funcs && vars == other.vars;
alias_of == other.alias_of && parents == other.parents &&
types == other.types && funcs == other.funcs && vars == other.vars;
}
bool operator!=(const TypeDefDefinitionData<TypeId, FuncId, VarId, Range>&
other) const {
bool operator!=(
const TypeDefDefinitionData<TypeId, FuncId, VarId, Range>& other) const {
return !(*this == other);
}
};
@ -211,7 +214,8 @@ void Reflect(TVisitor& visitor,
}
struct IndexType {
using Def = TypeDefDefinitionData<IndexTypeId, IndexFuncId, IndexVarId, Range>;
using Def =
TypeDefDefinitionData<IndexTypeId, IndexFuncId, IndexVarId, Range>;
Def def;
IndexTypeId id;
@ -268,8 +272,8 @@ struct FuncDefDefinitionData {
}
bool operator==(
const FuncDefDefinitionData<TypeId, FuncId, VarId, FuncRef, Range>&
other) const {
const FuncDefDefinitionData<TypeId, FuncId, VarId, FuncRef, Range>& other)
const {
return usr == other.usr && short_name == other.short_name &&
detailed_name == other.detailed_name &&
definition_spelling == other.definition_spelling &&
@ -278,8 +282,8 @@ struct FuncDefDefinitionData {
locals == other.locals && callees == other.callees;
}
bool operator!=(
const FuncDefDefinitionData<TypeId, FuncId, VarId, FuncRef, Range>&
other) const {
const FuncDefDefinitionData<TypeId, FuncId, VarId, FuncRef, Range>& other)
const {
return !(*this == other);
}
};
@ -307,7 +311,11 @@ void Reflect(
}
struct IndexFunc {
using Def = FuncDefDefinitionData<IndexTypeId, IndexFuncId, IndexVarId, IndexFuncRef, Range>;
using Def = FuncDefDefinitionData<IndexTypeId,
IndexFuncId,
IndexVarId,
IndexFuncRef,
Range>;
Def def;
IndexFuncId id;
@ -346,12 +354,13 @@ struct IndexFunc {
}
};
MAKE_HASHABLE(IndexFunc, t.def.usr);
MAKE_REFLECT_STRUCT(IndexFunc::Declaration, spelling, extent, content, param_spellings);
MAKE_REFLECT_STRUCT(IndexFunc::Declaration,
spelling,
extent,
content,
param_spellings);
template <typename TypeId,
typename FuncId,
typename VarId,
typename Range>
template <typename TypeId, typename FuncId, typename VarId, typename Range>
struct VarDefDefinitionData {
// General metadata.
std::string usr;
@ -377,8 +386,8 @@ struct VarDefDefinitionData {
VarDefDefinitionData() {} // For reflection.
VarDefDefinitionData(const std::string& usr) : usr(usr) {}
bool operator==(const VarDefDefinitionData<TypeId, FuncId, VarId, Range>&
other) const {
bool operator==(
const VarDefDefinitionData<TypeId, FuncId, VarId, Range>& other) const {
return usr == other.usr && short_name == other.short_name &&
detailed_name == other.detailed_name &&
declaration == other.declaration &&
@ -387,8 +396,8 @@ struct VarDefDefinitionData {
variable_type == other.variable_type &&
declaring_type == other.declaring_type;
}
bool operator!=(const VarDefDefinitionData<TypeId, FuncId, VarId, Range>&
other) const {
bool operator!=(
const VarDefDefinitionData<TypeId, FuncId, VarId, Range>& other) const {
return !(*this == other);
}
};
@ -509,9 +518,11 @@ struct FileContents {
// |import_file| is the cc file which is what gets passed to clang.
// |desired_index_file| is the (h or cc) file which has actually changed.
// |dependencies| are the existing dependencies of |import_file| if this is a reparse.
// |dependencies| are the existing dependencies of |import_file| if this is a
// reparse.
std::vector<std::unique_ptr<IndexFile>> Parse(
Config* config, FileConsumer::SharedState* file_consumer_shared,
Config* config,
FileConsumer::SharedState* file_consumer_shared,
std::string file,
std::vector<std::string> args,
std::vector<FileContents> file_contents,

View File

@ -85,7 +85,6 @@ const char* IpcIdToString(IpcId id) {
}
}
BaseIpcMessage::BaseIpcMessage(IpcId method_id)
: method_id(method_id) {}
BaseIpcMessage::BaseIpcMessage(IpcId method_id) : method_id(method_id) {}
BaseIpcMessage::~BaseIpcMessage() = default;

View File

@ -1,7 +1,8 @@
#pragma once
#include "utils.h"
#include "serializer.h"
#include "utils.h"
#include <string>
@ -88,10 +89,15 @@ struct Ipc_CqueryIndexFile : public IpcMessage<Ipc_CqueryIndexFile> {
};
Params params;
};
MAKE_REFLECT_STRUCT(Ipc_CqueryIndexFile::Params, path, args, is_interactive, contents);
MAKE_REFLECT_STRUCT(Ipc_CqueryIndexFile::Params,
path,
args,
is_interactive,
contents);
MAKE_REFLECT_STRUCT(Ipc_CqueryIndexFile, params);
struct Ipc_CqueryQueryDbWaitForIdleIndexer : public IpcMessage<Ipc_CqueryQueryDbWaitForIdleIndexer> {
struct Ipc_CqueryQueryDbWaitForIdleIndexer
: public IpcMessage<Ipc_CqueryQueryDbWaitForIdleIndexer> {
static constexpr IpcId kIpcId = IpcId::CqueryQueryDbWaitForIdleIndexer;
};
MAKE_REFLECT_EMPTY_STRUCT(Ipc_CqueryQueryDbWaitForIdleIndexer);

View File

@ -12,8 +12,10 @@ void IpcManager::CreateInstance(MultiQueueWaiter* waiter) {
instance_ = new IpcManager(waiter);
}
ThreadedQueue<std::unique_ptr<BaseIpcMessage>>* IpcManager::GetThreadedQueue(Destination destination) {
return destination == Destination::Client ? threaded_queue_for_client_.get() : threaded_queue_for_server_.get();
ThreadedQueue<std::unique_ptr<BaseIpcMessage>>* IpcManager::GetThreadedQueue(
Destination destination) {
return destination == Destination::Client ? threaded_queue_for_client_.get()
: threaded_queue_for_server_.get();
}
void IpcManager::SendOutMessageToClient(IpcId id, lsBaseOutMessage& response) {
@ -26,15 +28,19 @@ void IpcManager::SendOutMessageToClient(IpcId id, lsBaseOutMessage& response) {
GetThreadedQueue(Destination::Client)->Enqueue(std::move(out));
}
void IpcManager::SendMessage(Destination destination, std::unique_ptr<BaseIpcMessage> message) {
void IpcManager::SendMessage(Destination destination,
std::unique_ptr<BaseIpcMessage> message) {
GetThreadedQueue(destination)->Enqueue(std::move(message));
}
std::vector<std::unique_ptr<BaseIpcMessage>> IpcManager::GetMessages(Destination destination) {
std::vector<std::unique_ptr<BaseIpcMessage>> IpcManager::GetMessages(
Destination destination) {
return GetThreadedQueue(destination)->DequeueAll();
}
IpcManager::IpcManager(MultiQueueWaiter* waiter) {
threaded_queue_for_client_ = MakeUnique<ThreadedQueue<std::unique_ptr<BaseIpcMessage>>>(waiter);
threaded_queue_for_server_ = MakeUnique<ThreadedQueue<std::unique_ptr<BaseIpcMessage>>>(waiter);
threaded_queue_for_client_ =
MakeUnique<ThreadedQueue<std::unique_ptr<BaseIpcMessage>>>(waiter);
threaded_queue_for_server_ =
MakeUnique<ThreadedQueue<std::unique_ptr<BaseIpcMessage>>>(waiter);
}

View File

@ -11,20 +11,23 @@ struct IpcManager {
static IpcManager* instance();
static void CreateInstance(MultiQueueWaiter* waiter);
std::unique_ptr<ThreadedQueue<std::unique_ptr<BaseIpcMessage>>> threaded_queue_for_client_;
std::unique_ptr<ThreadedQueue<std::unique_ptr<BaseIpcMessage>>> threaded_queue_for_server_;
std::unique_ptr<ThreadedQueue<std::unique_ptr<BaseIpcMessage>>>
threaded_queue_for_client_;
std::unique_ptr<ThreadedQueue<std::unique_ptr<BaseIpcMessage>>>
threaded_queue_for_server_;
enum class Destination {
Client, Server
};
enum class Destination { Client, Server };
ThreadedQueue<std::unique_ptr<BaseIpcMessage>>* GetThreadedQueue(Destination destination);
ThreadedQueue<std::unique_ptr<BaseIpcMessage>>* GetThreadedQueue(
Destination destination);
void SendOutMessageToClient(IpcId id, lsBaseOutMessage& response);
void SendMessage(Destination destination, std::unique_ptr<BaseIpcMessage> message);
void SendMessage(Destination destination,
std::unique_ptr<BaseIpcMessage> message);
std::vector<std::unique_ptr<BaseIpcMessage>> GetMessages(Destination destination);
std::vector<std::unique_ptr<BaseIpcMessage>> GetMessages(
Destination destination);
private:
IpcManager(MultiQueueWaiter* waiter);

View File

@ -8,8 +8,7 @@ void Reflect(Writer& visitor, lsRequestId& value) {
if (value.id0) {
Reflect(visitor, value.id0.value());
}
else {
} else {
Reflect(visitor, value.id1.value());
}
}
@ -26,7 +25,8 @@ void Reflect(Reader& visitor, lsRequestId& id) {
MessageRegistry* MessageRegistry::instance_ = nullptr;
// Reads a JsonRpc message. |read| returns the next input character.
optional<std::string> ReadJsonRpcContentFrom(std::function<optional<char>()> read) {
optional<std::string> ReadJsonRpcContentFrom(
std::function<optional<char>()> read) {
// Read the content length. It is terminated by the "\r\n" sequence.
int exit_seq = 0;
std::string stringified_content_length;
@ -38,14 +38,17 @@ optional<std::string> ReadJsonRpcContentFrom(std::function<optional<char>()> rea
}
char c = *opt_c;
if (exit_seq == 0 && c == '\r') ++exit_seq;
if (exit_seq == 1 && c == '\n') break;
if (exit_seq == 0 && c == '\r')
++exit_seq;
if (exit_seq == 1 && c == '\n')
break;
stringified_content_length += c;
}
const char* kContentLengthStart = "Content-Length: ";
assert(StartsWith(stringified_content_length, kContentLengthStart));
int content_length = atoi(stringified_content_length.c_str() + strlen(kContentLengthStart));
int content_length =
atoi(stringified_content_length.c_str() + strlen(kContentLengthStart));
// There is always a "\r\n" sequence before the actual content.
auto expect_char = [&](char expected) {
@ -74,8 +77,8 @@ optional<std::string> ReadJsonRpcContentFrom(std::function<optional<char>()> rea
TEST_SUITE("FindIncludeLine");
std::function<optional<char>()>
MakeContentReader(std::string* content, bool can_be_empty) {
std::function<optional<char>()> MakeContentReader(std::string* content,
bool can_be_empty) {
return [content, can_be_empty]() -> optional<char> {
if (!can_be_empty)
REQUIRE(!content->empty());
@ -106,7 +109,8 @@ TEST_CASE("ReadContentFromSource") {
REQUIRE(parse_incorrect("ggg") == optional<std::string>());
REQUIRE(parse_incorrect("Content-Length: 0\r\n") == optional<std::string>());
REQUIRE(parse_incorrect("Content-Length: 5\r\n\r\nab") == optional<std::string>());
REQUIRE(parse_incorrect("Content-Length: 5\r\n\r\nab") ==
optional<std::string>());
}
TEST_SUITE_END();
@ -125,7 +129,8 @@ optional<char> ReadCharFromStdinBlocking() {
}
std::unique_ptr<BaseIpcMessage> MessageRegistry::ReadMessageFromStdin() {
optional<std::string> content = ReadJsonRpcContentFrom(&ReadCharFromStdinBlocking);
optional<std::string> content =
ReadJsonRpcContentFrom(&ReadCharFromStdinBlocking);
if (!content) {
LOG_S(FATAL) << "Failed to read JsonRpc input; exiting";
exit(1);
@ -139,7 +144,8 @@ std::unique_ptr<BaseIpcMessage> MessageRegistry::ReadMessageFromStdin() {
}
std::unique_ptr<BaseIpcMessage> MessageRegistry::Parse(Reader& visitor) {
if (!visitor.HasMember("jsonrpc") || std::string(visitor["jsonrpc"].GetString()) != "2.0") {
if (!visitor.HasMember("jsonrpc") ||
std::string(visitor["jsonrpc"].GetString()) != "2.0") {
std::cerr << "Bad or missing jsonrpc version" << std::endl;
exit(1);
}
@ -148,7 +154,8 @@ std::unique_ptr<BaseIpcMessage> MessageRegistry::Parse(Reader& visitor) {
ReflectMember(visitor, "method", method);
if (allocators.find(method) == allocators.end()) {
LOG_S(ERROR) << "Unable to find registered handler for method \"" << method << "\"" << std::endl;
LOG_S(ERROR) << "Unable to find registered handler for method \"" << method
<< "\"" << std::endl;
return nullptr;
}
@ -197,7 +204,8 @@ void lsDocumentUri::SetPath(const std::string& path) {
size_t index = raw_uri.find(":");
if (index == 1) { // widows drive letters must always be 1 char
raw_uri.replace(raw_uri.begin() + index, raw_uri.begin() + index + 1, "%3A");
raw_uri.replace(raw_uri.begin() + index, raw_uri.begin() + index + 1,
"%3A");
}
raw_uri = ReplaceAll(raw_uri, " ", "%20");
@ -249,7 +257,8 @@ std::string lsDocumentUri::GetPath() const {
}
lsPosition::lsPosition() {}
lsPosition::lsPosition(int line, int character) : line(line), character(character) {}
lsPosition::lsPosition(int line, int character)
: line(line), character(character) {}
bool lsPosition::operator==(const lsPosition& other) const {
return line == other.line && character == other.character;
@ -267,7 +276,8 @@ bool lsRange::operator==(const lsRange& other) const {
}
lsLocation::lsLocation() {}
lsLocation::lsLocation(lsDocumentUri uri, lsRange range) : uri(uri), range(range) {}
lsLocation::lsLocation(lsDocumentUri uri, lsRange range)
: uri(uri), range(range) {}
bool lsLocation::operator==(const lsLocation& other) const {
return uri == other.uri && range == other.range;

View File

@ -14,8 +14,8 @@
#include <unordered_map>
#include <unordered_set>
using std::experimental::optional;
using std::experimental::nullopt;
using std::experimental::optional;
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
@ -79,7 +79,8 @@ struct MessageRegistry {
static MessageRegistry* instance_;
static MessageRegistry* instance();
using Allocator = std::function<std::unique_ptr<BaseIpcMessage>(Reader& visitor)>;
using Allocator =
std::function<std::unique_ptr<BaseIpcMessage>(Reader& visitor)>;
std::unordered_map<std::string, Allocator> allocators;
template <typename T>
@ -96,7 +97,6 @@ struct MessageRegistry {
std::unique_ptr<BaseIpcMessage> Parse(Reader& visitor);
};
struct lsBaseOutMessage {
virtual ~lsBaseOutMessage();
virtual void Write(std::ostream& out) = 0;
@ -221,7 +221,6 @@ void Reflect(TVisitor& visitor, lsDocumentUri& value) {
Reflect(visitor, value.raw_uri);
}
struct lsPosition {
lsPosition();
lsPosition(int line, int character);
@ -377,7 +376,8 @@ enum class lsInsertTextFormat {
// the end of the snippet. Placeholders with equal identifiers are linked,
// that is typing in one will update others too.
//
// See also: https://github.com/Microsoft/vscode/blob/master/src/vs/editor/contrib/snippet/common/snippet.md
// See also:
// https://github.com/Microsoft/vscode/blob/master/src/vs/editor/contrib/snippet/common/snippet.md
Snippet = 2
};
MAKE_REFLECT_TYPE_PROXY(lsInsertTextFormat, int);
@ -406,7 +406,8 @@ enum class lsCompletionItemKind {
MAKE_REFLECT_TYPE_PROXY(lsCompletionItemKind, int);
struct lsCompletionItem {
// A set of function parameters. Used internally for signature help. Not sent to vscode.
// A set of function parameters. Used internally for signature help. Not sent
// to vscode.
std::vector<std::string> parameters_;
// The label of this completion item. By default
@ -437,15 +438,15 @@ struct lsCompletionItem {
// this completion. When `falsy` the label is used.
std::string insertText;
// The format of the insert text. The format applies to both the `insertText` property
// and the `newText` property of a provided `textEdit`.
// The format of the insert text. The format applies to both the `insertText`
// property and the `newText` property of a provided `textEdit`.
lsInsertTextFormat insertTextFormat = lsInsertTextFormat::Snippet;
// An edit which is applied to a document when selecting this completion. When an edit is provided the value of
// `insertText` is ignored.
// An edit which is applied to a document when selecting this completion. When
// an edit is provided the value of `insertText` is ignored.
//
// *Note:* The range of the edit must be a single line range and it must contain the position at which completion
// has been requested.
// *Note:* The range of the edit must be a single line range and it must
// contain the position at which completion has been requested.
optional<lsTextEdit> textEdit;
// An optional array of additional text edits that are applied when
@ -453,10 +454,9 @@ struct lsCompletionItem {
// nor with themselves.
// std::vector<TextEdit> additionalTextEdits;
// An optional command that is executed *after* inserting this completion. *Note* that
// additional modifications to the current document should be described with the
// additionalTextEdits-property.
// Command command;
// An optional command that is executed *after* inserting this completion.
// *Note* that additional modifications to the current document should be
// described with the additionalTextEdits-property. Command command;
// An data entry field that is preserved on a completion item between
// a completion and a completion resolve request.
@ -572,7 +572,6 @@ struct lsDiagnostic {
};
MAKE_REFLECT_STRUCT(lsDiagnostic, range, severity, source, message);
// TODO: DocumentFilter
// TODO: DocumentSelector
@ -687,17 +686,17 @@ struct lsWorkspaceClientCapabilites {
// Capabilities specific to `WorkspaceEdit`s
optional<lsWorkspaceEdit> workspaceEdit;
struct lsGenericDynamicReg {
// Did foo notification supports dynamic registration.
optional<bool> dynamicRegistration;
};
// Capabilities specific to the `workspace/didChangeConfiguration` notification.
// Capabilities specific to the `workspace/didChangeConfiguration`
// notification.
optional<lsGenericDynamicReg> didChangeConfiguration;
// Capabilities specific to the `workspace/didChangeWatchedFiles` notification.
// Capabilities specific to the `workspace/didChangeWatchedFiles`
// notification.
optional<lsGenericDynamicReg> didChangeWatchedFiles;
// Capabilities specific to the `workspace/symbol` request.
@ -707,8 +706,10 @@ struct lsWorkspaceClientCapabilites {
optional<lsGenericDynamicReg> executeCommand;
};
MAKE_REFLECT_STRUCT(lsWorkspaceClientCapabilites::lsWorkspaceEdit, documentChanges);
MAKE_REFLECT_STRUCT(lsWorkspaceClientCapabilites::lsGenericDynamicReg, dynamicRegistration);
MAKE_REFLECT_STRUCT(lsWorkspaceClientCapabilites::lsWorkspaceEdit,
documentChanges);
MAKE_REFLECT_STRUCT(lsWorkspaceClientCapabilites::lsGenericDynamicReg,
dynamicRegistration);
MAKE_REFLECT_STRUCT(lsWorkspaceClientCapabilites,
applyEdit,
workspaceEdit,
@ -717,8 +718,6 @@ MAKE_REFLECT_STRUCT(lsWorkspaceClientCapabilites,
symbol,
executeCommand);
// Text document specific client capabilities.
struct lsTextDocumentClientCapabilities {
struct lsSynchronization {
@ -810,11 +809,23 @@ struct lsTextDocumentClientCapabilities {
optional<lsGenericDynamicReg> rename;
};
MAKE_REFLECT_STRUCT(lsTextDocumentClientCapabilities::lsSynchronization, dynamicRegistration, willSave, willSaveWaitUntil, didSave);
MAKE_REFLECT_STRUCT(lsTextDocumentClientCapabilities::lsCompletion, dynamicRegistration, completionItem);
MAKE_REFLECT_STRUCT(lsTextDocumentClientCapabilities::lsCompletion::lsCompletionItem, snippetSupport);
MAKE_REFLECT_STRUCT(lsTextDocumentClientCapabilities::lsGenericDynamicReg, dynamicRegistration);
MAKE_REFLECT_STRUCT(lsTextDocumentClientCapabilities::CodeLensRegistrationOptions, dynamicRegistration, resolveProvider);
MAKE_REFLECT_STRUCT(lsTextDocumentClientCapabilities::lsSynchronization,
dynamicRegistration,
willSave,
willSaveWaitUntil,
didSave);
MAKE_REFLECT_STRUCT(lsTextDocumentClientCapabilities::lsCompletion,
dynamicRegistration,
completionItem);
MAKE_REFLECT_STRUCT(
lsTextDocumentClientCapabilities::lsCompletion::lsCompletionItem,
snippetSupport);
MAKE_REFLECT_STRUCT(lsTextDocumentClientCapabilities::lsGenericDynamicReg,
dynamicRegistration);
MAKE_REFLECT_STRUCT(
lsTextDocumentClientCapabilities::CodeLensRegistrationOptions,
dynamicRegistration,
resolveProvider);
MAKE_REFLECT_STRUCT(lsTextDocumentClientCapabilities,
synchronization,
completion,
@ -849,7 +860,8 @@ MAKE_REFLECT_STRUCT(lsClientCapabilities, workspace, textDocument);
struct lsInitializeParams {
// The process Id of the parent process that started
// the server. Is null if the process has not been started by another process.
// If the parent process is not alive then the server should exit (see exit notification) its process.
// If the parent process is not alive then the server should exit (see exit
// notification) its process.
optional<int> processId;
// The rootPath of the workspace. Is null
@ -881,8 +893,13 @@ struct lsInitializeParams {
};
void Reflect(Reader& reader, lsInitializeParams::lsTrace& value);
void Reflect(Writer& writer, lsInitializeParams::lsTrace& value);
MAKE_REFLECT_STRUCT(lsInitializeParams, processId, rootPath, rootUri, initializationOptions, capabilities, trace);
MAKE_REFLECT_STRUCT(lsInitializeParams,
processId,
rootPath,
rootUri,
initializationOptions,
capabilities,
trace);
struct lsInitializeError {
// Indicates whether the client should retry to send the
@ -892,7 +909,8 @@ struct lsInitializeError {
};
MAKE_REFLECT_STRUCT(lsInitializeError, retry);
// Defines how the host (editor) should sync document changes to the language server.
// Defines how the host (editor) should sync document changes to the language
// server.
enum class lsTextDocumentSyncKind {
// Documents should not be synced at all.
None = 0,
@ -941,7 +959,9 @@ struct lsDocumentOnTypeFormattingOptions {
// More trigger characters.
NonElidedVector<std::string> moreTriggerCharacter;
};
MAKE_REFLECT_STRUCT(lsDocumentOnTypeFormattingOptions, firstTriggerCharacter, moreTriggerCharacter);
MAKE_REFLECT_STRUCT(lsDocumentOnTypeFormattingOptions,
firstTriggerCharacter,
moreTriggerCharacter);
// Document link options
struct lsDocumentLinkOptions {
@ -967,8 +987,8 @@ MAKE_REFLECT_STRUCT(lsSaveOptions, includeText);
struct lsTextDocumentSyncOptions {
// Open and close notifications are sent to the server.
bool openClose = false;
// Change notificatins are sent to the server. See TextDocumentSyncKind.None, TextDocumentSyncKind.Full
// and TextDocumentSyncKindIncremental.
// Change notificatins are sent to the server. See TextDocumentSyncKind.None,
// TextDocumentSyncKind.Full and TextDocumentSyncKindIncremental.
lsTextDocumentSyncKind change = lsTextDocumentSyncKind::Incremental;
// Will save notifications are sent to the server.
optional<bool> willSave;
@ -977,11 +997,17 @@ struct lsTextDocumentSyncOptions {
// Save notifications are sent to the server.
optional<lsSaveOptions> save;
};
MAKE_REFLECT_STRUCT(lsTextDocumentSyncOptions, openClose, change, willSave, willSaveWaitUntil, save);
MAKE_REFLECT_STRUCT(lsTextDocumentSyncOptions,
openClose,
change,
willSave,
willSaveWaitUntil,
save);
struct lsServerCapabilities {
// Defines how text documents are synced. Is either a detailed structure defining each notification or
// for backwards compatibility the TextDocumentSyncKind number.
// Defines how text documents are synced. Is either a detailed structure
// defining each notification or for backwards compatibility the
// TextDocumentSyncKind number.
// TODO: It seems like the new API is broken and doesn't work.
// optional<lsTextDocumentSyncOptions> textDocumentSync;
lsTextDocumentSyncKind textDocumentSync;
@ -1056,7 +1082,8 @@ struct Out_InitializeResponse : public lsOutMessage<Out_InitializeResponse> {
MAKE_REFLECT_STRUCT(Out_InitializeResponse::InitializeResult, capabilities);
MAKE_REFLECT_STRUCT(Out_InitializeResponse, jsonrpc, id, result);
struct Ipc_InitializedNotification : public IpcMessage<Ipc_InitializedNotification> {
struct Ipc_InitializedNotification
: public IpcMessage<Ipc_InitializedNotification> {
const static IpcId kIpcId = IpcId::Initialized;
lsRequestId id;
@ -1136,11 +1163,6 @@ struct Out_Error : public lsOutMessage<Out_Error> {
MAKE_REFLECT_STRUCT(Out_Error::lsResponseError, code, message);
MAKE_REFLECT_STRUCT(Out_Error, jsonrpc, id, error);
// Cancel an existing request.
struct Ipc_CancelRequest : public IpcMessage<Ipc_CancelRequest> {
static const IpcId kIpcId = IpcId::CancelRequest;
@ -1148,10 +1170,6 @@ struct Ipc_CancelRequest : public IpcMessage<Ipc_CancelRequest> {
};
MAKE_REFLECT_STRUCT(Ipc_CancelRequest, id);
// Open, update, close file
struct Ipc_TextDocumentDidOpen : public IpcMessage<Ipc_TextDocumentDidOpen> {
struct Params {
@ -1163,7 +1181,8 @@ struct Ipc_TextDocumentDidOpen : public IpcMessage<Ipc_TextDocumentDidOpen> {
};
MAKE_REFLECT_STRUCT(Ipc_TextDocumentDidOpen::Params, textDocument);
MAKE_REFLECT_STRUCT(Ipc_TextDocumentDidOpen, params);
struct Ipc_TextDocumentDidChange : public IpcMessage<Ipc_TextDocumentDidChange> {
struct Ipc_TextDocumentDidChange
: public IpcMessage<Ipc_TextDocumentDidChange> {
struct lsTextDocumentContentChangeEvent {
// The range of the document that changed.
lsRange range;
@ -1181,8 +1200,13 @@ struct Ipc_TextDocumentDidChange : public IpcMessage<Ipc_TextDocumentDidChange>
const static IpcId kIpcId = IpcId::TextDocumentDidChange;
Params params;
};
MAKE_REFLECT_STRUCT(Ipc_TextDocumentDidChange::lsTextDocumentContentChangeEvent, range, rangeLength, text);
MAKE_REFLECT_STRUCT(Ipc_TextDocumentDidChange::Params, textDocument, contentChanges);
MAKE_REFLECT_STRUCT(Ipc_TextDocumentDidChange::lsTextDocumentContentChangeEvent,
range,
rangeLength,
text);
MAKE_REFLECT_STRUCT(Ipc_TextDocumentDidChange::Params,
textDocument,
contentChanges);
MAKE_REFLECT_STRUCT(Ipc_TextDocumentDidChange, params);
struct Ipc_TextDocumentDidClose : public IpcMessage<Ipc_TextDocumentDidClose> {
struct Params {
@ -1195,7 +1219,6 @@ struct Ipc_TextDocumentDidClose : public IpcMessage<Ipc_TextDocumentDidClose> {
MAKE_REFLECT_STRUCT(Ipc_TextDocumentDidClose::Params, textDocument);
MAKE_REFLECT_STRUCT(Ipc_TextDocumentDidClose, params);
struct Ipc_TextDocumentDidSave : public IpcMessage<Ipc_TextDocumentDidSave> {
struct Params {
// The document that was saved.
@ -1212,10 +1235,9 @@ struct Ipc_TextDocumentDidSave : public IpcMessage<Ipc_TextDocumentDidSave> {
MAKE_REFLECT_STRUCT(Ipc_TextDocumentDidSave::Params, textDocument);
MAKE_REFLECT_STRUCT(Ipc_TextDocumentDidSave, params);
// Diagnostics
struct Out_TextDocumentPublishDiagnostics : public lsOutMessage<Out_TextDocumentPublishDiagnostics> {
struct Out_TextDocumentPublishDiagnostics
: public lsOutMessage<Out_TextDocumentPublishDiagnostics> {
struct Params {
// The URI for which diagnostic information is reported.
lsDocumentUri uri;
@ -1235,9 +1257,9 @@ void Reflect(TVisitor& visitor, Out_TextDocumentPublishDiagnostics& value) {
REFLECT_MEMBER(params);
REFLECT_MEMBER_END();
}
MAKE_REFLECT_STRUCT(Out_TextDocumentPublishDiagnostics::Params, uri, diagnostics);
MAKE_REFLECT_STRUCT(Out_TextDocumentPublishDiagnostics::Params,
uri,
diagnostics);
// Rename
struct Ipc_TextDocumentRename : public IpcMessage<Ipc_TextDocumentRename> {
@ -1258,7 +1280,10 @@ struct Ipc_TextDocumentRename : public IpcMessage<Ipc_TextDocumentRename> {
lsRequestId id;
Params params;
};
MAKE_REFLECT_STRUCT(Ipc_TextDocumentRename::Params, textDocument, position, newName);
MAKE_REFLECT_STRUCT(Ipc_TextDocumentRename::Params,
textDocument,
position,
newName);
MAKE_REFLECT_STRUCT(Ipc_TextDocumentRename, id, params);
struct Out_TextDocumentRename : public lsOutMessage<Out_TextDocumentRename> {
lsRequestId id;
@ -1266,10 +1291,6 @@ struct Out_TextDocumentRename : public lsOutMessage<Out_TextDocumentRename> {
};
MAKE_REFLECT_STRUCT(Out_TextDocumentRename, jsonrpc, id, result);
// Code completion
struct Ipc_TextDocumentComplete : public IpcMessage<Ipc_TextDocumentComplete> {
const static IpcId kIpcId = IpcId::TextDocumentCompletion;
@ -1286,14 +1307,16 @@ struct lsTextDocumentCompleteResult {
NonElidedVector<lsCompletionItem> items;
};
MAKE_REFLECT_STRUCT(lsTextDocumentCompleteResult, isIncomplete, items);
struct Out_TextDocumentComplete : public lsOutMessage<Out_TextDocumentComplete> {
struct Out_TextDocumentComplete
: public lsOutMessage<Out_TextDocumentComplete> {
lsRequestId id;
lsTextDocumentCompleteResult result;
};
MAKE_REFLECT_STRUCT(Out_TextDocumentComplete, jsonrpc, id, result);
// Signature help.
struct Ipc_TextDocumentSignatureHelp : public IpcMessage<Ipc_TextDocumentSignatureHelp> {
struct Ipc_TextDocumentSignatureHelp
: public IpcMessage<Ipc_TextDocumentSignatureHelp> {
const static IpcId kIpcId = IpcId::TextDocumentSignatureHelp;
lsRequestId id;
@ -1353,36 +1376,44 @@ struct lsSignatureHelp {
// active signature does have any.
optional<int> activeParameter;
};
MAKE_REFLECT_STRUCT(lsSignatureHelp, signatures, activeSignature, activeParameter);
struct Out_TextDocumentSignatureHelp : public lsOutMessage<Out_TextDocumentSignatureHelp> {
MAKE_REFLECT_STRUCT(lsSignatureHelp,
signatures,
activeSignature,
activeParameter);
struct Out_TextDocumentSignatureHelp
: public lsOutMessage<Out_TextDocumentSignatureHelp> {
lsRequestId id;
lsSignatureHelp result;
};
MAKE_REFLECT_STRUCT(Out_TextDocumentSignatureHelp, jsonrpc, id, result);
// Goto definition
struct Ipc_TextDocumentDefinition : public IpcMessage<Ipc_TextDocumentDefinition> {
struct Ipc_TextDocumentDefinition
: public IpcMessage<Ipc_TextDocumentDefinition> {
const static IpcId kIpcId = IpcId::TextDocumentDefinition;
lsRequestId id;
lsTextDocumentPositionParams params;
};
MAKE_REFLECT_STRUCT(Ipc_TextDocumentDefinition, id, params);
struct Out_TextDocumentDefinition : public lsOutMessage<Out_TextDocumentDefinition> {
struct Out_TextDocumentDefinition
: public lsOutMessage<Out_TextDocumentDefinition> {
lsRequestId id;
NonElidedVector<lsLocation> result;
};
MAKE_REFLECT_STRUCT(Out_TextDocumentDefinition, jsonrpc, id, result);
// Document highlight
struct Ipc_TextDocumentDocumentHighlight : public IpcMessage<Ipc_TextDocumentDocumentHighlight> {
struct Ipc_TextDocumentDocumentHighlight
: public IpcMessage<Ipc_TextDocumentDocumentHighlight> {
const static IpcId kIpcId = IpcId::TextDocumentDocumentHighlight;
lsRequestId id;
lsTextDocumentPositionParams params;
};
MAKE_REFLECT_STRUCT(Ipc_TextDocumentDocumentHighlight, id, params);
struct Out_TextDocumentDocumentHighlight : public lsOutMessage<Out_TextDocumentDocumentHighlight> {
struct Out_TextDocumentDocumentHighlight
: public lsOutMessage<Out_TextDocumentDocumentHighlight> {
lsRequestId id;
NonElidedVector<lsDocumentHighlight> result;
};
@ -1409,7 +1440,8 @@ MAKE_REFLECT_STRUCT(Out_TextDocumentHover::Result, contents, range);
MAKE_REFLECT_STRUCT(Out_TextDocumentHover, jsonrpc, id, result);
// References
struct Ipc_TextDocumentReferences : public IpcMessage<Ipc_TextDocumentReferences> {
struct Ipc_TextDocumentReferences
: public IpcMessage<Ipc_TextDocumentReferences> {
struct lsReferenceContext {
// Include the declaration of the current symbol.
bool includeDeclaration;
@ -1425,17 +1457,23 @@ struct Ipc_TextDocumentReferences : public IpcMessage<Ipc_TextDocumentReferences
lsRequestId id;
lsReferenceParams params;
};
MAKE_REFLECT_STRUCT(Ipc_TextDocumentReferences::lsReferenceContext, includeDeclaration);
MAKE_REFLECT_STRUCT(Ipc_TextDocumentReferences::lsReferenceParams, textDocument, position, context);
MAKE_REFLECT_STRUCT(Ipc_TextDocumentReferences::lsReferenceContext,
includeDeclaration);
MAKE_REFLECT_STRUCT(Ipc_TextDocumentReferences::lsReferenceParams,
textDocument,
position,
context);
MAKE_REFLECT_STRUCT(Ipc_TextDocumentReferences, id, params);
struct Out_TextDocumentReferences : public lsOutMessage<Out_TextDocumentReferences> {
struct Out_TextDocumentReferences
: public lsOutMessage<Out_TextDocumentReferences> {
lsRequestId id;
NonElidedVector<lsLocation> result;
};
MAKE_REFLECT_STRUCT(Out_TextDocumentReferences, jsonrpc, id, result);
// Code action
struct Ipc_TextDocumentCodeAction : public IpcMessage<Ipc_TextDocumentCodeAction> {
struct Ipc_TextDocumentCodeAction
: public IpcMessage<Ipc_TextDocumentCodeAction> {
const static IpcId kIpcId = IpcId::TextDocumentCodeAction;
// Contains additional diagnostic information about the context in which
// a code action is run.
@ -1456,10 +1494,15 @@ struct Ipc_TextDocumentCodeAction : public IpcMessage<Ipc_TextDocumentCodeAction
lsRequestId id;
lsCodeActionParams params;
};
MAKE_REFLECT_STRUCT(Ipc_TextDocumentCodeAction::lsCodeActionContext, diagnostics);
MAKE_REFLECT_STRUCT(Ipc_TextDocumentCodeAction::lsCodeActionParams, textDocument, range, context);
MAKE_REFLECT_STRUCT(Ipc_TextDocumentCodeAction::lsCodeActionContext,
diagnostics);
MAKE_REFLECT_STRUCT(Ipc_TextDocumentCodeAction::lsCodeActionParams,
textDocument,
range,
context);
MAKE_REFLECT_STRUCT(Ipc_TextDocumentCodeAction, id, params);
struct Out_TextDocumentCodeAction : public lsOutMessage<Out_TextDocumentCodeAction> {
struct Out_TextDocumentCodeAction
: public lsOutMessage<Out_TextDocumentCodeAction> {
struct CommandArgs {
lsDocumentUri textDocumentUri;
NonElidedVector<lsTextEdit> edits;
@ -1469,7 +1512,9 @@ struct Out_TextDocumentCodeAction : public lsOutMessage<Out_TextDocumentCodeActi
lsRequestId id;
NonElidedVector<Command> result;
};
MAKE_REFLECT_STRUCT_WRITER_AS_ARRAY(Out_TextDocumentCodeAction::CommandArgs, textDocumentUri, edits);
MAKE_REFLECT_STRUCT_WRITER_AS_ARRAY(Out_TextDocumentCodeAction::CommandArgs,
textDocumentUri,
edits);
MAKE_REFLECT_STRUCT(Out_TextDocumentCodeAction, jsonrpc, id, result);
// List symbols in a document.
@ -1477,21 +1522,24 @@ struct lsDocumentSymbolParams {
lsTextDocumentIdentifier textDocument;
};
MAKE_REFLECT_STRUCT(lsDocumentSymbolParams, textDocument);
struct Ipc_TextDocumentDocumentSymbol : public IpcMessage<Ipc_TextDocumentDocumentSymbol> {
struct Ipc_TextDocumentDocumentSymbol
: public IpcMessage<Ipc_TextDocumentDocumentSymbol> {
const static IpcId kIpcId = IpcId::TextDocumentDocumentSymbol;
lsRequestId id;
lsDocumentSymbolParams params;
};
MAKE_REFLECT_STRUCT(Ipc_TextDocumentDocumentSymbol, id, params);
struct Out_TextDocumentDocumentSymbol : public lsOutMessage<Out_TextDocumentDocumentSymbol> {
struct Out_TextDocumentDocumentSymbol
: public lsOutMessage<Out_TextDocumentDocumentSymbol> {
lsRequestId id;
NonElidedVector<lsSymbolInformation> result;
};
MAKE_REFLECT_STRUCT(Out_TextDocumentDocumentSymbol, jsonrpc, id, result);
// List links a document
struct Ipc_TextDocumentDocumentLink : public IpcMessage<Ipc_TextDocumentDocumentLink> {
struct Ipc_TextDocumentDocumentLink
: public IpcMessage<Ipc_TextDocumentDocumentLink> {
const static IpcId kIpcId = IpcId::TextDocumentDocumentLink;
struct DocumentLinkParams {
@ -1502,10 +1550,11 @@ struct Ipc_TextDocumentDocumentLink : public IpcMessage<Ipc_TextDocumentDocument
lsRequestId id;
DocumentLinkParams params;
};
MAKE_REFLECT_STRUCT(Ipc_TextDocumentDocumentLink::DocumentLinkParams, textDocument);
MAKE_REFLECT_STRUCT(Ipc_TextDocumentDocumentLink::DocumentLinkParams,
textDocument);
MAKE_REFLECT_STRUCT(Ipc_TextDocumentDocumentLink, id, params);
// A document link is a range in a text document that links to an internal or external resource, like another
// text document or a web site.
// A document link is a range in a text document that links to an internal or
// external resource, like another text document or a web site.
struct lsDocumentLink {
// The range this link applies to.
lsRange range;
@ -1513,13 +1562,13 @@ struct lsDocumentLink {
optional<lsDocumentUri> target;
};
MAKE_REFLECT_STRUCT(lsDocumentLink, range, target);
struct Out_TextDocumentDocumentLink : public lsOutMessage<Out_TextDocumentDocumentLink> {
struct Out_TextDocumentDocumentLink
: public lsOutMessage<Out_TextDocumentDocumentLink> {
lsRequestId id;
NonElidedVector<lsDocumentLink> result;
};
MAKE_REFLECT_STRUCT(Out_TextDocumentDocumentLink, jsonrpc, id, result);
// List code lens in a document.
struct lsDocumentCodeLensParams {
lsTextDocumentIdentifier textDocument;
@ -1541,9 +1590,11 @@ struct Ipc_TextDocumentCodeLens : public IpcMessage<Ipc_TextDocumentCodeLens> {
lsDocumentCodeLensParams params;
};
MAKE_REFLECT_STRUCT(Ipc_TextDocumentCodeLens, id, params);
struct Out_TextDocumentCodeLens : public lsOutMessage<Out_TextDocumentCodeLens> {
struct Out_TextDocumentCodeLens
: public lsOutMessage<Out_TextDocumentCodeLens> {
lsRequestId id;
NonElidedVector<lsCodeLens<lsCodeLensUserData, lsCodeLensCommandArguments>> result;
NonElidedVector<lsCodeLens<lsCodeLensUserData, lsCodeLensCommandArguments>>
result;
};
MAKE_REFLECT_STRUCT(Out_TextDocumentCodeLens, jsonrpc, id, result);
struct Ipc_CodeLensResolve : public IpcMessage<Ipc_CodeLensResolve> {
@ -1577,12 +1628,7 @@ struct Out_WorkspaceSymbol : public lsOutMessage<Out_WorkspaceSymbol> {
MAKE_REFLECT_STRUCT(Out_WorkspaceSymbol, jsonrpc, id, result);
// Show a message to the user.
enum class lsMessageType : int {
Error = 1,
Warning = 2,
Info = 3,
Log = 4
};
enum class lsMessageType : int { Error = 1, Warning = 2, Info = 3, Log = 4 };
MAKE_REFLECT_TYPE_PROXY(lsMessageType, int)
struct Out_ShowLogMessageParams {
lsMessageType type = lsMessageType::Error;
@ -1590,9 +1636,7 @@ struct Out_ShowLogMessageParams {
};
MAKE_REFLECT_STRUCT(Out_ShowLogMessageParams, type, message);
struct Out_ShowLogMessage : public lsOutMessage<Out_ShowLogMessage> {
enum class DisplayType {
Show, Log
};
enum class DisplayType { Show, Log };
DisplayType display_type = DisplayType::Show;
std::string method();
@ -1608,8 +1652,8 @@ void Reflect(TVisitor& visitor, Out_ShowLogMessage& value) {
REFLECT_MEMBER_END();
}
struct Out_CquerySetInactiveRegion : public lsOutMessage<Out_CquerySetInactiveRegion> {
struct Out_CquerySetInactiveRegion
: public lsOutMessage<Out_CquerySetInactiveRegion> {
struct Params {
lsDocumentUri uri;
NonElidedVector<lsRange> inactiveRegions;
@ -1620,7 +1664,6 @@ struct Out_CquerySetInactiveRegion : public lsOutMessage<Out_CquerySetInactiveRe
MAKE_REFLECT_STRUCT(Out_CquerySetInactiveRegion::Params, uri, inactiveRegions);
MAKE_REFLECT_STRUCT(Out_CquerySetInactiveRegion, jsonrpc, method, params);
struct Ipc_CqueryFreshenIndex : public IpcMessage<Ipc_CqueryFreshenIndex> {
const static IpcId kIpcId = IpcId::CqueryFreshenIndex;
lsRequestId id;
@ -1628,13 +1671,15 @@ struct Ipc_CqueryFreshenIndex : public IpcMessage<Ipc_CqueryFreshenIndex> {
MAKE_REFLECT_STRUCT(Ipc_CqueryFreshenIndex, id);
// Type Hierarchy Tree
struct Ipc_CqueryTypeHierarchyTree : public IpcMessage<Ipc_CqueryTypeHierarchyTree> {
struct Ipc_CqueryTypeHierarchyTree
: public IpcMessage<Ipc_CqueryTypeHierarchyTree> {
const static IpcId kIpcId = IpcId::CqueryTypeHierarchyTree;
lsRequestId id;
lsTextDocumentPositionParams params;
};
MAKE_REFLECT_STRUCT(Ipc_CqueryTypeHierarchyTree, id, params);
struct Out_CqueryTypeHierarchyTree : public lsOutMessage<Out_CqueryTypeHierarchyTree> {
struct Out_CqueryTypeHierarchyTree
: public lsOutMessage<Out_CqueryTypeHierarchyTree> {
struct TypeEntry {
std::string name;
optional<lsLocation> location;
@ -1643,11 +1688,15 @@ struct Out_CqueryTypeHierarchyTree : public lsOutMessage<Out_CqueryTypeHierarchy
lsRequestId id;
optional<TypeEntry> result;
};
MAKE_REFLECT_STRUCT(Out_CqueryTypeHierarchyTree::TypeEntry, name, location, children);
MAKE_REFLECT_STRUCT(Out_CqueryTypeHierarchyTree::TypeEntry,
name,
location,
children);
MAKE_REFLECT_STRUCT(Out_CqueryTypeHierarchyTree, jsonrpc, id, result);
// Call Tree
struct Ipc_CqueryCallTreeInitial : public IpcMessage<Ipc_CqueryCallTreeInitial> {
struct Ipc_CqueryCallTreeInitial
: public IpcMessage<Ipc_CqueryCallTreeInitial> {
const static IpcId kIpcId = IpcId::CqueryCallTreeInitial;
lsRequestId id;
lsTextDocumentPositionParams params;
@ -1664,9 +1713,7 @@ struct Ipc_CqueryCallTreeExpand : public IpcMessage<Ipc_CqueryCallTreeExpand> {
MAKE_REFLECT_STRUCT(Ipc_CqueryCallTreeExpand::Params, usr);
MAKE_REFLECT_STRUCT(Ipc_CqueryCallTreeExpand, id, params);
struct Out_CqueryCallTree : public lsOutMessage<Out_CqueryCallTree> {
enum class CallType {
Direct = 0, Base = 1, Derived = 2
};
enum class CallType { Direct = 0, Base = 1, Derived = 2 };
struct CallEntry {
std::string name;
std::string usr;
@ -1679,7 +1726,12 @@ struct Out_CqueryCallTree : public lsOutMessage<Out_CqueryCallTree> {
NonElidedVector<CallEntry> result;
};
MAKE_REFLECT_TYPE_PROXY(Out_CqueryCallTree::CallType, int);
MAKE_REFLECT_STRUCT(Out_CqueryCallTree::CallEntry, name, usr, location, hasCallers, callType);
MAKE_REFLECT_STRUCT(Out_CqueryCallTree::CallEntry,
name,
usr,
location,
hasCallers,
callType);
MAKE_REFLECT_STRUCT(Out_CqueryCallTree, jsonrpc, id, result);
// Vars, Callers, Derived, GotoParent

View File

@ -20,7 +20,9 @@ int GetOffsetForPosition(lsPosition position, const std::string& content) {
return std::min<int>(offset + position.character, content.size());
}
lsPosition CharPos(const std::string& search, char character, int character_offset) {
lsPosition CharPos(const std::string& search,
char character,
int character_offset) {
lsPosition result;
int index = 0;
while (index < search.size()) {
@ -30,8 +32,7 @@ lsPosition CharPos(const std::string& search, char character, int character_offs
if (c == '\n') {
result.line += 1;
result.character = 0;
}
else {
} else {
result.character += 1;
}
++index;
@ -75,7 +76,11 @@ optional<lsRange> ExtractQuotedRange(int line_number, const std::string& line) {
return lsRange(lsPosition(line_number, start), lsPosition(line_number, end));
}
void LexFunctionDeclaration(const std::string& buffer_content, lsPosition declaration_spelling, optional<std::string> type_name, std::string* insert_text, int* newlines_after_name) {
void LexFunctionDeclaration(const std::string& buffer_content,
lsPosition declaration_spelling,
optional<std::string> type_name,
std::string* insert_text,
int* newlines_after_name) {
int name_start = GetOffsetForPosition(declaration_spelling, buffer_content);
bool parse_return_type = true;
@ -89,7 +94,8 @@ void LexFunctionDeclaration(const std::string& buffer_content, lsPosition declar
++name_end;
}
std::string func_name = buffer_content.substr(name_start, name_end - name_start);
std::string func_name =
buffer_content.substr(name_start, name_end - name_start);
if (func_name == *type_name || func_name == ("~" + *type_name))
parse_return_type = false;
}
@ -163,8 +169,7 @@ std::string LexWordAroundPos(lsPosition position, const std::string& content) {
char c = content[start - 1];
if (isalnum(c) || c == '_') {
--start;
}
else {
} else {
break;
}
}
@ -173,8 +178,7 @@ std::string LexWordAroundPos(lsPosition position, const std::string& content) {
char c = content[end + 1];
if (isalnum(c) || c == '_') {
++end;
}
else {
} else {
break;
}
}

View File

@ -4,18 +4,23 @@
#include <string>
// Utility method to map |position| to an offset inside of |content|.
int GetOffsetForPosition(lsPosition position, const std::string& content);
// Utility method to find a position for the given character.
lsPosition CharPos(const std::string& search, char character, int character_offset = 0);
lsPosition CharPos(const std::string& search,
char character,
int character_offset = 0);
bool ShouldRunIncludeCompletion(const std::string& line);
// TODO: eliminate |line_number| param.
optional<lsRange> ExtractQuotedRange(int line_number, const std::string& line);
void LexFunctionDeclaration(const std::string& buffer_content, lsPosition declaration_spelling, optional<std::string> type_name, std::string* insert_text, int* newlines_after_name);
void LexFunctionDeclaration(const std::string& buffer_content,
lsPosition declaration_spelling,
optional<std::string> type_name,
std::string* insert_text,
int* newlines_after_name);
std::string LexWordAroundPos(lsPosition position, const std::string& content);

View File

@ -2,8 +2,9 @@
#define CURSOR_H_
#include <string>
#include <vector>
#include <type_traits>
#include <vector>
#include <clang-c/Index.h>

View File

@ -1,13 +1,15 @@
#include "TranslationUnit.h"
#include "Utility.h"
#include "../platform.h"
#include "../utils.h"
#include "Utility.h"
#include <fstream>
#include <sstream>
#include <cassert>
#include <fstream>
#include <iostream>
#include <sstream>
namespace clang {
@ -24,10 +26,12 @@ TranslationUnit::TranslationUnit(Index* index,
for (const auto& arg : platform_args)
args.push_back(arg.c_str());
//std::cerr << "Parsing " << filepath << " with args " << StringJoin(args) << std::endl;
// std::cerr << "Parsing " << filepath << " with args " << StringJoin(args) <<
// std::endl;
// cx_tu = clang_createTranslationUnitFromSourceFile(
// index->cx_index, filepath.c_str(), args.size(), args.data(), (unsigned)unsaved_files.size(), unsaved_files.data());
// index->cx_index, filepath.c_str(), args.size(), args.data(),
// (unsigned)unsaved_files.size(), unsaved_files.data());
CXErrorCode error_code = clang_parseTranslationUnit2(
index->cx_index, filepath.c_str(), args.data(), (int)args.size(),
@ -38,20 +42,23 @@ TranslationUnit::TranslationUnit(Index* index,
did_fail = false;
break;
case CXError_Failure:
std::cerr << "libclang generic failure for " << filepath << " with args " << StringJoin(args) << std::endl;
std::cerr << "libclang generic failure for " << filepath << " with args "
<< StringJoin(args) << std::endl;
did_fail = true;
break;
case CXError_Crashed:
std::cerr << "libclang crashed for " << filepath << " with args " << StringJoin(args) << std::endl;
std::cerr << "libclang crashed for " << filepath << " with args "
<< StringJoin(args) << std::endl;
did_fail = true;
break;
case CXError_InvalidArguments:
std::cerr << "libclang had invalid arguments for " << " with args " << StringJoin(args) << filepath
<< std::endl;
std::cerr << "libclang had invalid arguments for "
<< " with args " << StringJoin(args) << filepath << std::endl;
did_fail = true;
break;
case CXError_ASTReadError:
std::cerr << "libclang had ast read error for " << filepath << " with args " << StringJoin(args) << std::endl;
std::cerr << "libclang had ast read error for " << filepath
<< " with args " << StringJoin(args) << std::endl;
did_fail = true;
break;
}
@ -63,8 +70,8 @@ TranslationUnit::~TranslationUnit() {
void TranslationUnit::ReparseTranslationUnit(
std::vector<CXUnsavedFile>& unsaved) {
int error_code =
clang_reparseTranslationUnit(cx_tu, (unsigned)unsaved.size(), unsaved.data(),
int error_code = clang_reparseTranslationUnit(
cx_tu, (unsigned)unsaved.size(), unsaved.data(),
clang_defaultReparseOptions(cx_tu));
switch (error_code) {
case CXError_Success:
@ -92,4 +99,4 @@ void TranslationUnit::ReparseTranslationUnit(
Cursor TranslationUnit::document_cursor() const {
return Cursor(clang_getTranslationUnitCursor(cx_tu));
}
}
} // namespace clang

View File

@ -1,8 +1,8 @@
#pragma once
#include "Index.h"
#include "Cursor.h"
#include "Index.h"
#include <clang-c/Index.h>

View File

@ -21,19 +21,18 @@ optional<Matcher> Matcher::Create(const std::string& search) {
try {
Matcher m;
m.regex_string = search;
m.regex = std::regex(search,
std::regex_constants::ECMAScript |
std::regex_constants::icase |
m.regex = std::regex(
search, std::regex_constants::ECMAScript | std::regex_constants::icase |
std::regex_constants::optimize
// std::regex_constants::nosubs
);
return m;
}
catch (std::exception e) {
} catch (std::exception e) {
Out_ShowLogMessage out;
out.display_type = Out_ShowLogMessage::DisplayType::Show;
out.params.type = lsMessageType::Error;
out.params.message = "cquery: Parsing EMCAScript regex \"" + search + "\" failed; " + e.what();
out.params.message = "cquery: Parsing EMCAScript regex \"" + search +
"\" failed; " + e.what();
IpcManager::instance()->SendOutMessageToClient(IpcId::Cout, out);
return nullopt;
}
@ -45,8 +44,7 @@ bool Matcher::IsMatch(const std::string& value) const {
return std::regex_match(value, regex, std::regex_constants::match_any);
}
GroupMatch::GroupMatch(
const std::vector<std::string>& whitelist,
GroupMatch::GroupMatch(const std::vector<std::string>& whitelist,
const std::vector<std::string>& blacklist) {
for (const std::string& entry : whitelist) {
optional<Matcher> m = Matcher::Create(entry);
@ -60,7 +58,8 @@ GroupMatch::GroupMatch(
}
}
bool GroupMatch::IsMatch(const std::string& value, std::string* match_failure_reason) const {
bool GroupMatch::IsMatch(const std::string& value,
std::string* match_failure_reason) const {
for (const Matcher& m : whitelist) {
if (!m.IsMatch(value)) {
if (match_failure_reason)
@ -80,7 +79,6 @@ bool GroupMatch::IsMatch(const std::string& value, std::string* match_failure_re
return true;
}
TEST_SUITE("Matcher");
TEST_CASE("sanity") {

View File

@ -6,8 +6,8 @@
#include <string>
#include <vector>
using std::experimental::optional;
using std::experimental::nullopt;
using std::experimental::optional;
struct Matcher {
static optional<Matcher> Create(const std::string& search);
@ -23,7 +23,8 @@ struct GroupMatch {
GroupMatch(const std::vector<std::string>& whitelist,
const std::vector<std::string>& blacklist);
bool IsMatch(const std::string& value, std::string* match_failure_reason = nullptr) const;
bool IsMatch(const std::string& value,
std::string* match_failure_reason = nullptr) const;
std::vector<Matcher> whitelist;
std::vector<Matcher> blacklist;

View File

@ -162,7 +162,6 @@ MessageQueue::MessageQueue(std::unique_ptr<Buffer> buffer, bool buffer_has_data)
local_buffer_ = Buffer::Create(buffer_->capacity - sizeof(BufferMetadata));
memset(local_buffer_->data, 0, local_buffer_->capacity);
}
void MessageQueue::Enqueue(const Message& message) {

View File

@ -1,8 +1,9 @@
#pragma once
#include <vector>
#include <memory>
#include <unordered_map>
#include <vector>
#include "buffer.h"

View File

@ -20,8 +20,7 @@ std::unordered_map<std::string, std::string> ParseOptions(int argc,
output[previous_arg] = arg;
previous_arg = "";
}
else {
} else {
output[arg] = "";
previous_arg = arg;
}

View File

@ -2,10 +2,8 @@
#include <unordered_map>
std::unordered_map<std::string, std::string> ParseOptions(
int argc,
std::unordered_map<std::string, std::string> ParseOptions(int argc,
char** argv);
bool HasOption(
const std::unordered_map<std::string, std::string>& options,
bool HasOption(const std::unordered_map<std::string, std::string>& options,
const std::string& option);

View File

@ -26,4 +26,10 @@ struct PerformanceImportFile {
// [querydb] apply IndexUpdate
// uint64_t querydb_apply_index_update = 0;
};
MAKE_REFLECT_STRUCT(PerformanceImportFile, index_parse, index_build, querydb_id_map, index_save_to_disk, index_load_cached, index_make_delta);
MAKE_REFLECT_STRUCT(PerformanceImportFile,
index_parse,
index_build,
querydb_id_map,
index_save_to_disk,
index_load_cached,
index_make_delta);

View File

@ -2,11 +2,12 @@
#include <iostream>
#include <iterator>
#include <string>
#include <sstream>
#include <string>
#include <thread>
#include <vector>
#include <doctest/doctest.h>
namespace {
@ -70,14 +71,16 @@ void MakeDirectoryRecursive(std::string path) {
}
if (first_success == -1) {
std::cerr << "Failed to make any parent directory for " << path << std::endl;
std::cerr << "Failed to make any parent directory for " << path
<< std::endl;
exit(1);
}
// Make all child directories.
for (int i = first_success + 1; i <= components.size(); ++i) {
if (TryMakeDirectory(prefix + Join(components, '/', i)) == false) {
std::cerr << "Failed making directory for " << path << " even after creating parent directories" << std::endl;
std::cerr << "Failed making directory for " << path
<< " even after creating parent directories" << std::endl;
exit(1);
}
}

View File

@ -6,8 +6,8 @@
#include <string>
#include <vector>
using std::experimental::optional;
using std::experimental::nullopt;
using std::experimental::optional;
struct PlatformMutex {
virtual ~PlatformMutex();

View File

@ -6,34 +6,39 @@
#include <loguru.hpp>
#include <malloc.h>
#include <cassert>
#include <string>
#include <pthread.h>
#include <cassert>
#include <iostream>
#include <string>
#include <unistd.h>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <semaphore.h>
#include <time.h>
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <semaphore.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h> // required for stat.h
#include <sys/stat.h>
#include <sys/types.h> // required for stat.h
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h> /* For O_* constants */
#include <sys/stat.h> /* For mode constants */
#include <semaphore.h>
#include <sys/mman.h>
#include <sys/stat.h> /* For mode constants */
#ifndef __APPLE__
#include <sys/prctl.h>
@ -86,7 +91,8 @@ struct PlatformSharedMemoryLinux : public PlatformSharedMemory {
PlatformSharedMemoryLinux(const std::string& name, size_t size)
: name_(name), size_(size) {
std::cerr << "PlatformSharedMemoryLinux name=" << name << ", size=" << size << std::endl;
std::cerr << "PlatformSharedMemoryLinux name=" << name << ", size=" << size
<< std::endl;
// Try to create shared memory but only if it does not already exist. Since
// we created the memory, we need to initialize it.
@ -105,8 +111,7 @@ struct PlatformSharedMemoryLinux : public PlatformSharedMemory {
}
// Map the shared memory to an address.
data =
CHECKED(mmap(nullptr /*kernel assigned starting address*/, size,
data = CHECKED(mmap(nullptr /*kernel assigned starting address*/, size,
PROT_READ | PROT_WRITE, MAP_SHARED, fd_, 0 /*offset*/));
capacity = size;
@ -134,13 +139,13 @@ std::unique_ptr<PlatformScopedMutexLock> CreatePlatformScopedMutexLock(
}
std::unique_ptr<PlatformSharedMemory> CreatePlatformSharedMemory(
const std::string& name, size_t size) {
const std::string& name,
size_t size) {
std::string name2 = "/" + name;
return MakeUnique<PlatformSharedMemoryLinux>(name2, size);
}
void PlatformInit() {
}
void PlatformInit() {}
std::string NormalizePath(const std::string& path) {
errno = 0;
@ -172,14 +177,16 @@ optional<int64_t> GetLastModificationTime(const std::string& absolute_path) {
if (stat(absolute_path.c_str(), &buf) != 0) {
switch (errno) {
case ENOENT:
//std::cerr << "GetLastModificationTime: unable to find file " << absolute_path << std::endl;
// std::cerr << "GetLastModificationTime: unable to find file " <<
// absolute_path << std::endl;
return nullopt;
case EINVAL:
//std::cerr << "GetLastModificationTime: invalid param to _stat for file file " << absolute_path << std::endl;
// std::cerr << "GetLastModificationTime: invalid param to _stat for
// file file " << absolute_path << std::endl;
return nullopt;
default:
//std::cerr << "GetLastModificationTime: unhandled for " << absolute_path << std::endl;
//exit(1);
// std::cerr << "GetLastModificationTime: unhandled for " <<
// absolute_path << std::endl; exit(1);
return nullopt;
}
}
@ -214,8 +221,7 @@ void CopyFileTo(const std::string& dest, const std::string& source) {
if (nwritten >= 0) {
nread -= nwritten;
out_ptr += nwritten;
}
else if (errno != EINTR)
} else if (errno != EINTR)
goto out_error;
} while (nread > 0);
}

View File

@ -5,13 +5,15 @@
#include <loguru.hpp>
#include <Windows.h>
#include <direct.h>
#include <fcntl.h>
#include <io.h>
#include <Windows.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <algorithm>
#include <cassert>
@ -161,8 +163,7 @@ bool TryMakeDirectory(const std::string& absolute_path) {
// See https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
const DWORD MS_VC_EXCEPTION = 0x406D1388;
#pragma pack(push, 8)
typedef struct tagTHREADNAME_INFO
{
typedef struct tagTHREADNAME_INFO {
DWORD dwType; // Must be 0x1000.
LPCSTR szName; // Pointer to name (in user addr space).
DWORD dwThreadID; // Thread ID (-1=caller thread).
@ -179,9 +180,10 @@ void SetCurrentThreadName(const std::string& thread_name) {
info.dwFlags = 0;
__try {
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info);
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR),
(ULONG_PTR*)&info);
} __except (EXCEPTION_EXECUTE_HANDLER) {
}
__except (EXCEPTION_EXECUTE_HANDLER) {}
}
optional<int64_t> GetLastModificationTime(const std::string& absolute_path) {
@ -189,14 +191,16 @@ optional<int64_t> GetLastModificationTime(const std::string& absolute_path) {
if (_stat(absolute_path.c_str(), &buf) != 0) {
switch (errno) {
case ENOENT:
//std::cerr << "GetLastModificationTime: unable to find file " << absolute_path << std::endl;
// std::cerr << "GetLastModificationTime: unable to find file " <<
// absolute_path << std::endl;
return nullopt;
case EINVAL:
//std::cerr << "GetLastModificationTime: invalid param to _stat for file file " << absolute_path << std::endl;
// std::cerr << "GetLastModificationTime: invalid param to _stat for
// file file " << absolute_path << std::endl;
return nullopt;
default:
//std::cerr << "GetLastModificationTime: unhandled for " << absolute_path << std::endl;
//exit(1);
// std::cerr << "GetLastModificationTime: unhandled for " <<
// absolute_path << std::endl; exit(1);
return nullopt;
}
}
@ -205,15 +209,11 @@ optional<int64_t> GetLastModificationTime(const std::string& absolute_path) {
}
void MoveFileTo(const std::string& destination, const std::string& source) {
MoveFile(source.c_str(),
destination.c_str());
MoveFile(source.c_str(), destination.c_str());
}
void CopyFileTo(const std::string& destination, const std::string& source) {
CopyFile(
source.c_str(),
destination.c_str(),
false /*failIfExists*/);
CopyFile(source.c_str(), destination.c_str(), false /*failIfExists*/);
}
bool IsSymLink(const std::string& path) {
@ -221,10 +221,7 @@ bool IsSymLink(const std::string& path) {
}
std::vector<std::string> GetPlatformClangArguments() {
return {
"-fms-compatibility",
"-fdelayed-template-parsing"
};
return {"-fms-compatibility", "-fdelayed-template-parsing"};
}
void FreeUnusedMemory() {}

View File

@ -12,8 +12,7 @@ const char* SkipAfter(const char* input, char skip_after) {
Position::Position() {}
Position::Position(int16_t line, int16_t column)
: line(line), column(column) {}
Position::Position(int16_t line, int16_t column) : line(line), column(column) {}
Position::Position(const char* encoded) {
assert(encoded);
@ -61,7 +60,9 @@ bool Position::operator==(const Position& that) const {
return line == that.line && column == that.column;
}
bool Position::operator!=(const Position& that) const { return !(*this == that); }
bool Position::operator!=(const Position& that) const {
return !(*this == that);
}
bool Position::operator<(const Position& that) const {
if (line < that.line)
@ -133,7 +134,9 @@ bool Range::operator==(const Range& that) const {
return start == that.start && end == that.end;
}
bool Range::operator!=(const Range& that) const { return !(*this == that); }
bool Range::operator!=(const Range& that) const {
return !(*this == that);
}
bool Range::operator<(const Range& that) const {
if (start < that.start)
@ -150,7 +153,6 @@ void Reflect(Writer& visitor, Position& value) {
visitor.String(output.c_str(), (rapidjson::SizeType)output.size());
}
// Range
void Reflect(Reader& visitor, Range& value) {
value = Range(visitor.GetString());

View File

@ -24,7 +24,9 @@ struct Position {
bool operator!=(const Position& that) const;
bool operator<(const Position& that) const;
};
static_assert(sizeof(Position) == 4, "Investigate, Position should be 32-bits for indexer size reasons");
static_assert(
sizeof(Position) == 4,
"Investigate, Position should be 32-bits for indexer size reasons");
MAKE_HASHABLE(Position, t.line, t.column);
struct Range {

View File

@ -1,11 +1,12 @@
#include "project.h"
#include "match.h"
#include "libclangmm/Utility.h"
#include "match.h"
#include "platform.h"
#include "serializer.h"
#include "utils.h"
#include <clang-c/CXCompilationDatabase.h>
#include <doctest/doctest.h>
#include <loguru.hpp>
@ -26,49 +27,22 @@ MAKE_REFLECT_STRUCT(CompileCommandsEntry, directory, file, command, args);
namespace {
static const char* kBlacklistMulti[] = {
"-MF",
"-Xclang"
};
static const char* kBlacklistMulti[] = {"-MF", "-Xclang"};
// Blacklisted flags which are always removed from the command line.
static const char* kBlacklist[] = {
"--param",
"-M",
"-MD",
"-MG",
"-MM",
"-MMD",
"-MP",
"-MQ",
"-MT",
"-Og",
"-Wa,--32",
"-Wa,--64",
"-Wl,--incremental-full",
"-Wl,--incremental-patch,1",
"-Wl,--no-incremental",
"-fbuild-session-file=",
"-fbuild-session-timestamp=",
"-fembed-bitcode",
"-fembed-bitcode-marker",
"-fmodules-validate-once-per-build-session",
"--param", "-M", "-MD", "-MG", "-MM", "-MMD", "-MP", "-MQ", "-MT", "-Og",
"-Wa,--32", "-Wa,--64", "-Wl,--incremental-full",
"-Wl,--incremental-patch,1", "-Wl,--no-incremental",
"-fbuild-session-file=", "-fbuild-session-timestamp=", "-fembed-bitcode",
"-fembed-bitcode-marker", "-fmodules-validate-once-per-build-session",
"-fno-delete-null-pointer-checks",
"-fno-use-linker-plugin"
"-fno-var-tracking",
"-fno-var-tracking-assignments",
"-fno-enforce-eh-specs",
"-fvar-tracking",
"-fvar-tracking-assignments",
"-fvar-tracking-assignments-toggle",
"-fno-var-tracking-assignments", "-fno-enforce-eh-specs", "-fvar-tracking",
"-fvar-tracking-assignments", "-fvar-tracking-assignments-toggle",
"-gcc-toolchain",
"-march=",
"-masm=",
"-mcpu=",
"-mfpmath=",
"-mtune=",
"-s",
"-march=", "-masm=", "-mcpu=", "-mfpmath=", "-mtune=", "-s",
"-B",
//"-f",
@ -77,28 +51,16 @@ static const char *kBlacklist[] = {
// TODO: make sure we consume includes before stripping all path-like args.
"/work/goma/gomacc",
"../../third_party/llvm-build/Release+Asserts/bin/clang++",
"-Wno-unused-lambda-capture",
"/",
"..",
"-Wno-unused-lambda-capture", "/", "..",
//"-stdlib=libc++"
};
// Arguments which are followed by a potentially relative path. We need to make
// all relative paths absolute, otherwise libclang will not resolve them.
const char* kPathArgs[] = {
"-I",
"-iquote",
"-isystem",
"--sysroot="
};
const char* kPathArgs[] = {"-I", "-iquote", "-isystem", "--sysroot="};
const char* kQuoteIncludeArgs[] = {
"-iquote"
};
const char* kAngleIncludeArgs[] = {
"-I",
"-isystem"
};
const char* kQuoteIncludeArgs[] = {"-iquote"};
const char* kAngleIncludeArgs[] = {"-I", "-isystem"};
bool ShouldAddToQuoteIncludes(const std::string& arg) {
for (const char* flag_type : kQuoteIncludeArgs) {
@ -122,8 +84,10 @@ bool IsCFile(const std::string& path) {
}
Project::Entry GetCompilationEntryFromCompileCommandEntry(
std::unordered_set<std::string>& quote_includes, std::unordered_set<std::string>& angle_includes,
const std::vector<std::string>& extra_flags, const CompileCommandsEntry& entry) {
std::unordered_set<std::string>& quote_includes,
std::unordered_set<std::string>& angle_includes,
const std::vector<std::string>& extra_flags,
const CompileCommandsEntry& entry) {
Project::Entry result;
result.filename = NormalizePath(entry.file);
@ -136,15 +100,15 @@ Project::Entry GetCompilationEntryFromCompileCommandEntry(
std::string arg = entry.args[i];
// If blacklist skip.
if (std::any_of(std::begin(kBlacklistMulti), std::end(kBlacklistMulti), [&arg](const char* value) {
return StartsWith(arg, value);
})) {
if (std::any_of(
std::begin(kBlacklistMulti), std::end(kBlacklistMulti),
[&arg](const char* value) { return StartsWith(arg, value); })) {
++i;
continue;
}
if (std::any_of(std::begin(kBlacklist), std::end(kBlacklist), [&arg](const char* value) {
return StartsWith(arg, value);
})) {
if (std::any_of(
std::begin(kBlacklist), std::end(kBlacklist),
[&arg](const char* value) { return StartsWith(arg, value); })) {
continue;
}
@ -215,12 +179,12 @@ Project::Entry GetCompilationEntryFromCompileCommandEntry(
/* TODO: Fix this function, it may be way faster than libclang's implementation.
std::vector<Project::Entry> LoadFromCompileCommandsJson(
std::unordered_set<std::string>& quote_includes, std::unordered_set<std::string>& angle_includes,
const std::vector<std::string>& extra_flags, const std::string& project_directory) {
std::unordered_set<std::string>& quote_includes,
std::unordered_set<std::string>& angle_includes, const std::vector<std::string>&
extra_flags, const std::string& project_directory) {
optional<std::string> compile_commands_content = ReadContent(project_directory + "/compile_commands.json");
if (!compile_commands_content)
return {};
optional<std::string> compile_commands_content = ReadContent(project_directory
+ "/compile_commands.json"); if (!compile_commands_content) return {};
rapidjson::Document reader;
reader.Parse(compile_commands_content->c_str());
@ -236,15 +200,18 @@ std::vector<Project::Entry> LoadFromCompileCommandsJson(
if (entry.args.empty() && !entry.command.empty())
entry.args = SplitString(entry.command, " ");
result.push_back(GetCompilationEntryFromCompileCommandEntry(quote_includes, angle_includes, extra_flags, entry));
result.push_back(GetCompilationEntryFromCompileCommandEntry(quote_includes,
angle_includes, extra_flags, entry));
}
return result;
}
*/
std::vector<Project::Entry> LoadFromDirectoryListing(
std::unordered_set<std::string>& quote_includes, std::unordered_set<std::string>& angle_includes,
const std::vector<std::string>& extra_flags, const std::string& project_directory) {
std::unordered_set<std::string>& quote_includes,
std::unordered_set<std::string>& angle_includes,
const std::vector<std::string>& extra_flags,
const std::string& project_directory) {
std::vector<Project::Entry> result;
std::vector<std::string> args;
@ -259,14 +226,16 @@ std::vector<Project::Entry> LoadFromDirectoryListing(
}
std::cerr << std::endl;
std::vector<std::string> files = GetFilesInFolder(project_directory, true /*recursive*/, true /*add_folder_to_path*/);
std::vector<std::string> files = GetFilesInFolder(
project_directory, true /*recursive*/, true /*add_folder_to_path*/);
for (const std::string& file : files) {
if (EndsWith(file, ".cc") || EndsWith(file, ".cpp") || EndsWith(file, ".c")) {
if (EndsWith(file, ".cc") || EndsWith(file, ".cpp") ||
EndsWith(file, ".c")) {
CompileCommandsEntry e;
e.file = NormalizePath(file);
e.args = args;
result.push_back(GetCompilationEntryFromCompileCommandEntry(quote_includes, angle_includes, extra_flags, e));
result.push_back(GetCompilationEntryFromCompileCommandEntry(
quote_includes, angle_includes, extra_flags, e));
}
}
@ -274,28 +243,38 @@ std::vector<Project::Entry> LoadFromDirectoryListing(
}
std::vector<Project::Entry> LoadCompilationEntriesFromDirectory(
std::unordered_set<std::string>& quote_includes, std::unordered_set<std::string>& angle_includes,
const std::vector<std::string>& extra_flags, const std::string& project_directory) {
std::unordered_set<std::string>& quote_includes,
std::unordered_set<std::string>& angle_includes,
const std::vector<std::string>& extra_flags,
const std::string& project_directory) {
// TODO: Figure out if this function or the clang one is faster.
// return LoadFromCompileCommandsJson(extra_flags, project_directory);
std::cerr << "Trying to load compile_commands.json" << std::endl;
CXCompilationDatabase_Error cx_db_load_error;
CXCompilationDatabase cx_db = clang_CompilationDatabase_fromDirectory(project_directory.c_str(), &cx_db_load_error);
CXCompilationDatabase cx_db = clang_CompilationDatabase_fromDirectory(
project_directory.c_str(), &cx_db_load_error);
if (cx_db_load_error == CXCompilationDatabase_CanNotLoadDatabase) {
std::cerr << "Unable to load compile_commands.json located at \"" << project_directory << "\"; using directory listing instead." << std::endl;
return LoadFromDirectoryListing(quote_includes, angle_includes, extra_flags, project_directory);
std::cerr << "Unable to load compile_commands.json located at \""
<< project_directory << "\"; using directory listing instead."
<< std::endl;
return LoadFromDirectoryListing(quote_includes, angle_includes, extra_flags,
project_directory);
}
CXCompileCommands cx_commands = clang_CompilationDatabase_getAllCompileCommands(cx_db);
CXCompileCommands cx_commands =
clang_CompilationDatabase_getAllCompileCommands(cx_db);
unsigned int num_commands = clang_CompileCommands_getSize(cx_commands);
std::vector<Project::Entry> result;
for (unsigned int i = 0; i < num_commands; i++) {
CXCompileCommand cx_command = clang_CompileCommands_getCommand(cx_commands, i);
CXCompileCommand cx_command =
clang_CompileCommands_getCommand(cx_commands, i);
std::string directory = clang::ToString(clang_CompileCommand_getDirectory(cx_command));
std::string relative_filename = clang::ToString(clang_CompileCommand_getFilename(cx_command));
std::string directory =
clang::ToString(clang_CompileCommand_getDirectory(cx_command));
std::string relative_filename =
clang::ToString(clang_CompileCommand_getFilename(cx_command));
std::string absolute_filename = directory + "/" + relative_filename;
CompileCommandsEntry entry;
@ -305,9 +284,11 @@ std::vector<Project::Entry> LoadCompilationEntriesFromDirectory(
unsigned num_args = clang_CompileCommand_getNumArgs(cx_command);
entry.args.reserve(num_args);
for (unsigned j = 0; j < num_args; ++j)
entry.args.push_back(clang::ToString(clang_CompileCommand_getArg(cx_command, j)));
entry.args.push_back(
clang::ToString(clang_CompileCommand_getArg(cx_command, j)));
result.push_back(GetCompilationEntryFromCompileCommandEntry(quote_includes, angle_includes, extra_flags, entry));
result.push_back(GetCompilationEntryFromCompileCommandEntry(
quote_includes, angle_includes, extra_flags, entry));
}
clang_CompileCommands_dispose(cx_commands);
@ -356,14 +337,18 @@ int ComputeGuessScore(const std::string& a, const std::string& b) {
} // namespace
void Project::Load(const std::vector<std::string>& extra_flags, const std::string& directory) {
void Project::Load(const std::vector<std::string>& extra_flags,
const std::string& directory) {
std::unordered_set<std::string> unique_quote_includes;
std::unordered_set<std::string> unique_angle_includes;
entries = LoadCompilationEntriesFromDirectory(unique_quote_includes, unique_angle_includes, extra_flags, directory);
entries = LoadCompilationEntriesFromDirectory(
unique_quote_includes, unique_angle_includes, extra_flags, directory);
quote_include_directories.assign(unique_quote_includes.begin(), unique_quote_includes.end());
angle_include_directories.assign(unique_angle_includes.begin(), unique_angle_includes.end());
quote_include_directories.assign(unique_quote_includes.begin(),
unique_quote_includes.end());
angle_include_directories.assign(unique_angle_includes.begin(),
unique_angle_includes.end());
for (std::string& path : quote_include_directories) {
EnsureEndsInSlash(path);
@ -379,7 +364,8 @@ void Project::Load(const std::vector<std::string>& extra_flags, const std::strin
absolute_path_to_entry_index_[entries[i].filename] = i;
}
Project::Entry Project::FindCompilationEntryForFile(const std::string& filename) {
Project::Entry Project::FindCompilationEntryForFile(
const std::string& filename) {
auto it = absolute_path_to_entry_index_.find(filename);
if (it != absolute_path_to_entry_index_.end())
return entries[it->second];
@ -404,7 +390,9 @@ Project::Entry Project::FindCompilationEntryForFile(const std::string& filename)
return result;
}
void Project::ForAllFilteredFiles(Config* config, std::function<void(int i, const Entry& entry)> action) {
void Project::ForAllFilteredFiles(
Config* config,
std::function<void(int i, const Entry& entry)> action) {
GroupMatch matcher(config->indexWhitelist, config->indexBlacklist);
for (int i = 0; i < entries.size(); ++i) {
const Project::Entry& entry = entries[i];
@ -413,7 +401,8 @@ void Project::ForAllFilteredFiles(Config* config, std::function<void(int i, cons
action(i, entries[i]);
else {
if (config->logSkippedPathsForIndex) {
LOG_S(INFO) << "[" << i + 1 << "/" << entries.size() << "]: Failed " << failure_reason << "; skipping " << entry.filename;
LOG_S(INFO) << "[" << i + 1 << "/" << entries.size() << "]: Failed "
<< failure_reason << "; skipping " << entry.filename;
}
}
}
@ -438,21 +427,24 @@ TEST_CASE("Entry inference") {
// Guess at same directory level, when there are parent directories.
{
optional<Project::Entry> entry = p.FindCompilationEntryForFile("/a/b/c/d/new.cc");
optional<Project::Entry> entry =
p.FindCompilationEntryForFile("/a/b/c/d/new.cc");
REQUIRE(entry.has_value());
REQUIRE(entry->args == std::vector<std::string>{"arg1"});
}
// Guess at same directory level, when there are child directories.
{
optional<Project::Entry> entry = p.FindCompilationEntryForFile("/a/b/c/new.cc");
optional<Project::Entry> entry =
p.FindCompilationEntryForFile("/a/b/c/new.cc");
REQUIRE(entry.has_value());
REQUIRE(entry->args == std::vector<std::string>{"arg2"});
}
// Guess at new directory (use the closest parent directory).
{
optional<Project::Entry> entry = p.FindCompilationEntryForFile("/a/b/c/new/new.cc");
optional<Project::Entry> entry =
p.FindCompilationEntryForFile("/a/b/c/new/new.cc");
REQUIRE(entry.has_value());
REQUIRE(entry->args == std::vector<std::string>{"arg2"});
}
@ -479,32 +471,36 @@ TEST_CASE("Entry inference prefers same file endings") {
p.entries.push_back(e);
}
// Prefer files with the same ending.
{
optional<Project::Entry> entry = p.FindCompilationEntryForFile("my_browsertest.cc");
optional<Project::Entry> entry =
p.FindCompilationEntryForFile("my_browsertest.cc");
REQUIRE(entry.has_value());
REQUIRE(entry->args == std::vector<std::string>{"arg1"});
}
{
optional<Project::Entry> entry = p.FindCompilationEntryForFile("my_unittest.cc");
optional<Project::Entry> entry =
p.FindCompilationEntryForFile("my_unittest.cc");
REQUIRE(entry.has_value());
REQUIRE(entry->args == std::vector<std::string>{"arg2"});
}
{
optional<Project::Entry> entry = p.FindCompilationEntryForFile("common/my_browsertest.cc");
optional<Project::Entry> entry =
p.FindCompilationEntryForFile("common/my_browsertest.cc");
REQUIRE(entry.has_value());
REQUIRE(entry->args == std::vector<std::string>{"arg1"});
}
{
optional<Project::Entry> entry = p.FindCompilationEntryForFile("common/my_unittest.cc");
optional<Project::Entry> entry =
p.FindCompilationEntryForFile("common/my_unittest.cc");
REQUIRE(entry.has_value());
REQUIRE(entry->args == std::vector<std::string>{"arg2"});
}
// Prefer the same directory over matching file-ending.
{
optional<Project::Entry> entry = p.FindCompilationEntryForFile("common/a/foo.cc");
optional<Project::Entry> entry =
p.FindCompilationEntryForFile("common/a/foo.cc");
REQUIRE(entry.has_value());
REQUIRE(entry->args == std::vector<std::string>{"arg3"});
}

View File

@ -10,8 +10,8 @@
#include <string>
#include <vector>
using std::experimental::optional;
using std::experimental::nullopt;
using std::experimental::optional;
struct Project {
struct Entry {
@ -35,12 +35,14 @@ struct Project {
// discover all files and args. Otherwise, a recursive directory listing of
// all *.cpp, *.cc, *.h, and *.hpp files will be used. clang arguments can be
// specified in a clang_args file located inside of |directory|.
void Load(const std::vector<std::string>& extra_flags, const std::string& directory);
void Load(const std::vector<std::string>& extra_flags,
const std::string& directory);
// Lookup the CompilationEntry for |filename|. If no entry was found this
// will infer one based on existing project structure.
Entry FindCompilationEntryForFile(const std::string& filename);
void ForAllFilteredFiles(Config* config, std::function<void(int i, const Entry& entry)> action);
void ForAllFilteredFiles(
Config* config,
std::function<void(int i, const Entry& entry)> action);
};

View File

@ -2,24 +2,26 @@
#include "indexer.h"
#include <optional.h>
#include <doctest/doctest.h>
#include <optional.h>
#include <loguru.hpp>
#include <cassert>
#include <cstdint>
#include <functional>
#include <unordered_set>
#include <unordered_map>
#include <string>
#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
// TODO: Make all copy constructors explicit.
namespace {
optional<QueryType::DefUpdate> ToQuery(const IdMap& id_map, const IndexType::Def& type) {
optional<QueryType::DefUpdate> ToQuery(const IdMap& id_map,
const IndexType::Def& type) {
if (type.detailed_name.empty())
return nullopt;
@ -36,7 +38,8 @@ optional<QueryType::DefUpdate> ToQuery(const IdMap& id_map, const IndexType::Def
return result;
}
optional<QueryFunc::DefUpdate> ToQuery(const IdMap& id_map, const IndexFunc::Def& func) {
optional<QueryFunc::DefUpdate> ToQuery(const IdMap& id_map,
const IndexFunc::Def& func) {
if (func.detailed_name.empty())
return nullopt;
@ -52,7 +55,8 @@ optional<QueryFunc::DefUpdate> ToQuery(const IdMap& id_map, const IndexFunc::Def
return result;
}
optional<QueryVar::DefUpdate> ToQuery(const IdMap& id_map, const IndexVar::Def& var) {
optional<QueryVar::DefUpdate> ToQuery(const IdMap& id_map,
const IndexVar::Def& var) {
if (var.detailed_name.empty())
return nullopt;
@ -69,7 +73,6 @@ optional<QueryVar::DefUpdate> ToQuery(const IdMap& id_map, const IndexVar::Def&
return result;
}
// Adds the mergeable updates in |source| to |dest|. If a mergeable update for
// the destination type already exists, it will be combined. This makes merging
// updates take longer but reduces import time on the querydb thread.
@ -77,7 +80,6 @@ template <typename TId, typename TValue>
void AddMergeableRange(
std::vector<MergeableUpdate<TId, TValue>>* dest,
const std::vector<MergeableUpdate<TId, TValue>>& source) {
// TODO: Consider caching the lookup table. It can probably save even more
// time at the cost of some additional memory.
@ -93,8 +95,7 @@ void AddMergeableRange(
if (it != id_to_index.end()) {
AddRange(&(*dest)[it->second].to_add, entry.to_add);
AddRange(&(*dest)[it->second].to_remove, entry.to_remove);
}
else {
} else {
dest->push_back(entry);
}
}
@ -106,33 +107,30 @@ void AddMergeableRange(
//
// Returns true iff |removed| or |added| are non-empty.
template <typename T>
bool ComputeDifferenceForUpdate(
std::vector<T>& previous, std::vector<T>& current,
std::vector<T>* removed, std::vector<T>* added) {
bool ComputeDifferenceForUpdate(std::vector<T>& previous,
std::vector<T>& current,
std::vector<T>* removed,
std::vector<T>* added) {
// We need to sort to use std::set_difference.
std::sort(previous.begin(), previous.end());
std::sort(current.begin(), current.end());
// Returns the elements in |previous| that are not in |current|.
std::set_difference(
previous.begin(), previous.end(),
current.begin(), current.end(),
std::back_inserter(*removed));
std::set_difference(previous.begin(), previous.end(), current.begin(),
current.end(), std::back_inserter(*removed));
// Returns the elements in |current| that are not in |previous|.
std::set_difference(
current.begin(), current.end(),
previous.begin(), previous.end(),
std::back_inserter(*added));
std::set_difference(current.begin(), current.end(), previous.begin(),
previous.end(), std::back_inserter(*added));
return !removed->empty() || !added->empty();
}
template <typename T>
void CompareGroups(
std::vector<T>& previous_data, std::vector<T>& current_data,
std::function<void(T*)> on_removed, std::function<void(T*)> on_added, std::function<void(T*, T*)> on_found) {
void CompareGroups(std::vector<T>& previous_data,
std::vector<T>& current_data,
std::function<void(T*)> on_removed,
std::function<void(T*)> on_added,
std::function<void(T*, T*)> on_found) {
std::sort(previous_data.begin(), previous_data.end());
std::sort(current_data.begin(), current_data.end());
@ -186,7 +184,8 @@ QueryFile::Def BuildFileDef(const IdMap& id_map, const IndexFile& indexed) {
for (const IndexType& type : indexed.types) {
if (type.def.definition_spelling.has_value())
add_all_symbols(id_map.ToSymbol(type.id), type.def.definition_spelling.value());
add_all_symbols(id_map.ToSymbol(type.id),
type.def.definition_spelling.value());
if (type.def.definition_extent.has_value())
add_outline(id_map.ToSymbol(type.id), type.def.definition_extent.value());
for (const Range& use : type.uses)
@ -194,7 +193,8 @@ QueryFile::Def BuildFileDef(const IdMap& id_map, const IndexFile& indexed) {
}
for (const IndexFunc& func : indexed.funcs) {
if (func.def.definition_spelling.has_value())
add_all_symbols(id_map.ToSymbol(func.id), func.def.definition_spelling.value());
add_all_symbols(id_map.ToSymbol(func.id),
func.def.definition_spelling.value());
if (func.def.definition_extent.has_value())
add_outline(id_map.ToSymbol(func.id), func.def.definition_extent.value());
for (const IndexFunc::Declaration& decl : func.declarations) {
@ -203,23 +203,27 @@ QueryFile::Def BuildFileDef(const IdMap& id_map, const IndexFile& indexed) {
add_outline(id_map.ToSymbol(func.id), decl.spelling);
}
for (const IndexFuncRef& caller : func.callers) {
if (caller.is_implicit) continue;
if (caller.is_implicit)
continue;
add_all_symbols(id_map.ToSymbol(func.id), caller.loc);
}
}
for (const IndexVar& var : indexed.vars) {
if (var.def.definition_spelling.has_value())
add_all_symbols(id_map.ToSymbol(var.id), var.def.definition_spelling.value());
add_all_symbols(id_map.ToSymbol(var.id),
var.def.definition_spelling.value());
if (var.def.definition_extent.has_value())
add_outline(id_map.ToSymbol(var.id), var.def.definition_extent.value());
for (const Range& use : var.uses)
add_all_symbols(id_map.ToSymbol(var.id), use);
}
std::sort(def.outline.begin(), def.outline.end(), [](const SymbolRef& a, const SymbolRef& b) {
std::sort(def.outline.begin(), def.outline.end(),
[](const SymbolRef& a, const SymbolRef& b) {
return a.loc.range.start < b.loc.range.start;
});
std::sort(def.all_symbols.begin(), def.all_symbols.end(), [](const SymbolRef& a, const SymbolRef& b) {
std::sort(def.all_symbols.begin(), def.all_symbols.end(),
[](const SymbolRef& a, const SymbolRef& b) {
return a.loc.range.start < b.loc.range.start;
});
@ -228,38 +232,8 @@ QueryFile::Def BuildFileDef(const IdMap& id_map, const IndexFile& indexed) {
} // namespace
QueryFileId GetQueryFileIdFromPath(QueryDatabase* query_db, const std::string& path) {
QueryFileId GetQueryFileIdFromPath(QueryDatabase* query_db,
const std::string& path) {
auto it = query_db->usr_to_file.find(LowerPathIfCaseInsensitive(path));
if (it != query_db->usr_to_file.end())
return QueryFileId(it->second.id);
@ -310,11 +284,13 @@ IdMap::IdMap(QueryDatabase* query_db, const IdCache& local_ids)
cached_type_ids_.resize(local_ids.type_id_to_usr.size());
for (const auto& entry : local_ids.type_id_to_usr)
cached_type_ids_[entry.first] = GetQueryTypeIdFromUsr(query_db, entry.second);
cached_type_ids_[entry.first] =
GetQueryTypeIdFromUsr(query_db, entry.second);
cached_func_ids_.resize(local_ids.func_id_to_usr.size());
for (const auto& entry : local_ids.func_id_to_usr)
cached_func_ids_[entry.first] = GetQueryFuncIdFromUsr(query_db, entry.second);
cached_func_ids_[entry.first] =
GetQueryFuncIdFromUsr(query_db, entry.second);
cached_var_ids_.resize(local_ids.var_id_to_usr.size());
for (const auto& entry : local_ids.var_id_to_usr)
@ -329,7 +305,8 @@ QueryTypeId IdMap::ToQuery(IndexTypeId id) const {
return QueryTypeId(cached_type_ids_.find(id)->second);
}
QueryFuncId IdMap::ToQuery(IndexFuncId id) const {
if (id.id == -1) return QueryFuncId((size_t)-1);
if (id.id == -1)
return QueryFuncId((size_t)-1);
assert(cached_func_ids_.find(id) != cached_func_ids_.end());
return QueryFuncId(cached_func_ids_.find(id)->second);
}
@ -370,14 +347,16 @@ optional<QueryFuncRef> IdMap::ToQuery(optional<IndexFuncRef> ref) const {
return nullopt;
return ToQuery(ref.value());
}
optional<QueryLocation> IdMap::ToQuery(optional<IndexFunc::Declaration> decl) const {
optional<QueryLocation> IdMap::ToQuery(
optional<IndexFunc::Declaration> decl) const {
if (!decl)
return nullopt;
return ToQuery(decl.value());
}
template <typename In, typename Out>
std::vector<Out> ToQueryTransform(const IdMap& id_map, const std::vector<In>& input) {
std::vector<Out> ToQueryTransform(const IdMap& id_map,
const std::vector<In>& input) {
std::vector<Out> result;
result.reserve(input.size());
for (const In& in : input)
@ -399,7 +378,8 @@ std::vector<QueryVarId> IdMap::ToQuery(std::vector<IndexVarId> ids) const {
std::vector<QueryFuncRef> IdMap::ToQuery(std::vector<IndexFuncRef> refs) const {
return ToQueryTransform<IndexFuncRef, QueryFuncRef>(*this, refs);
}
std::vector<QueryLocation> IdMap::ToQuery(std::vector<IndexFunc::Declaration> decls) const {
std::vector<QueryLocation> IdMap::ToQuery(
std::vector<IndexFunc::Declaration> decls) const {
return ToQueryTransform<IndexFunc::Declaration, QueryLocation>(*this, decls);
}
@ -412,33 +392,16 @@ SymbolIdx IdMap::ToSymbol(IndexFuncId id) const {
SymbolIdx IdMap::ToSymbol(IndexVarId id) const {
return SymbolIdx(SymbolKind::Var, ToQuery(id).id);
}
// ----------------------
// INDEX THREAD FUNCTIONS
// ----------------------
// static
IndexUpdate IndexUpdate::CreateDelta(const IdMap* previous_id_map, const IdMap* current_id_map, IndexFile* previous, IndexFile* current) {
IndexUpdate IndexUpdate::CreateDelta(const IdMap* previous_id_map,
const IdMap* current_id_map,
IndexFile* previous,
IndexFile* current) {
// This function runs on an indexer thread.
if (!previous_id_map) {
@ -449,7 +412,10 @@ IndexUpdate IndexUpdate::CreateDelta(const IdMap* previous_id_map, const IdMap*
return IndexUpdate(*previous_id_map, *current_id_map, *previous, *current);
}
IndexUpdate::IndexUpdate(const IdMap& previous_id_map, const IdMap& current_id_map, IndexFile& previous_file, IndexFile& current_file) {
IndexUpdate::IndexUpdate(const IdMap& previous_id_map,
const IdMap& current_id_map,
IndexFile& previous_file,
IndexFile& current_file) {
// This function runs on an indexer thread.
// |query_name| is the name of the variable on the query type.
@ -461,12 +427,14 @@ IndexUpdate::IndexUpdate(const IdMap& previous_id_map, const IdMap& current_id_m
std::vector<type> removed, added; \
auto previous = previous_id_map.ToQuery(previous_def->index_name); \
auto current = current_id_map.ToQuery(current_def->index_name); \
bool did_add = ComputeDifferenceForUpdate( \
previous, current, \
&removed, &added); \
bool did_add = \
ComputeDifferenceForUpdate(previous, current, &removed, &added); \
if (did_add) { \
/*std::cerr << "Adding mergeable update on " << current_def->def.short_name << " (" << current_def->def.usr << ") for field " << #index_name << std::endl;*/ \
query_name.push_back(MergeableUpdate<type_id, type>(current_id_map.ToQuery(current_def->id), added, removed)); \
/*std::cerr << "Adding mergeable update on " << \
* current_def->def.short_name << " (" << current_def->def.usr << ") for \
* field " << #index_name << std::endl;*/ \
query_name.push_back(MergeableUpdate<type_id, type>( \
current_id_map.ToQuery(current_def->id), added, removed)); \
} \
}
// File
@ -478,100 +446,158 @@ IndexUpdate::IndexUpdate(const IdMap& previous_id_map, const IdMap& current_id_m
// away we don't want to remove the type/func/var usage.
// Types
CompareGroups<IndexType>(previous_file.types, current_file.types,
/*onRemoved:*/[this, &previous_id_map](IndexType* type) {
CompareGroups<IndexType>(
previous_file.types, current_file.types,
/*onRemoved:*/
[this, &previous_id_map](IndexType* type) {
if (type->def.definition_spelling)
types_removed.push_back(type->def.usr);
else {
if (!type->derived.empty())
types_derived.push_back(QueryType::DerivedUpdate(previous_id_map.ToQuery(type->id), {}, previous_id_map.ToQuery(type->derived)));
types_derived.push_back(QueryType::DerivedUpdate(
previous_id_map.ToQuery(type->id), {},
previous_id_map.ToQuery(type->derived)));
if (!type->instances.empty())
types_instances.push_back(QueryType::InstancesUpdate(previous_id_map.ToQuery(type->id), {}, previous_id_map.ToQuery(type->instances)));
types_instances.push_back(QueryType::InstancesUpdate(
previous_id_map.ToQuery(type->id), {},
previous_id_map.ToQuery(type->instances)));
if (!type->uses.empty())
types_uses.push_back(QueryType::UsesUpdate(previous_id_map.ToQuery(type->id), {}, previous_id_map.ToQuery(type->uses)));
types_uses.push_back(
QueryType::UsesUpdate(previous_id_map.ToQuery(type->id), {},
previous_id_map.ToQuery(type->uses)));
}
},
/*onAdded:*/[this, &current_id_map](IndexType* type) {
optional<QueryType::DefUpdate> def_update = ToQuery(current_id_map, type->def);
/*onAdded:*/
[this, &current_id_map](IndexType* type) {
optional<QueryType::DefUpdate> def_update =
ToQuery(current_id_map, type->def);
if (def_update)
types_def_update.push_back(*def_update);
if (!type->derived.empty())
types_derived.push_back(QueryType::DerivedUpdate(current_id_map.ToQuery(type->id), current_id_map.ToQuery(type->derived)));
types_derived.push_back(
QueryType::DerivedUpdate(current_id_map.ToQuery(type->id),
current_id_map.ToQuery(type->derived)));
if (!type->instances.empty())
types_instances.push_back(QueryType::InstancesUpdate(current_id_map.ToQuery(type->id), current_id_map.ToQuery(type->instances)));
types_instances.push_back(QueryType::InstancesUpdate(
current_id_map.ToQuery(type->id),
current_id_map.ToQuery(type->instances)));
if (!type->uses.empty())
types_uses.push_back(QueryType::UsesUpdate(current_id_map.ToQuery(type->id), current_id_map.ToQuery(type->uses)));
types_uses.push_back(
QueryType::UsesUpdate(current_id_map.ToQuery(type->id),
current_id_map.ToQuery(type->uses)));
},
/*onFound:*/[this, &previous_id_map, &current_id_map](IndexType* previous_def, IndexType* current_def) {
optional<QueryType::DefUpdate> previous_remapped_def = ToQuery(previous_id_map, previous_def->def);
optional<QueryType::DefUpdate> current_remapped_def = ToQuery(current_id_map, current_def->def);
if (current_remapped_def && previous_remapped_def != current_remapped_def && !current_remapped_def->detailed_name.empty())
/*onFound:*/
[this, &previous_id_map, &current_id_map](IndexType* previous_def,
IndexType* current_def) {
optional<QueryType::DefUpdate> previous_remapped_def =
ToQuery(previous_id_map, previous_def->def);
optional<QueryType::DefUpdate> current_remapped_def =
ToQuery(current_id_map, current_def->def);
if (current_remapped_def &&
previous_remapped_def != current_remapped_def &&
!current_remapped_def->detailed_name.empty())
types_def_update.push_back(*current_remapped_def);
PROCESS_UPDATE_DIFF(QueryTypeId, types_derived, derived, QueryTypeId);
PROCESS_UPDATE_DIFF(QueryTypeId, types_instances, instances, QueryVarId);
PROCESS_UPDATE_DIFF(QueryTypeId, types_instances, instances,
QueryVarId);
PROCESS_UPDATE_DIFF(QueryTypeId, types_uses, uses, QueryLocation);
});
// Functions
CompareGroups<IndexFunc>(previous_file.funcs, current_file.funcs,
/*onRemoved:*/[this, &previous_id_map](IndexFunc* func) {
CompareGroups<IndexFunc>(
previous_file.funcs, current_file.funcs,
/*onRemoved:*/
[this, &previous_id_map](IndexFunc* func) {
if (func->def.definition_spelling) {
funcs_removed.push_back(func->def.usr);
}
else {
} else {
if (!func->declarations.empty())
funcs_declarations.push_back(QueryFunc::DeclarationsUpdate(previous_id_map.ToQuery(func->id), {}, previous_id_map.ToQuery(func->declarations)));
funcs_declarations.push_back(QueryFunc::DeclarationsUpdate(
previous_id_map.ToQuery(func->id), {},
previous_id_map.ToQuery(func->declarations)));
if (!func->derived.empty())
funcs_derived.push_back(QueryFunc::DerivedUpdate(previous_id_map.ToQuery(func->id), {}, previous_id_map.ToQuery(func->derived)));
funcs_derived.push_back(QueryFunc::DerivedUpdate(
previous_id_map.ToQuery(func->id), {},
previous_id_map.ToQuery(func->derived)));
if (!func->callers.empty())
funcs_callers.push_back(QueryFunc::CallersUpdate(previous_id_map.ToQuery(func->id), {}, previous_id_map.ToQuery(func->callers)));
funcs_callers.push_back(QueryFunc::CallersUpdate(
previous_id_map.ToQuery(func->id), {},
previous_id_map.ToQuery(func->callers)));
}
},
/*onAdded:*/[this, &current_id_map](IndexFunc* func) {
optional<QueryFunc::DefUpdate> def_update = ToQuery(current_id_map, func->def);
/*onAdded:*/
[this, &current_id_map](IndexFunc* func) {
optional<QueryFunc::DefUpdate> def_update =
ToQuery(current_id_map, func->def);
if (def_update)
funcs_def_update.push_back(*def_update);
if (!func->declarations.empty())
funcs_declarations.push_back(QueryFunc::DeclarationsUpdate(current_id_map.ToQuery(func->id), current_id_map.ToQuery(func->declarations)));
funcs_declarations.push_back(QueryFunc::DeclarationsUpdate(
current_id_map.ToQuery(func->id),
current_id_map.ToQuery(func->declarations)));
if (!func->derived.empty())
funcs_derived.push_back(QueryFunc::DerivedUpdate(current_id_map.ToQuery(func->id), current_id_map.ToQuery(func->derived)));
funcs_derived.push_back(
QueryFunc::DerivedUpdate(current_id_map.ToQuery(func->id),
current_id_map.ToQuery(func->derived)));
if (!func->callers.empty())
funcs_callers.push_back(QueryFunc::CallersUpdate(current_id_map.ToQuery(func->id), current_id_map.ToQuery(func->callers)));
funcs_callers.push_back(
QueryFunc::CallersUpdate(current_id_map.ToQuery(func->id),
current_id_map.ToQuery(func->callers)));
},
/*onFound:*/[this, &previous_id_map, &current_id_map](IndexFunc* previous_def, IndexFunc* current_def) {
optional<QueryFunc::DefUpdate> previous_remapped_def = ToQuery(previous_id_map, previous_def->def);
optional<QueryFunc::DefUpdate> current_remapped_def = ToQuery(current_id_map, current_def->def);
if (current_remapped_def && previous_remapped_def != current_remapped_def && !current_remapped_def->detailed_name.empty())
/*onFound:*/
[this, &previous_id_map, &current_id_map](IndexFunc* previous_def,
IndexFunc* current_def) {
optional<QueryFunc::DefUpdate> previous_remapped_def =
ToQuery(previous_id_map, previous_def->def);
optional<QueryFunc::DefUpdate> current_remapped_def =
ToQuery(current_id_map, current_def->def);
if (current_remapped_def &&
previous_remapped_def != current_remapped_def &&
!current_remapped_def->detailed_name.empty())
funcs_def_update.push_back(*current_remapped_def);
PROCESS_UPDATE_DIFF(QueryFuncId, funcs_declarations, declarations, QueryLocation);
PROCESS_UPDATE_DIFF(QueryFuncId, funcs_declarations, declarations,
QueryLocation);
PROCESS_UPDATE_DIFF(QueryFuncId, funcs_derived, derived, QueryFuncId);
PROCESS_UPDATE_DIFF(QueryFuncId, funcs_callers, callers, QueryFuncRef);
});
// Variables
CompareGroups<IndexVar>(previous_file.vars, current_file.vars,
/*onRemoved:*/[this, &previous_id_map](IndexVar* var) {
CompareGroups<IndexVar>(
previous_file.vars, current_file.vars,
/*onRemoved:*/
[this, &previous_id_map](IndexVar* var) {
if (var->def.definition_spelling) {
vars_removed.push_back(var->def.usr);
}
else {
} else {
if (!var->uses.empty())
vars_uses.push_back(QueryVar::UsesUpdate(previous_id_map.ToQuery(var->id), {}, previous_id_map.ToQuery(var->uses)));
vars_uses.push_back(
QueryVar::UsesUpdate(previous_id_map.ToQuery(var->id), {},
previous_id_map.ToQuery(var->uses)));
}
},
/*onAdded:*/[this, &current_id_map](IndexVar* var) {
optional<QueryVar::DefUpdate> def_update = ToQuery(current_id_map, var->def);
/*onAdded:*/
[this, &current_id_map](IndexVar* var) {
optional<QueryVar::DefUpdate> def_update =
ToQuery(current_id_map, var->def);
if (def_update)
vars_def_update.push_back(*def_update);
if (!var->uses.empty())
vars_uses.push_back(QueryVar::UsesUpdate(current_id_map.ToQuery(var->id), current_id_map.ToQuery(var->uses)));
vars_uses.push_back(
QueryVar::UsesUpdate(current_id_map.ToQuery(var->id),
current_id_map.ToQuery(var->uses)));
},
/*onFound:*/[this, &previous_id_map, &current_id_map](IndexVar* previous_def, IndexVar* current_def) {
optional<QueryVar::DefUpdate> previous_remapped_def = ToQuery(previous_id_map, previous_def->def);
optional<QueryVar::DefUpdate> current_remapped_def = ToQuery(current_id_map, current_def->def);
if (current_remapped_def && previous_remapped_def != current_remapped_def && !current_remapped_def->detailed_name.empty())
/*onFound:*/
[this, &previous_id_map, &current_id_map](IndexVar* previous_def,
IndexVar* current_def) {
optional<QueryVar::DefUpdate> previous_remapped_def =
ToQuery(previous_id_map, previous_def->def);
optional<QueryVar::DefUpdate> current_remapped_def =
ToQuery(current_id_map, current_def->def);
if (current_remapped_def &&
previous_remapped_def != current_remapped_def &&
!current_remapped_def->detailed_name.empty())
vars_def_update.push_back(*current_remapped_def);
PROCESS_UPDATE_DIFF(QueryVarId, vars_uses, uses, QueryLocation);
@ -583,10 +609,8 @@ IndexUpdate::IndexUpdate(const IdMap& previous_id_map, const IdMap& current_id_m
void IndexUpdate::Merge(const IndexUpdate& update) {
// This function runs on an indexer thread.
#define INDEX_UPDATE_APPEND(name) \
AddRange(&name, update.name);
#define INDEX_UPDATE_MERGE(name) \
AddMergeableRange(&name, update.name);
#define INDEX_UPDATE_APPEND(name) AddRange(&name, update.name);
#define INDEX_UPDATE_MERGE(name) AddMergeableRange(&name, update.name);
INDEX_UPDATE_APPEND(files_removed);
INDEX_UPDATE_APPEND(files_def_update);
@ -619,37 +643,12 @@ std::string IndexUpdate::ToString() {
return output.GetString();
}
// ------------------------
// QUERYDB THREAD FUNCTIONS
// ------------------------
void QueryDatabase::RemoveUsrs(SymbolKind usr_kind, const std::vector<Usr>& to_remove) {
void QueryDatabase::RemoveUsrs(SymbolKind usr_kind,
const std::vector<Usr>& to_remove) {
// This function runs on the querydb thread.
// When we remove an element, we just erase the state from the storage. We do
@ -694,8 +693,8 @@ void QueryDatabase::ApplyIndexUpdate(IndexUpdate* update) {
// Example types:
// storage_name => std::vector<optional<QueryType>>
// merge_update => QueryType::DerivedUpdate => MergeableUpdate<QueryTypeId, QueryTypeId>
// def => QueryType
// merge_update => QueryType::DerivedUpdate =>
// MergeableUpdate<QueryTypeId, QueryTypeId> def => QueryType
// def->def_var_name => std::vector<QueryTypeId>
#define HANDLE_MERGEABLE(update_var_name, def_var_name, storage_name) \
for (auto merge_update : update->update_var_name) { \
@ -728,7 +727,8 @@ void QueryDatabase::ApplyIndexUpdate(IndexUpdate* update) {
#undef HANDLE_MERGEABLE
}
void QueryDatabase::ImportOrUpdate(const std::vector<QueryFile::DefUpdate>& updates) {
void QueryDatabase::ImportOrUpdate(
const std::vector<QueryFile::DefUpdate>& updates) {
// This function runs on the querydb thread.
for (auto& def : updates) {
@ -740,11 +740,13 @@ void QueryDatabase::ImportOrUpdate(const std::vector<QueryFile::DefUpdate>& upda
existing = QueryFile(def.path);
existing->def = def;
UpdateDetailedNames(&existing->detailed_name_idx, SymbolKind::File, it->second.id, def.path);
UpdateDetailedNames(&existing->detailed_name_idx, SymbolKind::File,
it->second.id, def.path);
}
}
void QueryDatabase::ImportOrUpdate(const std::vector<QueryType::DefUpdate>& updates) {
void QueryDatabase::ImportOrUpdate(
const std::vector<QueryType::DefUpdate>& updates) {
// This function runs on the querydb thread.
for (auto& def : updates) {
@ -763,11 +765,13 @@ void QueryDatabase::ImportOrUpdate(const std::vector<QueryType::DefUpdate>& upda
continue;
existing->def = def;
UpdateDetailedNames(&existing->detailed_name_idx, SymbolKind::Type, it->second.id, def.detailed_name);
UpdateDetailedNames(&existing->detailed_name_idx, SymbolKind::Type,
it->second.id, def.detailed_name);
}
}
void QueryDatabase::ImportOrUpdate(const std::vector<QueryFunc::DefUpdate>& updates) {
void QueryDatabase::ImportOrUpdate(
const std::vector<QueryFunc::DefUpdate>& updates) {
// This function runs on the querydb thread.
for (auto& def : updates) {
@ -786,11 +790,13 @@ void QueryDatabase::ImportOrUpdate(const std::vector<QueryFunc::DefUpdate>& upda
continue;
existing->def = def;
UpdateDetailedNames(&existing->detailed_name_idx, SymbolKind::Func, it->second.id, def.detailed_name);
UpdateDetailedNames(&existing->detailed_name_idx, SymbolKind::Func,
it->second.id, def.detailed_name);
}
}
void QueryDatabase::ImportOrUpdate(const std::vector<QueryVar::DefUpdate>& updates) {
void QueryDatabase::ImportOrUpdate(
const std::vector<QueryVar::DefUpdate>& updates) {
// This function runs on the querydb thread.
for (auto& def : updates) {
@ -810,17 +816,20 @@ void QueryDatabase::ImportOrUpdate(const std::vector<QueryVar::DefUpdate>& updat
existing->def = def;
if (!def.is_local)
UpdateDetailedNames(&existing->detailed_name_idx, SymbolKind::Var, it->second.id, def.detailed_name);
UpdateDetailedNames(&existing->detailed_name_idx, SymbolKind::Var,
it->second.id, def.detailed_name);
}
}
void QueryDatabase::UpdateDetailedNames(size_t* qualified_name_index, SymbolKind kind, size_t symbol_index, const std::string& name) {
void QueryDatabase::UpdateDetailedNames(size_t* qualified_name_index,
SymbolKind kind,
size_t symbol_index,
const std::string& name) {
if (*qualified_name_index == -1) {
detailed_names.push_back(name);
symbols.push_back(SymbolIdx(kind, symbol_index));
*qualified_name_index = detailed_names.size() - 1;
}
else {
} else {
detailed_names[*qualified_name_index] = name;
}
}
@ -831,16 +840,20 @@ IndexUpdate GetDelta(IndexFile previous, IndexFile current) {
QueryDatabase db;
IdMap previous_map(&db, previous.id_cache);
IdMap current_map(&db, current.id_cache);
return IndexUpdate::CreateDelta(&previous_map, &current_map, &previous, &current);
return IndexUpdate::CreateDelta(&previous_map, &current_map, &previous,
&current);
}
TEST_CASE("remove defs") {
IndexFile previous("foo.cc");
IndexFile current("foo.cc");
previous.Resolve(previous.ToTypeId("usr1"))->def.definition_spelling = Range(Position(1, 0));
previous.Resolve(previous.ToFuncId("usr2"))->def.definition_spelling = Range(Position(2, 0));
previous.Resolve(previous.ToVarId("usr3"))->def.definition_spelling = Range(Position(3, 0));
previous.Resolve(previous.ToTypeId("usr1"))->def.definition_spelling =
Range(Position(1, 0));
previous.Resolve(previous.ToFuncId("usr2"))->def.definition_spelling =
Range(Position(2, 0));
previous.Resolve(previous.ToVarId("usr3"))->def.definition_spelling =
Range(Position(3, 0));
IndexUpdate update = GetDelta(previous, current);
@ -853,9 +866,13 @@ TEST_CASE("do not remove ref-only defs") {
IndexFile previous("foo.cc");
IndexFile current("foo.cc");
previous.Resolve(previous.ToTypeId("usr1"))->uses.push_back(Range(Position(1, 0)));
previous.Resolve(previous.ToFuncId("usr2"))->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(2, 0)), false /*is_implicit*/));
previous.Resolve(previous.ToVarId("usr3"))->uses.push_back(Range(Position(3, 0)));
previous.Resolve(previous.ToTypeId("usr1"))
->uses.push_back(Range(Position(1, 0)));
previous.Resolve(previous.ToFuncId("usr2"))
->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(2, 0)),
false /*is_implicit*/));
previous.Resolve(previous.ToVarId("usr3"))
->uses.push_back(Range(Position(3, 0)));
IndexUpdate update = GetDelta(previous, current);
@ -871,8 +888,10 @@ TEST_CASE("func callers") {
IndexFunc* pf = previous.Resolve(previous.ToFuncId("usr"));
IndexFunc* cf = current.Resolve(current.ToFuncId("usr"));
pf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(1, 0)), false /*is_implicit*/));
cf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(2, 0)), false /*is_implicit*/));
pf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(1, 0)),
false /*is_implicit*/));
cf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(2, 0)),
false /*is_implicit*/));
IndexUpdate update = GetDelta(previous, current);
@ -880,7 +899,8 @@ TEST_CASE("func callers") {
REQUIRE(update.funcs_callers.size() == 1);
REQUIRE(update.funcs_callers[0].id == QueryFuncId(0));
REQUIRE(update.funcs_callers[0].to_remove.size() == 1);
REQUIRE(update.funcs_callers[0].to_remove[0].loc.range == Range(Position(1, 0)));
REQUIRE(update.funcs_callers[0].to_remove[0].loc.range ==
Range(Position(1, 0)));
REQUIRE(update.funcs_callers[0].to_add.size() == 1);
REQUIRE(update.funcs_callers[0].to_add[0].loc.range == Range(Position(2, 0)));
}
@ -912,18 +932,24 @@ TEST_CASE("apply delta") {
IndexFunc* pf = previous.Resolve(previous.ToFuncId("usr"));
IndexFunc* cf = current.Resolve(current.ToFuncId("usr"));
pf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(1, 0)), false /*is_implicit*/));
pf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(2, 0)), false /*is_implicit*/));
cf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(4, 0)), false /*is_implicit*/));
cf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(5, 0)), false /*is_implicit*/));
pf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(1, 0)),
false /*is_implicit*/));
pf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(2, 0)),
false /*is_implicit*/));
cf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(4, 0)),
false /*is_implicit*/));
cf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(5, 0)),
false /*is_implicit*/));
QueryDatabase db;
IdMap previous_map(&db, previous.id_cache);
IdMap current_map(&db, current.id_cache);
REQUIRE(db.funcs.size() == 1);
IndexUpdate import_update = IndexUpdate::CreateDelta(nullptr, &previous_map, nullptr, &previous);
IndexUpdate delta_update = IndexUpdate::CreateDelta(&previous_map, &current_map, &previous, &current);
IndexUpdate import_update =
IndexUpdate::CreateDelta(nullptr, &previous_map, nullptr, &previous);
IndexUpdate delta_update = IndexUpdate::CreateDelta(
&previous_map, &current_map, &previous, &current);
db.ApplyIndexUpdate(&import_update);
REQUIRE(db.funcs[0]->callers.size() == 2);

View File

@ -20,22 +20,14 @@ using QueryTypeId = Id<QueryType>;
using QueryFuncId = Id<QueryFunc>;
using QueryVarId = Id<QueryVar>;
struct IdMap;
struct QueryLocation {
QueryFileId path;
Range range;
QueryLocation() {} // Do not use, needed for reflect.
QueryLocation(QueryFileId path, Range range)
: path(path), range(range) {}
QueryLocation(QueryFileId path, Range range) : path(path), range(range) {}
QueryLocation OffsetStartColumn(int16_t offset) const {
QueryLocation result = *this;
@ -45,11 +37,11 @@ struct QueryLocation {
bool operator==(const QueryLocation& other) const {
// Note: We ignore |is_interesting|.
return
path == other.path &&
range == other.range;
return path == other.path && range == other.range;
}
bool operator!=(const QueryLocation& other) const {
return !(*this == other);
}
bool operator!=(const QueryLocation& other) const { return !(*this == other); }
bool operator<(const QueryLocation& o) const {
if (path < o.path)
return true;
@ -66,7 +58,9 @@ struct SymbolIdx {
SymbolKind kind;
size_t idx;
SymbolIdx() : kind(SymbolKind::Invalid), idx((size_t)-1) {} // Default ctor needed by stdlib. Do not use.
SymbolIdx()
: kind(SymbolKind::Invalid),
idx((size_t)-1) {} // Default ctor needed by stdlib. Do not use.
SymbolIdx(SymbolKind kind, uint64_t idx) : kind(kind), idx(idx) {}
bool operator==(const SymbolIdx& that) const {
@ -109,10 +103,12 @@ struct QueryFuncRef {
bool has_id() const { return id_.id != -1; }
QueryFuncRef() {} // Do not use, needed for reflect.
QueryFuncRef(QueryFuncId id, QueryLocation loc, bool is_implicit) : id_(id), loc(loc), is_implicit(is_implicit) {}
QueryFuncRef(QueryFuncId id, QueryLocation loc, bool is_implicit)
: id_(id), loc(loc), is_implicit(is_implicit) {}
bool operator==(const QueryFuncRef& that) const {
return id_ == that.id_ && loc == that.loc && is_implicit == that.is_implicit;
return id_ == that.id_ && loc == that.loc &&
is_implicit == that.is_implicit;
}
bool operator!=(const QueryFuncRef& that) const { return !(*this == that); }
bool operator<(const QueryFuncRef& that) const {
@ -120,7 +116,8 @@ struct QueryFuncRef {
return true;
if (id_ == that.id_ && loc.range.start < that.loc.range.start)
return true;
return id_ == that.id_ && loc.range.start == that.loc.range.start && is_implicit < that.is_implicit;
return id_ == that.id_ && loc.range.start == that.loc.range.start &&
is_implicit < that.is_implicit;
}
};
MAKE_REFLECT_STRUCT(QueryFuncRef, id_, loc, is_implicit);
@ -145,7 +142,9 @@ struct MergeableUpdate {
MergeableUpdate(TId id, const std::vector<TValue>& to_add)
: id(id), to_add(to_add) {}
MergeableUpdate(TId id, const std::vector<TValue>& to_add, const std::vector<TValue>& to_remove)
MergeableUpdate(TId id,
const std::vector<TValue>& to_add,
const std::vector<TValue>& to_remove)
: id(id), to_add(to_add), to_remove(to_remove) {}
};
template <typename TVisitor, typename TId, typename TValue>
@ -178,7 +177,10 @@ struct QueryFile {
MAKE_REFLECT_STRUCT(QueryFile::Def, path, outline, all_symbols);
struct QueryType {
using DefUpdate = TypeDefDefinitionData<QueryTypeId, QueryFuncId, QueryVarId, QueryLocation>;
using DefUpdate = TypeDefDefinitionData<QueryTypeId,
QueryFuncId,
QueryVarId,
QueryLocation>;
using DerivedUpdate = MergeableUpdate<QueryTypeId, QueryTypeId>;
using InstancesUpdate = MergeableUpdate<QueryTypeId, QueryVarId>;
using UsesUpdate = MergeableUpdate<QueryTypeId, QueryLocation>;
@ -193,7 +195,11 @@ struct QueryType {
};
struct QueryFunc {
using DefUpdate = FuncDefDefinitionData<QueryTypeId, QueryFuncId, QueryVarId, QueryFuncRef, QueryLocation>;
using DefUpdate = FuncDefDefinitionData<QueryTypeId,
QueryFuncId,
QueryVarId,
QueryFuncRef,
QueryLocation>;
using DeclarationsUpdate = MergeableUpdate<QueryFuncId, QueryLocation>;
using DerivedUpdate = MergeableUpdate<QueryFuncId, QueryFuncId>;
using CallersUpdate = MergeableUpdate<QueryFuncId, QueryFuncRef>;
@ -208,7 +214,8 @@ struct QueryFunc {
};
struct QueryVar {
using DefUpdate = VarDefDefinitionData<QueryTypeId, QueryFuncId, QueryVarId, QueryLocation>;
using DefUpdate =
VarDefDefinitionData<QueryTypeId, QueryFuncId, QueryVarId, QueryLocation>;
using UsesUpdate = MergeableUpdate<QueryVarId, QueryLocation>;
DefUpdate def;
@ -221,7 +228,10 @@ struct QueryVar {
struct IndexUpdate {
// Creates a new IndexUpdate based on the delta from previous to current. If
// no delta computation should be done just pass null for previous.
static IndexUpdate CreateDelta(const IdMap* previous_id_map, const IdMap* current_id_map, IndexFile* previous, IndexFile* current);
static IndexUpdate CreateDelta(const IdMap* previous_id_map,
const IdMap* current_id_map,
IndexFile* previous,
IndexFile* current);
// Merge |update| into this update; this can reduce overhead / index update
// work can be parallelized.
@ -257,13 +267,27 @@ struct IndexUpdate {
// Creates an index update assuming that |previous| is already
// in the index, so only the delta between |previous| and |current|
// will be applied.
IndexUpdate(const IdMap& previous_id_map, const IdMap& current_id_map, IndexFile& previous, IndexFile& current);
IndexUpdate(const IdMap& previous_id_map,
const IdMap& current_id_map,
IndexFile& previous,
IndexFile& current);
};
// NOTICE: We're not reflecting on files_removed or files_def_update, it is too much output when logging
// NOTICE: We're not reflecting on files_removed or files_def_update, it is too
// much output when logging
MAKE_REFLECT_STRUCT(IndexUpdate,
types_removed, types_def_update, types_derived, types_instances, types_uses,
funcs_removed, funcs_def_update, funcs_declarations, funcs_derived, funcs_callers,
vars_removed, vars_def_update, vars_uses);
types_removed,
types_def_update,
types_derived,
types_instances,
types_uses,
funcs_removed,
funcs_def_update,
funcs_declarations,
funcs_derived,
funcs_callers,
vars_removed,
vars_def_update,
vars_uses);
// The query database is heavily optimized for fast queries. It is stored
// in-memory.
@ -295,12 +319,12 @@ struct QueryDatabase {
void ImportOrUpdate(const std::vector<QueryType::DefUpdate>& updates);
void ImportOrUpdate(const std::vector<QueryFunc::DefUpdate>& updates);
void ImportOrUpdate(const std::vector<QueryVar::DefUpdate>& updates);
void UpdateDetailedNames(size_t* qualified_name_index, SymbolKind kind, size_t symbol_index, const std::string& name);
void UpdateDetailedNames(size_t* qualified_name_index,
SymbolKind kind,
size_t symbol_index,
const std::string& name);
};
struct IdMap {
const IdCache& local_ids;
QueryFileId primary_file;
@ -324,11 +348,13 @@ struct IdMap {
std::vector<QueryFuncId> ToQuery(std::vector<IndexFuncId> ids) const;
std::vector<QueryVarId> ToQuery(std::vector<IndexVarId> ids) const;
std::vector<QueryFuncRef> ToQuery(std::vector<IndexFuncRef> refs) const;
std::vector<QueryLocation> ToQuery(std::vector<IndexFunc::Declaration> decls) const;
std::vector<QueryLocation> ToQuery(
std::vector<IndexFunc::Declaration> decls) const;
SymbolIdx ToSymbol(IndexTypeId id) const;
SymbolIdx ToSymbol(IndexFuncId id) const;
SymbolIdx ToSymbol(IndexVarId id) const;
private:
spp::sparse_hash_map<IndexTypeId, QueryTypeId> cached_type_ids_;
spp::sparse_hash_map<IndexFuncId, QueryFuncId> cached_func_ids_;

View File

@ -13,28 +13,32 @@ int ComputeRangeSize(const Range& range) {
} // namespace
optional<QueryLocation> GetDefinitionSpellingOfSymbol(QueryDatabase* db, const QueryTypeId& id) {
optional<QueryLocation> GetDefinitionSpellingOfSymbol(QueryDatabase* db,
const QueryTypeId& id) {
optional<QueryType>& type = db->types[id.id];
if (type)
return type->def.definition_spelling;
return nullopt;
}
optional<QueryLocation> GetDefinitionSpellingOfSymbol(QueryDatabase* db, const QueryFuncId& id) {
optional<QueryLocation> GetDefinitionSpellingOfSymbol(QueryDatabase* db,
const QueryFuncId& id) {
optional<QueryFunc>& func = db->funcs[id.id];
if (func)
return func->def.definition_spelling;
return nullopt;
}
optional<QueryLocation> GetDefinitionSpellingOfSymbol(QueryDatabase* db, const QueryVarId& id) {
optional<QueryLocation> GetDefinitionSpellingOfSymbol(QueryDatabase* db,
const QueryVarId& id) {
optional<QueryVar>& var = db->vars[id.id];
if (var)
return var->def.definition_spelling;
return nullopt;
}
optional<QueryLocation> GetDefinitionSpellingOfSymbol(QueryDatabase* db, const SymbolIdx& symbol) {
optional<QueryLocation> GetDefinitionSpellingOfSymbol(QueryDatabase* db,
const SymbolIdx& symbol) {
switch (symbol.kind) {
case SymbolKind::Type: {
optional<QueryType>& type = db->types[symbol.idx];
@ -63,7 +67,8 @@ optional<QueryLocation> GetDefinitionSpellingOfSymbol(QueryDatabase* db, const S
return nullopt;
}
optional<QueryLocation> GetDefinitionExtentOfSymbol(QueryDatabase* db, const SymbolIdx& symbol) {
optional<QueryLocation> GetDefinitionExtentOfSymbol(QueryDatabase* db,
const SymbolIdx& symbol) {
switch (symbol.kind) {
case SymbolKind::Type: {
optional<QueryType>& type = db->types[symbol.idx];
@ -84,7 +89,8 @@ optional<QueryLocation> GetDefinitionExtentOfSymbol(QueryDatabase* db, const Sym
break;
}
case SymbolKind::File: {
return QueryLocation(QueryFileId(symbol.idx), Range(Position(1, 1), Position(1, 1)));
return QueryLocation(QueryFileId(symbol.idx),
Range(Position(1, 1), Position(1, 1)));
}
case SymbolKind::Invalid: {
assert(false && "unexpected");
@ -123,7 +129,8 @@ std::string GetHoverForSymbol(QueryDatabase* db, const SymbolIdx& symbol) {
return "";
}
optional<QueryFileId> GetDeclarationFileForSymbol(QueryDatabase* db, const SymbolIdx& symbol) {
optional<QueryFileId> GetDeclarationFileForSymbol(QueryDatabase* db,
const SymbolIdx& symbol) {
switch (symbol.kind) {
case SymbolKind::Type: {
optional<QueryType>& type = db->types[symbol.idx];
@ -158,14 +165,18 @@ optional<QueryFileId> GetDeclarationFileForSymbol(QueryDatabase* db, const Symbo
return nullopt;
}
std::vector<QueryLocation> ToQueryLocation(QueryDatabase* db, const std::vector<QueryFuncRef>& refs) {
std::vector<QueryLocation> ToQueryLocation(
QueryDatabase* db,
const std::vector<QueryFuncRef>& refs) {
std::vector<QueryLocation> locs;
locs.reserve(refs.size());
for (const QueryFuncRef& ref : refs)
locs.push_back(ref.loc);
return locs;
}
std::vector<QueryLocation> ToQueryLocation(QueryDatabase* db, const std::vector<QueryTypeId>& ids) {
std::vector<QueryLocation> ToQueryLocation(
QueryDatabase* db,
const std::vector<QueryTypeId>& ids) {
std::vector<QueryLocation> locs;
locs.reserve(ids.size());
for (const QueryTypeId& id : ids) {
@ -175,7 +186,9 @@ std::vector<QueryLocation> ToQueryLocation(QueryDatabase* db, const std::vector<
}
return locs;
}
std::vector<QueryLocation> ToQueryLocation(QueryDatabase* db, const std::vector<QueryFuncId>& ids) {
std::vector<QueryLocation> ToQueryLocation(
QueryDatabase* db,
const std::vector<QueryFuncId>& ids) {
std::vector<QueryLocation> locs;
locs.reserve(ids.size());
for (const QueryFuncId& id : ids) {
@ -185,7 +198,8 @@ std::vector<QueryLocation> ToQueryLocation(QueryDatabase* db, const std::vector<
}
return locs;
}
std::vector<QueryLocation> ToQueryLocation(QueryDatabase* db, const std::vector<QueryVarId>& ids) {
std::vector<QueryLocation> ToQueryLocation(QueryDatabase* db,
const std::vector<QueryVarId>& ids) {
std::vector<QueryLocation> locs;
locs.reserve(ids.size());
for (const QueryVarId& id : ids) {
@ -196,9 +210,8 @@ std::vector<QueryLocation> ToQueryLocation(QueryDatabase* db, const std::vector<
return locs;
}
std::vector<QueryLocation> GetUsesOfSymbol(QueryDatabase* db, const SymbolIdx& symbol) {
std::vector<QueryLocation> GetUsesOfSymbol(QueryDatabase* db,
const SymbolIdx& symbol) {
switch (symbol.kind) {
case SymbolKind::Type: {
optional<QueryType>& type = db->types[symbol.idx];
@ -233,7 +246,9 @@ std::vector<QueryLocation> GetUsesOfSymbol(QueryDatabase* db, const SymbolIdx& s
return {};
}
std::vector<QueryLocation> GetDeclarationsOfSymbolForGotoDefinition(QueryDatabase* db, const SymbolIdx& symbol) {
std::vector<QueryLocation> GetDeclarationsOfSymbolForGotoDefinition(
QueryDatabase* db,
const SymbolIdx& symbol) {
switch (symbol.kind) {
case SymbolKind::Type: {
// Returning the definition spelling of a type is a hack (and is why the
@ -270,7 +285,9 @@ std::vector<QueryLocation> GetDeclarationsOfSymbolForGotoDefinition(QueryDatabas
return {};
}
optional<QueryLocation> GetBaseDefinitionOrDeclarationSpelling(QueryDatabase* db, QueryFunc& func) {
optional<QueryLocation> GetBaseDefinitionOrDeclarationSpelling(
QueryDatabase* db,
QueryFunc& func) {
if (!func.def.base)
return nullopt;
optional<QueryFunc>& base = db->funcs[func.def.base->id];
@ -316,7 +333,8 @@ bool HasCallersOnSelfOrBaseOrDerived(QueryDatabase* db, QueryFunc& root) {
return false;
}
std::vector<QueryFuncRef> GetCallersForAllBaseFunctions(QueryDatabase* db, QueryFunc& root) {
std::vector<QueryFuncRef> GetCallersForAllBaseFunctions(QueryDatabase* db,
QueryFunc& root) {
std::vector<QueryFuncRef> callers;
optional<QueryFuncId> func_id = root.def.base;
@ -332,7 +350,8 @@ std::vector<QueryFuncRef> GetCallersForAllBaseFunctions(QueryDatabase* db, Query
return callers;
}
std::vector<QueryFuncRef> GetCallersForAllDerivedFunctions(QueryDatabase* db, QueryFunc& root) {
std::vector<QueryFuncRef> GetCallersForAllDerivedFunctions(QueryDatabase* db,
QueryFunc& root) {
std::vector<QueryFuncRef> callers;
std::queue<QueryFuncId> queue;
@ -351,7 +370,8 @@ std::vector<QueryFuncRef> GetCallersForAllDerivedFunctions(QueryDatabase* db, Qu
return callers;
}
optional<lsPosition> GetLsPosition(WorkingFile* working_file, const Position& position) {
optional<lsPosition> GetLsPosition(WorkingFile* working_file,
const Position& position) {
if (!working_file)
return lsPosition(position.line - 1, position.column - 1);
@ -369,8 +389,10 @@ optional<lsRange> GetLsRange(WorkingFile* working_file, const Range& location) {
lsPosition(location.end.line - 1, location.end.column - 1));
}
optional<int> start = working_file->GetBufferLineFromIndexLine(location.start.line);
optional<int> end = working_file->GetBufferLineFromIndexLine(location.end.line);
optional<int> start =
working_file->GetBufferLineFromIndexLine(location.start.line);
optional<int> end =
working_file->GetBufferLineFromIndexLine(location.end.line);
if (!start || !end)
return nullopt;
@ -383,18 +405,18 @@ optional<lsRange> GetLsRange(WorkingFile* working_file, const Range& location) {
if (*end < *start)
*end = *start + (location.end.line - location.start.line);
return lsRange(
lsPosition(*start - 1, location.start.column - 1),
return lsRange(lsPosition(*start - 1, location.start.column - 1),
lsPosition(*end - 1, location.end.column - 1));
}
lsDocumentUri GetLsDocumentUri(QueryDatabase* db, QueryFileId file_id, std::string* path) {
lsDocumentUri GetLsDocumentUri(QueryDatabase* db,
QueryFileId file_id,
std::string* path) {
optional<QueryFile>& file = db->files[file_id.id];
if (file) {
*path = file->def.path;
return lsDocumentUri::FromPath(*path);
}
else {
} else {
*path = "";
return lsDocumentUri::FromPath("");
}
@ -404,25 +426,31 @@ lsDocumentUri GetLsDocumentUri(QueryDatabase* db, QueryFileId file_id) {
optional<QueryFile>& file = db->files[file_id.id];
if (file) {
return lsDocumentUri::FromPath(file->def.path);
}
else {
} else {
return lsDocumentUri::FromPath("");
}
}
optional<lsLocation> GetLsLocation(QueryDatabase* db, WorkingFiles* working_files, const QueryLocation& location) {
optional<lsLocation> GetLsLocation(QueryDatabase* db,
WorkingFiles* working_files,
const QueryLocation& location) {
std::string path;
lsDocumentUri uri = GetLsDocumentUri(db, location.path, &path);
optional<lsRange> range = GetLsRange(working_files->GetFileByFilename(path), location.range);
optional<lsRange> range =
GetLsRange(working_files->GetFileByFilename(path), location.range);
if (!range)
return nullopt;
return lsLocation(uri, *range);
}
NonElidedVector<lsLocation> GetLsLocations(QueryDatabase* db, WorkingFiles* working_files, const std::vector<QueryLocation>& locations) {
NonElidedVector<lsLocation> GetLsLocations(
QueryDatabase* db,
WorkingFiles* working_files,
const std::vector<QueryLocation>& locations) {
std::unordered_set<lsLocation> unique_locations;
for (const QueryLocation& query_location : locations) {
optional<lsLocation> location = GetLsLocation(db, working_files, query_location);
optional<lsLocation> location =
GetLsLocation(db, working_files, query_location);
if (!location)
continue;
unique_locations.insert(*location);
@ -435,7 +463,9 @@ NonElidedVector<lsLocation> GetLsLocations(QueryDatabase* db, WorkingFiles* work
}
// Returns a symbol. The symbol will have *NOT* have a location assigned.
optional<lsSymbolInformation> GetSymbolInfo(QueryDatabase* db, WorkingFiles* working_files, SymbolIdx symbol) {
optional<lsSymbolInformation> GetSymbolInfo(QueryDatabase* db,
WorkingFiles* working_files,
SymbolIdx symbol) {
switch (symbol.kind) {
case SymbolKind::File: {
optional<QueryFile>& file = db->files[symbol.idx];
@ -470,7 +500,8 @@ optional<lsSymbolInformation> GetSymbolInfo(QueryDatabase* db, WorkingFiles* wor
info.kind = lsSymbolKind::Function;
if (func->def.declaring_type.has_value()) {
optional<QueryType>& container = db->types[func->def.declaring_type->id];
optional<QueryType>& container =
db->types[func->def.declaring_type->id];
if (container)
info.kind = lsSymbolKind::Method;
}
@ -496,8 +527,7 @@ optional<lsSymbolInformation> GetSymbolInfo(QueryDatabase* db, WorkingFiles* wor
return nullopt;
}
void AddCodeLens(
const char* singular,
void AddCodeLens(const char* singular,
const char* plural,
CommonCodeLensParams* common,
QueryLocation loc,
@ -519,7 +549,8 @@ void AddCodeLens(
for (const QueryLocation& use : uses) {
if (excluded == use)
continue;
optional<lsLocation> location = GetLsLocation(common->db, common->working_files, use);
optional<lsLocation> location =
GetLsLocation(common->db, common->working_files, use);
if (!location)
continue;
unique_uses.insert(*location);
@ -539,11 +570,15 @@ void AddCodeLens(
common->result->push_back(code_lens);
}
lsWorkspaceEdit BuildWorkspaceEdit(QueryDatabase* db, WorkingFiles* working_files, const std::vector<QueryLocation>& locations, const std::string& new_text) {
lsWorkspaceEdit BuildWorkspaceEdit(QueryDatabase* db,
WorkingFiles* working_files,
const std::vector<QueryLocation>& locations,
const std::string& new_text) {
std::unordered_map<QueryFileId, lsTextDocumentEdit> path_to_edit;
for (auto& location : locations) {
optional<lsLocation> ls_location = GetLsLocation(db, working_files, location);
optional<lsLocation> ls_location =
GetLsLocation(db, working_files, location);
if (!ls_location)
continue;
@ -555,11 +590,13 @@ lsWorkspaceEdit BuildWorkspaceEdit(QueryDatabase* db, WorkingFiles* working_file
continue;
const std::string& path = file->def.path;
path_to_edit[location.path].textDocument.uri = lsDocumentUri::FromPath(path);
path_to_edit[location.path].textDocument.uri =
lsDocumentUri::FromPath(path);
WorkingFile* working_file = working_files->GetFileByFilename(path);
if (working_file)
path_to_edit[location.path].textDocument.version = working_file->version;
path_to_edit[location.path].textDocument.version =
working_file->version;
}
lsTextEdit edit;
@ -572,21 +609,23 @@ lsWorkspaceEdit BuildWorkspaceEdit(QueryDatabase* db, WorkingFiles* working_file
edits.push_back(edit);
}
lsWorkspaceEdit edit;
for (const auto& changes : path_to_edit)
edit.documentChanges.push_back(changes.second);
return edit;
}
std::vector<SymbolRef> FindSymbolsAtLocation(WorkingFile* working_file, QueryFile* file, lsPosition position) {
std::vector<SymbolRef> FindSymbolsAtLocation(WorkingFile* working_file,
QueryFile* file,
lsPosition position) {
std::vector<SymbolRef> symbols;
symbols.reserve(1);
int target_line = position.line + 1;
int target_column = position.character + 1;
if (working_file) {
optional<int> index_line = working_file->GetIndexLineFromBufferLine(target_line);
optional<int> index_line =
working_file->GetIndexLineFromBufferLine(target_line);
if (index_line)
target_line = *index_line;
}
@ -602,12 +641,14 @@ std::vector<SymbolRef> FindSymbolsAtLocation(WorkingFile* working_file, QueryFil
//
// Order functions before other types, which makes goto definition work
// better on constructors.
std::sort(symbols.begin(), symbols.end(), [](const SymbolRef& a, const SymbolRef& b) {
std::sort(symbols.begin(), symbols.end(),
[](const SymbolRef& a, const SymbolRef& b) {
int a_size = ComputeRangeSize(a.loc.range);
int b_size = ComputeRangeSize(b.loc.range);
if (a_size == b_size)
return a.idx.kind != b.idx.kind && a.idx.kind == SymbolKind::Func;
return a.idx.kind != b.idx.kind &&
a.idx.kind == SymbolKind::Func;
return a_size < b_size;
});
@ -615,7 +656,10 @@ std::vector<SymbolRef> FindSymbolsAtLocation(WorkingFile* working_file, QueryFil
return symbols;
}
NonElidedVector<Out_CqueryTypeHierarchyTree::TypeEntry> BuildParentInheritanceHierarchyForType(QueryDatabase* db, WorkingFiles* working_files, QueryTypeId root) {
NonElidedVector<Out_CqueryTypeHierarchyTree::TypeEntry>
BuildParentInheritanceHierarchyForType(QueryDatabase* db,
WorkingFiles* working_files,
QueryTypeId root) {
optional<QueryType>& root_type = db->types[root.id];
if (!root_type)
return {};
@ -631,8 +675,10 @@ NonElidedVector<Out_CqueryTypeHierarchyTree::TypeEntry> BuildParentInheritanceHi
Out_CqueryTypeHierarchyTree::TypeEntry parent_entry;
parent_entry.name = parent_type->def.detailed_name;
if (parent_type->def.definition_spelling)
parent_entry.location = GetLsLocation(db, working_files, *parent_type->def.definition_spelling);
parent_entry.children = BuildParentInheritanceHierarchyForType(db, working_files, parent_id);
parent_entry.location = GetLsLocation(
db, working_files, *parent_type->def.definition_spelling);
parent_entry.children =
BuildParentInheritanceHierarchyForType(db, working_files, parent_id);
parent_entries.push_back(parent_entry);
}
@ -640,8 +686,10 @@ NonElidedVector<Out_CqueryTypeHierarchyTree::TypeEntry> BuildParentInheritanceHi
return parent_entries;
}
optional<Out_CqueryTypeHierarchyTree::TypeEntry> BuildInheritanceHierarchyForType(QueryDatabase* db, WorkingFiles* working_files, QueryTypeId root_id) {
optional<Out_CqueryTypeHierarchyTree::TypeEntry>
BuildInheritanceHierarchyForType(QueryDatabase* db,
WorkingFiles* working_files,
QueryTypeId root_id) {
optional<QueryType>& root_type = db->types[root_id.id];
if (!root_type)
return nullopt;
@ -651,7 +699,8 @@ optional<Out_CqueryTypeHierarchyTree::TypeEntry> BuildInheritanceHierarchyForTyp
// Name and location.
entry.name = root_type->def.detailed_name;
if (root_type->def.definition_spelling)
entry.location = GetLsLocation(db, working_files, *root_type->def.definition_spelling);
entry.location =
GetLsLocation(db, working_files, *root_type->def.definition_spelling);
entry.children.reserve(root_type->derived.size());
@ -659,13 +708,15 @@ optional<Out_CqueryTypeHierarchyTree::TypeEntry> BuildInheritanceHierarchyForTyp
Out_CqueryTypeHierarchyTree::TypeEntry base;
base.name = "[[Base]]";
base.location = entry.location;
base.children = BuildParentInheritanceHierarchyForType(db, working_files, root_id);
base.children =
BuildParentInheritanceHierarchyForType(db, working_files, root_id);
if (!base.children.empty())
entry.children.push_back(base);
// Add derived.
for (QueryTypeId derived : root_type->derived) {
auto derived_entry = BuildInheritanceHierarchyForType(db, working_files, derived);
auto derived_entry =
BuildInheritanceHierarchyForType(db, working_files, derived);
if (derived_entry)
entry.children.push_back(*derived_entry);
}
@ -673,8 +724,10 @@ optional<Out_CqueryTypeHierarchyTree::TypeEntry> BuildInheritanceHierarchyForTyp
return entry;
}
NonElidedVector<Out_CqueryTypeHierarchyTree::TypeEntry> BuildParentInheritanceHierarchyForFunc(QueryDatabase* db, WorkingFiles* working_files, QueryFuncId root) {
NonElidedVector<Out_CqueryTypeHierarchyTree::TypeEntry>
BuildParentInheritanceHierarchyForFunc(QueryDatabase* db,
WorkingFiles* working_files,
QueryFuncId root) {
optional<QueryFunc>& root_func = db->funcs[root.id];
if (!root_func || !root_func->def.base)
return {};
@ -686,16 +739,20 @@ NonElidedVector<Out_CqueryTypeHierarchyTree::TypeEntry> BuildParentInheritanceHi
Out_CqueryTypeHierarchyTree::TypeEntry parent_entry;
parent_entry.name = parent_func->def.detailed_name;
if (parent_func->def.definition_spelling)
parent_entry.location = GetLsLocation(db, working_files, *parent_func->def.definition_spelling);
parent_entry.children = BuildParentInheritanceHierarchyForFunc(db, working_files, *root_func->def.base);
parent_entry.location =
GetLsLocation(db, working_files, *parent_func->def.definition_spelling);
parent_entry.children = BuildParentInheritanceHierarchyForFunc(
db, working_files, *root_func->def.base);
NonElidedVector<Out_CqueryTypeHierarchyTree::TypeEntry> entries;
entries.push_back(parent_entry);
return entries;
}
optional<Out_CqueryTypeHierarchyTree::TypeEntry> BuildInheritanceHierarchyForFunc(QueryDatabase* db, WorkingFiles* working_files, QueryFuncId root_id) {
optional<Out_CqueryTypeHierarchyTree::TypeEntry>
BuildInheritanceHierarchyForFunc(QueryDatabase* db,
WorkingFiles* working_files,
QueryFuncId root_id) {
optional<QueryFunc>& root_func = db->funcs[root_id.id];
if (!root_func)
return nullopt;
@ -705,7 +762,8 @@ optional<Out_CqueryTypeHierarchyTree::TypeEntry> BuildInheritanceHierarchyForFun
// Name and location.
entry.name = root_func->def.detailed_name;
if (root_func->def.definition_spelling)
entry.location = GetLsLocation(db, working_files, *root_func->def.definition_spelling);
entry.location =
GetLsLocation(db, working_files, *root_func->def.definition_spelling);
entry.children.reserve(root_func->derived.size());
@ -713,13 +771,15 @@ optional<Out_CqueryTypeHierarchyTree::TypeEntry> BuildInheritanceHierarchyForFun
Out_CqueryTypeHierarchyTree::TypeEntry base;
base.name = "[[Base]]";
base.location = entry.location;
base.children = BuildParentInheritanceHierarchyForFunc(db, working_files, root_id);
base.children =
BuildParentInheritanceHierarchyForFunc(db, working_files, root_id);
if (!base.children.empty())
entry.children.push_back(base);
// Add derived.
for (QueryFuncId derived : root_func->derived) {
auto derived_entry = BuildInheritanceHierarchyForFunc(db, working_files, derived);
auto derived_entry =
BuildInheritanceHierarchyForFunc(db, working_files, derived);
if (derived_entry)
entry.children.push_back(*derived_entry);
}
@ -727,13 +787,17 @@ optional<Out_CqueryTypeHierarchyTree::TypeEntry> BuildInheritanceHierarchyForFun
return entry;
}
NonElidedVector<Out_CqueryCallTree::CallEntry> BuildInitialCallTree(QueryDatabase* db, WorkingFiles* working_files, QueryFuncId root) {
NonElidedVector<Out_CqueryCallTree::CallEntry> BuildInitialCallTree(
QueryDatabase* db,
WorkingFiles* working_files,
QueryFuncId root) {
optional<QueryFunc>& root_func = db->funcs[root.id];
if (!root_func)
return {};
if (!root_func->def.definition_spelling)
return {};
optional<lsLocation> def_loc = GetLsLocation(db, working_files, *root_func->def.definition_spelling);
optional<lsLocation> def_loc =
GetLsLocation(db, working_files, *root_func->def.definition_spelling);
if (!def_loc)
return {};
@ -747,12 +811,17 @@ NonElidedVector<Out_CqueryCallTree::CallEntry> BuildInitialCallTree(QueryDatabas
return result;
}
NonElidedVector<Out_CqueryCallTree::CallEntry> BuildExpandCallTree(QueryDatabase* db, WorkingFiles* working_files, QueryFuncId root) {
NonElidedVector<Out_CqueryCallTree::CallEntry> BuildExpandCallTree(
QueryDatabase* db,
WorkingFiles* working_files,
QueryFuncId root) {
optional<QueryFunc>& root_func = db->funcs[root.id];
if (!root_func)
return {};
auto format_location = [&](const lsLocation& location, optional<QueryTypeId> declaring_type) -> std::string {
auto format_location =
[&](const lsLocation& location,
optional<QueryTypeId> declaring_type) -> std::string {
std::string base;
if (declaring_type) {
@ -774,8 +843,10 @@ NonElidedVector<Out_CqueryCallTree::CallEntry> BuildExpandCallTree(QueryDatabase
NonElidedVector<Out_CqueryCallTree::CallEntry> result;
std::unordered_set<QueryLocation> seen_locations;
auto handle_caller = [&](QueryFuncRef caller, Out_CqueryCallTree::CallType call_type) {
optional<lsLocation> call_location = GetLsLocation(db, working_files, caller.loc);
auto handle_caller = [&](QueryFuncRef caller,
Out_CqueryCallTree::CallType call_type) {
optional<lsLocation> call_location =
GetLsLocation(db, working_files, caller.loc);
if (!call_location)
return;
@ -803,14 +874,15 @@ NonElidedVector<Out_CqueryCallTree::CallEntry> BuildExpandCallTree(QueryDatabase
return;
Out_CqueryCallTree::CallEntry call_entry;
call_entry.name = call_func->def.short_name + " (" + format_location(*call_location, call_func->def.declaring_type) + ")";
call_entry.name =
call_func->def.short_name + " (" +
format_location(*call_location, call_func->def.declaring_type) + ")";
call_entry.usr = call_func->def.usr;
call_entry.location = *call_location;
call_entry.hasCallers = HasCallersOnSelfOrBaseOrDerived(db, *call_func);
call_entry.callType = call_type;
result.push_back(call_entry);
}
else {
} else {
// TODO: See if we can do a better job here. Need more information from
// the indexer.
Out_CqueryCallTree::CallEntry call_entry;
@ -823,9 +895,12 @@ NonElidedVector<Out_CqueryCallTree::CallEntry> BuildExpandCallTree(QueryDatabase
}
};
std::vector<QueryFuncRef> base_callers = GetCallersForAllBaseFunctions(db, *root_func);
std::vector<QueryFuncRef> derived_callers = GetCallersForAllDerivedFunctions(db, *root_func);
result.reserve(root_func->callers.size() + base_callers.size() + derived_callers.size());
std::vector<QueryFuncRef> base_callers =
GetCallersForAllBaseFunctions(db, *root_func);
std::vector<QueryFuncRef> derived_callers =
GetCallersForAllDerivedFunctions(db, *root_func);
result.reserve(root_func->callers.size() + base_callers.size() +
derived_callers.size());
for (QueryFuncRef caller : root_func->callers)
handle_caller(caller, Out_CqueryCallTree::CallType::Direct);

View File

@ -7,31 +7,59 @@
#include <optional.h>
optional<QueryLocation> GetDefinitionSpellingOfSymbol(QueryDatabase* db, const QueryTypeId& id);
optional<QueryLocation> GetDefinitionSpellingOfSymbol(QueryDatabase* db, const QueryFuncId& id);
optional<QueryLocation> GetDefinitionSpellingOfSymbol(QueryDatabase* db, const QueryVarId& id);
optional<QueryLocation> GetDefinitionSpellingOfSymbol(QueryDatabase* db, const SymbolIdx& symbol);
optional<QueryLocation> GetDefinitionExtentOfSymbol(QueryDatabase* db, const SymbolIdx& symbol);
optional<QueryLocation> GetDefinitionSpellingOfSymbol(QueryDatabase* db,
const QueryTypeId& id);
optional<QueryLocation> GetDefinitionSpellingOfSymbol(QueryDatabase* db,
const QueryFuncId& id);
optional<QueryLocation> GetDefinitionSpellingOfSymbol(QueryDatabase* db,
const QueryVarId& id);
optional<QueryLocation> GetDefinitionSpellingOfSymbol(QueryDatabase* db,
const SymbolIdx& symbol);
optional<QueryLocation> GetDefinitionExtentOfSymbol(QueryDatabase* db,
const SymbolIdx& symbol);
std::string GetHoverForSymbol(QueryDatabase* db, const SymbolIdx& symbol);
optional<QueryFileId> GetDeclarationFileForSymbol(QueryDatabase* db, const SymbolIdx& symbol);
std::vector<QueryLocation> ToQueryLocation(QueryDatabase* db, const std::vector<QueryFuncRef>& refs);
std::vector<QueryLocation> ToQueryLocation(QueryDatabase* db, const std::vector<QueryTypeId>& ids);
std::vector<QueryLocation> ToQueryLocation(QueryDatabase* db, const std::vector<QueryFuncId>& ids);
std::vector<QueryLocation> ToQueryLocation(QueryDatabase* db, const std::vector<QueryVarId>& ids);
std::vector<QueryLocation> GetUsesOfSymbol(QueryDatabase* db, const SymbolIdx& symbol);
std::vector<QueryLocation> GetDeclarationsOfSymbolForGotoDefinition(QueryDatabase* db, const SymbolIdx& symbol);
optional<QueryLocation> GetBaseDefinitionOrDeclarationSpelling(QueryDatabase* db, QueryFunc& func);
optional<QueryFileId> GetDeclarationFileForSymbol(QueryDatabase* db,
const SymbolIdx& symbol);
std::vector<QueryLocation> ToQueryLocation(
QueryDatabase* db,
const std::vector<QueryFuncRef>& refs);
std::vector<QueryLocation> ToQueryLocation(QueryDatabase* db,
const std::vector<QueryTypeId>& ids);
std::vector<QueryLocation> ToQueryLocation(QueryDatabase* db,
const std::vector<QueryFuncId>& ids);
std::vector<QueryLocation> ToQueryLocation(QueryDatabase* db,
const std::vector<QueryVarId>& ids);
std::vector<QueryLocation> GetUsesOfSymbol(QueryDatabase* db,
const SymbolIdx& symbol);
std::vector<QueryLocation> GetDeclarationsOfSymbolForGotoDefinition(
QueryDatabase* db,
const SymbolIdx& symbol);
optional<QueryLocation> GetBaseDefinitionOrDeclarationSpelling(
QueryDatabase* db,
QueryFunc& func);
bool HasCallersOnSelfOrBaseOrDerived(QueryDatabase* db, QueryFunc& root);
std::vector<QueryFuncRef> GetCallersForAllBaseFunctions(QueryDatabase* db, QueryFunc& root);
std::vector<QueryFuncRef> GetCallersForAllDerivedFunctions(QueryDatabase* db, QueryFunc& root);
optional<lsPosition> GetLsPosition(WorkingFile* working_file, const Position& position);
std::vector<QueryFuncRef> GetCallersForAllBaseFunctions(QueryDatabase* db,
QueryFunc& root);
std::vector<QueryFuncRef> GetCallersForAllDerivedFunctions(QueryDatabase* db,
QueryFunc& root);
optional<lsPosition> GetLsPosition(WorkingFile* working_file,
const Position& position);
optional<lsRange> GetLsRange(WorkingFile* working_file, const Range& location);
lsDocumentUri GetLsDocumentUri(QueryDatabase* db, QueryFileId file_id, std::string* path);
lsDocumentUri GetLsDocumentUri(QueryDatabase* db,
QueryFileId file_id,
std::string* path);
lsDocumentUri GetLsDocumentUri(QueryDatabase* db, QueryFileId file_id);
optional<lsLocation> GetLsLocation(QueryDatabase* db, WorkingFiles* working_files, const QueryLocation& location);
NonElidedVector<lsLocation> GetLsLocations(QueryDatabase* db, WorkingFiles* working_files, const std::vector<QueryLocation>& locations);
optional<lsLocation> GetLsLocation(QueryDatabase* db,
WorkingFiles* working_files,
const QueryLocation& location);
NonElidedVector<lsLocation> GetLsLocations(
QueryDatabase* db,
WorkingFiles* working_files,
const std::vector<QueryLocation>& locations);
// Returns a symbol. The symbol will have *NOT* have a location assigned.
optional<lsSymbolInformation> GetSymbolInfo(QueryDatabase* db, WorkingFiles* working_files, SymbolIdx symbol);
optional<lsSymbolInformation> GetSymbolInfo(QueryDatabase* db,
WorkingFiles* working_files,
SymbolIdx symbol);
struct CommonCodeLensParams {
std::vector<TCodeLens>* result;
@ -40,8 +68,7 @@ struct CommonCodeLensParams {
WorkingFile* working_file;
};
void AddCodeLens(
const char* singular,
void AddCodeLens(const char* singular,
const char* plural,
CommonCodeLensParams* common,
QueryLocation loc,
@ -49,12 +76,35 @@ void AddCodeLens(
optional<QueryLocation> excluded,
bool force_display);
lsWorkspaceEdit BuildWorkspaceEdit(QueryDatabase* db, WorkingFiles* working_files, const std::vector<QueryLocation>& locations, const std::string& new_text);
lsWorkspaceEdit BuildWorkspaceEdit(QueryDatabase* db,
WorkingFiles* working_files,
const std::vector<QueryLocation>& locations,
const std::string& new_text);
std::vector<SymbolRef> FindSymbolsAtLocation(WorkingFile* working_file, QueryFile* file, lsPosition position);
NonElidedVector<Out_CqueryTypeHierarchyTree::TypeEntry> BuildParentInheritanceHierarchyForType(QueryDatabase* db, WorkingFiles* working_files, QueryTypeId root);
optional<Out_CqueryTypeHierarchyTree::TypeEntry> BuildInheritanceHierarchyForType(QueryDatabase* db, WorkingFiles* working_files, QueryTypeId root_id);
NonElidedVector<Out_CqueryTypeHierarchyTree::TypeEntry> BuildParentInheritanceHierarchyForFunc(QueryDatabase* db, WorkingFiles* working_files, QueryFuncId root);
optional<Out_CqueryTypeHierarchyTree::TypeEntry> BuildInheritanceHierarchyForFunc(QueryDatabase* db, WorkingFiles* working_files, QueryFuncId root_id);
NonElidedVector<Out_CqueryCallTree::CallEntry> BuildInitialCallTree(QueryDatabase* db, WorkingFiles* working_files, QueryFuncId root);
NonElidedVector<Out_CqueryCallTree::CallEntry> BuildExpandCallTree(QueryDatabase* db, WorkingFiles* working_files, QueryFuncId root);
std::vector<SymbolRef> FindSymbolsAtLocation(WorkingFile* working_file,
QueryFile* file,
lsPosition position);
NonElidedVector<Out_CqueryTypeHierarchyTree::TypeEntry>
BuildParentInheritanceHierarchyForType(QueryDatabase* db,
WorkingFiles* working_files,
QueryTypeId root);
optional<Out_CqueryTypeHierarchyTree::TypeEntry>
BuildInheritanceHierarchyForType(QueryDatabase* db,
WorkingFiles* working_files,
QueryTypeId root_id);
NonElidedVector<Out_CqueryTypeHierarchyTree::TypeEntry>
BuildParentInheritanceHierarchyForFunc(QueryDatabase* db,
WorkingFiles* working_files,
QueryFuncId root);
optional<Out_CqueryTypeHierarchyTree::TypeEntry>
BuildInheritanceHierarchyForFunc(QueryDatabase* db,
WorkingFiles* working_files,
QueryFuncId root_id);
NonElidedVector<Out_CqueryCallTree::CallEntry> BuildInitialCallTree(
QueryDatabase* db,
WorkingFiles* working_files,
QueryFuncId root);
NonElidedVector<Out_CqueryCallTree::CallEntry> BuildExpandCallTree(
QueryDatabase* db,
WorkingFiles* working_files,
QueryFuncId root);

View File

@ -49,7 +49,6 @@ void Reflect(Writer& visitor, std::string& value) {
visitor.String(value.c_str(), (rapidjson::SizeType)value.size());
}
// ReflectMember
void ReflectMember(Writer& visitor, const char* name, std::string& value) {
if (value.empty())
@ -58,17 +57,6 @@ void ReflectMember(Writer& visitor, const char* name, std::string& value) {
Reflect(visitor, value);
}
// TODO: Move this to indexer.cc
template <typename TVisitor>
@ -91,7 +79,6 @@ void Reflect(TVisitor& visitor, IndexType& value) {
REFLECT_MEMBER_END();
}
template <typename TVisitor>
void Reflect(TVisitor& visitor, IndexFunc& value) {
REFLECT_MEMBER_START();
@ -111,7 +98,6 @@ void Reflect(TVisitor& visitor, IndexFunc& value) {
REFLECT_MEMBER_END();
}
template <typename TVisitor>
void Reflect(TVisitor& visitor, IndexVar& value) {
REFLECT_MEMBER_START();
@ -130,7 +116,6 @@ void Reflect(TVisitor& visitor, IndexVar& value) {
REFLECT_MEMBER_END();
}
// IndexFile
bool ReflectMemberStart(Writer& visitor, IndexFile& value) {
auto it = value.id_cache.usr_to_type_id.find("");
@ -161,12 +146,6 @@ void Reflect(TVisitor& visitor, IndexFile& value) {
REFLECT_MEMBER_END();
}
std::string Serialize(IndexFile& file) {
rapidjson::StringBuffer output;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(output);
@ -180,7 +159,9 @@ std::string Serialize(IndexFile& file) {
return output.GetString();
}
std::unique_ptr<IndexFile> Deserialize(std::string path, std::string serialized, optional<int> expected_version) {
std::unique_ptr<IndexFile> Deserialize(std::string path,
std::string serialized,
optional<int> expected_version) {
rapidjson::Document reader;
reader.Parse(serialized.c_str());
if (reader.HasParseError())

View File

@ -9,27 +9,23 @@
#include <string>
#include <vector>
using std::experimental::optional;
using std::experimental::nullopt;
using std::experimental::optional;
using Reader = rapidjson::GenericValue<rapidjson::UTF8<>>;
using Writer = rapidjson::Writer<rapidjson::StringBuffer>;
struct IndexFile;
#define REFLECT_MEMBER_START() \
if (!ReflectMemberStart(visitor, value)) return
if (!ReflectMemberStart(visitor, value)) \
return
#define REFLECT_MEMBER_START1(value) \
if (!ReflectMemberStart(visitor, value)) return
#define REFLECT_MEMBER_END() \
ReflectMemberEnd(visitor, value);
#define REFLECT_MEMBER_END1(value) \
ReflectMemberEnd(visitor, value);
#define REFLECT_MEMBER(name) \
ReflectMember(visitor, #name, value.name)
#define REFLECT_MEMBER2(name, value) \
ReflectMember(visitor, name, value)
if (!ReflectMemberStart(visitor, value)) \
return
#define REFLECT_MEMBER_END() ReflectMemberEnd(visitor, value);
#define REFLECT_MEMBER_END1(value) ReflectMemberEnd(visitor, value);
#define REFLECT_MEMBER(name) ReflectMember(visitor, #name, value.name)
#define REFLECT_MEMBER2(name, value) ReflectMember(visitor, name, value)
#define MAKE_REFLECT_TYPE_PROXY(type, as_type) \
template <typename TVisitor> \
@ -39,8 +35,7 @@ struct IndexFile;
value = static_cast<type>(value0); \
}
#define _MAPPABLE_REFLECT_MEMBER(name) \
REFLECT_MEMBER(name);
#define _MAPPABLE_REFLECT_MEMBER(name) REFLECT_MEMBER(name);
#define MAKE_REFLECT_EMPTY_STRUCT(type, ...) \
template <typename TVisitor> \
@ -57,8 +52,7 @@ struct IndexFile;
REFLECT_MEMBER_END(); \
}
#define _MAPPABLE_REFLECT_ARRAY(name) \
Reflect(visitor, value.name);
#define _MAPPABLE_REFLECT_ARRAY(name) Reflect(visitor, value.name);
// Reflects the struct so it is serialized as an array instead of an object.
// This currently only supports writers.
@ -69,24 +63,9 @@ struct IndexFile;
visitor.EndArray(); \
}
template <typename T>
struct NonElidedVector : public std::vector<T> {};
// API:
/*
template<typename TVisitor, typename T>
@ -108,7 +87,6 @@ void ReflectMemberEnd(TVisitor& visitor, T& value) {
}
*/
// int16_t
void Reflect(Reader& visitor, int16_t& value);
void Reflect(Writer& visitor, int16_t& value);
@ -128,12 +106,6 @@ void Reflect(Writer& visitor, bool& value);
void Reflect(Reader& visitor, std::string& value);
void Reflect(Writer& visitor, std::string& value);
// Writer:
template <typename T>
void Reflect(Writer& visitor, std::vector<T>& values) {
@ -175,7 +147,9 @@ void ReflectMember(Writer& visitor, const char* name, std::vector<T>& values) {
visitor.EndArray();
}
template <typename T>
void ReflectMember(Writer& visitor, const char* name, NonElidedVector<T>& values) {
void ReflectMember(Writer& visitor,
const char* name,
NonElidedVector<T>& values) {
visitor.Key(name);
visitor.StartArray();
for (auto& value : values)
@ -223,6 +197,8 @@ void ReflectMember(Reader& visitor, const char* name, T& value) {
}
std::string Serialize(IndexFile& file);
std::unique_ptr<IndexFile> Deserialize(std::string path, std::string serialized, optional<int> expected_version);
std::unique_ptr<IndexFile> Deserialize(std::string path,
std::string serialized,
optional<int> expected_version);
void SetTestOutputMode();

View File

@ -22,16 +22,24 @@ std::string ToString(const rapidjson::Document& document) {
return buffer.GetString();
}
void DiffDocuments(std::string path, std::string path_section, rapidjson::Document& expected, rapidjson::Document& actual) {
void DiffDocuments(std::string path,
std::string path_section,
rapidjson::Document& expected,
rapidjson::Document& actual) {
std::string joined_actual_output = ToString(actual);
std::vector<std::string> actual_output = SplitString(joined_actual_output, "\n");
std::vector<std::string> actual_output =
SplitString(joined_actual_output, "\n");
std::string joined_expected_output = ToString(expected);
std::vector<std::string> expected_output = SplitString(joined_expected_output, "\n");
std::vector<std::string> expected_output =
SplitString(joined_expected_output, "\n");
std::cout << "[FAILED] " << path << " (section " << path_section << ")" << std::endl;
std::cout << "Expected output for " << path << " (section " << path_section << "):" << std::endl;
std::cout << "[FAILED] " << path << " (section " << path_section << ")"
<< std::endl;
std::cout << "Expected output for " << path << " (section " << path_section
<< "):" << std::endl;
std::cout << joined_expected_output << std::endl;
std::cout << "Actual output for " << path << " (section " << path_section << "):" << std::endl;
std::cout << "Actual output for " << path << " (section " << path_section
<< "):" << std::endl;
std::cout << joined_actual_output << std::endl;
std::cout << std::endl;
@ -66,15 +74,19 @@ void DiffDocuments(std::string path, std::string path_section, rapidjson::Docume
void VerifySerializeToFrom(IndexFile* file) {
std::string expected = file->ToString();
std::unique_ptr<IndexFile> result = Deserialize("--.cc", Serialize(*file), nullopt /*expected_version*/);
std::unique_ptr<IndexFile> result =
Deserialize("--.cc", Serialize(*file), nullopt /*expected_version*/);
std::string actual = result->ToString();
if (expected != actual) {
std::cerr << "Serialization failure" << std::endl;;
std::cerr << "Serialization failure" << std::endl;
;
assert(false);
}
}
std::string FindExpectedOutputForFilename(std::string filename, const std::unordered_map<std::string, std::string>& expected) {
std::string FindExpectedOutputForFilename(
std::string filename,
const std::unordered_map<std::string, std::string>& expected) {
for (const auto& entry : expected) {
if (EndsWith(entry.first, filename))
return entry.second;
@ -86,7 +98,9 @@ std::string FindExpectedOutputForFilename(std::string filename, const std::unord
return "{}";
}
IndexFile* FindDbForPathEnding(const std::string& path, const std::vector<std::unique_ptr<IndexFile>>& dbs) {
IndexFile* FindDbForPathEnding(
const std::string& path,
const std::vector<std::unique_ptr<IndexFile>>& dbs) {
for (auto& db : dbs) {
if (EndsWith(db->path, path))
return db.get();
@ -101,21 +115,25 @@ void RunTests() {
bool update_all = false;
clang::Index index(1, 0);
for (std::string path : GetFilesInFolder("tests", true /*recursive*/, true /*add_folder_to_path*/)) {
for (std::string path : GetFilesInFolder("tests", true /*recursive*/,
true /*add_folder_to_path*/)) {
float memory_before = GetProcessMemoryUsedInMb();
float memory_after = -1.;
{
// if (path != "tests/templates/specialized_func_definition.cc") continue;
//if (path != "tests/templates/namespace_template_class_template_func_usage_folded_into_one.cc") continue;
//if (path != "tests/multi_file/funky_enum.cc") continue;
//if (path != "tests/multi_file/simple_impl.cc") continue;
//if (path != "tests/inheritance/interface_pure_virtual.cc") continue;
//if (path != "tests/_empty_test.cc") continue;
//if (path != "tests/declaration_vs_definition/func_associated_function_params.cc") continue;
// if (path !=
// "tests/templates/namespace_template_class_template_func_usage_folded_into_one.cc")
// continue; if (path != "tests/multi_file/funky_enum.cc") continue; if
// (path != "tests/multi_file/simple_impl.cc") continue; if (path !=
// "tests/inheritance/interface_pure_virtual.cc") continue; if (path !=
// "tests/_empty_test.cc") continue; if (path !=
// "tests/declaration_vs_definition/func_associated_function_params.cc")
// continue;
//if (path != "tests/templates/template_class_type_usage_folded_into_one.cc") continue;
//path = "C:/Users/jacob/Desktop/superindex/indexer/" + path;
// if (path !=
// "tests/templates/template_class_type_usage_folded_into_one.cc")
// continue; path = "C:/Users/jacob/Desktop/superindex/indexer/" + path;
Config config;
FileConsumer::SharedState file_consumer_shared;
@ -124,25 +142,21 @@ void RunTests() {
// std::cout << "[START] " << path << std::endl;
PerformanceImportFile perf;
std::vector<std::unique_ptr<IndexFile>> dbs = Parse(
&config, &file_consumer_shared,
path,
{
"-xc++",
"-std=c++11",
&config, &file_consumer_shared, path,
{"-xc++", "-std=c++11",
"-IC:/Users/jacob/Desktop/cquery/third_party/",
"-IC:/Users/jacob/Desktop/cquery/third_party/doctest/",
"-IC:/Users/jacob/Desktop/cquery/third_party/rapidjson/include",
"-IC:/Users/jacob/Desktop/cquery/src",
"-isystemC:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.10.25017/include",
"-isystemC:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/ucrt"
},
{},
&perf,
&index,
false /*dump_ast*/);
"-isystemC:/Program Files (x86)/Microsoft Visual "
"Studio/2017/Community/VC/Tools/MSVC/14.10.25017/include",
"-isystemC:/Program Files (x86)/Windows "
"Kits/10/Include/10.0.15063.0/ucrt"},
{}, &perf, &index, false /*dump_ast*/);
// Parse expected output from the test, parse it into JSON document.
std::unordered_map<std::string, std::string> all_expected_output = ParseTestExpectation(path);
std::unordered_map<std::string, std::string> all_expected_output =
ParseTestExpectation(path);
for (auto& entry : all_expected_output) {
const std::string& expected_path = entry.first;
const std::string& expected_output = entry.second;
@ -164,12 +178,12 @@ void RunTests() {
if (actual == expected) {
std::cout << "[PASSED] " << path << std::endl;
}
else {
} else {
DiffDocuments(path, expected_path, expected, actual);
std::cout << std::endl;
std::cout << std::endl;
std::cout << "[Enter to continue - type u to update test, a to update all]";
std::cout
<< "[Enter to continue - type u to update test, a to update all]";
char c = 'u';
if (!update_all) {
c = (char)std::cin.get();
@ -180,9 +194,9 @@ void RunTests() {
update_all = true;
if (update_all || c == 'u') {
UpdateTestExpectation(path, expected_output, ToString(actual) + "\n");
UpdateTestExpectation(path, expected_output,
ToString(actual) + "\n");
}
}
}
@ -190,12 +204,16 @@ void RunTests() {
}
float memory_cleanup = GetProcessMemoryUsedInMb();
std::cerr << "[memory] before=" << memory_before << "mb, after=" << memory_after << "mb, cleanup=" << memory_cleanup << "mb" << std::endl;
std::cerr << "[memory] before=" << memory_before
<< "mb, after=" << memory_after
<< "mb, cleanup=" << memory_cleanup << "mb" << std::endl;
}
std::cerr << "[final presleep] " << GetProcessMemoryUsedInMb() << "mb" << std::endl;
std::cerr << "[final presleep] " << GetProcessMemoryUsedInMb() << "mb"
<< std::endl;
// std::this_thread::sleep_for(std::chrono::seconds(10));
//std::cerr << "[final postsleep] " << GetProcessMemoryUsedInMb() << "mb" << std::endl;
// std::cerr << "[final postsleep] " << GetProcessMemoryUsedInMb() << "mb" <<
// std::endl;
std::cerr << std::endl;
std::cerr << std::endl;
std::cerr << std::endl;
@ -204,5 +222,5 @@ void RunTests() {
}
// TODO: ctor/dtor, copy ctor
// TODO: Always pass IndexFile by pointer, ie, search and remove all IndexFile& refs.
// TODO: Always pass IndexFile by pointer, ie, search and remove all IndexFile&
// refs.

View File

@ -1,16 +1,18 @@
#pragma once
#include "work_thread.h"
#include <optional.h>
#include "work_thread.h"
#include <algorithm>
#include <atomic>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <mutex>
#include <queue>
using std::experimental::optional;
using std::experimental::nullopt;
using std::experimental::optional;
// TODO: cleanup includes.
@ -114,9 +116,8 @@ public:
template <typename TAction>
T DequeuePlusAction(TAction action) {
std::unique_lock<std::mutex> lock(mutex_);
waiter_->cv.wait(lock, [&]() {
return !priority_.empty() || !queue_.empty();
});
waiter_->cv.wait(lock,
[&]() { return !priority_.empty() || !queue_.empty(); });
if (!priority_.empty()) {
auto val = std::move(priority_.front());

View File

@ -10,12 +10,15 @@ Timer::Timer() {
long long Timer::ElapsedMicroseconds() const {
std::chrono::time_point<Clock> end = Clock::now();
return std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
return std::chrono::duration_cast<std::chrono::microseconds>(end - start)
.count();
}
long long Timer::ElapsedMicrosecondsAndReset() {
std::chrono::time_point<Clock> end = Clock::now();
long long microseconds = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
long long microseconds =
std::chrono::duration_cast<std::chrono::microseconds>(end - start)
.count();
Reset();
return microseconds;
}
@ -26,11 +29,14 @@ void Timer::Reset() {
void Timer::ResetAndPrint(const std::string& message) {
std::chrono::time_point<Clock> end = Clock::now();
long long elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
long long elapsed =
std::chrono::duration_cast<std::chrono::microseconds>(end - start)
.count();
long long milliseconds = elapsed / 1000;
long long remaining = elapsed - milliseconds;
LOG_S(INFO) << message << " took " << milliseconds << "." << remaining << "ms";
LOG_S(INFO) << message << " took " << milliseconds << "." << remaining
<< "ms";
Reset();
}

View File

@ -17,11 +17,9 @@ struct TypedBidiMessageQueue {
std::function<std::unique_ptr<TMessage>(Reader& visitor)>;
TypedBidiMessageQueue(const std::string& name, size_t buffer_size)
: for_server(
Buffer::CreateSharedBuffer(name + "_fs", buffer_size),
: for_server(Buffer::CreateSharedBuffer(name + "_fs", buffer_size),
false /*buffer_has_data*/),
for_client(
Buffer::CreateSharedBuffer(name + "_fc", buffer_size),
for_client(Buffer::CreateSharedBuffer(name + "_fc", buffer_size),
true /*buffer_has_data*/) {}
void RegisterId(TId id,
@ -42,7 +40,8 @@ struct TypedBidiMessageQueue {
writer.SetIndent(' ', 0);
// Serialize the message.
assert(serializers_.find(id) != serializers_.end() && "No registered serializer");
assert(serializers_.find(id) != serializers_.end() &&
"No registered serializer");
const Serializer& serializer = serializers_.find(id)->second;
serializer(writer, message);

View File

@ -7,25 +7,29 @@
#include <algorithm>
#include <cassert>
#include <cctype>
#include <fstream>
#include <functional>
#include <iostream>
#include <fstream>
#include <locale>
#include <sstream>
#include <unordered_map>
#if !defined(__APPLE__)
#include <sparsepp/spp_memory.h>
#endif
// See http://stackoverflow.com/a/217605
void TrimStart(std::string& s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(),
s.erase(s.begin(),
std::find_if(s.begin(), s.end(),
std::not1(std::ptr_fun<int, int>(std::isspace))));
}
void TrimEnd(std::string& s) {
s.erase(std::find_if(s.rbegin(), s.rend(),
std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
std::not1(std::ptr_fun<int, int>(std::isspace)))
.base(),
s.end());
}
void Trim(std::string& s) {
TrimStart(s);
@ -45,20 +49,24 @@ bool StartsWith(const std::string& value, const std::string& start) {
return std::equal(start.begin(), start.end(), value.begin());
}
bool AnyStartsWith(const std::vector<std::string>& values, const std::string& start) {
return std::any_of(std::begin(values), std::end(values), [&start](const std::string& value) {
return StartsWith(value, start);
});
bool AnyStartsWith(const std::vector<std::string>& values,
const std::string& start) {
return std::any_of(
std::begin(values), std::end(values),
[&start](const std::string& value) { return StartsWith(value, start); });
}
bool EndsWithAny(const std::string& value, const std::vector<std::string>& endings) {
return std::any_of(std::begin(endings), std::end(endings), [&value](const std::string& ending) {
return EndsWith(value, ending);
});
bool EndsWithAny(const std::string& value,
const std::vector<std::string>& endings) {
return std::any_of(
std::begin(endings), std::end(endings),
[&value](const std::string& ending) { return EndsWith(value, ending); });
}
// See http://stackoverflow.com/a/29752943
std::string ReplaceAll(const std::string& source, const std::string& from, const std::string& to) {
std::string ReplaceAll(const std::string& source,
const std::string& from,
const std::string& to) {
std::string result;
result.reserve(source.length()); // avoids a few memory allocations
@ -77,7 +85,8 @@ std::string ReplaceAll(const std::string& source, const std::string& from, const
return result;
}
std::vector<std::string> SplitString(const std::string& str, const std::string& delimiter) {
std::vector<std::string> SplitString(const std::string& str,
const std::string& delimiter) {
// http://stackoverflow.com/a/13172514
std::vector<std::string> strings;
@ -104,7 +113,10 @@ std::string LowerPathIfCaseInsensitive(const std::string& path) {
}
static void GetFilesInFolderHelper(
std::string folder, bool recursive, std::string output_prefix, const std::function<void(const std::string&)>& handler) {
std::string folder,
bool recursive,
std::string output_prefix,
const std::function<void(const std::string&)>& handler) {
tinydir_dir dir;
if (tinydir_open(&dir, folder.c_str()) == -1) {
// perror("Error opening file");
@ -127,10 +139,10 @@ static void GetFilesInFolderHelper(
if (recursive) {
std::string child_dir = output_prefix + file.name + "/";
if (!IsSymLink(child_dir))
GetFilesInFolderHelper(file.path, true /*recursive*/, child_dir, handler);
GetFilesInFolderHelper(file.path, true /*recursive*/, child_dir,
handler);
}
}
else {
} else {
handler(output_prefix + file.name);
}
}
@ -145,19 +157,24 @@ bail:
tinydir_close(&dir);
}
std::vector<std::string> GetFilesInFolder(std::string folder, bool recursive, bool add_folder_to_path) {
std::vector<std::string> GetFilesInFolder(std::string folder,
bool recursive,
bool add_folder_to_path) {
EnsureEndsInSlash(folder);
std::vector<std::string> result;
GetFilesInFolderHelper(folder, recursive, add_folder_to_path ? folder : "", [&result](const std::string& path) {
result.push_back(path);
});
GetFilesInFolderHelper(
folder, recursive, add_folder_to_path ? folder : "",
[&result](const std::string& path) { result.push_back(path); });
return result;
}
void GetFilesInFolder(std::string folder, bool recursive, bool add_folder_to_path, const std::function<void(const std::string&)>& handler) {
void GetFilesInFolder(std::string folder,
bool recursive,
bool add_folder_to_path,
const std::function<void(const std::string&)>& handler) {
EnsureEndsInSlash(folder);
GetFilesInFolderHelper(folder, recursive, add_folder_to_path ? folder : "", handler);
GetFilesInFolderHelper(folder, recursive, add_folder_to_path ? folder : "",
handler);
}
void EnsureEndsInSlash(std::string& path) {
@ -165,7 +182,6 @@ void EnsureEndsInSlash(std::string& path) {
path += '/';
}
// http://stackoverflow.com/a/6089413
std::istream& SafeGetline(std::istream& is, std::string& t) {
t.clear();
@ -205,8 +221,7 @@ optional<std::string> ReadContent(const std::string& filename) {
if (!cache.good())
return nullopt;
return std::string(
std::istreambuf_iterator<char>(cache),
return std::string(std::istreambuf_iterator<char>(cache),
std::istreambuf_iterator<char>());
}
@ -220,7 +235,8 @@ std::vector<std::string> ReadLines(std::string filename) {
return result;
}
std::vector<std::string> ToLines(const std::string& content, bool trim_whitespace) {
std::vector<std::string> ToLines(const std::string& content,
bool trim_whitespace) {
std::vector<std::string> result;
std::istringstream lines(content);
@ -235,7 +251,8 @@ std::vector<std::string> ToLines(const std::string& content, bool trim_whitespac
return result;
}
std::unordered_map<std::string, std::string> ParseTestExpectation(std::string filename) {
std::unordered_map<std::string, std::string> ParseTestExpectation(
std::string filename) {
bool in_output = false;
#if false
@ -275,8 +292,7 @@ std::unordered_map<std::string, std::string> ParseTestExpectation(std::string fi
active_output_contents = "";
in_output = true;
}
else if (in_output)
} else if (in_output)
active_output_contents += line + "\n";
}
@ -285,11 +301,14 @@ std::unordered_map<std::string, std::string> ParseTestExpectation(std::string fi
return result;
}
void UpdateTestExpectation(const std::string& filename, const std::string& expectation, const std::string& actual) {
void UpdateTestExpectation(const std::string& filename,
const std::string& expectation,
const std::string& actual) {
// Read the entire file into a string.
std::ifstream in(filename);
std::string str;
str.assign(std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>());
str.assign(std::istreambuf_iterator<char>(in),
std::istreambuf_iterator<char>());
in.close();
// Replace expectation
@ -333,4 +352,3 @@ std::string FormatMicroseconds(long long microseconds) {
return std::to_string(milliseconds) + "." + std::to_string(remaining) + "ms";
}

View File

@ -10,8 +10,8 @@
#include <unordered_map>
#include <vector>
using std::experimental::optional;
using std::experimental::nullopt;
using std::experimental::optional;
// Trim from start (in place)
void TrimStart(std::string& s);
@ -23,16 +23,20 @@ void Trim(std::string& s);
// Returns true if |value| starts/ends with |start| or |ending|.
bool StartsWith(const std::string& value, const std::string& start);
bool EndsWith(const std::string& value, const std::string& ending);
bool AnyStartsWith(const std::vector<std::string>& values, const std::string& start);
bool EndsWithAny(const std::string& value, const std::vector<std::string>& endings);
bool AnyStartsWith(const std::vector<std::string>& values,
const std::string& start);
bool EndsWithAny(const std::string& value,
const std::vector<std::string>& endings);
std::string ReplaceAll(const std::string& source, const std::string& from, const std::string& to);
std::string ReplaceAll(const std::string& source,
const std::string& from,
const std::string& to);
std::vector<std::string> SplitString(const std::string& str, const std::string& delimiter);
std::vector<std::string> SplitString(const std::string& str,
const std::string& delimiter);
std::string LowerPathIfCaseInsensitive(const std::string& path);
template <typename TValues, typename TMap>
std::string StringJoinMap(const TValues& values, const TMap& map) {
std::string result;
@ -48,33 +52,39 @@ std::string StringJoinMap(const TValues& values, const TMap& map) {
template <typename TValues>
std::string StringJoin(const TValues& values) {
return StringJoinMap(values, [](const std::string& entry) {
return entry;
});
return StringJoinMap(values, [](const std::string& entry) { return entry; });
}
// Finds all files in the given folder. This is recursive.
std::vector<std::string> GetFilesInFolder(std::string folder, bool recursive, bool add_folder_to_path);
void GetFilesInFolder(std::string folder, bool recursive, bool add_folder_to_path, const std::function<void(const std::string&)>& handler);
std::vector<std::string> GetFilesInFolder(std::string folder,
bool recursive,
bool add_folder_to_path);
void GetFilesInFolder(std::string folder,
bool recursive,
bool add_folder_to_path,
const std::function<void(const std::string&)>& handler);
// Ensures that |path| ends in a slash.
void EnsureEndsInSlash(std::string& path);
optional<std::string> ReadContent(const std::string& filename);
std::vector<std::string> ReadLines(std::string filename);
std::vector<std::string> ToLines(const std::string& content, bool trim_whitespace);
std::vector<std::string> ToLines(const std::string& content,
bool trim_whitespace);
std::unordered_map<std::string, std::string> ParseTestExpectation(std::string filename);
void UpdateTestExpectation(const std::string& filename, const std::string& expectation, const std::string& actual);
std::unordered_map<std::string, std::string> ParseTestExpectation(
std::string filename);
void UpdateTestExpectation(const std::string& filename,
const std::string& expectation,
const std::string& actual);
void Fail(const std::string& message);
void WriteToFile(const std::string& filename, const std::string& content);
// note: this implementation does not disable this overload for array types
// See http://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique#Possible_Implementatiog
// See
// http://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique#Possible_Implementatiog
template <typename T, typename... Args>
std::unique_ptr<T> MakeUnique(Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
@ -94,10 +104,14 @@ void PushRange(std::queue<T>* dest, const std::vector<T>& to_add) {
template <typename T>
void RemoveRange(std::vector<T>* dest, const std::vector<T>& to_remove) {
dest->erase(std::remove_if(dest->begin(), dest->end(), [&](const T& t) {
dest->erase(std::remove_if(dest->begin(), dest->end(),
[&](const T& t) {
// TODO: make to_remove a set?
return std::find(to_remove.begin(), to_remove.end(), t) != to_remove.end();
}), dest->end());
return std::find(to_remove.begin(),
to_remove.end(),
t) != to_remove.end();
}),
dest->end());
}
// http://stackoverflow.com/a/38140932
@ -120,7 +134,8 @@ inline void hash_combine(std::size_t& seed, const T& v, Rest... rest) {
#define MAKE_HASHABLE(type, ...) \
namespace std { \
template<> struct hash<type> {\
template <> \
struct hash<type> { \
std::size_t operator()(const type& t) const { \
std::size_t ret = 0; \
hash_combine(ret, __VA_ARGS__); \
@ -131,7 +146,8 @@ inline void hash_combine(std::size_t& seed, const T& v, Rest... rest) {
#define MAKE_ENUM_HASHABLE(type) \
namespace std { \
template<> struct hash<type> {\
template <> \
struct hash<type> { \
std::size_t operator()(const type& t) const { \
return hash<int>()(static_cast<int>(t)); \
} \

View File

@ -6,8 +6,7 @@ std::atomic<int> WorkThread::num_active_threads;
std::atomic<bool> WorkThread::request_exit_on_idle;
// static
void WorkThread::StartThread(
const std::string& thread_name,
void WorkThread::StartThread(const std::string& thread_name,
const std::function<Result()>& entry_point) {
new std::thread([thread_name, entry_point]() {
SetCurrentThreadName(thread_name);

View File

@ -9,11 +9,7 @@
// Helper methods for starting threads that do some work. Enables test code to
// wait for all work to complete.
struct WorkThread {
enum class Result {
MoreWork,
NoWork,
ExitThread
};
enum class Result { MoreWork, NoWork, ExitThread };
// The number of active worker threads.
static std::atomic<int> num_active_threads;
@ -22,8 +18,7 @@ struct WorkThread {
// Launch a new thread. |entry_point| will be called continously. It should
// return true if it there is still known work to be done.
static void StartThread(
const std::string& thread_name,
static void StartThread(const std::string& thread_name,
const std::function<Result()>& entry_point);
// Static-only class.

View File

@ -19,8 +19,7 @@ lsPosition GetPositionForOffset(const std::string& content, int offset) {
if (content[i] == '\n') {
result.line += 1;
result.character = 0;
}
else {
} else {
result.character += 1;
}
++i;
@ -31,9 +30,8 @@ lsPosition GetPositionForOffset(const std::string& content, int offset) {
} // namespace
WorkingFile::WorkingFile(const std::string& filename, const std::string& buffer_content)
WorkingFile::WorkingFile(const std::string& filename,
const std::string& buffer_content)
: filename(filename), buffer_content(buffer_content) {
OnBufferContentUpdated();
@ -88,12 +86,13 @@ optional<int> WorkingFile::GetBufferLineFromIndexLine(int index_line) const {
// TODO: reenable this assert once we are using the real indexed file.
// assert(index_line >= 1 && index_line <= index_lines.size());
if (index_line < 1 || index_line > index_lines.size()) {
std::cerr << "!! Bad index_line (got " << index_line << ", expected [1, " << index_lines.size() << "])" << std::endl;
std::cerr << "!! Bad index_line (got " << index_line << ", expected [1, "
<< index_lines.size() << "])" << std::endl;
return nullopt;
}
// Find the line in the cached index file. We'll try to find the most similar line
// in the buffer and return the index for that.
// Find the line in the cached index file. We'll try to find the most similar
// line in the buffer and return the index for that.
std::string index = index_lines[index_line - 1];
auto buffer_it = all_buffer_lines_lookup.find(index);
if (buffer_it == all_buffer_lines_lookup.end()) {
@ -124,7 +123,8 @@ optional<int> WorkingFile::GetIndexLineFromBufferLine(int buffer_line) const {
// Note: |index_line| and |buffer_line| are 1-based.
// assert(buffer_line >= 1 && buffer_line < all_buffer_lines.size());
if (buffer_line < 1 || buffer_line > all_buffer_lines.size()) {
std::cerr << "!! Bad buffer_line (got " << buffer_line << ", expected [1, " << all_buffer_lines.size() << "])" << std::endl;
std::cerr << "!! Bad buffer_line (got " << buffer_line << ", expected [1, "
<< all_buffer_lines.size() << "])" << std::endl;
return nullopt;
}
@ -154,7 +154,9 @@ optional<int> WorkingFile::GetIndexLineFromBufferLine(int buffer_line) const {
return closest_index_line;
}
optional<std::string> WorkingFile::GetBufferLineContentFromIndexLine(int indexed_line, optional<int>* out_buffer_line) const {
optional<std::string> WorkingFile::GetBufferLineContentFromIndexLine(
int indexed_line,
optional<int>* out_buffer_line) const {
optional<int> buffer_line = GetBufferLineFromIndexLine(indexed_line);
if (out_buffer_line)
*out_buffer_line = buffer_line;
@ -163,14 +165,19 @@ optional<std::string> WorkingFile::GetBufferLineContentFromIndexLine(int indexed
return nullopt;
if (*buffer_line < 1 || *buffer_line >= all_buffer_lines.size()) {
std::cerr << "GetBufferLineContentFromIndexLine buffer line lookup not in all_buffer_lines" << std::endl;
std::cerr << "GetBufferLineContentFromIndexLine buffer line lookup not in "
"all_buffer_lines"
<< std::endl;
return nullopt;
}
return all_buffer_lines[*buffer_line - 1];
}
std::string WorkingFile::FindClosestCallNameInBuffer(lsPosition position, int* active_parameter, lsPosition* completion_position) const {
std::string WorkingFile::FindClosestCallNameInBuffer(
lsPosition position,
int* active_parameter,
lsPosition* completion_position) const {
*active_parameter = 0;
int offset = GetOffsetForPosition(position, buffer_content);
@ -184,8 +191,10 @@ std::string WorkingFile::FindClosestCallNameInBuffer(lsPosition position, int* a
int balance = 0;
while (offset > 0) {
char c = buffer_content[offset];
if (c == ')') ++balance;
else if (c == '(') --balance;
if (c == ')')
++balance;
else if (c == '(')
--balance;
if (balance == 0 && c == ',')
*active_parameter += 1;
@ -214,7 +223,10 @@ std::string WorkingFile::FindClosestCallNameInBuffer(lsPosition position, int* a
return buffer_content.substr(offset, start_offset - offset + 1);
}
lsPosition WorkingFile::FindStableCompletionSource(lsPosition position, bool* is_global_completion, std::string* existing_completion) const {
lsPosition WorkingFile::FindStableCompletionSource(
lsPosition position,
bool* is_global_completion,
std::string* existing_completion) const {
*is_global_completion = true;
int start_offset = GetOffsetForPosition(position, buffer_content);
@ -257,7 +269,8 @@ WorkingFile* WorkingFiles::GetFileByFilename(const std::string& filename) {
return GetFileByFilenameNoLock(filename);
}
WorkingFile* WorkingFiles::GetFileByFilenameNoLock(const std::string& filename) {
WorkingFile* WorkingFiles::GetFileByFilenameNoLock(
const std::string& filename) {
for (auto& file : files) {
if (file->filename == filename)
return file.get();
@ -273,7 +286,6 @@ void WorkingFiles::DoAction(const std::function<void()>& action) {
void WorkingFiles::DoActionOnFile(
const std::string& filename,
const std::function<void(WorkingFile* file)>& action) {
std::lock_guard<std::mutex> lock(files_mutex);
WorkingFile* file = GetFileByFilenameNoLock(filename);
action(file);
@ -303,7 +315,8 @@ void WorkingFiles::OnChange(const Ipc_TextDocumentDidChange::Params& change) {
std::string filename = change.textDocument.uri.GetPath();
WorkingFile* file = GetFileByFilenameNoLock(filename);
if (!file) {
std::cerr << "Could not change " << filename << " because it was not open" << std::endl;
std::cerr << "Could not change " << filename << " because it was not open"
<< std::endl;
return;
}
@ -311,18 +324,22 @@ void WorkingFiles::OnChange(const Ipc_TextDocumentDidChange::Params& change) {
// std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
// std::cerr << "VERSION " << change.textDocument.version << std::endl;
for (const Ipc_TextDocumentDidChange::lsTextDocumentContentChangeEvent& diff : change.contentChanges) {
for (const Ipc_TextDocumentDidChange::lsTextDocumentContentChangeEvent& diff :
change.contentChanges) {
// std::cerr << "|" << file->buffer_content << "|" << std::endl;
// If range or rangeLength are emitted we replace everything, per the spec.
if (diff.rangeLength == -1) {
file->buffer_content = diff.text;
file->OnBufferContentUpdated();
// std::cerr << "-> Replacing entire content";
}
else {
int start_offset = GetOffsetForPosition(diff.range.start, file->buffer_content);
// std::cerr << "-> Applying diff start=" << diff.range.start.ToString() << ", end=" << diff.range.end.ToString() << ", start_offset=" << start_offset << std::endl;
file->buffer_content.replace(file->buffer_content.begin() + start_offset,
} else {
int start_offset =
GetOffsetForPosition(diff.range.start, file->buffer_content);
// std::cerr << "-> Applying diff start=" << diff.range.start.ToString()
// << ", end=" << diff.range.end.ToString() << ", start_offset=" <<
// start_offset << std::endl;
file->buffer_content.replace(
file->buffer_content.begin() + start_offset,
file->buffer_content.begin() + start_offset + diff.rangeLength,
diff.text);
file->OnBufferContentUpdated();
@ -332,7 +349,8 @@ void WorkingFiles::OnChange(const Ipc_TextDocumentDidChange::Params& change) {
}
// std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
//std::cerr << std::endl << std::endl << "--------" << file->content << "--------" << std::endl << std::endl;
// std::cerr << std::endl << std::endl << "--------" << file->content <<
// "--------" << std::endl << std::endl;
}
void WorkingFiles::OnClose(const Ipc_TextDocumentDidClose::Params& close) {
@ -347,7 +365,8 @@ void WorkingFiles::OnClose(const Ipc_TextDocumentDidClose::Params& close) {
}
}
std::cerr << "Could not close " << filename << " because it was not open" << std::endl;
std::cerr << "Could not close " << filename << " because it was not open"
<< std::endl;
}
std::vector<CXUnsavedFile> WorkingFiles::AsUnsavedFiles() {
@ -362,82 +381,108 @@ std::vector<CXUnsavedFile> WorkingFiles::AsUnsavedFiles() {
TEST_SUITE("WorkingFile");
lsPosition CharPos(const WorkingFile& file, char character, int character_offset = 0) {
lsPosition CharPos(const WorkingFile& file,
char character,
int character_offset = 0) {
return CharPos(file.buffer_content, character, character_offset);
}
TEST_CASE("simple call") {
WorkingFile f("foo.cc", "abcd(1, 2");
int active_param = 0;
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, '('), &active_param) == "abcd");
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, '('), &active_param) ==
"abcd");
REQUIRE(active_param == 0);
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, '1'), &active_param) == "abcd");
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, '1'), &active_param) ==
"abcd");
REQUIRE(active_param == 0);
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, ','), &active_param) == "abcd");
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, ','), &active_param) ==
"abcd");
REQUIRE(active_param == 1);
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, ' '), &active_param) == "abcd");
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, ' '), &active_param) ==
"abcd");
REQUIRE(active_param == 1);
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, '2'), &active_param) == "abcd");
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, '2'), &active_param) ==
"abcd");
REQUIRE(active_param == 1);
}
TEST_CASE("nested call") {
WorkingFile f("foo.cc", "abcd(efg(), 2");
int active_param = 0;
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, '('), &active_param) == "abcd");
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, '('), &active_param) ==
"abcd");
REQUIRE(active_param == 0);
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, 'e'), &active_param) == "abcd");
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, 'e'), &active_param) ==
"abcd");
REQUIRE(active_param == 0);
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, 'f'), &active_param) == "abcd");
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, 'f'), &active_param) ==
"abcd");
REQUIRE(active_param == 0);
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, 'g'), &active_param) == "abcd");
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, 'g'), &active_param) ==
"abcd");
REQUIRE(active_param == 0);
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, 'g', 1), &active_param) == "efg");
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, 'g', 1), &active_param) ==
"efg");
REQUIRE(active_param == 0);
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, 'g', 2), &active_param) == "efg");
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, 'g', 2), &active_param) ==
"efg");
REQUIRE(active_param == 0);
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, ','), &active_param) == "abcd");
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, ','), &active_param) ==
"abcd");
REQUIRE(active_param == 1);
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, ' '), &active_param) == "abcd");
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, ' '), &active_param) ==
"abcd");
REQUIRE(active_param == 1);
}
TEST_CASE("auto-insert )") {
WorkingFile f("foo.cc", "abc()");
int active_param = 0;
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, ')'), &active_param) == "abc");
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, ')'), &active_param) ==
"abc");
REQUIRE(active_param == 0);
}
TEST_CASE("existing completion") {
// TODO: remove trailing space in zz.asdf. Lexing doesn't work correctly if done at the end of input.
// TODO: remove trailing space in zz.asdf. Lexing doesn't work correctly if
// done at the end of input.
WorkingFile f("foo.cc", "zzz.asdf ");
bool is_global_completion;
std::string existing_completion;
f.FindStableCompletionSource(CharPos(f, '.'), &is_global_completion, &existing_completion);
f.FindStableCompletionSource(CharPos(f, '.'), &is_global_completion,
&existing_completion);
REQUIRE(existing_completion == "zzz");
f.FindStableCompletionSource(CharPos(f, 'a', 1), &is_global_completion, &existing_completion);
f.FindStableCompletionSource(CharPos(f, 'a', 1), &is_global_completion,
&existing_completion);
REQUIRE(existing_completion == "a");
f.FindStableCompletionSource(CharPos(f, 's', 1), &is_global_completion, &existing_completion);
f.FindStableCompletionSource(CharPos(f, 's', 1), &is_global_completion,
&existing_completion);
REQUIRE(existing_completion == "as");
f.FindStableCompletionSource(CharPos(f, 'd', 1), &is_global_completion, &existing_completion);
f.FindStableCompletionSource(CharPos(f, 'd', 1), &is_global_completion,
&existing_completion);
REQUIRE(existing_completion == "asd");
f.FindStableCompletionSource(CharPos(f, 'f', 1), &is_global_completion, &existing_completion);
f.FindStableCompletionSource(CharPos(f, 'f', 1), &is_global_completion,
&existing_completion);
REQUIRE(existing_completion == "asdf");
}
TEST_CASE("existing completion underscore") {
// TODO: remove trailing space in ABC_DEF. Lexing doesn't work correctly if done at the end of input.
// TODO: remove trailing space in ABC_DEF. Lexing doesn't work correctly if
// done at the end of input.
WorkingFile f("foo.cc", "ABC_DEF ");
bool is_global_completion;
std::string existing_completion;
f.FindStableCompletionSource(CharPos(f, 'C'), &is_global_completion, &existing_completion);
f.FindStableCompletionSource(CharPos(f, 'C'), &is_global_completion,
&existing_completion);
REQUIRE(existing_completion == "AB");
f.FindStableCompletionSource(CharPos(f, '_'), &is_global_completion, &existing_completion);
f.FindStableCompletionSource(CharPos(f, '_'), &is_global_completion,
&existing_completion);
REQUIRE(existing_completion == "ABC");
f.FindStableCompletionSource(CharPos(f, 'D'), &is_global_completion, &existing_completion);
f.FindStableCompletionSource(CharPos(f, 'D'), &is_global_completion,
&existing_completion);
REQUIRE(existing_completion == "ABC_");
}

View File

@ -9,8 +9,8 @@
#include <mutex>
#include <string>
using std::experimental::optional;
using std::experimental::nullopt;
using std::experimental::optional;
struct WorkingFile {
int version = 0;
@ -28,7 +28,8 @@ struct WorkingFile {
// Note: The items in the value entry are 1-based liness.
std::unordered_map<std::string, std::vector<int>> all_buffer_lines_lookup;
// A set of diagnostics that have been reported for this file.
// NOTE: _ is appended because it must be accessed under the WorkingFiles lock!
// NOTE: _ is appended because it must be accessed under the WorkingFiles
// lock!
std::vector<lsDiagnostic> diagnostics_;
WorkingFile(const std::string& filename, const std::string& buffer_content);
@ -45,16 +46,22 @@ struct WorkingFile {
// accepts and returns 1-based lines.
optional<int> GetIndexLineFromBufferLine(int buffer_line) const;
optional<std::string> GetBufferLineContentFromIndexLine(int indexed_line, optional<int>* out_buffer_line) const;
optional<std::string> GetBufferLineContentFromIndexLine(
int indexed_line,
optional<int>* out_buffer_line) const;
// TODO: Move FindClosestCallNameInBuffer and FindStableCompletionSource into lex_utils.h/cc
// TODO: Move FindClosestCallNameInBuffer and FindStableCompletionSource into
// lex_utils.h/cc
// Finds the closest 'callable' name prior to position. This is used for
// signature help to filter code completion results.
//
// |completion_position| will be point to a good code completion location to
// for fetching signatures.
std::string FindClosestCallNameInBuffer(lsPosition position, int* active_parameter, lsPosition* completion_position = nullptr) const;
std::string FindClosestCallNameInBuffer(
lsPosition position,
int* active_parameter,
lsPosition* completion_position = nullptr) const;
// Returns a relatively stable completion position (it jumps back until there
// is a non-alphanumeric character).
@ -63,7 +70,9 @@ struct WorkingFile {
// global completion.
// The out param |existing_completion| is set to any existing completion
// content the user has entered.
lsPosition FindStableCompletionSource(lsPosition position, bool* is_global_completion, std::string* existing_completion) const;
lsPosition FindStableCompletionSource(lsPosition position,
bool* is_global_completion,
std::string* existing_completion) const;
CXUnsavedFile AsUnsavedFile() const;
};
@ -79,8 +88,10 @@ struct WorkingFiles {
// Run |action| under the lock.
void DoAction(const std::function<void()>& action);
// Run |action| on the file identified by |filename|. This executes under the lock.
void DoActionOnFile(const std::string& filename, const std::function<void(WorkingFile* file)>& action);
// Run |action| on the file identified by |filename|. This executes under the
// lock.
void DoActionOnFile(const std::string& filename,
const std::function<void(WorkingFile* file)>& action);
WorkingFile* OnOpen(const Ipc_TextDocumentDidOpen::Params& open);
void OnChange(const Ipc_TextDocumentDidChange::Params& change);