jvikstrom created this revision. jvikstrom added reviewers: hokein, ilya-biryukov. Herald added subscribers: cfe-commits, kadircet, arphaman, jkorous, MaskRay, kristof.beyls, javed.absar. Herald added a project: clang.
Types dependant on templates did not get highlighted properly. This is because the TemplateTypeParmType does not contain a TagDecl and was special cased in the Visit* function, not in the addType function. Moved the spec ial casing to the addType function to solve this. addType is now also checking if a type is a pointer type and recursing to the underlying type if it is. Otherwise for example, `using D = T*;` would not get highlighted. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D66516 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 @@ -427,6 +427,14 @@ assert($Variable[[x]] != $Variable[[y]]); assert($Variable[[x]] != $Function[[f]]()); } + )cpp", + R"cpp( + template<class $TemplateParameter[[T]]> + class $Class[[A]] { + using $TemplateParameter[[D]] = $TemplateParameter[[T]]; + using $TemplateParameter[[DD]] = $TemplateParameter[[T]] *; + using $TemplateParameter[[DDD]] = $TemplateParameter[[T]] &; + }; )cpp"}; for (const auto &TestCase : TestCases) { checkHighlightings(TestCase); Index: clang-tools-extra/clangd/SemanticHighlighting.cpp =================================================================== --- clang-tools-extra/clangd/SemanticHighlighting.cpp +++ clang-tools-extra/clangd/SemanticHighlighting.cpp @@ -14,6 +14,7 @@ #include "clang/AST/Decl.h" #include "clang/AST/DeclCXX.h" #include "clang/AST/RecursiveASTVisitor.h" +#include "clang/AST/Type.h" #include <algorithm> namespace clang { @@ -123,12 +124,6 @@ return true; } - bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc &TL) { - // TemplateTypeParmTypeLoc does not have a TagDecl in its type ptr. - addToken(TL.getBeginLoc(), TL.getDecl()); - return true; - } - bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc &TL) { if (const TemplateDecl *TD = TL.getTypePtr()->getTemplateName().getAsTemplateDecl()) @@ -179,9 +174,17 @@ void addType(SourceLocation Loc, const Type *TP) { if (!TP) return; + if (TP->isPointerType() || TP->isLValueReferenceType()) + // When highlighting dependant template types the type can be a pointer or + // reference of a template type. To highlight that type we need to get to + // the underlying type. + addType(Loc, TP->getPointeeType().getTypePtr()); if (TP->isBuiltinType()) // Builtins must be special cased as they do not have a TagDecl. addToken(Loc, HighlightingKind::Primitive); + if (const TemplateTypeParmType *TD = dyn_cast<TemplateTypeParmType>(TP)) + // TemplateTypeParmType does not have a TagDecl. + addToken(Loc, TD->getDecl()); if (const TagDecl *TD = TP->getAsTagDecl()) addToken(Loc, TD); }
Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp +++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp @@ -427,6 +427,14 @@ assert($Variable[[x]] != $Variable[[y]]); assert($Variable[[x]] != $Function[[f]]()); } + )cpp", + R"cpp( + template<class $TemplateParameter[[T]]> + class $Class[[A]] { + using $TemplateParameter[[D]] = $TemplateParameter[[T]]; + using $TemplateParameter[[DD]] = $TemplateParameter[[T]] *; + using $TemplateParameter[[DDD]] = $TemplateParameter[[T]] &; + }; )cpp"}; for (const auto &TestCase : TestCases) { checkHighlightings(TestCase); Index: clang-tools-extra/clangd/SemanticHighlighting.cpp =================================================================== --- clang-tools-extra/clangd/SemanticHighlighting.cpp +++ clang-tools-extra/clangd/SemanticHighlighting.cpp @@ -14,6 +14,7 @@ #include "clang/AST/Decl.h" #include "clang/AST/DeclCXX.h" #include "clang/AST/RecursiveASTVisitor.h" +#include "clang/AST/Type.h" #include <algorithm> namespace clang { @@ -123,12 +124,6 @@ return true; } - bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc &TL) { - // TemplateTypeParmTypeLoc does not have a TagDecl in its type ptr. - addToken(TL.getBeginLoc(), TL.getDecl()); - return true; - } - bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc &TL) { if (const TemplateDecl *TD = TL.getTypePtr()->getTemplateName().getAsTemplateDecl()) @@ -179,9 +174,17 @@ void addType(SourceLocation Loc, const Type *TP) { if (!TP) return; + if (TP->isPointerType() || TP->isLValueReferenceType()) + // When highlighting dependant template types the type can be a pointer or + // reference of a template type. To highlight that type we need to get to + // the underlying type. + addType(Loc, TP->getPointeeType().getTypePtr()); if (TP->isBuiltinType()) // Builtins must be special cased as they do not have a TagDecl. addToken(Loc, HighlightingKind::Primitive); + if (const TemplateTypeParmType *TD = dyn_cast<TemplateTypeParmType>(TP)) + // TemplateTypeParmType does not have a TagDecl. + addToken(Loc, TD->getDecl()); if (const TagDecl *TD = TP->getAsTagDecl()) addToken(Loc, TD); }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits