jvikstrom updated this revision to Diff 206375.
jvikstrom added a comment.

Made SemanticTokenCollector visible and removed getSemanticHighlights function.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D63559/new/

https://reviews.llvm.org/D63559

Files:
  clang-tools-extra/clangd/CMakeLists.txt
  clang-tools-extra/clangd/SemanticHighlight.cpp
  clang-tools-extra/clangd/SemanticHighlight.h
  clang-tools-extra/clangd/unittests/CMakeLists.txt
  clang-tools-extra/clangd/unittests/SemanticHighlightTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightTests.cpp
===================================================================
--- /dev/null
+++ clang-tools-extra/clangd/unittests/SemanticHighlightTests.cpp
@@ -0,0 +1,63 @@
+//===- SemanticHighlightTest.cpp - SemanticHighlightTest tests-*- C++ -* -===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "Annotations.h"
+#include "ClangdUnit.h"
+#include "Protocol.h"
+#include "SemanticHighlight.h"
+#include "SourceCode.h"
+#include "TestTU.h"
+#include "llvm/Support/ScopedPrinter.h"
+#include "gmock/gmock-matchers.h"
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+namespace clang {
+namespace clangd {
+namespace {
+
+void checkTokensExists(std::vector<SemanticToken> Tokens,
+                       std::vector<Range> ExpectedRanges,
+                       SemanticHighlightKind Kind) {
+  std::vector<Range> ActualRanges;
+  for (SemanticToken Token : Tokens) {
+    if (Token.Kind == Kind) {
+      ActualRanges.push_back(Token.R);
+    }
+  }
+
+  EXPECT_THAT(ActualRanges, testing::UnorderedElementsAreArray(ExpectedRanges));
+}
+
+TEST(SemanticTokenCollector, GetBeginningOfIdentifier) {
+  std::string Preamble = R"cpp(
+    void $Function[[foo]](int);
+    struct A {
+      double SomeMember;
+    };
+    void $Function[[foo]](int $Variable[[a]]) {
+      SOMEDECL( );
+      auto $Variable[[VeryLongVariableName]] = 12312;
+      A     $Variable[[aa]];
+    }
+  )cpp";
+
+  Annotations Test(Preamble);
+  auto Variables = Test.ranges("Variable");
+  auto Function = Test.ranges("Function");
+
+  auto AST = TestTU::withCode(Test.code()).build();
+  auto Tokens = SemanticTokenCollector(AST).collectTokens();
+
+  checkTokensExists(Tokens, Variables, SemanticHighlightKind::Variable);
+  checkTokensExists(Tokens, Function, SemanticHighlightKind::Function);
+}
+
+} // namespace
+} // namespace clangd
+} // namespace clang
Index: clang-tools-extra/clangd/unittests/CMakeLists.txt
===================================================================
--- clang-tools-extra/clangd/unittests/CMakeLists.txt
+++ clang-tools-extra/clangd/unittests/CMakeLists.txt
@@ -53,6 +53,7 @@
   RenameTests.cpp
   RIFFTests.cpp
   SelectionTests.cpp
+  SemanticHighlightTests.cpp
   SerializationTests.cpp
   SourceCodeTests.cpp
   SymbolCollectorTests.cpp
