diff --git a/clang-tools-extra/clangd/SemanticHighlighting.h b/clang-tools-extra/clangd/SemanticHighlighting.h --- a/clang-tools-extra/clangd/SemanticHighlighting.h +++ b/clang-tools-extra/clangd/SemanticHighlighting.h @@ -33,6 +33,7 @@ EnumConstant, Namespace, TemplateParameter, + Primitive, NumKinds, }; diff --git a/clang-tools-extra/clangd/SemanticHighlighting.cpp b/clang-tools-extra/clangd/SemanticHighlighting.cpp --- a/clang-tools-extra/clangd/SemanticHighlighting.cpp +++ b/clang-tools-extra/clangd/SemanticHighlighting.cpp @@ -134,6 +134,11 @@ private: void addTypeLoc(SourceLocation Loc, const TypeLoc &TL) { + // Builtins must be special cased as they do not have a TagDecl. + if (const auto *T = TL.getTypePtr()) + if (T->isBuiltinType()) + addToken(Loc, HighlightingKind::Primitive); + if (const Type *TP = TL.getTypePtr()) if (const TagDecl *TD = TP->getAsTagDecl()) addToken(Loc, TD); @@ -382,6 +387,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"); } diff --git a/clang-tools-extra/clangd/test/semantic-highlighting.test b/clang-tools-extra/clangd/test/semantic-highlighting.test --- a/clang-tools-extra/clangd/test/semantic-highlighting.test +++ b/clang-tools-extra/clangd/test/semantic-highlighting.test @@ -30,6 +30,9 @@ # CHECK-NEXT: ], # CHECK-NEXT: [ # CHECK-NEXT: "entity.name.type.template.cpp" +# CHECK-NEXT: ], +# CHECK-NEXT: [ +# CHECK-NEXT: "storage.type.primitive.cpp" # CHECK-NEXT: ] # CHECK-NEXT: ] # CHECK-NEXT: }, @@ -40,7 +43,7 @@ # CHECK-NEXT: "lines": [ # CHECK-NEXT: { # CHECK-NEXT: "line": 0, -# CHECK-NEXT: "tokens": "AAAABAABAAA=" +# CHECK-NEXT: "tokens": "AAAAAAADAAkAAAAEAAEAAA==" # CHECK-NEXT: } # CHECK-NEXT: ], # CHECK-NEXT: "textDocument": { @@ -55,11 +58,11 @@ # CHECK-NEXT: "lines": [ # CHECK-NEXT: { # CHECK-NEXT: "line": 0, -# CHECK-NEXT: "tokens": "AAAABAABAAA=" +# CHECK-NEXT: "tokens": "AAAAAAADAAkAAAAEAAEAAA==" # CHECK-NEXT: } # CHECK-NEXT: { # CHECK-NEXT: "line": 1, -# CHECK-NEXT: "tokens": "AAAABAABAAA=" +# CHECK-NEXT: "tokens": "AAAAAAADAAkAAAAEAAEAAA==" # CHECK-NEXT: } # CHECK-NEXT: ], # CHECK-NEXT: "textDocument": { @@ -74,7 +77,7 @@ # CHECK-NEXT: "lines": [ # CHECK-NEXT: { # CHECK-NEXT: "line": 1, -# CHECK-NEXT: "tokens": "AAAABAABAAA=" +# CHECK-NEXT: "tokens": "AAAAAAADAAkAAAAEAAEAAA==" # CHECK-NEXT: } # CHECK-NEXT: ], # CHECK-NEXT: "textDocument": { diff --git a/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp b/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp --- a/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp +++ b/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]](); $Variable[[BB]].~$Class[[B]](); $Class[[B]](); @@ -154,7 +155,7 @@ $Enum[[E]] $Field[[EEE]]; $Enum[[EE]] $Field[[EEEE]]; }; - int $Variable[[I]] = $EnumConstant[[Hi]]; + $Primitive[[int]] $Variable[[I]] = $EnumConstant[[Hi]]; $Enum[[E]] $Variable[[L]] = $Enum[[E]]::$EnumConstant[[B]]; )cpp", R"cpp( @@ -182,13 +183,13 @@ )cpp", R"cpp( struct $Class[[D]] { - double $Field[[C]]; + $Primitive[[double]] $Field[[C]]; }; struct $Class[[A]] { - double $Field[[B]]; + $Primitive[[double]] $Field[[B]]; $Class[[D]] $Field[[E]]; - static double $Variable[[S]]; - void $Method[[foo]]() { + static $Primitive[[double]] $Variable[[S]]; + $Primitive[[void]] $Method[[foo]]() { $Field[[B]] = 123; this->$Field[[B]] = 156; this->$Method[[foo]](); @@ -196,7 +197,7 @@ $Variable[[S]] = 90.1; } }; - void $Function[[foo]]() { + $Primitive[[void]] $Function[[foo]]() { $Class[[A]] $Variable[[AA]]; $Variable[[AA]].$Field[[B]] += 2; $Variable[[AA]].$Method[[foo]](); @@ -206,14 +207,15 @@ )cpp", R"cpp( struct $Class[[AA]] { - int $Field[[A]]; + $Primitive[[int]] $Field[[A]]; } - int $Variable[[B]]; + $Primitive[[int]] $Variable[[B]]; $Class[[AA]] $Variable[[A]]{$Variable[[B]]}; )cpp", R"cpp( namespace $Namespace[[a]] { struct $Class[[A]] {}; + typedef $Primitive[[char]] $Primitive[[C]]; } typedef $Namespace[[a]]::$Class[[A]] $Class[[B]]; using $Class[[BB]] = $Namespace[[a]]::$Class[[A]]; @@ -223,9 +225,11 @@ using $Enum[[CD]] = $Enum[[CC]]; $Enum[[CC]] $Function[[f]]($Class[[B]]); $Enum[[CD]] $Function[[f]]($Class[[BB]]); + typedef $Namespace[[a]]::$Primitive[[C]] $Primitive[[PC]]; + typedef $Primitive[[float]] $Primitive[[F]]; )cpp", R"cpp( - template + template class $Class[[A]] { $TemplateParameter[[T]] $Field[[AA]]; $TemplateParameter[[T]] $Method[[foo]](); @@ -237,7 +241,7 @@ template class $Class[[BB]] {}; template - class $Class[[BB]]<$TemplateParameter[[T]], int> {}; + class $Class[[BB]]<$TemplateParameter[[T]], $Primitive[[int]]> {}; template class $Class[[BB]]<$TemplateParameter[[T]], $TemplateParameter[[T]]*> {}; @@ -248,13 +252,13 @@ class $Class[[Foo]] {}; template - void $Function[[foo]]($TemplateParameter[[T]] ...); + $Primitive[[void]] $Function[[foo]]($TemplateParameter[[T]] ...); )cpp", R"cpp( template struct $Class[[Tmpl]] {$TemplateParameter[[T]] $Field[[x]] = 0;}; - extern template struct $Class[[Tmpl]]; - template struct $Class[[Tmpl]]; + extern template struct $Class[[Tmpl]]<$Primitive[[float]]>; + template struct $Class[[Tmpl]]<$Primitive[[double]]>; )cpp"}; for (const auto &TestCase : TestCases) { checkHighlightings(TestCase); diff --git a/clang-tools-extra/clangd/unittests/TweakTests.cpp b/clang-tools-extra/clangd/unittests/TweakTests.cpp --- a/clang-tools-extra/clangd/unittests/TweakTests.cpp +++ b/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"); }