[clangd] Mark "override" and "final" as modifiers
authorChristian Kandeler <christian.kandeler@qt.io>
Mon, 21 Nov 2022 21:00:23 +0000 (22:00 +0100)
committerChristian Kandeler <christian.kandeler@qt.io>
Mon, 21 Nov 2022 21:01:12 +0000 (22:01 +0100)
... in semantic highlighting.
These specifiers cannot be identified by simple lexing (since e.g.
variables with these names can legally be declared), which means they
should be semantic tokens.

Reviewed By: sammccall

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

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

index dd9392b..dd25a2f 100644 (file)
@@ -809,6 +809,18 @@ public:
     return true;
   }
 
+  bool VisitAttr(Attr *A) {
+    switch (A->getKind()) {
+    case attr::Override:
+    case attr::Final:
+      H.addToken(A->getLocation(), HighlightingKind::Modifier);
+      break;
+    default:
+      break;
+    }
+    return true;
+  }
+
   bool VisitDependentNameTypeLoc(DependentNameTypeLoc L) {
     H.addToken(L.getNameLoc(), HighlightingKind::Type)
         .addModifier(HighlightingModifier::DependentName)
@@ -985,6 +997,8 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, HighlightingKind K) {
     return OS << "Primitive";
   case HighlightingKind::Macro:
     return OS << "Macro";
+  case HighlightingKind::Modifier:
+    return OS << "Modifier";
   case HighlightingKind::InactiveCode:
     return OS << "InactiveCode";
   }
@@ -1119,6 +1133,8 @@ llvm::StringRef toSemanticTokenType(HighlightingKind Kind) {
     return "type";
   case HighlightingKind::Macro:
     return "macro";
+  case HighlightingKind::Modifier:
+    return "modifier";
   case HighlightingKind::InactiveCode:
     return "comment";
   }
index 64ad431..e8f60c1 100644 (file)
@@ -49,6 +49,7 @@ enum class HighlightingKind {
   Concept,
   Primitive,
   Macro,
+  Modifier,
 
   // This one is different from the other kinds as it's a line style
   // rather than a token style.
index 3ea4a58..70d8f91 100644 (file)
@@ -864,6 +864,12 @@ sizeof...($TemplateParameter[[Elements]]);
             const char *$LocalVariable_def_readonly[[s]] = $LocalVariable_readonly_static[[__func__]];
         }
       )cpp",
+      // override and final
+      R"cpp(
+        class $Class_def_abstract[[Base]] { virtual void $Method_decl_abstract_virtual[[m]]() = 0; };
+        class $Class_def[[override]] : public $Class_abstract[[Base]] { void $Method_decl_virtual[[m]]() $Modifier[[override]]; };
+        class $Class_def[[final]] : public $Class[[override]] { void $Method_decl_virtual[[m]]() $Modifier[[override]] $Modifier[[final]]; };
+      )cpp",
       // Issue 1222: readonly modifier for generic parameter
       R"cpp(
         template <typename $TemplateParameter_def[[T]]>