Index: clang-tools-extra/clangd/SemanticHighlight.h
===================================================================
--- /dev/null
+++ clang-tools-extra/clangd/SemanticHighlight.h
@@ -0,0 +1,54 @@
+//==-- SemanticHighlight.h - Generating highlights from the AST--*- C++ -*-==//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICHIGHLIGHT_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICHIGHLIGHT_H
+
+#include "ClangdUnit.h"
+#include "Protocol.h"
+#include "clang/AST/RecursiveASTVisitor.h"
+
+namespace clang {
+namespace clangd {
+
+enum class SemanticHighlightKind : int {
+  Variable = 0,
+  Function = 1,
+};
+
+// Contains all information needed for the highlighting a token.
+struct SemanticToken {
+  SemanticHighlightKind Kind;
+  Range R;
+};
+
+// Collects all semantic tokens in an ASTContext.
+class SemanticTokenCollector
+    : private RecursiveASTVisitor<SemanticTokenCollector> {
+  friend class RecursiveASTVisitor<SemanticTokenCollector>;
+  std::vector<SemanticToken> Tokens;
+  ASTContext &Ctx;
+  const SourceManager &SM;
+
+public:
+  SemanticTokenCollector(ParsedAST &AST);
+  std::vector<SemanticToken> collectTokens();
+
+private:
+  bool VisitVarDecl(VarDecl *Var);
+  bool VisitFunctionDecl(FunctionDecl *Func);
+
+  void addSymbol(NamedDecl *D, SemanticHighlightKind Kind);
+};
+
+bool operator==(const SemanticToken &Lhs, const SemanticToken &Rhs);
+bool operator!=(const SemanticToken &Lhs, const SemanticToken &Rhs);
+
+} // namespace clangd
+} // namespace clang
+
+#endif
Index: clang-tools-extra/clangd/SemanticHighlight.cpp
===================================================================
--- /dev/null
+++ clang-tools-extra/clangd/SemanticHighlight.cpp
@@ -0,0 +1,63 @@
+#include "SemanticHighlight.h"
+#include "SourceCode.h"
+#include "clang/Lex/Lexer.h"
+
+namespace clang {
+namespace clangd {
+
+SemanticTokenCollector::SemanticTokenCollector(ParsedAST &AST)
+    : Ctx(AST.getASTContext()), SM(AST.getSourceManager()) {
+  Ctx.setTraversalScope(AST.getLocalTopLevelDecls());
+}
+
+bool SemanticTokenCollector::VisitVarDecl(VarDecl *Var) {
+  addSymbol(Var, SemanticHighlightKind::Variable);
+  return true;
+}
+
+bool SemanticTokenCollector::VisitFunctionDecl(FunctionDecl *Func) {
+  addSymbol(Func, SemanticHighlightKind::Function);
+  return true;
+}
+
+std::vector<SemanticToken> SemanticTokenCollector::collectTokens() {
+  TraverseAST(Ctx);
+  return Tokens;
+}
+
+void SemanticTokenCollector::addSymbol(NamedDecl *D,
+                                           SemanticHighlightKind Kind) {
+  if (D->getName().size() == 0) {
+    // Don't add symbols that don't have any length.
+    return;
+  }
+
+  unsigned int Len =
+      clang::Lexer::MeasureTokenLength(D->getLocation(), SM, Ctx.getLangOpts());
+
+  if (D->getLocation().isMacroID()) {
+    // FIXME; This is inside a macro. For now skip the token
+    return;
+  }
+
+  Position LSPLoc = sourceLocToPosition(SM, D->getLocation());
+
+  SemanticToken S;
+  S.R.start.character = LSPLoc.character;
+  S.R.start.line = LSPLoc.line;
+  S.R.end.character = LSPLoc.character + Len;
+  S.R.end.line = LSPLoc.line;
+  S.Kind = Kind;
+
+  Tokens.push_back(S);
+}
+
+bool operator==(const SemanticToken &Lhs, const SemanticToken &Rhs) {
+  return Lhs.Kind == Rhs.Kind && Lhs.R == Rhs.R;
+}
+bool operator!=(const SemanticToken &Lhs, const SemanticToken &Rhs) {
+  return !(Lhs == Rhs);
+}
+
+} // namespace clangd
+} // namespace clang
Index: clang-tools-extra/clangd/CMakeLists.txt
===================================================================
--- clang-tools-extra/clangd/CMakeLists.txt
+++ clang-tools-extra/clangd/CMakeLists.txt
@@ -62,6 +62,7 @@
   Quality.cpp
   RIFF.cpp
   Selection.cpp
+  SemanticHighlight.cpp
   SourceCode.cpp
   Threading.cpp
   Trace.cpp
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to