[PATCH] D65943: [clangd] Added semantic highlighting support for primitives.

2019-08-08 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 rL368291: [clangd] Added semantic highlighting support for 
primitives. (authored by jvikstrom, committed by ).
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D65943?vs=214121&id=214130#toc

Repository:
  rL LLVM

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

https://reviews.llvm.org/D65943

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
  clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp

Index: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp
===
--- clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp
@@ -138,9 +138,13 @@
 
 private:
   void addTypeLoc(SourceLocation Loc, const TypeLoc &TL) {
-if (const Type *TP = TL.getTypePtr())
+if (const Type *TP = TL.getTypePtr()) {
   if (const TagDecl *TD = TP->getAsTagDecl())
 addToken(Loc, TD);
+  if (TP->isBuiltinType())
+// Builtins must be special cased as they do not have a TagDecl.
+addToken(Loc, HighlightingKind::Primitive);
+}
   }
 
   void addToken(SourceLocation Loc, const NamedDecl *D) {
@@ -386,6 +390,8 @@
 return "entity.name.namespace.cpp";
   case HighlightingKind::TemplateParameter:
 return "entity.name.type.template.cpp";
+  case HighlightingKind::Primitive:
+return "storage.type.primitive.cpp";
   case HighlightingKind::NumKinds:
 llvm_unreachable("must not pass NumKinds to the function");
   }
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
@@ -39,7 +39,8 @@
   {HighlightingKind::EnumConstant, "EnumConstant"},
   {HighlightingKind::Field, "Field"},
   {HighlightingKind::Method, "Method"},
