aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/codesearch
diff options
context:
space:
mode:
authorArtem Metla <ametla@google.com>2026-02-02 15:31:52 +0100
committerDmitry Vyukov <dvyukov@google.com>2026-02-17 17:06:23 +0000
commit39751c2134a039db51aba39640ff82d072213d73 (patch)
tree0341ebc367d12ec096864eb0de512cbde1121fea /pkg/codesearch
parente263b4d7a71c6049e9b187adb5d1309f366acd23 (diff)
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 <cstdint> in json.h
Diffstat (limited to 'pkg/codesearch')
-rw-r--r--pkg/codesearch/database.go18
-rw-r--r--pkg/codesearch/testdata/global_vars.c18
-rw-r--r--pkg/codesearch/testdata/global_vars.c.json61
-rw-r--r--pkg/codesearch/testdata/query-dir-index-root1
-rw-r--r--pkg/codesearch/testdata/query-dir-index-root21
5 files changed, 90 insertions, 9 deletions
diff --git a/pkg/codesearch/database.go b/pkg/codesearch/database.go
index 9185804ed..1ead2fb56 100644
--- a/pkg/codesearch/database.go
+++ b/pkg/codesearch/database.go
@@ -60,7 +60,7 @@ const (
EntityKindFunction
EntityKindStruct
EntityKindUnion
- EntityKindVariable
+ EntityKindGlobalVariable
EntityKindMacro
EntityKindEnum
EntityKindTypedef
@@ -69,14 +69,14 @@ const (
)
var entityKindNames = [...]string{
- EntityKindFunction: "function",
- EntityKindStruct: "struct",
- EntityKindUnion: "union",
- EntityKindVariable: "variable",
- EntityKindMacro: "macro",
- EntityKindEnum: "enum",
- EntityKindTypedef: "typedef",
- EntityKindField: "field",
+ EntityKindFunction: "function",
+ EntityKindStruct: "struct",
+ EntityKindUnion: "union",
+ EntityKindGlobalVariable: "global_variable",
+ EntityKindMacro: "macro",
+ EntityKindEnum: "enum",
+ EntityKindTypedef: "typedef",
+ EntityKindField: "field",
}
var entityKindBytes = func() [entityKindLast][]byte {
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