kadircet created this revision. kadircet added a reviewer: sammccall. Herald added subscribers: usaxena95, arphaman. kadircet requested review of this revision. Herald added subscribers: cfe-commits, ilya-biryukov. Herald added projects: clang, clang-tools-extra.
Fixes https://github.com/clangd/clangd/issues/878. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D111260 Files: clang-tools-extra/clangd/unittests/XRefsTests.cpp clang/lib/Index/IndexDecl.cpp clang/unittests/Index/IndexTests.cpp Index: clang/unittests/Index/IndexTests.cpp =================================================================== --- clang/unittests/Index/IndexTests.cpp +++ clang/unittests/Index/IndexTests.cpp @@ -377,6 +377,21 @@ Not(HasRole(SymbolRole::RelationBaseOf))))); } +TEST(IndexTest, EnumBase) { + std::string Code = R"cpp( + typedef int MyTypedef; + enum Foo : MyTypedef; + enum Foo : MyTypedef {}; + )cpp"; + auto Index = std::make_shared<Indexer>(); + tooling::runToolOnCode(std::make_unique<IndexAction>(Index), Code); + EXPECT_THAT( + Index->Symbols, + AllOf(Contains(AllOf(QName("MyTypedef"), HasRole(SymbolRole::Reference), + WrittenAt(Position(3, 16)))), + Contains(AllOf(QName("MyTypedef"), HasRole(SymbolRole::Reference), + WrittenAt(Position(4, 16)))))); +} } // namespace } // namespace index } // namespace clang Index: clang/lib/Index/IndexDecl.cpp =================================================================== --- clang/lib/Index/IndexDecl.cpp +++ clang/lib/Index/IndexDecl.cpp @@ -8,6 +8,7 @@ #include "IndexingContext.h" #include "clang/AST/Attr.h" +#include "clang/AST/Decl.h" #include "clang/AST/DeclVisitor.h" #include "clang/Index/IndexDataConsumer.h" @@ -372,6 +373,15 @@ return true; } + bool VisitEnumDecl(const EnumDecl *ED) { + TRY_TO(VisitTagDecl(ED)); + // Indexing for enumdecl itself is handled inside TagDecl, we just want to + // visit integer-base here, which is different than other TagDecl bases. + if (auto *TSI = ED->getIntegerTypeSourceInfo()) + IndexCtx.indexTypeSourceInfo(TSI, ED, ED, /*isBase=*/true); + return true; + } + bool handleReferencedProtocols(const ObjCProtocolList &ProtList, const ObjCContainerDecl *ContD, SourceLocation SuperLoc) { Index: clang-tools-extra/clangd/unittests/XRefsTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/XRefsTests.cpp +++ clang-tools-extra/clangd/unittests/XRefsTests.cpp @@ -1966,6 +1966,20 @@ [[f^oo]](s); } )cpp", + + // Enum base + R"cpp( + typedef int $def[[MyTypeDef]]; + enum MyEnum : [[MyTy^peDef]] { }; + )cpp", + R"cpp( + typedef int $def[[MyType^Def]]; + enum MyEnum : [[MyTypeDef]]; + )cpp", + R"cpp( + using $def[[MyTypeDef]] = int; + enum MyEnum : [[MyTy^peDef]] { }; + )cpp", }; for (const char *Test : Tests) checkFindRefs(Test);
Index: clang/unittests/Index/IndexTests.cpp =================================================================== --- clang/unittests/Index/IndexTests.cpp +++ clang/unittests/Index/IndexTests.cpp @@ -377,6 +377,21 @@ Not(HasRole(SymbolRole::RelationBaseOf))))); } +TEST(IndexTest, EnumBase) { + std::string Code = R"cpp( + typedef int MyTypedef; + enum Foo : MyTypedef; + enum Foo : MyTypedef {}; + )cpp"; + auto Index = std::make_shared<Indexer>(); + tooling::runToolOnCode(std::make_unique<IndexAction>(Index), Code); + EXPECT_THAT( + Index->Symbols, + AllOf(Contains(AllOf(QName("MyTypedef"), HasRole(SymbolRole::Reference), + WrittenAt(Position(3, 16)))), + Contains(AllOf(QName("MyTypedef"), HasRole(SymbolRole::Reference), + WrittenAt(Position(4, 16)))))); +} } // namespace } // namespace index } // namespace clang Index: clang/lib/Index/IndexDecl.cpp =================================================================== --- clang/lib/Index/IndexDecl.cpp +++ clang/lib/Index/IndexDecl.cpp @@ -8,6 +8,7 @@ #include "IndexingContext.h" #include "clang/AST/Attr.h" +#include "clang/AST/Decl.h" #include "clang/AST/DeclVisitor.h" #include "clang/Index/IndexDataConsumer.h" @@ -372,6 +373,15 @@ return true; } + bool VisitEnumDecl(const EnumDecl *ED) { + TRY_TO(VisitTagDecl(ED)); + // Indexing for enumdecl itself is handled inside TagDecl, we just want to + // visit integer-base here, which is different than other TagDecl bases. + if (auto *TSI = ED->getIntegerTypeSourceInfo()) + IndexCtx.indexTypeSourceInfo(TSI, ED, ED, /*isBase=*/true); + return true; + } + bool handleReferencedProtocols(const ObjCProtocolList &ProtList, const ObjCContainerDecl *ContD, SourceLocation SuperLoc) { Index: clang-tools-extra/clangd/unittests/XRefsTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/XRefsTests.cpp +++ clang-tools-extra/clangd/unittests/XRefsTests.cpp @@ -1966,6 +1966,20 @@ [[f^oo]](s); } )cpp", + + // Enum base + R"cpp( + typedef int $def[[MyTypeDef]]; + enum MyEnum : [[MyTy^peDef]] { }; + )cpp", + R"cpp( + typedef int $def[[MyType^Def]]; + enum MyEnum : [[MyTypeDef]]; + )cpp", + R"cpp( + using $def[[MyTypeDef]] = int; + enum MyEnum : [[MyTy^peDef]] { }; + )cpp", }; for (const char *Test : Tests) checkFindRefs(Test);
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits