[clang-tools-extra] r342137 - [clangd] Use JSON format in benchmark requests reader

Kirill Bobyrev via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 13 07:21:50 PDT 2018


Author: omtcyfz
Date: Thu Sep 13 07:21:50 2018
New Revision: 342137

URL: http://llvm.org/viewvc/llvm-project?rev=342137&view=rev
Log:
[clangd] Use JSON format in benchmark requests reader

After `FuzzyFindRequest` JSON (de)serialization was introduced, it
should replace ad-hoc fuzzy-find request parsing implemented in the
IndexBenchmark driver.

Reviewed By: ilya-biryukov

Differential Revision: https://reviews.llvm.org/D51971

Added:
    clang-tools-extra/trunk/test/clangd/Inputs/requests.json
Removed:
    clang-tools-extra/trunk/test/clangd/Inputs/requests.log
Modified:
    clang-tools-extra/trunk/clangd/benchmarks/IndexBenchmark.cpp
    clang-tools-extra/trunk/test/clangd/index-tools.test

Modified: clang-tools-extra/trunk/clangd/benchmarks/IndexBenchmark.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/benchmarks/IndexBenchmark.cpp?rev=342137&r1=342136&r2=342137&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/benchmarks/IndexBenchmark.cpp (original)
+++ clang-tools-extra/trunk/clangd/benchmarks/IndexBenchmark.cpp Thu Sep 13 07:21:50 2018
@@ -19,56 +19,51 @@
 #include <string>
 
 const char *IndexFilename;