-  {HighlightingKind::TemplateParameter, "TemplateParameter"}};
+  {HighlightingKind::TemplateParameter, "TemplateParameter"},
+  {HighlightingKind::Primitive, "Primitive"}};
   std::vector ExpectedTokens;
   for (const auto &KindString : KindToString) {
 std::vector Toks = makeHighlightingTokens(
@@ -93,26 +94,26 @@
   const char *TestCases[] = {
 R"cpp(
   struct $Class[[AS]] {
-double $Field[[SomeMember]];
+$Primitive[[double]] $Field[[SomeMember]];
   };
   struct {
   } $Variable[[S]];
-  void $Function[[foo]](int $Variable[[A]], $Class[[AS]] $Variable[[As]]) {
+  $Primitive[[void]] $Function[[foo]]($Primitive[[int]] $Variable[[A]], $Class[[AS]] $Variable[[As]]) {
 auto $Variable[[VeryLongVariableName]] = 12312;
 $Class[[AS]] $Variable[[AA]];
 auto $Variable[[L]] = $Variable[[AA]].$Field[[SomeMember]] + $Variable[[A]];
-auto $Variable[[FN]] = [ $Variable[[AA]]](int $Variable[[A]]) -> void {};
+auto $Variable[[FN]] = [ $Variable[[AA]]]($Primitive[[int]] $Variable[[A]]) -> $Primitive[[void]] {};
 $Variable[[FN]](12312);
   }
 )cpp",
 R"cpp(
-  void $Function[[foo]](int);
-  void $Function[[Gah]]();
-  void $Function[[foo]]() {
+  $Primitive[[void]] $Function[[foo]]($Primitive[[int]]);
+  $Primitive[[void]] $Function[[Gah]]();
+  $Primitive[[void]] $Function[[foo]]() {
 auto $Variable[[Bou]] = $Function[[Gah]];
   }
   struct $Class[[A]] {
-void $Method[[abc]]();
+$Primitive[[void]] $Method[[abc]]();
   };
 )cpp",
 R"cpp(
@@ -126,17 +127,17 @@
   struct $Class[[C]] : $Namespace[[abc]]::$Class[[A]]<$TemplateParameter[[T]]> {
 typename $TemplateParameter[[T]]::A* $Field[[D]];
   };
-  $Namespace[[abc]]::$Class[[A]] $Variable[[AA]];
-  typedef $Namespace[[abc]]::$Class[[A]] $Class[[AAA]];
+  $Namespace[[abc]]::$Class[[A]]<$Primitive[[int]]> $Variable[[AA]];
+  typedef $Namespace[[abc]]::$Class[[A]]<$Primitive[[int]]> $Class[[AAA]];
   struct $Class[[B]] {
 $Class[[B]]();
 ~$Class[[B]]();
-void operator<<($Class[[B]]);
+$Primitive[[void]] operator<<($Class[[B]]);
 $Class[[AAA]] $Field[[AA]];
   };
   $Class[[B]]::$Class[[B]]() {}
   $Class[[B]]::~$Class[[B]]() {}
-  void $Function[[f]] () {
+  $Primitive[[void]] $Function[[f]] () {
 $Class[[B]] $Variable[[BB]] = $Class[[B]]();
 $Variable[[BB]].~$Class[[B]]();
 $Class[[B]]();
@@ -154,7 +155,7 @@
 $Enum[[E]] $Fi

[PATCH] D65943: [clangd] Added semantic highlighting support for primitives.

2019-08-08 Thread Haojian Wu via Phabricator via cfe-commits
hokein accepted this revision.
hokein added a comment.
This revision is now accepted and ready to land.

nice, just a nit.




Comment at: clang-tools-extra/clangd/SemanticHighlighting.cpp:138
+// Builtins must be special cased as they do not have a TagDecl.
+if (const auto *T = TL.getTypePtr())
+  if (T->isBuiltinType())

nit: we could merge this to the `if` below. 


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D65943



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


[PATCH] D65943: [clangd] Added semantic highlighting support for primitives.

2019-08-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.

Adds a new HighlightingKind "Primitive". Adds a special case for TypeLocs that 
have an underlying TypePtr that is are builtin types, adding them as primitives.
The primary reason for this change is because otherwise typedefs that typedef 
primitives `typedef int A` would not get highlighted (so in the example `A` 
would not get any highlightings.)


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D65943

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
  clang-tools-extra/clangd/unittests/TweakTests.cpp

Index: clang-tools-extra/clangd/unittests/TweakTests.cpp
===
--- clang-tools-extra/clangd/unittests/TweakTests.cpp
+++ clang-tools-extra/clangd/unittests/TweakTests.cpp
@@ -488,7 +488,7 @@
   checkAvailable(ID, "^vo^id^ ^f(^) {^}^"); // available everywhere.
   checkAvailable(ID, "[[int a; int b;]]");
   const char *Input = "void ^f() {}";
-  const char *Output = "void /* entity.name.function.cpp */f() {}";
+  const char *Output = "/* storage.type.primitive.cpp */void /* entity.name.function.cpp */f() {}";
   checkTransform(ID, Input, Output);
 
   checkTransform(ID,
@@ -497,8 +497,8 @@
 void f2();]]
 )cpp",
   R"cpp(
-void /* entity.name.function.cpp */f1();
-void /* entity.name.function.cpp */f2();
+/* storage.type.primitive.cpp */void /* entity.name.function.cpp */f1();
+/* storage.type.primitive.cpp */void /* entity.name.function.cpp */f2();
 )cpp");
 
checkTransform(ID,
@@ -509,7 +509,7 @@
 
   R"cpp(
 void f1();
-void /* entity.name.function.cpp */f2() {};
+/* storage.type.primitive.cpp */void /* entity.name.function.cpp */f2() {};
 )cpp");
 }
 
Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -39,7 +39,8 @@
   {HighlightingKind::EnumConstant, "EnumConstant"},
   {HighlightingKind::Field, "Field"},
   {HighlightingKind::Method, "Method"},
-  {HighlightingKind::TemplateParameter, "TemplateParameter"}};
+  {HighlightingKind::TemplateParameter, "TemplateParameter"},
+  {HighlightingKind::Primitive, "Primitive"}};
   std::vector ExpectedTokens;
   for (const auto &KindString : KindToString) {
 std::vector Toks = makeHighlightingTokens(
@@ -93,26 +94,26 @@
   const char *TestCases[] = {
 R"cpp(
   struct $Class[[AS]] {
-double $Field[[SomeMember]];
+$Primitive[[double]] $Field[[SomeMember]];
   };
   struct {
   } $Variable[[S]];
-  void $Function[[foo]](int $Variable[[A]], $Class[[AS]] $Variable[[As]]) {
+  $Primitive[[void]] $Function[[foo]]($Primitive[[int]] $Variable[[A]], $Class[[AS]] $Variable[[As]]) {
 auto $Variable[[VeryLongVariableName]] = 12312;
 $Class[[AS]] $Variable[[AA]];
 auto $Variable[[L]] = $Variable[[AA]].$Field[[SomeMember]] + $Variable[[A]];
-auto $Variable[[FN]] = [ $Variable[[AA]]](int $Variable[[A]]) -> void {};
+auto $Variable[[FN]] = [ $Variable[[AA]]]($Primitive[[int]] $Variable[[A]]) -> $Primitive[[void]] {};
 $Variable[[FN]](12312);
   }
 )cpp",
 R"cpp(
-  void $Function[[foo]](int);
-  void $Function[[Gah]]();
-  void $Function[[foo]]() {
+  $Primitive[[void]] $Function[[foo]]($Primitive[[int]]);
+  $Primitive[[void]] $Function[[Gah]]();
+  $Primitive[[void]] $Function[[foo]]() {
 auto $Variable[[Bou]] = $Function[[Gah]];
   }
   struct $Class[[A]] {
-void $Method[[abc]]();
+$Primitive[[void]] $Method[[abc]]();
   };
 )cpp",
 R"cpp(
@@ -126,17 +127,17 @@
   struct $Class[[C]] : $Namespace[[abc]]::$Class[[A]]<$TemplateParameter[[T]]> {
 typename $TemplateParameter[[T]]::A* $Field[[D]];
   };
-  $Namespace[[abc]]::$Class[[A]] $Variable[[AA]];
-  typedef $Namespace[[abc]]::$Class[[A]] $Class[[AAA]];
+  $Namespace[[abc]]::$Class[[A]]<$Primitive[[int]]> $Variable[[AA]];
+  typedef $Namespace[[abc]]::$Class[[A]]<$Primitive[[int]]> $Class[[AAA]];
   struct $Class[[B]] {
 $Class[[B]]();
 ~$Class[[B]]();
-void operator<<($Class[[B]]);
+$Primitive[[void]] operator<<($Class[[B]]);
 $Class[[AAA]] $Field[[AA]];
   };
   $Class[[B]]::$Class[[B]]() {}
   $Class[[B]]::~$Class[[B]]() {}
-  void $Function[[f]] () {
+  $Primitive[[void]] $Function[[f]] () {
 $Class[[B]] $Variable[[BB]] = $Class[[B]]();