[PATCH] D63253: [clang-tidy] Made abseil-faster-strsplit-delimiter tests pass on C++17

2019-06-13 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom created this revision.
jvikstrom added a reviewer: hokein.
Herald added a subscriber: xazax.hun.
Herald added a project: clang.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D63253

Files:
  clang-tools-extra/clang-tidy/abseil/FasterStrsplitDelimiterCheck.cpp
  clang-tools-extra/test/clang-tidy/abseil-faster-strsplit-delimiter.cpp


Index: clang-tools-extra/test/clang-tidy/abseil-faster-strsplit-delimiter.cpp
===
--- clang-tools-extra/test/clang-tidy/abseil-faster-strsplit-delimiter.cpp
+++ clang-tools-extra/test/clang-tidy/abseil-faster-strsplit-delimiter.cpp
@@ -1,4 +1,4 @@
-// RUN: %check_clang_tidy -std=c++11,c++14 %s abseil-faster-strsplit-delimiter 
%t
+// RUN: %check_clang_tidy -std=c++11-or-later %s 
abseil-faster-strsplit-delimiter %t
 // FIXME: Fix the checker to work in C++17 mode.
 
 namespace absl {
Index: clang-tools-extra/clang-tidy/abseil/FasterStrsplitDelimiterCheck.cpp
===
--- clang-tools-extra/clang-tidy/abseil/FasterStrsplitDelimiterCheck.cpp
+++ clang-tools-extra/clang-tidy/abseil/FasterStrsplitDelimiterCheck.cpp
@@ -20,21 +20,6 @@
 
 AST_MATCHER(StringLiteral, lengthIsOne) { return Node.getLength() == 1; }
 
-::internal::Matcher
-constructExprWithArg(llvm::StringRef ClassName,
- const ::internal::Matcher ) {
-  auto ConstrExpr = cxxConstructExpr(hasType(recordDecl(hasName(ClassName))),
- hasArgument(0, ignoringParenCasts(Arg)));
-
-  return anyOf(ConstrExpr, cxxBindTemporaryExpr(has(ConstrExpr)));
-}
-
-::internal::Matcher
-copyConstructExprWithArg(llvm::StringRef ClassName,
- const ::internal::Matcher ) {
-  return constructExprWithArg(ClassName, constructExprWithArg(ClassName, Arg));
-}
-
 llvm::Optional makeCharacterLiteral(const StringLiteral *Literal) 
{
   std::string Result;
   {
@@ -74,11 +59,17 @@
 
   // Binds to a string_view (either absl or std) that was passed by value and
   // contructed from string literal.
-  auto StringViewArg =
-  copyConstructExprWithArg("::absl::string_view", SingleChar);
+  auto StringViewArg = ignoringElidableConstructorCall(ignoringImpCasts(
+  cxxConstructExpr(hasType(recordDecl(hasName("::absl::string_view"))),
+   hasArgument(0, ignoringParenImpCasts(SingleChar);
 
+  // Need to ignore the elidable constructor as otherwise there is no match for
+  // c++14 and earlier.
   auto ByAnyCharArg =
-  expr(copyConstructExprWithArg("::absl::ByAnyChar", StringViewArg))
+  expr(has(ignoringElidableConstructorCall(
+   ignoringParenCasts(cxxBindTemporaryExpr(has(cxxConstructExpr(
+   hasType(recordDecl(hasName("::absl::ByAnyChar"))),
+   hasArgument(0, StringViewArg
   .bind("ByAnyChar");
 
   // Find uses of absl::StrSplit(..., "x") and absl::StrSplit(...,


Index: clang-tools-extra/test/clang-tidy/abseil-faster-strsplit-delimiter.cpp
===
--- clang-tools-extra/test/clang-tidy/abseil-faster-strsplit-delimiter.cpp
+++ clang-tools-extra/test/clang-tidy/abseil-faster-strsplit-delimiter.cpp
@@ -1,4 +1,4 @@
-// RUN: %check_clang_tidy -std=c++11,c++14 %s abseil-faster-strsplit-delimiter %t
+// RUN: %check_clang_tidy -std=c++11-or-later %s abseil-faster-strsplit-delimiter %t
 // FIXME: Fix the checker to work in C++17 mode.
 
 namespace absl {
Index: clang-tools-extra/clang-tidy/abseil/FasterStrsplitDelimiterCheck.cpp
===
--- clang-tools-extra/clang-tidy/abseil/FasterStrsplitDelimiterCheck.cpp
+++ clang-tools-extra/clang-tidy/abseil/FasterStrsplitDelimiterCheck.cpp
@@ -20,21 +20,6 @@
 
 AST_MATCHER(StringLiteral, lengthIsOne) { return Node.getLength() == 1; }
 
-::internal::Matcher
-constructExprWithArg(llvm::StringRef ClassName,
- const ::internal::Matcher ) {
-  auto ConstrExpr = cxxConstructExpr(hasType(recordDecl(hasName(ClassName))),
- hasArgument(0, ignoringParenCasts(Arg)));
-
-  return anyOf(ConstrExpr, cxxBindTemporaryExpr(has(ConstrExpr)));
-}
-
-::internal::Matcher
-copyConstructExprWithArg(llvm::StringRef ClassName,
- const ::internal::Matcher ) {
-  return constructExprWithArg(ClassName, constructExprWithArg(ClassName, Arg));
-}
-
 llvm::Optional makeCharacterLiteral(const StringLiteral *Literal) {
   std::string Result;
   {
@@ -74,11 +59,17 @@
 
   // Binds to a string_view (either absl or std) that was passed by value and
   // contructed from string literal.
-  auto StringViewArg =
-  copyConstructExprWithArg("::absl::string_view", SingleChar);
+  auto StringViewArg = ignoringElidableConstructorCall(ignoringImpCasts(
+  cxxConstructExpr(hasType(recordDecl(hasName("::absl::string_view"))),
+  

[PATCH] D63149: Added AST matcher for ignoring elidable constructors

2019-06-13 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 204447.
jvikstrom marked 3 inline comments as done.
jvikstrom added a comment.

- Removed unnecessary return


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63149

Files:
  clang/docs/LibASTMatchersReference.html
  clang/include/clang/ASTMatchers/ASTMatchers.h
  clang/lib/ASTMatchers/Dynamic/Registry.cpp
  clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
  clang/unittests/ASTMatchers/ASTMatchersTest.h

Index: clang/unittests/ASTMatchers/ASTMatchersTest.h
===
--- clang/unittests/ASTMatchers/ASTMatchersTest.h
+++ clang/unittests/ASTMatchers/ASTMatchersTest.h
@@ -57,6 +57,17 @@
   const std::unique_ptr FindResultReviewer;
 };
 
+enum class LanguageMode {
+  Cxx11,
+  Cxx14,
+  Cxx17,
+  Cxx2a,
+  Cxx11OrLater,
+  Cxx14OrLater,
+  Cxx17OrLater,
+  Cxx2aOrLater
+};
+
 template 
 testing::AssertionResult matchesConditionally(
 const std::string , const T , bool ExpectMatch,
@@ -116,14 +127,71 @@
 }
 
 template 
-testing::AssertionResult matches(const std::string , const T ) {
-  return matchesConditionally(Code, AMatcher, true, "-std=c++11");
+testing::AssertionResult
+matchesConditionally(const std::string , const T ,
+ bool ExpectMatch, const LanguageMode ) {
+  std::vector LangModes;
+  switch (Mode) {
+  case LanguageMode::Cxx11:
+  case LanguageMode::Cxx14:
+  case LanguageMode::Cxx17:
+  case LanguageMode::Cxx2a:
+LangModes = {Mode};
+break;
+  case LanguageMode::Cxx11OrLater:
+LangModes = {LanguageMode::Cxx11, LanguageMode::Cxx14, LanguageMode::Cxx17,
+ LanguageMode::Cxx2a};
+break;
+  case LanguageMode::Cxx14OrLater:
+LangModes = {LanguageMode::Cxx14, LanguageMode::Cxx17, LanguageMode::Cxx2a};
+break;
+  case LanguageMode::Cxx17OrLater:
+LangModes = {LanguageMode::Cxx17, LanguageMode::Cxx2a};
+break;
+  case LanguageMode::Cxx2aOrLater:
+LangModes = {LanguageMode::Cxx2a};
+  }
+
+  for (auto Mode : LangModes) {
+std::string LangModeArg;
+switch (Mode) {
+case LanguageMode::Cxx11:
+  LangModeArg = "-std=c++11";
+  break;
+case LanguageMode::Cxx14:
+  LangModeArg = "-std=c++14";
+  break;
+case LanguageMode::Cxx17:
+  LangModeArg = "-std=c++17";
+  break;
+case LanguageMode::Cxx2a:
+  LangModeArg = "-std=c++2a";
+  break;
+default:
+  llvm_unreachable("Invalid language mode");
+}
+
+auto Result =
+matchesConditionally(Code, AMatcher, ExpectMatch, LangModeArg);
+if (!Result)
+  return Result;
+  }
+
+  return testing::AssertionSuccess();
+}
+
+template 
+testing::AssertionResult
+matches(const std::string , const T ,
+const LanguageMode  = LanguageMode::Cxx11) {
+  return matchesConditionally(Code, AMatcher, true, Modes);
 }
 
 template 
-testing::AssertionResult notMatches(const std::string ,
-const T ) {
-  return matchesConditionally(Code, AMatcher, false, "-std=c++11");
+testing::AssertionResult
+notMatches(const std::string , const T ,
+   const LanguageMode  = LanguageMode::Cxx11) {
+  return matchesConditionally(Code, AMatcher, false, Modes);
 }
 
 template 
Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
===
--- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -566,6 +566,74 @@
llvm::make_unique>("x")));
 }
 
+TEST(Matcher, IgnoresElidableConstructors) {
+  EXPECT_TRUE(
+  matches("struct H {};"
+  "template H B(T A);"
+  "void f() {"
+  "  H D1;"
+  "  D1 = B(B(1));"
+  "}",
+  cxxOperatorCallExpr(hasArgument(
+  1, callExpr(hasArgument(
+ 0, ignoringElidableConstructorCall(callExpr()),
+  LanguageMode::Cxx11OrLater));
+  EXPECT_TRUE(
+  matches("struct H {};"
+  "template H B(T A);"
+  "void f() {"
+  "  H D1;"
+  "  D1 = B(1);"
+  "}",
+  cxxOperatorCallExpr(hasArgument(
+  1, callExpr(hasArgument(0, ignoringElidableConstructorCall(
+ integerLiteral()),
+  LanguageMode::Cxx11OrLater));
+  EXPECT_TRUE(matches(
+  "struct H {};"
+  "H G();"
+  "void f() {"
+  "  H D = G();"
+  "}",
+  varDecl(hasInitializer(anyOf(
+  ignoringElidableConstructorCall(callExpr()),
+  exprWithCleanups(has(ignoringElidableConstructorCall(callExpr())),
+  LanguageMode::Cxx11OrLater));
+}
+
+TEST(Matcher, IgnoresElidableInReturn) {
+  auto matcher = 

[PATCH] D63149: Added AST matcher for ignoring elidable constructors

2019-06-13 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom marked an inline comment as done.
jvikstrom added inline comments.



Comment at: clang/unittests/ASTMatchers/ASTMatchersTest.h:153
+  case LanguageMode::Cxx2aOrLater:
+LangModes = {LanguageMode::Cxx2a};
+  }

hokein wrote:
> nit: add a llvm_unreachable for `default`.
But the switch covers all enum values so that would give warnings


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63149



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D63149: Added AST matcher for ignoring elidable constructors

2019-06-13 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 204443.
jvikstrom marked an inline comment as done.
jvikstrom added a comment.

- Removed dependency on unordered map


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63149

Files:
  clang/docs/LibASTMatchersReference.html
  clang/include/clang/ASTMatchers/ASTMatchers.h
  clang/lib/ASTMatchers/Dynamic/Registry.cpp
  clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
  clang/unittests/ASTMatchers/ASTMatchersTest.h

Index: clang/unittests/ASTMatchers/ASTMatchersTest.h
===
--- clang/unittests/ASTMatchers/ASTMatchersTest.h
+++ clang/unittests/ASTMatchers/ASTMatchersTest.h
@@ -57,6 +57,17 @@
   const std::unique_ptr FindResultReviewer;
 };
 
+enum class LanguageMode {
+  Cxx11,
+  Cxx14,
+  Cxx17,
+  Cxx2a,
+  Cxx11OrLater,
+  Cxx14OrLater,
+  Cxx17OrLater,
+  Cxx2aOrLater
+};
+
 template 
 testing::AssertionResult matchesConditionally(
 const std::string , const T , bool ExpectMatch,
@@ -116,14 +127,72 @@
 }
 
 template 
-testing::AssertionResult matches(const std::string , const T ) {
-  return matchesConditionally(Code, AMatcher, true, "-std=c++11");
+testing::AssertionResult
+matchesConditionally(const std::string , const T ,
+ bool ExpectMatch, const LanguageMode ) {
+  std::vector LangModes;
+  switch (Mode) {
+  case LanguageMode::Cxx11:
+  case LanguageMode::Cxx14:
+  case LanguageMode::Cxx17:
+  case LanguageMode::Cxx2a:
+LangModes = {Mode};
+break;
+  case LanguageMode::Cxx11OrLater:
+LangModes = {LanguageMode::Cxx11, LanguageMode::Cxx14, LanguageMode::Cxx17,
+ LanguageMode::Cxx2a};
+break;
+  case LanguageMode::Cxx14OrLater:
+LangModes = {LanguageMode::Cxx14, LanguageMode::Cxx17, LanguageMode::Cxx2a};
+break;
+  case LanguageMode::Cxx17OrLater:
+LangModes = {LanguageMode::Cxx17, LanguageMode::Cxx2a};
+break;
+  case LanguageMode::Cxx2aOrLater:
+LangModes = {LanguageMode::Cxx2a};
+  }
+
+  for (auto Mode : LangModes) {
+std::string LangModeArg;
+switch (Mode) {
+case LanguageMode::Cxx11:
+  LangModeArg = "-std=c++11";
+  break;
+case LanguageMode::Cxx14:
+  LangModeArg = "-std=c++14";
+  break;
+case LanguageMode::Cxx17:
+  LangModeArg = "-std=c++17";
+  break;
+case LanguageMode::Cxx2a:
+  LangModeArg = "-std=c++2a";
+  break;
+default:
+  llvm_unreachable("Invalid language mode");
+}
+
+auto Result =
+matchesConditionally(Code, AMatcher, ExpectMatch, LangModeArg);
+if (!Result) {
+  return Result;
+}
+  }
+
+  return testing::AssertionSuccess();
 }
 
 template 
-testing::AssertionResult notMatches(const std::string ,
-const T ) {
-  return matchesConditionally(Code, AMatcher, false, "-std=c++11");
+testing::AssertionResult
+matches(const std::string , const T ,
+const LanguageMode  = LanguageMode::Cxx11) {
+  return matchesConditionally(Code, AMatcher, true, Modes);
+}
+
+template 
+testing::AssertionResult
+notMatches(const std::string , const T ,
+   const LanguageMode  = LanguageMode::Cxx11) {
+  return matchesConditionally(Code, AMatcher, false, Modes);
 }
 
 template 
Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
===
--- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -566,6 +566,74 @@
llvm::make_unique>("x")));
 }
 
+TEST(Matcher, IgnoresElidableConstructors) {
+  EXPECT_TRUE(
+  matches("struct H {};"
+  "template H B(T A);"
+  "void f() {"
+  "  H D1;"
+  "  D1 = B(B(1));"
+  "}",
+  cxxOperatorCallExpr(hasArgument(
+  1, callExpr(hasArgument(
+ 0, ignoringElidableConstructorCall(callExpr()),
+  LanguageMode::Cxx11OrLater));
+  EXPECT_TRUE(
+  matches("struct H {};"
+  "template H B(T A);"
+  "void f() {"
+  "  H D1;"
+  "  D1 = B(1);"
+  "}",
+  cxxOperatorCallExpr(hasArgument(
+  1, callExpr(hasArgument(0, ignoringElidableConstructorCall(
+ integerLiteral()),
+  LanguageMode::Cxx11OrLater));
+  EXPECT_TRUE(matches(
+  "struct H {};"
+  "H G();"
+  "void f() {"
+  "  H D = G();"
+  "}",
+  varDecl(hasInitializer(anyOf(
+  ignoringElidableConstructorCall(callExpr()),
+  exprWithCleanups(has(ignoringElidableConstructorCall(callExpr())),
+  LanguageMode::Cxx11OrLater));
+}
+
+TEST(Matcher, IgnoresElidableInReturn) {
+  auto matcher = 

[PATCH] D63149: Added AST matcher for ignoring elidable constructors

2019-06-13 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 204442.
jvikstrom added a comment.

- Updated outdated docs


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63149

Files:
  clang/docs/LibASTMatchersReference.html
  clang/include/clang/ASTMatchers/ASTMatchers.h
  clang/lib/ASTMatchers/Dynamic/Registry.cpp
  clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
  clang/unittests/ASTMatchers/ASTMatchersTest.h

Index: clang/unittests/ASTMatchers/ASTMatchersTest.h
===
--- clang/unittests/ASTMatchers/ASTMatchersTest.h
+++ clang/unittests/ASTMatchers/ASTMatchersTest.h
@@ -13,6 +13,7 @@
 #include "clang/Frontend/ASTUnit.h"
 #include "clang/Tooling/Tooling.h"
 #include "gtest/gtest.h"
+#include 
 
 namespace clang {
 namespace ast_matchers {
@@ -57,6 +58,17 @@
   const std::unique_ptr FindResultReviewer;
 };
 
+enum class LanguageMode {
+  Cxx11,
+  Cxx14,
+  Cxx17,
+  Cxx2a,
+  Cxx11OrLater,
+  Cxx14OrLater,
+  Cxx17OrLater,
+  Cxx2aOrLater
+};
+
 template 
 testing::AssertionResult matchesConditionally(
 const std::string , const T , bool ExpectMatch,
@@ -116,14 +128,72 @@
 }
 
 template 
-testing::AssertionResult matches(const std::string , const T ) {
-  return matchesConditionally(Code, AMatcher, true, "-std=c++11");
+testing::AssertionResult
+matchesConditionally(const std::string , const T ,
+ bool ExpectMatch, const LanguageMode ) {
+  std::vector LangModes;
+  switch (Mode) {
+  case LanguageMode::Cxx11:
+  case LanguageMode::Cxx14:
+  case LanguageMode::Cxx17:
+  case LanguageMode::Cxx2a:
+LangModes = {Mode};
+break;
+  case LanguageMode::Cxx11OrLater:
+LangModes = {LanguageMode::Cxx11, LanguageMode::Cxx14, LanguageMode::Cxx17,
+ LanguageMode::Cxx2a};
+break;
+  case LanguageMode::Cxx14OrLater:
+LangModes = {LanguageMode::Cxx14, LanguageMode::Cxx17, LanguageMode::Cxx2a};
+break;
+  case LanguageMode::Cxx17OrLater:
+LangModes = {LanguageMode::Cxx17, LanguageMode::Cxx2a};
+break;
+  case LanguageMode::Cxx2aOrLater:
+LangModes = {LanguageMode::Cxx2a};
+  }
+
+  for (auto Mode : LangModes) {
+std::string LangModeArg;
+switch (Mode) {
+case LanguageMode::Cxx11:
+  LangModeArg = "-std=c++11";
+  break;
+case LanguageMode::Cxx14:
+  LangModeArg = "-std=c++14";
+  break;
+case LanguageMode::Cxx17:
+  LangModeArg = "-std=c++17";
+  break;
+case LanguageMode::Cxx2a:
+  LangModeArg = "-std=c++2a";
+  break;
+default:
+  llvm_unreachable("Invalid language mode");
+}
+
+auto Result =
+matchesConditionally(Code, AMatcher, ExpectMatch, LangModeArg);
+if (!Result) {
+  return Result;
+}
+  }
+
+  return testing::AssertionSuccess();
 }
 
 template 
-testing::AssertionResult notMatches(const std::string ,
-const T ) {
-  return matchesConditionally(Code, AMatcher, false, "-std=c++11");
+testing::AssertionResult
+matches(const std::string , const T ,
+const LanguageMode  = LanguageMode::Cxx11) {
+  return matchesConditionally(Code, AMatcher, true, Modes);
+}
+
+template 
+testing::AssertionResult
+notMatches(const std::string , const T ,
+   const LanguageMode  = LanguageMode::Cxx11) {
+  return matchesConditionally(Code, AMatcher, false, Modes);
 }
 
 template 
Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
===
--- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -566,6 +566,74 @@
llvm::make_unique>("x")));
 }
 
+TEST(Matcher, IgnoresElidableConstructors) {
+  EXPECT_TRUE(
+  matches("struct H {};"
+  "template H B(T A);"
+  "void f() {"
+  "  H D1;"
+  "  D1 = B(B(1));"
+  "}",
+  cxxOperatorCallExpr(hasArgument(
+  1, callExpr(hasArgument(
+ 0, ignoringElidableConstructorCall(callExpr()),
+  LanguageMode::Cxx11OrLater));
+  EXPECT_TRUE(
+  matches("struct H {};"
+  "template H B(T A);"
+  "void f() {"
+  "  H D1;"
+  "  D1 = B(1);"
+  "}",
+  cxxOperatorCallExpr(hasArgument(
+  1, callExpr(hasArgument(0, ignoringElidableConstructorCall(
+ integerLiteral()),
+  LanguageMode::Cxx11OrLater));
+  EXPECT_TRUE(matches(
+  "struct H {};"
+  "H G();"
+  "void f() {"
+  "  H D = G();"
+  "}",
+  varDecl(hasInitializer(anyOf(
+  ignoringElidableConstructorCall(callExpr()),
+  exprWithCleanups(has(ignoringElidableConstructorCall(callExpr())),
+  

[PATCH] D63149: Added AST matcher for ignoring elidable constructors

2019-06-12 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 204229.
jvikstrom added a comment.

- Made ignoreElidable also ignore materializeTemporaryExpr and reformatted code


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63149

Files:
  clang/docs/LibASTMatchersReference.html
  clang/include/clang/ASTMatchers/ASTMatchers.h
  clang/lib/ASTMatchers/Dynamic/Registry.cpp
  clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
===
--- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -566,6 +566,71 @@
llvm::make_unique>("x")));
 }
 
+TEST(Matcher, IgnoresElidableConstructors) {
+  StatementMatcher matcher1 = cxxOperatorCallExpr(hasArgument(
+  1, callExpr(hasArgument(
+ 0, ignoringElidableMoveConstructorCall(callExpr().bind("c"));
+  StatementMatcher matcher2 = cxxOperatorCallExpr(hasArgument(
+  1, callExpr(hasArgument(0, ignoringElidableMoveConstructorCall(
+ integerLiteral().bind("c"));
+
+  auto code1 = "struct H {};"
+   "template H B(T A);"
+   "void f(){"
+   "H D1;"
+   "D1=B(B(1));"
+   "}";
+  auto code2 = "struct H {};"
+   "template H B(T A);"
+   "void f(){"
+   "H D1;"
+   "D1=B(1);"
+   "}";
+
+  EXPECT_TRUE(matchesConditionally(code1, matcher1, true, "-std=c++14"));
+  EXPECT_TRUE(matchesConditionally(code1, matcher1, true, "-std=c++17"));
+
+  EXPECT_TRUE(matchesConditionally(code2, matcher2, true, "-std=c++14"));
+  EXPECT_TRUE(matchesConditionally(code2, matcher2, true, "-std=c++17"));
+}
+
+TEST(Matcher, IgnoresElidableInReturn) {
+  auto matcher = expr(ignoringElidableMoveConstructorCall(declRefExpr()));
+  auto code1 = "struct H{};"
+   "H f(){"
+   "H g;"
+   "return g;"
+   "}";
+  auto code2 = "struct H{};"
+   "H f(){"
+   "return H();"
+   "}";
+  EXPECT_TRUE(matchesConditionally(code1, matcher, true, "-std=c++14"));
+  EXPECT_TRUE(matchesConditionally(code1, matcher, true, "-std=c++17"));
+  EXPECT_TRUE(matchesConditionally(code2, matcher, false, "-std=c++14"));
+  EXPECT_TRUE(matchesConditionally(code2, matcher, false, "-std=c++17"));
+}
+
+TEST(Matcher, IgnoreElidableConstructorDoesNotMatchConstructors) {
+  auto matcher = varDecl(
+  hasInitializer(ignoringElidableMoveConstructorCall(cxxConstructExpr(;
+  auto code = "struct H {};"
+  "void f(){"
+  "H D;"
+  "}";
+  EXPECT_TRUE(matchesConditionally(code, matcher, true, "-std=c++14"));
+  EXPECT_TRUE(matchesConditionally(code, matcher, true, "-std=c++17"));
+};
+
+TEST(Matcher, IgnoresElidableDoesNotPreventMatches) {
+  auto matcher = expr(ignoringElidableMoveConstructorCall(integerLiteral()));
+  auto code = "void f(){"
+  "int D = 10;"
+  "}";
+  EXPECT_TRUE(matchesConditionally(code, matcher, true, "-std=c++14"));
+  EXPECT_TRUE(matchesConditionally(code, matcher, true, "-std=c++17"));
+}
+
 TEST(Matcher, BindTheSameNameInAlternatives) {
   StatementMatcher matcher = anyOf(
 binaryOperator(hasOperatorName("+"),
Index: clang/lib/ASTMatchers/Dynamic/Registry.cpp
===
--- clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -320,6 +320,7 @@
   REGISTER_MATCHER(hasUnqualifiedDesugaredType);
   REGISTER_MATCHER(hasValueType);
   REGISTER_MATCHER(ifStmt);
+  REGISTER_MATCHER(ignoringElidableMoveConstructorCall);
   REGISTER_MATCHER(ignoringImpCasts);
   REGISTER_MATCHER(ignoringImplicit);
   REGISTER_MATCHER(ignoringParenCasts);
Index: clang/include/clang/ASTMatchers/ASTMatchers.h
===
--- clang/include/clang/ASTMatchers/ASTMatchers.h
+++ clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -6452,6 +6452,43 @@
   return false;
 }
 
+/// Matches expressions that match InnerMatcher after any elidable constructor
+/// are stripped off. In C++17 copy elidable constructors are no longer being
+/// generated in the AST as it is not permitted by the standard. They are
+/// however part of the C++14 AST. This means that there are cases where it is
+/// needed to use ``anyOf(cxxConstructorExpr(Inner), Inner)`` to match for both
+/// the C++14 and C++17 AST. Instead of doing this, this matcher can be used as
+/// ``ignoringElidableMoveConstructorCall(Inner)``.
+///
+/// Given
+///
+/// \code
+/// struct H {};
+/// template H B(T A);
+/// void f() {
+///   H D1;
+///   D1 = B(B(1));
+/// }
+/// \endcode
+///
+/// 

[PATCH] D63149: Added AST matcher for ignoring elidable constructors

2019-06-12 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 204244.
jvikstrom marked an inline comment as done.
jvikstrom added a comment.

- Updated example and fixed edge case in ignoringElidableConstructorCall


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63149

Files:
  clang/docs/LibASTMatchersReference.html
  clang/include/clang/ASTMatchers/ASTMatchers.h
  clang/lib/ASTMatchers/Dynamic/Registry.cpp
  clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
===
--- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -566,6 +566,81 @@
llvm::make_unique>("x")));
 }
 
+TEST(Matcher, IgnoresElidableConstructors) {
+  auto matcher1 = cxxOperatorCallExpr(hasArgument(
+  1, callExpr(hasArgument(
+ 0, ignoringElidableMoveConstructorCall(callExpr().bind("c"));
+  auto matcher2 = cxxOperatorCallExpr(hasArgument(
+  1, callExpr(hasArgument(0, ignoringElidableMoveConstructorCall(
+ integerLiteral().bind("c"));
+  auto matcher3 =
+  varDecl(hasInitializer(ignoringElidableMoveConstructorCall(callExpr(;
+
+  auto code1 = "struct H {};"
+   "template H B(T A);"
+   "void f(){"
+   "H D1;"
+   "D1=B(B(1));"
+   "}";
+  auto code2 = "struct H {};"
+   "template H B(T A);"
+   "void f(){"
+   "H D1;"
+   "D1=B(1);"
+   "}";
+  auto code3 = "struct H {};"
+   "H G();"
+   "void f(){"
+   "H D = G();"
+   "}";
+
+  EXPECT_TRUE(matchesConditionally(code1, matcher1, true, "-std=c++14"));
+  EXPECT_TRUE(matchesConditionally(code1, matcher1, true, "-std=c++17"));
+
+  EXPECT_TRUE(matchesConditionally(code2, matcher2, true, "-std=c++14"));
+  EXPECT_TRUE(matchesConditionally(code2, matcher2, true, "-std=c++17"));
+
+  EXPECT_TRUE(matchesConditionally(code3, matcher3, true, "-std=c++14"));
+  EXPECT_TRUE(matchesConditionally(code3, matcher3, true, "-std=c++17"));
+}
+
+TEST(Matcher, IgnoresElidableInReturn) {
+  auto matcher = expr(ignoringElidableMoveConstructorCall(declRefExpr()));
+  auto code1 = "struct H{};"
+   "H f(){"
+   "H g;"
+   "return g;"
+   "}";
+  auto code2 = "struct H{};"
+   "H f(){"
+   "return H();"
+   "}";
+  EXPECT_TRUE(matchesConditionally(code1, matcher, true, "-std=c++14"));
+  EXPECT_TRUE(matchesConditionally(code1, matcher, true, "-std=c++17"));
+  EXPECT_TRUE(matchesConditionally(code2, matcher, false, "-std=c++14"));
+  EXPECT_TRUE(matchesConditionally(code2, matcher, false, "-std=c++17"));
+}
+
+TEST(Matcher, IgnoreElidableConstructorDoesNotMatchConstructors) {
+  auto matcher = varDecl(
+  hasInitializer(ignoringElidableMoveConstructorCall(cxxConstructExpr(;
+  auto code = "struct H {};"
+  "void f(){"
+  "H D;"
+  "}";
+  EXPECT_TRUE(matchesConditionally(code, matcher, true, "-std=c++14"));
+  EXPECT_TRUE(matchesConditionally(code, matcher, true, "-std=c++17"));
+};
+
+TEST(Matcher, IgnoresElidableDoesNotPreventMatches) {
+  auto matcher = expr(ignoringElidableMoveConstructorCall(integerLiteral()));
+  auto code = "void f(){"
+  "int D = 10;"
+  "}";
+  EXPECT_TRUE(matchesConditionally(code, matcher, true, "-std=c++14"));
+  EXPECT_TRUE(matchesConditionally(code, matcher, true, "-std=c++17"));
+}
+
 TEST(Matcher, BindTheSameNameInAlternatives) {
   StatementMatcher matcher = anyOf(
 binaryOperator(hasOperatorName("+"),
Index: clang/lib/ASTMatchers/Dynamic/Registry.cpp
===
--- clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -320,6 +320,7 @@
   REGISTER_MATCHER(hasUnqualifiedDesugaredType);
   REGISTER_MATCHER(hasValueType);
   REGISTER_MATCHER(ifStmt);
+  REGISTER_MATCHER(ignoringElidableMoveConstructorCall);
   REGISTER_MATCHER(ignoringImpCasts);
   REGISTER_MATCHER(ignoringImplicit);
   REGISTER_MATCHER(ignoringParenCasts);
Index: clang/include/clang/ASTMatchers/ASTMatchers.h
===
--- clang/include/clang/ASTMatchers/ASTMatchers.h
+++ clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -6452,6 +6452,56 @@
   return false;
 }
 
+/// Matches expressions that match InnerMatcher that are possibly wrapped in an
+/// elidable constructor.
+///
+/// In C++17 copy elidable constructors are no longer being
+/// generated in the AST as it is not permitted by the standard. They are
+/// however part of the AST in C++14 and earlier. Therefore, to 

[PATCH] D63149: Added AST matcher for ignoring elidable constructors

2019-06-12 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 204245.
jvikstrom added a comment.

- Using CamelCase and also renamed ignoringElidableMoveConstructorCall to 
ignoringElidableConstructorCall


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63149

Files:
  clang/docs/LibASTMatchersReference.html
  clang/include/clang/ASTMatchers/ASTMatchers.h
  clang/lib/ASTMatchers/Dynamic/Registry.cpp
  clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
===
--- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -566,6 +566,81 @@
llvm::make_unique>("x")));
 }
 
+TEST(Matcher, IgnoresElidableConstructors) {
+  auto matcher1 = cxxOperatorCallExpr(hasArgument(
+  1, callExpr(hasArgument(
+ 0, ignoringElidableMoveConstructorCall(callExpr().bind("c"));
+  auto matcher2 = cxxOperatorCallExpr(hasArgument(
+  1, callExpr(hasArgument(0, ignoringElidableMoveConstructorCall(
+ integerLiteral().bind("c"));
+  auto matcher3 =
+  varDecl(hasInitializer(ignoringElidableMoveConstructorCall(callExpr(;
+
+  auto code1 = "struct H {};"
+   "template H B(T A);"
+   "void f(){"
+   "H D1;"
+   "D1=B(B(1));"
+   "}";
+  auto code2 = "struct H {};"
+   "template H B(T A);"
+   "void f(){"
+   "H D1;"
+   "D1=B(1);"
+   "}";
+  auto code3 = "struct H {};"
+   "H G();"
+   "void f(){"
+   "H D = G();"
+   "}";
+
+  EXPECT_TRUE(matchesConditionally(code1, matcher1, true, "-std=c++14"));
+  EXPECT_TRUE(matchesConditionally(code1, matcher1, true, "-std=c++17"));
+
+  EXPECT_TRUE(matchesConditionally(code2, matcher2, true, "-std=c++14"));
+  EXPECT_TRUE(matchesConditionally(code2, matcher2, true, "-std=c++17"));
+
+  EXPECT_TRUE(matchesConditionally(code3, matcher3, true, "-std=c++14"));
+  EXPECT_TRUE(matchesConditionally(code3, matcher3, true, "-std=c++17"));
+}
+
+TEST(Matcher, IgnoresElidableInReturn) {
+  auto matcher = expr(ignoringElidableMoveConstructorCall(declRefExpr()));
+  auto code1 = "struct H{};"
+   "H f(){"
+   "H g;"
+   "return g;"
+   "}";
+  auto code2 = "struct H{};"
+   "H f(){"
+   "return H();"
+   "}";
+  EXPECT_TRUE(matchesConditionally(code1, matcher, true, "-std=c++14"));
+  EXPECT_TRUE(matchesConditionally(code1, matcher, true, "-std=c++17"));
+  EXPECT_TRUE(matchesConditionally(code2, matcher, false, "-std=c++14"));
+  EXPECT_TRUE(matchesConditionally(code2, matcher, false, "-std=c++17"));
+}
+
+TEST(Matcher, IgnoreElidableConstructorDoesNotMatchConstructors) {
+  auto matcher = varDecl(
+  hasInitializer(ignoringElidableMoveConstructorCall(cxxConstructExpr(;
+  auto code = "struct H {};"
+  "void f(){"
+  "H D;"
+  "}";
+  EXPECT_TRUE(matchesConditionally(code, matcher, true, "-std=c++14"));
+  EXPECT_TRUE(matchesConditionally(code, matcher, true, "-std=c++17"));
+};
+
+TEST(Matcher, IgnoresElidableDoesNotPreventMatches) {
+  auto matcher = expr(ignoringElidableMoveConstructorCall(integerLiteral()));
+  auto code = "void f(){"
+  "int D = 10;"
+  "}";
+  EXPECT_TRUE(matchesConditionally(code, matcher, true, "-std=c++14"));
+  EXPECT_TRUE(matchesConditionally(code, matcher, true, "-std=c++17"));
+}
+
 TEST(Matcher, BindTheSameNameInAlternatives) {
   StatementMatcher matcher = anyOf(
 binaryOperator(hasOperatorName("+"),
Index: clang/lib/ASTMatchers/Dynamic/Registry.cpp
===
--- clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -320,6 +320,7 @@
   REGISTER_MATCHER(hasUnqualifiedDesugaredType);
   REGISTER_MATCHER(hasValueType);
   REGISTER_MATCHER(ifStmt);
+  REGISTER_MATCHER(ignoringElidableMoveConstructorCall);
   REGISTER_MATCHER(ignoringImpCasts);
   REGISTER_MATCHER(ignoringImplicit);
   REGISTER_MATCHER(ignoringParenCasts);
Index: clang/include/clang/ASTMatchers/ASTMatchers.h
===
--- clang/include/clang/ASTMatchers/ASTMatchers.h
+++ clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -6452,6 +6452,56 @@
   return false;
 }
 
+/// Matches expressions that match InnerMatcher that are possibly wrapped in an
+/// elidable constructor.
+///
+/// In C++17 copy elidable constructors are no longer being
+/// generated in the AST as it is not permitted by the standard. They are
+/// however part of the AST in C++14 and earlier. Therefore, to write a 

[PATCH] D63127: [clang-tidy] Fixed checker for abseil to work in C++17 mode

2019-06-11 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom created this revision.
jvikstrom added reviewers: hokein, gribozavr.
Herald added a subscriber: xazax.hun.
Herald added a project: clang.

Fixes the checker for abseil to make tests pass in C++17 mode


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D63127

Files:
  clang-tools-extra/clang-tidy/abseil/DurationUnnecessaryConversionCheck.cpp
  clang-tools-extra/clang-tidy/abseil/FasterStrsplitDelimiterCheck.cpp
  clang-tools-extra/clang-tidy/abseil/TimeSubtractionCheck.cpp
  clang-tools-extra/clang-tidy/abseil/UpgradeDurationConversionsCheck.cpp
  clang-tools-extra/clang-tidy/abseil/UpgradeDurationConversionsCheck.h
  clang-tools-extra/test/clang-tidy/abseil-duration-unnecessary-conversion.cpp
  clang-tools-extra/test/clang-tidy/abseil-faster-strsplit-delimiter.cpp
  clang-tools-extra/test/clang-tidy/abseil-time-subtraction.cpp
  clang-tools-extra/test/clang-tidy/abseil-upgrade-duration-conversions.cpp

Index: clang-tools-extra/test/clang-tidy/abseil-upgrade-duration-conversions.cpp
===
--- clang-tools-extra/test/clang-tidy/abseil-upgrade-duration-conversions.cpp
+++ clang-tools-extra/test/clang-tidy/abseil-upgrade-duration-conversions.cpp
@@ -1,5 +1,4 @@
-// RUN: %check_clang_tidy -std=c++11,c++14 %s abseil-upgrade-duration-conversions %t -- -- -I%S/Inputs
-// FIXME: Fix the checker to work in C++17 mode.
+// RUN: %check_clang_tidy -std=c++11,c++14,c++17 %s abseil-upgrade-duration-conversions %t -- -- -I%S/Inputs
 
 using int64_t = long long;
 
Index: clang-tools-extra/test/clang-tidy/abseil-time-subtraction.cpp
===
--- clang-tools-extra/test/clang-tidy/abseil-time-subtraction.cpp
+++ clang-tools-extra/test/clang-tidy/abseil-time-subtraction.cpp
@@ -1,5 +1,4 @@
-// RUN: %check_clang_tidy -std=c++11,c++14 %s abseil-time-subtraction %t -- -- -I %S/Inputs
-// FIXME: Fix the checker to work in C++17 mode.
+// RUN: %check_clang_tidy -std=c++11,c++14,c++17 %s abseil-time-subtraction %t -- -- -I %S/Inputs
 
 #include "absl/time/time.h"
 
Index: clang-tools-extra/test/clang-tidy/abseil-faster-strsplit-delimiter.cpp
===
--- clang-tools-extra/test/clang-tidy/abseil-faster-strsplit-delimiter.cpp
+++ clang-tools-extra/test/clang-tidy/abseil-faster-strsplit-delimiter.cpp
@@ -1,5 +1,4 @@
-// RUN: %check_clang_tidy -std=c++11,c++14 %s abseil-faster-strsplit-delimiter %t
-// FIXME: Fix the checker to work in C++17 mode.
+// RUN: %check_clang_tidy -std=c++11,c++14,c++17 %s abseil-faster-strsplit-delimiter %t
 
 namespace absl {
 
Index: clang-tools-extra/test/clang-tidy/abseil-duration-unnecessary-conversion.cpp
===
--- clang-tools-extra/test/clang-tidy/abseil-duration-unnecessary-conversion.cpp
+++ clang-tools-extra/test/clang-tidy/abseil-duration-unnecessary-conversion.cpp
@@ -1,5 +1,4 @@
-// RUN: %check_clang_tidy -std=c++11,c++14 %s abseil-duration-unnecessary-conversion %t -- -- -I %S/Inputs
-// FIXME: Fix the checker to work in C++17 mode.
+// RUN: %check_clang_tidy -std=c++11,c++14,c++17 %s abseil-duration-unnecessary-conversion %t -- -- -I %S/Inputs
 
 #include "absl/time/time.h"
 
Index: clang-tools-extra/clang-tidy/abseil/UpgradeDurationConversionsCheck.h
===
--- clang-tools-extra/clang-tidy/abseil/UpgradeDurationConversionsCheck.h
+++ clang-tools-extra/clang-tidy/abseil/UpgradeDurationConversionsCheck.h
@@ -17,7 +17,8 @@
 namespace tidy {
 namespace abseil {
 
-/// Finds deprecated uses of `absl::Duration` arithmetic operators and factories.
+/// Finds deprecated uses of `absl::Duration` arithmetic operators and
+/// factories.
 ///
 /// For the user-facing documentation see:
 /// http://clang.llvm.org/extra/clang-tidy/checks/abseil-upgrade-duration-conversions.html
Index: clang-tools-extra/clang-tidy/abseil/UpgradeDurationConversionsCheck.cpp
===
--- clang-tools-extra/clang-tidy/abseil/UpgradeDurationConversionsCheck.cpp
+++ clang-tools-extra/clang-tidy/abseil/UpgradeDurationConversionsCheck.cpp
@@ -10,6 +10,8 @@
 #include "DurationRewriter.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/Basic/Specifiers.h"
 
 using namespace clang::ast_matchers;
 
@@ -34,13 +36,15 @@
   Finder->addMatcher(
   cxxOperatorCallExpr(
   argumentCountIs(2),
-  hasArgument(
-  0, expr(hasType(cxxRecordDecl(hasName("::absl::Duration"),
+  hasArgument(0,
+  expr(hasType(cxxRecordDecl(hasName("::absl::Duration"
+  .bind("arg0")),
   hasArgument(1, expr().bind("arg")),
   callee(functionDecl(
   

[PATCH] D63149: Added AST matcher for ignoring elidable constructors

2019-06-11 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom created this revision.
jvikstrom added reviewers: hokein, gribozavr.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Added AST matcher for ignoring elidable move constructors


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D63149

Files:
  clang/docs/LibASTMatchersReference.html
  clang/include/clang/ASTMatchers/ASTMatchers.h
  clang/lib/ASTMatchers/Dynamic/Registry.cpp
  clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
===
--- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -566,6 +566,71 @@
llvm::make_unique>("x")));
 }
 
+TEST(Matcher, IgnoresElidableConstructors) {
+  StatementMatcher matcher = cxxOperatorCallExpr(
+hasArgument(1, callExpr(hasArgument(0, 
+ignoringElidableMoveConstructorCall(
+  ignoringParenImpCasts(expr().bind("c")));
+
+  EXPECT_TRUE(matchAndVerifyResultTrue(
+"struct H {};"
+"template H B(T A);"
+"void f(){"
+"H D1;"
+"D1=B(B(1));"
+"}"
+, matcher, llvm::make_unique>("c")));
+
+  EXPECT_TRUE(matchAndVerifyResultTrue(
+"struct H {};"
+"template H B(T A);"
+"void f(){"
+"H D1;"
+"D1=B(1);"
+"}"
+, matcher, llvm::make_unique>("c")));
+}
+
+TEST(Matcher, IgnoresElidableInReturn) {
+  auto matcher = expr(ignoringElidableMoveConstructorCall(declRefExpr()));
+
+  EXPECT_TRUE(matches(
+"struct H{};"
+"H f(){"
+"H g;"
+"return g;"
+"}"
+, matcher));
+
+  EXPECT_FALSE(matches(
+"struct H{};"
+"H f(){"
+"return H();"
+"}"
+, matcher
+  ));
+}
+
+TEST(Matcher, IgnoreElidableConstructorDoesNotMatchConstructors) {
+  auto matcher = varDecl(hasInitializer(ignoringElidableMoveConstructorCall(cxxConstructExpr(;
+  EXPECT_TRUE(matches(
+"struct H {};"
+"void f(){"
+"H D;"
+"}"
+  , matcher));
+
+};
+
+TEST(Matcher, IgnoresElidableDoesNotPreventMatches) {
+  auto matcher = expr(ignoringElidableMoveConstructorCall(integerLiteral()));
+  EXPECT_TRUE(matches(
+"void f(){"
+"int D = 10;"
+"}"
+  , matcher));
+}
+
 TEST(Matcher, BindTheSameNameInAlternatives) {
   StatementMatcher matcher = anyOf(
 binaryOperator(hasOperatorName("+"),
Index: clang/lib/ASTMatchers/Dynamic/Registry.cpp
===
--- clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -320,6 +320,7 @@
   REGISTER_MATCHER(hasUnqualifiedDesugaredType);
   REGISTER_MATCHER(hasValueType);
   REGISTER_MATCHER(ifStmt);
+  REGISTER_MATCHER(ignoringElidableMoveConstructorCall);
   REGISTER_MATCHER(ignoringImpCasts);
   REGISTER_MATCHER(ignoringImplicit);
   REGISTER_MATCHER(ignoringParenCasts);
Index: clang/include/clang/ASTMatchers/ASTMatchers.h
===
--- clang/include/clang/ASTMatchers/ASTMatchers.h
+++ clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -6452,6 +6452,31 @@
   return false;
 }
 
+/// Matches expressions that match InnerMatcher after any elidable constructor are stripped off.
+///
+/// Example matches the entire D1 = ... (matcher = cxxOperatorCallExpr(hasArgument(1, callExpr(hasArgument(0, ignoringElidableMoveConstructorCall(ignoringParenImpCasts(callExpr(
+/// \code
+/// struct H {};
+/// template H B(T A);
+/// void f() {
+///   H D1;
+///   D1 = B(B(1));
+/// }
+/// \endcode
+AST_MATCHER_P(Expr, ignoringElidableMoveConstructorCall,
+  ast_matchers::internal::Matcher, InnerMatcher) {
+  if (const auto* cxx_construct_expr = dyn_cast()) {
+if (cxx_construct_expr->isElidable()) {
+  if (const auto* materialize_temp = dyn_cast(
+  cxx_construct_expr->getArg(0))) {
+return InnerMatcher.matches(*materialize_temp, Finder, Builder);
+  }
+  return InnerMatcher.matches(*cxx_construct_expr, Finder, Builder);
+}
+  }
+  return InnerMatcher.matches(Node, Finder, Builder);
+}
+
 ////
 // OpenMP handling.
 ////
Index: clang/docs/LibASTMatchersReference.html
===
--- clang/docs/LibASTMatchersReference.html
+++ clang/docs/LibASTMatchersReference.html
@@ -5728,6 +5728,19 @@
 
 
 
+Matcherhttps://clang.llvm.org/doxygen/classclang_1_1Expr.html;>ExprignoringElidableMoveConstructorCallast_matchers::Matcherhttps://clang.llvm.org/doxygen/classclang_1_1Expr.html;>Expr InnerMatcher
+Matches expressions that match InnerMatcher after any elidable constructor are stripped off.
+
+Example matches the entire D1 = ... 

[PATCH] D63559: [clang-tidy] Added functionality for getting semantic highlights for variable and function declarations

2019-06-25 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom marked an inline comment as done.
jvikstrom added inline comments.



Comment at: clang-tools-extra/clangd/SemanticHighlight.cpp:75
+  SemanticSymbolASTCollector Collector(Ctx);
+  Collector.TraverseAST(Ctx);
+  return Collector.getSymbols();

jvikstrom wrote:
> hokein wrote:
> > let's move the above lines into `SemanticSymbolASTCollector`, we can define 
> > a new method "collectTokens()".
> Should I expose the entire class or keep the getSemanticHighlights function?  
> (I'm just thinking that RecursiveASTVisitor contains a lot of public 
> functions which makes it not super obvious which function to call to get 
> semantic highlight things when looking at autocomplete)
Actually I can just do private inheritance and declare 
RecursiveASTVisitor to be a friend.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63559



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D63559: [clangd] Added functionality for getting semantic highlights for variable and function declarations

2019-06-25 Thread Johan Vikström via Phabricator via cfe-commits
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 Tokens,
+   std::vector ExpectedRanges,
+   SemanticHighlightKind Kind) {
+  std::vector 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 {
+  friend class RecursiveASTVisitor;
+  std::vector Tokens;
+  ASTContext 
+  const SourceManager 
+
+public:
+  SemanticTokenCollector(ParsedAST );
+  std::vector collectTokens();
+
+private:
+  bool VisitVarDecl(VarDecl *Var);
+  bool VisitFunctionDecl(FunctionDecl *Func);
+
+  void addSymbol(NamedDecl *D, SemanticHighlightKind Kind);
+};
+
+bool operator==(const SemanticToken , const SemanticToken );
+bool operator!=(const SemanticToken , const SemanticToken );
+
+} // namespace clangd
+} // namespace clang
+
+#endif
Index: clang-tools-extra/clangd/SemanticHighlight.cpp
===
--- /dev/null
+++ clang-tools-extra/clangd/SemanticHighlight.cpp
@@ 

[PATCH] D63559: [clangd] Added functionality for getting semantic highlights for variable and function declarations

2019-06-25 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 206376.
jvikstrom marked 3 inline comments as done.
jvikstrom added a comment.

Added header and empty line


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 Tokens,
+   std::vector ExpectedRanges,
+   SemanticHighlightKind Kind) {
+  std::vector 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,55 @@
+//==-- 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 {
+  friend class RecursiveASTVisitor;
+  std::vector Tokens;
+  ASTContext 
+  const SourceManager 
+
+public:
+  SemanticTokenCollector(ParsedAST );
+  std::vector collectTokens();
+
+private:
+  bool VisitVarDecl(VarDecl *Var);
+  bool VisitFunctionDecl(FunctionDecl *Func);
+
+  void addSymbol(NamedDecl *D, SemanticHighlightKind Kind);
+};
+
+bool operator==(const SemanticToken , const SemanticToken );
+bool operator!=(const SemanticToken , const SemanticToken );
+
+} // namespace clangd
+} // namespace clang
+
+#endif
Index: clang-tools-extra/clangd/SemanticHighlight.cpp
===
--- /dev/null
+++ clang-tools-extra/clangd/SemanticHighlight.cpp
@@ -0,0 

[PATCH] D63559: [clangd] Added functionality for getting semantic highlights for variable and function declarations

2019-06-25 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom added inline comments.



Comment at: clang-tools-extra/clangd/unittests/SemanticHighlightTests.cpp:57
+
+  checkTokensExists(Tokens, Variables, SemanticHighlightKind::Variable);
+  checkTokensExists(Tokens, Function, SemanticHighlightKind::Function);

hokein wrote:
> 
> ```
> checkHighlights(R"cpp(void $Function[[Foo]]))cpp");
> 
> checkHighlights(...) {
>...
>ExpectedTokens = 
> annotation.ranges(toString(SemanticHighlightKind::Variable));
>ExpectedTokens = 
> annotation.ranges(toString(SemanticHighlightKind::Function));
>EXPECT_THAT(ExpectedTokens, UnorderedElementsAreArray(ActualTokens));
> }
Had to add some extra logic to merge the different token types


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63559



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D63559: [clangd] Added functionality for getting semantic highlights for variable and function declarations

2019-06-25 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 206393.
jvikstrom marked 4 inline comments as done.
jvikstrom added a comment.

Changed tests


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,73 @@
+//===- 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 {
+
+std::vector makeSemanticTokens(std::vector Ranges,
+ SemanticHighlightKind Kind) {
+  std::vector Tokens(Ranges.size());
+  for (int I = 0, End = Ranges.size(); I < End; I++) {
+Tokens[I].R = Ranges[I];
+Tokens[I].Kind = Kind;
+  }
+
+  return Tokens;
+}
+
+void checkHighlights(std::string Code) {
+
+  Annotations Test(Code);
+  auto AST = TestTU::withCode(Test.code()).build();
+  std::map KindToString{
+  {SemanticHighlightKind::Variable, "Variable"},
+  {SemanticHighlightKind::Function, "Function"}};
+  std::vector ExpectedTokens;
+  for (auto KindString : KindToString) {
+std::vector Toks =
+makeSemanticTokens(Test.ranges(KindString.second), KindString.first);
+ExpectedTokens.insert(ExpectedTokens.end(), Toks.begin(), Toks.end());
+  }
+
+  auto ActualTokens = getSemanticHighlights(AST);
+  EXPECT_THAT(ActualTokens, testing::UnorderedElementsAreArray(ExpectedTokens));
+}
+
+TEST(SemanticTokenCollector, GetBeginningOfIdentifier) {
+  checkHighlights(R"cpp(
+struct A {
+  double SomeMember;
+};
+void $Function[[foo]](int $Variable[[a]]) {
+  auto $Variable[[VeryLongVariableName]] = 12312;
+  A $Variable[[aa]];
+}
+  )cpp");
+}
+
+TEST(SemanticTokenCollector, DoesNotGetUnnamedParamDecls) {
+  checkHighlights(R"cpp(
+void $Function[[foo]](int);
+  )cpp");
+}
+
+} // 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,38 @@
+//==-- 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;
+};
+
+bool operator==(const SemanticToken , const SemanticToken );
+bool operator!=(const SemanticToken , const SemanticToken );
+
+std::vector getSemanticHighlights(ParsedAST );
+
+} // namespace clangd
+} // namespace clang
+
+#endif
\ No newline at end of file
Index: clang-tools-extra/clangd/SemanticHighlight.cpp
===
--- /dev/null
+++ clang-tools-extra/clangd/SemanticHighlight.cpp
@@ -0,0 +1,83 @@
+//===--- SemanticHighlight.cpp - -- -*- C++ -*-===//
+//
+// Part of the LLVM Project, under the 

[PATCH] D63559: [clangd] Added functionality for getting semantic highlights for variable and function declarations

2019-06-25 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 206450.
jvikstrom added a comment.

Made SemanticTokenCollector skip Decls not in the main file.


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,73 @@
+//===- 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 {
+
+std::vector makeSemanticTokens(std::vector Ranges,
+ SemanticHighlightKind Kind) {
+  std::vector Tokens(Ranges.size());
+  for (int I = 0, End = Ranges.size(); I < End; I++) {
+Tokens[I].R = Ranges[I];
+Tokens[I].Kind = Kind;
+  }
+
+  return Tokens;
+}
+
+void checkHighlights(std::string Code) {
+
+  Annotations Test(Code);
+  auto AST = TestTU::withCode(Test.code()).build();
+  std::map KindToString{
+  {SemanticHighlightKind::Variable, "Variable"},
+  {SemanticHighlightKind::Function, "Function"}};
+  std::vector ExpectedTokens;
+  for (auto KindString : KindToString) {
+std::vector Toks =
+makeSemanticTokens(Test.ranges(KindString.second), KindString.first);
+ExpectedTokens.insert(ExpectedTokens.end(), Toks.begin(), Toks.end());
+  }
+
+  auto ActualTokens = getSemanticHighlights(AST);
+  EXPECT_THAT(ActualTokens, testing::UnorderedElementsAreArray(ExpectedTokens));
+}
+
+TEST(SemanticTokenCollector, GetBeginningOfIdentifier) {
+  checkHighlights(R"cpp(
+struct A {
+  double SomeMember;
+};
+void $Function[[foo]](int $Variable[[a]]) {
+  auto $Variable[[VeryLongVariableName]] = 12312;
+  A $Variable[[aa]];
+}
+  )cpp");
+}
+
+TEST(SemanticTokenCollector, DoesNotGetUnnamedParamDecls) {
+  checkHighlights(R"cpp(
+void $Function[[foo]](int);
+  )cpp");
+}
+
+} // 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,38 @@
+//==-- 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;
+};
+
+bool operator==(const SemanticToken , const SemanticToken );
+bool operator!=(const SemanticToken , const SemanticToken );
+
+std::vector getSemanticHighlights(ParsedAST );
+
+} // namespace clangd
+} // namespace clang
+
+#endif
\ No newline at end of file
Index: clang-tools-extra/clangd/SemanticHighlight.cpp
===
--- /dev/null
+++ clang-tools-extra/clangd/SemanticHighlight.cpp
@@ -0,0 +1,94 @@
+//===--- SemanticHighlight.cpp - -- -*- C++ -*-===//
+//
+// Part of the LLVM Project, under the 

[PATCH] D63262: [clang-tidy] Made abseil-upgrade-duration-conversions tests pass on c++17

2019-06-13 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom created this revision.
jvikstrom added reviewers: hokein, gribozavr.
Herald added subscribers: cfe-commits, xazax.hun.
Herald added a project: clang.

Made abseil-upgrade-duration-conversions tests pass on c++17


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D63262

Files:
  clang-tools-extra/clang-tidy/abseil/UpgradeDurationConversionsCheck.cpp
  clang-tools-extra/test/clang-tidy/abseil-upgrade-duration-conversions.cpp


Index: clang-tools-extra/test/clang-tidy/abseil-upgrade-duration-conversions.cpp
===
--- clang-tools-extra/test/clang-tidy/abseil-upgrade-duration-conversions.cpp
+++ clang-tools-extra/test/clang-tidy/abseil-upgrade-duration-conversions.cpp
@@ -1,4 +1,4 @@
-// RUN: %check_clang_tidy -std=c++11,c++14 %s 
abseil-upgrade-duration-conversions %t -- -- -I%S/Inputs
+// RUN: %check_clang_tidy -std=c++11-or-later %s 
abseil-upgrade-duration-conversions %t -- -- -I%S/Inputs
 // FIXME: Fix the checker to work in C++17 mode.
 
 using int64_t = long long;
Index: clang-tools-extra/clang-tidy/abseil/UpgradeDurationConversionsCheck.cpp
===
--- clang-tools-extra/clang-tidy/abseil/UpgradeDurationConversionsCheck.cpp
+++ clang-tools-extra/clang-tidy/abseil/UpgradeDurationConversionsCheck.cpp
@@ -40,7 +40,8 @@
   callee(functionDecl(
   hasParent(functionTemplateDecl()),
   unless(hasTemplateArgument(0, refersToType(builtinType(,
-  hasAnyName("operator*=", "operator/=",
+  hasAnyName("operator*=", "operator/="
+  .bind("OuterExpr"),
   this);
 
   // Match expressions like `a.operator*=(b)` and `a.operator/=(b)` where `a`
@@ -52,7 +53,8 @@
   hasParent(functionTemplateDecl()),
   unless(hasTemplateArgument(0, refersToType(builtinType(,
   hasAnyName("operator*=", "operator/="))),
-  argumentCountIs(1), hasArgument(0, expr().bind("arg"))),
+  argumentCountIs(1), hasArgument(0, expr().bind("arg")))
+  .bind("OuterExpr"),
   this);
 
   // Match expressions like `a * b`, `a / b`, `operator*(a, b)`, and
@@ -66,7 +68,8 @@
argumentCountIs(2),
hasArgument(0, expr(hasType(
   
cxxRecordDecl(hasName("::absl::Duration"),
-   hasArgument(1, expr().bind("arg"))),
+   hasArgument(1, expr().bind("arg")))
+  .bind("OuterExpr"),
   this);
 
   // Match expressions like `a * b` and `operator*(a, b)` where `a` is not of a
@@ -77,8 +80,9 @@
unless(hasTemplateArgument(0, refersToType(builtinType(,
hasName("::absl::operator*"))),
argumentCountIs(2), hasArgument(0, expr().bind("arg")),
-   hasArgument(1, expr(hasType(cxxRecordDecl(
-  hasName("::absl::Duration")),
+   hasArgument(1, expr(hasType(
+  
cxxRecordDecl(hasName("::absl::Duration"))
+  .bind("OuterExpr"),
   this);
 
   // For the factory functions, we match only the non-templated overloads that
@@ -103,8 +107,9 @@
 has(implicitCastExpr(hasCastKind(CK_UserDefinedConversion,
   hasParent(callExpr(
   callee(functionDecl(DurationFactoryFunction(),
-  unless(hasParent(functionTemplateDecl(),
-  hasArgument(0, expr().bind("arg"),
+  unless(hasParent(functionTemplateDecl(),
+  hasArgument(0, expr().bind("arg")
+  .bind("OuterExpr"),
   this);
 }
 
@@ -117,7 +122,10 @@
   const auto *ArgExpr = Result.Nodes.getNodeAs("arg");
   SourceLocation Loc = ArgExpr->getBeginLoc();
 
-  if (!match(isInTemplateInstantiation(), *ArgExpr, *Result.Context).empty()) {
+  const auto *OuterExpr = Result.Nodes.getNodeAs("OuterExpr");
+
+  if (!match(isInTemplateInstantiation(), *OuterExpr, *Result.Context)
+   .empty()) {
 if (MatchedTemplateLocations.count(Loc.getRawEncoding()) == 0) {
   // For each location matched in a template instantiation, we check if the
   // location can also be found in `MatchedTemplateLocations`. If it is not


Index: clang-tools-extra/test/clang-tidy/abseil-upgrade-duration-conversions.cpp
===
--- clang-tools-extra/test/clang-tidy/abseil-upgrade-duration-conversions.cpp
+++ clang-tools-extra/test/clang-tidy/abseil-upgrade-duration-conversions.cpp
@@ -1,4 +1,4 @@
-// RUN: %check_clang_tidy -std=c++11,c++14 %s abseil-upgrade-duration-conversions %t -- -- -I%S/Inputs
+// RUN: %check_clang_tidy -std=c++11-or-later %s abseil-upgrade-duration-conversions %t -- -- -I%S/Inputs
 // FIXME: Fix the checker to work in C++17 mode.
 
 using int64_t = long long;
Index: 

[PATCH] D63261: [clang-tidy] Fixed abseil-time-subtraction to work on C++17

2019-06-13 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom created this revision.
jvikstrom added reviewers: hokein, gribozavr.
Herald added subscribers: cfe-commits, xazax.hun.
Herald added a project: clang.

Fixed abseil-time-subtraction to work on C++17


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D63261

Files:
  clang-tools-extra/clang-tidy/abseil/TimeSubtractionCheck.cpp
  clang-tools-extra/test/clang-tidy/abseil-time-subtraction.cpp


Index: clang-tools-extra/test/clang-tidy/abseil-time-subtraction.cpp
===
--- clang-tools-extra/test/clang-tidy/abseil-time-subtraction.cpp
+++ clang-tools-extra/test/clang-tidy/abseil-time-subtraction.cpp
@@ -1,4 +1,4 @@
-// RUN: %check_clang_tidy -std=c++11,c++14 %s abseil-time-subtraction %t -- -- 
-I %S/Inputs
+// RUN: %check_clang_tidy -std=c++11-or-later %s abseil-time-subtraction %t -- 
-- -I %S/Inputs
 // FIXME: Fix the checker to work in C++17 mode.
 
 #include "absl/time/time.h"
Index: clang-tools-extra/clang-tidy/abseil/TimeSubtractionCheck.cpp
===
--- clang-tools-extra/clang-tidy/abseil/TimeSubtractionCheck.cpp
+++ clang-tools-extra/clang-tidy/abseil/TimeSubtractionCheck.cpp
@@ -29,16 +29,26 @@
 
 static bool isConstructorAssignment(const MatchFinder::MatchResult ,
 const Expr *Node) {
+  // For C++14 and earlier there are elidable constructors that must be matched
+  // in hasParent. The elidable constructors do not exist in C++17 and later 
and
+  // therefore an additional check that does not match against the elidable
+  // constructors are needed for this case.
   return selectFirst(
- "e", match(expr(hasParent(materializeTemporaryExpr(hasParent(
- cxxConstructExpr(hasParent(exprWithCleanups(
- hasParent(varDecl()
-.bind("e"),
-*Node, *Result.Context)) != nullptr;
+ "e",
+ match(callExpr(hasParent(materializeTemporaryExpr(
+hasParent(cxxConstructExpr(hasParent(
+
exprWithCleanups(hasParent(varDecl()
+   .bind("e"),
+   *Node, *Result.Context)) != nullptr ||
+ selectFirst("e",
+ 
match(callExpr(hasParent(varDecl())).bind("e"),
+   *Node, *Result.Context)) != nullptr;
 }
 
 static bool isArgument(const MatchFinder::MatchResult ,
const Expr *Node) {
+  // For the same reason as in isConstructorAssignment two separate 
selectFirsts
+  // need to be checked here
   return selectFirst(
  "e",
  match(expr(hasParent(
@@ -46,16 +56,26 @@
 hasParent(callExpr()),
 unless(hasParent(cxxOperatorCallExpr(
.bind("e"),
-   *Node, *Result.Context)) != nullptr;
+   *Node, *Result.Context)) != nullptr ||
+ selectFirst(
+ "e", match(expr(hasParent(callExpr()),
+ unless(hasParent(cxxOperatorCallExpr(
+.bind("e"),
+*Node, *Result.Context)) != nullptr;
 }
 
 static bool isReturn(const MatchFinder::MatchResult , const Expr *Node) 
{
+  // For the same reason as in isConstructorAssignment two separate 
selectFirsts
+  // need to be checked here
   return selectFirst(
  "e", match(expr(hasParent(materializeTemporaryExpr(hasParent(
  cxxConstructExpr(hasParent(exprWithCleanups(
  hasParent(returnStmt()
 .bind("e"),
-*Node, *Result.Context)) != nullptr;
+*Node, *Result.Context)) != nullptr ||
+ selectFirst("e",
+ match(expr(hasParent(returnStmt())).bind("e"),
+   *Node, *Result.Context)) != nullptr;
 }
 
 static bool parensRequired(const MatchFinder::MatchResult ,


Index: clang-tools-extra/test/clang-tidy/abseil-time-subtraction.cpp
===
--- clang-tools-extra/test/clang-tidy/abseil-time-subtraction.cpp
+++ clang-tools-extra/test/clang-tidy/abseil-time-subtraction.cpp
@@ -1,4 +1,4 @@
-// RUN: %check_clang_tidy -std=c++11,c++14 %s abseil-time-subtraction %t -- -- -I %S/Inputs
+// RUN: %check_clang_tidy -std=c++11-or-later %s abseil-time-subtraction %t -- -- -I %S/Inputs
 // FIXME: Fix the checker to work in C++17 mode.
 
 #include "absl/time/time.h"
Index: clang-tools-extra/clang-tidy/abseil/TimeSubtractionCheck.cpp
===
--- 

[PATCH] D63263: [clang-tidy] Fixed abseil-duration-unnecessary-conversion tests for c++17

2019-06-13 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom created this revision.
jvikstrom added reviewers: hokein, gribozavr.
Herald added subscribers: cfe-commits, xazax.hun.
Herald added a project: clang.

Fixed abseil-duration-unnecessary-conversion tests for c++17


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D63263

Files:
  clang-tools-extra/clang-tidy/abseil/DurationUnnecessaryConversionCheck.cpp
  clang-tools-extra/test/clang-tidy/abseil-duration-unnecessary-conversion.cpp


Index: 
clang-tools-extra/test/clang-tidy/abseil-duration-unnecessary-conversion.cpp
===
--- clang-tools-extra/test/clang-tidy/abseil-duration-unnecessary-conversion.cpp
+++ clang-tools-extra/test/clang-tidy/abseil-duration-unnecessary-conversion.cpp
@@ -1,5 +1,4 @@
-// RUN: %check_clang_tidy -std=c++11,c++14 %s 
abseil-duration-unnecessary-conversion %t -- -- -I %S/Inputs
-// FIXME: Fix the checker to work in C++17 mode.
+// RUN: %check_clang_tidy -std=c++11-or-later %s 
abseil-duration-unnecessary-conversion %t -- -- -I %S/Inputs
 
 #include "absl/time/time.h"
 
Index: 
clang-tools-extra/clang-tidy/abseil/DurationUnnecessaryConversionCheck.cpp
===
--- clang-tools-extra/clang-tidy/abseil/DurationUnnecessaryConversionCheck.cpp
+++ clang-tools-extra/clang-tidy/abseil/DurationUnnecessaryConversionCheck.cpp
@@ -30,10 +30,9 @@
 
 // Matcher which matches the current scale's factory with a `1` argument,
 // e.g. `absl::Seconds(1)`.
-auto factory_matcher = cxxConstructExpr(hasArgument(
-0,
+auto factory_matcher = ignoringElidableConstructorCall(
 callExpr(callee(functionDecl(hasName(DurationFactory))),
- hasArgument(0, 
ignoringImpCasts(integerLiteral(equals(1)));
+ hasArgument(0, ignoringImpCasts(integerLiteral(equals(1));
 
 // Matcher which matches either inverse function and binds its argument,
 // e.g. `absl::ToDoubleSeconds(dur)`.


Index: clang-tools-extra/test/clang-tidy/abseil-duration-unnecessary-conversion.cpp
===
--- clang-tools-extra/test/clang-tidy/abseil-duration-unnecessary-conversion.cpp
+++ clang-tools-extra/test/clang-tidy/abseil-duration-unnecessary-conversion.cpp
@@ -1,5 +1,4 @@
-// RUN: %check_clang_tidy -std=c++11,c++14 %s abseil-duration-unnecessary-conversion %t -- -- -I %S/Inputs
-// FIXME: Fix the checker to work in C++17 mode.
+// RUN: %check_clang_tidy -std=c++11-or-later %s abseil-duration-unnecessary-conversion %t -- -- -I %S/Inputs
 
 #include "absl/time/time.h"
 
Index: clang-tools-extra/clang-tidy/abseil/DurationUnnecessaryConversionCheck.cpp
===
--- clang-tools-extra/clang-tidy/abseil/DurationUnnecessaryConversionCheck.cpp
+++ clang-tools-extra/clang-tidy/abseil/DurationUnnecessaryConversionCheck.cpp
@@ -30,10 +30,9 @@
 
 // Matcher which matches the current scale's factory with a `1` argument,
 // e.g. `absl::Seconds(1)`.
-auto factory_matcher = cxxConstructExpr(hasArgument(
-0,
+auto factory_matcher = ignoringElidableConstructorCall(
 callExpr(callee(functionDecl(hasName(DurationFactory))),
- hasArgument(0, ignoringImpCasts(integerLiteral(equals(1)));
+ hasArgument(0, ignoringImpCasts(integerLiteral(equals(1));
 
 // Matcher which matches either inverse function and binds its argument,
 // e.g. `absl::ToDoubleSeconds(dur)`.
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D63127: [clang-tidy] Fixed checker for abseil to work in C++17 mode

2019-06-13 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom abandoned this revision.
jvikstrom added a comment.

Resubmitted as 4 different CLs


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63127



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D63261: [clang-tidy] Fixed abseil-time-subtraction to work on C++17

2019-06-13 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 204538.
jvikstrom marked an inline comment as done.
jvikstrom added a comment.

Using anyOf instead of multiple selectFirsts


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63261

Files:
  clang-tools-extra/clang-tidy/abseil/TimeSubtractionCheck.cpp
  clang-tools-extra/test/clang-tidy/abseil-time-subtraction.cpp


Index: clang-tools-extra/test/clang-tidy/abseil-time-subtraction.cpp
===
--- clang-tools-extra/test/clang-tidy/abseil-time-subtraction.cpp
+++ clang-tools-extra/test/clang-tidy/abseil-time-subtraction.cpp
@@ -1,4 +1,4 @@
-// RUN: %check_clang_tidy -std=c++11,c++14 %s abseil-time-subtraction %t -- -- 
-I %S/Inputs
+// RUN: %check_clang_tidy -std=c++11-or-later %s abseil-time-subtraction %t -- 
-- -I %S/Inputs
 // FIXME: Fix the checker to work in C++17 mode.
 
 #include "absl/time/time.h"
Index: clang-tools-extra/clang-tidy/abseil/TimeSubtractionCheck.cpp
===
--- clang-tools-extra/clang-tidy/abseil/TimeSubtractionCheck.cpp
+++ clang-tools-extra/clang-tidy/abseil/TimeSubtractionCheck.cpp
@@ -29,33 +29,52 @@
 
 static bool isConstructorAssignment(const MatchFinder::MatchResult ,
 const Expr *Node) {
+  // For C++14 and earlier there are elidable constructors that must be matched
+  // in hasParent. The elidable constructors do not exist in C++17 and later 
and
+  // therefore an additional check that does not match against the elidable
+  // constructors are needed for this case.
   return selectFirst(
- "e", match(expr(hasParent(materializeTemporaryExpr(hasParent(
- cxxConstructExpr(hasParent(exprWithCleanups(
- hasParent(varDecl()
-.bind("e"),
-*Node, *Result.Context)) != nullptr;
+ "e",
+ match(expr(anyOf(
+   callExpr(hasParent(materializeTemporaryExpr(hasParent(
+
cxxConstructExpr(hasParent(exprWithCleanups(
+hasParent(varDecl()
+   .bind("e"),
+   callExpr(hasParent(varDecl())).bind("e"))),
+   *Node, *Result.Context)) != nullptr;
 }
 
 static bool isArgument(const MatchFinder::MatchResult ,
const Expr *Node) {
+  // For the same reason as in isConstructorAssignment two separate 
selectFirsts
+  // need to be checked here
   return selectFirst(
  "e",
- match(expr(hasParent(
-
materializeTemporaryExpr(hasParent(cxxConstructExpr(
-hasParent(callExpr()),
-unless(hasParent(cxxOperatorCallExpr(
-   .bind("e"),
-   *Node, *Result.Context)) != nullptr;
+ match(
+ expr(anyOf(
+ expr(hasParent(materializeTemporaryExpr(
+  hasParent(cxxConstructExpr(
+  hasParent(callExpr()),
+  unless(hasParent(cxxOperatorCallExpr(
+ .bind("e"),
+ expr(hasParent(callExpr()),
+  unless(hasParent(cxxOperatorCallExpr(
+ .bind("e"))),
+ *Node, *Result.Context)) != nullptr;
 }
 
 static bool isReturn(const MatchFinder::MatchResult , const Expr *Node) 
{
+  // For the same reason as in isConstructorAssignment two separate 
selectFirsts
+  // need to be checked here
   return selectFirst(
- "e", match(expr(hasParent(materializeTemporaryExpr(hasParent(
- cxxConstructExpr(hasParent(exprWithCleanups(
- hasParent(returnStmt()
-.bind("e"),
-*Node, *Result.Context)) != nullptr;
+ "e",
+ match(expr(anyOf(
+   expr(hasParent(materializeTemporaryExpr(hasParent(
+cxxConstructExpr(hasParent(exprWithCleanups(
+hasParent(returnStmt()
+   .bind("e"),
+   expr(hasParent(returnStmt())).bind("e"))),
+   *Node, *Result.Context)) != nullptr;
 }
 
 static bool parensRequired(const MatchFinder::MatchResult ,


Index: clang-tools-extra/test/clang-tidy/abseil-time-subtraction.cpp
===
--- clang-tools-extra/test/clang-tidy/abseil-time-subtraction.cpp
+++ clang-tools-extra/test/clang-tidy/abseil-time-subtraction.cpp
@@ -1,4 +1,4 @@

[PATCH] D63149: Added AST matcher for ignoring elidable constructors

2019-06-12 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 204277.
jvikstrom marked 10 inline comments as done.
jvikstrom added a comment.

- Fixed wrong formatting in test code


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63149

Files:
  clang/docs/LibASTMatchersReference.html
  clang/include/clang/ASTMatchers/ASTMatchers.h
  clang/lib/ASTMatchers/Dynamic/Registry.cpp
  clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
  clang/unittests/ASTMatchers/ASTMatchersTest.h

Index: clang/unittests/ASTMatchers/ASTMatchersTest.h
===
--- clang/unittests/ASTMatchers/ASTMatchersTest.h
+++ clang/unittests/ASTMatchers/ASTMatchersTest.h
@@ -57,6 +57,17 @@
   const std::unique_ptr FindResultReviewer;
 };
 
+enum class LanguageMode : int {
+  CXX11 = 0,
+  CXX14 = 1,
+  CXX17 = 2,
+  CXX20 = 3,
+  CXX11OrLater,
+  CXX14OrLater,
+  CXX17OrLater,
+  CXX20OrLater
+};
+
 template 
 testing::AssertionResult matchesConditionally(
 const std::string , const T , bool ExpectMatch,
@@ -116,14 +127,56 @@
 }
 
 template 
-testing::AssertionResult matches(const std::string , const T ) {
-  return matchesConditionally(Code, AMatcher, true, "-std=c++11");
+testing::AssertionResult
+matchesConditionally(const std::string , const T ,
+ bool ExpectMatch, const LanguageMode ) {
+  std::vector LangModeStrings{"-std=c++11", "-std=c++14",
+   "-std=c++17", "-std=c++20"};
+  std::vector LangModes;
+  switch (Mode) {
+  case LanguageMode::CXX11:
+  case LanguageMode::CXX14:
+  case LanguageMode::CXX17:
+  case LanguageMode::CXX20:
+LangModes = {Mode};
+break;
+  case LanguageMode::CXX11OrLater:
+LangModes = {LanguageMode::CXX11, LanguageMode::CXX14, LanguageMode::CXX17, LanguageMode::CXX20};
+break;
+  case LanguageMode::CXX14OrLater:
+LangModes = {LanguageMode::CXX14, LanguageMode::CXX17, LanguageMode::CXX20};
+break;
+  case LanguageMode::CXX17OrLater:
+LangModes = {LanguageMode::CXX17, LanguageMode::CXX20};
+break;
+  case LanguageMode::CXX20OrLater:
+LangModes = {LanguageMode::CXX20};
+  };
+
+  for (auto Mode : LangModes) {
+auto LangModeArg = LangModeStrings[static_cast(Mode)];
+auto Result =
+matchesConditionally(Code, AMatcher, ExpectMatch, LangModeArg);
+if (!Result) {
+  return Result;
+}
+  }
+
+  return testing::AssertionSuccess();
 }
 
 template 
-testing::AssertionResult notMatches(const std::string ,
-const T ) {
-  return matchesConditionally(Code, AMatcher, false, "-std=c++11");
+testing::AssertionResult
+matches(const std::string , const T ,
+const LanguageMode  = LanguageMode::CXX11) {
+  return matchesConditionally(Code, AMatcher, true, Modes);
+}
+
+template 
+testing::AssertionResult
+notMatches(const std::string , const T ,
+   const LanguageMode  = LanguageMode::CXX11) {
+  return matchesConditionally(Code, AMatcher, false, Modes);
 }
 
 template 
Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
===
--- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -566,6 +566,73 @@
llvm::make_unique>("x")));
 }
 
+TEST(Matcher, IgnoresElidableConstructors) {
+  auto matcher1 = cxxOperatorCallExpr(hasArgument(
+  1,
+  callExpr(hasArgument(0, ignoringElidableConstructorCall(callExpr());
+  auto matcher2 = cxxOperatorCallExpr(
+  hasArgument(1, callExpr(hasArgument(0, ignoringElidableConstructorCall(
+ integerLiteral());
+  auto matcher3 = varDecl(hasInitializer(anyOf(
+  ignoringElidableConstructorCall(callExpr()),
+  exprWithCleanups(has(ignoringElidableConstructorCall(callExpr()));
+
+  auto code1 = "struct H {};"
+   "template H B(T A);"
+   "void f() {"
+   "  H D1;"
+   "  D1 = B(B(1));"
+   "}";
+  auto code2 = "struct H {};"
+   "template H B(T A);"
+   "void f() {"
+   "  H D1;"
+   "  D1 = B(1);"
+   "}";
+  auto code3 = "struct H {};"
+   "H G();"
+   "void f() {"
+   "  H D = G();"
+   "}";
+
+  EXPECT_TRUE(matches(code1, matcher1, LanguageMode::CXX11OrLater));
+  EXPECT_TRUE(matches(code2, matcher2, LanguageMode::CXX11OrLater));
+  EXPECT_TRUE(matches(code3, matcher3, LanguageMode::CXX11OrLater));
+}
+
+TEST(Matcher, IgnoresElidableInReturn) {
+  auto matcher = expr(ignoringElidableConstructorCall(declRefExpr()));
+  EXPECT_TRUE(matches("struct H {};"
+  "H f() {"
+  "  H g;"
+  "  return g;"
+  "}",

[PATCH] D63149: Added AST matcher for ignoring elidable constructors

2019-06-12 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 204269.
jvikstrom marked 3 inline comments as done.
jvikstrom added a comment.

- Added match conditionally overload to control in what language standard a 
match should run in


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63149

Files:
  clang/docs/LibASTMatchersReference.html
  clang/include/clang/ASTMatchers/ASTMatchers.h
  clang/lib/ASTMatchers/Dynamic/Registry.cpp
  clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
  clang/unittests/ASTMatchers/ASTMatchersTest.h

Index: clang/unittests/ASTMatchers/ASTMatchersTest.h
===
--- clang/unittests/ASTMatchers/ASTMatchersTest.h
+++ clang/unittests/ASTMatchers/ASTMatchersTest.h
@@ -57,6 +57,17 @@
   const std::unique_ptr FindResultReviewer;
 };
 
+enum class LanguageMode : int {
+  CXX11 = 0,
+  CXX14 = 1,
+  CXX17 = 2,
+  CXX20 = 3,
+  CXX11OrLater,
+  CXX14OrLater,
+  CXX17OrLater,
+  CXX20OrLater
+};
+
 template 
 testing::AssertionResult matchesConditionally(
 const std::string , const T , bool ExpectMatch,
@@ -116,14 +127,56 @@
 }
 
 template 
-testing::AssertionResult matches(const std::string , const T ) {
-  return matchesConditionally(Code, AMatcher, true, "-std=c++11");
+testing::AssertionResult
+matchesConditionally(const std::string , const T ,
+ bool ExpectMatch, const LanguageMode ) {
+  std::vector LangModeStrings{"-std=c++11", "-std=c++14",
+   "-std=c++17", "-std=c++20"};
+  std::vector LangModes;
+  switch (Mode) {
+  case LanguageMode::CXX11:
+  case LanguageMode::CXX14:
+  case LanguageMode::CXX17:
+  case LanguageMode::CXX20:
+LangModes = {Mode};
+break;
+  case LanguageMode::CXX11OrLater:
+LangModes = {LanguageMode::CXX11, LanguageMode::CXX14, LanguageMode::CXX17, LanguageMode::CXX20};
+break;
+  case LanguageMode::CXX14OrLater:
+LangModes = {LanguageMode::CXX14, LanguageMode::CXX17, LanguageMode::CXX20};
+break;
+  case LanguageMode::CXX17OrLater:
+LangModes = {LanguageMode::CXX17, LanguageMode::CXX20};
+break;
+  case LanguageMode::CXX20OrLater:
+LangModes = {LanguageMode::CXX20};
+  };
+
+  for (auto Mode : LangModes) {
+auto LangModeArg = LangModeStrings[static_cast(Mode)];
+auto Result =
+matchesConditionally(Code, AMatcher, ExpectMatch, LangModeArg);
+if (!Result) {
+  return Result;
+}
+  }
+
+  return testing::AssertionSuccess();
 }
 
 template 
-testing::AssertionResult notMatches(const std::string ,
-const T ) {
-  return matchesConditionally(Code, AMatcher, false, "-std=c++11");
+testing::AssertionResult
+matches(const std::string , const T ,
+const LanguageMode  = LanguageMode::CXX11) {
+  return matchesConditionally(Code, AMatcher, true, Modes);
+}
+
+template 
+testing::AssertionResult
+notMatches(const std::string , const T ,
+   const LanguageMode  = LanguageMode::CXX11) {
+  return matchesConditionally(Code, AMatcher, false, Modes);
 }
 
 template 
Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
===
--- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -566,6 +566,76 @@
llvm::make_unique>("x")));
 }
 
+TEST(Matcher, IgnoresElidableConstructors) {
+  auto matcher1 = cxxOperatorCallExpr(hasArgument(
+  1, callExpr(hasArgument(
+ 0, ignoringElidableConstructorCall(callExpr());
+  auto matcher2 = cxxOperatorCallExpr(hasArgument(
+  1, callExpr(hasArgument(0, ignoringElidableConstructorCall(
+ integerLiteral());
+  auto matcher3 =
+  varDecl(hasInitializer(
+anyOf(
+ignoringElidableConstructorCall(callExpr()),
+exprWithCleanups(has(ignoringElidableConstructorCall(callExpr(
+)));
+
+  auto code1 = "struct H {};"
+   "template H B(T A);"
+   "void f() {"
+   "  H D1;"
+   "  D1 = B(B(1));"
+   "}";
+  auto code2 = "struct H {};"
+   "template H B(T A);"
+   "void f() {"
+   "  H D1;"
+   "  D1 = B(1);"
+   "}";
+  auto code3 = "struct H {};"
+   "H G();"
+   "void f() {"
+   "  H D = G();"
+   "}";
+
+  EXPECT_TRUE(matches(code1, matcher1, LanguageMode::CXX11OrLater));
+  EXPECT_TRUE(matches(code2, matcher2, LanguageMode::CXX11OrLater));
+  EXPECT_TRUE(matches(code3, matcher3, LanguageMode::CXX11OrLater));
+}
+
+TEST(Matcher, IgnoresElidableInReturn) {
+  auto matcher = expr(ignoringElidableConstructorCall(declRefExpr()));
+  EXPECT_TRUE(matches("struct H{};"
+  "H f() {"
+ 

[PATCH] D63149: Added AST matcher for ignoring elidable constructors

2019-06-12 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 204282.
jvikstrom marked 7 inline comments as done.
jvikstrom added a comment.

- Using switch for choosing language standard


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63149

Files:
  clang/docs/LibASTMatchersReference.html
  clang/include/clang/ASTMatchers/ASTMatchers.h
  clang/lib/ASTMatchers/Dynamic/Registry.cpp
  clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
  clang/unittests/ASTMatchers/ASTMatchersTest.h

Index: clang/unittests/ASTMatchers/ASTMatchersTest.h
===
--- clang/unittests/ASTMatchers/ASTMatchersTest.h
+++ clang/unittests/ASTMatchers/ASTMatchersTest.h
@@ -13,6 +13,7 @@
 #include "clang/Frontend/ASTUnit.h"
 #include "clang/Tooling/Tooling.h"
 #include "gtest/gtest.h"
+#include 
 
 namespace clang {
 namespace ast_matchers {
@@ -57,6 +58,17 @@
   const std::unique_ptr FindResultReviewer;
 };
 
+enum class LanguageMode {
+  CXX11,
+  CXX14,
+  CXX17,
+  CXX2A,
+  CXX11OrLater,
+  CXX14OrLater,
+  CXX17OrLater,
+  CXX2AOrLater
+};
+
 template 
 testing::AssertionResult matchesConditionally(
 const std::string , const T , bool ExpectMatch,
@@ -116,14 +128,73 @@
 }
 
 template 
-testing::AssertionResult matches(const std::string , const T ) {
-  return matchesConditionally(Code, AMatcher, true, "-std=c++11");
+testing::AssertionResult
+matchesConditionally(const std::string , const T ,
+ bool ExpectMatch, const LanguageMode ) {
+  std::vector LangModes;
+  switch (Mode) {
+  case LanguageMode::CXX11:
+  case LanguageMode::CXX14:
+  case LanguageMode::CXX17:
+  case LanguageMode::CXX2A:
+LangModes = {Mode};
+break;
+  case LanguageMode::CXX11OrLater:
+LangModes = {LanguageMode::CXX11, LanguageMode::CXX14, LanguageMode::CXX17,
+ LanguageMode::CXX2A};
+break;
+  case LanguageMode::CXX14OrLater:
+LangModes = {LanguageMode::CXX14, LanguageMode::CXX17, LanguageMode::CXX2A};
+break;
+  case LanguageMode::CXX17OrLater:
+LangModes = {LanguageMode::CXX17, LanguageMode::CXX2A};
+break;
+  case LanguageMode::CXX2AOrLater:
+LangModes = {LanguageMode::CXX2A};
+  }
+
+  for (auto Mode : LangModes) {
+std::string LangModeArg;
+switch (Mode) {
+case LanguageMode::CXX11OrLater:
+case LanguageMode::CXX11:
+  LangModeArg = "-std=c++11";
+  break;
+case LanguageMode::CXX14OrLater:
+case LanguageMode::CXX14:
+  LangModeArg = "-std=c++14";
+  break;
+case LanguageMode::CXX17OrLater:
+case LanguageMode::CXX17:
+  LangModeArg = "-std=c++17";
+  break;
+case LanguageMode::CXX2AOrLater:
+case LanguageMode::CXX2A:
+  LangModeArg = "-std=c++2a";
+}
+
+auto Result =
+matchesConditionally(Code, AMatcher, ExpectMatch, LangModeArg);
+if (!Result) {
+  return Result;
+}
+  }
+
+  return testing::AssertionSuccess();
 }
 
 template 
-testing::AssertionResult notMatches(const std::string ,
-const T ) {
-  return matchesConditionally(Code, AMatcher, false, "-std=c++11");
+testing::AssertionResult
+matches(const std::string , const T ,
+const LanguageMode  = LanguageMode::CXX11) {
+  return matchesConditionally(Code, AMatcher, true, Modes);
+}
+
+template 
+testing::AssertionResult
+notMatches(const std::string , const T ,
+   const LanguageMode  = LanguageMode::CXX11) {
+  return matchesConditionally(Code, AMatcher, false, Modes);
 }
 
 template 
Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
===
--- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -566,6 +566,74 @@
llvm::make_unique>("x")));
 }
 
+TEST(Matcher, IgnoresElidableConstructors) {
+  EXPECT_TRUE(
+  matches("struct H {};"
+  "template H B(T A);"
+  "void f() {"
+  "  H D1;"
+  "  D1 = B(B(1));"
+  "}",
+  cxxOperatorCallExpr(hasArgument(
+  1, callExpr(hasArgument(
+ 0, ignoringElidableConstructorCall(callExpr()),
+  LanguageMode::CXX11OrLater));
+  EXPECT_TRUE(
+  matches("struct H {};"
+  "template H B(T A);"
+  "void f() {"
+  "  H D1;"
+  "  D1 = B(1);"
+  "}",
+  cxxOperatorCallExpr(hasArgument(
+  1, callExpr(hasArgument(0, ignoringElidableConstructorCall(
+ integerLiteral()),
+  LanguageMode::CXX11OrLater));
+  EXPECT_TRUE(matches(
+  "struct H {};"
+  "H G();"
+  "void f() {"
+  "  H D = G();"
+  "}",
+  varDecl(hasInitializer(anyOf(
+  

[PATCH] D63149: Added AST matcher for ignoring elidable constructors

2019-06-13 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 204437.
jvikstrom marked an inline comment as done.
jvikstrom added a comment.

- Added default to switch for language modes


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63149

Files:
  clang/docs/LibASTMatchersReference.html
  clang/include/clang/ASTMatchers/ASTMatchers.h
  clang/lib/ASTMatchers/Dynamic/Registry.cpp
  clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
  clang/unittests/ASTMatchers/ASTMatchersTest.h

Index: clang/unittests/ASTMatchers/ASTMatchersTest.h
===
--- clang/unittests/ASTMatchers/ASTMatchersTest.h
+++ clang/unittests/ASTMatchers/ASTMatchersTest.h
@@ -13,6 +13,7 @@
 #include "clang/Frontend/ASTUnit.h"
 #include "clang/Tooling/Tooling.h"
 #include "gtest/gtest.h"
+#include 
 
 namespace clang {
 namespace ast_matchers {
@@ -57,6 +58,17 @@
   const std::unique_ptr FindResultReviewer;
 };
 
+enum class LanguageMode {
+  Cxx11,
+  Cxx14,
+  Cxx17,
+  Cxx2a,
+  Cxx11OrLater,
+  Cxx14OrLater,
+  Cxx17OrLater,
+  Cxx2aOrLater
+};
+
 template 
 testing::AssertionResult matchesConditionally(
 const std::string , const T , bool ExpectMatch,
@@ -116,14 +128,72 @@
 }
 
 template 
-testing::AssertionResult matches(const std::string , const T ) {
-  return matchesConditionally(Code, AMatcher, true, "-std=c++11");
+testing::AssertionResult
+matchesConditionally(const std::string , const T ,
+ bool ExpectMatch, const LanguageMode ) {
+  std::vector LangModes;
+  switch (Mode) {
+  case LanguageMode::Cxx11:
+  case LanguageMode::Cxx14:
+  case LanguageMode::Cxx17:
+  case LanguageMode::Cxx2a:
+LangModes = {Mode};
+break;
+  case LanguageMode::Cxx11OrLater:
+LangModes = {LanguageMode::Cxx11, LanguageMode::Cxx14, LanguageMode::Cxx17,
+ LanguageMode::Cxx2a};
+break;
+  case LanguageMode::Cxx14OrLater:
+LangModes = {LanguageMode::Cxx14, LanguageMode::Cxx17, LanguageMode::Cxx2a};
+break;
+  case LanguageMode::Cxx17OrLater:
+LangModes = {LanguageMode::Cxx17, LanguageMode::Cxx2a};
+break;
+  case LanguageMode::Cxx2aOrLater:
+LangModes = {LanguageMode::Cxx2a};
+  }
+
+  for (auto Mode : LangModes) {
+std::string LangModeArg;
+switch (Mode) {
+case LanguageMode::Cxx11:
+  LangModeArg = "-std=c++11";
+  break;
+case LanguageMode::Cxx14:
+  LangModeArg = "-std=c++14";
+  break;
+case LanguageMode::Cxx17:
+  LangModeArg = "-std=c++17";
+  break;
+case LanguageMode::Cxx2a:
+  LangModeArg = "-std=c++2a";
+  break;
+default:
+  llvm_unreachable("Invalid language mode");
+}
+
+auto Result =
+matchesConditionally(Code, AMatcher, ExpectMatch, LangModeArg);
+if (!Result) {
+  return Result;
+}
+  }
+
+  return testing::AssertionSuccess();
 }
 
 template 
-testing::AssertionResult notMatches(const std::string ,
-const T ) {
-  return matchesConditionally(Code, AMatcher, false, "-std=c++11");
+testing::AssertionResult
+matches(const std::string , const T ,
+const LanguageMode  = LanguageMode::Cxx11) {
+  return matchesConditionally(Code, AMatcher, true, Modes);
+}
+
+template 
+testing::AssertionResult
+notMatches(const std::string , const T ,
+   const LanguageMode  = LanguageMode::Cxx11) {
+  return matchesConditionally(Code, AMatcher, false, Modes);
 }
 
 template 
Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
===
--- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -566,6 +566,74 @@
llvm::make_unique>("x")));
 }
 
+TEST(Matcher, IgnoresElidableConstructors) {
+  EXPECT_TRUE(
+  matches("struct H {};"
+  "template H B(T A);"
+  "void f() {"
+  "  H D1;"
+  "  D1 = B(B(1));"
+  "}",
+  cxxOperatorCallExpr(hasArgument(
+  1, callExpr(hasArgument(
+ 0, ignoringElidableConstructorCall(callExpr()),
+  LanguageMode::Cxx11OrLater));
+  EXPECT_TRUE(
+  matches("struct H {};"
+  "template H B(T A);"
+  "void f() {"
+  "  H D1;"
+  "  D1 = B(1);"
+  "}",
+  cxxOperatorCallExpr(hasArgument(
+  1, callExpr(hasArgument(0, ignoringElidableConstructorCall(
+ integerLiteral()),
+  LanguageMode::Cxx11OrLater));
+  EXPECT_TRUE(matches(
+  "struct H {};"
+  "H G();"
+  "void f() {"
+  "  H D = G();"
+  "}",
+  varDecl(hasInitializer(anyOf(
+  ignoringElidableConstructorCall(callExpr()),
+  

[PATCH] D63559: [clang-tidy] Added functionality for getting semantic highlights for variable and function declarations

2019-06-19 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 205608.
jvikstrom added a comment.
Herald added a subscriber: mgorny.

Adds CMakeLists changes


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,76 @@
+//===-- SemanticSymbolASTCollectorTests.cpp - SemanticSymbolASTCollector 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.h"
+#include "gtest/gtest.h"
+
+namespace clang {
+namespace clangd {
+namespace {
+
+using ::testing::ElementsAreArray;
+
+Position createPosition(int Line, int Character) {
+  Position Pos;
+  Pos.character = Character;
+  Pos.line = Line;
+  return Pos;
+}
+
+TEST(SemanticSymbolASTCollector, GetBeginningOfIdentifier) {
+  std::string Preamble = R"cpp(
+struct A {
+  double SomeMember;
+};
+void $foo[[foo]](int $a[[a]]) {
+  auto $vlvn[[VeryLongVariableName]] = 12312;
+  A $aa[[aa]];
+}
+  )cpp";
+
+  Annotations Test(Preamble);
+  auto Foo = Test.range("foo");
+  auto A = Test.range("a");
+  auto VeryLong = Test.range("vlvn");
+  auto AA = Test.range("aa");
+  std::vector CorrectLines = std::vector{
+  LineHighlight(
+  Foo.start.line,
+  {SemanticSymbol(SemanticScope::FunctionDeclaration,
+  createPosition(Foo.start.line, Foo.start.character),
+  3),
+   SemanticSymbol(SemanticScope::VariableDeclaration,
+  createPosition(A.start.line, A.start.character), 1)}),
+  LineHighlight(
+  VeryLong.start.line,
+  {SemanticSymbol(
+  SemanticScope::VariableDeclaration,
+  createPosition(VeryLong.start.line, VeryLong.start.character),
+  VeryLong.end.character - VeryLong.start.character)}),
+  LineHighlight(
+  AA.start.line,
+  {SemanticSymbol(SemanticScope::VariableDeclaration,
+  createPosition(AA.start.line, AA.start.character),
+  2)})};
+
+  auto AST = TestTU::withCode(Test.code()).build();
+  auto Lines = getASTHighlights(AST.getASTContext());
+  EXPECT_THAT(Lines, ElementsAreArray(CorrectLines));
+}
+
+} // 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,67 @@
+//===--- 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
+//
+//===--===//
+//
+// Code for collecting semantic symbols from the C++ AST using the
+// RecursiveASTVisitor
+//
+//===--===//
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICSYMBOLASTCOLLECTOR_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICSYMBOLASTCOLLECTOR_H
+
+#include "AST.h"
+#include "Headers.h"
+#include "Protocol.h"
+#include "clang/AST/RecursiveASTVisitor.h"
+#include "clang/Lex/Lexer.h"
+
+namespace clang {
+namespace clangd {
+
+// ScopeIndex represents the mapping from the scopes list to a type of
+// expression.
+enum class SemanticScope : int {
+  VariableDeclaration = 0,
+  FunctionDeclaration = 1,
+};
+
+// Contains all information needed for the highlighting a symbol.

[PATCH] D63559: [clang-tidy] Added functionality for getting semantic highlights for variable and function declarations

2019-06-19 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 205626.
jvikstrom added a comment.

Renamed SemanticSymbol to SemanticToken


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,76 @@
+//===-- SemanticSymbolASTCollectorTests.cpp - SemanticSymbolASTCollector 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.h"
+#include "gtest/gtest.h"
+
+namespace clang {
+namespace clangd {
+namespace {
+
+using ::testing::ElementsAreArray;
+
+Position createPosition(int Line, int Character) {
+  Position Pos;
+  Pos.character = Character;
+  Pos.line = Line;
+  return Pos;
+}
+
+TEST(SemanticSymbolASTCollector, GetBeginningOfIdentifier) {
+  std::string Preamble = R"cpp(
+struct A {
+  double SomeMember;
+};
+void $foo[[foo]](int $a[[a]]) {
+  auto $vlvn[[VeryLongVariableName]] = 12312;
+  A $aa[[aa]];
+}
+  )cpp";
+
+  Annotations Test(Preamble);
+  auto Foo = Test.range("foo");
+  auto A = Test.range("a");
+  auto VeryLong = Test.range("vlvn");
+  auto AA = Test.range("aa");
+  std::vector CorrectLines = std::vector{
+  LineHighlight(
+  Foo.start.line,
+  {SemanticToken(SemanticScope::FunctionDeclaration,
+  createPosition(Foo.start.line, Foo.start.character),
+  3),
+   SemanticToken(SemanticScope::VariableDeclaration,
+  createPosition(A.start.line, A.start.character), 1)}),
+  LineHighlight(
+  VeryLong.start.line,
+  {SemanticToken(
+  SemanticScope::VariableDeclaration,
+  createPosition(VeryLong.start.line, VeryLong.start.character),
+  VeryLong.end.character - VeryLong.start.character)}),
+  LineHighlight(
+  AA.start.line,
+  {SemanticToken(SemanticScope::VariableDeclaration,
+  createPosition(AA.start.line, AA.start.character),
+  2)})};
+
+  auto AST = TestTU::withCode(Test.code()).build();
+  auto Lines = getASTHighlights(AST.getASTContext());
+  EXPECT_THAT(Lines, ElementsAreArray(CorrectLines));
+}
+
+} // 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,67 @@
+//===--- 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
+//
+//===--===//
+//
+// Code for collecting semantic symbols from the C++ AST using the
+// RecursiveASTVisitor
+//
+//===--===//
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICSYMBOLASTCOLLECTOR_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICSYMBOLASTCOLLECTOR_H
+
+#include "AST.h"
+#include "Headers.h"
+#include "Protocol.h"
+#include "clang/AST/RecursiveASTVisitor.h"
+#include "clang/Lex/Lexer.h"
+
+namespace clang {
+namespace clangd {
+
+// ScopeIndex represents the mapping from the scopes list to a type of
+// expression.
+enum class SemanticScope : int {
+  VariableDeclaration = 0,
+  FunctionDeclaration = 1,
+};
+
+// Contains all information needed for the highlighting a symbol.
+struct SemanticToken {

[PATCH] D63559: [clang-tidy] Added functionality for getting semantic highlights for variable and function declarations

2019-06-19 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom created this revision.
jvikstrom added reviewers: hokein, ilya-biryukov.
Herald added subscribers: cfe-commits, kadircet, arphaman, jkorous, xazax.hun.
Herald added a project: clang.

Adds functionality for getting semantic highlights


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D63559

Files:
  clang-tools-extra/clangd/SemanticHighlight.cpp
  clang-tools-extra/clangd/SemanticHighlight.h
  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,76 @@
+//===-- SemanticSymbolASTCollectorTests.cpp - SemanticSymbolASTCollector 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.h"
+#include "gtest/gtest.h"
+
+namespace clang {
+namespace clangd {
+namespace {
+
+using ::testing::ElementsAreArray;
+
+Position createPosition(int Line, int Character) {
+  Position Pos;
+  Pos.character = Character;
+  Pos.line = Line;
+  return Pos;
+}
+
+TEST(SemanticSymbolASTCollector, GetBeginningOfIdentifier) {
+  std::string Preamble = R"cpp(
+struct A {
+  double SomeMember;
+};
+void $foo[[foo]](int $a[[a]]) {
+  auto $vlvn[[VeryLongVariableName]] = 12312;
+  A $aa[[aa]];
+}
+  )cpp";
+
+  Annotations Test(Preamble);
+  auto Foo = Test.range("foo");
+  auto A = Test.range("a");
+  auto VeryLong = Test.range("vlvn");
+  auto AA = Test.range("aa");
+  std::vector CorrectLines = std::vector{
+  LineHighlight(
+  Foo.start.line,
+  {SemanticSymbol(SemanticScope::FunctionDeclaration,
+  createPosition(Foo.start.line, Foo.start.character),
+  3),
+   SemanticSymbol(SemanticScope::VariableDeclaration,
+  createPosition(A.start.line, A.start.character), 1)}),
+  LineHighlight(
+  VeryLong.start.line,
+  {SemanticSymbol(
+  SemanticScope::VariableDeclaration,
+  createPosition(VeryLong.start.line, VeryLong.start.character),
+  VeryLong.end.character - VeryLong.start.character)}),
+  LineHighlight(
+  AA.start.line,
+  {SemanticSymbol(SemanticScope::VariableDeclaration,
+  createPosition(AA.start.line, AA.start.character),
+  2)})};
+
+  auto AST = TestTU::withCode(Test.code()).build();
+  auto Lines = getASTHighlights(AST.getASTContext());
+  EXPECT_THAT(Lines, ElementsAreArray(CorrectLines));
+}
+
+} // namespace
+} // namespace clangd
+} // namespace clang
Index: clang-tools-extra/clangd/SemanticHighlight.h
===
--- /dev/null
+++ clang-tools-extra/clangd/SemanticHighlight.h
@@ -0,0 +1,67 @@
+//===--- 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
+//
+//===--===//
+//
+// Code for collecting semantic symbols from the C++ AST using the
+// RecursiveASTVisitor
+//
+//===--===//
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICSYMBOLASTCOLLECTOR_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICSYMBOLASTCOLLECTOR_H
+
+#include "AST.h"
+#include "Headers.h"
+#include "Protocol.h"
+#include "clang/AST/RecursiveASTVisitor.h"
+#include "clang/Lex/Lexer.h"
+
+namespace clang {
+namespace clangd {
+
+// ScopeIndex represents the mapping from the scopes list to a type of
+// expression.
+enum class SemanticScope : int {
+  VariableDeclaration = 0,
+  FunctionDeclaration = 1,
+};
+
+// Contains all information needed for the highlighting a symbol.
+struct SemanticSymbol {
+  SemanticSymbol() {}
+  SemanticSymbol(SemanticScope Scope, Position StartPosition, unsigned int Len)
+  : Scope(Scope), StartPosition(StartPosition), Len(Len) {}
+  SemanticScope Scope;
+  Position StartPosition;
+  unsigned int Len;
+};
+
+bool operator==(const SemanticSymbol , const SemanticSymbol );
+bool operator!=(const SemanticSymbol , const SemanticSymbol );
+
+// Contains all highlights in a single line.
+struct 

[PATCH] D63559: [clang-tidy] Added functionality for getting semantic highlights for variable and function declarations

2019-06-24 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 206178.
jvikstrom marked 18 inline comments as done.
jvikstrom added a comment.

Removed LSP specific stuff. Changed a bunch of types and names. Added fixme for 
macro expansion.


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,55 @@
+//===- 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.h"
+#include "gtest/gtest.h"
+
+namespace clang {
+namespace clangd {
+namespace {
+
+using ::testing::ElementsAreArray;
+
+TEST(SemanticSymbolASTCollector, GetBeginningOfIdentifier) {
+  std::string Preamble = R"cpp(
+struct A {
+  double SomeMember;
+};
+void $foo[[foo]](int $a[[a]]) {
+  auto $vlvn[[VeryLongVariableName]] = 12312;
+  A $aa[[aa]];
+}
+  )cpp";
+
+  Annotations Test(Preamble);
+  auto Foo = Test.range("foo");
+  auto A = Test.range("a");
+  auto VeryLong = Test.range("vlvn");
+  auto AA = Test.range("aa");
+  std::vector CorrectTokens = std::vector{
+  SemanticToken(SemanticHighlightKind::Function, Foo),
+  SemanticToken(SemanticHighlightKind::Variable, A),
+  SemanticToken(SemanticHighlightKind::Variable, VeryLong),
+  SemanticToken(SemanticHighlightKind::Variable, AA)};
+
+  auto AST = TestTU::withCode(Test.code()).build();
+  auto Tokens = getSemanticHighlights(AST);
+
+  EXPECT_THAT(Tokens, ElementsAreArray(CorrectTokens));
+}
+
+} // 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,46 @@
+//==-- 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
+//
+//===--===//
+//
+// Code for collecting semantic symbols from the C++ AST using the
+// RecursiveASTVisitor
+//
+//===--===//
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICHIGHLIGHT_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICHIGHLIGHT_H
+
+#include "ClangdUnit.h"
+#include "Protocol.h"
+
+namespace clang {
+namespace clangd {
+
+enum class SemanticHighlightKind : int {
+  Variable = 0,
+  Function = 1,
+};
+
+// Contains all information needed for the highlighting a symbol.
+struct SemanticToken {
+  SemanticToken() = default;
+  SemanticToken(SemanticHighlightKind Kind, Range R) : Kind(Kind), R(R) {}
+  SemanticHighlightKind Kind;
+  Range R;
+};
+
+bool operator==(const SemanticToken , const SemanticToken );
+bool operator!=(const SemanticToken , const SemanticToken );
+
+// Returns semantic highlights for the AST. The vector is ordered in ascending
+// order by the line number. Every symbol in LineHighlight is ordered in
+// ascending order by their coumn number.
+std::vector getSemanticHighlights(ParsedAST );
+
+} // namespace clangd
+} // namespace clang
+
+#endif
Index: clang-tools-extra/clangd/SemanticHighlight.cpp
===
--- /dev/null
+++ clang-tools-extra/clangd/SemanticHighlight.cpp
@@ -0,0 +1,79 @@
+#include "SemanticHighlight.h"
+#include "SourceCode.h"
+#include "clang/AST/ASTContext.h"
+#include 

[PATCH] D63559: [clang-tidy] Added functionality for getting semantic highlights for variable and function declarations

2019-06-24 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom marked 2 inline comments as done.
jvikstrom added inline comments.



Comment at: clang-tools-extra/clangd/SemanticHighlight.cpp:129
+std::vector> getSemanticScopes() {
+  return {{"variable"}, {"entity.name.function"}};
+}

hokein wrote:
> This is Textmate-specific, I think we should lift it to a new File 
> (TextMate.cpp). We can do it in a separate patch.
> 
> I'd use a map to explicitly express the relationship between `SemanticScope` 
> and TextMate scope, the current implementation is not obvious.
> 
> ```
> {SemanticScope::Function, "entity.name.function"},
> {SemanticScope::Variable, "variable.other"},
> ```
Will do this in CL when adding C++ ClangdServer api calls


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63559



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D63559: [clang-tidy] Added functionality for getting semantic highlights for variable and function declarations

2019-06-24 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 206247.
jvikstrom marked 7 inline comments as done.
jvikstrom added a comment.

Fixed tests and edge case with function declarations without names for 
parameters.


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 Tokens,
+   std::vector ExpectedRanges,
+   SemanticHighlightKind Kind) {
+  std::vector ActualRanges;
+  for (SemanticToken Token : Tokens) {
+if (Token.Kind == Kind) {
+  ActualRanges.push_back(Token.R);
+}
+  }
+
+  EXPECT_THAT(ActualRanges, testing::UnorderedElementsAreArray(ExpectedRanges));
+}
+
+TEST(SemanticSymbolASTCollector, 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 = getSemanticHighlights(AST);
+
+  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,37 @@
+//==-- 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"
+
+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;
+};
+
+bool operator==(const SemanticToken , const SemanticToken );
+bool operator!=(const SemanticToken , const SemanticToken );
+
+// Returns semantic highlights for the AST. 
+std::vector getSemanticHighlights(ParsedAST );
+
+} // namespace clangd
+} // namespace clang
+
+#endif
Index: clang-tools-extra/clangd/SemanticHighlight.cpp
===
--- /dev/null
+++ clang-tools-extra/clangd/SemanticHighlight.cpp
@@ -0,0 +1,82 @@
+#include "SemanticHighlight.h"
+#include "SourceCode.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/RecursiveASTVisitor.h"
+#include "clang/Lex/Lexer.h"
+
+namespace clang {
+namespace clangd {
+namespace {
+
+// Collects all semantic tokens in an ASTContext.
+class SemanticSymbolASTCollector
+: public RecursiveASTVisitor {
+  std::vector 

[PATCH] D63559: [clang-tidy] Added functionality for getting semantic highlights for variable and function declarations

2019-06-24 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom added inline comments.



Comment at: clang-tools-extra/clangd/SemanticHighlight.cpp:75
+  SemanticSymbolASTCollector Collector(Ctx);
+  Collector.TraverseAST(Ctx);
+  return Collector.getSymbols();

hokein wrote:
> let's move the above lines into `SemanticSymbolASTCollector`, we can define a 
> new method "collectTokens()".
Should I expose the entire class or keep the getSemanticHighlights function?  
(I'm just thinking that RecursiveASTVisitor contains a lot of public functions 
which makes it not super obvious which function to call to get semantic 
highlight things when looking at autocomplete)


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63559



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D63479: Added SemanticSymbolASTCollector for collecting semantic symbolsCurrently collects variable and function declarations

2019-06-18 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom created this revision.
jvikstrom added a reviewer: hokein.
Herald added subscribers: cfe-commits, kadircet, arphaman, jkorous, 
ilya-biryukov, mgorny.
Herald added a project: clang.

[clangd] Added SemanticSymbolASTCollector that collects variable and function 
declarations


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D63479

Files:
  clang-tools-extra/clangd/CMakeLists.txt
  clang-tools-extra/clangd/SemanticSymbolASTCollector.cpp
  clang-tools-extra/clangd/SemanticSymbolASTCollector.h
  clang-tools-extra/clangd/unittests/CMakeLists.txt
  clang-tools-extra/clangd/unittests/SemanticSymbolASTCollectorTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticSymbolASTCollectorTests.cpp
===
--- /dev/null
+++ clang-tools-extra/clangd/unittests/SemanticSymbolASTCollectorTests.cpp
@@ -0,0 +1,64 @@
+//===-- SemanticSymbolASTCollectorTests.cpp - SemanticSymbolASTCollector 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 "SemanticSymbolASTCollector.h"
+#include "SourceCode.h"
+#include "TestTU.h"
+#include "llvm/Support/ScopedPrinter.h"
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+namespace clang {
+namespace clangd {
+namespace {
+
+using ::testing::ElementsAreArray;
+
+Position createPosition(int Line, int Character) {
+  Position Pos;
+  Pos.character = Character;
+  Pos.line = Line;
+  return Pos;
+}
+
+TEST(SemanticSymbolASTCollector, GetBeginningOfIdentifier) {
+  std::string Preamble = R"cpp(
+struct A {
+  double SomeMember;
+};
+void foo(int a) {
+  auto VeryLongVariableName = 12312;
+  A aa;
+}
+  )cpp";
+
+  Annotations TestCase(Preamble);
+  std::vector CorrectSymbols = std::vector{
+  SemanticSymbol(SemanticScope::FunctionDeclaration, createPosition(4, 9),
+ 3),
+  SemanticSymbol(SemanticScope::VariableDeclaration, createPosition(4, 17),
+ 1),
+  SemanticSymbol(SemanticScope::VariableDeclaration, createPosition(5, 11),
+ 20),
+  SemanticSymbol(SemanticScope::VariableDeclaration, createPosition(6, 12),
+ 2)};
+
+  auto AST = TestTU::withCode(TestCase.code()).build();
+  SemanticSymbolASTCollector Collector(AST.getASTContext());
+  Collector.TraverseAST(AST.getASTContext());
+  auto Symbols = Collector.getSymbols();
+  EXPECT_THAT(Symbols, ElementsAreArray(CorrectSymbols));
+}
+
+} // 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
+  SemanticSymbolASTCollectorTests.cpp
   SerializationTests.cpp
   SourceCodeTests.cpp
   SymbolCollectorTests.cpp
Index: clang-tools-extra/clangd/SemanticSymbolASTCollector.h
===
--- /dev/null
+++ clang-tools-extra/clangd/SemanticSymbolASTCollector.h
@@ -0,0 +1,91 @@
+//===--- SemanticSymbolASTCollector.h - Manipulating source code as strings
+//-*- 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
+//
+//===--===//
+//
+// Code for collecting semantic symbols from the C++ AST using the
+// RecursiveASTVisitor
+//
+//===--===//
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICSYMBOLASTCOLLECTOR_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICSYMBOLASTCOLLECTOR_H
+
+#include "AST.h"
+#include "Headers.h"
+#include "Protocol.h"
+#include "clang/AST/RecursiveASTVisitor.h"
+#include "clang/Lex/Lexer.h"
+
+namespace clang {
+namespace clangd {
+
+// ScopeIndex represents the mapping from the scopes list to a type of
+// expression
+enum class SemanticScope : int {
+  VariableDeclaration = 0,
+  FunctionDeclaration = 1,
+};
+
+// Holds all information needed for the highlighting
+struct SemanticSymbol {
+  SemanticSymbol() {}
+  SemanticSymbol(SemanticScope Scope, Position StartPosition, unsigned int Len)
+  : Scope(Scope), StartPosition(StartPosition), Len(Len) {}
+  SemanticScope Scope;
+  Position StartPosition;
+  unsigned int Len;
+};
+
+bool operator==(const SemanticSymbol , const SemanticSymbol 

[PATCH] D63479: Added SemanticSymbolASTCollector for collecting semantic symbolsCurrently collects variable and function declarations

2019-06-18 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 205275.
jvikstrom added a comment.

Added dots at the end of comments

Updating D63479: Added SemanticSymbolASTCollector for collecting semantic 
symbols
=

Currently collects variable and function declarations


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63479

Files:
  clang-tools-extra/clangd/CMakeLists.txt
  clang-tools-extra/clangd/SemanticSymbolASTCollector.cpp
  clang-tools-extra/clangd/SemanticSymbolASTCollector.h
  clang-tools-extra/clangd/unittests/CMakeLists.txt
  clang-tools-extra/clangd/unittests/SemanticSymbolASTCollectorTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticSymbolASTCollectorTests.cpp
===
--- /dev/null
+++ clang-tools-extra/clangd/unittests/SemanticSymbolASTCollectorTests.cpp
@@ -0,0 +1,64 @@
+//===-- SemanticSymbolASTCollectorTests.cpp - SemanticSymbolASTCollector 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 "SemanticSymbolASTCollector.h"
+#include "SourceCode.h"
+#include "TestTU.h"
+#include "llvm/Support/ScopedPrinter.h"
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+namespace clang {
+namespace clangd {
+namespace {
+
+using ::testing::ElementsAreArray;
+
+Position createPosition(int Line, int Character) {
+  Position Pos;
+  Pos.character = Character;
+  Pos.line = Line;
+  return Pos;
+}
+
+TEST(SemanticSymbolASTCollector, GetBeginningOfIdentifier) {
+  std::string Preamble = R"cpp(
+struct A {
+  double SomeMember;
+};
+void foo(int a) {
+  auto VeryLongVariableName = 12312;
+  A aa;
+}
+  )cpp";
+
+  Annotations TestCase(Preamble);
+  std::vector CorrectSymbols = std::vector{
+  SemanticSymbol(SemanticScope::FunctionDeclaration, createPosition(4, 9),
+ 3),
+  SemanticSymbol(SemanticScope::VariableDeclaration, createPosition(4, 17),
+ 1),
+  SemanticSymbol(SemanticScope::VariableDeclaration, createPosition(5, 11),
+ 20),
+  SemanticSymbol(SemanticScope::VariableDeclaration, createPosition(6, 12),
+ 2)};
+
+  auto AST = TestTU::withCode(TestCase.code()).build();
+  SemanticSymbolASTCollector Collector(AST.getASTContext());
+  Collector.TraverseAST(AST.getASTContext());
+  auto Symbols = Collector.getSymbols();
+  EXPECT_THAT(Symbols, ElementsAreArray(CorrectSymbols));
+}
+
+} // 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
+  SemanticSymbolASTCollectorTests.cpp
   SerializationTests.cpp
   SourceCodeTests.cpp
   SymbolCollectorTests.cpp
Index: clang-tools-extra/clangd/SemanticSymbolASTCollector.h
===
--- /dev/null
+++ clang-tools-extra/clangd/SemanticSymbolASTCollector.h
@@ -0,0 +1,91 @@
+//===--- SemanticSymbolASTCollector.h - Manipulating source code as strings
+//-*- 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
+//
+//===--===//
+//
+// Code for collecting semantic symbols from the C++ AST using the
+// RecursiveASTVisitor
+//
+//===--===//
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICSYMBOLASTCOLLECTOR_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICSYMBOLASTCOLLECTOR_H
+
+#include "AST.h"
+#include "Headers.h"
+#include "Protocol.h"
+#include "clang/AST/RecursiveASTVisitor.h"
+#include "clang/Lex/Lexer.h"
+
+namespace clang {
+namespace clangd {
+
+// ScopeIndex represents the mapping from the scopes list to a type of
+// expression.
+enum class SemanticScope : int {
+  VariableDeclaration = 0,
+  FunctionDeclaration = 1,
+};
+
+// Holds all information needed for the highlighting.
+struct SemanticSymbol {
+  SemanticSymbol() {}
+  SemanticSymbol(SemanticScope Scope, Position StartPosition, unsigned int Len)
+  : Scope(Scope), StartPosition(StartPosition), Len(Len) {}
+  SemanticScope Scope;
+ 

[PATCH] D63479: Added SemanticSymbolASTCollector for collecting semantic symbolsCurrently collects variable and function declarations

2019-06-18 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 205301.
jvikstrom added a comment.

Moved SemanticSymbolASTVisitor to an anonymous namespace and added a function 
for getting highlights that returns highlights line by line

Updating D63479: Added SemanticSymbolASTCollector for collecting semantic 
symbols
=

Currently collects variable and function declarations


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63479

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,64 @@
+//===-- SemanticSymbolASTCollectorTests.cpp - SemanticSymbolASTCollector 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.h"
+#include "gtest/gtest.h"
+
+namespace clang {
+namespace clangd {
+namespace {
+
+using ::testing::ElementsAreArray;
+
+Position createPosition(int Line, int Character) {
+  Position Pos;
+  Pos.character = Character;
+  Pos.line = Line;
+  return Pos;
+}
+
+TEST(SemanticSymbolASTCollector, GetBeginningOfIdentifier) {
+  std::string Preamble = R"cpp(
+struct A {
+  double SomeMember;
+};
+void foo(int a) {
+  auto VeryLongVariableName = 12312;
+  A aa;
+}
+  )cpp";
+
+  Annotations TestCase(Preamble);
+  std::vector CorrectLines = std::vector{
+  LineHighlight(4, {SemanticSymbol(SemanticScope::FunctionDeclaration,
+   createPosition(4, 9), 3),
+SemanticSymbol(SemanticScope::VariableDeclaration,
+   createPosition(4, 17), 1)}),
+  LineHighlight(5, {SemanticSymbol(SemanticScope::VariableDeclaration,
+   createPosition(5, 11), 20)}),
+  LineHighlight(6, {SemanticSymbol(SemanticScope::VariableDeclaration,
+   createPosition(6, 12), 2)})
+
+  };
+
+  auto AST = TestTU::withCode(TestCase.code()).build();
+  auto Lines = getASTHighlights(AST.getASTContext());
+  EXPECT_THAT(Lines, ElementsAreArray(CorrectLines));
+}
+
+} // 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,62 @@
+//===--- SemanticSymbolASTCollector.h - Manipulating source code as strings
+//-*- 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
+//
+//===--===//
+//
+// Code for collecting semantic symbols from the C++ AST using the
+// RecursiveASTVisitor
+//
+//===--===//
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICSYMBOLASTCOLLECTOR_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICSYMBOLASTCOLLECTOR_H
+
+#include "AST.h"
+#include "Headers.h"
+#include "Protocol.h"
+#include "clang/AST/RecursiveASTVisitor.h"
+#include "clang/Lex/Lexer.h"
+
+namespace clang {
+namespace clangd {
+
+// ScopeIndex represents the mapping from the scopes list to a type of
+// expression.
+enum class SemanticScope : int {
+  VariableDeclaration = 0,
+  FunctionDeclaration = 1,
+};
+
+// Contains all information needed for the highlighting a symbol.
+struct SemanticSymbol {
+  SemanticSymbol() {}
+  SemanticSymbol(SemanticScope Scope, Position StartPosition, unsigned int Len)

[PATCH] D63479: Added SemanticSymbolASTCollector for collecting semantic symbolsCurrently collects variable and function declarations

2019-06-18 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom abandoned this revision.
jvikstrom added a comment.

Resubmitting later with "complete" prototype in CL


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63479



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D63919: [clangd] Emit publishSemanticHighlighting in LSP if enabled

2019-07-03 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 207714.
jvikstrom added a comment.

Removed unused headers


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63919

Files:
  clang-tools-extra/clangd/ClangdLSPServer.cpp
  clang-tools-extra/clangd/ClangdLSPServer.h
  clang-tools-extra/clangd/Protocol.cpp
  clang-tools-extra/clangd/Protocol.h
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/SemanticHighlighting.h
  clang-tools-extra/clangd/test/initialize-params.test
  clang-tools-extra/clangd/test/semantic-highlighting.test
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -8,6 +8,7 @@
 
 #include "Annotations.h"
 #include "ClangdServer.h"
+#include "Protocol.h"
 #include "SemanticHighlighting.h"
 #include "TestFS.h"
 #include "TestTU.h"
@@ -66,7 +67,7 @@
   }
 }
 
-TEST(ClangdSemanticHighlightingTest, GeneratesHighlightsWhenFileChange) {
+TEST(SemanticHighlighting, GeneratesHighlightsWhenFileChange) {
   class HighlightingsCounterDiagConsumer : public DiagnosticsConsumer {
   public:
 std::atomic Count = {0};
@@ -90,6 +91,29 @@
   ASSERT_EQ(DiagConsumer.Count, 1);
 }
 
+TEST(SemanticHighlighting, toSemanticHighlightingInformation) {
+  auto CreatePosition = [](int Line, int Character) -> Position {
+Position Pos;
+Pos.line = Line;
+Pos.character = Character;
+return Pos;
+  };
+
+  std::vector Tokens{
+  {HighlightingKind::Variable,
+Range{CreatePosition(3, 8), CreatePosition(3, 12)}},
+  {HighlightingKind::Function,
+Range{CreatePosition(3, 4), CreatePosition(3, 7)}},
+  {HighlightingKind::Variable,
+Range{CreatePosition(1, 1), CreatePosition(1, 5)}}};
+  std::vector Info =
+  toSemanticHighlightingInformation(Tokens);
+  std::vector Correct = {
+  {1, "AQAEAAA="},
+  {3, "CAAEAAAEAAMAAQ=="}};
+  ASSERT_EQ(Info, Correct);
+}
+
 } // namespace
 } // namespace clangd
 } // namespace clang
Index: clang-tools-extra/clangd/test/semantic-highlighting.test
===
--- /dev/null
+++ clang-tools-extra/clangd/test/semantic-highlighting.test
@@ -0,0 +1,21 @@
+# RUN: clangd -lit-test < %s | FileCheck -strict-whitespace %s
+{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{"textDocument": {"semanticHighlightingCapabilities": {"semanticHighlighting": true}}},"trace":"off"}}
+---
+{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"uri":"test:///foo.cpp","languageId":"cpp","version":1,"text":"int x = 2;"}}}
+#  CHECK:  "method": "textDocument/semanticHighlighting",
+# CHECK-NEXT:  "params": {
+# CHECK-NEXT:"lines": [
+# CHECK-NEXT:  {
+# CHECK-NEXT:"line": 0,
+# CHECK-NEXT:"tokens": "BAABAAA="
+# CHECK-NEXT:  }
+# CHECK-NEXT:],
+# CHECK-NEXT:"textDocument": {
+# CHECK-NEXT:  "uri": "file:///clangd-test/foo.cpp"
+# CHECK-NEXT:}
+# CHECK-NEXT:  }
+# CHECK-NEXT:}
+---
+{"jsonrpc":"2.0","id":3,"method":"shutdown"}
+---
+{"jsonrpc":"2.0","method":"exit"}
Index: clang-tools-extra/clangd/test/initialize-params.test
===
--- clang-tools-extra/clangd/test/initialize-params.test
+++ clang-tools-extra/clangd/test/initialize-params.test
@@ -33,6 +33,16 @@
 # CHECK-NEXT:  "hoverProvider": true,
 # CHECK-NEXT:  "referencesProvider": true,
 # CHECK-NEXT:  "renameProvider": true,
+# CHECK-NEXT:  "semanticHighlighting": {
+# CHECK-NEXT:"scopes": [
+# CHECK-NEXT:  [
+# CHECK-NEXT:"variable.cpp"
+# CHECK-NEXT:  ],
+# CHECK-NEXT:  [
+# CHECK-NEXT:"entity.name.function.cpp"
+# CHECK-NEXT:  ]
+# CHECK-NEXT:]
+# CHECK-NEXT:  },
 # CHECK-NEXT:  "signatureHelpProvider": {
 # CHECK-NEXT:"triggerCharacters": [
 # CHECK-NEXT:  "(",
Index: clang-tools-extra/clangd/SemanticHighlighting.h
===
--- clang-tools-extra/clangd/SemanticHighlighting.h
+++ clang-tools-extra/clangd/SemanticHighlighting.h
@@ -4,19 +4,25 @@
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
+// An implementation of semantic highlighting based on this proposal:
+// https://github.com/microsoft/vscode-languageserver-node/pull/367 in clangd.
+// Semantic highlightings are calculated for an AST by visiting every AST node
+// and classifying nodes that are 

[PATCH] D64199: [clangd] Added highlighting for variable references (declrefs)

2019-07-04 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 208028.
jvikstrom added a comment.

Added overload for addToken and added more code to the test cases.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64199

Files:
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -28,7 +28,7 @@
 
   return Tokens;
 }
-
+#include 
 void checkHighlightings(llvm::StringRef Code) {
   Annotations Test(Code);
   auto AST = TestTU::withCode(Test.code()).build();
@@ -43,6 +43,14 @@
   }
 
   auto ActualTokens = getSemanticHighlightings(AST);
+  std::cout << "\n\n";
+  for(auto Tok : ExpectedTokens) {
+std::cout << "E:: " << Tok.R.start.line << ":" << Tok.R.start.character << std::endl;
+  }
+  for(auto Tok : ActualTokens) {
+std::cout << "A:: " << Tok.R.start.line << ":" << Tok.R.start.character << std::endl;
+  }
+
   EXPECT_THAT(ActualTokens, testing::UnorderedElementsAreArray(ExpectedTokens));
 }
 
@@ -57,10 +65,20 @@
 void $Function[[foo]](int $Variable[[a]]) {
   auto $Variable[[VeryLongVariableName]] = 12312;
   A $Variable[[aa]];
+  auto $Variable[[l]] = $Variable[[aa]].SomeMember + $Variable[[a]];
 }
   )cpp",
   R"cpp(
 void $Function[[foo]](int);
+  )cpp",
+  R"cpp(
+void $Function[[gah]]();
+void $Function[[foo]]() {
+  int $Variable[[b]];
+  auto $Variable[[FN]] = [ $Variable[[b]]](int $Variable[[a]]) -> void {};
+  $Variable[[FN]](12312);
+  auto $Variable[[bou]] = $Function[[gah]];
+}
   )cpp"};
   for (const auto  : TestCases) {
 checkHighlightings(TestCase);
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -35,25 +35,42 @@
   }
 
   bool VisitVarDecl(VarDecl *Var) {
-addToken(Var, HighlightingKind::Variable);
+addNamedDecl(Var);
 return true;
   }
   bool VisitFunctionDecl(FunctionDecl *Func) {
-addToken(Func, HighlightingKind::Function);
+addNamedDecl(Func);
+return true;
+  }
+
+  bool VisitDeclRefExpr(DeclRefExpr *Ref) {
+if (Ref->getNameInfo().getName().getNameKind() ==
+DeclarationName::CXXOperatorName)
+  // Don't want to highlight operator usages.
+  return true;
+
+addToken(Ref->getLocation(), Ref->getDecl());
 return true;
   }
 
 private:
-  void addToken(const NamedDecl *D, HighlightingKind Kind) {
-if (D->getLocation().isMacroID())
-  // FIXME: skip tokens inside macros for now.
+  void addToken(SourceLocation Loc, const Decl* D) {
+if(isa(D)) {
+  addToken(Loc, HighlightingKind::Variable);
   return;
+}
+if(isa(D)) {
+  addToken(Loc, HighlightingKind::Function);
+  return;
+}
+  }
 
-if (D->getDeclName().isEmpty())
-  // Don't add symbols that don't have any length.
+  void addToken(SourceLocation Loc, HighlightingKind Kind) {
+if (Loc.isMacroID())
+  // FIXME: skip tokens inside macros for now.
   return;
 
-auto R = getTokenRange(SM, Ctx.getLangOpts(), D->getLocation());
+auto R = getTokenRange(SM, Ctx.getLangOpts(), Loc);
 if (!R) {
   // R should always have a value, if it doesn't something is very wrong.
   elog("Tried to add semantic token with an invalid range");
@@ -62,6 +79,13 @@
 
 Tokens.push_back({Kind, R.getValue()});
   }
+
+  void addNamedDecl(const NamedDecl *D) {
+if (D->getDeclName().isEmpty())
+  // Don't add symbols that don't have any length.
+  return;
+addToken(D->getLocation(), D);
+  }
 };
 
 // Encode binary data into base64.
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D64199: [clangd] Added highlighting for variable references (declrefs)

2019-07-04 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 208030.
jvikstrom added a comment.

Removed debug prints from test.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64199

Files:
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp


Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -57,10 +57,20 @@
 void $Function[[foo]](int $Variable[[a]]) {
   auto $Variable[[VeryLongVariableName]] = 12312;
   A $Variable[[aa]];
+  auto $Variable[[l]] = $Variable[[aa]].SomeMember + $Variable[[a]];
 }
   )cpp",
   R"cpp(
 void $Function[[foo]](int);
+  )cpp",
+  R"cpp(
+void $Function[[gah]]();
+void $Function[[foo]]() {
+  int $Variable[[b]];
+  auto $Variable[[FN]] = [ $Variable[[b]]](int $Variable[[a]]) -> void {};
+  $Variable[[FN]](12312);
+  auto $Variable[[bou]] = $Function[[gah]];
+}
   )cpp"};
   for (const auto  : TestCases) {
 checkHighlightings(TestCase);
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -34,26 +34,42 @@
 return Tokens;
   }
 
-  bool VisitVarDecl(VarDecl *Var) {
-addToken(Var, HighlightingKind::Variable);
+  bool VisitNamedDecl(NamedDecl *ND) {
+if (ND->getDeclName().isEmpty())
+  // Don't add symbols that don't have any length.
+  return true;
+addToken(ND->getLocation(), ND);
 return true;
   }
-  bool VisitFunctionDecl(FunctionDecl *Func) {
-addToken(Func, HighlightingKind::Function);
+
+  bool VisitDeclRefExpr(DeclRefExpr *Ref) {
+if (Ref->getNameInfo().getName().getNameKind() ==
+DeclarationName::CXXOperatorName)
+  // Don't want to highlight operator usages.
+  return true;
+
+addToken(Ref->getLocation(), Ref->getDecl());
 return true;
   }
 
 private:
-  void addToken(const NamedDecl *D, HighlightingKind Kind) {
-if (D->getLocation().isMacroID())
-  // FIXME: skip tokens inside macros for now.
+  void addToken(SourceLocation Loc, const Decl* D) {
+if(isa(D)) {
+  addToken(Loc, HighlightingKind::Variable);
+  return;
+}
+if(isa(D)) {
+  addToken(Loc, HighlightingKind::Function);
   return;
+}
+  }
 
-if (D->getDeclName().isEmpty())
-  // Don't add symbols that don't have any length.
+  void addToken(SourceLocation Loc, HighlightingKind Kind) {
+if (Loc.isMacroID())
+  // FIXME: skip tokens inside macros for now.
   return;
 
-auto R = getTokenRange(SM, Ctx.getLangOpts(), D->getLocation());
+auto R = getTokenRange(SM, Ctx.getLangOpts(), Loc);
 if (!R) {
   // R should always have a value, if it doesn't something is very wrong.
   elog("Tried to add semantic token with an invalid range");


Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -57,10 +57,20 @@
 void $Function[[foo]](int $Variable[[a]]) {
   auto $Variable[[VeryLongVariableName]] = 12312;
   A $Variable[[aa]];
+  auto $Variable[[l]] = $Variable[[aa]].SomeMember + $Variable[[a]];
 }
   )cpp",
   R"cpp(
 void $Function[[foo]](int);
+  )cpp",
+  R"cpp(
+void $Function[[gah]]();
+void $Function[[foo]]() {
+  int $Variable[[b]];
+  auto $Variable[[FN]] = [ $Variable[[b]]](int $Variable[[a]]) -> void {};
+  $Variable[[FN]](12312);
+  auto $Variable[[bou]] = $Function[[gah]];
+}
   )cpp"};
   for (const auto  : TestCases) {
 checkHighlightings(TestCase);
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -34,26 +34,42 @@
 return Tokens;
   }
 
-  bool VisitVarDecl(VarDecl *Var) {
-addToken(Var, HighlightingKind::Variable);
+  bool VisitNamedDecl(NamedDecl *ND) {
+if (ND->getDeclName().isEmpty())
+  // Don't add symbols that don't have any length.
+  return true;
+addToken(ND->getLocation(), ND);
 return true;
   }
-  bool VisitFunctionDecl(FunctionDecl *Func) {
-addToken(Func, HighlightingKind::Function);
+
+  bool VisitDeclRefExpr(DeclRefExpr *Ref) {
+if (Ref->getNameInfo().getName().getNameKind() ==
+DeclarationName::CXXOperatorName)
+  // Don't want to 

[PATCH] D64199: [clangd] Added highlighting for variable references (declrefs)

2019-07-04 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom added inline comments.



Comment at: clang-tools-extra/clangd/SemanticHighlighting.cpp:38
+  bool VisitNamedDecl(NamedDecl *ND) {
+if (ND->getDeclName().isEmpty())
+  // Don't add symbols that don't have any length.

sammccall wrote:
> I think you might want to bail out (both here and in VisitDeclRefExpr) if the 
> name kind isn't identifier.
> 
> Reason is you're only coloring the token at location, and most of the other 
> name kinds can span multiple tokens or otherwise need special consideration.
I must have missed the Identifier NameKind because I was first-hand looking for 
something like that. 
Thanks.

Are you aware of any testcase I could add for this by the way?



Comment at: clang-tools-extra/clangd/SemanticHighlighting.cpp:61
+}
+if(isa(D)) {
+  addToken(Loc, HighlightingKind::Function);

sammccall wrote:
> note that methods, constructors, and destructors inherit from functiondecl, 
> so if you want to exclude/distinguish those, order matters here
I'm aware of that, but thanks for the heads up. Although should I add it in a 
comment somewhere in the method? Also added an additional testcase for classes 
and FIXMEs to the skip if statement in VisitNamedDecl.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64199



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D64199: [clangd] Added highlighting for variable references (declrefs)

2019-07-04 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 208041.
jvikstrom marked 2 inline comments as done.
jvikstrom added a comment.

Added testcae. Added another bailout from VisitNamedDecl.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64199

Files:
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp


Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -57,10 +57,31 @@
 void $Function[[foo]](int $Variable[[a]]) {
   auto $Variable[[VeryLongVariableName]] = 12312;
   A $Variable[[aa]];
+  auto $Variable[[l]] = $Variable[[aa]].SomeMember + $Variable[[a]];
 }
   )cpp",
   R"cpp(
 void $Function[[foo]](int);
+  )cpp",
+  R"cpp(
+void $Function[[gah]]();
+void $Function[[foo]]() {
+  int $Variable[[b]];
+  auto $Variable[[FN]] = [ $Variable[[b]]](int $Variable[[a]]) -> void {};
+  $Variable[[FN]](12312);
+  auto $Variable[[bou]] = $Function[[gah]];
+}
+  )cpp",
+  R"cpp(
+struct A {
+  A();
+  ~A();
+  void $Function[[abc]]();
+};
+void $Function[[foo]]() {
+  A $Variable[[a]];
+  $Variable[[a]].abc();
+}
   )cpp"};
   for (const auto  : TestCases) {
 checkHighlightings(TestCase);
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -34,26 +34,46 @@
 return Tokens;
   }
 
-  bool VisitVarDecl(VarDecl *Var) {
-addToken(Var, HighlightingKind::Variable);
+  bool VisitNamedDecl(NamedDecl *ND) {
+// FIXME: This also skips constructors and destructors.
+if (ND->getDeclName().getNameKind() != DeclarationName::Identifier)
+  return true;
+
+if (ND->getDeclName().isEmpty())
+  // Don't add symbols that don't have any length.
+  return true;
+addToken(ND->getLocation(), ND);
 return true;
   }
-  bool VisitFunctionDecl(FunctionDecl *Func) {
-addToken(Func, HighlightingKind::Function);
+
+  bool VisitDeclRefExpr(DeclRefExpr *Ref) {
+if (Ref->getNameInfo().getName().getNameKind() !=
+DeclarationName::Identifier)
+  // Only want to highlight identifiers.
+  return true;
+
+addToken(Ref->getLocation(), Ref->getDecl());
 return true;
   }
 
 private:
-  void addToken(const NamedDecl *D, HighlightingKind Kind) {
-if (D->getLocation().isMacroID())
-  // FIXME: skip tokens inside macros for now.
+  void addToken(SourceLocation Loc, const Decl *D) {
+if (isa(D)) {
+  addToken(Loc, HighlightingKind::Variable);
   return;
+}
+if (isa(D)) {
+  addToken(Loc, HighlightingKind::Function);
+  return;
+}
+  }
 
-if (D->getDeclName().isEmpty())
-  // Don't add symbols that don't have any length.
+  void addToken(SourceLocation Loc, HighlightingKind Kind) {
+if (Loc.isMacroID())
+  // FIXME: skip tokens inside macros for now.
   return;
 
-auto R = getTokenRange(SM, Ctx.getLangOpts(), D->getLocation());
+auto R = getTokenRange(SM, Ctx.getLangOpts(), Loc);
 if (!R) {
   // R should always have a value, if it doesn't something is very wrong.
   elog("Tried to add semantic token with an invalid range");


Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -57,10 +57,31 @@
 void $Function[[foo]](int $Variable[[a]]) {
   auto $Variable[[VeryLongVariableName]] = 12312;
   A $Variable[[aa]];
+  auto $Variable[[l]] = $Variable[[aa]].SomeMember + $Variable[[a]];
 }
   )cpp",
   R"cpp(
 void $Function[[foo]](int);
+  )cpp",
+  R"cpp(
+void $Function[[gah]]();
+void $Function[[foo]]() {
+  int $Variable[[b]];
+  auto $Variable[[FN]] = [ $Variable[[b]]](int $Variable[[a]]) -> void {};
+  $Variable[[FN]](12312);
+  auto $Variable[[bou]] = $Function[[gah]];
+}
+  )cpp",
+  R"cpp(
+struct A {
+  A();
+  ~A();
+  void $Function[[abc]]();
+};
+void $Function[[foo]]() {
+  A $Variable[[a]];
+  $Variable[[a]].abc();
+}
   )cpp"};
   for (const auto  : TestCases) {
 checkHighlightings(TestCase);
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ 

[PATCH] D64199: [clangd] Added highlighting for variable references (declrefs)

2019-07-04 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 208029.
jvikstrom added a comment.

Removed VisitVarDecl and VisitFuncDecl in favor of VisitNamedDecl.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64199

Files:
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -28,7 +28,7 @@
 
   return Tokens;
 }
-
+#include 
 void checkHighlightings(llvm::StringRef Code) {
   Annotations Test(Code);
   auto AST = TestTU::withCode(Test.code()).build();
@@ -43,6 +43,14 @@
   }
 
   auto ActualTokens = getSemanticHighlightings(AST);
+  std::cout << "\n\n";
+  for(auto Tok : ExpectedTokens) {
+std::cout << "E:: " << Tok.R.start.line << ":" << Tok.R.start.character << std::endl;
+  }
+  for(auto Tok : ActualTokens) {
+std::cout << "A:: " << Tok.R.start.line << ":" << Tok.R.start.character << std::endl;
+  }
+
   EXPECT_THAT(ActualTokens, testing::UnorderedElementsAreArray(ExpectedTokens));
 }
 
@@ -57,10 +65,20 @@
 void $Function[[foo]](int $Variable[[a]]) {
   auto $Variable[[VeryLongVariableName]] = 12312;
   A $Variable[[aa]];
+  auto $Variable[[l]] = $Variable[[aa]].SomeMember + $Variable[[a]];
 }
   )cpp",
   R"cpp(
 void $Function[[foo]](int);
+  )cpp",
+  R"cpp(
+void $Function[[gah]]();
+void $Function[[foo]]() {
+  int $Variable[[b]];
+  auto $Variable[[FN]] = [ $Variable[[b]]](int $Variable[[a]]) -> void {};
+  $Variable[[FN]](12312);
+  auto $Variable[[bou]] = $Function[[gah]];
+}
   )cpp"};
   for (const auto  : TestCases) {
 checkHighlightings(TestCase);
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -34,26 +34,42 @@
 return Tokens;
   }
 
-  bool VisitVarDecl(VarDecl *Var) {
-addToken(Var, HighlightingKind::Variable);
+  bool VisitNamedDecl(NamedDecl *ND) {
+if (ND->getDeclName().isEmpty())
+  // Don't add symbols that don't have any length.
+  return true;
+addToken(ND->getLocation(), ND);
 return true;
   }
-  bool VisitFunctionDecl(FunctionDecl *Func) {
-addToken(Func, HighlightingKind::Function);
+
+  bool VisitDeclRefExpr(DeclRefExpr *Ref) {
+if (Ref->getNameInfo().getName().getNameKind() ==
+DeclarationName::CXXOperatorName)
+  // Don't want to highlight operator usages.
+  return true;
+
+addToken(Ref->getLocation(), Ref->getDecl());
 return true;
   }
 
 private:
-  void addToken(const NamedDecl *D, HighlightingKind Kind) {
-if (D->getLocation().isMacroID())
-  // FIXME: skip tokens inside macros for now.
+  void addToken(SourceLocation Loc, const Decl* D) {
+if(isa(D)) {
+  addToken(Loc, HighlightingKind::Variable);
+  return;
+}
+if(isa(D)) {
+  addToken(Loc, HighlightingKind::Function);
   return;
+}
+  }
 
-if (D->getDeclName().isEmpty())
-  // Don't add symbols that don't have any length.
+  void addToken(SourceLocation Loc, HighlightingKind Kind) {
+if (Loc.isMacroID())
+  // FIXME: skip tokens inside macros for now.
   return;
 
-auto R = getTokenRange(SM, Ctx.getLangOpts(), D->getLocation());
+auto R = getTokenRange(SM, Ctx.getLangOpts(), Loc);
 if (!R) {
   // R should always have a value, if it doesn't something is very wrong.
   elog("Tried to add semantic token with an invalid range");
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D63919: [clangd] Emit publishSemanticHighlighting in LSP if enabled

2019-07-03 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 207758.
jvikstrom marked 8 inline comments as done.
jvikstrom added a comment.

Not sending TextMate scopes over LSP if semantic highlighting is not enabled.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63919

Files:
  clang-tools-extra/clangd/ClangdLSPServer.cpp
  clang-tools-extra/clangd/ClangdLSPServer.h
  clang-tools-extra/clangd/Protocol.cpp
  clang-tools-extra/clangd/Protocol.h
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/SemanticHighlighting.h
  clang-tools-extra/clangd/test/initialize-params.test
  clang-tools-extra/clangd/test/semantic-highlighting.test
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -8,6 +8,7 @@
 
 #include "Annotations.h"
 #include "ClangdServer.h"
+#include "Protocol.h"
 #include "SemanticHighlighting.h"
 #include "TestFS.h"
 #include "TestTU.h"
@@ -66,7 +67,7 @@
   }
 }
 
-TEST(ClangdSemanticHighlightingTest, GeneratesHighlightsWhenFileChange) {
+TEST(SemanticHighlighting, GeneratesHighlightsWhenFileChange) {
   class HighlightingsCounterDiagConsumer : public DiagnosticsConsumer {
   public:
 std::atomic Count = {0};
@@ -90,6 +91,29 @@
   ASSERT_EQ(DiagConsumer.Count, 1);
 }
 
+TEST(SemanticHighlighting, toSemanticHighlightingInformation) {
+  auto CreatePosition = [](int Line, int Character) -> Position {
+Position Pos;
+Pos.line = Line;
+Pos.character = Character;
+return Pos;
+  };
+
+  std::vector Tokens{
+  {HighlightingKind::Variable,
+Range{CreatePosition(3, 8), CreatePosition(3, 12)}},
+  {HighlightingKind::Function,
+Range{CreatePosition(3, 4), CreatePosition(3, 7)}},
+  {HighlightingKind::Variable,
+Range{CreatePosition(1, 1), CreatePosition(1, 5)}}};
+  std::vector ActualResults =
+  toSemanticHighlightingInformation(Tokens);
+  std::vector ExpectedResults = {
+  {1, "AQAEAAA="},
+  {3, "CAAEAAAEAAMAAQ=="}};
+  EXPECT_EQ(ActualResults, ExpectedResults);
+}
+
 } // namespace
 } // namespace clangd
 } // namespace clang
Index: clang-tools-extra/clangd/test/semantic-highlighting.test
===
--- /dev/null
+++ clang-tools-extra/clangd/test/semantic-highlighting.test
@@ -0,0 +1,21 @@
+# RUN: clangd -lit-test < %s | FileCheck -strict-whitespace %s
+{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{"textDocument":{"semanticHighlightingCapabilities":{"semanticHighlighting":true}}},"trace":"off"}}
+---
+{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"uri":"test:///foo.cpp","languageId":"cpp","version":1,"text":"int x = 2;"}}}
+#  CHECK:  "method": "textDocument/semanticHighlighting",
+# CHECK-NEXT:  "params": {
+# CHECK-NEXT:"lines": [
+# CHECK-NEXT:  {
+# CHECK-NEXT:"line": 0,
+# CHECK-NEXT:"tokens": "BAABAAA="
+# CHECK-NEXT:  }
+# CHECK-NEXT:],
+# CHECK-NEXT:"textDocument": {
+# CHECK-NEXT:  "uri": "file:///clangd-test/foo.cpp"
+# CHECK-NEXT:}
+# CHECK-NEXT:  }
+# CHECK-NEXT:}
+---
+{"jsonrpc":"2.0","id":3,"method":"shutdown"}
+---
+{"jsonrpc":"2.0","method":"exit"}
Index: clang-tools-extra/clangd/test/initialize-params.test
===
--- clang-tools-extra/clangd/test/initialize-params.test
+++ clang-tools-extra/clangd/test/initialize-params.test
@@ -1,6 +1,6 @@
 # RUN: clangd -lit-test < %s | FileCheck -strict-whitespace %s
 # Test initialize request parameters with rootUri
-{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootUri":"test:///workspace","capabilities":{},"trace":"off"}}
+{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootUri":"test:///workspace","capabilities":{"textDocument":{"semanticHighlightingCapabilities":{"semanticHighlighting":true}}},"trace":"off"}}
 #  CHECK:  "id": 0,
 # CHECK-NEXT:  "jsonrpc": "2.0",
 # CHECK-NEXT:  "result": {
@@ -33,6 +33,16 @@
 # CHECK-NEXT:  "hoverProvider": true,
 # CHECK-NEXT:  "referencesProvider": true,
 # CHECK-NEXT:  "renameProvider": true,
+# CHECK-NEXT:  "semanticHighlighting": {
+# CHECK-NEXT:"scopes": [
+# CHECK-NEXT:  [
+# CHECK-NEXT:"variable.cpp"
+# CHECK-NEXT:  ],
+# CHECK-NEXT:  [
+# CHECK-NEXT:"entity.name.function.cpp"
+# CHECK-NEXT:  ]
+# CHECK-NEXT:]
+# CHECK-NEXT:  },
 # CHECK-NEXT:  "signatureHelpProvider": {
 # CHECK-NEXT:   

[PATCH] D63919: [clangd] Emit publishSemanticHighlighting in LSP if enabled

2019-07-03 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 207783.
jvikstrom marked 7 inline comments as done.
jvikstrom added a comment.

Addressed comments.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63919

Files:
  clang-tools-extra/clangd/ClangdLSPServer.cpp
  clang-tools-extra/clangd/ClangdLSPServer.h
  clang-tools-extra/clangd/Protocol.cpp
  clang-tools-extra/clangd/Protocol.h
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/SemanticHighlighting.h
  clang-tools-extra/clangd/test/semantic-highlighting.test
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -8,6 +8,7 @@
 
 #include "Annotations.h"
 #include "ClangdServer.h"
+#include "Protocol.h"
 #include "SemanticHighlighting.h"
 #include "TestFS.h"
 #include "TestTU.h"
@@ -66,7 +67,7 @@
   }
 }
 
-TEST(ClangdSemanticHighlightingTest, GeneratesHighlightsWhenFileChange) {
+TEST(SemanticHighlighting, GeneratesHighlightsWhenFileChange) {
   class HighlightingsCounterDiagConsumer : public DiagnosticsConsumer {
   public:
 std::atomic Count = {0};
@@ -90,6 +91,29 @@
   ASSERT_EQ(DiagConsumer.Count, 1);
 }
 
+TEST(SemanticHighlighting, toSemanticHighlightingInformation) {
+  auto CreatePosition = [](int Line, int Character) -> Position {
+Position Pos;
+Pos.line = Line;
+Pos.character = Character;
+return Pos;
+  };
+
+  std::vector Tokens{
+  {HighlightingKind::Variable,
+Range{CreatePosition(3, 8), CreatePosition(3, 12)}},
+  {HighlightingKind::Function,
+Range{CreatePosition(3, 4), CreatePosition(3, 7)}},
+  {HighlightingKind::Variable,
+Range{CreatePosition(1, 1), CreatePosition(1, 5)}}};
+  std::vector ActualResults =
+  toSemanticHighlightingInformation(Tokens);
+  std::vector ExpectedResults = {
+  {1, "AQAEAAA="},
+  {3, "CAAEAAAEAAMAAQ=="}};
+  EXPECT_EQ(ActualResults, ExpectedResults);
+}
+
 } // namespace
 } // namespace clangd
 } // namespace clang
Index: clang-tools-extra/clangd/test/semantic-highlighting.test
===
--- /dev/null
+++ clang-tools-extra/clangd/test/semantic-highlighting.test
@@ -0,0 +1,75 @@
+# RUN: clangd -lit-test < %s | FileCheck -strict-whitespace %s
+{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{"textDocument":{"semanticHighlightingCapabilities":{"semanticHighlighting":true}}},"trace":"off"}}
+---
+#  CHECK:  "id": 0,
+# CHECK-NEXT:  "jsonrpc": "2.0",
+# CHECK-NEXT:  "result": {
+# CHECK-NEXT:"capabilities": {
+# CHECK-NEXT:  "codeActionProvider": true,
+# CHECK-NEXT:  "completionProvider": {
+# CHECK-NEXT:"resolveProvider": false,
+# CHECK-NEXT:"triggerCharacters": [
+# CHECK-NEXT:  ".",
+# CHECK-NEXT:  ">",
+# CHECK-NEXT:  ":"
+# CHECK-NEXT:]
+# CHECK-NEXT:  },
+# CHECK-NEXT:  "declarationProvider": true,
+# CHECK-NEXT:  "definitionProvider": true,
+# CHECK-NEXT:  "documentFormattingProvider": true,
+# CHECK-NEXT:  "documentHighlightProvider": true,
+# CHECK-NEXT:  "documentOnTypeFormattingProvider": {
+# CHECK-NEXT:"firstTriggerCharacter": "\n",
+# CHECK-NEXT:"moreTriggerCharacter": []
+# CHECK-NEXT:  },
+# CHECK-NEXT:  "documentRangeFormattingProvider": true,
+# CHECK-NEXT:  "documentSymbolProvider": true,
+# CHECK-NEXT:  "executeCommandProvider": {
+# CHECK-NEXT:"commands": [
+# CHECK-NEXT:  "clangd.applyFix",
+# CHECK-NEXT:  "clangd.applyTweak"
+# CHECK-NEXT:]
+# CHECK-NEXT:  },
+# CHECK-NEXT:  "hoverProvider": true,
+# CHECK-NEXT:  "referencesProvider": true,
+# CHECK-NEXT:  "renameProvider": true,
+# CHECK-NEXT:  "semanticHighlighting": {
+# CHECK-NEXT:"scopes": [
+# CHECK-NEXT:  [
+# CHECK-NEXT:"variable.cpp"
+# CHECK-NEXT:  ],
+# CHECK-NEXT:  [
+# CHECK-NEXT:"entity.name.function.cpp"
+# CHECK-NEXT:  ]
+# CHECK-NEXT:]
+# CHECK-NEXT:  },
+# CHECK-NEXT:  "signatureHelpProvider": {
+# CHECK-NEXT:"triggerCharacters": [
+# CHECK-NEXT:  "(",
+# CHECK-NEXT:  ","
+# CHECK-NEXT:]
+# CHECK-NEXT:  },
+# CHECK-NEXT:  "textDocumentSync": 2,
+# CHECK-NEXT:  "typeHierarchyProvider": true
+# CHECK-NEXT:  "workspaceSymbolProvider": true
+# CHECK-NEXT:}
+# CHECK-NEXT:  }
+---

[PATCH] D64199: [clangd] Added highlighting for variable references (declrefs)

2019-07-05 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 208123.
jvikstrom added a comment.

Added additional testcase.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64199

Files:
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp


Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -57,10 +57,32 @@
 void $Function[[foo]](int $Variable[[a]]) {
   auto $Variable[[VeryLongVariableName]] = 12312;
   A $Variable[[aa]];
+  auto $Variable[[l]] = $Variable[[aa]].SomeMember + $Variable[[a]];
 }
   )cpp",
   R"cpp(
 void $Function[[foo]](int);
+  )cpp",
+  R"cpp(
+void $Function[[gah]]();
+void $Function[[foo]]() {
+  int $Variable[[b]];
+  auto $Variable[[FN]] = [ $Variable[[b]]](int $Variable[[a]]) -> void {};
+  $Variable[[FN]](12312);
+  auto $Variable[[bou]] = $Function[[gah]];
+}
+  )cpp",
+  R"cpp(
+struct A {
+  A();
+  ~A();
+  void $Function[[abc]]();
+  void operator<<(int);
+};
+void $Function[[foo]]() {
+  A $Variable[[a]];
+  $Variable[[a]].abc();
+}
   )cpp"};
   for (const auto  : TestCases) {
 checkHighlightings(TestCase);
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -34,26 +34,46 @@
 return Tokens;
   }
 
-  bool VisitVarDecl(VarDecl *Var) {
-addToken(Var, HighlightingKind::Variable);
+  bool VisitNamedDecl(NamedDecl *ND) {
+// FIXME: (De)Constructors/operator need to be highlighted some other way.
+if (ND->getDeclName().getNameKind() != DeclarationName::Identifier)
+  return true;
+
+if (ND->getDeclName().isEmpty())
+  // Don't add symbols that don't have any length.
+  return true;
+addToken(ND->getLocation(), ND);
 return true;
   }
-  bool VisitFunctionDecl(FunctionDecl *Func) {
-addToken(Func, HighlightingKind::Function);
+
+  bool VisitDeclRefExpr(DeclRefExpr *Ref) {
+if (Ref->getNameInfo().getName().getNameKind() !=
+DeclarationName::Identifier)
+  // Only want to highlight identifiers.
+  return true;
+
+addToken(Ref->getLocation(), Ref->getDecl());
 return true;
   }
 
 private:
-  void addToken(const NamedDecl *D, HighlightingKind Kind) {
-if (D->getLocation().isMacroID())
-  // FIXME: skip tokens inside macros for now.
+  void addToken(SourceLocation Loc, const Decl *D) {
+if (isa(D)) {
+  addToken(Loc, HighlightingKind::Variable);
   return;
+}
+if (isa(D)) {
+  addToken(Loc, HighlightingKind::Function);
+  return;
+}
+  }
 
-if (D->getDeclName().isEmpty())
-  // Don't add symbols that don't have any length.
+  void addToken(SourceLocation Loc, HighlightingKind Kind) {
+if (Loc.isMacroID())
+  // FIXME: skip tokens inside macros for now.
   return;
 
-auto R = getTokenRange(SM, Ctx.getLangOpts(), D->getLocation());
+auto R = getTokenRange(SM, Ctx.getLangOpts(), Loc);
 if (!R) {
   // R should always have a value, if it doesn't something is very wrong.
   elog("Tried to add semantic token with an invalid range");


Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -57,10 +57,32 @@
 void $Function[[foo]](int $Variable[[a]]) {
   auto $Variable[[VeryLongVariableName]] = 12312;
   A $Variable[[aa]];
+  auto $Variable[[l]] = $Variable[[aa]].SomeMember + $Variable[[a]];
 }
   )cpp",
   R"cpp(
 void $Function[[foo]](int);
+  )cpp",
+  R"cpp(
+void $Function[[gah]]();
+void $Function[[foo]]() {
+  int $Variable[[b]];
+  auto $Variable[[FN]] = [ $Variable[[b]]](int $Variable[[a]]) -> void {};
+  $Variable[[FN]](12312);
+  auto $Variable[[bou]] = $Function[[gah]];
+}
+  )cpp",
+  R"cpp(
+struct A {
+  A();
+  ~A();
+  void $Function[[abc]]();
+  void operator<<(int);
+};
+void $Function[[foo]]() {
+  A $Variable[[a]];
+  $Variable[[a]].abc();
+}
   )cpp"};
   for (const auto  : TestCases) {
 checkHighlightings(TestCase);
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp

[PATCH] D64199: [clangd] Added highlighting for variable references (declrefs)

2019-07-05 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 208143.
jvikstrom added a comment.

Separated into three testcases.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64199

Files:
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -49,19 +49,34 @@
 TEST(SemanticHighlighting, GetsCorrectTokens) {
   const char *TestCases[] = {
   R"cpp(
-struct A {
-  double SomeMember;
-};
-struct {
-}   $Variable[[HStruct]];
-void $Function[[foo]](int $Variable[[a]]) {
-  auto $Variable[[VeryLongVariableName]] = 12312;
-  A $Variable[[aa]];
-}
-  )cpp",
+  struct AS {
+double SomeMember;
+  };
+  struct {
+  } $Variable[[S]];
+  void $Function[[foo]](int $Variable[[A]]) {
+auto $Variable[[VeryLongVariableName]] = 12312;
+AS $Variable[[AA]];
+auto $Variable[[L]] = $Variable[[AA]].SomeMember + $Variable[[A]];
+auto $Variable[[FN]] = [ $Variable[[AA]]](int $Variable[[A]]) -> void {};
+$Variable[[FN]](12312);
+  }
+)cpp",
+  R"cpp(
+  void $Function[[foo]](int);
+  void $Function[[Gah]]();
+  void $Function[[foo]]() {
+auto $Variable[[Bou]] = $Function[[Gah]];
+  }
+)cpp",
   R"cpp(
-void $Function[[foo]](int);
-  )cpp"};
+  struct A {
+A();
+~A();
+void $Function[[abc]]();
+void operator<<(int);
+  };
+)cpp"};
   for (const auto  : TestCases) {
 checkHighlightings(TestCase);
   }
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -34,26 +34,46 @@
 return Tokens;
   }
 
-  bool VisitVarDecl(VarDecl *Var) {
-addToken(Var, HighlightingKind::Variable);
+  bool VisitNamedDecl(NamedDecl *ND) {
+// FIXME: (De)Constructors/operator need to be highlighted some other way.
+if (ND->getDeclName().getNameKind() != DeclarationName::Identifier)
+  return true;
+
+if (ND->getDeclName().isEmpty())
+  // Don't add symbols that don't have any length.
+  return true;
+addToken(ND->getLocation(), ND);
 return true;
   }
-  bool VisitFunctionDecl(FunctionDecl *Func) {
-addToken(Func, HighlightingKind::Function);
+
+  bool VisitDeclRefExpr(DeclRefExpr *Ref) {
+if (Ref->getNameInfo().getName().getNameKind() !=
+DeclarationName::Identifier)
+  // Only want to highlight identifiers.
+  return true;
+
+addToken(Ref->getLocation(), Ref->getDecl());
 return true;
   }
 
 private:
-  void addToken(const NamedDecl *D, HighlightingKind Kind) {
-if (D->getLocation().isMacroID())
-  // FIXME: skip tokens inside macros for now.
+  void addToken(SourceLocation Loc, const Decl *D) {
+if (isa(D)) {
+  addToken(Loc, HighlightingKind::Variable);
   return;
+}
+if (isa(D)) {
+  addToken(Loc, HighlightingKind::Function);
+  return;
+}
+  }
 
-if (D->getDeclName().isEmpty())
-  // Don't add symbols that don't have any length.
+  void addToken(SourceLocation Loc, HighlightingKind Kind) {
+if (Loc.isMacroID())
+  // FIXME: skip tokens inside macros for now.
   return;
 
-auto R = getTokenRange(SM, Ctx.getLangOpts(), D->getLocation());
+auto R = getTokenRange(SM, Ctx.getLangOpts(), Loc);
 if (!R) {
   // R should always have a value, if it doesn't something is very wrong.
   elog("Tried to add semantic token with an invalid range");
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D64199: [clangd] Added highlighting for variable references (declrefs)

2019-07-05 Thread Johan Vikström via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
jvikstrom marked an inline comment as done.
Closed by commit rL365205: [clangd] Added highlighting for variable references 
(declrefs) (authored by jvikstrom, committed by ).
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D64199?vs=208143=208165#toc

Repository:
  rL LLVM

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

https://reviews.llvm.org/D64199

Files:
  clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp
  clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp
@@ -34,26 +34,46 @@
 return Tokens;
   }
 
-  bool VisitVarDecl(VarDecl *Var) {
-addToken(Var, HighlightingKind::Variable);
+  bool VisitNamedDecl(NamedDecl *ND) {
+// FIXME: (De)Constructors/operator need to be highlighted some other way.
+if (ND->getDeclName().getNameKind() != DeclarationName::Identifier)
+  return true;
+
+if (ND->getDeclName().isEmpty())
+  // Don't add symbols that don't have any length.
+  return true;
+addToken(ND->getLocation(), ND);
 return true;
   }
-  bool VisitFunctionDecl(FunctionDecl *Func) {
-addToken(Func, HighlightingKind::Function);
+
+  bool VisitDeclRefExpr(DeclRefExpr *Ref) {
+if (Ref->getNameInfo().getName().getNameKind() !=
+DeclarationName::Identifier)
+  // Only want to highlight identifiers.
+  return true;
+
+addToken(Ref->getLocation(), Ref->getDecl());
 return true;
   }
 
 private:
-  void addToken(const NamedDecl *D, HighlightingKind Kind) {
-if (D->getLocation().isMacroID())
-  // FIXME: skip tokens inside macros for now.
+  void addToken(SourceLocation Loc, const Decl *D) {
+if (isa(D)) {
+  addToken(Loc, HighlightingKind::Variable);
   return;
+}
+if (isa(D)) {
+  addToken(Loc, HighlightingKind::Function);
+  return;
+}
+  }
 
-if (D->getDeclName().isEmpty())
-  // Don't add symbols that don't have any length.
+  void addToken(SourceLocation Loc, HighlightingKind Kind) {
+if (Loc.isMacroID())
+  // FIXME: skip tokens inside macros for now.
   return;
 
-auto R = getTokenRange(SM, Ctx.getLangOpts(), D->getLocation());
+auto R = getTokenRange(SM, Ctx.getLangOpts(), Loc);
 if (!R) {
   // R should always have a value, if it doesn't something is very wrong.
   elog("Tried to add semantic token with an invalid range");
Index: clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp
@@ -48,20 +48,35 @@
 
 TEST(SemanticHighlighting, GetsCorrectTokens) {
   const char *TestCases[] = {
-  R"cpp(
-struct A {
-  double SomeMember;
-};
-struct {
-}   $Variable[[HStruct]];
-void $Function[[foo]](int $Variable[[a]]) {
-  auto $Variable[[VeryLongVariableName]] = 12312;
-  A $Variable[[aa]];
-}
-  )cpp",
-  R"cpp(
-void $Function[[foo]](int);
-  )cpp"};
+R"cpp(
+  struct AS {
+double SomeMember;
+  };
+  struct {
+  } $Variable[[S]];
+  void $Function[[foo]](int $Variable[[A]]) {
+auto $Variable[[VeryLongVariableName]] = 12312;
+AS $Variable[[AA]];
+auto $Variable[[L]] = $Variable[[AA]].SomeMember + $Variable[[A]];
+auto $Variable[[FN]] = [ $Variable[[AA]]](int $Variable[[A]]) -> void {};
+$Variable[[FN]](12312);
+  }
+)cpp",
+R"cpp(
+  void $Function[[foo]](int);
+  void $Function[[Gah]]();
+  void $Function[[foo]]() {
+auto $Variable[[Bou]] = $Function[[Gah]];
+  }
+)cpp",
+R"cpp(
+  struct A {
+A();
+~A();
+void $Function[[abc]]();
+void operator<<(int);
+  };
+)cpp"};
   for (const auto  : TestCases) {
 checkHighlightings(TestCase);
   }
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D64199: [clangd] Added highlighting for variable references (declrefs)

2019-07-05 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 208140.
jvikstrom added a comment.

Made tests more readable.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64199

Files:
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -49,19 +49,43 @@
 TEST(SemanticHighlighting, GetsCorrectTokens) {
   const char *TestCases[] = {
   R"cpp(
-struct A {
-  double SomeMember;
-};
-struct {
-}   $Variable[[HStruct]];
-void $Function[[foo]](int $Variable[[a]]) {
-  auto $Variable[[VeryLongVariableName]] = 12312;
-  A $Variable[[aa]];
-}
-  )cpp",
+  struct AS {
+double SomeMember;
+  };
+  void $Function[[foo]](int $Variable[[A]]) {
+auto $Variable[[VeryLongVariableName]] = 12312;
+AS $Variable[[AA]];
+auto $Variable[[L]] = $Variable[[AA]].SomeMember + $Variable[[A]];
+  }
+)cpp",
+  R"cpp(
+  struct {
+  } $Variable[[S]];
+)cpp",
+  R"cpp(
+  void $Function[[foo]](int);
+)cpp",
+  R"cpp(
+  void $Function[[Gah]]();
+  void $Function[[foo]]() {
+auto $Variable[[Bou]] = $Function[[Gah]];
+  }
+)cpp",
+  R"cpp(
+  void $Function[[foo]]() {
+int $Variable[[B]];
+auto $Variable[[FN]] = [ $Variable[[B]]](int $Variable[[A]]) -> void {};
+$Variable[[FN]](12312);
+  }
+)cpp",
   R"cpp(
-void $Function[[foo]](int);
-  )cpp"};
+  struct A {
+A();
+~A();
+void $Function[[abc]]();
+void operator<<(int);
+  };
+)cpp"};
   for (const auto  : TestCases) {
 checkHighlightings(TestCase);
   }
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -34,26 +34,46 @@
 return Tokens;
   }
 
-  bool VisitVarDecl(VarDecl *Var) {
-addToken(Var, HighlightingKind::Variable);
+  bool VisitNamedDecl(NamedDecl *ND) {
+// FIXME: (De)Constructors/operator need to be highlighted some other way.
+if (ND->getDeclName().getNameKind() != DeclarationName::Identifier)
+  return true;
+
+if (ND->getDeclName().isEmpty())
+  // Don't add symbols that don't have any length.
+  return true;
+addToken(ND->getLocation(), ND);
 return true;
   }
-  bool VisitFunctionDecl(FunctionDecl *Func) {
-addToken(Func, HighlightingKind::Function);
+
+  bool VisitDeclRefExpr(DeclRefExpr *Ref) {
+if (Ref->getNameInfo().getName().getNameKind() !=
+DeclarationName::Identifier)
+  // Only want to highlight identifiers.
+  return true;
+
+addToken(Ref->getLocation(), Ref->getDecl());
 return true;
   }
 
 private:
-  void addToken(const NamedDecl *D, HighlightingKind Kind) {
-if (D->getLocation().isMacroID())
-  // FIXME: skip tokens inside macros for now.
+  void addToken(SourceLocation Loc, const Decl *D) {
+if (isa(D)) {
+  addToken(Loc, HighlightingKind::Variable);
   return;
+}
+if (isa(D)) {
+  addToken(Loc, HighlightingKind::Function);
+  return;
+}
+  }
 
-if (D->getDeclName().isEmpty())
-  // Don't add symbols that don't have any length.
+  void addToken(SourceLocation Loc, HighlightingKind Kind) {
+if (Loc.isMacroID())
+  // FIXME: skip tokens inside macros for now.
   return;
 
-auto R = getTokenRange(SM, Ctx.getLangOpts(), D->getLocation());
+auto R = getTokenRange(SM, Ctx.getLangOpts(), Loc);
 if (!R) {
   // R should always have a value, if it doesn't something is very wrong.
   elog("Tried to add semantic token with an invalid range");
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D64199: [clangd] Added highlighting for variable references (declrefs)

2019-07-05 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom marked 4 inline comments as done.
jvikstrom added inline comments.



Comment at: clang-tools-extra/clangd/SemanticHighlighting.cpp:61
+}
+if(isa(D)) {
+  addToken(Loc, HighlightingKind::Function);

hokein wrote:
> sammccall wrote:
> > jvikstrom wrote:
> > > sammccall wrote:
> > > > note that methods, constructors, and destructors inherit from 
> > > > functiondecl, so if you want to exclude/distinguish those, order 
> > > > matters here
> > > I'm aware of that, but thanks for the heads up. Although should I add it 
> > > in a comment somewhere in the method? Also added an additional testcase 
> > > for classes and FIXMEs to the skip if statement in VisitNamedDecl.
> > I don't think it needs a comment, especially if you're not actually 
> > highlighting them (because they have weird DeclarationNames)
> > 
> > > FIXMEs to the skip if statement in VisitNamedDecl
> > I'm not actually sure there's anything to fix here - it's a bit hard to 
> > talk about constructor/destructor highlighting as distinct from type name 
> > highlighting in C++. If you want them highlighted as classes, then that 
> > should just start working when you start handling TypeLocs.
> I think constructor/destructor can be categorized in the `function` group, 
> like `entity.name.function.constructor`, `entity.name.function.destructor`
I'll have a look at constructors/destructors at the same time as I look at types



Comment at: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp:69
+void $Function[[foo]]() {
+  int $Variable[[b]];
+  auto $Variable[[FN]] = [ $Variable[[b]]](int $Variable[[a]]) -> void {};

hokein wrote:
> nit: even for the test code, could we make the code style consistent (like 
> follow the LLVM code style) here?
I think this should be consistent with LLVM code style. 


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64199



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D64257: [clangd] Added highlighting for non-builtin types

2019-07-05 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom created this revision.
jvikstrom added reviewers: hokein, sammccall, ilya-biryukov.
Herald added subscribers: cfe-commits, kadircet, arphaman, jkorous, MaskRay.
Herald added a project: clang.

Added highlighting for non-builtin types using VisitTypeLoc. Ignoring namespace 
qualifiers as for now.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D64257

Files:
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/SemanticHighlighting.h
  clang-tools-extra/clangd/test/semantic-highlighting.test
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -34,11 +34,12 @@
   auto AST = TestTU::withCode(Test.code()).build();
   static const std::map KindToString{
   {HighlightingKind::Variable, "Variable"},
-  {HighlightingKind::Function, "Function"}};
+  {HighlightingKind::Function, "Function"},
+  {HighlightingKind::Type, "Type"}};
   std::vector ExpectedTokens;
   for (const auto  : KindToString) {
-std::vector Toks =
-makeHighlightingTokens(Test.ranges(KindString.second), KindString.first);
+std::vector Toks = makeHighlightingTokens(
+Test.ranges(KindString.second), KindString.first);
 ExpectedTokens.insert(ExpectedTokens.end(), Toks.begin(), Toks.end());
   }
 
@@ -49,14 +50,14 @@
 TEST(SemanticHighlighting, GetsCorrectTokens) {
   const char *TestCases[] = {
 R"cpp(
-  struct AS {
+  struct $Type[[AS]] {
 double SomeMember;
   };
-  struct {
+  $Type[[struct]] {
   } $Variable[[S]];
-  void $Function[[foo]](int $Variable[[A]]) {
+  void $Function[[foo]](int $Variable[[A]], $Type[[AS]] $Variable[[As]]) {
 auto $Variable[[VeryLongVariableName]] = 12312;
-AS $Variable[[AA]];
+$Type[[AS]] $Variable[[AA]];
 auto $Variable[[L]] = $Variable[[AA]].SomeMember + $Variable[[A]];
 auto $Variable[[FN]] = [ $Variable[[AA]]](int $Variable[[A]]) -> void {};
 $Variable[[FN]](12312);
@@ -70,11 +71,18 @@
   }
 )cpp",
 R"cpp(
-  struct A {
+  namespace abc {
+template
+struct $Type[[A]] {};
+  }
+  abc::$Type[[A]] $Variable[[AA]];
+)cpp",
+R"cpp(
+  struct $Type[[A]] {
 A();
 ~A();
 void $Function[[abc]]();
-void operator<<(int);
+void operator<<($Type[[A]]);
   };
 )cpp"};
   for (const auto  : TestCases) {
Index: clang-tools-extra/clangd/test/semantic-highlighting.test
===
--- clang-tools-extra/clangd/test/semantic-highlighting.test
+++ clang-tools-extra/clangd/test/semantic-highlighting.test
@@ -10,6 +10,9 @@
 # CHECK-NEXT:  [
 # CHECK-NEXT:"entity.name.function.cpp"
 # CHECK-NEXT:  ]
+# CHECK-NEXT:  [
+# CHECK-NEXT:"entity.name.type.cpp"
+# CHECK-NEXT:  ]
 # CHECK-NEXT:]
 # CHECK-NEXT:  },
 ---
Index: clang-tools-extra/clangd/SemanticHighlighting.h
===
--- clang-tools-extra/clangd/SemanticHighlighting.h
+++ clang-tools-extra/clangd/SemanticHighlighting.h
@@ -26,6 +26,7 @@
 enum class HighlightingKind {
   Variable = 0,
   Function,
+  Type,
 
   NumKinds,
 };
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -56,6 +56,36 @@
 return true;
   }
 
+  bool VisitTypeLoc(TypeLoc ) {
+// The check for DependentName is so namespace qualifiers are not
+// highlighted. The check for elaborated type is for not getting two entries
+// whenever there is an anonymous struct.
+if (TL.getTypeLocClass() == TypeLoc::TypeLocClass::DependentName ||
+TL.getTypeLocClass() == TypeLoc::TypeLocClass::Elaborated) {
+  return true;
+}
+
+TagDecl *D = TL.getTypePtr()->getAsTagDecl();
+if (!D)
+  return true;
+
+if (auto RD = dyn_cast(D)) {
+  if (auto DC = RD->getDestructor()) {
+auto Range = DC->getSourceRange();
+// Destructors appear as a TagTypeLoc RecordDecl. To not highlight
+// destructors incorrectly the TagTypeLoc is skipped if it is wrapped
+// inside the actual destructor.
+if (Range.getBegin() < TL.getBeginLoc() &&
+TL.getBeginLoc() < Range.getEnd()) {
+  return true;
+}
+  }
+}
+
+addToken(TL.getBeginLoc(), D);
+return true;
+  }
+
 private:
   void addToken(SourceLocation Loc, const Decl *D) {
 if (isa(D)) {
@@ -66,6 +96,10 @@
   

[PATCH] D63559: [clangd] Added functionality for getting semantic highlights for variable and function declarations

2019-06-26 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 206630.
jvikstrom marked 6 inline comments as done.
jvikstrom added a comment.

Renamed types to follow LSP. Also renamed files.


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/SemanticHighlighting.cpp
  clang-tools-extra/clangd/SemanticHighlighting.h
  clang-tools-extra/clangd/unittests/CMakeLists.txt
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- /dev/null
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -0,0 +1,69 @@
+//==- SemanticHighlightingTest.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 "SemanticHighlighting.h"
+#include "TestTU.h"
+#include "gmock/gmock.h"
+
+namespace clang {
+namespace clangd {
+namespace {
+
+std::vector makeHighlightingTokens(llvm::ArrayRef Ranges,
+  HighlightingKind Kind) {
+  std::vector Tokens(Ranges.size());
+  for (int I = 0, End = Ranges.size(); I < End; ++I) {
+Tokens[I].R = Ranges[I];
+Tokens[I].Kind = Kind;
+  }
+
+  return Tokens;
+}
+
+void checkHighlightings(llvm::StringRef Code) {
+  Annotations Test(Code);
+  auto AST = TestTU::withCode(Test.code()).build();
+  static const std::map KindToString{
+  {HighlightingKind::Variable, "Variable"},
+  {HighlightingKind::Function, "Function"}};
+  std::vector ExpectedTokens;
+  for (const auto  : KindToString) {
+std::vector Toks =
+makeHighlightingTokens(Test.ranges(KindString.second), KindString.first);
+ExpectedTokens.insert(ExpectedTokens.end(), Toks.begin(), Toks.end());
+  }
+
+  auto ActualTokens = getSemanticHighlightings(AST);
+  EXPECT_THAT(ActualTokens, testing::UnorderedElementsAreArray(ExpectedTokens));
+}
+
+TEST(SemanticHighlighting, GetsCorrectTokens) {
+  const char *TestCases[] = {
+  R"cpp(
+struct A {
+  double SomeMember;
+};
+struct {
+}   $Variable[[HStruct]];
+void $Function[[foo]](int $Variable[[a]]) {
+  auto $Variable[[VeryLongVariableName]] = 12312;
+  A $Variable[[aa]];
+}
+  )cpp",
+  R"cpp(
+void $Function[[foo]](int);
+  )cpp"};
+  for (const auto  : TestCases) {
+checkHighlightings(TestCase);
+  }
+}
+
+} // 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
+  SemanticHighlightingTests.cpp
   SerializationTests.cpp
   SourceCodeTests.cpp
   SymbolCollectorTests.cpp
Index: clang-tools-extra/clangd/SemanticHighlighting.h
===
--- /dev/null
+++ clang-tools-extra/clangd/SemanticHighlighting.h
@@ -0,0 +1,37 @@
+//==-- SemanticHighlighting.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"
+
+namespace clang {
+namespace clangd {
+
+enum class HighlightingKind {
+  Variable,
+  Function,
+};
+
+// Contains all information needed for the highlighting a token.
+struct HighlightingToken {
+  HighlightingKind Kind;
+  Range R;
+};
+
+bool operator==(const HighlightingToken , const HighlightingToken );
+
+// Returns all HighlightingTokens from an AST. Only generates highlights for the
+// main AST.
+std::vector getSemanticHighlightings(ParsedAST );
+
+} // namespace clangd
+} // namespace clang
+
+#endif
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===
--- /dev/null
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -0,0 +1,78 @@
+//===--- SemanticHighlighting.cpp - -- ---*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// 

[PATCH] D63559: [clangd] Added functionality for getting semantic highlights for variable and function declarations

2019-06-26 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom added inline comments.



Comment at: clang-tools-extra/clangd/SemanticHighlight.cpp:28
+  : Ctx(AST.getASTContext()), SM(AST.getSourceManager()) {
+Ctx.setTraversalScope(AST.getLocalTopLevelDecls());
+  }

hokein wrote:
> I'd move this line to `collectTokens` as they are related.
> 
> As discussed, setTraversalScope doesn't guarantee that we wouldnot visit 
> Decl* outside of the main file (some decls are still reachable), so we may 
> get tokens outside of the main file. If you don't address it in this patch, 
> that's fine, leave a FIXME here.
Will fix in a separate patch for topLevelDecls. Don't really know what FIXME to 
add  here. Added one to getLocalTopLevelDecls. Don't really have a good 
understanding of the reason as to what happens yet so the FIXME is very general 
(as you can probably tell from it)...



Comment at: clang-tools-extra/clangd/SemanticHighlight.cpp:56
+
+auto R = getTokenRange(SM, Ctx.getLangOpts(), D->getLocation());
+if (!R.hasValue()) {

hokein wrote:
> How about pulling out a function `llvm::Optional 
> makeSemanticToken(..)`?
Don't understand what you mean.



Comment at: clang-tools-extra/clangd/unittests/SemanticHighlightTests.cpp:24
+
+std::vector makeSemanticTokens(std::vector Ranges,
+ SemanticHighlightKind Kind) {

hokein wrote:
> we are passing a copy here, use llvm::ArrayRef.
I changed to pass a const vector & instead. Is that alright as well?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63559



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D63559: [clangd] Added functionality for getting semantic highlights for variable and function declarations

2019-06-26 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 206618.
jvikstrom marked 11 inline comments as done.
jvikstrom added a comment.

- [clangd] Added functionality for getting semantic highlights for variable and 
function declarations


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,69 @@
+//===- 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 "SemanticHighlight.h"
+#include "TestTU.h"
+#include "gmock/gmock.h"
+
+namespace clang {
+namespace clangd {
+namespace {
+
+std::vector makeSemanticTokens(llvm::ArrayRef Ranges,
+  SemanticHighlightKind Kind) {
+  std::vector Tokens(Ranges.size());
+  for (int I = 0, End = Ranges.size(); I < End; ++I) {
+Tokens[I].R = Ranges[I];
+Tokens[I].Kind = Kind;
+  }
+
+  return Tokens;
+}
+
+void checkHighlights(llvm::StringRef Code) {
+  Annotations Test(Code);
+  auto AST = TestTU::withCode(Test.code()).build();
+  static const std::map KindToString{
+  {SemanticHighlightKind::Variable, "Variable"},
+  {SemanticHighlightKind::Function, "Function"}};
+  std::vector ExpectedTokens;
+  for (const auto  : KindToString) {
+std::vector Toks =
+makeSemanticTokens(Test.ranges(KindString.second), KindString.first);
+ExpectedTokens.insert(ExpectedTokens.end(), Toks.begin(), Toks.end());
+  }
+
+  auto ActualTokens = getSemanticHighlights(AST);
+  EXPECT_THAT(ActualTokens, testing::UnorderedElementsAreArray(ExpectedTokens));
+}
+
+TEST(SemanticTokenCollector, GetsCorrectTokens) {
+  const char *TestCases[] = {
+  R"cpp(
+struct A {
+  double SomeMember;
+};
+struct {
+}   $Variable[[HStruct]];
+void $Function[[foo]](int $Variable[[a]]) {
+  auto $Variable[[VeryLongVariableName]] = 12312;
+  A $Variable[[aa]];
+}
+  )cpp",
+  R"cpp(
+void $Function[[foo]](int);
+  )cpp"};
+  for (const auto  : TestCases) {
+checkHighlights(TestCase);
+  }
+}
+
+} // 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,37 @@
+//==-- 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"
+
+namespace clang {
+namespace clangd {
+
+enum class SemanticHighlightKind {
+  Variable,
+  Function,
+};
+
+// Contains all information needed for the highlighting a token.
+struct SemanticToken {
+  SemanticHighlightKind Kind;
+  Range R;
+};
+
+bool operator==(const SemanticToken , const SemanticToken );
+
+// Returns all SemanticTokens from an AST. Only generates highlights for the
+// main AST.
+std::vector getSemanticHighlights(ParsedAST );
+
+} // namespace clangd
+} // namespace clang
+
+#endif
Index: clang-tools-extra/clangd/SemanticHighlight.cpp
===
--- /dev/null
+++ clang-tools-extra/clangd/SemanticHighlight.cpp
@@ -0,0 +1,78 @@
+//===--- SemanticHighlight.cpp - -- -*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license 

[PATCH] D63559: [clangd] Added functionality for getting semantic highlights for variable and function declarations

2019-06-26 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom added inline comments.



Comment at: clang-tools-extra/clangd/unittests/SemanticHighlightTests.cpp:47
+TEST(SemanticTokenCollector, GetsCorrectTokens) {
+  const char *TestCases[] = {
+  R"cpp(

hokein wrote:
> the code seems not clang-format, could you run clang-format on your patch?
It has been clang-formated. The spaces are part of the test (need to check that 
the range does not include spaces even if there are multiple ones)


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63559



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D63559: [clangd] Added functionality for getting semantic highlights for variable and function declarations

2019-06-26 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom marked an inline comment as done.
jvikstrom added inline comments.



Comment at: clang-tools-extra/clangd/SemanticHighlight.h:17
+
+enum class SemanticHighlightKind {
+  Variable,

hokein wrote:
> LSP proposal is using `Highlighting` rather than `Highlight`, let's align 
> with the LSP proposal, using  `Highlighting` in our names (comments, 
> function, class, and files).
> 
> 
> The name seems too verbose, let's drop the `Semantic`, just use 
> `HighlightingKind`.
There is also a `DocumentHighlightingKind` enum though. Is there not a 
possibility of  confusing people with `HighlightingKind` and 
`DocumentHighlightingKind`?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63559



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D63821: [clangd] Added C++ API code for semantic highlighting

2019-06-27 Thread Johan Vikström via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL364551: [clangd] Emit semantic highlighting tokens when the 
main AST is built. (authored by jvikstrom, committed by ).
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D63821?vs=206854=206867#toc

Repository:
  rL LLVM

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

https://reviews.llvm.org/D63821

Files:
  clang-tools-extra/trunk/clangd/ClangdServer.cpp
  clang-tools-extra/trunk/clangd/ClangdServer.h
  clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/trunk/clangd/ClangdServer.h
===
--- clang-tools-extra/trunk/clangd/ClangdServer.h
+++ clang-tools-extra/trunk/clangd/ClangdServer.h
@@ -18,6 +18,7 @@
 #include "Function.h"
 #include "GlobalCompilationDatabase.h"
 #include "Protocol.h"
+#include "SemanticHighlighting.h"
 #include "TUScheduler.h"
 #include "XRefs.h"
 #include "index/Background.h"
@@ -49,6 +50,11 @@
   std::vector Diagnostics) = 0;
   /// Called whenever the file status is updated.
   virtual void onFileUpdated(PathRef File, const TUStatus ){};
+
+  /// Called by ClangdServer when some \p Highlightings for \p File are ready.
+  virtual void
+  onHighlightingsReady(PathRef File,
+   std::vector Highlightings) {}
 };
 
 /// When set, used by ClangdServer to get clang-tidy options for each particular
@@ -131,6 +137,9 @@
 /// Clangd will execute compiler drivers matching one of these globs to
 /// fetch system include path.
 std::vector QueryDriverGlobs;
+
+/// Enable semantic highlighting features.
+bool SemanticHighlighting = false;
   };
   // Sensible default options for use in tests.
   // Features like indexing must be enabled if desired.
@@ -304,7 +313,7 @@
   // can be caused by missing includes (e.g. member access in incomplete type).
   bool SuggestMissingIncludes = false;
   bool EnableHiddenFeatures = false;
-
+  
   // GUARDED_BY(CachedCompletionFuzzyFindRequestMutex)
   llvm::StringMap>
   CachedCompletionFuzzyFindRequestByFile;
Index: clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp
@@ -7,7 +7,9 @@
 //===--===//
 
 #include "Annotations.h"
+#include "ClangdServer.h"
 #include "SemanticHighlighting.h"
+#include "TestFS.h"
 #include "TestTU.h"
 #include "gmock/gmock.h"
 
@@ -64,6 +66,30 @@
   }
 }
 
+TEST(ClangdSemanticHighlightingTest, GeneratesHighlightsWhenFileChange) {
+  class HighlightingsCounterDiagConsumer : public DiagnosticsConsumer {
+  public:
+std::atomic Count = {0};
+
+void onDiagnosticsReady(PathRef, std::vector) override {}
+void onHighlightingsReady(
+PathRef File, std::vector Highlightings) override {
+  ++Count;
+}
+  };
+
+  auto FooCpp = testPath("foo.cpp");
+  MockFSProvider FS;
+  FS.Files[FooCpp] = "";
+
+  MockCompilationDatabase MCD;
+  HighlightingsCounterDiagConsumer DiagConsumer;
+  ClangdServer Server(MCD, FS, DiagConsumer, ClangdServer::optsForTest());
+  Server.addDocument(FooCpp, "int a;");
+  ASSERT_TRUE(Server.blockUntilIdleForTest()) << "Waiting for server";
+  ASSERT_EQ(DiagConsumer.Count, 1);
+}
+
 } // namespace
 } // namespace clangd
 } // namespace clang
Index: clang-tools-extra/trunk/clangd/ClangdServer.cpp
===
--- clang-tools-extra/trunk/clangd/ClangdServer.cpp
+++ clang-tools-extra/trunk/clangd/ClangdServer.cpp
@@ -48,8 +48,10 @@
 
 // Update the FileIndex with new ASTs and plumb the diagnostics responses.
 struct UpdateIndexCallbacks : public ParsingCallbacks {
-  UpdateIndexCallbacks(FileIndex *FIndex, DiagnosticsConsumer )
-  : FIndex(FIndex), DiagConsumer(DiagConsumer) {}
+  UpdateIndexCallbacks(FileIndex *FIndex, DiagnosticsConsumer ,
+   bool SemanticHighlighting)
+  : FIndex(FIndex), DiagConsumer(DiagConsumer),
+SemanticHighlighting(SemanticHighlighting) {}
 
   void onPreambleAST(PathRef Path, ASTContext ,
  std::shared_ptr PP,
@@ -61,6 +63,8 @@
   void onMainAST(PathRef Path, ParsedAST ) override {
 if (FIndex)
   FIndex->updateMain(Path, AST);
+if (SemanticHighlighting)
+  DiagConsumer.onHighlightingsReady(Path, getSemanticHighlightings(AST));
   }
 
   void onDiagnostics(PathRef File, std::vector Diags) override {
@@ -74,6 +78,7 @@
 private:
   FileIndex *FIndex;
   DiagnosticsConsumer 
+  bool SemanticHighlighting;
 };
 } // namespace
 
@@ -82,6 +87,7 @@
   Opts.UpdateDebounce = 

[PATCH] D63821: [clangd] Added C++ API code for semantic highlighting

2019-06-27 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 206854.
jvikstrom marked 2 inline comments as done.
jvikstrom added a comment.

Made test safe again


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63821

Files:
  clang-tools-extra/clangd/ClangdServer.cpp
  clang-tools-extra/clangd/ClangdServer.h
  clang-tools-extra/clangd/unittests/ClangdTests.cpp

Index: clang-tools-extra/clangd/unittests/ClangdTests.cpp
===
--- clang-tools-extra/clangd/unittests/ClangdTests.cpp
+++ clang-tools-extra/clangd/unittests/ClangdTests.cpp
@@ -841,6 +841,30 @@
   EXPECT_FALSE(PathResult.hasValue());
 }
 
+TEST(ClangdSemanticHighlightingTest, GeneratesHighlightsWhenFileChange) {
+  class HighlightingsCounterDiagConsumer : public DiagnosticsConsumer {
+  public:
+std::atomic Count = {0};
+
+void onDiagnosticsReady(PathRef, std::vector) override {}
+void onHighlightingsReady(
+PathRef File, std::vector Highlightings) override {
+  ++Count;
+}
+  };
+
+  auto FooCpp = testPath("foo.cpp");
+  MockFSProvider FS;
+  FS.Files[FooCpp] = "";
+
+  MockCompilationDatabase MCD;
+  HighlightingsCounterDiagConsumer DiagConsumer;
+  ClangdServer Server(MCD, FS, DiagConsumer, ClangdServer::optsForTest());
+  Server.addDocument(FooCpp, "int a;");
+  ASSERT_TRUE(Server.blockUntilIdleForTest()) << "Waiting for server";
+  ASSERT_EQ(DiagConsumer.Count, 1);
+}
+
 TEST_F(ClangdThreadingTest, NoConcurrentDiagnostics) {
   class NoConcurrentAccessDiagConsumer : public DiagnosticsConsumer {
   public:
Index: clang-tools-extra/clangd/ClangdServer.h
===
--- clang-tools-extra/clangd/ClangdServer.h
+++ clang-tools-extra/clangd/ClangdServer.h
@@ -18,6 +18,7 @@
 #include "Function.h"
 #include "GlobalCompilationDatabase.h"
 #include "Protocol.h"
+#include "SemanticHighlighting.h"
 #include "TUScheduler.h"
 #include "XRefs.h"
 #include "index/Background.h"
@@ -49,6 +50,11 @@
   std::vector Diagnostics) = 0;
   /// Called whenever the file status is updated.
   virtual void onFileUpdated(PathRef File, const TUStatus ){};
+
+  /// Called by ClangdServer when some \p Highlightings for \p File are ready.
+  virtual void
+  onHighlightingsReady(PathRef File,
+   std::vector Highlightings) {}
 };
 
 /// When set, used by ClangdServer to get clang-tidy options for each particular
@@ -131,6 +137,9 @@
 /// Clangd will execute compiler drivers matching one of these globs to
 /// fetch system include path.
 std::vector QueryDriverGlobs;
+
+/// Enable semantic highlighting features.
+bool SemanticHighlighting = false;
   };
   // Sensible default options for use in tests.
   // Features like indexing must be enabled if desired.
@@ -304,7 +313,7 @@
   // can be caused by missing includes (e.g. member access in incomplete type).
   bool SuggestMissingIncludes = false;
   bool EnableHiddenFeatures = false;
-
+  
   // GUARDED_BY(CachedCompletionFuzzyFindRequestMutex)
   llvm::StringMap>
   CachedCompletionFuzzyFindRequestByFile;
Index: clang-tools-extra/clangd/ClangdServer.cpp
===
--- clang-tools-extra/clangd/ClangdServer.cpp
+++ clang-tools-extra/clangd/ClangdServer.cpp
@@ -48,8 +48,10 @@
 
 // Update the FileIndex with new ASTs and plumb the diagnostics responses.
 struct UpdateIndexCallbacks : public ParsingCallbacks {
-  UpdateIndexCallbacks(FileIndex *FIndex, DiagnosticsConsumer )
-  : FIndex(FIndex), DiagConsumer(DiagConsumer) {}
+  UpdateIndexCallbacks(FileIndex *FIndex, DiagnosticsConsumer ,
+   bool SemanticHighlighting)
+  : FIndex(FIndex), DiagConsumer(DiagConsumer),
+SemanticHighlighting(SemanticHighlighting) {}
 
   void onPreambleAST(PathRef Path, ASTContext ,
  std::shared_ptr PP,
@@ -61,6 +63,8 @@
   void onMainAST(PathRef Path, ParsedAST ) override {
 if (FIndex)
   FIndex->updateMain(Path, AST);
+if (SemanticHighlighting)
+  DiagConsumer.onHighlightingsReady(Path, getSemanticHighlightings(AST));
   }
 
   void onDiagnostics(PathRef File, std::vector Diags) override {
@@ -74,6 +78,7 @@
 private:
   FileIndex *FIndex;
   DiagnosticsConsumer 
+  bool SemanticHighlighting;
 };
 } // namespace
 
@@ -82,6 +87,7 @@
   Opts.UpdateDebounce = std::chrono::steady_clock::duration::zero(); // Faster!
   Opts.StorePreamblesInMemory = true;
   Opts.AsyncThreadsCount = 4; // Consistent!
+  Opts.SemanticHighlighting = true;
   return Opts;
 }
 
@@ -102,10 +108,11 @@
   // is parsed.
   // FIXME(ioeric): this can be slow and we may be able to index on less
   // critical paths.
-  WorkScheduler(CDB, Opts.AsyncThreadsCount, Opts.StorePreamblesInMemory,
-llvm::make_unique(DynamicIdx.get(),
-   

[PATCH] D63817: [clangd] No longer getting template instantiations from header files in Main AST.Added another check to DeclTrackingASTConsumer to check that a Decl is actually in the main file.

2019-06-26 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom created this revision.
jvikstrom added reviewers: hokein, ilya-biryukov, sammccall.
Herald added subscribers: cfe-commits, kadircet, arphaman, jkorous, MaskRay.
Herald added a project: clang.

[clangd] No longer getting template instantiations from header files in Main 
AST.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D63817

Files:
  clang-tools-extra/clangd/ClangdUnit.cpp
  clang-tools-extra/clangd/unittests/ClangdUnitTests.cpp


Index: clang-tools-extra/clangd/unittests/ClangdUnitTests.cpp
===
--- clang-tools-extra/clangd/unittests/ClangdUnitTests.cpp
+++ clang-tools-extra/clangd/unittests/ClangdUnitTests.cpp
@@ -83,6 +83,23 @@
   EXPECT_THAT(AST.getLocalTopLevelDecls(), ElementsAre(DeclNamed("main")));
 }
 
+TEST(ClangdUnitTest, DoesNotGetIncludedTopDecls) {
+  TestTU TU;
+  TU.HeaderCode = R"cpp(
+template
+struct H {
+  H() {}
+};
+  )cpp";
+  TU.Code = R"cpp(
+int main() {
+  H h;
+}
+  )cpp";
+  auto AST = TU.build();
+  EXPECT_THAT(AST.getLocalTopLevelDecls(), ElementsAre(DeclNamed("main")));
+}
+
 TEST(ClangdUnitTest, TokensAfterPreamble) {
   TestTU TU;
   TU.AdditionalFiles["foo.h"] = R"(
Index: clang-tools-extra/clangd/ClangdUnit.cpp
===
--- clang-tools-extra/clangd/ClangdUnit.cpp
+++ clang-tools-extra/clangd/ClangdUnit.cpp
@@ -61,6 +61,7 @@
 }
 
 class DeclTrackingASTConsumer : public ASTConsumer {
+  SourceManager *SM;
 public:
   DeclTrackingASTConsumer(std::vector )
   : TopLevelDecls(TopLevelDecls) {}
@@ -70,6 +71,11 @@
   if (D->isFromASTFile())
 continue;
 
+  if (!SM->isInMainFile(D->getLocation()))
+// This decl comes from another file and should not be included in the
+// top level decls.
+continue;
+
   // ObjCMethodDecl are not actually top-level decls.
   if (isa(D))
 continue;
@@ -79,6 +85,10 @@
 return true;
   }
 
+  virtual void Initialize(ASTContext ) override {
+SM = ();
+  }
+
 private:
   std::vector 
 };


Index: clang-tools-extra/clangd/unittests/ClangdUnitTests.cpp
===
--- clang-tools-extra/clangd/unittests/ClangdUnitTests.cpp
+++ clang-tools-extra/clangd/unittests/ClangdUnitTests.cpp
@@ -83,6 +83,23 @@
   EXPECT_THAT(AST.getLocalTopLevelDecls(), ElementsAre(DeclNamed("main")));
 }
 
+TEST(ClangdUnitTest, DoesNotGetIncludedTopDecls) {
+  TestTU TU;
+  TU.HeaderCode = R"cpp(
+template
+struct H {
+  H() {}
+};
+  )cpp";
+  TU.Code = R"cpp(
+int main() {
+  H h;
+}
+  )cpp";
+  auto AST = TU.build();
+  EXPECT_THAT(AST.getLocalTopLevelDecls(), ElementsAre(DeclNamed("main")));
+}
+
 TEST(ClangdUnitTest, TokensAfterPreamble) {
   TestTU TU;
   TU.AdditionalFiles["foo.h"] = R"(
Index: clang-tools-extra/clangd/ClangdUnit.cpp
===
--- clang-tools-extra/clangd/ClangdUnit.cpp
+++ clang-tools-extra/clangd/ClangdUnit.cpp
@@ -61,6 +61,7 @@
 }
 
 class DeclTrackingASTConsumer : public ASTConsumer {
+  SourceManager *SM;
 public:
   DeclTrackingASTConsumer(std::vector )
   : TopLevelDecls(TopLevelDecls) {}
@@ -70,6 +71,11 @@
   if (D->isFromASTFile())
 continue;
 
+  if (!SM->isInMainFile(D->getLocation()))
+// This decl comes from another file and should not be included in the
+// top level decls.
+continue;
+
   // ObjCMethodDecl are not actually top-level decls.
   if (isa(D))
 continue;
@@ -79,6 +85,10 @@
 return true;
   }
 
+  virtual void Initialize(ASTContext ) override {
+SM = ();
+  }
+
 private:
   std::vector 
 };
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D63821: [clangd] Added C++ API code for semantic highlighting

2019-06-26 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom created this revision.
jvikstrom added reviewers: hokein, ilya-biryukov.
Herald added subscribers: cfe-commits, kadircet, arphaman, jkorous, MaskRay, 
javed.absar.
Herald added a project: clang.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D63821

Files:
  clang-tools-extra/clangd/ClangdLSPServer.cpp
  clang-tools-extra/clangd/ClangdLSPServer.h
  clang-tools-extra/clangd/ClangdServer.cpp
  clang-tools-extra/clangd/ClangdServer.h
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/SemanticHighlighting.h
  clang-tools-extra/clangd/TUScheduler.cpp
  clang-tools-extra/clangd/TUScheduler.h
  clang-tools-extra/clangd/unittests/ClangdTests.cpp
  clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
  clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp
  clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp
  clang-tools-extra/clangd/unittests/XRefsTests.cpp

Index: clang-tools-extra/clangd/unittests/XRefsTests.cpp
===
--- clang-tools-extra/clangd/unittests/XRefsTests.cpp
+++ clang-tools-extra/clangd/unittests/XRefsTests.cpp
@@ -10,6 +10,7 @@
 #include "Compiler.h"
 #include "Matchers.h"
 #include "Protocol.h"
+#include "SemanticHighlighting.h"
 #include "SyncAPI.h"
 #include "TestFS.h"
 #include "TestTU.h"
@@ -33,9 +34,13 @@
 using ::testing::Matcher;
 using ::testing::UnorderedElementsAreArray;
 
-class IgnoreDiagnostics : public DiagnosticsConsumer {
+class IgnoreDiagnostics : public DiagnosticsConsumer, public HighlightingsConsumer {
   void onDiagnosticsReady(PathRef File,
   std::vector Diagnostics) override {}
+
+  virtual void
+  onHighlightingsReady(PathRef File,
+std::vector Highlightings) override {}
 };
 
 MATCHER_P2(FileRange, File, Range, "") {
@@ -539,7 +544,7 @@
 
   IgnoreDiagnostics DiagConsumer;
   MockFSProvider FS;
-  ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
+  ClangdServer Server(CDB, FS, DiagConsumer, DiagConsumer, ClangdServer::optsForTest());
 
   // Fill the filesystem.
   auto FooCpp = testPath("src/foo.cpp");
@@ -1791,7 +1796,7 @@
   MockFSProvider FS;
   IgnoreDiagnostics DiagConsumer;
   MockCompilationDatabase CDB;
-  ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
+  ClangdServer Server(CDB, FS, DiagConsumer, DiagConsumer, ClangdServer::optsForTest());
 
   auto FooCpp = testPath("foo.cpp");
   const char *SourceContents = R"cpp(
@@ -1866,7 +1871,7 @@
   MockFSProvider FS;
   IgnoreDiagnostics DiagConsumer;
   MockCompilationDatabase CDB;
-  ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
+  ClangdServer Server(CDB, FS, DiagConsumer, DiagConsumer, ClangdServer::optsForTest());
 
   auto FooCpp = testPath("foo.cpp");
   // The trigger locations must be the same.
Index: clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp
===
--- clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp
+++ clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp
@@ -7,6 +7,7 @@
 //===--===//
 
 #include "Annotations.h"
+#include "ClangdServer.h"
 #include "Context.h"
 #include "Matchers.h"
 #include "TUScheduler.h"
@@ -44,9 +45,10 @@
 
   void updateWithCallback(TUScheduler , PathRef File,
   llvm::StringRef Contents, WantDiagnostics WD,
+  bool WantHighlight,
   llvm::unique_function CB) {
 WithContextValue Ctx(llvm::make_scope_exit(std::move(CB)));
-S.update(File, getInputs(File, Contents), WD);
+S.update(File, getInputs(File, Contents), WD, WantHighlight);
   }
 
   static Key)>>
@@ -71,7 +73,7 @@
   /// any. The TUScheduler should be created with captureDiags as a
   /// DiagsCallback for this to work.
   void updateWithDiags(TUScheduler , PathRef File, ParseInputs Inputs,
-   WantDiagnostics WD,
+   WantDiagnostics WD, bool WantHighlight,
llvm::unique_function)> CB) {
 Path OrigFile = File.str();
 WithContextValue Ctx(
@@ -82,13 +84,13 @@
   CB(std::move(Diags));
 },
 std::move(CB)));
-S.update(File, std::move(Inputs), WD);
+S.update(File, std::move(Inputs), WD, WantHighlight);
   }
 
   void updateWithDiags(TUScheduler , PathRef File, llvm::StringRef Contents,
-   WantDiagnostics WD,
+   WantDiagnostics WD, bool WantHighlight,
llvm::unique_function)> CB) {
-return updateWithDiags(S, File, getInputs(File, Contents), WD,
+return updateWithDiags(S, File, getInputs(File, Contents), WD, WantHighlight,
std::move(CB));
   }
 
@@ -113,7 +115,7 @@
   Files[Missing] = "";
 
   EXPECT_EQ(S.getContents(Added), "");
-  

[PATCH] D63817: [clangd] No longer getting template instantiations from header files in Main AST.

2019-06-26 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom marked 3 inline comments as done.
jvikstrom added inline comments.



Comment at: clang-tools-extra/clangd/ClangdUnit.cpp:75
+  if (!SM->isInMainFile(D->getLocation()))
+// This decl comes from another file and should not be included in the
+// top level decls.

hokein wrote:
> nit: This comment just repeats the code, I think the comment describe why we 
> need to do this check (because of the template instantiation?)  
Removed the D->isFromASTFile because this if actually covers that as well. 
Don't know what to do about comments though as it is not only specific to that 
case.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63817



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D63817: [clangd] No longer getting template instantiations from header files in Main AST.

2019-06-26 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 206664.
jvikstrom added a comment.

Removed comment, getting SM from Decl and removed old check for checking the 
file.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63817

Files:
  clang-tools-extra/clangd/ClangdUnit.cpp
  clang-tools-extra/clangd/unittests/ClangdUnitTests.cpp


Index: clang-tools-extra/clangd/unittests/ClangdUnitTests.cpp
===
--- clang-tools-extra/clangd/unittests/ClangdUnitTests.cpp
+++ clang-tools-extra/clangd/unittests/ClangdUnitTests.cpp
@@ -83,6 +83,23 @@
   EXPECT_THAT(AST.getLocalTopLevelDecls(), ElementsAre(DeclNamed("main")));
 }
 
+TEST(ClangdUnitTest, DoesNotGetIncludedTopDecls) {
+  TestTU TU;
+  TU.HeaderCode = R"cpp(
+template
+struct H {
+  H() {}
+};
+  )cpp";
+  TU.Code = R"cpp(
+int main() {
+  H h;
+}
+  )cpp";
+  auto AST = TU.build();
+  EXPECT_THAT(AST.getLocalTopLevelDecls(), ElementsAre(DeclNamed("main")));
+}
+
 TEST(ClangdUnitTest, TokensAfterPreamble) {
   TestTU TU;
   TU.AdditionalFiles["foo.h"] = R"(
Index: clang-tools-extra/clangd/ClangdUnit.cpp
===
--- clang-tools-extra/clangd/ClangdUnit.cpp
+++ clang-tools-extra/clangd/ClangdUnit.cpp
@@ -44,6 +44,7 @@
 #include "llvm/Support/raw_ostream.h"
 #include 
 #include 
+#include 
 
 namespace clang {
 namespace clangd {
@@ -67,7 +68,7 @@
 
   bool HandleTopLevelDecl(DeclGroupRef DG) override {
 for (Decl *D : DG) {
-  if (D->isFromASTFile())
+  if 
(!D->getASTContext().getSourceManager().isInMainFile(D->getLocation()))
 continue;
 
   // ObjCMethodDecl are not actually top-level decls.


Index: clang-tools-extra/clangd/unittests/ClangdUnitTests.cpp
===
--- clang-tools-extra/clangd/unittests/ClangdUnitTests.cpp
+++ clang-tools-extra/clangd/unittests/ClangdUnitTests.cpp
@@ -83,6 +83,23 @@
   EXPECT_THAT(AST.getLocalTopLevelDecls(), ElementsAre(DeclNamed("main")));
 }
 
+TEST(ClangdUnitTest, DoesNotGetIncludedTopDecls) {
+  TestTU TU;
+  TU.HeaderCode = R"cpp(
+template
+struct H {
+  H() {}
+};
+  )cpp";
+  TU.Code = R"cpp(
+int main() {
+  H h;
+}
+  )cpp";
+  auto AST = TU.build();
+  EXPECT_THAT(AST.getLocalTopLevelDecls(), ElementsAre(DeclNamed("main")));
+}
+
 TEST(ClangdUnitTest, TokensAfterPreamble) {
   TestTU TU;
   TU.AdditionalFiles["foo.h"] = R"(
Index: clang-tools-extra/clangd/ClangdUnit.cpp
===
--- clang-tools-extra/clangd/ClangdUnit.cpp
+++ clang-tools-extra/clangd/ClangdUnit.cpp
@@ -44,6 +44,7 @@
 #include "llvm/Support/raw_ostream.h"
 #include 
 #include 
+#include 
 
 namespace clang {
 namespace clangd {
@@ -67,7 +68,7 @@
 
   bool HandleTopLevelDecl(DeclGroupRef DG) override {
 for (Decl *D : DG) {
-  if (D->isFromASTFile())
+  if (!D->getASTContext().getSourceManager().isInMainFile(D->getLocation()))
 continue;
 
   // ObjCMethodDecl are not actually top-level decls.
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D63919: [clangd] Emit publishSemanticHighlighting in LSP if enabled

2019-06-28 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom created this revision.
jvikstrom added reviewers: hokein, kadircet.
Herald added subscribers: cfe-commits, arphaman, jkorous, MaskRay, 
ilya-biryukov.
Herald added a project: clang.

Emit publishSemanticHighlighting in LSP if enabled


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D63919

Files:
  clang-tools-extra/clangd/ClangdLSPServer.cpp
  clang-tools-extra/clangd/ClangdLSPServer.h
  clang-tools-extra/clangd/Protocol.cpp
  clang-tools-extra/clangd/Protocol.h
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/SemanticHighlighting.h
  clang-tools-extra/clangd/test/initialize-params.test

Index: clang-tools-extra/clangd/test/initialize-params.test
===
--- clang-tools-extra/clangd/test/initialize-params.test
+++ clang-tools-extra/clangd/test/initialize-params.test
@@ -33,6 +33,16 @@
 # CHECK-NEXT:  "hoverProvider": true,
 # CHECK-NEXT:  "referencesProvider": true,
 # CHECK-NEXT:  "renameProvider": true,
+# CHECK-NEXT:  "semanticHighlighting": {
+# CHECK-NEXT:"scopes": [
+# CHECK-NEXT:  [
+# CHECK-NEXT:"variable.cpp"
+# CHECK-NEXT:  ],
+# CHECK-NEXT:  [
+# CHECK-NEXT:"entity.name.function.cpp"
+# CHECK-NEXT:  ]
+# CHECK-NEXT:]
+# CHECK-NEXT:  },
 # CHECK-NEXT:  "signatureHelpProvider": {
 # CHECK-NEXT:"triggerCharacters": [
 # CHECK-NEXT:  "(",
Index: clang-tools-extra/clangd/SemanticHighlighting.h
===
--- clang-tools-extra/clangd/SemanticHighlighting.h
+++ clang-tools-extra/clangd/SemanticHighlighting.h
@@ -27,10 +27,34 @@
 
 bool operator==(const HighlightingToken , const HighlightingToken );
 
+// Contains all highlightings in a single line.
+struct LineHighlighting {
+  LineHighlighting(unsigned int Line = 0,
+   std::vector Tokens = {})
+  : Line(Line), Tokens(Tokens) {}
+  unsigned int Line;
+  std::vector Tokens;
+};
+
+llvm::json::Value toJSON(const LineHighlighting );
+
 // Returns all HighlightingTokens from an AST. Only generates highlights for the
 // main AST.
 std::vector getSemanticHighlightings(ParsedAST );
 
+// Gets the TextMate scopes as a double nested array where the
+// SemanticHighlightKind indexes correctly into this vector.
+std::vector> getSemanticTextMateScopes();
+
+// Converts a vector of HighlightingTokens to a vector of LineHighlightings.
+// Assumes the HighlightingToken's vector is ordered in ascending order by line
+// and secondarily by column of the token's start position. The returned
+// LineHighlighting vector will be ordered in ascending order according to the
+// line number. The tokens in a LineHighlighting are ordered in ascending order
+// according to the token's start character.
+std::vector
+highlightingTokensToLines(llvm::ArrayRef Tokens);
+
 } // namespace clangd
 } // namespace clang
 
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -63,8 +63,69 @@
   }
 };
 
+// Encode binary data into base64.
+// This was copied from compiler-rt/lib/fuzzer/FuzzerUtil.cpp.
+// FIXME: Factor this out into llvm/Support?
+std::string encodeBase64(const llvm::SmallVectorImpl ) {
+  static const char Table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+  "abcdefghijklmnopqrstuvwxyz"
+  "0123456789+/";
+  std::string Res;
+  size_t I;
+  for (I = 0; I + 2 < U.size(); I += 3) {
+uint32_t X = (U[I] << 16) + (U[I + 1] << 8) + U[I + 2];
+Res += Table[(X >> 18) & 63];
+Res += Table[(X >> 12) & 63];
+Res += Table[(X >> 6) & 63];
+Res += Table[X & 63];
+  }
+  if (I + 1 == U.size()) {
+uint32_t X = (U[I] << 16);
+Res += Table[(X >> 18) & 63];
+Res += Table[(X >> 12) & 63];
+Res += "==";
+  } else if (I + 2 == U.size()) {
+uint32_t X = (U[I] << 16) + (U[I + 1] << 8);
+Res += Table[(X >> 18) & 63];
+Res += Table[(X >> 12) & 63];
+Res += Table[(X >> 6) & 63];
+Res += "=";
+  }
+  return Res;
+}
+
+void write32be(uint32_t I, llvm::raw_ostream ) {
+  std::array Buf;
+  llvm::support::endian::write32be(Buf.data(), I);
+  OS.write(Buf.data(), Buf.size());
+}
+
+void write16be(uint16_t I, llvm::raw_ostream ) {
+  std::array Buf;
+  llvm::support::endian::write16be(Buf.data(), I);
+  OS.write(Buf.data(), Buf.size());
+}
 } // namespace
 
+llvm::json::Value toJSON(const LineHighlighting ) {
+  llvm::SmallVector BinaryHighlights;
+  llvm::raw_svector_ostream OS(BinaryHighlights);
+
+  for (size_t I = 0, End = Highlight.Tokens.size(); I < End; I++) {
+// Each token should consists of 2 32 bit integers. The first integer is the
+// start column of the token. The second integer's first 16 bits are 

[PATCH] D63919: [clangd] Emit publishSemanticHighlighting in LSP if enabled

2019-06-28 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 207031.
jvikstrom marked an inline comment as done.
jvikstrom added a comment.

Added SemanticHighlightingInformation and SemantigHighlightingParams.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63919

Files:
  clang-tools-extra/clangd/ClangdLSPServer.cpp
  clang-tools-extra/clangd/ClangdLSPServer.h
  clang-tools-extra/clangd/Protocol.cpp
  clang-tools-extra/clangd/Protocol.h
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/SemanticHighlighting.h
  clang-tools-extra/clangd/test/initialize-params.test
  clang-tools-extra/clangd/test/semantic-highlighting.test

Index: clang-tools-extra/clangd/test/semantic-highlighting.test
===
--- /dev/null
+++ clang-tools-extra/clangd/test/semantic-highlighting.test
@@ -0,0 +1,21 @@
+# RUN: clangd -lit-test < %s | FileCheck -strict-whitespace %s
+{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{"textDocument": {"semanticHighlightingCapabilities": {"semanticHighlighting": true}}},"trace":"off"}}
+---
+{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"uri":"test:///foo.cpp","languageId":"cpp","version":1,"text":"int x = 2;"}}}
+#  CHECK:  "method": "textDocument/semanticHighlighting",
+# CHECK-NEXT:  "params": {
+# CHECK-NEXT:"lines": [
+# CHECK-NEXT:  {
+# CHECK-NEXT:"line": 0,
+# CHECK-NEXT:"tokens": "BAABAAA="
+# CHECK-NEXT:  }
+# CHECK-NEXT:],
+# CHECK-NEXT:"textDocument": {
+# CHECK-NEXT:  "uri": "file:///clangd-test/foo.cpp"
+# CHECK-NEXT:}
+# CHECK-NEXT:  }
+# CHECK-NEXT:}
+---
+{"jsonrpc":"2.0","id":3,"method":"shutdown"}
+---
+{"jsonrpc":"2.0","method":"exit"}
Index: clang-tools-extra/clangd/test/initialize-params.test
===
--- clang-tools-extra/clangd/test/initialize-params.test
+++ clang-tools-extra/clangd/test/initialize-params.test
@@ -33,6 +33,16 @@
 # CHECK-NEXT:  "hoverProvider": true,
 # CHECK-NEXT:  "referencesProvider": true,
 # CHECK-NEXT:  "renameProvider": true,
+# CHECK-NEXT:  "semanticHighlighting": {
+# CHECK-NEXT:"scopes": [
+# CHECK-NEXT:  [
+# CHECK-NEXT:"variable.cpp"
+# CHECK-NEXT:  ],
+# CHECK-NEXT:  [
+# CHECK-NEXT:"entity.name.function.cpp"
+# CHECK-NEXT:  ]
+# CHECK-NEXT:]
+# CHECK-NEXT:  },
 # CHECK-NEXT:  "signatureHelpProvider": {
 # CHECK-NEXT:"triggerCharacters": [
 # CHECK-NEXT:  "(",
Index: clang-tools-extra/clangd/SemanticHighlighting.h
===
--- clang-tools-extra/clangd/SemanticHighlighting.h
+++ clang-tools-extra/clangd/SemanticHighlighting.h
@@ -10,6 +10,7 @@
 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICHIGHLIGHTING_H
 
 #include "ClangdUnit.h"
+#include "Protocol.h"
 
 namespace clang {
 namespace clangd {
@@ -31,6 +32,20 @@
 // main AST.
 std::vector getSemanticHighlightings(ParsedAST );
 
+// Gets the TextMate scopes as a double nested array where the
+// SemanticHighlightKind indexes correctly into this vector.
+std::vector> getSemanticTextMateScopes();
+
+// Converts a vector of HighlightingTokens to a vector of
+// SemanticHighlightingInformation. Assumes the HighlightingToken's vector is
+// ordered in ascending order by line and secondarily by column of the token's
+// start position. The returned SemanticHighlightingInformation vector will be
+// ordered in ascending order according to the line number. The token string in
+// SemanticHighlightingInformation is base64 encoded.
+std::vector
+highlightingTokensToSemanticHighlightingInformation(
+llvm::ArrayRef Tokens);
+
 } // namespace clangd
 } // namespace clang
 
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -8,6 +8,7 @@
 
 #include "SemanticHighlighting.h"
 #include "Logger.h"
+#include "Protocol.h"
 #include "SourceCode.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/RecursiveASTVisitor.h"
@@ -63,6 +64,48 @@
   }
 };
 
+// Encode binary data into base64.
+// This was copied from compiler-rt/lib/fuzzer/FuzzerUtil.cpp.
+// FIXME: Factor this out into llvm/Support?
+std::string encodeBase64(const llvm::SmallVectorImpl ) {
+  static const char Table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+  "abcdefghijklmnopqrstuvwxyz"
+  "0123456789+/";
+  std::string Res;
+  size_t I;
+  for (I = 0; I + 2 < U.size(); I += 3) {
+uint32_t X = (U[I] << 16) + (U[I + 1] << 8) + U[I + 2];
+Res += Table[(X >> 18) & 63];
+Res += Table[(X >> 12) & 

[PATCH] D64492: [clangd] Added highlightings for namespace specifiers.

2019-07-10 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom created this revision.
jvikstrom added reviewers: hokein, sammccall, ilya-biryukov.
Herald added subscribers: cfe-commits, kadircet, arphaman, jkorous, MaskRay.
Herald added a project: clang.

Added highlightings for namespace specifiers.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D64492

Files:
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/SemanticHighlighting.h
  clang-tools-extra/clangd/test/semantic-highlighting.test
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -36,7 +36,8 @@
   {HighlightingKind::Variable, "Variable"},
   {HighlightingKind::Function, "Function"},
   {HighlightingKind::Class, "Class"},
-  {HighlightingKind::Enum, "Enum"}};
+  {HighlightingKind::Enum, "Enum"},
+  {HighlightingKind::Namespace, "Namespace"}};
   std::vector ExpectedTokens;
   for (const auto  : KindToString) {
 std::vector Toks = makeHighlightingTokens(
@@ -75,18 +76,18 @@
   };
 )cpp",
 R"cpp(
-  namespace abc {
+  namespace $Namespace[[abc]] {
 template
 struct $Class[[A]] {
   T t;
 };
   }
   template
-  struct $Class[[C]] : abc::A {
+  struct $Class[[C]] : $Namespace[[abc]]::A {
 typename T::A* D;
   };
-  abc::$Class[[A]] $Variable[[AA]];
-  typedef abc::$Class[[A]] AAA;
+  $Namespace[[abc]]::$Class[[A]] $Variable[[AA]];
+  typedef $Namespace[[abc]]::$Class[[A]] AAA;
   struct $Class[[B]] {
 $Class[[B]]();
 ~$Class[[B]]();
@@ -108,6 +109,26 @@
 $Enum[[E]] EEE;
 $Enum[[EE]] ;
   };
+)cpp",
+R"cpp(
+  namespace $Namespace[[abc]] {
+namespace $Namespace[[bcd]] {
+  struct $Class[[A]] {};
+  namespace $Namespace[[cde]] {
+struct $Class[[A]] {
+  static enum class $Enum[[B]] {
+Hi,
+  };
+};
+  }
+}
+  }
+  using namespace $Namespace[[abc]]::$Namespace[[bcd]];
+  namespace $Namespace[[vwz]] =
+$Namespace[[abc]]::$Namespace[[bcd]]::$Namespace[[cde]];
+  $Namespace[[abc]]::$Namespace[[bcd]]::$Class[[A]] $Variable[[AA]];
+  $Namespace[[vwz]]::$Class[[A]]::$Enum[[B]] $Variable[[AAA]] =
+$Namespace[[vwz]]::$Class[[A]]::$Enum[[B]]::Hi;
 )cpp"};
   for (const auto  : TestCases) {
 checkHighlightings(TestCase);
Index: clang-tools-extra/clangd/test/semantic-highlighting.test
===
--- clang-tools-extra/clangd/test/semantic-highlighting.test
+++ clang-tools-extra/clangd/test/semantic-highlighting.test
@@ -9,12 +9,15 @@
 # CHECK-NEXT:  ],
 # CHECK-NEXT:  [
 # CHECK-NEXT:"entity.name.function.cpp"
-# CHECK-NEXT:  ]
+# CHECK-NEXT:  ],
 # CHECK-NEXT:  [
 # CHECK-NEXT:"entity.name.type.class.cpp"
 # CHECK-NEXT:  ],
 # CHECK-NEXT:  [
 # CHECK-NEXT:"entity.name.type.enum.cpp"
+# CHECK-NEXT:  ],
+# CHECK-NEXT:  [
+# CHECK-NEXT:"entity.name.type.namespace.cpp"
 # CHECK-NEXT:  ]
 # CHECK-NEXT:]
 # CHECK-NEXT:  },
Index: clang-tools-extra/clangd/SemanticHighlighting.h
===
--- clang-tools-extra/clangd/SemanticHighlighting.h
+++ clang-tools-extra/clangd/SemanticHighlighting.h
@@ -28,6 +28,7 @@
   Function,
   Class,
   Enum,
+  Namespace,
 
   NumKinds,
 };
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -11,8 +11,6 @@
 #include "Protocol.h"
 #include "SourceCode.h"
 #include "clang/AST/ASTContext.h"
-#include "clang/AST/Decl.h"
-#include "clang/AST/DeclarationName.h"
 #include "clang/AST/RecursiveASTVisitor.h"
 
 namespace clang {
@@ -37,6 +35,13 @@
   }
 
   bool VisitNamedDecl(NamedDecl *ND) {
+// UsingDirectiveDecl's namespaces do not show up anywhere else in the
+// Visit/Traverse mehods. But they should also be highlighted as a
+// namespace.
+if (const auto UD = dyn_cast(ND)) {
+  addToken(UD->getIdentLocation(), HighlightingKind::Namespace);
+}
+
 // Constructors' TypeLoc has a TypePtr that is a FunctionProtoType. It has
 // no tag decl and therefore constructors must be gotten as NamedDecls
 // instead.
@@ -76,6 +81,16 @@
 return true;
   }
 
+  bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNSLoc) {
+if (NestedNameSpecifier *NNS = 

[PATCH] D64492: [clangd] Added highlightings for namespace specifiers.

2019-07-10 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 208998.
jvikstrom marked 6 inline comments as done.
jvikstrom added a comment.

Addressed comments.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64492

Files:
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/SemanticHighlighting.h
  clang-tools-extra/clangd/test/semantic-highlighting.test
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -36,7 +36,8 @@
   {HighlightingKind::Variable, "Variable"},
   {HighlightingKind::Function, "Function"},
   {HighlightingKind::Class, "Class"},
-  {HighlightingKind::Enum, "Enum"}};
+  {HighlightingKind::Enum, "Enum"},
+  {HighlightingKind::Namespace, "Namespace"}};
   std::vector ExpectedTokens;
   for (const auto  : KindToString) {
 std::vector Toks = makeHighlightingTokens(
@@ -75,18 +76,18 @@
   };
 )cpp",
 R"cpp(
-  namespace abc {
+  namespace $Namespace[[abc]] {
 template
 struct $Class[[A]] {
   T t;
 };
   }
   template
-  struct $Class[[C]] : abc::A {
+  struct $Class[[C]] : $Namespace[[abc]]::A {
 typename T::A* D;
   };
-  abc::$Class[[A]] $Variable[[AA]];
-  typedef abc::$Class[[A]] AAA;
+  $Namespace[[abc]]::$Class[[A]] $Variable[[AA]];
+  typedef $Namespace[[abc]]::$Class[[A]] AAA;
   struct $Class[[B]] {
 $Class[[B]]();
 ~$Class[[B]]();
@@ -108,6 +109,27 @@
 $Enum[[E]] EEE;
 $Enum[[EE]] ;
   };
+)cpp",
+R"cpp(
+  namespace $Namespace[[abc]] {
+namespace {}
+namespace $Namespace[[bcd]] {
+  struct $Class[[A]] {};
+  namespace $Namespace[[cde]] {
+struct $Class[[A]] {
+  enum class $Enum[[B]] {
+Hi,
+  };
+};
+  }
+}
+  }
+  using namespace $Namespace[[abc]]::$Namespace[[bcd]];
+  namespace $Namespace[[vwz]] =
+$Namespace[[abc]]::$Namespace[[bcd]]::$Namespace[[cde]];
+  $Namespace[[abc]]::$Namespace[[bcd]]::$Class[[A]] $Variable[[AA]];
+  $Namespace[[vwz]]::$Class[[A]]::$Enum[[B]] $Variable[[AAA]] =
+$Namespace[[vwz]]::$Class[[A]]::$Enum[[B]]::Hi;
 )cpp"};
   for (const auto  : TestCases) {
 checkHighlightings(TestCase);
Index: clang-tools-extra/clangd/test/semantic-highlighting.test
===
--- clang-tools-extra/clangd/test/semantic-highlighting.test
+++ clang-tools-extra/clangd/test/semantic-highlighting.test
@@ -9,12 +9,15 @@
 # CHECK-NEXT:  ],
 # CHECK-NEXT:  [
 # CHECK-NEXT:"entity.name.function.cpp"
-# CHECK-NEXT:  ]
+# CHECK-NEXT:  ],
 # CHECK-NEXT:  [
 # CHECK-NEXT:"entity.name.type.class.cpp"
 # CHECK-NEXT:  ],
 # CHECK-NEXT:  [
 # CHECK-NEXT:"entity.name.type.enum.cpp"
+# CHECK-NEXT:  ],
+# CHECK-NEXT:  [
+# CHECK-NEXT:"entity.name.type.namespace.cpp"
 # CHECK-NEXT:  ]
 # CHECK-NEXT:]
 # CHECK-NEXT:  },
Index: clang-tools-extra/clangd/SemanticHighlighting.h
===
--- clang-tools-extra/clangd/SemanticHighlighting.h
+++ clang-tools-extra/clangd/SemanticHighlighting.h
@@ -28,6 +28,7 @@
   Function,
   Class,
   Enum,
+  Namespace,
 
   NumKinds,
 };
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -11,8 +11,6 @@
 #include "Protocol.h"
 #include "SourceCode.h"
 #include "clang/AST/ASTContext.h"
-#include "clang/AST/Decl.h"
-#include "clang/AST/DeclarationName.h"
 #include "clang/AST/RecursiveASTVisitor.h"
 
 namespace clang {
@@ -37,6 +35,14 @@
   }
 
   bool VisitNamedDecl(NamedDecl *ND) {
+// UsingDirectiveDecl's namespaces do not show up anywhere else in the
+// Visit/Traverse mehods. But they should also be highlighted as a
+// namespace.
+if (const auto *UD = dyn_cast(ND)) {
+  addToken(UD->getIdentLocation(), HighlightingKind::Namespace);
+  return true;
+}
+
 // Constructors' TypeLoc has a TypePtr that is a FunctionProtoType. It has
 // no tag decl and therefore constructors must be gotten as NamedDecls
 // instead.
@@ -65,17 +71,28 @@
 
   bool VisitTypeLoc(TypeLoc ) {
 // This check is for not getting two entries when there are anonymous
-// structs. It also makes us not 

[PATCH] D64492: [clangd] Added highlightings for namespace specifiers.

2019-07-10 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom added inline comments.



Comment at: clang-tools-extra/clangd/SemanticHighlighting.cpp:73
 // This check is for not getting two entries when there are anonymous
 // structs. It also makes us not highlight namespace qualifiers. For
 // elaborated types the actual type is highlighted as an inner TypeLoc.

hokein wrote:
> this comment is stale with this patch, now we are highlighting namespace, it 
> seems more natural to do it here, we can get a NestedNameSpecifierLoc from an 
> `ElaboratedTypeLoc ` (so that we don't need the 
> `TraverseNestedNameSpecifierLoc`).
Doing it in VisitTypeLoc means that we fail on this testcase:

```
  namespace $Namespace[[aa]] {
namespace $Namespace[[bb]] {
  struct $Class[[A]] {};
}
  }
  $Namespace[[aa]]::$Namespace[[bb]]::$Class[[A]] $Variable[[a]];
```

It can't detect the `bb` namespace qualifier in `aa::bb::A a;`.

Maybe there is some way of solving this without 
`TraverseNestedNameSpecifierLoc` that I am not aware of?
Also don't know how I'd get a `NestedNameSpecifierLoc` from an 
`ElaboratedTypeLoc`.



Comment at: clang-tools-extra/clangd/SemanticHighlighting.cpp:247
+  case HighlightingKind::Namespace:
+return "entity.name.type.namespace.cpp";
   case HighlightingKind::NumKinds:

hokein wrote:
> I think here should be `entity.name.namespace.cpp`, vscode uses this TX scope 
> for namespace.
Really? Because I see `entity.name.type.namespace.cpp` when I inspect the TM 
scopes for namespaces.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64492



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D64492: [clangd] Added highlightings for namespace specifiers.

2019-07-11 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 209139.
jvikstrom marked 4 inline comments as done.
jvikstrom added a comment.

Moved alias target namespace add token to another function and added testcase 
for global namespace specifier.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64492

Files:
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/SemanticHighlighting.h
  clang-tools-extra/clangd/test/semantic-highlighting.test
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -36,7 +36,8 @@
   {HighlightingKind::Variable, "Variable"},
   {HighlightingKind::Function, "Function"},
   {HighlightingKind::Class, "Class"},
-  {HighlightingKind::Enum, "Enum"}};
+  {HighlightingKind::Enum, "Enum"},
+  {HighlightingKind::Namespace, "Namespace"}};
   std::vector ExpectedTokens;
   for (const auto  : KindToString) {
 std::vector Toks = makeHighlightingTokens(
@@ -75,18 +76,18 @@
   };
 )cpp",
 R"cpp(
-  namespace abc {
+  namespace $Namespace[[abc]] {
 template
 struct $Class[[A]] {
   T t;
 };
   }
   template
-  struct $Class[[C]] : abc::A {
+  struct $Class[[C]] : $Namespace[[abc]]::A {
 typename T::A* D;
   };
-  abc::$Class[[A]] $Variable[[AA]];
-  typedef abc::$Class[[A]] AAA;
+  $Namespace[[abc]]::$Class[[A]] $Variable[[AA]];
+  typedef $Namespace[[abc]]::$Class[[A]] AAA;
   struct $Class[[B]] {
 $Class[[B]]();
 ~$Class[[B]]();
@@ -108,6 +109,29 @@
 $Enum[[E]] EEE;
 $Enum[[EE]] ;
   };
+)cpp",
+R"cpp(
+  namespace $Namespace[[abc]] {
+namespace {}
+namespace $Namespace[[bcd]] {
+  struct $Class[[A]] {};
+  namespace $Namespace[[cde]] {
+struct $Class[[A]] {
+  enum class $Enum[[B]] {
+Hi,
+  };
+};
+  }
+}
+  }
+  using namespace $Namespace[[abc]]::$Namespace[[bcd]];
+  namespace $Namespace[[vwz]] =
+$Namespace[[abc]]::$Namespace[[bcd]]::$Namespace[[cde]];
+  $Namespace[[abc]]::$Namespace[[bcd]]::$Class[[A]] $Variable[[AA]];
+  $Namespace[[vwz]]::$Class[[A]]::$Enum[[B]] $Variable[[AAA]] =
+$Namespace[[vwz]]::$Class[[A]]::$Enum[[B]]::Hi;
+  ::$Namespace[[vwz]]::$Class[[A]] $Variable[[B]];
+  ::$Namespace[[abc]]::$Namespace[[bcd]]::$Class[[A]] $Variable[[BB]];
 )cpp"};
   for (const auto  : TestCases) {
 checkHighlightings(TestCase);
Index: clang-tools-extra/clangd/test/semantic-highlighting.test
===
--- clang-tools-extra/clangd/test/semantic-highlighting.test
+++ clang-tools-extra/clangd/test/semantic-highlighting.test
@@ -9,12 +9,15 @@
 # CHECK-NEXT:  ],
 # CHECK-NEXT:  [
 # CHECK-NEXT:"entity.name.function.cpp"
-# CHECK-NEXT:  ]
+# CHECK-NEXT:  ],
 # CHECK-NEXT:  [
 # CHECK-NEXT:"entity.name.type.class.cpp"
 # CHECK-NEXT:  ],
 # CHECK-NEXT:  [
 # CHECK-NEXT:"entity.name.type.enum.cpp"
+# CHECK-NEXT:  ],
+# CHECK-NEXT:  [
+# CHECK-NEXT:"entity.name.type.namespace.cpp"
 # CHECK-NEXT:  ]
 # CHECK-NEXT:]
 # CHECK-NEXT:  },
Index: clang-tools-extra/clangd/SemanticHighlighting.h
===
--- clang-tools-extra/clangd/SemanticHighlighting.h
+++ clang-tools-extra/clangd/SemanticHighlighting.h
@@ -28,6 +28,7 @@
   Function,
   Class,
   Enum,
+  Namespace,
 
   NumKinds,
 };
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -11,8 +11,6 @@
 #include "Protocol.h"
 #include "SourceCode.h"
 #include "clang/AST/ASTContext.h"
-#include "clang/AST/Decl.h"
-#include "clang/AST/DeclarationName.h"
 #include "clang/AST/RecursiveASTVisitor.h"
 
 namespace clang {
@@ -36,7 +34,21 @@
 return Tokens;
   }
 
+  bool VisitNamespaceAliasDecl(NamespaceAliasDecl *NAD) {
+// The target namespace of an alias can not be found in any other way.
+addToken(NAD->getTargetNameLoc(), HighlightingKind::Namespace);
+return true;
+  }
+
   bool VisitNamedDecl(NamedDecl *ND) {
+// UsingDirectiveDecl's namespaces do not show up anywhere else in the
+// Visit/Traverse mehods. But they should also be highlighted as a
+// namespace.
+if 

[PATCH] D64492: [clangd] Added highlightings for namespace specifiers.

2019-07-11 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom added inline comments.



Comment at: clang-tools-extra/clangd/SemanticHighlighting.cpp:87
+  bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNSLoc) {
+if (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier())
+  if (NNS->getKind() == NestedNameSpecifier::Namespace ||

sammccall wrote:
> if you're just doing something and then calling base, can you make this Visit 
> instead of Traverse?
NestedNameSpecifierLoc does not have a visit method for some reason. It only 
has a traverse method and this seems to be the way people get nested namespaces 
from what I can tell. (Maybe a Visit method is something that should be added 
to the RecursiveASTVisitor, but I wouldn't do that in this patch).



Comment at: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp:86
   template
-  struct $Class[[C]] : abc::A {
+  struct $Class[[C]] : $Namespace[[abc]]::A {
 typename T::A* D;

sammccall wrote:
> can you add a case where we spell with leading colons e.g. `::abc::A`
Added it in the case that focuses on namespaces further down.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64492



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D64492: [clangd] Added highlightings for namespace specifiers.

2019-07-11 Thread Johan Vikström via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL365745: [clangd] Added highlightings for namespace 
specifiers. (authored by jvikstrom, committed by ).
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D64492?vs=209139=209146#toc

Repository:
  rL LLVM

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

https://reviews.llvm.org/D64492

Files:
  clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp
  clang-tools-extra/trunk/clangd/SemanticHighlighting.h
  clang-tools-extra/trunk/clangd/test/semantic-highlighting.test
  clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp
@@ -11,8 +11,6 @@
 #include "Protocol.h"
 #include "SourceCode.h"
 #include "clang/AST/ASTContext.h"
-#include "clang/AST/Decl.h"
-#include "clang/AST/DeclarationName.h"
 #include "clang/AST/RecursiveASTVisitor.h"
 
 namespace clang {
@@ -36,7 +34,21 @@
 return Tokens;
   }
 
+  bool VisitNamespaceAliasDecl(NamespaceAliasDecl *NAD) {
+// The target namespace of an alias can not be found in any other way.
+addToken(NAD->getTargetNameLoc(), HighlightingKind::Namespace);
+return true;
+  }
+
   bool VisitNamedDecl(NamedDecl *ND) {
+// UsingDirectiveDecl's namespaces do not show up anywhere else in the
+// Visit/Traverse mehods. But they should also be highlighted as a
+// namespace.
+if (const auto *UD = dyn_cast(ND)) {
+  addToken(UD->getIdentLocation(), HighlightingKind::Namespace);
+  return true;
+}
+
 // Constructors' TypeLoc has a TypePtr that is a FunctionProtoType. It has
 // no tag decl and therefore constructors must be gotten as NamedDecls
 // instead.
@@ -65,17 +77,28 @@
 
   bool VisitTypeLoc(TypeLoc ) {
 // This check is for not getting two entries when there are anonymous
-// structs. It also makes us not highlight namespace qualifiers. For
-// elaborated types the actual type is highlighted as an inner TypeLoc.
+// structs. It also makes us not highlight certain namespace qualifiers
+// twice. For elaborated types the actual type is highlighted as an inner
+// TypeLoc.
 if (TL.getTypeLocClass() == TypeLoc::TypeLocClass::Elaborated)
   return true;
 
 if (const Type *TP = TL.getTypePtr())
   if (const TagDecl *TD = TP->getAsTagDecl())
-  addToken(TL.getBeginLoc(), TD);
+addToken(TL.getBeginLoc(), TD);
 return true;
   }
 
+  bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNSLoc) {
+if (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier())
+  if (NNS->getKind() == NestedNameSpecifier::Namespace ||
+  NNS->getKind() == NestedNameSpecifier::NamespaceAlias)
+addToken(NNSLoc.getLocalBeginLoc(), HighlightingKind::Namespace);
+
+return RecursiveASTVisitor<
+HighlightingTokenCollector>::TraverseNestedNameSpecifierLoc(NNSLoc);
+  }
+
 private:
   void addToken(SourceLocation Loc, const NamedDecl *D) {
 if (D->getDeclName().isIdentifier() && D->getName().empty())
@@ -104,6 +127,14 @@
   addToken(Loc, HighlightingKind::Function);
   return;
 }
+if (isa(D)) {
+  addToken(Loc, HighlightingKind::Namespace);
+  return;
+}
+if (isa(D)) {
+  addToken(Loc, HighlightingKind::Namespace);
+  return;
+}
   }
 
   void addToken(SourceLocation Loc, HighlightingKind Kind) {
@@ -218,6 +249,8 @@
 return "entity.name.type.class.cpp";
   case HighlightingKind::Enum:
 return "entity.name.type.enum.cpp";
+  case HighlightingKind::Namespace:
+return "entity.name.namespace.cpp";
   case HighlightingKind::NumKinds:
 llvm_unreachable("must not pass NumKinds to the function");
   }
Index: clang-tools-extra/trunk/clangd/test/semantic-highlighting.test
===
--- clang-tools-extra/trunk/clangd/test/semantic-highlighting.test
+++ clang-tools-extra/trunk/clangd/test/semantic-highlighting.test
@@ -9,12 +9,15 @@
 # CHECK-NEXT:  ],
 # CHECK-NEXT:  [
 # CHECK-NEXT:"entity.name.function.cpp"
-# CHECK-NEXT:  ]
+# CHECK-NEXT:  ],
 # CHECK-NEXT:  [
 # CHECK-NEXT:"entity.name.type.class.cpp"
 # CHECK-NEXT:  ],
 # CHECK-NEXT:  [
 # CHECK-NEXT:"entity.name.type.enum.cpp"
+# CHECK-NEXT:  ],
+# CHECK-NEXT:  [
+# CHECK-NEXT:"entity.name.namespace.cpp"
 # CHECK-NEXT:  ]
 # CHECK-NEXT:]
 # CHECK-NEXT:  },
Index: clang-tools-extra/trunk/clangd/SemanticHighlighting.h
===
--- 

[PATCH] D64475: [clangd] Duplicate lines of semantic highlightings sent removed.

2019-07-11 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 209228.
jvikstrom added a comment.

Removed unused code snippets.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64475

Files:
  clang-tools-extra/clangd/ClangdLSPServer.cpp
  clang-tools-extra/clangd/ClangdLSPServer.h
  clang-tools-extra/clangd/ClangdServer.cpp
  clang-tools-extra/clangd/ClangdServer.h
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/SemanticHighlighting.h
  clang-tools-extra/clangd/test/semantic-highlighting.test
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -29,7 +29,9 @@
   return Tokens;
 }
 
-void checkHighlightings(llvm::StringRef Code) {
+std::tuple,
+   std::vector>
+getHighlightingsAnnotated(llvm::StringRef Code) {
   Annotations Test(Code);
   auto AST = TestTU::withCode(Test.code()).build();
   static const std::map KindToString{
@@ -46,9 +48,43 @@
   }
 
   auto ActualTokens = getSemanticHighlightings(AST);
+  return {std::move(AST), ActualTokens, ExpectedTokens};
+}
+
+void checkHighlightings(llvm::StringRef Code) {
+  std::vector ActualTokens;
+  std::vector ExpectedTokens;
+  std::tie(std::ignore, ActualTokens, ExpectedTokens) =
+  getHighlightingsAnnotated(Code);
   EXPECT_THAT(ActualTokens, testing::UnorderedElementsAreArray(ExpectedTokens));
 }
 
+void checkDiffedHighlights(
+const std::vector ,
+const std::vector ,
+std::vector>> ) {
+  std::map> ExpectedLines;
+  for (const HighlightingToken  : ExpectedTokens)
+ExpectedLines[Token.R.start.line].push_back(Token);
+  std::vector>>
+  ExpectedLinePairHighlighting;
+  for (int Line : EmptyLines)
+ExpectedLinePairHighlighting.push_back({Line, {}});
+  for (auto  : ExpectedLines) {
+std::sort(LineTokens.second.begin(), LineTokens.second.end());
+ExpectedLinePairHighlighting.push_back(
+{LineTokens.first, LineTokens.second});
+  }
+
+  // The UnorderedElementsAreArray only checks that the top level vector
+  // is unordered. The vectors in the pair must be in the correct order.
+  for (unsigned I = 0, End = ActualDiffed.size(); I < End; ++I)
+std::sort(ActualDiffed[I].second.begin(), ActualDiffed[I].second.end());
+
+  EXPECT_THAT(ActualDiffed,
+  testing::UnorderedElementsAreArray(ExpectedLinePairHighlighting));
+}
+
 TEST(SemanticHighlighting, GetsCorrectTokens) {
   const char *TestCases[] = {
 R"cpp(
@@ -145,7 +181,9 @@
 
 void onDiagnosticsReady(PathRef, std::vector) override {}
 void onHighlightingsReady(
-PathRef File, std::vector Highlightings) override {
+PathRef File,
+std::vector>>
+Highlightings) override {
   ++Count;
 }
   };
@@ -170,21 +208,148 @@
 return Pos;
   };
 
-  std::vector Tokens{
-  {HighlightingKind::Variable,
-Range{CreatePosition(3, 8), CreatePosition(3, 12)}},
-  {HighlightingKind::Function,
-Range{CreatePosition(3, 4), CreatePosition(3, 7)}},
-  {HighlightingKind::Variable,
-Range{CreatePosition(1, 1), CreatePosition(1, 5)}}};
+  std::vector>> Tokens{
+  {3,
+   {{HighlightingKind::Variable,
+ Range{CreatePosition(3, 8), CreatePosition(3, 12)}},
+{HighlightingKind::Function,
+ Range{CreatePosition(3, 4), CreatePosition(3, 7),
+  {1,
+   {{HighlightingKind::Variable,
+ Range{CreatePosition(1, 1), CreatePosition(1, 5)};
   std::vector ActualResults =
   toSemanticHighlightingInformation(Tokens);
   std::vector ExpectedResults = {
-  {1, "AQAEAAA="},
-  {3, "CAAEAAAEAAMAAQ=="}};
+  {3, "CAAEAAAEAAMAAQ=="}, {1, "AQAEAAA="}};
   EXPECT_EQ(ActualResults, ExpectedResults);
 }
 
+TEST(SemanticHighlighting, HighlightingDiffer) {
+  std::vector<
+  std::pair, std::pair>>
+  TestCases{{{},
+ {
+ R"cpp(
+int $Variable[[A]]
+double $Variable[[B]];
+struct $Class[[C]] {};
+  )cpp",
+ R"cpp(
+int A;
+double B;
+struct C {};
+  )cpp"}},
+{{5},
+ {
+ R"cpp(
+  struct $Class[[Alpha]] {
+double SomeVariable = 9483.301;
+  };
+  struct $Class[[Beta]] {};
+  int $Variable[[A]] = 121;
+  $Class[[Alpha]] $Variable[[Var]];
+  )cpp",
+ R"cpp(
+  struct Alpha {
+double SomeVariable = 9483.301;
+  };
+  struct Beta   {}; // Some comment
+  intA = 121;
+  $Class[[Beta]] 

[PATCH] D64475: [clangd] Duplicate lines of semantic highlightings sent removed.

2019-07-11 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom marked an inline comment as done.
jvikstrom added inline comments.



Comment at: clang-tools-extra/clangd/SemanticHighlighting.cpp:172
+  const HighlightingToken ) {
+  return Lhs.R.start.line == Rhs.R.start.line
+ ? Lhs.R.start.character < Rhs.R.start.character

sammccall wrote:
> I think this is just Lhs.R.start < Lhs.R.end
> 
> (if it's not, we should add the `operator<`)
> 
> is it enforced somewhere that you don't have two highlights at the same spot?
It's not enforced anywhere, it depends on how the  HighlightingTokenCollector 
is implemented, it should not currently take any multiples of tokens at the 
same spot.

Even if there are tokens at the same location it should still satisfy Compare 
though?



Comment at: clang-tools-extra/clangd/SemanticHighlighting.cpp:190
+  // highlightings for old ASTs)
+  std::lock_guard Lock(PrevMutex);
+  // The files are different therefore all highlights differ.

sammccall wrote:
> holding the lock while doing all the diffing seems dubious
> 
> You could reasonably put the cache as a map in 
> ClangdServer, then you don't have to deal with not having the cache for the 
> current file.
> 
> You'd need to lock the map itself, but note that no races on updating 
> individual entries are possible because onMainAST is called synchronously 
> from the (per-file) worker thread.
I did not know onMainAST was called synchronously per file. 
I feel like after a while a lot of memory is going to be consumed by keeping 
ASTs for files that were closed long ago in the cache (say when you've opened 
1000 files in a session, all those files will have one Highlightings entry here)
Could solve that with LRU (just keep a set of pair 
(unix time of adding/editing to cache and filename)... would actually have to 
be a struct, would need to make the equal operator check the filename only)

But maybe that isn't a big concern/a concern at all?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64475



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D64475: [clangd] Duplicate lines of semantic highlightings sent removed.

2019-07-11 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 209225.
jvikstrom marked 5 inline comments as done.
jvikstrom added a comment.

Made diffing function shorter, added multiple previous highlighting entries.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64475

Files:
  clang-tools-extra/clangd/ClangdLSPServer.cpp
  clang-tools-extra/clangd/ClangdLSPServer.h
  clang-tools-extra/clangd/ClangdServer.cpp
  clang-tools-extra/clangd/ClangdServer.h
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/SemanticHighlighting.h
  clang-tools-extra/clangd/test/semantic-highlighting.test
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -29,7 +29,9 @@
   return Tokens;
 }
 
-void checkHighlightings(llvm::StringRef Code) {
+std::tuple,
+   std::vector>
+getHighlightingsAnnotated(llvm::StringRef Code) {
   Annotations Test(Code);
   auto AST = TestTU::withCode(Test.code()).build();
   static const std::map KindToString{
@@ -46,9 +48,43 @@
   }
 
   auto ActualTokens = getSemanticHighlightings(AST);
+  return {std::move(AST), ActualTokens, ExpectedTokens};
+}
+
+void checkHighlightings(llvm::StringRef Code) {
+  std::vector ActualTokens;
+  std::vector ExpectedTokens;
+  std::tie(std::ignore, ActualTokens, ExpectedTokens) =
+  getHighlightingsAnnotated(Code);
   EXPECT_THAT(ActualTokens, testing::UnorderedElementsAreArray(ExpectedTokens));
 }
 
+void checkDiffedHighlights(
+const std::vector ,
+const std::vector ,
+std::vector>> ) {
+  std::map> ExpectedLines;
+  for (const HighlightingToken  : ExpectedTokens)
+ExpectedLines[Token.R.start.line].push_back(Token);
+  std::vector>>
+  ExpectedLinePairHighlighting;
+  for (int Line : EmptyLines)
+ExpectedLinePairHighlighting.push_back({Line, {}});
+  for (auto LineTokens : ExpectedLines) {
+std::sort(LineTokens.second.begin(), LineTokens.second.end());
+ExpectedLinePairHighlighting.push_back(
+{LineTokens.first, LineTokens.second});
+  }
+
+  // The UnorderedElementsAreArray only checks that the top level vector
+  // is unordered. The vectors in the pair must be in the correct order.
+  for (unsigned I = 0, End = ActualDiffed.size(); I < End; ++I)
+std::sort(ActualDiffed[I].second.begin(), ActualDiffed[I].second.end());
+
+  EXPECT_THAT(ActualDiffed,
+  testing::UnorderedElementsAreArray(ExpectedLinePairHighlighting));
+}
+
 TEST(SemanticHighlighting, GetsCorrectTokens) {
   const char *TestCases[] = {
 R"cpp(
@@ -145,7 +181,9 @@
 
 void onDiagnosticsReady(PathRef, std::vector) override {}
 void onHighlightingsReady(
-PathRef File, std::vector Highlightings) override {
+PathRef File,
+std::vector>>
+Highlightings) override {
   ++Count;
 }
   };
@@ -170,21 +208,148 @@
 return Pos;
   };
 
-  std::vector Tokens{
-  {HighlightingKind::Variable,
-Range{CreatePosition(3, 8), CreatePosition(3, 12)}},
-  {HighlightingKind::Function,
-Range{CreatePosition(3, 4), CreatePosition(3, 7)}},
-  {HighlightingKind::Variable,
-Range{CreatePosition(1, 1), CreatePosition(1, 5)}}};
+  std::vector>> Tokens{
+  {3,
+   {{HighlightingKind::Variable,
+ Range{CreatePosition(3, 8), CreatePosition(3, 12)}},
+{HighlightingKind::Function,
+ Range{CreatePosition(3, 4), CreatePosition(3, 7),
+  {1,
+   {{HighlightingKind::Variable,
+ Range{CreatePosition(1, 1), CreatePosition(1, 5)};
   std::vector ActualResults =
   toSemanticHighlightingInformation(Tokens);
   std::vector ExpectedResults = {
-  {1, "AQAEAAA="},
-  {3, "CAAEAAAEAAMAAQ=="}};
+  {3, "CAAEAAAEAAMAAQ=="}, {1, "AQAEAAA="}};
   EXPECT_EQ(ActualResults, ExpectedResults);
 }
 
+TEST(SemanticHighlighting, HighlightingDiffer) {
+  std::vector<
+  std::pair, std::pair>>
+  TestCases{{{},
+ {
+ R"cpp(
+int $Variable[[A]]
+double $Variable[[B]];
+struct $Class[[C]] {};
+  )cpp",
+ R"cpp(
+int A;
+double B;
+struct C {};
+  )cpp"}},
+{{5},
+ {
+ R"cpp(
+  struct $Class[[Alpha]] {
+double SomeVariable = 9483.301;
+  };
+  struct $Class[[Beta]] {};
+  int $Variable[[A]] = 121;
+  $Class[[Alpha]] $Variable[[Var]];
+  )cpp",
+ R"cpp(
+  struct Alpha {
+double SomeVariable = 9483.301;
+  };
+  

[PATCH] D64624: [clangd] Added highlighting to enum constants.

2019-07-12 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom created this revision.
jvikstrom added reviewers: hokein, sammccall, ilya-biryukov.
Herald added subscribers: cfe-commits, kadircet, arphaman, jkorous, MaskRay.
Herald added a project: clang.

VSCode does not have a scope for enum constants. So they were placed under 
"constant.other.enum" as that seems to be the most correct scope for enum 
constants. However, this makes theia color them blue (the same color it uses 
for keywords).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D64624

Files:
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/SemanticHighlighting.h
  clang-tools-extra/clangd/test/semantic-highlighting.test
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp


Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -37,7 +37,8 @@
   {HighlightingKind::Function, "Function"},
   {HighlightingKind::Class, "Class"},
   {HighlightingKind::Enum, "Enum"},
-  {HighlightingKind::Namespace, "Namespace"}};
+  {HighlightingKind::Namespace, "Namespace"},
+  {HighlightingKind::EnumConstant, "EnumConstant"}};
   std::vector ExpectedTokens;
   for (const auto  : KindToString) {
 std::vector Toks = makeHighlightingTokens(
@@ -118,7 +119,7 @@
   namespace $Namespace[[cde]] {
 struct $Class[[A]] {
   enum class $Enum[[B]] {
-Hi,
+$EnumConstant[[Hi]],
   };
 };
   }
@@ -129,9 +130,20 @@
 $Namespace[[abc]]::$Namespace[[bcd]]::$Namespace[[cde]];
   $Namespace[[abc]]::$Namespace[[bcd]]::$Class[[A]] $Variable[[AA]];
   $Namespace[[vwz]]::$Class[[A]]::$Enum[[B]] $Variable[[AAA]] =
-$Namespace[[vwz]]::$Class[[A]]::$Enum[[B]]::Hi;
+$Namespace[[vwz]]::$Class[[A]]::$Enum[[B]]::$EnumConstant[[Hi]];
   ::$Namespace[[vwz]]::$Class[[A]] $Variable[[B]];
   ::$Namespace[[abc]]::$Namespace[[bcd]]::$Class[[A]] $Variable[[BB]];
+)cpp",
+R"cpp(
+  enum $Enum[[ABC]] {
+$EnumConstant[[Hi]],
+  };
+  int $Variable[[I]] = $EnumConstant[[Hi]];
+  enum class $Enum[[BC]] {
+$EnumConstant[[A]],
+$EnumConstant[[B]],
+  };
+  $Enum[[BC]] $Variable[[L]] = $Enum[[BC]]::$EnumConstant[[B]];
 )cpp"};
   for (const auto  : TestCases) {
 checkHighlightings(TestCase);
Index: clang-tools-extra/clangd/test/semantic-highlighting.test
===
--- clang-tools-extra/clangd/test/semantic-highlighting.test
+++ clang-tools-extra/clangd/test/semantic-highlighting.test
@@ -17,6 +17,9 @@
 # CHECK-NEXT:"entity.name.type.enum.cpp"
 # CHECK-NEXT:  ],
 # CHECK-NEXT:  [
+# CHECK-NEXT:"constant.other.enum.cpp"
+# CHECK-NEXT:  ],
+# CHECK-NEXT:  [
 # CHECK-NEXT:"entity.name.namespace.cpp"
 # CHECK-NEXT:  ]
 # CHECK-NEXT:]
Index: clang-tools-extra/clangd/SemanticHighlighting.h
===
--- clang-tools-extra/clangd/SemanticHighlighting.h
+++ clang-tools-extra/clangd/SemanticHighlighting.h
@@ -28,6 +28,7 @@
   Function,
   Class,
   Enum,
+  EnumConstant,
   Namespace,
 
   NumKinds,
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -119,6 +119,10 @@
   addToken(Loc, HighlightingKind::Enum);
   return;
 }
+if(isa(D)) {
+  addToken(Loc, HighlightingKind::EnumConstant);
+  return;
+}
 if (isa(D)) {
   addToken(Loc, HighlightingKind::Variable);
   return;
@@ -249,6 +253,8 @@
 return "entity.name.type.class.cpp";
   case HighlightingKind::Enum:
 return "entity.name.type.enum.cpp";
+  case HighlightingKind::EnumConstant:
+return "constant.other.enum.cpp";
   case HighlightingKind::Namespace:
 return "entity.name.namespace.cpp";
   case HighlightingKind::NumKinds:


Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -37,7 +37,8 @@
   {HighlightingKind::Function, "Function"},
   {HighlightingKind::Class, "Class"},
   {HighlightingKind::Enum, "Enum"},
-  {HighlightingKind::Namespace, "Namespace"}};
+  {HighlightingKind::Namespace, "Namespace"},
+  {HighlightingKind::EnumConstant, "EnumConstant"}};
   std::vector ExpectedTokens;
   for (const auto  : 

[PATCH] D64617: [clangd] Added highlighting for members and methods

2019-07-12 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom created this revision.
jvikstrom added reviewers: hokein, sammccall, ilya-biryukov.
Herald added subscribers: cfe-commits, kadircet, arphaman, jkorous, MaskRay.
Herald added a project: clang.

Added highlighting for members and methods.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D64617

Files:
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/SemanticHighlighting.h
  clang-tools-extra/clangd/test/semantic-highlighting.test
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -37,7 +37,9 @@
   {HighlightingKind::Function, "Function"},
   {HighlightingKind::Class, "Class"},
   {HighlightingKind::Enum, "Enum"},
-  {HighlightingKind::Namespace, "Namespace"}};
+  {HighlightingKind::Namespace, "Namespace"},
+  {HighlightingKind::MemberVariable, "MemberVariable"},
+  {HighlightingKind::Method, "Method"}};
   std::vector ExpectedTokens;
   for (const auto  : KindToString) {
 std::vector Toks = makeHighlightingTokens(
@@ -53,14 +55,14 @@
   const char *TestCases[] = {
 R"cpp(
   struct $Class[[AS]] {
-double SomeMember;
+double $MemberVariable[[SomeMember]];
   };
   struct {
   } $Variable[[S]];
   void $Function[[foo]](int $Variable[[A]], $Class[[AS]] $Variable[[As]]) {
 auto $Variable[[VeryLongVariableName]] = 12312;
 $Class[[AS]] $Variable[[AA]];
-auto $Variable[[L]] = $Variable[[AA]].SomeMember + $Variable[[A]];
+auto $Variable[[L]] = $Variable[[AA]].$MemberVariable[[SomeMember]] + $Variable[[A]];
 auto $Variable[[FN]] = [ $Variable[[AA]]](int $Variable[[A]]) -> void {};
 $Variable[[FN]](12312);
   }
@@ -72,19 +74,19 @@
 auto $Variable[[Bou]] = $Function[[Gah]];
   }
   struct $Class[[A]] {
-void $Function[[abc]]();
+void $Method[[abc]]();
   };
 )cpp",
 R"cpp(
   namespace $Namespace[[abc]] {
 template
 struct $Class[[A]] {
-  T t;
+  T $MemberVariable[[t]];
 };
   }
   template
   struct $Class[[C]] : $Namespace[[abc]]::A {
-typename T::A* D;
+typename T::A* $MemberVariable[[D]];
   };
   $Namespace[[abc]]::$Class[[A]] $Variable[[AA]];
   typedef $Namespace[[abc]]::$Class[[A]] AAA;
@@ -92,7 +94,7 @@
 $Class[[B]]();
 ~$Class[[B]]();
 void operator<<($Class[[B]]);
-$Class[[AAA]] AA;
+$Class[[AAA]] $MemberVariable[[AA]];
   };
   $Class[[B]]::$Class[[B]]() {}
   $Class[[B]]::~$Class[[B]]() {}
@@ -106,8 +108,8 @@
   enum class $Enum[[E]] {};
   enum $Enum[[EE]] {};
   struct $Class[[A]] {
-$Enum[[E]] EEE;
-$Enum[[EE]] ;
+$Enum[[E]] $MemberVariable[[EEE]];
+$Enum[[EE]] $MemberVariable[[]];
   };
 )cpp",
 R"cpp(
@@ -132,6 +134,27 @@
 $Namespace[[vwz]]::$Class[[A]]::$Enum[[B]]::Hi;
   ::$Namespace[[vwz]]::$Class[[A]] $Variable[[B]];
   ::$Namespace[[abc]]::$Namespace[[bcd]]::$Class[[A]] $Variable[[BB]];
+)cpp",
+R"cpp(
+  struct $Class[[D]] {
+double $MemberVariable[[C]];
+  };
+  struct $Class[[A]] {
+double $MemberVariable[[B]];
+$Class[[D]] $MemberVariable[[E]];
+void $Method[[foo]]() {
+  $MemberVariable[[B]] = 123;
+  this->$MemberVariable[[B]] = 156;
+  this->$Method[[foo]]();
+  $Method[[foo]]();
+}
+  };
+  void $Function[[foo]]() {
+$Class[[A]] $Variable[[AA]];
+$Variable[[AA]].$MemberVariable[[B]] += 2;
+$Variable[[AA]].$Method[[foo]]();
+$Variable[[AA]].$MemberVariable[[E]].$MemberVariable[[C]];
+  }
 )cpp"};
   for (const auto  : TestCases) {
 checkHighlightings(TestCase);
@@ -181,7 +204,7 @@
   toSemanticHighlightingInformation(Tokens);
   std::vector ExpectedResults = {
   {1, "AQAEAAA="},
-  {3, "CAAEAAAEAAMAAQ=="}};
+  {3, "CAAEAAAEAAMAAg=="}};
   EXPECT_EQ(ActualResults, ExpectedResults);
 }
 
Index: clang-tools-extra/clangd/test/semantic-highlighting.test
===
--- clang-tools-extra/clangd/test/semantic-highlighting.test
+++ clang-tools-extra/clangd/test/semantic-highlighting.test
@@ -5,12 +5,18 @@
 # CHECK:  "semanticHighlighting": {
 # CHECK-NEXT:"scopes": [
 # CHECK-NEXT:  [
-# CHECK-NEXT:"variable.cpp"
+# CHECK-NEXT:"variable.other.cpp"
+# CHECK-NEXT:  ],
+# CHECK-NEXT:  [
+# CHECK-NEXT:"variable.other.member.cpp"
 # CHECK-NEXT:  ],
 # 

[PATCH] D64617: [clangd] Added highlighting for members and methods

2019-07-12 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 209509.
jvikstrom marked 6 inline comments as done.
jvikstrom added a comment.

Addressed comments.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64617

Files:
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/SemanticHighlighting.h
  clang-tools-extra/clangd/test/semantic-highlighting.test
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -37,7 +37,9 @@
   {HighlightingKind::Function, "Function"},
   {HighlightingKind::Class, "Class"},
   {HighlightingKind::Enum, "Enum"},
-  {HighlightingKind::Namespace, "Namespace"}};
+  {HighlightingKind::Namespace, "Namespace"},
+  {HighlightingKind::Field, "Field"},
+  {HighlightingKind::Method, "Method"}};
   std::vector ExpectedTokens;
   for (const auto  : KindToString) {
 std::vector Toks = makeHighlightingTokens(
@@ -53,14 +55,14 @@
   const char *TestCases[] = {
 R"cpp(
   struct $Class[[AS]] {
-double SomeMember;
+double $Field[[SomeMember]];
   };
   struct {
   } $Variable[[S]];
   void $Function[[foo]](int $Variable[[A]], $Class[[AS]] $Variable[[As]]) {
 auto $Variable[[VeryLongVariableName]] = 12312;
 $Class[[AS]] $Variable[[AA]];
-auto $Variable[[L]] = $Variable[[AA]].SomeMember + $Variable[[A]];
+auto $Variable[[L]] = $Variable[[AA]].$Field[[SomeMember]] + $Variable[[A]];
 auto $Variable[[FN]] = [ $Variable[[AA]]](int $Variable[[A]]) -> void {};
 $Variable[[FN]](12312);
   }
@@ -72,19 +74,19 @@
 auto $Variable[[Bou]] = $Function[[Gah]];
   }
   struct $Class[[A]] {
-void $Function[[abc]]();
+void $Method[[abc]]();
   };
 )cpp",
 R"cpp(
   namespace $Namespace[[abc]] {
 template
 struct $Class[[A]] {
-  T t;
+  T $Field[[t]];
 };
   }
   template
   struct $Class[[C]] : $Namespace[[abc]]::A {
-typename T::A* D;
+typename T::A* $Field[[D]];
   };
   $Namespace[[abc]]::$Class[[A]] $Variable[[AA]];
   typedef $Namespace[[abc]]::$Class[[A]] AAA;
@@ -92,7 +94,7 @@
 $Class[[B]]();
 ~$Class[[B]]();
 void operator<<($Class[[B]]);
-$Class[[AAA]] AA;
+$Class[[AAA]] $Field[[AA]];
   };
   $Class[[B]]::$Class[[B]]() {}
   $Class[[B]]::~$Class[[B]]() {}
@@ -106,8 +108,8 @@
   enum class $Enum[[E]] {};
   enum $Enum[[EE]] {};
   struct $Class[[A]] {
-$Enum[[E]] EEE;
-$Enum[[EE]] ;
+$Enum[[E]] $Field[[EEE]];
+$Enum[[EE]] $Field[[]];
   };
 )cpp",
 R"cpp(
@@ -132,6 +134,30 @@
 $Namespace[[vwz]]::$Class[[A]]::$Enum[[B]]::Hi;
   ::$Namespace[[vwz]]::$Class[[A]] $Variable[[B]];
   ::$Namespace[[abc]]::$Namespace[[bcd]]::$Class[[A]] $Variable[[BB]];
+)cpp",
+R"cpp(
+  struct $Class[[D]] {
+double $Field[[C]];
+  };
+  struct $Class[[A]] {
+double $Field[[B]];
+$Class[[D]] $Field[[E]];
+static double $Variable[[S]];
+void $Method[[foo]]() {
+  $Field[[B]] = 123;
+  this->$Field[[B]] = 156;
+  this->$Method[[foo]]();
+  $Method[[foo]]();
+  $Variable[[S]] = 90.1;
+}
+  };
+  void $Function[[foo]]() {
+$Class[[A]] $Variable[[AA]];
+$Variable[[AA]].$Field[[B]] += 2;
+$Variable[[AA]].$Method[[foo]]();
+$Variable[[AA]].$Field[[E]].$Field[[C]];
+$Class[[A]]::$Variable[[S]] = 90;
+  }
 )cpp"};
   for (const auto  : TestCases) {
 checkHighlightings(TestCase);
@@ -181,7 +207,7 @@
   toSemanticHighlightingInformation(Tokens);
   std::vector ExpectedResults = {
   {1, "AQAEAAA="},
-  {3, "CAAEAAAEAAMAAQ=="}};
+  {3, "CAAEAAAEAAMAAg=="}};
   EXPECT_EQ(ActualResults, ExpectedResults);
 }
 
Index: clang-tools-extra/clangd/test/semantic-highlighting.test
===
--- clang-tools-extra/clangd/test/semantic-highlighting.test
+++ clang-tools-extra/clangd/test/semantic-highlighting.test
@@ -5,12 +5,18 @@
 # CHECK:  "semanticHighlighting": {
 # CHECK-NEXT:"scopes": [
 # CHECK-NEXT:  [
-# CHECK-NEXT:"variable.cpp"
+# CHECK-NEXT:"variable.other.cpp"
+# CHECK-NEXT:  ],
+# CHECK-NEXT:  [
+# CHECK-NEXT:"variable.other.field.cpp"
 # CHECK-NEXT:  ],
 # CHECK-NEXT:  [
 # CHECK-NEXT:"entity.name.function.cpp"
 # 

[PATCH] D64634: [clangd] Fix duplicate highlighting tokens appearing in initializer lists

2019-07-12 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom created this revision.
jvikstrom added reviewers: hokein, sammccall, ilya-biryukov.
Herald added subscribers: cfe-commits, kadircet, arphaman, mgrang, jkorous, 
MaskRay.
Herald added a project: clang.

The RecursiveASTVisitor sometimes visits exprs in initializer lists twice. 
Added deduplication to prevent duplicate highlighting tokens from appearing. 
Done by sorting and a linear search.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D64634

Files:
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/SemanticHighlighting.h
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp


Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -132,6 +132,13 @@
 $Namespace[[vwz]]::$Class[[A]]::$Enum[[B]]::Hi;
   ::$Namespace[[vwz]]::$Class[[A]] $Variable[[B]];
   ::$Namespace[[abc]]::$Namespace[[bcd]]::$Class[[A]] $Variable[[BB]];
+)cpp",
+R"cpp(
+  struct $Class[[AA]] {
+int A;
+  }
+  int $Variable[[B]];
+  $Class[[AA]] $Variable[[A]]{$Variable[[B]]};
 )cpp"};
   for (const auto  : TestCases) {
 checkHighlightings(TestCase);
Index: clang-tools-extra/clangd/SemanticHighlighting.h
===
--- clang-tools-extra/clangd/SemanticHighlighting.h
+++ clang-tools-extra/clangd/SemanticHighlighting.h
@@ -40,6 +40,7 @@
 };
 
 bool operator==(const HighlightingToken , const HighlightingToken );
+bool operator<(const HighlightingToken , const HighlightingToken );
 
 // Returns all HighlightingTokens from an AST. Only generates highlights for 
the
 // main AST.
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -31,6 +31,16 @@
   std::vector collectTokens() {
 Tokens.clear();
 TraverseAST(Ctx);
+// Initializer lists can give duplicates of tokens, therefore all tokens
+// must be deduplicated.
+std::sort(Tokens.begin(), Tokens.end());
+for (unsigned I = 1; I < Tokens.size(); ++I) {
+  if (Tokens[I] == Tokens[I - 1]) {
+Tokens.erase(Tokens.begin() + I);
+--I;
+  }
+}
+
 return Tokens;
   }
 
@@ -70,7 +80,6 @@
 DeclarationName::Identifier)
   // Only want to highlight identifiers.
   return true;
-
 addToken(Ref->getLocation(), Ref->getDecl());
 return true;
   }
@@ -201,6 +210,10 @@
   return Lhs.Kind == Rhs.Kind && Lhs.R == Rhs.R;
 }
 
+bool operator<(const HighlightingToken , const HighlightingToken ) {
+  return Lhs.R.start < Rhs.R.start;
+}
+
 std::vector getSemanticHighlightings(ParsedAST ) {
   return HighlightingTokenCollector(AST).collectTokens();
 }


Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -132,6 +132,13 @@
 $Namespace[[vwz]]::$Class[[A]]::$Enum[[B]]::Hi;
   ::$Namespace[[vwz]]::$Class[[A]] $Variable[[B]];
   ::$Namespace[[abc]]::$Namespace[[bcd]]::$Class[[A]] $Variable[[BB]];
+)cpp",
+R"cpp(
+  struct $Class[[AA]] {
+int A;
+  }
+  int $Variable[[B]];
+  $Class[[AA]] $Variable[[A]]{$Variable[[B]]};
 )cpp"};
   for (const auto  : TestCases) {
 checkHighlightings(TestCase);
Index: clang-tools-extra/clangd/SemanticHighlighting.h
===
--- clang-tools-extra/clangd/SemanticHighlighting.h
+++ clang-tools-extra/clangd/SemanticHighlighting.h
@@ -40,6 +40,7 @@
 };
 
 bool operator==(const HighlightingToken , const HighlightingToken );
+bool operator<(const HighlightingToken , const HighlightingToken );
 
 // Returns all HighlightingTokens from an AST. Only generates highlights for the
 // main AST.
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -31,6 +31,16 @@
   std::vector collectTokens() {
 Tokens.clear();
 TraverseAST(Ctx);
+// Initializer lists can give duplicates of tokens, therefore all tokens
+// must be deduplicated.
+std::sort(Tokens.begin(), Tokens.end());
+for (unsigned I = 1; I < Tokens.size(); ++I) {
+  if (Tokens[I] == Tokens[I - 1]) {
+Tokens.erase(Tokens.begin() + I);
+--I;
+  }
+}
+
 return Tokens;
   }
 
@@ -70,7 +80,6 @@
 

[PATCH] D64624: [clangd] Added highlighting to enum constants.

2019-07-12 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 209514.
jvikstrom marked 2 inline comments as done.
jvikstrom added a comment.

Address comments.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64624

Files:
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/SemanticHighlighting.h
  clang-tools-extra/clangd/test/semantic-highlighting.test
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -37,7 +37,8 @@
   {HighlightingKind::Function, "Function"},
   {HighlightingKind::Class, "Class"},
   {HighlightingKind::Enum, "Enum"},
-  {HighlightingKind::Namespace, "Namespace"}};
+  {HighlightingKind::Namespace, "Namespace"},
+  {HighlightingKind::EnumConstant, "EnumConstant"}};
   std::vector ExpectedTokens;
   for (const auto  : KindToString) {
 std::vector Toks = makeHighlightingTokens(
@@ -103,12 +104,19 @@
   }
 )cpp",
 R"cpp(
-  enum class $Enum[[E]] {};
-  enum $Enum[[EE]] {};
+  enum class $Enum[[E]] {
+$EnumConstant[[A]],
+$EnumConstant[[B]],
+  };
+  enum $Enum[[EE]] {
+$EnumConstant[[Hi]],
+  };
   struct $Class[[A]] {
 $Enum[[E]] EEE;
 $Enum[[EE]] ;
   };
+  int $Variable[[I]] = $EnumConstant[[Hi]];
+  $Enum[[E]] $Variable[[L]] = $Enum[[E]]::$EnumConstant[[B]];
 )cpp",
 R"cpp(
   namespace $Namespace[[abc]] {
@@ -118,7 +126,7 @@
   namespace $Namespace[[cde]] {
 struct $Class[[A]] {
   enum class $Enum[[B]] {
-Hi,
+$EnumConstant[[Hi]],
   };
 };
   }
@@ -129,7 +137,7 @@
 $Namespace[[abc]]::$Namespace[[bcd]]::$Namespace[[cde]];
   $Namespace[[abc]]::$Namespace[[bcd]]::$Class[[A]] $Variable[[AA]];
   $Namespace[[vwz]]::$Class[[A]]::$Enum[[B]] $Variable[[AAA]] =
-$Namespace[[vwz]]::$Class[[A]]::$Enum[[B]]::Hi;
+$Namespace[[vwz]]::$Class[[A]]::$Enum[[B]]::$EnumConstant[[Hi]];
   ::$Namespace[[vwz]]::$Class[[A]] $Variable[[B]];
   ::$Namespace[[abc]]::$Namespace[[bcd]]::$Class[[A]] $Variable[[BB]];
 )cpp"};
Index: clang-tools-extra/clangd/test/semantic-highlighting.test
===
--- clang-tools-extra/clangd/test/semantic-highlighting.test
+++ clang-tools-extra/clangd/test/semantic-highlighting.test
@@ -17,6 +17,9 @@
 # CHECK-NEXT:"entity.name.type.enum.cpp"
 # CHECK-NEXT:  ],
 # CHECK-NEXT:  [
+# CHECK-NEXT:"constant.other.enum.cpp"
+# CHECK-NEXT:  ],
+# CHECK-NEXT:  [
 # CHECK-NEXT:"entity.name.namespace.cpp"
 # CHECK-NEXT:  ]
 # CHECK-NEXT:]
Index: clang-tools-extra/clangd/SemanticHighlighting.h
===
--- clang-tools-extra/clangd/SemanticHighlighting.h
+++ clang-tools-extra/clangd/SemanticHighlighting.h
@@ -28,6 +28,7 @@
   Function,
   Class,
   Enum,
+  EnumConstant,
   Namespace,
 
   NumKinds,
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -119,6 +119,10 @@
   addToken(Loc, HighlightingKind::Enum);
   return;
 }
+if (isa(D)) {
+  addToken(Loc, HighlightingKind::EnumConstant);
+  return;
+}
 if (isa(D)) {
   addToken(Loc, HighlightingKind::Variable);
   return;
@@ -249,6 +253,8 @@
 return "entity.name.type.class.cpp";
   case HighlightingKind::Enum:
 return "entity.name.type.enum.cpp";
+  case HighlightingKind::EnumConstant:
+return "constant.other.enum.cpp";
   case HighlightingKind::Namespace:
 return "entity.name.namespace.cpp";
   case HighlightingKind::NumKinds:
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D64617: [clangd] Added highlighting for members and methods

2019-07-12 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 209510.
jvikstrom added a comment.

Removed addToken for Exprs.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64617

Files:
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/SemanticHighlighting.h
  clang-tools-extra/clangd/test/semantic-highlighting.test
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -37,7 +37,9 @@
   {HighlightingKind::Function, "Function"},
   {HighlightingKind::Class, "Class"},
   {HighlightingKind::Enum, "Enum"},
-  {HighlightingKind::Namespace, "Namespace"}};
+  {HighlightingKind::Namespace, "Namespace"},
+  {HighlightingKind::Field, "Field"},
+  {HighlightingKind::Method, "Method"}};
   std::vector ExpectedTokens;
   for (const auto  : KindToString) {
 std::vector Toks = makeHighlightingTokens(
@@ -53,14 +55,14 @@
   const char *TestCases[] = {
 R"cpp(
   struct $Class[[AS]] {
-double SomeMember;
+double $Field[[SomeMember]];
   };
   struct {
   } $Variable[[S]];
   void $Function[[foo]](int $Variable[[A]], $Class[[AS]] $Variable[[As]]) {
 auto $Variable[[VeryLongVariableName]] = 12312;
 $Class[[AS]] $Variable[[AA]];
-auto $Variable[[L]] = $Variable[[AA]].SomeMember + $Variable[[A]];
+auto $Variable[[L]] = $Variable[[AA]].$Field[[SomeMember]] + $Variable[[A]];
 auto $Variable[[FN]] = [ $Variable[[AA]]](int $Variable[[A]]) -> void {};
 $Variable[[FN]](12312);
   }
@@ -72,19 +74,19 @@
 auto $Variable[[Bou]] = $Function[[Gah]];
   }
   struct $Class[[A]] {
-void $Function[[abc]]();
+void $Method[[abc]]();
   };
 )cpp",
 R"cpp(
   namespace $Namespace[[abc]] {
 template
 struct $Class[[A]] {
-  T t;
+  T $Field[[t]];
 };
   }
   template
   struct $Class[[C]] : $Namespace[[abc]]::A {
-typename T::A* D;
+typename T::A* $Field[[D]];
   };
   $Namespace[[abc]]::$Class[[A]] $Variable[[AA]];
   typedef $Namespace[[abc]]::$Class[[A]] AAA;
@@ -92,7 +94,7 @@
 $Class[[B]]();
 ~$Class[[B]]();
 void operator<<($Class[[B]]);
-$Class[[AAA]] AA;
+$Class[[AAA]] $Field[[AA]];
   };
   $Class[[B]]::$Class[[B]]() {}
   $Class[[B]]::~$Class[[B]]() {}
@@ -106,8 +108,8 @@
   enum class $Enum[[E]] {};
   enum $Enum[[EE]] {};
   struct $Class[[A]] {
-$Enum[[E]] EEE;
-$Enum[[EE]] ;
+$Enum[[E]] $Field[[EEE]];
+$Enum[[EE]] $Field[[]];
   };
 )cpp",
 R"cpp(
@@ -132,6 +134,30 @@
 $Namespace[[vwz]]::$Class[[A]]::$Enum[[B]]::Hi;
   ::$Namespace[[vwz]]::$Class[[A]] $Variable[[B]];
   ::$Namespace[[abc]]::$Namespace[[bcd]]::$Class[[A]] $Variable[[BB]];
+)cpp",
+R"cpp(
+  struct $Class[[D]] {
+double $Field[[C]];
+  };
+  struct $Class[[A]] {
+double $Field[[B]];
+$Class[[D]] $Field[[E]];
+static double $Variable[[S]];
+void $Method[[foo]]() {
+  $Field[[B]] = 123;
+  this->$Field[[B]] = 156;
+  this->$Method[[foo]]();
+  $Method[[foo]]();
+  $Variable[[S]] = 90.1;
+}
+  };
+  void $Function[[foo]]() {
+$Class[[A]] $Variable[[AA]];
+$Variable[[AA]].$Field[[B]] += 2;
+$Variable[[AA]].$Method[[foo]]();
+$Variable[[AA]].$Field[[E]].$Field[[C]];
+$Class[[A]]::$Variable[[S]] = 90;
+  }
 )cpp"};
   for (const auto  : TestCases) {
 checkHighlightings(TestCase);
@@ -181,7 +207,7 @@
   toSemanticHighlightingInformation(Tokens);
   std::vector ExpectedResults = {
   {1, "AQAEAAA="},
-  {3, "CAAEAAAEAAMAAQ=="}};
+  {3, "CAAEAAAEAAMAAg=="}};
   EXPECT_EQ(ActualResults, ExpectedResults);
 }
 
Index: clang-tools-extra/clangd/test/semantic-highlighting.test
===
--- clang-tools-extra/clangd/test/semantic-highlighting.test
+++ clang-tools-extra/clangd/test/semantic-highlighting.test
@@ -5,12 +5,18 @@
 # CHECK:  "semanticHighlighting": {
 # CHECK-NEXT:"scopes": [
 # CHECK-NEXT:  [
-# CHECK-NEXT:"variable.cpp"
+# CHECK-NEXT:"variable.other.cpp"
+# CHECK-NEXT:  ],
+# CHECK-NEXT:  [
+# CHECK-NEXT:"variable.other.field.cpp"
 # CHECK-NEXT:  ],
 # CHECK-NEXT:  [
 # CHECK-NEXT:"entity.name.function.cpp"
 # CHECK-NEXT:  ],
 # CHECK-NEXT:

[PATCH] D64257: [clangd] Added highlighting for non-builtin types

2019-07-09 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 208621.
jvikstrom marked 5 inline comments as done.
jvikstrom added a comment.

Addressed comments.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64257

Files:
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/SemanticHighlighting.h
  clang-tools-extra/clangd/test/semantic-highlighting.test
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -34,11 +34,13 @@
   auto AST = TestTU::withCode(Test.code()).build();
   static const std::map KindToString{
   {HighlightingKind::Variable, "Variable"},
-  {HighlightingKind::Function, "Function"}};
+  {HighlightingKind::Function, "Function"},
+  {HighlightingKind::Class, "Class"},
+  {HighlightingKind::Enum, "Enum"}};
   std::vector ExpectedTokens;
   for (const auto  : KindToString) {
-std::vector Toks =
-makeHighlightingTokens(Test.ranges(KindString.second), KindString.first);
+std::vector Toks = makeHighlightingTokens(
+Test.ranges(KindString.second), KindString.first);
 ExpectedTokens.insert(ExpectedTokens.end(), Toks.begin(), Toks.end());
   }
 
@@ -49,14 +51,14 @@
 TEST(SemanticHighlighting, GetsCorrectTokens) {
   const char *TestCases[] = {
 R"cpp(
-  struct AS {
+  struct $Class[[AS]] {
 double SomeMember;
   };
-  struct {
+  $Class[[struct]] {
   } $Variable[[S]];
-  void $Function[[foo]](int $Variable[[A]]) {
+  void $Function[[foo]](int $Variable[[A]], $Class[[AS]] $Variable[[As]]) {
 auto $Variable[[VeryLongVariableName]] = 12312;
-AS $Variable[[AA]];
+$Class[[AS]] $Variable[[AA]];
 auto $Variable[[L]] = $Variable[[AA]].SomeMember + $Variable[[A]];
 auto $Variable[[FN]] = [ $Variable[[AA]]](int $Variable[[A]]) -> void {};
 $Variable[[FN]](12312);
@@ -68,13 +70,43 @@
   void $Function[[foo]]() {
 auto $Variable[[Bou]] = $Function[[Gah]];
   }
+  struct $Class[[A]] {
+void $Function[[abc]]();
+  };
 )cpp",
 R"cpp(
-  struct A {
-A();
-~A();
-void $Function[[abc]]();
-void operator<<(int);
+  namespace abc {
+template
+struct $Class[[A]] {
+  T t;
+};
+  }
+  template
+  struct $Class[[C]] : abc::A {
+typename T::A* D;
+  };
+  abc::$Class[[A]] $Variable[[AA]];
+  typedef abc::$Class[[A]] AAA;
+  struct $Class[[B]] {
+$Class[[B]]();
+~$Class[[B]]();
+void operator<<($Class[[B]]);
+$Class[[AAA]] AA;
+  };
+  $Class[[B]]::$Class[[B]]() {}
+  $Class[[B]]::~$Class[[B]]() {}
+  void $Function[[f]] () {
+$Class[[B]] $Variable[[BB]] = $Class[[B]]();
+$Variable[[BB]].~$Class[[B]]();
+$Class[[B]]();
+  }
+)cpp",
+R"cpp(
+  enum class $Enum[[E]] {};
+  enum $Enum[[EE]] {};
+  struct $Class[[A]] {
+$Enum[[E]] EEE;
+$Enum[[EE]] ;
   };
 )cpp"};
   for (const auto  : TestCases) {
Index: clang-tools-extra/clangd/test/semantic-highlighting.test
===
--- clang-tools-extra/clangd/test/semantic-highlighting.test
+++ clang-tools-extra/clangd/test/semantic-highlighting.test
@@ -10,6 +10,12 @@
 # CHECK-NEXT:  [
 # CHECK-NEXT:"entity.name.function.cpp"
 # CHECK-NEXT:  ]
+# CHECK-NEXT:  [
+# CHECK-NEXT:"entity.name.type.class.cpp"
+# CHECK-NEXT:  ],
+# CHECK-NEXT:  [
+# CHECK-NEXT:"entity.name.type.enum.cpp"
+# CHECK-NEXT:  ]
 # CHECK-NEXT:]
 # CHECK-NEXT:  },
 ---
Index: clang-tools-extra/clangd/SemanticHighlighting.h
===
--- clang-tools-extra/clangd/SemanticHighlighting.h
+++ clang-tools-extra/clangd/SemanticHighlighting.h
@@ -26,6 +26,8 @@
 enum class HighlightingKind {
   Variable = 0,
   Function,
+  Class,
+  Enum,
 
   NumKinds,
 };
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -35,7 +35,14 @@
   }
 
   bool VisitNamedDecl(NamedDecl *ND) {
-// FIXME: (De)Constructors/operator need to be highlighted some other way.
+// Constructors have a TypePtr TagDecl that is a Function, therefore we need
+// to get constructors as a NamedDecl instead.
+if (ND->getDeclName().getNameKind() ==
+

[PATCH] D64257: [clangd] Added highlighting for non-builtin types

2019-07-09 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 208622.
jvikstrom added a comment.

Changed commit message.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64257

Files:
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/SemanticHighlighting.h
  clang-tools-extra/clangd/test/semantic-highlighting.test
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -34,11 +34,13 @@
   auto AST = TestTU::withCode(Test.code()).build();
   static const std::map KindToString{
   {HighlightingKind::Variable, "Variable"},
-  {HighlightingKind::Function, "Function"}};
+  {HighlightingKind::Function, "Function"},
+  {HighlightingKind::Class, "Class"},
+  {HighlightingKind::Enum, "Enum"}};
   std::vector ExpectedTokens;
   for (const auto  : KindToString) {
-std::vector Toks =
-makeHighlightingTokens(Test.ranges(KindString.second), KindString.first);
+std::vector Toks = makeHighlightingTokens(
+Test.ranges(KindString.second), KindString.first);
 ExpectedTokens.insert(ExpectedTokens.end(), Toks.begin(), Toks.end());
   }
 
@@ -49,14 +51,14 @@
 TEST(SemanticHighlighting, GetsCorrectTokens) {
   const char *TestCases[] = {
 R"cpp(
-  struct AS {
+  struct $Class[[AS]] {
 double SomeMember;
   };
-  struct {
+  $Class[[struct]] {
   } $Variable[[S]];
-  void $Function[[foo]](int $Variable[[A]]) {
+  void $Function[[foo]](int $Variable[[A]], $Class[[AS]] $Variable[[As]]) {
 auto $Variable[[VeryLongVariableName]] = 12312;
-AS $Variable[[AA]];
+$Class[[AS]] $Variable[[AA]];
 auto $Variable[[L]] = $Variable[[AA]].SomeMember + $Variable[[A]];
 auto $Variable[[FN]] = [ $Variable[[AA]]](int $Variable[[A]]) -> void {};
 $Variable[[FN]](12312);
@@ -68,13 +70,43 @@
   void $Function[[foo]]() {
 auto $Variable[[Bou]] = $Function[[Gah]];
   }
+  struct $Class[[A]] {
+void $Function[[abc]]();
+  };
 )cpp",
 R"cpp(
-  struct A {
-A();
-~A();
-void $Function[[abc]]();
-void operator<<(int);
+  namespace abc {
+template
+struct $Class[[A]] {
+  T t;
+};
+  }
+  template
+  struct $Class[[C]] : abc::A {
+typename T::A* D;
+  };
+  abc::$Class[[A]] $Variable[[AA]];
+  typedef abc::$Class[[A]] AAA;
+  struct $Class[[B]] {
+$Class[[B]]();
+~$Class[[B]]();
+void operator<<($Class[[B]]);
+$Class[[AAA]] AA;
+  };
+  $Class[[B]]::$Class[[B]]() {}
+  $Class[[B]]::~$Class[[B]]() {}
+  void $Function[[f]] () {
+$Class[[B]] $Variable[[BB]] = $Class[[B]]();
+$Variable[[BB]].~$Class[[B]]();
+$Class[[B]]();
+  }
+)cpp",
+R"cpp(
+  enum class $Enum[[E]] {};
+  enum $Enum[[EE]] {};
+  struct $Class[[A]] {
+$Enum[[E]] EEE;
+$Enum[[EE]] ;
   };
 )cpp"};
   for (const auto  : TestCases) {
Index: clang-tools-extra/clangd/test/semantic-highlighting.test
===
--- clang-tools-extra/clangd/test/semantic-highlighting.test
+++ clang-tools-extra/clangd/test/semantic-highlighting.test
@@ -10,6 +10,12 @@
 # CHECK-NEXT:  [
 # CHECK-NEXT:"entity.name.function.cpp"
 # CHECK-NEXT:  ]
+# CHECK-NEXT:  [
+# CHECK-NEXT:"entity.name.type.class.cpp"
+# CHECK-NEXT:  ],
+# CHECK-NEXT:  [
+# CHECK-NEXT:"entity.name.type.enum.cpp"
+# CHECK-NEXT:  ]
 # CHECK-NEXT:]
 # CHECK-NEXT:  },
 ---
Index: clang-tools-extra/clangd/SemanticHighlighting.h
===
--- clang-tools-extra/clangd/SemanticHighlighting.h
+++ clang-tools-extra/clangd/SemanticHighlighting.h
@@ -26,6 +26,8 @@
 enum class HighlightingKind {
   Variable = 0,
   Function,
+  Class,
+  Enum,
 
   NumKinds,
 };
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -35,7 +35,14 @@
   }
 
   bool VisitNamedDecl(NamedDecl *ND) {
-// FIXME: (De)Constructors/operator need to be highlighted some other way.
+// Constructors have a TypePtr TagDecl that is a Function, therefore we need
+// to get constructors as a NamedDecl instead.
+if (ND->getDeclName().getNameKind() ==
+DeclarationName::CXXConstructorName) {
+  

[PATCH] D64257: [clangd] Added highlighting for class and enum types

2019-07-09 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom added inline comments.



Comment at: clang-tools-extra/clangd/SemanticHighlighting.cpp:81
   void addToken(SourceLocation Loc, const Decl *D) {
+if (isa(D)) {
+  addToken(Loc, HighlightingKind::Class);

hokein wrote:
> nit: move this around `if (isa(D)) {` since they are related to 
> `Class`, and we should have a comment describing the highlighting behavior of 
> `class, constructor, and destructor`.
I don't really know what you mean with this comment after the move RecordDecl 
around (or rather where to put the comment and what to put in it)
I wrote a comment but don't know if that's  really helpful


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64257



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D64257: [clangd] Added highlighting for class and enum types

2019-07-09 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom added inline comments.



Comment at: clang-tools-extra/clangd/SemanticHighlighting.cpp:38
   bool VisitNamedDecl(NamedDecl *ND) {
-// FIXME: (De)Constructors/operator need to be highlighted some other way.
+// Constructors have a TypePtr TagDecl that is a Function, therefore we 
need
+// to get constructors as a NamedDecl instead.

hokein wrote:
> I don't understand this comment -- when visiting the constructor AST, we get 
> a TagTypeLoc, and its underlying Decl is a `CXXConstructorDecl`
So the Constructor TypeLoc does not have a TagTypeDecl and is not a TagTypeLoc. 
When we get the TypePtr of the constructor it's a "FunctionProtoType" and there 
is no way to distinguish it from other functions. Therefore we need to get the 
constructor decls as NamedDecls..

The comment was written badly though. This version should be better now I hope.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64257



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D64257: [clangd] Added highlighting for class and enum types

2019-07-09 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 208670.
jvikstrom marked 4 inline comments as done.
jvikstrom added a comment.

No longer highlighting anonymous structs as types.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64257

Files:
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/SemanticHighlighting.h
  clang-tools-extra/clangd/test/semantic-highlighting.test
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -34,11 +34,13 @@
   auto AST = TestTU::withCode(Test.code()).build();
   static const std::map KindToString{
   {HighlightingKind::Variable, "Variable"},
-  {HighlightingKind::Function, "Function"}};
+  {HighlightingKind::Function, "Function"},
+  {HighlightingKind::Class, "Class"},
+  {HighlightingKind::Enum, "Enum"}};
   std::vector ExpectedTokens;
   for (const auto  : KindToString) {
-std::vector Toks =
-makeHighlightingTokens(Test.ranges(KindString.second), KindString.first);
+std::vector Toks = makeHighlightingTokens(
+Test.ranges(KindString.second), KindString.first);
 ExpectedTokens.insert(ExpectedTokens.end(), Toks.begin(), Toks.end());
   }
 
@@ -49,14 +51,14 @@
 TEST(SemanticHighlighting, GetsCorrectTokens) {
   const char *TestCases[] = {
 R"cpp(
-  struct AS {
+  struct $Class[[AS]] {
 double SomeMember;
   };
   struct {
   } $Variable[[S]];
-  void $Function[[foo]](int $Variable[[A]]) {
+  void $Function[[foo]](int $Variable[[A]], $Class[[AS]] $Variable[[As]]) {
 auto $Variable[[VeryLongVariableName]] = 12312;
-AS $Variable[[AA]];
+$Class[[AS]] $Variable[[AA]];
 auto $Variable[[L]] = $Variable[[AA]].SomeMember + $Variable[[A]];
 auto $Variable[[FN]] = [ $Variable[[AA]]](int $Variable[[A]]) -> void {};
 $Variable[[FN]](12312);
@@ -68,13 +70,43 @@
   void $Function[[foo]]() {
 auto $Variable[[Bou]] = $Function[[Gah]];
   }
+  struct $Class[[A]] {
+void $Function[[abc]]();
+  };
 )cpp",
 R"cpp(
-  struct A {
-A();
-~A();
-void $Function[[abc]]();
-void operator<<(int);
+  namespace abc {
+template
+struct $Class[[A]] {
+  T t;
+};
+  }
+  template
+  struct $Class[[C]] : abc::A {
+typename T::A* D;
+  };
+  abc::$Class[[A]] $Variable[[AA]];
+  typedef abc::$Class[[A]] AAA;
+  struct $Class[[B]] {
+$Class[[B]]();
+~$Class[[B]]();
+void operator<<($Class[[B]]);
+$Class[[AAA]] AA;
+  };
+  $Class[[B]]::$Class[[B]]() {}
+  $Class[[B]]::~$Class[[B]]() {}
+  void $Function[[f]] () {
+$Class[[B]] $Variable[[BB]] = $Class[[B]]();
+$Variable[[BB]].~$Class[[B]]();
+$Class[[B]]();
+  }
+)cpp",
+R"cpp(
+  enum class $Enum[[E]] {};
+  enum $Enum[[EE]] {};
+  struct $Class[[A]] {
+$Enum[[E]] EEE;
+$Enum[[EE]] ;
   };
 )cpp"};
   for (const auto  : TestCases) {
Index: clang-tools-extra/clangd/test/semantic-highlighting.test
===
--- clang-tools-extra/clangd/test/semantic-highlighting.test
+++ clang-tools-extra/clangd/test/semantic-highlighting.test
@@ -10,6 +10,12 @@
 # CHECK-NEXT:  [
 # CHECK-NEXT:"entity.name.function.cpp"
 # CHECK-NEXT:  ]
+# CHECK-NEXT:  [
+# CHECK-NEXT:"entity.name.type.class.cpp"
+# CHECK-NEXT:  ],
+# CHECK-NEXT:  [
+# CHECK-NEXT:"entity.name.type.enum.cpp"
+# CHECK-NEXT:  ]
 # CHECK-NEXT:]
 # CHECK-NEXT:  },
 ---
Index: clang-tools-extra/clangd/SemanticHighlighting.h
===
--- clang-tools-extra/clangd/SemanticHighlighting.h
+++ clang-tools-extra/clangd/SemanticHighlighting.h
@@ -26,6 +26,8 @@
 enum class HighlightingKind {
   Variable = 0,
   Function,
+  Class,
+  Enum,
 
   NumKinds,
 };
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -11,6 +11,8 @@
 #include "Protocol.h"
 #include "SourceCode.h"
 #include "clang/AST/ASTContext.h"
+#include "clang/AST/Decl.h"
+#include "clang/AST/DeclarationName.h"
 #include "clang/AST/RecursiveASTVisitor.h"
 
 namespace clang {
@@ -35,7 +37,15 @@
   }
 
   bool VisitNamedDecl(NamedDecl *ND) {
-// FIXME: 

[PATCH] D64257: [clangd] Added highlighting for class and enum types

2019-07-09 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 208671.
jvikstrom added a comment.

Changed comment in addToken.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64257

Files:
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/SemanticHighlighting.h
  clang-tools-extra/clangd/test/semantic-highlighting.test
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -34,11 +34,13 @@
   auto AST = TestTU::withCode(Test.code()).build();
   static const std::map KindToString{
   {HighlightingKind::Variable, "Variable"},
-  {HighlightingKind::Function, "Function"}};
+  {HighlightingKind::Function, "Function"},
+  {HighlightingKind::Class, "Class"},
+  {HighlightingKind::Enum, "Enum"}};
   std::vector ExpectedTokens;
   for (const auto  : KindToString) {
-std::vector Toks =
-makeHighlightingTokens(Test.ranges(KindString.second), KindString.first);
+std::vector Toks = makeHighlightingTokens(
+Test.ranges(KindString.second), KindString.first);
 ExpectedTokens.insert(ExpectedTokens.end(), Toks.begin(), Toks.end());
   }
 
@@ -49,14 +51,14 @@
 TEST(SemanticHighlighting, GetsCorrectTokens) {
   const char *TestCases[] = {
 R"cpp(
-  struct AS {
+  struct $Class[[AS]] {
 double SomeMember;
   };
   struct {
   } $Variable[[S]];
-  void $Function[[foo]](int $Variable[[A]]) {
+  void $Function[[foo]](int $Variable[[A]], $Class[[AS]] $Variable[[As]]) {
 auto $Variable[[VeryLongVariableName]] = 12312;
-AS $Variable[[AA]];
+$Class[[AS]] $Variable[[AA]];
 auto $Variable[[L]] = $Variable[[AA]].SomeMember + $Variable[[A]];
 auto $Variable[[FN]] = [ $Variable[[AA]]](int $Variable[[A]]) -> void {};
 $Variable[[FN]](12312);
@@ -68,13 +70,43 @@
   void $Function[[foo]]() {
 auto $Variable[[Bou]] = $Function[[Gah]];
   }
+  struct $Class[[A]] {
+void $Function[[abc]]();
+  };
 )cpp",
 R"cpp(
-  struct A {
-A();
-~A();
-void $Function[[abc]]();
-void operator<<(int);
+  namespace abc {
+template
+struct $Class[[A]] {
+  T t;
+};
+  }
+  template
+  struct $Class[[C]] : abc::A {
+typename T::A* D;
+  };
+  abc::$Class[[A]] $Variable[[AA]];
+  typedef abc::$Class[[A]] AAA;
+  struct $Class[[B]] {
+$Class[[B]]();
+~$Class[[B]]();
+void operator<<($Class[[B]]);
+$Class[[AAA]] AA;
+  };
+  $Class[[B]]::$Class[[B]]() {}
+  $Class[[B]]::~$Class[[B]]() {}
+  void $Function[[f]] () {
+$Class[[B]] $Variable[[BB]] = $Class[[B]]();
+$Variable[[BB]].~$Class[[B]]();
+$Class[[B]]();
+  }
+)cpp",
+R"cpp(
+  enum class $Enum[[E]] {};
+  enum $Enum[[EE]] {};
+  struct $Class[[A]] {
+$Enum[[E]] EEE;
+$Enum[[EE]] ;
   };
 )cpp"};
   for (const auto  : TestCases) {
Index: clang-tools-extra/clangd/test/semantic-highlighting.test
===
--- clang-tools-extra/clangd/test/semantic-highlighting.test
+++ clang-tools-extra/clangd/test/semantic-highlighting.test
@@ -10,6 +10,12 @@
 # CHECK-NEXT:  [
 # CHECK-NEXT:"entity.name.function.cpp"
 # CHECK-NEXT:  ]
+# CHECK-NEXT:  [
+# CHECK-NEXT:"entity.name.type.class.cpp"
+# CHECK-NEXT:  ],
+# CHECK-NEXT:  [
+# CHECK-NEXT:"entity.name.type.enum.cpp"
+# CHECK-NEXT:  ]
 # CHECK-NEXT:]
 # CHECK-NEXT:  },
 ---
Index: clang-tools-extra/clangd/SemanticHighlighting.h
===
--- clang-tools-extra/clangd/SemanticHighlighting.h
+++ clang-tools-extra/clangd/SemanticHighlighting.h
@@ -26,6 +26,8 @@
 enum class HighlightingKind {
   Variable = 0,
   Function,
+  Class,
+  Enum,
 
   NumKinds,
 };
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -11,6 +11,8 @@
 #include "Protocol.h"
 #include "SourceCode.h"
 #include "clang/AST/ASTContext.h"
+#include "clang/AST/Decl.h"
+#include "clang/AST/DeclarationName.h"
 #include "clang/AST/RecursiveASTVisitor.h"
 
 namespace clang {
@@ -35,7 +37,15 @@
   }
 
   bool VisitNamedDecl(NamedDecl *ND) {
-// FIXME: (De)Constructors/operator need to be highlighted some other way.
+// Constructors' 

[PATCH] D64257: [clangd] Added highlighting for class and enum types

2019-07-09 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 208708.
jvikstrom marked 4 inline comments as done.
jvikstrom added a comment.

Addressed comments.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64257

Files:
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/SemanticHighlighting.h
  clang-tools-extra/clangd/test/semantic-highlighting.test
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -34,11 +34,13 @@
   auto AST = TestTU::withCode(Test.code()).build();
   static const std::map KindToString{
   {HighlightingKind::Variable, "Variable"},
-  {HighlightingKind::Function, "Function"}};
+  {HighlightingKind::Function, "Function"},
+  {HighlightingKind::Class, "Class"},
+  {HighlightingKind::Enum, "Enum"}};
   std::vector ExpectedTokens;
   for (const auto  : KindToString) {
-std::vector Toks =
-makeHighlightingTokens(Test.ranges(KindString.second), KindString.first);
+std::vector Toks = makeHighlightingTokens(
+Test.ranges(KindString.second), KindString.first);
 ExpectedTokens.insert(ExpectedTokens.end(), Toks.begin(), Toks.end());
   }
 
@@ -49,14 +51,14 @@
 TEST(SemanticHighlighting, GetsCorrectTokens) {
   const char *TestCases[] = {
 R"cpp(
-  struct AS {
+  struct $Class[[AS]] {
 double SomeMember;
   };
   struct {
   } $Variable[[S]];
-  void $Function[[foo]](int $Variable[[A]]) {
+  void $Function[[foo]](int $Variable[[A]], $Class[[AS]] $Variable[[As]]) {
 auto $Variable[[VeryLongVariableName]] = 12312;
-AS $Variable[[AA]];
+$Class[[AS]] $Variable[[AA]];
 auto $Variable[[L]] = $Variable[[AA]].SomeMember + $Variable[[A]];
 auto $Variable[[FN]] = [ $Variable[[AA]]](int $Variable[[A]]) -> void {};
 $Variable[[FN]](12312);
@@ -68,13 +70,43 @@
   void $Function[[foo]]() {
 auto $Variable[[Bou]] = $Function[[Gah]];
   }
+  struct $Class[[A]] {
+void $Function[[abc]]();
+  };
 )cpp",
 R"cpp(
-  struct A {
-A();
-~A();
-void $Function[[abc]]();
-void operator<<(int);
+  namespace abc {
+template
+struct $Class[[A]] {
+  T t;
+};
+  }
+  template
+  struct $Class[[C]] : abc::A {
+typename T::A* D;
+  };
+  abc::$Class[[A]] $Variable[[AA]];
+  typedef abc::$Class[[A]] AAA;
+  struct $Class[[B]] {
+$Class[[B]]();
+~$Class[[B]]();
+void operator<<($Class[[B]]);
+$Class[[AAA]] AA;
+  };
+  $Class[[B]]::$Class[[B]]() {}
+  $Class[[B]]::~$Class[[B]]() {}
+  void $Function[[f]] () {
+$Class[[B]] $Variable[[BB]] = $Class[[B]]();
+$Variable[[BB]].~$Class[[B]]();
+$Class[[B]]();
+  }
+)cpp",
+R"cpp(
+  enum class $Enum[[E]] {};
+  enum $Enum[[EE]] {};
+  struct $Class[[A]] {
+$Enum[[E]] EEE;
+$Enum[[EE]] ;
   };
 )cpp"};
   for (const auto  : TestCases) {
Index: clang-tools-extra/clangd/test/semantic-highlighting.test
===
--- clang-tools-extra/clangd/test/semantic-highlighting.test
+++ clang-tools-extra/clangd/test/semantic-highlighting.test
@@ -10,6 +10,12 @@
 # CHECK-NEXT:  [
 # CHECK-NEXT:"entity.name.function.cpp"
 # CHECK-NEXT:  ]
+# CHECK-NEXT:  [
+# CHECK-NEXT:"entity.name.type.class.cpp"
+# CHECK-NEXT:  ],
+# CHECK-NEXT:  [
+# CHECK-NEXT:"entity.name.type.enum.cpp"
+# CHECK-NEXT:  ]
 # CHECK-NEXT:]
 # CHECK-NEXT:  },
 ---
Index: clang-tools-extra/clangd/SemanticHighlighting.h
===
--- clang-tools-extra/clangd/SemanticHighlighting.h
+++ clang-tools-extra/clangd/SemanticHighlighting.h
@@ -26,6 +26,8 @@
 enum class HighlightingKind {
   Variable = 0,
   Function,
+  Class,
+  Enum,
 
   NumKinds,
 };
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -11,6 +11,8 @@
 #include "Protocol.h"
 #include "SourceCode.h"
 #include "clang/AST/ASTContext.h"
+#include "clang/AST/Decl.h"
+#include "clang/AST/DeclarationName.h"
 #include "clang/AST/RecursiveASTVisitor.h"
 
 namespace clang {
@@ -35,13 +37,18 @@
   }
 
   bool VisitNamedDecl(NamedDecl *ND) {
-// FIXME: (De)Constructors/operator need to be highlighted 

[PATCH] D63919: [clangd] Emit publishSemanticHighlighting in LSP if enabled

2019-07-03 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom marked an inline comment as done.
jvikstrom added inline comments.



Comment at: clang-tools-extra/clangd/SemanticHighlighting.cpp:138
+  for (const auto  : TokenLines) {
+llvm::SmallVector LineHighlights;
+llvm::raw_svector_ostream OS(LineHighlights);

hokein wrote:
> The code is a bit tricky here, if I understand the code correctly, 
> `LineHighlights` is the binary data of tokens (each `char` represents a byte).
> Maybe `LineEncodingTokens`?
They aren't "fully" encoded yet though, they get encoded after the inner loop 
is done. How about `LineByteTokens`?




Comment at: clang-tools-extra/clangd/SemanticHighlighting.cpp:144
+  write32be(Token.R.start.character, OS);
+  write16be(Token.R.end.character - Token.R.start.character, OS);
+  write16be(static_cast(Token.Kind), OS);

hokein wrote:
> jvikstrom wrote:
> > hokein wrote:
> > > if the token is across multiple line, we will emit an ill-formed results.
> > There's a FIXME above (which is where it should probably be handled). A bit 
> > unsure how to solve though. If a token is a block comment spanning multiple 
> > lines we would need to know the length of every line in the block comment. 
> > Probably something that can be solved with the ASTContext  or SourceManager 
> > but that can't be accessed in this function.
> oh, i missed that FIXME, the FIXME is a bit far away, maybe move it here (now 
> we assume the token is always on the same line).
I think that this should probably be handled above (so if a token covers `N` 
different lines it would be separated into `N` tokens in the `TokenLines` map. 
I don't really see how it would be possible to handle that in this loop 
(without basically putting all the smallvectors for every line in another 
vector and  writing to `Lines` in another loop after the loop is done. But it 
feels like that would be way more complex) 



Comment at: clang-tools-extra/clangd/SemanticHighlighting.h:8
 
//===--===//
 
 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICHIGHLIGHTING_H

hokein wrote:
> The semantic highlighting is not in LSP yet, I think we need some 
> documentations here to explain some more details about this feature in 
> clangd, like the implementations are based on the proposal 
> (https://github.com/microsoft/vscode-languageserver-node/pull/367).  
Changed the initialize-params test to have semantic highlighting enabled as 
well.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63919



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D63919: [clangd] Emit publishSemanticHighlighting in LSP if enabled

2019-07-02 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 207537.
jvikstrom marked 8 inline comments as done.
jvikstrom added a comment.

Address comments.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63919

Files:
  clang-tools-extra/clangd/ClangdLSPServer.cpp
  clang-tools-extra/clangd/ClangdLSPServer.h
  clang-tools-extra/clangd/Protocol.cpp
  clang-tools-extra/clangd/Protocol.h
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/SemanticHighlighting.h
  clang-tools-extra/clangd/test/initialize-params.test
  clang-tools-extra/clangd/test/semantic-highlighting.test
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -8,6 +8,7 @@
 
 #include "Annotations.h"
 #include "ClangdServer.h"
+#include "Protocol.h"
 #include "SemanticHighlighting.h"
 #include "TestFS.h"
 #include "TestTU.h"
@@ -66,7 +67,7 @@
   }
 }
 
-TEST(ClangdSemanticHighlightingTest, GeneratesHighlightsWhenFileChange) {
+TEST(SemanticHighlighting, GeneratesHighlightsWhenFileChange) {
   class HighlightingsCounterDiagConsumer : public DiagnosticsConsumer {
   public:
 std::atomic Count = {0};
@@ -90,6 +91,29 @@
   ASSERT_EQ(DiagConsumer.Count, 1);
 }
 
+TEST(SemanticHighlighting, toSemanticHighlightingInformation) {
+  auto CreatePosition = [](int Line, int Character) -> Position {
+Position Pos;
+Pos.line = Line;
+Pos.character = Character;
+return Pos;
+  };
+
+  std::vector Tokens{
+  {HighlightingKind::Variable,
+Range{CreatePosition(3, 8), CreatePosition(3, 12)}},
+  {HighlightingKind::Function,
+Range{CreatePosition(3, 4), CreatePosition(3, 7)}},
+  {HighlightingKind::Variable,
+Range{CreatePosition(1, 1), CreatePosition(1, 5)}}};
+  std::vector Info =
+  toSemanticHighlightingInformation(Tokens);
+  std::vector Correct = {
+  {1, "AQAEAAA="},
+  {3, "CAAEAAAEAAMAAQ=="}};
+  ASSERT_EQ(Info, Correct);
+}
+
 } // namespace
 } // namespace clangd
 } // namespace clang
Index: clang-tools-extra/clangd/test/semantic-highlighting.test
===
--- /dev/null
+++ clang-tools-extra/clangd/test/semantic-highlighting.test
@@ -0,0 +1,21 @@
+# RUN: clangd -hidden-features -lit-test < %s | FileCheck -strict-whitespace %s
+{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{"textDocument": {"semanticHighlightingCapabilities": {"semanticHighlighting": true}}},"trace":"off"}}
+---
+{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"uri":"test:///foo.cpp","languageId":"cpp","version":1,"text":"int x = 2;"}}}
+#  CHECK:  "method": "textDocument/semanticHighlighting",
+# CHECK-NEXT:  "params": {
+# CHECK-NEXT:"lines": [
+# CHECK-NEXT:  {
+# CHECK-NEXT:"line": 0,
+# CHECK-NEXT:"tokens": "BAABAAA="
+# CHECK-NEXT:  }
+# CHECK-NEXT:],
+# CHECK-NEXT:"textDocument": {
+# CHECK-NEXT:  "uri": "file:///clangd-test/foo.cpp"
+# CHECK-NEXT:}
+# CHECK-NEXT:  }
+# CHECK-NEXT:}
+---
+{"jsonrpc":"2.0","id":3,"method":"shutdown"}
+---
+{"jsonrpc":"2.0","method":"exit"}
Index: clang-tools-extra/clangd/test/initialize-params.test
===
--- clang-tools-extra/clangd/test/initialize-params.test
+++ clang-tools-extra/clangd/test/initialize-params.test
@@ -33,6 +33,16 @@
 # CHECK-NEXT:  "hoverProvider": true,
 # CHECK-NEXT:  "referencesProvider": true,
 # CHECK-NEXT:  "renameProvider": true,
+# CHECK-NEXT:  "semanticHighlighting": {
+# CHECK-NEXT:"scopes": [
+# CHECK-NEXT:  [
+# CHECK-NEXT:"variable.cpp"
+# CHECK-NEXT:  ],
+# CHECK-NEXT:  [
+# CHECK-NEXT:"entity.name.function.cpp"
+# CHECK-NEXT:  ]
+# CHECK-NEXT:]
+# CHECK-NEXT:  },
 # CHECK-NEXT:  "signatureHelpProvider": {
 # CHECK-NEXT:"triggerCharacters": [
 # CHECK-NEXT:  "(",
Index: clang-tools-extra/clangd/SemanticHighlighting.h
===
--- clang-tools-extra/clangd/SemanticHighlighting.h
+++ clang-tools-extra/clangd/SemanticHighlighting.h
@@ -4,19 +4,25 @@
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
+// An implementation of semantic highlighting based on this proposal:
+// https://github.com/microsoft/vscode-languageserver-node/pull/367 in clangd.
+// Semantic highlightings are calculated for an AST by visiting 

[PATCH] D63919: [clangd] Emit publishSemanticHighlighting in LSP if enabled

2019-07-02 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom added inline comments.



Comment at: clang-tools-extra/clangd/SemanticHighlighting.cpp:144
+  write32be(Token.R.start.character, OS);
+  write16be(Token.R.end.character - Token.R.start.character, OS);
+  write16be(static_cast(Token.Kind), OS);

hokein wrote:
> if the token is across multiple line, we will emit an ill-formed results.
There's a FIXME above (which is where it should probably be handled). A bit 
unsure how to solve though. If a token is a block comment spanning multiple 
lines we would need to know the length of every line in the block comment. 
Probably something that can be solved with the ASTContext  or SourceManager but 
that can't be accessed in this function.



Comment at: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp:104
+  HighlightingToken{HighlightingKind::Variable,
+Range{CreatePosition(3, 8), CreatePosition(3, 12)}},
+  HighlightingToken{HighlightingKind::Function,

hokein wrote:
> `Range{ /*start*/{3, 8}, /*end*/{3, 12} }` should be compilable.
Doesn't compile because of the default initialization of `line` and `character` 
in Position.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63919



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D63919: [clangd] Emit publishSemanticHighlighting in LSP if enabled

2019-07-02 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 207563.
jvikstrom added a comment.

Removed from hiding under hidden flag .


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63919

Files:
  clang-tools-extra/clangd/ClangdLSPServer.cpp
  clang-tools-extra/clangd/ClangdLSPServer.h
  clang-tools-extra/clangd/Protocol.cpp
  clang-tools-extra/clangd/Protocol.h
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/SemanticHighlighting.h
  clang-tools-extra/clangd/test/initialize-params.test
  clang-tools-extra/clangd/test/semantic-highlighting.test
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -8,6 +8,7 @@
 
 #include "Annotations.h"
 #include "ClangdServer.h"
+#include "Protocol.h"
 #include "SemanticHighlighting.h"
 #include "TestFS.h"
 #include "TestTU.h"
@@ -66,7 +67,7 @@
   }
 }
 
-TEST(ClangdSemanticHighlightingTest, GeneratesHighlightsWhenFileChange) {
+TEST(SemanticHighlighting, GeneratesHighlightsWhenFileChange) {
   class HighlightingsCounterDiagConsumer : public DiagnosticsConsumer {
   public:
 std::atomic Count = {0};
@@ -90,6 +91,29 @@
   ASSERT_EQ(DiagConsumer.Count, 1);
 }
 
+TEST(SemanticHighlighting, toSemanticHighlightingInformation) {
+  auto CreatePosition = [](int Line, int Character) -> Position {
+Position Pos;
+Pos.line = Line;
+Pos.character = Character;
+return Pos;
+  };
+
+  std::vector Tokens{
+  {HighlightingKind::Variable,
+Range{CreatePosition(3, 8), CreatePosition(3, 12)}},
+  {HighlightingKind::Function,
+Range{CreatePosition(3, 4), CreatePosition(3, 7)}},
+  {HighlightingKind::Variable,
+Range{CreatePosition(1, 1), CreatePosition(1, 5)}}};
+  std::vector Info =
+  toSemanticHighlightingInformation(Tokens);
+  std::vector Correct = {
+  {1, "AQAEAAA="},
+  {3, "CAAEAAAEAAMAAQ=="}};
+  ASSERT_EQ(Info, Correct);
+}
+
 } // namespace
 } // namespace clangd
 } // namespace clang
Index: clang-tools-extra/clangd/test/semantic-highlighting.test
===
--- /dev/null
+++ clang-tools-extra/clangd/test/semantic-highlighting.test
@@ -0,0 +1,21 @@
+# RUN: clangd -lit-test < %s | FileCheck -strict-whitespace %s
+{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{"textDocument": {"semanticHighlightingCapabilities": {"semanticHighlighting": true}}},"trace":"off"}}
+---
+{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"uri":"test:///foo.cpp","languageId":"cpp","version":1,"text":"int x = 2;"}}}
+#  CHECK:  "method": "textDocument/semanticHighlighting",
+# CHECK-NEXT:  "params": {
+# CHECK-NEXT:"lines": [
+# CHECK-NEXT:  {
+# CHECK-NEXT:"line": 0,
+# CHECK-NEXT:"tokens": "BAABAAA="
+# CHECK-NEXT:  }
+# CHECK-NEXT:],
+# CHECK-NEXT:"textDocument": {
+# CHECK-NEXT:  "uri": "file:///clangd-test/foo.cpp"
+# CHECK-NEXT:}
+# CHECK-NEXT:  }
+# CHECK-NEXT:}
+---
+{"jsonrpc":"2.0","id":3,"method":"shutdown"}
+---
+{"jsonrpc":"2.0","method":"exit"}
Index: clang-tools-extra/clangd/test/initialize-params.test
===
--- clang-tools-extra/clangd/test/initialize-params.test
+++ clang-tools-extra/clangd/test/initialize-params.test
@@ -33,6 +33,16 @@
 # CHECK-NEXT:  "hoverProvider": true,
 # CHECK-NEXT:  "referencesProvider": true,
 # CHECK-NEXT:  "renameProvider": true,
+# CHECK-NEXT:  "semanticHighlighting": {
+# CHECK-NEXT:"scopes": [
+# CHECK-NEXT:  [
+# CHECK-NEXT:"variable.cpp"
+# CHECK-NEXT:  ],
+# CHECK-NEXT:  [
+# CHECK-NEXT:"entity.name.function.cpp"
+# CHECK-NEXT:  ]
+# CHECK-NEXT:]
+# CHECK-NEXT:  },
 # CHECK-NEXT:  "signatureHelpProvider": {
 # CHECK-NEXT:"triggerCharacters": [
 # CHECK-NEXT:  "(",
Index: clang-tools-extra/clangd/SemanticHighlighting.h
===
--- clang-tools-extra/clangd/SemanticHighlighting.h
+++ clang-tools-extra/clangd/SemanticHighlighting.h
@@ -4,19 +4,25 @@
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
+// An implementation of semantic highlighting based on this proposal:
+// https://github.com/microsoft/vscode-languageserver-node/pull/367 in clangd.
+// Semantic highlightings are calculated for an AST by visiting every AST node
+// and classifying nodes 

[PATCH] D63919: [clangd] Emit publishSemanticHighlighting in LSP if enabled

2019-07-04 Thread Johan Vikström via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
jvikstrom marked 2 inline comments as done.
Closed by commit rL365111: [clangd] Emit publishSemanticHighlighting in LSP if 
enabled (authored by jvikstrom, committed by ).
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D63919?vs=207783=207978#toc

Repository:
  rL LLVM

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

https://reviews.llvm.org/D63919

Files:
  clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp
  clang-tools-extra/trunk/clangd/ClangdLSPServer.h
  clang-tools-extra/trunk/clangd/Protocol.cpp
  clang-tools-extra/trunk/clangd/Protocol.h
  clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp
  clang-tools-extra/trunk/clangd/SemanticHighlighting.h
  clang-tools-extra/trunk/clangd/test/semantic-highlighting.test
  clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/trunk/clangd/ClangdLSPServer.h
===
--- clang-tools-extra/trunk/clangd/ClangdLSPServer.h
+++ clang-tools-extra/trunk/clangd/ClangdLSPServer.h
@@ -55,6 +55,8 @@
   // Implement DiagnosticsConsumer.
   void onDiagnosticsReady(PathRef File, std::vector Diagnostics) override;
   void onFileUpdated(PathRef File, const TUStatus ) override;
+  void onHighlightingsReady(PathRef File,
+ std::vector Highlightings) override;
 
   // LSP methods. Notifications have signature void(const Params&).
   // Calls have signature void(const Params&, Callback).
@@ -115,6 +117,9 @@
   void reparseOpenedFiles();
   void applyConfiguration(const ConfigurationSettings );
 
+  /// Sends a "publishSemanticHighlighting" notification to the LSP client.
+  void publishSemanticHighlighting(SemanticHighlightingParams Params);
+
   /// Sends a "publishDiagnostics" notification to the LSP client.
   void publishDiagnostics(const URIForFile ,
   std::vector Diagnostics);
Index: clang-tools-extra/trunk/clangd/Protocol.cpp
===
--- clang-tools-extra/trunk/clangd/Protocol.cpp
+++ clang-tools-extra/trunk/clangd/Protocol.cpp
@@ -273,6 +273,12 @@
   if (!O)
 return false;
   if (auto *TextDocument = O->getObject("textDocument")) {
+if (auto *SemanticHighlighting =
+TextDocument->getObject("semanticHighlightingCapabilities")) {
+  if (auto SemanticHighlightingSupport =
+  SemanticHighlighting->getBoolean("semanticHighlighting"))
+R.SemanticHighlighting = *SemanticHighlightingSupport;
+}
 if (auto *Diagnostics = TextDocument->getObject("publishDiagnostics")) {
   if (auto CategorySupport = Diagnostics->getBoolean("categorySupport"))
 R.DiagnosticCategory = *CategorySupport;
@@ -1027,5 +1033,22 @@
   return OS << toString(Enc);
 }
 
+bool operator==(const SemanticHighlightingInformation ,
+const SemanticHighlightingInformation ) {
+  return Lhs.Line == Rhs.Line && Lhs.Tokens == Rhs.Tokens;
+}
+
+llvm::json::Value toJSON(const SemanticHighlightingInformation ) {
+  return llvm::json::Object{{"line", Highlighting.Line},
+{"tokens", Highlighting.Tokens}};
+}
+
+llvm::json::Value toJSON(const SemanticHighlightingParams ) {
+  return llvm::json::Object{
+  {"textDocument", Highlighting.TextDocument},
+  {"lines", std::move(Highlighting.Lines)},
+  };
+}
+
 } // namespace clangd
 } // namespace clang
Index: clang-tools-extra/trunk/clangd/test/semantic-highlighting.test
===
--- clang-tools-extra/trunk/clangd/test/semantic-highlighting.test
+++ clang-tools-extra/trunk/clangd/test/semantic-highlighting.test
@@ -0,0 +1,75 @@
+# RUN: clangd -lit-test < %s | FileCheck -strict-whitespace %s
+{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{"textDocument":{"semanticHighlightingCapabilities":{"semanticHighlighting":true}}},"trace":"off"}}
+---
+#  CHECK:  "id": 0,
+# CHECK-NEXT:  "jsonrpc": "2.0",
+# CHECK-NEXT:  "result": {
+# CHECK-NEXT:"capabilities": {
+# CHECK-NEXT:  "codeActionProvider": true,
+# CHECK-NEXT:  "completionProvider": {
+# CHECK-NEXT:"resolveProvider": false,
+# CHECK-NEXT:"triggerCharacters": [
+# CHECK-NEXT:  ".",
+# CHECK-NEXT:  ">",
+# CHECK-NEXT:  ":"
+# CHECK-NEXT:]
+# CHECK-NEXT:  },
+# CHECK-NEXT:  "declarationProvider": true,
+# CHECK-NEXT:  "definitionProvider": true,
+# CHECK-NEXT:  "documentFormattingProvider": true,
+# CHECK-NEXT:  "documentHighlightProvider": true,
+# CHECK-NEXT:  "documentOnTypeFormattingProvider": {
+# CHECK-NEXT:"firstTriggerCharacter": "\n",
+# CHECK-NEXT:"moreTriggerCharacter": []
+# CHECK-NEXT:  },
+# CHECK-NEXT: 

[PATCH] D64199: [clangd] Added highlighting for variable references (declrefs)

2019-07-04 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom created this revision.
jvikstrom added reviewers: hokein, sammccall.
Herald added subscribers: cfe-commits, kadircet, arphaman, jkorous, MaskRay, 
ilya-biryukov.
Herald added a project: clang.

Added highlighting for variable references using VisitDeclRefExpr.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D64199

Files:
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp


Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -57,10 +57,18 @@
 void $Function[[foo]](int $Variable[[a]]) {
   auto $Variable[[VeryLongVariableName]] = 12312;
   A $Variable[[aa]];
+  auto $Variable[[l]] = $Variable[[aa]].SomeMember + $Variable[[a]];
 }
   )cpp",
   R"cpp(
 void $Function[[foo]](int);
+  )cpp",
+  R"cpp(
+void $Function[[foo]]() {
+  int $Variable[[b]];
+  auto $Variable[[FN]] = [ $Variable[[b]]](int $Variable[[a]]) -> void {};
+  $Variable[[FN]](12312);
+}
   )cpp"};
   for (const auto  : TestCases) {
 checkHighlightings(TestCase);
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -35,25 +35,31 @@
   }
 
   bool VisitVarDecl(VarDecl *Var) {
-addToken(Var, HighlightingKind::Variable);
+addNamedDecl(Var, HighlightingKind::Variable);
 return true;
   }
   bool VisitFunctionDecl(FunctionDecl *Func) {
-addToken(Func, HighlightingKind::Function);
+addNamedDecl(Func, HighlightingKind::Function);
+return true;
+  }
+
+  bool VisitDeclRefExpr(DeclRefExpr *Ref) {
+if (Ref->getNameInfo().getName().getNameKind() ==
+DeclarationName::CXXOperatorName)
+  // Don't want to highlight operators.
+  return true;
+
+addToken(Ref->getLocation(), HighlightingKind::Variable);
 return true;
   }
 
 private:
-  void addToken(const NamedDecl *D, HighlightingKind Kind) {
-if (D->getLocation().isMacroID())
+  void addToken(SourceLocation Loc, HighlightingKind Kind) {
+if (Loc.isMacroID())
   // FIXME: skip tokens inside macros for now.
   return;
 
-if (D->getDeclName().isEmpty())
-  // Don't add symbols that don't have any length.
-  return;
-
-auto R = getTokenRange(SM, Ctx.getLangOpts(), D->getLocation());
+auto R = getTokenRange(SM, Ctx.getLangOpts(), Loc);
 if (!R) {
   // R should always have a value, if it doesn't something is very wrong.
   elog("Tried to add semantic token with an invalid range");
@@ -62,6 +68,13 @@
 
 Tokens.push_back({Kind, R.getValue()});
   }
+
+  void addNamedDecl(const NamedDecl *D, HighlightingKind Kind) {
+if (D->getDeclName().isEmpty())
+  // Don't add symbols that don't have any length.
+  return;
+addToken(D->getLocation(), Kind);
+  }
 };
 
 // Encode binary data into base64.


Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -57,10 +57,18 @@
 void $Function[[foo]](int $Variable[[a]]) {
   auto $Variable[[VeryLongVariableName]] = 12312;
   A $Variable[[aa]];
+  auto $Variable[[l]] = $Variable[[aa]].SomeMember + $Variable[[a]];
 }
   )cpp",
   R"cpp(
 void $Function[[foo]](int);
+  )cpp",
+  R"cpp(
+void $Function[[foo]]() {
+  int $Variable[[b]];
+  auto $Variable[[FN]] = [ $Variable[[b]]](int $Variable[[a]]) -> void {};
+  $Variable[[FN]](12312);
+}
   )cpp"};
   for (const auto  : TestCases) {
 checkHighlightings(TestCase);
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -35,25 +35,31 @@
   }
 
   bool VisitVarDecl(VarDecl *Var) {
-addToken(Var, HighlightingKind::Variable);
+addNamedDecl(Var, HighlightingKind::Variable);
 return true;
   }
   bool VisitFunctionDecl(FunctionDecl *Func) {
-addToken(Func, HighlightingKind::Function);
+addNamedDecl(Func, HighlightingKind::Function);
+return true;
+  }
+
+  bool VisitDeclRefExpr(DeclRefExpr *Ref) {
+if (Ref->getNameInfo().getName().getNameKind() ==
+DeclarationName::CXXOperatorName)
+  // Don't want to highlight operators.
+  return true;
+
+addToken(Ref->getLocation(), HighlightingKind::Variable);
 return true;
   }
 
 private:
-  void addToken(const 

[PATCH] D63817: [clangd] No longer getting template instantiations from header files in Main AST.

2019-07-01 Thread Johan Vikström via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL364747: [clangd] No longer getting template instantiations 
from header files in Main… (authored by jvikstrom, committed by ).
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D63817?vs=206685=207261#toc

Repository:
  rL LLVM

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

https://reviews.llvm.org/D63817

Files:
  clang-tools-extra/trunk/clangd/ClangdUnit.cpp
  clang-tools-extra/trunk/clangd/unittests/ClangdUnitTests.cpp


Index: clang-tools-extra/trunk/clangd/ClangdUnit.cpp
===
--- clang-tools-extra/trunk/clangd/ClangdUnit.cpp
+++ clang-tools-extra/trunk/clangd/ClangdUnit.cpp
@@ -67,7 +67,8 @@
 
   bool HandleTopLevelDecl(DeclGroupRef DG) override {
 for (Decl *D : DG) {
-  if (D->isFromASTFile())
+  auto  = D->getASTContext().getSourceManager();
+  if (!SM.isWrittenInMainFile(SM.getExpansionLoc(D->getLocation(
 continue;
 
   // ObjCMethodDecl are not actually top-level decls.
Index: clang-tools-extra/trunk/clangd/unittests/ClangdUnitTests.cpp
===
--- clang-tools-extra/trunk/clangd/unittests/ClangdUnitTests.cpp
+++ clang-tools-extra/trunk/clangd/unittests/ClangdUnitTests.cpp
@@ -83,6 +83,26 @@
   EXPECT_THAT(AST.getLocalTopLevelDecls(), ElementsAre(DeclNamed("main")));
 }
 
+TEST(ClangdUnitTest, DoesNotGetIncludedTopDecls) {
+  TestTU TU;
+  TU.HeaderCode = R"cpp(
+#define LL void foo(){}
+template
+struct H {
+  H() {}
+  LL
+};
+  )cpp";
+  TU.Code = R"cpp(
+int main() {
+  H h;
+  h.foo();
+}
+  )cpp";
+  auto AST = TU.build();
+  EXPECT_THAT(AST.getLocalTopLevelDecls(), ElementsAre(DeclNamed("main")));
+}
+
 TEST(ClangdUnitTest, TokensAfterPreamble) {
   TestTU TU;
   TU.AdditionalFiles["foo.h"] = R"(


Index: clang-tools-extra/trunk/clangd/ClangdUnit.cpp
===
--- clang-tools-extra/trunk/clangd/ClangdUnit.cpp
+++ clang-tools-extra/trunk/clangd/ClangdUnit.cpp
@@ -67,7 +67,8 @@
 
   bool HandleTopLevelDecl(DeclGroupRef DG) override {
 for (Decl *D : DG) {
-  if (D->isFromASTFile())
+  auto  = D->getASTContext().getSourceManager();
+  if (!SM.isWrittenInMainFile(SM.getExpansionLoc(D->getLocation(
 continue;
 
   // ObjCMethodDecl are not actually top-level decls.
Index: clang-tools-extra/trunk/clangd/unittests/ClangdUnitTests.cpp
===
--- clang-tools-extra/trunk/clangd/unittests/ClangdUnitTests.cpp
+++ clang-tools-extra/trunk/clangd/unittests/ClangdUnitTests.cpp
@@ -83,6 +83,26 @@
   EXPECT_THAT(AST.getLocalTopLevelDecls(), ElementsAre(DeclNamed("main")));
 }
 
+TEST(ClangdUnitTest, DoesNotGetIncludedTopDecls) {
+  TestTU TU;
+  TU.HeaderCode = R"cpp(
+#define LL void foo(){}
+template
+struct H {
+  H() {}
+  LL
+};
+  )cpp";
+  TU.Code = R"cpp(
+int main() {
+  H h;
+  h.foo();
+}
+  )cpp";
+  auto AST = TU.build();
+  EXPECT_THAT(AST.getLocalTopLevelDecls(), ElementsAre(DeclNamed("main")));
+}
+
 TEST(ClangdUnitTest, TokensAfterPreamble) {
   TestTU TU;
   TU.AdditionalFiles["foo.h"] = R"(
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D63919: [clangd] Emit publishSemanticHighlighting in LSP if enabled

2019-07-01 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 207241.
jvikstrom marked 13 inline comments as done.
jvikstrom added a comment.

Addressed comments.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D63919

Files:
  clang-tools-extra/clangd/ClangdLSPServer.cpp
  clang-tools-extra/clangd/ClangdLSPServer.h
  clang-tools-extra/clangd/ClangdServer.cpp
  clang-tools-extra/clangd/Protocol.cpp
  clang-tools-extra/clangd/Protocol.h
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/SemanticHighlighting.h
  clang-tools-extra/clangd/test/initialize-params.test
  clang-tools-extra/clangd/test/semantic-highlighting.test
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -8,6 +8,7 @@
 
 #include "Annotations.h"
 #include "ClangdServer.h"
+#include "Protocol.h"
 #include "SemanticHighlighting.h"
 #include "TestFS.h"
 #include "TestTU.h"
@@ -66,7 +67,7 @@
   }
 }
 
-TEST(ClangdSemanticHighlightingTest, GeneratesHighlightsWhenFileChange) {
+TEST(SemanticHighlighting, GeneratesHighlightsWhenFileChange) {
   class HighlightingsCounterDiagConsumer : public DiagnosticsConsumer {
   public:
 std::atomic Count = {0};
@@ -90,6 +91,29 @@
   ASSERT_EQ(DiagConsumer.Count, 1);
 }
 
+TEST(SemanticHighlighting, toSemanticHighlightingInformation) {
+  auto CreatePosition = [](int Line, int Character) -> Position {
+Position Pos;
+Pos.line = Line;
+Pos.character = Character;
+return Pos;
+  };
+
+  std::vector Tokens{
+  HighlightingToken{HighlightingKind::Variable,
+Range{CreatePosition(3, 8), CreatePosition(3, 12)}},
+  HighlightingToken{HighlightingKind::Function,
+Range{CreatePosition(3, 4), CreatePosition(3, 7)}},
+  HighlightingToken{HighlightingKind::Variable,
+Range{CreatePosition(1, 1), CreatePosition(1, 5)}}};
+  std::vector Info =
+  toSemanticHighlightingInformation(Tokens);
+  std::vector Correct = {
+  SemanticHighlightingInformation{1, "AQAEAAA="},
+  SemanticHighlightingInformation{3, "CAAEAAAEAAMAAQ=="}};
+  ASSERT_EQ(Info, Correct);
+}
+
 } // namespace
 } // namespace clangd
 } // namespace clang
Index: clang-tools-extra/clangd/test/semantic-highlighting.test
===
--- /dev/null
+++ clang-tools-extra/clangd/test/semantic-highlighting.test
@@ -0,0 +1,21 @@
+# RUN: clangd -lit-test < %s | FileCheck -strict-whitespace %s
+{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{"textDocument": {"semanticHighlightingCapabilities": {"semanticHighlighting": true}}},"trace":"off"}}
+---
+{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"uri":"test:///foo.cpp","languageId":"cpp","version":1,"text":"int x = 2;"}}}
+#  CHECK:  "method": "textDocument/semanticHighlighting",
+# CHECK-NEXT:  "params": {
+# CHECK-NEXT:"lines": [
+# CHECK-NEXT:  {
+# CHECK-NEXT:"line": 0,
+# CHECK-NEXT:"tokens": "BAABAAA="
+# CHECK-NEXT:  }
+# CHECK-NEXT:],
+# CHECK-NEXT:"textDocument": {
+# CHECK-NEXT:  "uri": "file:///clangd-test/foo.cpp"
+# CHECK-NEXT:}
+# CHECK-NEXT:  }
+# CHECK-NEXT:}
+---
+{"jsonrpc":"2.0","id":3,"method":"shutdown"}
+---
+{"jsonrpc":"2.0","method":"exit"}
Index: clang-tools-extra/clangd/test/initialize-params.test
===
--- clang-tools-extra/clangd/test/initialize-params.test
+++ clang-tools-extra/clangd/test/initialize-params.test
@@ -33,6 +33,16 @@
 # CHECK-NEXT:  "hoverProvider": true,
 # CHECK-NEXT:  "referencesProvider": true,
 # CHECK-NEXT:  "renameProvider": true,
+# CHECK-NEXT:  "semanticHighlighting": {
+# CHECK-NEXT:"scopes": [
+# CHECK-NEXT:  [
+# CHECK-NEXT:"variable.cpp"
+# CHECK-NEXT:  ],
+# CHECK-NEXT:  [
+# CHECK-NEXT:"entity.name.function.cpp"
+# CHECK-NEXT:  ]
+# CHECK-NEXT:]
+# CHECK-NEXT:  },
 # CHECK-NEXT:  "signatureHelpProvider": {
 # CHECK-NEXT:"triggerCharacters": [
 # CHECK-NEXT:  "(",
Index: clang-tools-extra/clangd/SemanticHighlighting.h
===
--- clang-tools-extra/clangd/SemanticHighlighting.h
+++ clang-tools-extra/clangd/SemanticHighlighting.h
@@ -4,19 +4,25 @@
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
+// An implementation of semantic highlighting based on this proposal:
+// 

[PATCH] D64257: [clangd] Added highlighting for non-builtin types

2019-07-08 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom added inline comments.



Comment at: clang-tools-extra/clangd/SemanticHighlighting.cpp:73
+if (auto RD = dyn_cast(D)) {
+  if (auto DC = RD->getDestructor()) {
+auto Range = DC->getSourceRange();

hokein wrote:
> Here is the case:
> 
> ```
> class Foo {
>~Foo
>  // ^~~ we get a TypeLoc whose TagDecl is a cxxRecordDecl.
> }
> ```
> not sure this is expected in clang AST, but it is unreasonable in 
> highlighting context -- ideally, we want to highlight `~Foo` as a destructor 
> (we may encounter a tricky case, like `~ /*comment*/ Foo()`, but I assume 
> this is rarce, should be fine), @sammccall, @ilya-biryukov, thoughts?
Do we want to highlight the entire "~Foo" or only the ~"Foo" for destructors?



Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64257



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D64310: [clangd] Added semantic highlighting for constructors and destructors.

2019-07-08 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom created this revision.
jvikstrom added reviewers: hokein, ilya-biryukov.
Herald added subscribers: cfe-commits, kadircet, arphaman, jkorous, MaskRay.
Herald added a project: clang.

Add semantic highlighting to constructor and destructor declarations.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D64310

Files:
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/SemanticHighlighting.h
  clang-tools-extra/clangd/test/semantic-highlighting.test
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -34,7 +34,9 @@
   auto AST = TestTU::withCode(Test.code()).build();
   static const std::map KindToString{
   {HighlightingKind::Variable, "Variable"},
-  {HighlightingKind::Function, "Function"}};
+  {HighlightingKind::Function, "Function"},
+  {HighlightingKind::Constructor, "Constructor"},
+  {HighlightingKind::Destructor, "Destructor"}};
   std::vector ExpectedTokens;
   for (const auto  : KindToString) {
 std::vector Toks =
@@ -71,11 +73,12 @@
 )cpp",
 R"cpp(
   struct A {
-A();
-~A();
+$Constructor[[A]]();
+$Destructor[[~A]]()  {}
 void $Function[[abc]]();
 void operator<<(int);
   };
+  struct B {};
 )cpp"};
   for (const auto  : TestCases) {
 checkHighlightings(TestCase);
Index: clang-tools-extra/clangd/test/semantic-highlighting.test
===
--- clang-tools-extra/clangd/test/semantic-highlighting.test
+++ clang-tools-extra/clangd/test/semantic-highlighting.test
@@ -10,6 +10,12 @@
 # CHECK-NEXT:  [
 # CHECK-NEXT:"entity.name.function.cpp"
 # CHECK-NEXT:  ]
+# CHECK-NEXT:  [
+# CHECK-NEXT:"entity.name.function.constructor.cpp"
+# CHECK-NEXT:  ],
+# CHECK-NEXT:  [
+# CHECK-NEXT:"entity.name.function.destructor.cpp"
+# CHECK-NEXT:  ]
 # CHECK-NEXT:]
 # CHECK-NEXT:  },
 ---
Index: clang-tools-extra/clangd/SemanticHighlighting.h
===
--- clang-tools-extra/clangd/SemanticHighlighting.h
+++ clang-tools-extra/clangd/SemanticHighlighting.h
@@ -26,6 +26,8 @@
 enum class HighlightingKind {
   Variable = 0,
   Function,
+  Constructor,
+  Destructor,
 
   NumKinds,
 };
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -36,7 +36,10 @@
 
   bool VisitNamedDecl(NamedDecl *ND) {
 // FIXME: (De)Constructors/operator need to be highlighted some other way.
-if (ND->getDeclName().getNameKind() != DeclarationName::Identifier)
+auto Kind = ND->getDeclName().getNameKind();
+if (Kind != DeclarationName::Identifier &&
+Kind != DeclarationName::CXXConstructorName &&
+Kind != DeclarationName::CXXDestructorName)
   return true;
 
 if (ND->getDeclName().isEmpty())
@@ -58,6 +61,21 @@
 
 private:
   void addToken(SourceLocation Loc, const Decl *D) {
+if (Loc.isMacroID())
+  // FIXME: skip tokens inside macros for now.
+  return;
+
+if(isa(D)) {
+  addToken(Loc, HighlightingKind::Constructor);
+  return;
+}
+if (isa(D)) {
+  addToken(
+  Loc,
+  dyn_cast(D)->getDeclName().getAsString().size(),
+  HighlightingKind::Destructor);
+  return;
+}
 if (isa(D)) {
   addToken(Loc, HighlightingKind::Variable);
   return;
@@ -69,10 +87,6 @@
   }
 
   void addToken(SourceLocation Loc, HighlightingKind Kind) {
-if (Loc.isMacroID())
-  // FIXME: skip tokens inside macros for now.
-  return;
-
 auto R = getTokenRange(SM, Ctx.getLangOpts(), Loc);
 if (!R) {
   // R should always have a value, if it doesn't something is very wrong.
@@ -82,6 +96,13 @@
 
 Tokens.push_back({Kind, R.getValue()});
   }
+  void addToken(SourceLocation Loc, int Len, HighlightingKind Kind) {
+auto StartLoc = sourceLocToPosition(SM, Loc);
+Position EndLoc;
+EndLoc.line = StartLoc.line;
+EndLoc.character = StartLoc.character + Len;
+Tokens.push_back({Kind, {StartLoc, EndLoc}});
+  }
 };
 
 // Encode binary data into base64.
@@ -176,6 +197,10 @@
 return "entity.name.function.cpp";
   case HighlightingKind::Variable:
 return "variable.cpp";
+  case HighlightingKind::Constructor:
+return "entity.name.function.constructor.cpp";
+  case HighlightingKind::Destructor:
+return "entity.name.function.destructor.cpp";
   case HighlightingKind::NumKinds:
 

[PATCH] D64257: [clangd] Added highlighting for non-builtin types

2019-07-08 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 208416.
jvikstrom added a comment.

Clang formatted.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64257

Files:
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/SemanticHighlighting.h
  clang-tools-extra/clangd/test/semantic-highlighting.test
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -34,11 +34,13 @@
   auto AST = TestTU::withCode(Test.code()).build();
   static const std::map KindToString{
   {HighlightingKind::Variable, "Variable"},
-  {HighlightingKind::Function, "Function"}};
+  {HighlightingKind::Function, "Function"},
+  {HighlightingKind::Class, "Class"},
+  {HighlightingKind::Enum, "Enum"}};
   std::vector ExpectedTokens;
   for (const auto  : KindToString) {
-std::vector Toks =
-makeHighlightingTokens(Test.ranges(KindString.second), KindString.first);
+std::vector Toks = makeHighlightingTokens(
+Test.ranges(KindString.second), KindString.first);
 ExpectedTokens.insert(ExpectedTokens.end(), Toks.begin(), Toks.end());
   }
 
@@ -49,14 +51,14 @@
 TEST(SemanticHighlighting, GetsCorrectTokens) {
   const char *TestCases[] = {
 R"cpp(
-  struct AS {
+  struct $Class[[AS]] {
 double SomeMember;
   };
-  struct {
+  $Class[[struct]] {
   } $Variable[[S]];
-  void $Function[[foo]](int $Variable[[A]]) {
+  void $Function[[foo]](int $Variable[[A]], $Class[[AS]] $Variable[[As]]) {
 auto $Variable[[VeryLongVariableName]] = 12312;
-AS $Variable[[AA]];
+$Class[[AS]] $Variable[[AA]];
 auto $Variable[[L]] = $Variable[[AA]].SomeMember + $Variable[[A]];
 auto $Variable[[FN]] = [ $Variable[[AA]]](int $Variable[[A]]) -> void {};
 $Variable[[FN]](12312);
@@ -70,12 +72,40 @@
   }
 )cpp",
 R"cpp(
-  struct A {
-A();
-~A();
+  namespace abc {
+template
+struct $Class[[A]] {
+  T t;
+};
+  }
+  template
+  struct $Class[[C]] : abc::A {
+typename T::A* D;
+  };
+  abc::$Class[[A]] $Variable[[AA]];
+  typedef abc::$Class[[A]] AAA;
+  enum class $Enum[[E]] {};
+  enum $Enum[[EE]] {};
+  struct $Class[[B]] {
+$Enum[[E]] EEE;
+$Enum[[EE]] ;
+$Class[[AAA]] AA;
+  };
+  void $Function[[f]] () {
+$Class[[B]] $Variable[[BB]] = $Class[[B]]();
+$Variable[[BB]].~$Class[[B]]();
+$Class[[B]]();
+  }
+)cpp",
+R"cpp(
+  struct $Class[[A]] {
+$Class[[A]]();
+~$Class[[A]]();
 void $Function[[abc]]();
-void operator<<(int);
+void operator<<($Class[[A]]);
   };
+  $Class[[A]]::$Class[[A]]() {}
+  $Class[[A]]::~$Class[[A]]() {}
 )cpp"};
   for (const auto  : TestCases) {
 checkHighlightings(TestCase);
Index: clang-tools-extra/clangd/test/semantic-highlighting.test
===
--- clang-tools-extra/clangd/test/semantic-highlighting.test
+++ clang-tools-extra/clangd/test/semantic-highlighting.test
@@ -10,6 +10,12 @@
 # CHECK-NEXT:  [
 # CHECK-NEXT:"entity.name.function.cpp"
 # CHECK-NEXT:  ]
+# CHECK-NEXT:  [
+# CHECK-NEXT:"entity.name.type.class.cpp"
+# CHECK-NEXT:  ],
+# CHECK-NEXT:  [
+# CHECK-NEXT:"entity.name.type.enum.cpp"
+# CHECK-NEXT:  ]
 # CHECK-NEXT:]
 # CHECK-NEXT:  },
 ---
Index: clang-tools-extra/clangd/SemanticHighlighting.h
===
--- clang-tools-extra/clangd/SemanticHighlighting.h
+++ clang-tools-extra/clangd/SemanticHighlighting.h
@@ -26,6 +26,8 @@
 enum class HighlightingKind {
   Variable = 0,
   Function,
+  Class,
+  Enum,
 
   NumKinds,
 };
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -35,7 +35,12 @@
   }
 
   bool VisitNamedDecl(NamedDecl *ND) {
-// FIXME: (De)Constructors/operator need to be highlighted some other way.
+if (ND->getDeclName().getNameKind() ==
+DeclarationName::CXXConstructorName) {
+  addToken(ND->getLocation(), ND);
+  return true;
+}
+
 if (ND->getDeclName().getNameKind() != DeclarationName::Identifier)
   return true;
 
@@ -56,8 +61,27 @@
 return true;
   }
 
+  bool VisitTypeLoc(TypeLoc ) {
+// This check is for not 

[PATCH] D64310: [clangd] Added semantic highlighting for constructors and destructors.

2019-07-08 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom abandoned this revision.
jvikstrom added a comment.

Abandoning after discussion with sammccall and hokein.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64310



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D64257: [clangd] Added highlighting for non-builtin types

2019-07-08 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 208412.
jvikstrom marked 5 inline comments as done.
jvikstrom added a comment.

Addressed comments.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64257

Files:
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/SemanticHighlighting.h
  clang-tools-extra/clangd/test/semantic-highlighting.test
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -34,11 +34,13 @@
   auto AST = TestTU::withCode(Test.code()).build();
   static const std::map KindToString{
   {HighlightingKind::Variable, "Variable"},
-  {HighlightingKind::Function, "Function"}};
+  {HighlightingKind::Function, "Function"},
+  {HighlightingKind::Class, "Class"},
+  {HighlightingKind::Enum, "Enum"}};
   std::vector ExpectedTokens;
   for (const auto  : KindToString) {
-std::vector Toks =
-makeHighlightingTokens(Test.ranges(KindString.second), KindString.first);
+std::vector Toks = makeHighlightingTokens(
+Test.ranges(KindString.second), KindString.first);
 ExpectedTokens.insert(ExpectedTokens.end(), Toks.begin(), Toks.end());
   }
 
@@ -49,14 +51,14 @@
 TEST(SemanticHighlighting, GetsCorrectTokens) {
   const char *TestCases[] = {
 R"cpp(
-  struct AS {
+  struct $Class[[AS]] {
 double SomeMember;
   };
-  struct {
+  $Class[[struct]] {
   } $Variable[[S]];
-  void $Function[[foo]](int $Variable[[A]]) {
+  void $Function[[foo]](int $Variable[[A]], $Class[[AS]] $Variable[[As]]) {
 auto $Variable[[VeryLongVariableName]] = 12312;
-AS $Variable[[AA]];
+$Class[[AS]] $Variable[[AA]];
 auto $Variable[[L]] = $Variable[[AA]].SomeMember + $Variable[[A]];
 auto $Variable[[FN]] = [ $Variable[[AA]]](int $Variable[[A]]) -> void {};
 $Variable[[FN]](12312);
@@ -70,12 +72,40 @@
   }
 )cpp",
 R"cpp(
-  struct A {
-A();
-~A();
+  namespace abc {
+template
+struct $Class[[A]] {
+  T t;
+};
+  }
+  template
+  struct $Class[[C]] : abc::A {
+typename T::A* D;
+  };
+  abc::$Class[[A]] $Variable[[AA]];
+  typedef abc::$Class[[A]] AAA;
+  enum class $Enum[[E]] {};
+  enum $Enum[[EE]] {};
+  struct $Class[[B]] {
+$Enum[[E]] EEE;
+$Enum[[EE]] ;
+$Class[[AAA]] AA;
+  };
+  void $Function[[f]] () {
+$Class[[B]] $Variable[[BB]] = $Class[[B]]();
+$Variable[[BB]].~$Class[[B]]();
+$Class[[B]]();
+  }
+)cpp",
+R"cpp(
+  struct $Class[[A]] {
+$Class[[A]]();
+~$Class[[A]]();
 void $Function[[abc]]();
-void operator<<(int);
+void operator<<($Class[[A]]);
   };
+  $Class[[A]]::$Class[[A]]() {}
+  $Class[[A]]::~$Class[[A]]() {}
 )cpp"};
   for (const auto  : TestCases) {
 checkHighlightings(TestCase);
Index: clang-tools-extra/clangd/test/semantic-highlighting.test
===
--- clang-tools-extra/clangd/test/semantic-highlighting.test
+++ clang-tools-extra/clangd/test/semantic-highlighting.test
@@ -10,6 +10,12 @@
 # CHECK-NEXT:  [
 # CHECK-NEXT:"entity.name.function.cpp"
 # CHECK-NEXT:  ]
+# CHECK-NEXT:  [
+# CHECK-NEXT:"entity.name.type.class.cpp"
+# CHECK-NEXT:  ],
+# CHECK-NEXT:  [
+# CHECK-NEXT:"entity.name.type.enum.cpp"
+# CHECK-NEXT:  ]
 # CHECK-NEXT:]
 # CHECK-NEXT:  },
 ---
Index: clang-tools-extra/clangd/SemanticHighlighting.h
===
--- clang-tools-extra/clangd/SemanticHighlighting.h
+++ clang-tools-extra/clangd/SemanticHighlighting.h
@@ -26,6 +26,8 @@
 enum class HighlightingKind {
   Variable = 0,
   Function,
+  Class,
+  Enum,
 
   NumKinds,
 };
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -35,7 +35,11 @@
   }
 
   bool VisitNamedDecl(NamedDecl *ND) {
-// FIXME: (De)Constructors/operator need to be highlighted some other way.
+if(ND->getDeclName().getNameKind() == DeclarationName::CXXConstructorName) {
+  addToken(ND->getLocation(), ND);
+  return true;
+}
+
 if (ND->getDeclName().getNameKind() != DeclarationName::Identifier)
   return true;
 
@@ -56,8 +60,25 @@
 return true;
   }
 
+  bool 

[PATCH] D64257: [clangd] Added highlighting for non-builtin types

2019-07-08 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 208354.
jvikstrom marked an inline comment as done.
jvikstrom added a comment.

Type -> Class
Also added more tests.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64257

Files:
  clang-tools-extra/clangd/SemanticHighlighting.cpp
  clang-tools-extra/clangd/SemanticHighlighting.h
  clang-tools-extra/clangd/test/semantic-highlighting.test
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -34,11 +34,12 @@
   auto AST = TestTU::withCode(Test.code()).build();
   static const std::map KindToString{
   {HighlightingKind::Variable, "Variable"},
-  {HighlightingKind::Function, "Function"}};
+  {HighlightingKind::Function, "Function"},
+  {HighlightingKind::Class, "Class"}};
   std::vector ExpectedTokens;
   for (const auto  : KindToString) {
-std::vector Toks =
-makeHighlightingTokens(Test.ranges(KindString.second), KindString.first);
+std::vector Toks = makeHighlightingTokens(
+Test.ranges(KindString.second), KindString.first);
 ExpectedTokens.insert(ExpectedTokens.end(), Toks.begin(), Toks.end());
   }
 
@@ -49,14 +50,14 @@
 TEST(SemanticHighlighting, GetsCorrectTokens) {
   const char *TestCases[] = {
 R"cpp(
-  struct AS {
+  struct $Class[[AS]] {
 double SomeMember;
   };
-  struct {
+  $Class[[struct]] {
   } $Variable[[S]];
-  void $Function[[foo]](int $Variable[[A]]) {
+  void $Function[[foo]](int $Variable[[A]], $Class[[AS]] $Variable[[As]]) {
 auto $Variable[[VeryLongVariableName]] = 12312;
-AS $Variable[[AA]];
+$Class[[AS]] $Variable[[AA]];
 auto $Variable[[L]] = $Variable[[AA]].SomeMember + $Variable[[A]];
 auto $Variable[[FN]] = [ $Variable[[AA]]](int $Variable[[A]]) -> void {};
 $Variable[[FN]](12312);
@@ -70,11 +71,28 @@
   }
 )cpp",
 R"cpp(
-  struct A {
+  namespace abc {
+template
+struct $Class[[A]] {
+  T t;
+};
+  }
+  abc::$Class[[A]] $Variable[[AA]];
+  typedef abc::$Class[[A]] AAA;
+  enum class E {};
+  enum EE {};
+  struct $Class[[B]] {
+E EEE;
+EE ;
+$Class[[AAA]] AA;
+  };
+)cpp",
+R"cpp(
+  struct $Class[[A]] {
 A();
 ~A();
 void $Function[[abc]]();
-void operator<<(int);
+void operator<<($Class[[A]]);
   };
 )cpp"};
   for (const auto  : TestCases) {
Index: clang-tools-extra/clangd/test/semantic-highlighting.test
===
--- clang-tools-extra/clangd/test/semantic-highlighting.test
+++ clang-tools-extra/clangd/test/semantic-highlighting.test
@@ -10,6 +10,9 @@
 # CHECK-NEXT:  [
 # CHECK-NEXT:"entity.name.function.cpp"
 # CHECK-NEXT:  ]
+# CHECK-NEXT:  [
+# CHECK-NEXT:"entity.name.type.class.cpp"
+# CHECK-NEXT:  ]
 # CHECK-NEXT:]
 # CHECK-NEXT:  },
 ---
Index: clang-tools-extra/clangd/SemanticHighlighting.h
===
--- clang-tools-extra/clangd/SemanticHighlighting.h
+++ clang-tools-extra/clangd/SemanticHighlighting.h
@@ -26,6 +26,7 @@
 enum class HighlightingKind {
   Variable = 0,
   Function,
+  Class,
 
   NumKinds,
 };
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -12,6 +12,7 @@
 #include "SourceCode.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/RecursiveASTVisitor.h"
+#include "clang/AST/TypeLoc.h"
 
 namespace clang {
 namespace clangd {
@@ -56,6 +57,36 @@
 return true;
   }
 
+  bool VisitTypeLoc(TypeLoc ) {
+// The check for DependentName is so namespace qualifiers are not
+// highlighted. The check for elaborated type is for not getting two entries
+// whenever there is an anonymous struct.
+if (TL.getTypeLocClass() == TypeLoc::TypeLocClass::DependentName ||
+TL.getTypeLocClass() == TypeLoc::TypeLocClass::Elaborated) {
+  return true;
+}
+
+TagDecl *D = TL.getTypePtr()->getAsTagDecl();
+if (!D)
+  return true;
+
+if (const auto *RD = dyn_cast(D)) {
+  if (const auto *DC = RD->getDestructor()) {
+auto Range = DC->getSourceRange();
+// Destructors appear as a TagTypeLoc RecordDecl. To not highlight
+// destructors incorrectly the TagTypeLoc is skipped if it is wrapped
+// inside the 

[PATCH] D64257: [clangd] Added highlighting for non-builtin types

2019-07-08 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom marked 4 inline comments as done.
jvikstrom added inline comments.



Comment at: clang-tools-extra/clangd/SemanticHighlighting.cpp:68
+
+TagDecl *D = TL.getTypePtr()->getAsTagDecl();
+if (!D)

hokein wrote:
> We are only interested in `TagDecl`, maybe use the `VisitTagLoc` callback, so 
> that you can get rid of the filtering code above.
With just VisitTagLoc it does not catch this case: 
```
namespace abc {
  template
  struct $Type[[A]] {};
}
abc::$Type[[A]] $Variable[[AA]];```

I guess I could add a bunch of ```Visit*TypeLoc``` methods but I can't seem to 
find the correct Visit method for the case above... 


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64257



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


  1   2   3   4   >