aboutsummaryrefslogtreecommitdiffstats
path: root/tools/clang/codesearch/output.h
blob: ac490bb9197a50968840ed504c53e5de3b6befe6 (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
// 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>

constexpr char KindFunction[] = "function";
constexpr char KindStruct[] = "struct";
constexpr char KindVariable[] = "variable";
constexpr char KindMacro[] = "macro";
constexpr char KindEnum[] = "enum";

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

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;
};

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 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, 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