diff --git a/clang-tools-extra/clangd/index/SymbolCollector.cpp b/clang-tools-extra/clangd/index/SymbolCollector.cpp --- a/clang-tools-extra/clangd/index/SymbolCollector.cpp +++ b/clang-tools-extra/clangd/index/SymbolCollector.cpp @@ -828,6 +828,23 @@ // FIXME: this returns foo:bar: for objective-C methods, we prefer only foo: // for consistency with CodeCompletionString and a clean name/signature split. std::tie(S.Scope, S.Name) = splitQualifiedName(QName); + std::string Scope = S.Scope.str(); + // Put additional qualifiers for enum constants: technically, they can be + // accessed without using the enum name prefix (unless declared as enum + // class) but it is more convenient to enhance the scope with the enum type + // name. + // FIXME: It would be better to just have the SymbolIndex look up the same + // enum constant both with and without enum type name but at the time the + // index is built the information about ND is already lost. + if (const auto *EnumConstant = llvm::dyn_cast(&ND)) { + if (const auto *II = EnumConstant->getType().getBaseTypeIdentifier()) { + std::string EnumScope = II->getName().str() + "::"; + // For "enum class" the qualified name already has EnumScope. + if (!S.Scope.endswith(EnumScope)) + Scope += EnumScope; + } + } + S.Scope = Scope; std::string TemplateSpecializationArgs = printTemplateSpecializationArgs(ND); S.TemplateSpecializationArgs = TemplateSpecializationArgs; diff --git a/clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp b/clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp --- a/clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp +++ b/clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp @@ -250,14 +250,15 @@ )cpp"; EXPECT_THAT(getSymbols(TU, "Red"), ElementsAre(QName("Red"))); EXPECT_THAT(getSymbols(TU, "::Red"), ElementsAre(QName("Red"))); - EXPECT_THAT(getSymbols(TU, "Green"), ElementsAre(QName("Green"))); - EXPECT_THAT(getSymbols(TU, "Green"), ElementsAre(QName("Green"))); + EXPECT_THAT(getSymbols(TU, "Color::Green"), + ElementsAre(QName("Color::Green"))); EXPECT_THAT(getSymbols(TU, "Color2::Yellow"), ElementsAre(QName("Color2::Yellow"))); EXPECT_THAT(getSymbols(TU, "Yellow"), ElementsAre(QName("Color2::Yellow"))); EXPECT_THAT(getSymbols(TU, "ns::Black"), ElementsAre(QName("ns::Black"))); - EXPECT_THAT(getSymbols(TU, "ns::Blue"), ElementsAre(QName("ns::Blue"))); + EXPECT_THAT(getSymbols(TU, "ns::Color3::Blue"), + ElementsAre(QName("ns::Color3::Blue"))); EXPECT_THAT(getSymbols(TU, "ns::Color4::White"), ElementsAre(QName("ns::Color4::White"))); } diff --git a/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp b/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp --- a/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp +++ b/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp @@ -1232,7 +1232,7 @@ UnorderedElementsAre( AllOf(QName("Red"), ForCodeCompletion(true)), AllOf(QName("Color"), ForCodeCompletion(true)), - AllOf(QName("Green"), ForCodeCompletion(true)), + AllOf(QName("Color::Green"), ForCodeCompletion(true)), AllOf(QName("Color2"), ForCodeCompletion(true)), AllOf(QName("Color2::Yellow"), ForCodeCompletion(false)), AllOf(QName("ns"), ForCodeCompletion(true)),