-const char *LogFilename;
+const char *RequestsFilename;
 
 namespace clang {
 namespace clangd {
 namespace {
 
-std::unique_ptr<clang::clangd::SymbolIndex> buildMem() {
-  return clang::clangd::loadIndex(IndexFilename, {}, false);
+std::unique_ptr<SymbolIndex> buildMem() {
+  return loadIndex(IndexFilename, {}, false);
 }
 
-std::unique_ptr<clang::clangd::SymbolIndex> buildDex() {
-  return clang::clangd::loadIndex(IndexFilename, {}, true);
+std::unique_ptr<SymbolIndex> buildDex() {
+  return loadIndex(IndexFilename, {}, true);
 }
 
-// This function processes user-provided Log file with fuzzy find requests in
-// the following format:
-//
-// fuzzyFind("UnqualifiedName", scopes=["clang::", "clang::clangd::"])
-//
-// It constructs vector of FuzzyFindRequests which is later used for the
-// benchmarks.
-std::vector<clang::clangd::FuzzyFindRequest> extractQueriesFromLogs() {
-  llvm::Regex RequestMatcher("fuzzyFind\\(\"([a-zA-Z]*)\", scopes=\\[(.*)\\]");
-  llvm::SmallVector<llvm::StringRef, 200> Matches;
-  std::ifstream InputStream(LogFilename);
+// Reads JSON array of serialized FuzzyFindRequest's from user-provided file.
+std::vector<FuzzyFindRequest> extractQueriesFromLogs() {
+  std::ifstream InputStream(RequestsFilename);
   std::string Log((std::istreambuf_iterator<char>(InputStream)),
                   std::istreambuf_iterator<char>());
-  llvm::StringRef Temporary(Log);
-  llvm::SmallVector<llvm::StringRef, 200> Strings;
-  Temporary.split(Strings, '\n');
-
-  clang::clangd::FuzzyFindRequest R;
-  R.MaxCandidateCount = 100;
-
-  llvm::SmallVector<llvm::StringRef, 200> CommaSeparatedValues;
-
-  std::vector<clang::clangd::FuzzyFindRequest> RealRequests;
-  for (auto Line : Strings) {
-    if (RequestMatcher.match(Line, &Matches)) {
-      R.Query = Matches[1];
-      CommaSeparatedValues.clear();
-      Line.split(CommaSeparatedValues, ',');
-      R.Scopes.clear();
-      for (auto C : CommaSeparatedValues) {
-        R.Scopes.push_back(C);
-      }
-      RealRequests.push_back(R);
+
+  std::vector<FuzzyFindRequest> Requests;
+  auto JSONArray = llvm::json::parse(Log);
+
+  // Panic if the provided file couldn't be parsed.
+  if (!JSONArray) {
+    llvm::errs() << "Error when parsing JSON requests file: "
+                 << llvm::toString(JSONArray.takeError());
+    exit(1);
+  }
+  if (!JSONArray->getAsArray()) {
+    llvm::errs() << "Error: top-level value is not a JSON array: " << Log
+                 << '\n';
+    exit(1);
+  }
+
+  for (const auto &Item : *JSONArray->getAsArray()) {
+    FuzzyFindRequest Request;
+    // Panic if the provided file couldn't be parsed.
+    if (!fromJSON(Item, Request)) {
+      llvm::errs() << "Error when deserializing request: " << Item << '\n';
+      exit(1);
     }
+    Requests.push_back(Request);
   }
-  return RealRequests;
+  return Requests;
 }
 
 static void MemQueries(benchmark::State &State) {
@@ -100,12 +95,12 @@ BENCHMARK(DexQueries);
 int main(int argc, char *argv[]) {
   if (argc < 3) {
     llvm::errs() << "Usage: " << argv[0]
-                 << " global-symbol-index.yaml fuzzy-find-requests.log "
+                 << " global-symbol-index.yaml requests.json "
                     "BENCHMARK_OPTIONS...\n";
     return -1;
   }
   IndexFilename = argv[1];
-  LogFilename = argv[2];
+  RequestsFilename = argv[2];
   // Trim first two arguments of the benchmark invocation.
   argv += 3;
   argc -= 3;

Added: clang-tools-extra/trunk/test/clangd/Inputs/requests.json
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clangd/Inputs/requests.json?rev=342137&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clangd/Inputs/requests.json (added)
+++ clang-tools-extra/trunk/test/clangd/Inputs/requests.json Thu Sep 13 07:21:50 2018
@@ -0,0 +1,7 @@
+[{"MaxCandidateCount":100,"ProximityPaths":["/usr/home/user/clang-tools-extra/clangd/benchmarks/IndexBenchmark.cpp"],"Query":"OMP","RestrictForCodeCompletion":true,"Scopes":["clang::"]},
+{"MaxCandidateCount":100,"ProximityPaths":[],"Query":"s","RestrictForCodeCompletion":true,"Scopes":["llvm::", ""]},
+{"MaxCandidateCount":100,"ProximityPaths":[],"Query":"sy","RestrictForCodeCompletion":true,"Scopes":["llvm::", ""]},
+{"MaxCandidateCount":100,"ProximityPaths":[],"Query":"sys","RestrictForCodeCompletion":true,"Scopes":["llvm::", ""]},
+{"MaxCandidateCount":100,"ProximityPaths":[],"Query":"sys","RestrictForCodeCompletion":true,"Scopes":["llvm::", ""]},
+{"MaxCandidateCount":100,"ProximityPaths":[],"Query":"Dex","RestrictForCodeCompletion":true,"Scopes":["clang::clangd::", "clang::", "clang::clangd::dex::"]},
+{"MaxCandidateCount":100,"ProximityPaths":[],"Query":"Variable","RestrictForCodeCompletion":true,"Scopes":[""]}]

Removed: clang-tools-extra/trunk/test/clangd/Inputs/requests.log
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clangd/Inputs/requests.log?rev=342136&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clangd/Inputs/requests.log (original)
+++ clang-tools-extra/trunk/test/clangd/Inputs/requests.log (removed)
@@ -1,5 +0,0 @@
-V[09:08:34.301] Code complete: fuzzyFind("s", scopes=[llvm::])
-V[09:08:34.368] Code complete: fuzzyFind("sy", scopes=[llvm::])
-V[09:08:34.442] Code complete: fuzzyFind("sys", scopes=[llvm::])
-V[09:09:12.075] Code complete: fuzzyFind("Dex", scopes=[clang::clangd::, clang::, clang::clangd::dex::])
-V[09:09:12.075] Code complete: fuzzyFind("Variable", scopes=[])

Modified: clang-tools-extra/trunk/test/clangd/index-tools.test
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clangd/index-tools.test?rev=342137&r1=342136&r2=342137&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clangd/index-tools.test (original)
+++ clang-tools-extra/trunk/test/clangd/index-tools.test Thu Sep 13 07:21:50 2018
@@ -1,3 +1,5 @@
 # RUN: clangd-indexer %p/Inputs/BenchmarkSource.cpp -- -I%p/Inputs > %t.index
 # FIXME: By default, benchmarks are excluded from the list of default targets hence not built. Find a way to depend on benchmarks to run the next command.
-# RUN: if [ -f %clangd-benchmark-dir/IndexBenchmark ]; then %clangd-benchmark-dir/IndexBenchmark %t.index %p/Inputs/requests.log --benchmark_min_time=0.01 ; fi
+# RUN: if [ -f %clangd-benchmark-dir/IndexBenchmark ]; then %clangd-benchmark-dir/IndexBenchmark %t.index %p/Inputs/requests.json --benchmark_min_time=0.01 ; fi
+# Pass invalid JSON file and check that IndexBenchmark fails to parse it.
+# RUN: if [ -f %clangd-benchmark-dir/IndexBenchmark ]; then not %clangd-benchmark-dir/IndexBenchmark %t.index %t --benchmark_min_time=0.01 ; fi




More information about the cfe-commits mailing list