• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2019 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef HEADER_CHECKER_REPR_JSON_CONVERTER_H_
16 #define HEADER_CHECKER_REPR_JSON_CONVERTER_H_
17 
18 #include "repr/ir_representation.h"
19 
20 #include <json/value.h>
21 
22 #include <map>
23 #include <string>
24 
25 #include <llvm/Support/raw_ostream.h>
26 
27 
28 namespace header_checker {
29 namespace repr {
30 
31 
32 // These classes wrap constructors of Json::Value.
33 class JsonArray : public Json::Value {
34  public:
JsonArray()35   JsonArray() : Json::Value(Json::ValueType::arrayValue) {}
36 };
37 
38 
39 class JsonObject : public Json::Value {
40  public:
JsonObject()41   JsonObject() : Json::Value(Json::ValueType::objectValue) {}
42 
43   // This method inserts the key-value pair if the value is not equal to the
44   // omissible value.
45   // Omit false.
46   void Set(const std::string &key, bool value);
47 
48   // Omit 0.
49   void Set(const std::string &key, uint64_t value);
50 
51   // Omit 0.
52   void Set(const std::string &key, int64_t value);
53 
54   // Omit "".
55   void Set(const std::string &key, const std::string &value);
56 
57   // Omit [].
58   void Set(const std::string &key, const JsonArray &value);
59 
60  private:
61   template <typename T>
SetOmissible(const std::string & key,T value,T omissible_value)62   inline void SetOmissible(const std::string &key, T value, T omissible_value) {
63     if (value != omissible_value) {
64       (*this)[key] = value;
65     } else {
66       removeMember(key);
67     }
68   }
69 };
70 
71 
72 extern const JsonArray json_empty_array;
73 extern const JsonObject json_empty_object;
74 extern const Json::Value json_0;
75 extern const Json::Value json_false;
76 extern const Json::Value json_empty_string;
77 
78 extern const AccessSpecifierIR default_access_ir;
79 extern const RecordTypeIR::RecordKind default_record_kind_ir;
80 extern const VTableComponentIR::Kind default_vtable_component_kind_ir;
81 extern const ElfSymbolIR::ElfSymbolBinding default_elf_symbol_binding_ir;
82 
83 
84 // Conversion between IR enums and JSON strings.
85 static const std::map<AccessSpecifierIR, std::string> access_ir_to_json{
86   {AccessSpecifierIR::PublicAccess, "public"},
87   {AccessSpecifierIR::ProtectedAccess, "protected"},
88   {AccessSpecifierIR::PrivateAccess, "private"},
89 };
90 
91 static const std::map<RecordTypeIR::RecordKind, std::string>
92     record_kind_ir_to_json{
93   {RecordTypeIR::RecordKind::struct_kind, "struct"},
94   {RecordTypeIR::RecordKind::class_kind, "class"},
95   {RecordTypeIR::RecordKind::union_kind, "union"},
96 };
97 
98 static const std::map<VTableComponentIR::Kind, std::string>
99     vtable_component_kind_ir_to_json{
100   {VTableComponentIR::Kind::VCallOffset, "vcall_offset"},
101   {VTableComponentIR::Kind::VBaseOffset, "vbase_offset"},
102   {VTableComponentIR::Kind::OffsetToTop, "offset_to_top"},
103   {VTableComponentIR::Kind::RTTI, "rtti"},
104   {VTableComponentIR::Kind::FunctionPointer, "function_pointer"},
105   {VTableComponentIR::Kind::CompleteDtorPointer, "complete_dtor_pointer"},
106   {VTableComponentIR::Kind::DeletingDtorPointer, "deleting_dtor_pointer"},
107   {VTableComponentIR::Kind::UnusedFunctionPointer, "unused_function_pointer"},
108 };
109 
110 static const std::map<ElfSymbolIR::ElfSymbolBinding, std::string>
111     elf_symbol_binding_ir_to_json{
112   {ElfSymbolIR::ElfSymbolBinding::Weak, "weak"},
113   {ElfSymbolIR::ElfSymbolBinding::Global, "global"},
114 };
115 
116 // If m contains k, this function returns the value.
117 // Otherwise, it prints error_msg and exits.
118 template <typename K, typename V>
FindInMap(const std::map<K,V> & m,const K & k,const std::string & error_msg)119 static inline const V &FindInMap(const std::map<K, V> &m, const K &k,
120                                  const std::string &error_msg) {
121   auto it = m.find(k);
122   if (it == m.end()) {
123     llvm::errs() << error_msg << "\n";
124     ::exit(1);
125   }
126   return it->second;
127 }
128 
129 
130 }  // namespace repr
131 }  // namespace header_checker
132 
133 
134 #endif  // HEADER_CHECKER_REPR_JSON_CONVERTER_H_
135