aboutsummaryrefslogtreecommitdiffstats
path: root/tools/clang/codesearch/output.h
blob: d7ce80d1397a2015418af78ef0a36d2494abdee1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2025 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.

#ifndef SYZ_INDEXER_OUTPUT_H
#define SYZ_INDEXER_OUTPUT_H

#include "json.h"
#include <vector>

// Note: these string names are exposed all way to LLMs,
// so keep them readable and meaningful.

constexpr char EntityKindFunction[] = "function";
constexpr char EntityKindStruct[] = "struct";
constexpr char EntityKindUnion[] = "union";
constexpr char EntityKindGlobalVariable[] = "global_variable";
constexpr char EntityKindMacro[] = "macro";
constexpr char EntityKindEnum[] = "enum";
constexpr char EntityKindTypedef[] = "typedef";
constexpr char EntityKindField[] = "field";

// The uses reference is very generic, ideally we refine it in the future
// (e.g. "used as an argument type", "cast to this type", "includes field of this type", etc).
constexpr char RefKindUses[] = "uses";
constexpr char RefKindCall[] = "calls";
constexpr char RefKindRead[] = "reads";
constexpr char RefKindWrite[] = "writes";
constexpr char RefKindTakesAddr[] = "takes-address-of";

struct LineRange {
  std::string File;
  int StartLine = 0;
  int EndLine = 0;
};

struct Reference {
  const char* Kind;
  const char* EntityKind;
  std::string Name;
  int Line;
};

struct FieldInfo {
  std::string Name;
  uint64_t OffsetBits;
  uint64_t SizeBits;
};

struct Definition {
  const char* Kind; // one of Kind* consts
  std::string Name;
  std::string Type; // raw C type
  bool IsStatic = false;
  // If the kernel-doc comment is placed around the body,
  // then it's included in the body range.
  LineRange Body;
  // Location of the kernel-doc comment.
  LineRange Comment;
  std::vector<Reference> Refs;
  std::vector<FieldInfo> Fields;
};

inline void print(JSONPrinter& Printer, const LineRange& V) {
  JSONPrinter::Scope Scope(Printer);
  Printer.Field("file", V.File);
  Printer.Field("start_line", V.StartLine);
  Printer.Field("end_line", V.EndLine, true);
}

inline void print(JSONPrinter& Printer, const Reference& V) {
  JSONPrinter::Scope Scope(Printer);
  Printer.Field("kind", V.Kind);
  Printer.Field("entity_kind", V.EntityKind);
  Printer.Field("name", V.Name);
  Printer.Field("line", V.Line, true);
}

inline void print(JSONPrinter& Printer, const FieldInfo& V) {
  JSONPrinter::Scope Scope(Printer);
  Printer.Field("name", V.Name);
  Printer.Field("offset", V.OffsetBits);
  Printer.Field("size", V.SizeBits, true);
}

inline void print(JSONPrinter& Printer, const Definition& V) {
  JSONPrinter::Scope Scope(Printer);
  Printer.Field("kind", V.Kind);
  Printer.Field("name", V.Name);
  Printer.Field("type", V.Type);
  Printer.Field("is_static", V.IsStatic);
  Printer.Field("body", V.Body);
  Printer.Field("comment", V.Comment);
  Printer.Field("refs", V.Refs);
  Printer.Field("fields", V.Fields, true);
}

class Output {
public:
  void emit(Definition&& V) { Definitions.push_back(std::move(V)); }

  void print() const {
    JSONPrinter Printer;
    Printer.Field("definitions", Definitions, true);
  }

private:
  std::vector<Definition> Definitions;
};

#endif