summaryrefslogtreecommitdiff
path: root/vndk/tools/header-checker/src/repr/json/ir_dumper.cpp
blob: bf9e38ed5fe4b35ceae561ed200d2be355fd014c (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
// Copyright (C) 2019 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "repr/json/ir_dumper.h"

#include "repr/ir_dumper.h"
#include "repr/ir_reader.h"
#include "repr/ir_representation_internal.h"
#include "repr/json/api.h"
#include "repr/json/converter.h"

#include <json/reader.h>
#include <json/writer.h>

#include <llvm/Support/raw_ostream.h>

#include <cstdlib>
#include <fstream>
#include <sstream>
#include <string>


namespace header_checker {
namespace repr {


static void AddAccess(JsonObject &type_decl, AccessSpecifierIR value) {
  if (value != default_access_ir) {
    type_decl.Set("access",
                  FindInMap(access_ir_to_json, value,
                            "Failed to convert AccessSpecifierIR to JSON"));
  }
}

static void AddRecordKind(JsonObject &record_type,
                          RecordTypeIR::RecordKind value) {
  if (value != default_record_kind_ir) {
    record_type.Set("record_kind",
                    FindInMap(record_kind_ir_to_json, value,
                              "Failed to convert RecordKind to JSON"));
  }
}

static void AddVtableComponentKind(JsonObject &vtable_component,
                                   VTableComponentIR::Kind value) {
  if (value != default_vtable_component_kind_ir) {
    vtable_component.Set(
        "kind", FindInMap(vtable_component_kind_ir_to_json, value,
                          "Failed to convert VTableComponentIR::Kind to JSON"));
  }
}

static void AddElfSymbolBinding(JsonObject &elf_symbol,
                                ElfSymbolIR::ElfSymbolBinding value) {
  if (value != default_elf_symbol_binding_ir) {
    elf_symbol.Set("binding",
                   FindInMap(elf_symbol_binding_ir_to_json, value,
                             "Failed to convert ElfSymbolBinding to JSON"));
  }
}

void IRToJsonConverter::AddTemplateInfo(
    JsonObject &type_decl, const TemplatedArtifactIR *template_ir) {
  JsonArray args;
  for (auto &&template_element_ir : template_ir->GetTemplateElements()) {
    args.append(template_element_ir.GetReferencedType());
  }
  type_decl.Set("template_args", args);
}

void IRToJsonConverter::AddTypeInfo(JsonObject &type_decl,
                                    const TypeIR *type_ir) {
  // LinkableMessageIR
  type_decl.Set("source_file", type_ir->GetSourceFile());
  const std::string &linker_set_key = type_ir->GetLinkerSetKey();
  type_decl.Set("linker_set_key", linker_set_key);
  // TypeIR
  type_decl.Set("name", type_ir->GetName());
  type_decl.Set("size", (uint64_t)type_ir->GetSize());
  type_decl.Set("alignment", (uint64_t)type_ir->GetAlignment());
  const std::string &self_type = type_ir->GetSelfType();
  if (self_type != linker_set_key) {
    type_decl.Set("self_type", self_type);
  }
  // ReferencesOtherType
  const std::string &referenced_type = type_ir->GetReferencedType();
  if (referenced_type != self_type) {
    type_decl.Set("referenced_type", referenced_type);
  }
}

static JsonObject ConvertRecordFieldIR(const RecordFieldIR *record_field_ir) {
  JsonObject record_field;
  record_field.Set("field_name", record_field_ir->GetName());
  record_field.Set("referenced_type", record_field_ir->GetReferencedType());
  AddAccess(record_field, record_field_ir->GetAccess());
  record_field.Set("field_offset", (uint64_t)record_field_ir->GetOffset());
  return record_field;
}

void IRToJsonConverter::AddRecordFields(JsonObject &record_type,
                                        const RecordTypeIR *record_ir) {
  JsonArray fields;
  for (auto &&field_ir : record_ir->GetFields()) {
    fields.append(ConvertRecordFieldIR(&field_ir));
  }
  record_type.Set("fields", fields);
}

static JsonObject
ConvertBaseSpecifierIR(const CXXBaseSpecifierIR &base_specifier_ir) {
  JsonObject base_specifier;
  base_specifier.Set("referenced_type", base_specifier_ir.GetReferencedType());
  base_specifier.Set("is_virtual", base_specifier_ir.IsVirtual());
  AddAccess(base_specifier, base_specifier_ir.GetAccess());
  return base_specifier;
}

void IRToJsonConverter::AddBaseSpecifiers(JsonObject &record_type,
                                          const RecordTypeIR *record_ir) {
  JsonArray base_specifiers;
  for (auto &&base_ir : record_ir->GetBases()) {
    base_specifiers.append(ConvertBaseSpecifierIR(base_ir));
  }
  record_type.Set("base_specifiers", base_specifiers);
}

static JsonObject
ConvertVTableComponentIR(const VTableComponentIR &vtable_component_ir) {
  JsonObject vtable_component;
  AddVtableComponentKind(vtable_component, vtable_component_ir.GetKind());
  vtable_component.Set("component_value",
                       (int64_t)vtable_component_ir.GetValue());
  vtable_component.Set("mangled_component_name", vtable_component_ir.GetName());
  vtable_component.Set("is_pure", vtable_component_ir.GetIsPure());
  return vtable_component;
}

void IRToJsonConverter::AddVTableLayout(JsonObject &record_type,
                                        const RecordTypeIR *record_ir) {
  JsonArray vtable_components;
  for (auto &&vtable_component_ir :
       record_ir->GetVTableLayout().GetVTableComponents()) {
    vtable_components.append(ConvertVTableComponentIR(vtable_component_ir));
  }
  record_type.Set("vtable_components", vtable_components);
}

JsonObject IRToJsonConverter::ConvertRecordTypeIR(const RecordTypeIR *recordp) {
  JsonObject record_type;

  AddAccess(record_type, recordp->GetAccess());
  AddRecordKind(record_type, recordp->GetRecordKind());
  record_type.Set("is_anonymous", recordp->IsAnonymous());
  AddTypeInfo(record_type, recordp);
  AddRecordFields(record_type, recordp);
  AddBaseSpecifiers(record_type, recordp);
  AddVTableLayout(record_type, recordp);
  AddTemplateInfo(record_type, recordp);
  return record_type;
}

void IRToJsonConverter::AddFunctionParametersAndSetReturnType(
    JsonObject &function, const CFunctionLikeIR *cfunction_like_ir) {
  function.Set("return_type", cfunction_like_ir->GetReturnType());
  AddFunctionParameters(function, cfunction_like_ir);
}

void IRToJsonConverter::AddFunctionParameters(
    JsonObject &function, const CFunctionLikeIR *cfunction_like_ir) {
  JsonArray parameters;
  for (auto &&parameter_ir : cfunction_like_ir->GetParameters()) {
    JsonObject parameter;
    parameter.Set("referenced_type", parameter_ir.GetReferencedType());
    parameter.Set("default_arg", parameter_ir.GetIsDefault());
    parameter.Set("is_this_ptr", parameter_ir.GetIsThisPtr());
    parameters.append(parameter);
  }
  function.Set("parameters", parameters);
}

JsonObject
IRToJsonConverter::ConvertFunctionTypeIR(const FunctionTypeIR *function_typep) {
  JsonObject function_type;
  AddTypeInfo(function_type, function_typep);
  AddFunctionParametersAndSetReturnType(function_type, function_typep);
  return function_type;
}

JsonObject IRToJsonConverter::ConvertFunctionIR(const FunctionIR *functionp) {
  JsonObject function;
  AddAccess(function, functionp->GetAccess());
  function.Set("linker_set_key", functionp->GetLinkerSetKey());
  function.Set("source_file", functionp->GetSourceFile());
  function.Set("function_name", functionp->GetName());
  AddFunctionParametersAndSetReturnType(function, functionp);
  AddTemplateInfo(function, functionp);
  return function;
}

static JsonObject ConvertEnumFieldIR(const EnumFieldIR *enum_field_ir) {
  JsonObject enum_field;
  enum_field.Set("name", enum_field_ir->GetName());
  // Never omit enum values.
  Json::Value &enum_field_value = enum_field["enum_field_value"];
  if (enum_field_ir->IsSigned()) {
    enum_field_value = Json::Int64(enum_field_ir->GetSignedValue());
  } else {
    enum_field_value = Json::UInt64(enum_field_ir->GetUnsignedValue());
  }
  return enum_field;
}

void IRToJsonConverter::AddEnumFields(JsonObject &enum_type,
                                      const EnumTypeIR *enum_ir) {
  JsonArray enum_fields;
  for (auto &&field : enum_ir->GetFields()) {
    enum_fields.append(ConvertEnumFieldIR(&field));
  }
  enum_type.Set("enum_fields", enum_fields);
}

JsonObject IRToJsonConverter::ConvertEnumTypeIR(const EnumTypeIR *enump) {
  JsonObject enum_type;
  AddAccess(enum_type, enump->GetAccess());
  enum_type.Set("underlying_type", enump->GetUnderlyingType());
  AddTypeInfo(enum_type, enump);
  AddEnumFields(enum_type, enump);
  return enum_type;
}

JsonObject
IRToJsonConverter::ConvertGlobalVarIR(const GlobalVarIR *global_varp) {
  JsonObject global_var;
  // GlobalVarIR
  global_var.Set("name", global_varp->GetName());
  AddAccess(global_var, global_varp->GetAccess());
  // LinkableMessageIR
  global_var.Set("source_file", global_varp->GetSourceFile());
  const std::string &linker_set_key = global_varp->GetLinkerSetKey();
  global_var.Set("linker_set_key", linker_set_key);
  // ReferencesOtherType
  const std::string &referenced_type = global_varp->GetReferencedType();
  if (linker_set_key != referenced_type) {
    global_var.Set("referenced_type", referenced_type);
  }
  return global_var;
}

JsonObject
IRToJsonConverter::ConvertPointerTypeIR(const PointerTypeIR *pointerp) {
  JsonObject pointer_type;
  AddTypeInfo(pointer_type, pointerp);
  return pointer_type;
}

JsonObject
IRToJsonConverter::ConvertQualifiedTypeIR(const QualifiedTypeIR *qualtypep) {
  JsonObject qualified_type;
  AddTypeInfo(qualified_type, qualtypep);
  qualified_type.Set("is_const", qualtypep->IsConst());
  qualified_type.Set("is_volatile", qualtypep->IsVolatile());
  qualified_type.Set("is_restricted", qualtypep->IsRestricted());
  return qualified_type;
}

JsonObject
IRToJsonConverter::ConvertBuiltinTypeIR(const BuiltinTypeIR *builtin_typep) {
  JsonObject builtin_type;
  builtin_type.Set("is_unsigned", builtin_typep->IsUnsigned());
  builtin_type.Set("is_integral", builtin_typep->IsIntegralType());
  AddTypeInfo(builtin_type, builtin_typep);
  return builtin_type;
}

JsonObject
IRToJsonConverter::ConvertArrayTypeIR(const ArrayTypeIR *array_typep) {
  JsonObject array_type;
  array_type.Set("is_of_unknown_bound", array_typep->IsOfUnknownBound());
  AddTypeInfo(array_type, array_typep);
  return array_type;
}

JsonObject IRToJsonConverter::ConvertLvalueReferenceTypeIR(
    const LvalueReferenceTypeIR *lvalue_reference_typep) {
  JsonObject lvalue_reference_type;
  AddTypeInfo(lvalue_reference_type, lvalue_reference_typep);
  return lvalue_reference_type;
}

JsonObject IRToJsonConverter::ConvertRvalueReferenceTypeIR(
    const RvalueReferenceTypeIR *rvalue_reference_typep) {
  JsonObject rvalue_reference_type;
  AddTypeInfo(rvalue_reference_type, rvalue_reference_typep);
  return rvalue_reference_type;
}

bool JsonIRDumper::AddLinkableMessageIR(const LinkableMessageIR *lm) {
  std::string key;
  JsonObject converted;
  // No RTTI
  switch (lm->GetKind()) {
  case RecordTypeKind:
    key = "record_types";
    converted = ConvertRecordTypeIR(static_cast<const RecordTypeIR *>(lm));
    break;
  case EnumTypeKind:
    key = "enum_types";
    converted = ConvertEnumTypeIR(static_cast<const EnumTypeIR *>(lm));
    break;
  case PointerTypeKind:
    key = "pointer_types";
    converted = ConvertPointerTypeIR(static_cast<const PointerTypeIR *>(lm));
    break;
  case QualifiedTypeKind:
    key = "qualified_types";
    converted =
        ConvertQualifiedTypeIR(static_cast<const QualifiedTypeIR *>(lm));
    break;
  case ArrayTypeKind:
    key = "array_types";
    converted = ConvertArrayTypeIR(static_cast<const ArrayTypeIR *>(lm));
    break;
  case LvalueReferenceTypeKind:
    key = "lvalue_reference_types";
    converted = ConvertLvalueReferenceTypeIR(
        static_cast<const LvalueReferenceTypeIR *>(lm));
    break;
  case RvalueReferenceTypeKind:
    key = "rvalue_reference_types";
    converted = ConvertRvalueReferenceTypeIR(
        static_cast<const RvalueReferenceTypeIR *>(lm));
    break;
  case BuiltinTypeKind:
    key = "builtin_types";
    converted = ConvertBuiltinTypeIR(static_cast<const BuiltinTypeIR *>(lm));
    break;
  case FunctionTypeKind:
    key = "function_types";
    converted = ConvertFunctionTypeIR(static_cast<const FunctionTypeIR *>(lm));
    break;
  case GlobalVarKind:
    key = "global_vars";
    converted = ConvertGlobalVarIR(static_cast<const GlobalVarIR *>(lm));
    break;
  case FunctionKind:
    key = "functions";
    converted = ConvertFunctionIR(static_cast<const FunctionIR *>(lm));
    break;
  default:
    return false;
  }
  translation_unit_[key].append(converted);
  return true;
}

bool JsonIRDumper::AddElfSymbolMessageIR(const ElfSymbolIR *elf_symbol_ir) {
  std::string key;
  switch (elf_symbol_ir->GetKind()) {
  case ElfSymbolIR::ElfFunctionKind:
    key = "elf_functions";
    break;
  case ElfSymbolIR::ElfObjectKind:
    key = "elf_objects";
    break;
  default:
    return false;
  }
  JsonObject elf_symbol;
  elf_symbol.Set("name", elf_symbol_ir->GetName());
  AddElfSymbolBinding(elf_symbol, elf_symbol_ir->GetBinding());
  translation_unit_[key].append(elf_symbol);
  return true;
}

static std::string DumpJson(const JsonObject &obj) {
  Json::StreamWriterBuilder factory;
  factory["indentation"] = " ";
  return Json::writeString(factory, obj);
}

static bool WriteTailTrimmedLinesToFile(const std::string &path,
                                        const std::string &output_string) {
  std::ofstream output_file(path);
  size_t line_start = 0;
  while (line_start < output_string.size()) {
    size_t trailing_space_start = line_start;
    size_t index;
    for (index = line_start;
         index < output_string.size() && output_string[index] != '\n';
         index++) {
      if (output_string[index] != ' ') {
        trailing_space_start = index + 1;
      }
    }
    // Only write this line if this line contains non-whitespace characters.
    if (trailing_space_start != line_start) {
      if (output_file
              .write(output_string.data() + line_start,
                     trailing_space_start - line_start)
              .fail()) {
        return false;
      }
      if (output_file.write("\n", 1).fail()) {
        return false;
      }
    }
    line_start = index + 1;
  }
  return output_file.flush().good();
}

bool JsonIRDumper::Dump(const ModuleIR &module) {
  DumpModule(module);
  std::string output_string = DumpJson(translation_unit_);
  return WriteTailTrimmedLinesToFile(dump_path_, output_string);
}

JsonIRDumper::JsonIRDumper(const std::string &dump_path)
    : IRDumper(dump_path), translation_unit_() {
  const std::string keys[] = {
    "record_types",
    "enum_types",
    "pointer_types",
    "lvalue_reference_types",
    "rvalue_reference_types",
    "builtin_types",
    "qualified_types",
    "array_types",
    "function_types",
    "functions",
    "global_vars",
    "elf_functions",
    "elf_objects",
  };
  for (auto key : keys) {
    translation_unit_[key] = JsonArray();
  }
}

std::unique_ptr<IRDumper> CreateJsonIRDumper(const std::string &dump_path) {
  return std::make_unique<JsonIRDumper>(dump_path);
}


}  // namespace repr
}  // header_checker