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 @@ -49,7 +49,7 @@ return HighlightingKind::Typedef; } // We highlight class decls, constructor decls and destructor decls as - // `Class` type. The destructor decls are handled in `VisitTypeLoc` (we + // `Class` type. The destructor decls are handled in `VisitTagTypeLoc` (we // will visit a TypeLoc where the underlying Type is a CXXRecordDecl). if (auto *RD = llvm::dyn_cast(D)) { // We don't want to highlight lambdas like classes. @@ -214,25 +214,27 @@ return true; } - bool WalkUpFromTagTypeLoc(TagTypeLoc L) { + bool VisitTagTypeLoc(TagTypeLoc L) { if (L.isDefinition()) return true; // Definition will be highligthed by VisitNamedDecl. - return RecursiveASTVisitor::WalkUpFromTagTypeLoc(L); + if (auto K = kindForType(L.getTypePtr())) + addToken(L.getBeginLoc(), *K); + return true; } - bool WalkUpFromElaboratedTypeLoc(ElaboratedTypeLoc L) { - // Avoid highlighting 'struct' or 'enum' keywords. + bool VisitDecltypeTypeLoc(DecltypeTypeLoc L) { + if (auto K = kindForType(L.getTypePtr())) + addToken(L.getBeginLoc(), *K); return true; } - bool WalkUpFromDependentNameTypeLoc(DependentNameTypeLoc L) { + bool VisitDependentNameTypeLoc(DependentNameTypeLoc L) { addToken(L.getNameLoc(), HighlightingKind::DependentType); return true; } - bool VisitTypeLoc(TypeLoc TL) { - if (auto K = kindForType(TL.getTypePtr())) - addToken(TL.getBeginLoc(), *K); + bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { + addToken(TL.getBeginLoc(), HighlightingKind::TemplateParameter); return true; } 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 @@ -153,26 +153,26 @@ const char *TestCases[] = { R"cpp( struct $Class[[AS]] { - $Primitive[[double]] $Field[[SomeMember]]; + double $Field[[SomeMember]]; }; struct { } $Variable[[S]]; - $Primitive[[void]] $Function[[foo]]($Primitive[[int]] $Parameter[[A]], $Class[[AS]] $Parameter[[As]]) { + void $Function[[foo]](int $Parameter[[A]], $Class[[AS]] $Parameter[[As]]) { $Primitive[[auto]] $LocalVariable[[VeryLongVariableName]] = 12312; $Class[[AS]] $LocalVariable[[AA]]; $Primitive[[auto]] $LocalVariable[[L]] = $LocalVariable[[AA]].$Field[[SomeMember]] + $Parameter[[A]]; - auto $LocalVariable[[FN]] = [ $LocalVariable[[AA]]]($Primitive[[int]] $Parameter[[A]]) -> $Primitive[[void]] {}; + auto $LocalVariable[[FN]] = [ $LocalVariable[[AA]]](int $Parameter[[A]]) -> void {}; $LocalVariable[[FN]](12312); } )cpp", R"cpp( - $Primitive[[void]] $Function[[foo]]($Primitive[[int]]); - $Primitive[[void]] $Function[[Gah]](); - $Primitive[[void]] $Function[[foo]]() { + void $Function[[foo]](int); + void $Function[[Gah]](); + void $Function[[foo]]() { auto $LocalVariable[[Bou]] = $Function[[Gah]]; } struct $Class[[A]] { - $Primitive[[void]] $Method[[abc]](); + void $Method[[abc]](); }; )cpp", R"cpp( @@ -186,17 +186,17 @@ struct $Class[[C]] : $Namespace[[abc]]::$Class[[A]]<$TemplateParameter[[T]]> { typename $TemplateParameter[[T]]::$DependentType[[A]]* $Field[[D]]; }; - $Namespace[[abc]]::$Class[[A]]<$Primitive[[int]]> $Variable[[AA]]; - typedef $Namespace[[abc]]::$Class[[A]]<$Primitive[[int]]> $Class[[AAA]]; + $Namespace[[abc]]::$Class[[A]] $Variable[[AA]]; + typedef $Namespace[[abc]]::$Class[[A]] $Class[[AAA]]; struct $Class[[B]] { $Class[[B]](); ~$Class[[B]](); - $Primitive[[void]] operator<<($Class[[B]]); + void operator<<($Class[[B]]); $Class[[AAA]] $Field[[AA]]; }; $Class[[B]]::$Class[[B]]() {} $Class[[B]]::~$Class[[B]]() {} - $Primitive[[void]] $Function[[f]] () { + void $Function[[f]] () { $Class[[B]] $LocalVariable[[BB]] = $Class[[B]](); $LocalVariable[[BB]].~$Class[[B]](); $Class[[B]](); @@ -214,7 +214,7 @@ $Enum[[E]] $Field[[EEE]]; $Enum[[EE]] $Field[[EEEE]]; }; - $Primitive[[int]] $Variable[[I]] = $EnumConstant[[Hi]]; + int $Variable[[I]] = $EnumConstant[[Hi]]; $Enum[[E]] $Variable[[L]] = $Enum[[E]]::$EnumConstant[[B]]; )cpp", R"cpp( @@ -242,14 +242,14 @@ )cpp", R"cpp( struct $Class[[D]] { - $Primitive[[double]] $Field[[C]]; + double $Field[[C]]; }; struct $Class[[A]] { - $Primitive[[double]] $Field[[B]]; + double $Field[[B]]; $Class[[D]] $Field[[E]]; - static $Primitive[[double]] $StaticField[[S]]; - static $Primitive[[void]] $StaticMethod[[bar]]() {} - $Primitive[[void]] $Method[[foo]]() { + static double $StaticField[[S]]; + static void $StaticMethod[[bar]]() {} + void $Method[[foo]]() { $Field[[B]] = 123; this->$Field[[B]] = 156; this->$Method[[foo]](); @@ -258,7 +258,7 @@ $StaticField[[S]] = 90.1; } }; - $Primitive[[void]] $Function[[foo]]() { + void $Function[[foo]]() { $Class[[A]] $LocalVariable[[AA]]; $LocalVariable[[AA]].$Field[[B]] += 2; $LocalVariable[[AA]].$Method[[foo]](); @@ -268,15 +268,15 @@ )cpp", R"cpp( struct $Class[[AA]] { - $Primitive[[int]] $Field[[A]]; + int $Field[[A]]; } - $Primitive[[int]] $Variable[[B]]; + int $Variable[[B]]; $Class[[AA]] $Variable[[A]]{$Variable[[B]]}; )cpp", R"cpp( namespace $Namespace[[a]] { struct $Class[[A]] {}; - typedef $Primitive[[char]] $Primitive[[C]]; + typedef char $Primitive[[C]]; } typedef $Namespace[[a]]::$Class[[A]] $Class[[B]]; using $Class[[BB]] = $Namespace[[a]]::$Class[[A]]; @@ -287,10 +287,10 @@ $Enum[[CC]] $Function[[f]]($Class[[B]]); $Enum[[CD]] $Function[[f]]($Class[[BB]]); typedef $Namespace[[a]]::$Primitive[[C]] $Primitive[[PC]]; - typedef $Primitive[[float]] $Primitive[[F]]; + typedef float $Primitive[[F]]; )cpp", R"cpp( - template + template class $Class[[A]] { $TemplateParameter[[T]] $Field[[AA]]; $TemplateParameter[[T]] $Method[[foo]](); @@ -302,7 +302,7 @@ template class $Class[[BB]] {}; template - class $Class[[BB]]<$TemplateParameter[[T]], $Primitive[[int]]> {}; + class $Class[[BB]]<$TemplateParameter[[T]], int> {}; template class $Class[[BB]]<$TemplateParameter[[T]], $TemplateParameter[[T]]*> {}; @@ -313,13 +313,13 @@ class $Class[[Foo]] {}; template - $Primitive[[void]] $Function[[foo]]($TemplateParameter[[T]] ...); + void $Function[[foo]]($TemplateParameter[[T]] ...); )cpp", R"cpp( template struct $Class[[Tmpl]] {$TemplateParameter[[T]] $Field[[x]] = 0;}; - extern template struct $Class[[Tmpl]]<$Primitive[[float]]>; - template struct $Class[[Tmpl]]<$Primitive[[double]]>; + extern template struct $Class[[Tmpl]]; + template struct $Class[[Tmpl]]; )cpp", // This test is to guard against highlightings disappearing when using // conversion operators as their behaviour in the clang AST differ from @@ -328,14 +328,14 @@ class $Class[[Foo]] {}; struct $Class[[Bar]] { explicit operator $Class[[Foo]]*() const; - explicit operator $Primitive[[int]]() const; + explicit operator int() const; operator $Class[[Foo]](); }; - $Primitive[[void]] $Function[[f]]() { + void $Function[[f]]() { $Class[[Bar]] $LocalVariable[[B]]; $Class[[Foo]] $LocalVariable[[F]] = $LocalVariable[[B]]; $Class[[Foo]] *$LocalVariable[[FP]] = ($Class[[Foo]]*)$LocalVariable[[B]]; - $Primitive[[int]] $LocalVariable[[I]] = ($Primitive[[int]])$LocalVariable[[B]]; + int $LocalVariable[[I]] = (int)$LocalVariable[[B]]; } )cpp" R"cpp( @@ -355,7 +355,7 @@ class $Class[[Bar]] { $Class[[Foo]] $Field[[Fo]]; $Enum[[En]] $Field[[E]]; - $Primitive[[int]] $Field[[I]]; + int $Field[[I]]; $Class[[Bar]] ($Class[[Foo]] $Parameter[[F]], $Enum[[En]] $Parameter[[E]]) : $Field[[Fo]] ($Parameter[[F]]), $Field[[E]] ($Parameter[[E]]), @@ -377,7 +377,7 @@ $Enum[[auto]] &$Variable[[AER]] = $Variable[[AE]]; $Primitive[[auto]] $Variable[[Form]] = 10.2 + 2 * 4; $Primitive[[decltype]]($Variable[[Form]]) $Variable[[F]] = 10; - auto $Variable[[Fun]] = []()->$Primitive[[void]]{}; + auto $Variable[[Fun]] = []()->void{}; )cpp", R"cpp( class $Class[[G]] {}; @@ -385,22 +385,22 @@ class $Class[[GP]] {}; template<$Class[[G]] &$TemplateParameter[[U]]> class $Class[[GR]] {}; - template<$Primitive[[int]] *$TemplateParameter[[U]]> + template class $Class[[IP]] { - $Primitive[[void]] $Method[[f]]() { + void $Method[[f]]() { *$TemplateParameter[[U]] += 5; } }; - template<$Primitive[[unsigned]] $TemplateParameter[[U]] = 2> + template class $Class[[Foo]] { - $Primitive[[void]] $Method[[f]]() { - for($Primitive[[int]] $LocalVariable[[I]] = 0; + void $Method[[f]]() { + for(int $LocalVariable[[I]] = 0; $LocalVariable[[I]] < $TemplateParameter[[U]];) {} } }; $Class[[G]] $Variable[[L]]; - $Primitive[[void]] $Function[[f]]() { + void $Function[[f]]() { $Class[[Foo]]<123> $LocalVariable[[F]]; $Class[[GP]]<&$Variable[[L]]> $LocalVariable[[LL]]; $Class[[GR]]<$Variable[[L]]> $LocalVariable[[LLL]]; @@ -408,24 +408,24 @@ )cpp", R"cpp( template + void (T::*$TemplateParameter[[method]])(int)> struct $Class[[G]] { - $Primitive[[void]] $Method[[foo]]( + void $Method[[foo]]( $TemplateParameter[[T]] *$Parameter[[O]]) { ($Parameter[[O]]->*$TemplateParameter[[method]])(10); } }; struct $Class[[F]] { - $Primitive[[void]] $Method[[f]]($Primitive[[int]]); + void $Method[[f]](int); }; - template<$Primitive[[void]] (*$TemplateParameter[[Func]])()> + template struct $Class[[A]] { - $Primitive[[void]] $Method[[f]]() { + void $Method[[f]]() { (*$TemplateParameter[[Func]])(); } }; - $Primitive[[void]] $Function[[foo]]() { + void $Function[[foo]]() { $Class[[F]] $LocalVariable[[FF]]; $Class[[G]]<$Class[[F]], &$Class[[F]]::$Method[[f]]> $LocalVariable[[GG]]; $LocalVariable[[GG]].$Method[[foo]](&$LocalVariable[[FF]]); @@ -449,21 +449,21 @@ #define $Macro[[SOME_NAME]] variable #define $Macro[[SOME_NAME_SET]] variable2 = 123 #define $Macro[[INC_VAR]](X) X += 2 - $Primitive[[void]] $Function[[foo]]() { + void $Function[[foo]]() { $Macro[[DEF_VAR]]($LocalVariable[[X]], 123); $Macro[[DEF_VAR_REV]](908, $LocalVariable[[XY]]); - $Primitive[[int]] $Macro[[CPY]]( $LocalVariable[[XX]] ); + int $Macro[[CPY]]( $LocalVariable[[XX]] ); $Macro[[DEF_VAR_TYPE]]($Class[[A]], $LocalVariable[[AA]]); - $Primitive[[double]] $Macro[[SOME_NAME]]; - $Primitive[[int]] $Macro[[SOME_NAME_SET]]; + double $Macro[[SOME_NAME]]; + int $Macro[[SOME_NAME_SET]]; $LocalVariable[[variable]] = 20.1; - $Macro[[MACRO_CONCAT]](var, 2, $Primitive[[float]]); + $Macro[[MACRO_CONCAT]](var, 2, float); $Macro[[DEF_VAR_T]]($Class[[A]], $Macro[[CPY]]( $Macro[[CPY]]($LocalVariable[[Nested]])), $Macro[[CPY]]($Class[[A]]())); $Macro[[INC_VAR]]($LocalVariable[[variable]]); } - $Primitive[[void]] $Macro[[SOME_NAME]](); + void $Macro[[SOME_NAME]](); $Macro[[DEF_VAR]]($Variable[[XYZ]], 567); $Macro[[DEF_VAR_REV]](756, $Variable[[AB]]); @@ -477,10 +477,10 @@ #define $Macro[[fail]](expr) expr #define $Macro[[assert]](COND) if (!(COND)) { fail("assertion failed" #COND); } // Preamble ends. - $Primitive[[int]] $Variable[[x]]; - $Primitive[[int]] $Variable[[y]]; - $Primitive[[int]] $Function[[f]](); - $Primitive[[void]] $Function[[foo]]() { + int $Variable[[x]]; + int $Variable[[y]]; + int $Function[[f]](); + void $Function[[foo]]() { $Macro[[assert]]($Variable[[x]] != $Variable[[y]]); $Macro[[assert]]($Variable[[x]] != $Function[[f]]()); } @@ -501,12 +501,12 @@ )cpp", R"cpp( struct $Class[[S]] { - $Primitive[[float]] $Field[[Value]]; + float $Field[[Value]]; $Class[[S]] *$Field[[Next]]; }; $Class[[S]] $Variable[[Global]][2] = {$Class[[S]](), $Class[[S]]()}; - $Primitive[[void]] $Function[[f]]($Class[[S]] $Parameter[[P]]) { - $Primitive[[int]] $LocalVariable[[A]][2] = {1,2}; + void $Function[[f]]($Class[[S]] $Parameter[[P]]) { + int $LocalVariable[[A]][2] = {1,2}; auto [$Variable[[B1]], $Variable[[B2]]] = $LocalVariable[[A]]; auto [$Variable[[G1]], $Variable[[G2]]] = $Variable[[Global]]; $Class[[auto]] [$Variable[[P1]], $Variable[[P2]]] = $Parameter[[P]]; @@ -519,25 +519,25 @@ class $Class[[A]] { using $TemplateParameter[[TemplateParam1]] = $TemplateParameter[[T]]; typedef $TemplateParameter[[T]] $TemplateParameter[[TemplateParam2]]; - using $Primitive[[IntType]] = $Primitive[[int]]; + using $Primitive[[IntType]] = int; using $Typedef[[Pointer]] = $TemplateParameter[[T]] *; using $Typedef[[LVReference]] = $TemplateParameter[[T]] &; using $Typedef[[RVReference]] = $TemplateParameter[[T]]&&; using $Typedef[[Array]] = $TemplateParameter[[T]]*[3]; - using $Typedef[[MemberPointer]] = $Primitive[[int]] (A::*)($Primitive[[int]]); + using $Typedef[[MemberPointer]] = int (A::*)(int); // Use various previously defined typedefs in a function type. - $Primitive[[void]] $Method[[func]]( + void $Method[[func]]( $Typedef[[Pointer]], $Typedef[[LVReference]], $Typedef[[RVReference]], $Typedef[[Array]], $Typedef[[MemberPointer]]); }; )cpp", R"cpp( template - $Primitive[[void]] $Function[[phase1]]($TemplateParameter[[T]]); + void $Function[[phase1]]($TemplateParameter[[T]]); template - $Primitive[[void]] $Function[[foo]]($TemplateParameter[[T]] $Parameter[[P]]) { + void $Function[[foo]]($TemplateParameter[[T]] $Parameter[[P]]) { $Function[[phase1]]($Parameter[[P]]); $DependentName[[phase2]]($Parameter[[P]]); } @@ -545,42 +545,42 @@ R"cpp( class $Class[[A]] { template - $Primitive[[void]] $Method[[bar]]($TemplateParameter[[T]]); + void $Method[[bar]]($TemplateParameter[[T]]); }; template - $Primitive[[void]] $Function[[foo]]($TemplateParameter[[U]] $Parameter[[P]]) { + void $Function[[foo]]($TemplateParameter[[U]] $Parameter[[P]]) { $Class[[A]]().$Method[[bar]]($Parameter[[P]]); } )cpp", R"cpp( struct $Class[[A]] { template - static $Primitive[[void]] $StaticMethod[[foo]]($TemplateParameter[[T]]); + static void $StaticMethod[[foo]]($TemplateParameter[[T]]); }; template struct $Class[[B]] { - $Primitive[[void]] $Method[[bar]]() { + void $Method[[bar]]() { $Class[[A]]::$StaticMethod[[foo]]($TemplateParameter[[T]]()); } }; )cpp", R"cpp( template - $Primitive[[void]] $Function[[foo]](typename $TemplateParameter[[T]]::$DependentType[[Type]] + void $Function[[foo]](typename $TemplateParameter[[T]]::$DependentType[[Type]] = $TemplateParameter[[T]]::$DependentName[[val]]); )cpp", R"cpp( template - $Primitive[[void]] $Function[[foo]]($TemplateParameter[[T]] $Parameter[[P]]) { + void $Function[[foo]]($TemplateParameter[[T]] $Parameter[[P]]) { $Parameter[[P]].$DependentName[[Field]]; } )cpp", R"cpp( template class $Class[[A]] { - $Primitive[[int]] $Method[[foo]]() { + int $Method[[foo]]() { return $TemplateParameter[[T]]::$DependentName[[Field]]; } }; 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 @@ -432,20 +432,16 @@ TEST_F(AnnotateHighlightingsTest, Test) { EXPECT_AVAILABLE("^vo^id^ ^f(^) {^}^"); // available everywhere. EXPECT_AVAILABLE("[[int a; int b;]]"); - EXPECT_EQ("/* storage.type.primitive.cpp */void " - "/* entity.name.function.cpp */f() {}", + EXPECT_EQ("void /* entity.name.function.cpp */f() {}", apply("void ^f() {}")); EXPECT_EQ(apply("[[void f1(); void f2();]]"), - "/* storage.type.primitive.cpp */void " - "/* entity.name.function.cpp */f1(); " - "/* storage.type.primitive.cpp */void " - "/* entity.name.function.cpp */f2();"); + "void /* entity.name.function.cpp */f1(); " + "void /* entity.name.function.cpp */f2();"); EXPECT_EQ(apply("void f1(); void f2() {^}"), "void f1(); " - "/* storage.type.primitive.cpp */void " - "/* entity.name.function.cpp */f2() {}"); + "void /* entity.name.function.cpp */f2() {}"); } TWEAK_TEST(ExpandMacro);