From 82596abc51c7912f4f3ba088e92b796af2f9cff7 Mon Sep 17 00:00:00 2001 From: Jacob Dufault Date: Thu, 30 Mar 2017 21:13:58 -0700 Subject: [PATCH] misc cleanup --- src/clang_args | 16 ++++---- src/code_completion.cc | 59 +++++++++++------------------- src/command_line.cc | 24 ++++-------- src/compilation_database_loader.cc | 1 - src/timer.cc | 7 ++++ src/timer.h | 3 ++ 6 files changed, 48 insertions(+), 62 deletions(-) diff --git a/src/clang_args b/src/clang_args index 72392f12..c48c4029 100644 --- a/src/clang_args +++ b/src/clang_args @@ -4,15 +4,15 @@ # Includes # Windows -#-IC:/Users/jacob/Desktop/superindex/indexer/third_party -#-IC:/Users/jacob/Desktop/superindex/indexer/third_party/doctest -#-IC:/Users/jacob/Desktop/superindex/indexer/third_party/rapidjson/include -#-IC:/Program Files/LLVM/include +-IC:/Users/jacob/Desktop/superindex/indexer/third_party +-IC:/Users/jacob/Desktop/superindex/indexer/third_party/doctest +-IC:/Users/jacob/Desktop/superindex/indexer/third_party/rapidjson/include +-IC:/Program Files/LLVM/include # OSX --I/Users/jdufault/Personal/super-clang-index/third_party --I/Users/jdufault/Personal/super-clang-index/third_party/doctest --I/Users/jdufault/Personal/super-clang-index/third_party/rapidjson/include --I/Users/jdufault/Personal/super-clang-index/build/clang+llvm-4.0.0-x86_64-apple-darwin/include +#-I/Users/jdufault/Personal/super-clang-index/third_party +#-I/Users/jdufault/Personal/super-clang-index/third_party/doctest +#-I/Users/jdufault/Personal/super-clang-index/third_party/rapidjson/include +#-I/Users/jdufault/Personal/super-clang-index/build/clang+llvm-4.0.0-x86_64-apple-darwin/include # Use libcxx #-stdlib=libc++ diff --git a/src/code_completion.cc b/src/code_completion.cc index e5e33e46..7f2cc799 100644 --- a/src/code_completion.cc +++ b/src/code_completion.cc @@ -94,6 +94,21 @@ lsCompletionItemKind GetCompletionKind(CXCursorKind cursor_kind) { } } +std::string BuildLabelString(CXCompletionString completion_string) { + std::string label; + + int num_chunks = clang_getNumCompletionChunks(completion_string); + for (unsigned i = 0; i < num_chunks; ++i) { + CXCompletionChunkKind kind = clang_getCompletionChunkKind(completion_string, i); + if (kind == CXCompletionChunk_TypedText) { + label += clang::ToString(clang_getCompletionChunkText(completion_string, i)); + break; + } + } + + return label; +} + std::string BuildDetailString(CXCompletionString completion_string) { std::string detail; @@ -249,13 +264,12 @@ NonElidedVector CompletionManager::CodeComplete(const lsTextDo std::cerr << "Code completion failed" << std::endl; return ls_result; } - std::cerr << "clang_codeCompleteAt took " << timer.ElapsedMilliseconds() << "ms; got " << cx_results->NumResults << " results" << std::endl; + timer.ResetAndPrint("clangCodeCompleteAt"); + std::cerr << "Got " << cx_results->NumResults << " results" << std::endl; // TODO: for comments we could hack the unsaved buffer and transform // into /// - timer.Reset(); ls_result.reserve(cx_results->NumResults); - std::cerr << "Reserving results took " << timer.ElapsedMilliseconds() << "ms" << std::endl; timer.Reset(); for (int i = 0; i < cx_results->NumResults; ++i) { @@ -281,26 +295,14 @@ NonElidedVector CompletionManager::CodeComplete(const lsTextDo // TODO: fill in more data lsCompletionItem ls_completion_item; + // kind/label/detail/docs ls_completion_item.kind = GetCompletionKind(result.CursorKind); - - - // Get the primary text to insert. - int num_chunks = clang_getNumCompletionChunks(result.CompletionString); - for (unsigned i = 0; i < num_chunks; ++i) { - CXCompletionChunkKind kind = clang_getCompletionChunkKind(result.CompletionString, i); - if (kind == CXCompletionChunk_TypedText) { - ls_completion_item.label += clang::ToString(clang_getCompletionChunkText(result.CompletionString, i)); - break; - } - } + ls_completion_item.label = BuildLabelString(result.CompletionString); ls_completion_item.detail = BuildDetailString(result.CompletionString); - - // Get docs. ls_completion_item.documentation = clang::ToString(clang_getCompletionBriefComment(result.CompletionString)); - // Set priority, and make some adjustments. + // Priority int priority = clang_getCompletionPriority(result.CompletionString); - if (result.CursorKind == CXCursor_Destructor) { priority *= 100; //std::cerr << "Bumping[destructor] " << ls_completion_item.label << std::endl; @@ -321,29 +323,12 @@ NonElidedVector CompletionManager::CodeComplete(const lsTextDo ls_result.push_back(ls_completion_item); } - std::cerr << "Building completion results took " << timer.ElapsedMilliseconds() << "ms" << std::endl; + timer.ResetAndPrint("Building completion results"); - timer.Reset(); clang_disposeCodeCompleteResults(cx_results); - std::cerr << "clang_disposeCodeCompleteResults took " << timer.ElapsedMilliseconds() << "ms" << std::endl; - - // Score completions by priority. - /* - timer.Reset(); - std::sort( - ls_result.begin(), ls_result.end(), - [](const lsCompletionItem& a, const lsCompletionItem& b) { - return a.priority_ < b.priority_; - }); - std::cerr << "Sorting completion results took " << timer.ElapsedMilliseconds() << "ms" << std::endl; - */ - //std::cerr << std::endl; - //for (auto& result : ls_result) { - // std::cerr << "SORTED priority=" << result.priority_ << ", sortText=" << result.sortText << ", label=" << result.label << std::endl; - //} + timer.ResetAndPrint("clang_disposeCodeCompleteResults "); return ls_result; - //clang_codeCompleteAt() // we should probably main two translation units, one for // serving current requests, and one that is reparsing (follow qtcreator) diff --git a/src/command_line.cc b/src/command_line.cc index e4f619a6..61430daa 100644 --- a/src/command_line.cc +++ b/src/command_line.cc @@ -168,19 +168,14 @@ void IndexMain(IndexRequestQueue* requests, IndexResponseQueue* responses) { Timer time; IndexedFile file = Parse(request->path, request->args); - std::cerr << "Parsing/indexing took " << time.ElapsedMilliseconds() - << "ms" << std::endl; + time.ResetAndPrint("Parsing/indexing"); - time.Reset(); IndexUpdate update(file); IndexTranslationUnitResponse response(update); - std::cerr << "Creating index update took " << time.ElapsedMilliseconds() - << "ms" << std::endl; + time.ResetAndPrint("Creating index update/response"); - time.Reset(); responses->Enqueue(response); - std::cerr << "Sending to server took " << time.ElapsedMilliseconds() - << "ms" << std::endl; + time.ResetAndPrint("Sending update to server"); } } @@ -212,11 +207,9 @@ void AddCodeLens(std::vector* result, const char* singular, const char* plural) { TCodeLens code_lens; - code_lens.range.start.line = - loc.line - 1; // TODO: cleanup indexer to negate by 1. - code_lens.range.start.character = - loc.column - 1; // TODO: cleanup indexer to negate by 1. - // TODO: store range information. + code_lens.range.start.line = loc.line - 1; + code_lens.range.start.character = loc.column - 1; + // TODO: store range information. code_lens.range.end.line = code_lens.range.start.line; code_lens.range.end.character = code_lens.range.start.character; @@ -379,7 +372,7 @@ void QueryDbMainLoop( Timer timer; response.Write(std::cout); - std::cerr << "Writing completion results to stdout took " << timer.ElapsedMilliseconds() << "ms" << std::endl; + timer.ResetAndPrint("Writing completion results"); //SendOutMessageToClient(language_client, response); break; } @@ -620,8 +613,7 @@ void QueryDbMainLoop( Timer time; db->ApplyIndexUpdate(&response->update); - std::cerr << "Applying index update took " << time.ElapsedMilliseconds() - << "ms" << std::endl; + time.ResetAndPrint("Applying index update"); } } diff --git a/src/compilation_database_loader.cc b/src/compilation_database_loader.cc index 4a9dda2e..aec319a0 100644 --- a/src/compilation_database_loader.cc +++ b/src/compilation_database_loader.cc @@ -143,7 +143,6 @@ std::vector LoadCompilationEntriesFromDirectory(const std::str CXCompileCommand cx_command = clang_CompileCommands_getCommand(cx_commands, i); CompilationEntry entry; - // TODO: remove ComplationEntry::directory 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; diff --git a/src/timer.cc b/src/timer.cc index 6ef17120..431ab105 100644 --- a/src/timer.cc +++ b/src/timer.cc @@ -1,5 +1,7 @@ #include "timer.h" +#include + Timer::Timer() { Reset(); } @@ -8,6 +10,11 @@ void Timer::Reset() { start = Clock::now(); } +void Timer::ResetAndPrint(const std::string& message) { + std::cerr << message << " took " << ElapsedMilliseconds() << "ms" << std::endl; + Reset(); +} + long long Timer::ElapsedMilliseconds() { std::chrono::time_point end = Clock::now(); return std::chrono::duration_cast(end - start).count(); diff --git a/src/timer.h b/src/timer.h index 8bf5d01b..6f3607a4 100644 --- a/src/timer.h +++ b/src/timer.h @@ -1,6 +1,7 @@ #pragma once #include +#include struct Timer { using Clock = std::chrono::high_resolution_clock; @@ -10,6 +11,8 @@ struct Timer { // Restart/reset the timer. void Reset(); + // Resets timer and prints a message like " took 5ms" + void ResetAndPrint(const std::string& message); // Return the number of milliseconds since the timer was last reset. long long ElapsedMilliseconds();