[clang] [clang-tools-extra] [clangd] CodeCompletion consistent parameter names (PR #206716)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Jun 30 05:12:12 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clangd
Author: timon-ul
<details>
<summary>Changes</summary>
Resolves issue https://github.com/clangd/clangd/issues/2651
---
Full diff: https://github.com/llvm/llvm-project/pull/206716.diff
3 Files Affected:
- (modified) clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp (+18)
- (modified) clang-tools-extra/clangd/unittests/IndexActionTests.cpp (+25)
- (modified) clang/lib/Sema/SemaCodeComplete.cpp (+7)
``````````diff
diff --git a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
index 5808b2145965f..037983a80300d 100644
--- a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
+++ b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
@@ -698,6 +698,24 @@ TEST(CompletionTest, PrivateMemberDefinition) {
snippetSuffix("(int a, int b)"))));
}
+TEST(CompletionTest, DeclParamName) {
+ // This is 1/2 regression tests to make sure signatures
+ // 1) have consistent variable names between header and source file
+ // 2) find variable names in other declarations
+ clangd::CodeCompleteOptions Opts;
+ Opts.EnableSnippets = true;
+ auto FuncFromSource = completions(R"cpp(
+ void sun(int);
+ void sun(int day);
+ void sun(int);
+ void position() {
+ sun^;
+ }
+ )cpp",
+ {}, Opts);
+ EXPECT_THAT(FuncFromSource.Completions, Contains(signature("(int day)")));
+}
+
TEST(CompletionTest, DefaultArgsWithValues) {
clangd::CodeCompleteOptions Opts;
Opts.EnableSnippets = true;
diff --git a/clang-tools-extra/clangd/unittests/IndexActionTests.cpp b/clang-tools-extra/clangd/unittests/IndexActionTests.cpp
index 025027fdcee5d..5b8029654a73d 100644
--- a/clang-tools-extra/clangd/unittests/IndexActionTests.cpp
+++ b/clang-tools-extra/clangd/unittests/IndexActionTests.cpp
@@ -11,6 +11,7 @@
#include "URI.h"
#include "index/IndexAction.h"
#include "index/Serialization.h"
+#include "index/Symbol.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Tooling/Tooling.h"
@@ -38,6 +39,8 @@ MATCHER_P(hasDigest, Digest, "") { return arg.Digest == Digest; }
MATCHER_P(hasName, Name, "") { return arg.Name == Name; }
+MATCHER_P(hasSignature, Signature, "") { return arg.Signature == Signature; }
+
MATCHER(hasSameURI, "") {
llvm::StringRef URI = ::testing::get<0>(arg);
const std::string &Path = ::testing::get<1>(arg);
@@ -258,6 +261,28 @@ TEST_F(IndexActionTest, NoWarnings) {
EXPECT_THAT(*IndexFile.Symbols, ElementsAre(hasName("foo"), hasName("bar")));
}
+TEST_F(IndexActionTest, DeclParamName) {
+ // This is 2/2 regression tests to make sure signatures
+ // 1) have consistent variable names between header and source file
+ // 2) find variable names in other declarations
+ std::string MainFilePath = testPath("main.cpp");
+ std::string MainCode = R"cpp( #include "zenith.hpp" )cpp";
+ std::string HeaderPath = testPath("zenith.hpp");
+ std::string HeaderCode = R"cpp(
+ void moon(int, int);
+ void moon(int month, int day);
+ void moon(int, int);
+ )cpp";
+
+ addFile(MainFilePath, MainCode);
+ addFile(HeaderPath, HeaderCode);
+
+ IndexFileIn IndexFile = runIndexingAction(MainFilePath);
+
+ EXPECT_THAT(*IndexFile.Symbols,
+ ElementsAre(hasSignature("(int month, int day)")));
+}
+
TEST_F(IndexActionTest, SkipFiles) {
std::string MainFilePath = testPath("main.cpp");
addFile(MainFilePath, R"cpp(
diff --git a/clang/lib/Sema/SemaCodeComplete.cpp b/clang/lib/Sema/SemaCodeComplete.cpp
index bef5eea9ed442..f724fb44f184e 100644
--- a/clang/lib/Sema/SemaCodeComplete.cpp
+++ b/clang/lib/Sema/SemaCodeComplete.cpp
@@ -3320,6 +3320,13 @@ static void AddFunctionParameterChunks(
for (unsigned P = Start, N = Function->getNumParams(); P != N; ++P) {
const ParmVarDecl *Param = Function->getParamDecl(P);
+ // Select a parameter with identifier if possible for better signature
+ if (!Param->getIdentifier()) {
+ for (auto *Redecl : Function->redecls()) {
+ if (auto *RParam = Redecl->getParamDecl(P); RParam->getIdentifier())
+ Param = RParam;
+ }
+ }
if (Param->hasDefaultArg() && !InOptional && !IsInDeclarationContext &&
!AsInformativeChunk) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/206716
More information about the cfe-commits
mailing list