Index: clangd/Quality.cpp =================================================================== --- clangd/Quality.cpp +++ clangd/Quality.cpp @@ -11,10 +11,12 @@ #include "URI.h" #include "index/Index.h" #include "clang/AST/ASTContext.h" +#include "clang/AST/DeclCXX.h" #include "clang/AST/DeclVisitor.h" #include "clang/Basic/CharInfo.h" #include "clang/Basic/SourceManager.h" #include "clang/Sema/CodeCompleteConsumer.h" +#include "llvm/Support/Casting.h" #include "llvm/Support/FormatVariadic.h" #include "llvm/Support/MathExtras.h" #include "llvm/Support/raw_ostream.h" @@ -195,6 +197,9 @@ if (auto *R = dyn_cast_or_null(D)) if (R->isInjectedClassName()) DC = DC->getParent(); + // Class constructor should have the same scope as the class. + if (const auto *Ctor = llvm::dyn_cast(D)) + DC = DC->getParent(); bool InClass = false; for (; !DC->isFileContext(); DC = DC->getParent()) { if (DC->isFunctionOrMethod()) Index: unittests/clangd/QualityTests.cpp =================================================================== --- unittests/clangd/QualityTests.cpp +++ unittests/clangd/QualityTests.cpp @@ -21,6 +21,9 @@ #include "Quality.h" #include "TestFS.h" #include "TestTU.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "llvm/Support/Casting.h" #include "gmock/gmock.h" #include "gtest/gtest.h" @@ -199,6 +202,31 @@ EXPECT_LT(sortText(0, "a"), sortText(0, "z")); } +TEST(QualityTests, NoBoostForClassConstructor) { + auto Header = TestTU::withHeaderCode(R"cpp( + class Foo { + public: + Foo(int); + }; + )cpp"); + auto Symbols = Header.headerSymbols(); + auto AST = Header.build(); + + const NamedDecl *Foo = &findDecl(AST, "Foo"); + SymbolRelevanceSignals Cls; + Cls.merge(CodeCompletionResult(Foo, /*Priority=*/0)); + + const NamedDecl *CtorDecl = &findAnyDecl(AST, [](const NamedDecl &ND) { + return (ND.getQualifiedNameAsString() == "Foo::Foo") && + llvm::isa(&ND); + }); + SymbolRelevanceSignals Ctor; + Ctor.merge(CodeCompletionResult(CtorDecl, /*Priority=*/0)); + + EXPECT_EQ(Cls.Scope, SymbolRelevanceSignals::GlobalScope); + EXPECT_EQ(Ctor.Scope, SymbolRelevanceSignals::GlobalScope); +} + } // namespace } // namespace clangd } // namespace clang Index: unittests/clangd/TestTU.h =================================================================== --- unittests/clangd/TestTU.h +++ unittests/clangd/TestTU.h @@ -56,6 +56,9 @@ const Symbol &findSymbol(const SymbolSlab &, llvm::StringRef QName); // Look up an AST symbol by qualified name, which must be unique and top-level. const NamedDecl &findDecl(ParsedAST &AST, llvm::StringRef QName); +// Look up a main-file AST symbol that satisfies \p Filter. +const NamedDecl &findAnyDecl(ParsedAST &AST, + std::function Filter); // Look up a main-file AST symbol by unqualified name, which must be unique. const NamedDecl &findAnyDecl(ParsedAST &AST, llvm::StringRef Name); Index: unittests/clangd/TestTU.cpp =================================================================== --- unittests/clangd/TestTU.cpp +++ unittests/clangd/TestTU.cpp @@ -93,26 +93,35 @@ return LookupDecl(*Scope, Components.back()); } -const NamedDecl &findAnyDecl(ParsedAST &AST, llvm::StringRef Name) { +const NamedDecl &findAnyDecl(ParsedAST &AST, + std::function Callback) { struct Visitor : RecursiveASTVisitor { - llvm::StringRef Name; + decltype(Callback) CB; llvm::SmallVector Decls; bool VisitNamedDecl(const NamedDecl *ND) { - if (auto *ID = ND->getIdentifier()) - if (ID->getName() == Name) - Decls.push_back(ND); + if (CB(*ND)) + Decls.push_back(ND); return true; } } Visitor; - Visitor.Name = Name; + Visitor.CB = Callback; for (Decl *D : AST.getLocalTopLevelDecls()) Visitor.TraverseDecl(D); if (Visitor.Decls.size() != 1) { - ADD_FAILURE() << Visitor.Decls.size() << " symbols named " << Name; + ADD_FAILURE() << Visitor.Decls.size() << " symbols matched."; assert(Visitor.Decls.size() == 1); } return *Visitor.Decls.front(); } +const NamedDecl &findAnyDecl(ParsedAST &AST, llvm::StringRef Name) { + return findAnyDecl(AST, [Name](const NamedDecl &ND) { + if (auto *ID = ND.getIdentifier()) + if (ID->getName() == Name) + return true; + return false; + }); +} + } // namespace clangd } // namespace clang