1 //==-- proto_to_cxx.cpp - Protobuf-C++ conversion --------------------------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Implements functions for converting between protobufs and C++.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "proto_to_cxx.h"
14 #include "cxx_proto.pb.h"
15 
16 #include <ostream>
17 #include <sstream>
18 
19 namespace clang_fuzzer {
20 
21 // Forward decls.
22 std::ostream &operator<<(std::ostream &os, const BinaryOp &x);
23 std::ostream &operator<<(std::ostream &os, const StatementSeq &x);
24 
25 // Proto to C++.
operator <<(std::ostream & os,const Const & x)26 std::ostream &operator<<(std::ostream &os, const Const &x) {
27   return os << "(" << x.val() << ")";
28 }
operator <<(std::ostream & os,const VarRef & x)29 std::ostream &operator<<(std::ostream &os, const VarRef &x) {
30   return os << "a[" << (static_cast<uint32_t>(x.varnum()) % 100) << "]";
31 }
operator <<(std::ostream & os,const Lvalue & x)32 std::ostream &operator<<(std::ostream &os, const Lvalue &x) {
33   return os << x.varref();
34 }
operator <<(std::ostream & os,const Rvalue & x)35 std::ostream &operator<<(std::ostream &os, const Rvalue &x) {
36     if (x.has_varref()) return os << x.varref();
37     if (x.has_cons())   return os << x.cons();
38     if (x.has_binop())  return os << x.binop();
39     return os << "1";
40 }
operator <<(std::ostream & os,const BinaryOp & x)41 std::ostream &operator<<(std::ostream &os, const BinaryOp &x) {
42   os << "(" << x.left();
43   switch (x.op()) {
44     case BinaryOp::PLUS: os << "+"; break;
45     case BinaryOp::MINUS: os << "-"; break;
46     case BinaryOp::MUL: os << "*"; break;
47     case BinaryOp::DIV: os << "/"; break;
48     case BinaryOp::MOD: os << "%"; break;
49     case BinaryOp::XOR: os << "^"; break;
50     case BinaryOp::AND: os << "&"; break;
51     case BinaryOp::OR: os << "|"; break;
52     case BinaryOp::EQ: os << "=="; break;
53     case BinaryOp::NE: os << "!="; break;
54     case BinaryOp::LE: os << "<="; break;
55     case BinaryOp::GE: os << ">="; break;
56     case BinaryOp::LT: os << "<"; break;
57     case BinaryOp::GT: os << ">"; break;
58   }
59   return os << x.right() << ")";
60 }
operator <<(std::ostream & os,const AssignmentStatement & x)61 std::ostream &operator<<(std::ostream &os, const AssignmentStatement &x) {
62   return os << x.lvalue() << "=" << x.rvalue() << ";\n";
63 }
operator <<(std::ostream & os,const IfElse & x)64 std::ostream &operator<<(std::ostream &os, const IfElse &x) {
65   return os << "if (" << x.cond() << "){\n"
66             << x.if_body() << "} else { \n"
67             << x.else_body() << "}\n";
68 }
operator <<(std::ostream & os,const While & x)69 std::ostream &operator<<(std::ostream &os, const While &x) {
70   return os << "while (" << x.cond() << "){\n" << x.body() << "}\n";
71 }
operator <<(std::ostream & os,const Statement & x)72 std::ostream &operator<<(std::ostream &os, const Statement &x) {
73   if (x.has_assignment()) return os << x.assignment();
74   if (x.has_ifelse())     return os << x.ifelse();
75   if (x.has_while_loop()) return os << x.while_loop();
76   return os << "(void)0;\n";
77 }
operator <<(std::ostream & os,const StatementSeq & x)78 std::ostream &operator<<(std::ostream &os, const StatementSeq &x) {
79   for (auto &st : x.statements()) os << st;
80   return os;
81 }
operator <<(std::ostream & os,const Function & x)82 std::ostream &operator<<(std::ostream &os, const Function &x) {
83   return os << "void foo(int *a) {\n" << x.statements() << "}\n";
84 }
85 
86 // ---------------------------------
87 
FunctionToString(const Function & input)88 std::string FunctionToString(const Function &input) {
89   std::ostringstream os;
90   os << input;
91   return os.str();
92 
93 }
ProtoToCxx(const uint8_t * data,size_t size)94 std::string ProtoToCxx(const uint8_t *data, size_t size) {
95   Function message;
96   if (!message.ParsePartialFromArray(data, size))
97     return "#error invalid proto\n";
98   return FunctionToString(message);
99 }
100 
101 } // namespace clang_fuzzer
102