1 //==-- proto_to_cxx.cpp - Protobuf-C++ conversion --------------------------==//
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
7 //===----------------------------------------------------------------------===//
9 // Implements functions for converting between protobufs and C++.
11 //===----------------------------------------------------------------------===//
13 #include "proto_to_cxx.h"
14 #include "cxx_proto.pb.h"
19 namespace clang_fuzzer
{
22 std::ostream
&operator<<(std::ostream
&os
, const BinaryOp
&x
);
23 std::ostream
&operator<<(std::ostream
&os
, const StatementSeq
&x
);
26 std::ostream
&operator<<(std::ostream
&os
, const Const
&x
) {
27 return os
<< "(" << x
.val() << ")";
29 std::ostream
&operator<<(std::ostream
&os
, const VarRef
&x
) {
30 return os
<< "a[" << (static_cast<uint32_t>(x
.varnum()) % 100) << "]";
32 std::ostream
&operator<<(std::ostream
&os
, const Lvalue
&x
) {
33 return os
<< x
.varref();
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();
41 std::ostream
&operator<<(std::ostream
&os
, const BinaryOp
&x
) {
42 os
<< "(" << x
.left();
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;
59 return os
<< x
.right() << ")";
61 std::ostream
&operator<<(std::ostream
&os
, const AssignmentStatement
&x
) {
62 return os
<< x
.lvalue() << "=" << x
.rvalue() << ";\n";
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";
69 std::ostream
&operator<<(std::ostream
&os
, const While
&x
) {
70 return os
<< "while (" << x
.cond() << "){\n" << x
.body() << "}\n";
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";
78 std::ostream
&operator<<(std::ostream
&os
, const StatementSeq
&x
) {
79 for (auto &st
: x
.statements()) os
<< st
;
82 std::ostream
&operator<<(std::ostream
&os
, const Function
&x
) {
83 return os
<< "void foo(int *a) {\n" << x
.statements() << "}\n";
86 // ---------------------------------
88 std::string
FunctionToString(const Function
&input
) {
89 std::ostringstream os
;
94 std::string
ProtoToCxx(const uint8_t *data
, size_t size
) {
96 if (!message
.ParsePartialFromArray(data
, size
))
97 return "#error invalid proto\n";
98 return FunctionToString(message
);
101 } // namespace clang_fuzzer