Home | History | Annotate | Download | only in cpp-define-generator
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include <iostream>
     18 #include <sstream>
     19 #include <type_traits>
     20 #include <ios>
     21 #include <algorithm>
     22 #include <string>
     23 
     24 // Art Offset file dependencies
     25 #define DEFINE_INCLUDE_DEPENDENCIES
     26 #include "offsets_all.def"
     27 
     28 std::string to_upper(std::string input) {
     29   std::transform(input.begin(), input.end(), input.begin(), ::toupper);
     30   return input;
     31 }
     32 
     33 template <typename T, typename = void>
     34 typename std::enable_if<!std::is_signed<T>::value, std::string>::type
     35 pretty_format(T value) {
     36   // Print most values as hex.
     37   std::stringstream ss;
     38   ss << std::showbase << std::hex << value;
     39   return ss.str();
     40 }
     41 
     42 template <typename T, typename = void>
     43 typename std::enable_if<std::is_signed<T>::value, std::string>::type
     44 pretty_format(T value) {
     45   // Print "signed" values as decimal so that the negativity doesn't get lost.
     46   std::stringstream ss;
     47 
     48   // For negative values add a (). Omit it from positive values for conciseness.
     49   if (value < 0) {
     50     ss << "(";
     51   }
     52 
     53   ss << value;
     54 
     55   if (value < 0) {
     56     ss << ")";
     57   }
     58   return ss.str();
     59 }
     60 
     61 template <typename T>
     62 void cpp_define(const std::string& name, T value) {
     63   std::cout << "#define " << name << " " << pretty_format(value) << std::endl;
     64 }
     65 
     66 template <typename T>
     67 void emit_check_eq(T value, const std::string& expr) {
     68   std::cout << "DEFINE_CHECK_EQ(" << value << ", (" << expr << "))" << std::endl;
     69 }
     70 
     71 const char *kFileHeader = /* // NOLINT [readability/multiline_string] [5] */ R"L1C3NS3(
     72 /*
     73  * Copyright (C) 2016 The Android Open Source Project
     74  *
     75  * Licensed under the Apache License, Version 2.0 (the "License");
     76  * you may not use this file except in compliance with the License.
     77  * You may obtain a copy of the License at
     78  *
     79  *      http://www.apache.org/licenses/LICENSE-2.0
     80  *
     81  * Unless required by applicable law or agreed to in writing, software
     82  * distributed under the License is distributed on an "AS IS" BASIS,
     83  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     84  * See the License for the specific language governing permissions and
     85  * limitations under the License.
     86  */
     87 
     88 #ifndef ART_RUNTIME_GENERATED_ASM_SUPPORT_GEN_H_
     89 #define ART_RUNTIME_GENERATED_ASM_SUPPORT_GEN_H_
     90 
     91 // This file has been auto-generated by cpp-define-generator; do not edit directly.
     92 )L1C3NS3";  // NOLINT [readability/multiline_string] [5]
     93 
     94 const char *kFileFooter = /* // NOLINT [readability/multiline_string] [5] */ R"F00T3R(
     95 #endif  // ART_RUNTIME_GENERATED_ASM_SUPPORT_GEN_H_
     96 )F00T3R";  // NOLINT [readability/multiline_string] [5]
     97 
     98 #define MACROIZE(holder_type, field_name) to_upper(#holder_type "_" #field_name "_OFFSET")
     99 
    100 int main() {
    101   std::cout << kFileHeader << std::endl;
    102 
    103   std::string z = "";
    104 
    105   // Print every constant expression to stdout as a #define or a CHECK_EQ
    106 #define DEFINE_EXPR(macro_name, field_type, expr) \
    107   cpp_define(to_upper(#macro_name), static_cast<field_type>(expr)); \
    108   emit_check_eq(z + "static_cast<" #field_type ">(" + to_upper(#macro_name) + ")", \
    109                 "static_cast<" #field_type ">(" #expr ")");
    110 #include "offsets_all.def"
    111 
    112   std::cout << kFileFooter << std::endl;
    113   return 0;
    114 }
    115