From f855e4845e221d8e036caa7edac9fe2926020ba4 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Wed, 21 Jan 2026 08:27:08 +0100 Subject: pkg/codesearch: fix resolving of static functions declared in headers Update #6469 --- pkg/codesearch/codesearch.go | 25 ++++++++++++++-------- pkg/codesearch/testdata/query-def-source-in-header | 8 +++++++ pkg/codesearch/testdata/source0.c.json | 12 +++++++++++ pkg/codesearch/testdata/source0.h | 5 +++++ 4 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 pkg/codesearch/testdata/query-def-source-in-header (limited to 'pkg/codesearch') diff --git a/pkg/codesearch/codesearch.go b/pkg/codesearch/codesearch.go index 396df4f82..051cad3c5 100644 --- a/pkg/codesearch/codesearch.go +++ b/pkg/codesearch/codesearch.go @@ -312,18 +312,25 @@ func (index *Index) FindReferences(contextFile, name, srcPrefix string, contextL } func (index *Index) findDefinition(contextFile, name string) *Definition { - var weakMatch *Definition + var weakMatch, veryWeakMatch *Definition for _, def := range index.db.Definitions { - if def.Name == name { - if def.Body.File == contextFile { - return def - } - if !def.IsStatic { - weakMatch = def - } + if def.Name != name { + continue } + if def.Body.File == contextFile { + return def + } + // Strictly speaking there may be several different static functions in different headers, + // but we ignore such possibility for now. + if !def.IsStatic || strings.HasSuffix(def.Body.File, ".h") { + weakMatch = def + } + veryWeakMatch = def + } + if weakMatch != nil { + return weakMatch } - return weakMatch + return veryWeakMatch } func (index *Index) formatSource(lines LineRange, includeLines bool) (string, error) { diff --git a/pkg/codesearch/testdata/query-def-source-in-header b/pkg/codesearch/testdata/query-def-source-in-header new file mode 100644 index 000000000..3e6fe581a --- /dev/null +++ b/pkg/codesearch/testdata/query-def-source-in-header @@ -0,0 +1,8 @@ +def-source source0.c func_in_header yes + +function func_in_header is defined in source0.h: + + 12: static inline int func_in_header() + 13: { + 14: return 0; + 15: } diff --git a/pkg/codesearch/testdata/source0.c.json b/pkg/codesearch/testdata/source0.c.json index 5eea7aba9..cb80dc021 100644 --- a/pkg/codesearch/testdata/source0.c.json +++ b/pkg/codesearch/testdata/source0.c.json @@ -11,6 +11,18 @@ }, "comment": {} }, + { + "kind": "function", + "name": "func_in_header", + "type": "int ()", + "is_static": true, + "body": { + "file": "source0.h", + "start_line": 12, + "end_line": 15 + }, + "comment": {} + }, { "kind": "function", "name": "function_with_comment_in_header", diff --git a/pkg/codesearch/testdata/source0.h b/pkg/codesearch/testdata/source0.h index 339975b2e..d1540ebce 100644 --- a/pkg/codesearch/testdata/source0.h +++ b/pkg/codesearch/testdata/source0.h @@ -8,3 +8,8 @@ void function_with_comment_in_header(); void same_name_in_several_files(); + +static inline int func_in_header() +{ + return 0; +} -- cgit mrf-deployment