diff options
| author | Johan Vikstrom <jvikstrom@google.com> | 2019-08-12 07:45:12 +0000 |
|---|---|---|
| committer | Johan Vikstrom <jvikstrom@google.com> | 2019-08-12 07:45:12 +0000 |
| commit | fd5ea1b0d904dc8f9793fcdf7699d10009f6a45f (patch) | |
| tree | 500e81d5ab468a1fd29948c9c271ea311362d85e | |
| parent | 4b9d20008bbcc3cc4a36dca73e7ccec16779abd6 (diff) | |
| download | bcm5719-llvm-fd5ea1b0d904dc8f9793fcdf7699d10009f6a45f.tar.gz bcm5719-llvm-fd5ea1b0d904dc8f9793fcdf7699d10009f6a45f.zip | |
[clangd] Highlighting auto variables as the deduced type.
Summary:
Done in VisitDeclaratorDecl as the AutoTypeLoc is not deduced.
Scoped to only work for variables.
auto function return values need to be handled in a special way (separate patch for that).
auto's that are in lambdas ar enot highlighted as we don't highlight their underlying type (it's a RecordDecl, but the name is not an identifier so it returns in addToken).
Reviewers: hokein, ilya-biryukov
Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D65996
llvm-svn: 368546
| -rw-r--r-- | clang-tools-extra/clangd/SemanticHighlighting.cpp | 38 | ||||
| -rw-r--r-- | clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp | 18 |
2 files changed, 40 insertions, 16 deletions
diff --git a/clang-tools-extra/clangd/SemanticHighlighting.cpp b/clang-tools-extra/clangd/SemanticHighlighting.cpp index fac9ff7839a..404cdb1e5dd 100644 --- a/clang-tools-extra/clangd/SemanticHighlighting.cpp +++ b/clang-tools-extra/clangd/SemanticHighlighting.cpp @@ -97,8 +97,8 @@ public: } bool VisitTypedefNameDecl(TypedefNameDecl *TD) { - if(const auto *TSI = TD->getTypeSourceInfo()) - addTypeLoc(TD->getLocation(), TSI->getTypeLoc()); + if (const auto *TSI = TD->getTypeSourceInfo()) + addType(TD->getLocation(), TSI->getTypeLoc().getTypePtr()); return true; } @@ -120,10 +120,8 @@ public: // structs. It also makes us not highlight certain namespace qualifiers // twice. For elaborated types the actual type is highlighted as an inner // TypeLoc. - if (TL.getTypeLocClass() == TypeLoc::TypeLocClass::Elaborated) - return true; - - addTypeLoc(TL.getBeginLoc(), TL); + if (TL.getTypeLocClass() != TypeLoc::TypeLocClass::Elaborated) + addType(TL.getBeginLoc(), TL.getTypePtr()); return true; } @@ -144,15 +142,27 @@ public: HighlightingTokenCollector>::TraverseConstructorInitializer(CI); } -private: - void addTypeLoc(SourceLocation Loc, const TypeLoc &TL) { - 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); + bool VisitDeclaratorDecl(DeclaratorDecl *D) { + if ((!D->getTypeSourceInfo())) + return true; + + if (auto *AT = D->getType()->getContainedAutoType()) { + const auto Deduced = AT->getDeducedType(); + if (!Deduced.isNull()) + addType(D->getTypeSpecStartLoc(), Deduced.getTypePtr()); } + return true; + } + +private: + void addType(SourceLocation Loc, const Type *TP) { + if (!TP) + return; + if (TP->isBuiltinType()) + // Builtins must be special cased as they do not have a TagDecl. + addToken(Loc, HighlightingKind::Primitive); + if (const TagDecl *TD = TP->getAsTagDecl()) + addToken(Loc, TD); } void addToken(SourceLocation Loc, const NamedDecl *D) { diff --git a/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp b/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp index 76a453044c9..4bc2f37ca6f 100644 --- a/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp +++ b/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp @@ -99,9 +99,9 @@ TEST(SemanticHighlighting, GetsCorrectTokens) { struct { } $Variable[[S]]; $Primitive[[void]] $Function[[foo]]($Primitive[[int]] $Variable[[A]], $Class[[AS]] $Variable[[As]]) { - auto $Variable[[VeryLongVariableName]] = 12312; + $Primitive[[auto]] $Variable[[VeryLongVariableName]] = 12312; $Class[[AS]] $Variable[[AA]]; - auto $Variable[[L]] = $Variable[[AA]].$Field[[SomeMember]] + $Variable[[A]]; + $Primitive[[auto]] $Variable[[L]] = $Variable[[AA]].$Field[[SomeMember]] + $Variable[[A]]; auto $Variable[[FN]] = [ $Variable[[AA]]]($Primitive[[int]] $Variable[[A]]) -> $Primitive[[void]] {}; $Variable[[FN]](12312); } @@ -303,6 +303,20 @@ TEST(SemanticHighlighting, GetsCorrectTokens) { class $Class[[Bar2]] : public $Class[[Bar]] { $Class[[Bar2]]() : $Class[[Bar]]($Class[[Foo]](), $EnumConstant[[EC]]) {} }; + )cpp", + R"cpp( + enum $Enum[[E]] { + $EnumConstant[[E]], + }; + class $Class[[Foo]] {}; + $Enum[[auto]] $Variable[[AE]] = $Enum[[E]]::$EnumConstant[[E]]; + $Class[[auto]] $Variable[[AF]] = $Class[[Foo]](); + $Class[[decltype]](auto) $Variable[[AF2]] = $Class[[Foo]](); + $Class[[auto]] *$Variable[[AFP]] = &$Variable[[AF]]; + $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]]{}; )cpp"}; for (const auto &TestCase : TestCases) { checkHighlightings(TestCase); |

