From 39751c2134a039db51aba39640ff82d072213d73 Mon Sep 17 00:00:00 2001 From: Artem Metla Date: Mon, 2 Feb 2026 15:31:52 +0100 Subject: tools/clang/codesearch: improve codesearch to handle global variables Contributes to #6469. To handle global variables: * Add EntityKindGlobalVariable * Modify TraverseVarDecl() function logic * Add a check to ensure StartLine and EndLine are in the same file * Fix missing #include in json.h --- pkg/codesearch/testdata/global_vars.c | 18 ++++++++ pkg/codesearch/testdata/global_vars.c.json | 61 +++++++++++++++++++++++++++ pkg/codesearch/testdata/query-dir-index-root | 1 + pkg/codesearch/testdata/query-dir-index-root2 | 1 + 4 files changed, 81 insertions(+) create mode 100644 pkg/codesearch/testdata/global_vars.c create mode 100644 pkg/codesearch/testdata/global_vars.c.json (limited to 'pkg/codesearch/testdata') diff --git a/pkg/codesearch/testdata/global_vars.c b/pkg/codesearch/testdata/global_vars.c new file mode 100644 index 000000000..a7f3e76f0 --- /dev/null +++ b/pkg/codesearch/testdata/global_vars.c @@ -0,0 +1,18 @@ +// Copyright 2026 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +#define DEFINE_VAR(name) int name = 1 +#define DEFINE_STATIC_VAR(name) static int name = 2 + +DEFINE_VAR(macro_var); + +DEFINE_STATIC_VAR(static_macro_var); + +int global_var = 3; +static int local_to_file_var = 4; + +void some_function(void) +{ + int local_var = 5; + (void)local_var; +} diff --git a/pkg/codesearch/testdata/global_vars.c.json b/pkg/codesearch/testdata/global_vars.c.json new file mode 100644 index 000000000..89fc1a04b --- /dev/null +++ b/pkg/codesearch/testdata/global_vars.c.json @@ -0,0 +1,61 @@ +{ + "definitions": [ + { + "name": "some_function", + "type": "void (void)", + "kind": "function", + "body": { + "file": "global_vars.c", + "start_line": 14, + "end_line": 18 + }, + "comment": {} + }, + { + "name": "global_var", + "type": "int", + "kind": "global_variable", + "body": { + "file": "global_vars.c", + "start_line": 11, + "end_line": 11 + }, + "comment": {} + }, + { + "name": "local_to_file_var", + "type": "int", + "kind": "global_variable", + "is_static": true, + "body": { + "file": "global_vars.c", + "start_line": 12, + "end_line": 12 + }, + "comment": {} + }, + { + "name": "macro_var", + "type": "int", + "kind": "global_variable", + "body": { + "file": "global_vars.c", + "start_line": 7, + "end_line": 7 + }, + "comment": {} + }, + { + "name": "static_macro_var", + "type": "int", + "kind": "global_variable", + "is_static": true, + "body": { + "file": "global_vars.c", + "start_line": 9, + "end_line": 9 + }, + "comment": {} + } + ] +} \ No newline at end of file diff --git a/pkg/codesearch/testdata/query-dir-index-root b/pkg/codesearch/testdata/query-dir-index-root index 5e72b64c1..7370911ea 100644 --- a/pkg/codesearch/testdata/query-dir-index-root +++ b/pkg/codesearch/testdata/query-dir-index-root @@ -4,6 +4,7 @@ directory / subdirs: - mm directory / files: + - global_vars.c - refs.c - source0.c - source0.h diff --git a/pkg/codesearch/testdata/query-dir-index-root2 b/pkg/codesearch/testdata/query-dir-index-root2 index 804bdce54..2b9fdce5d 100644 --- a/pkg/codesearch/testdata/query-dir-index-root2 +++ b/pkg/codesearch/testdata/query-dir-index-root2 @@ -4,6 +4,7 @@ directory /mm/.. subdirs: - mm directory /mm/.. files: + - global_vars.c - refs.c - source0.c - source0.h -- cgit mrf-deployment