From 4203ac208bacb2f0b0f19c298897d7833bd90496 Mon Sep 17 00:00:00 2001 From: Andrew Ferraiuolo Date: Tue, 25 Jan 2022 17:31:37 +0000 Subject: [PATCH] linters --- src/ir/auth_logic/BUILD | 2 +- src/ir/auth_logic/souffle_emitter.h | 176 ++++++++++++++-------------- 2 files changed, 86 insertions(+), 92 deletions(-) diff --git a/src/ir/auth_logic/BUILD b/src/ir/auth_logic/BUILD index d64772f2e..37ef4f3a9 100644 --- a/src/ir/auth_logic/BUILD +++ b/src/ir/auth_logic/BUILD @@ -43,8 +43,8 @@ cc_library( ], visibility = ["//test:__pkg__"], deps = [ - "//src/ir/auth_logic:ast", "//src/common/logging", + "//src/ir/auth_logic:ast", "//src/utils:overloaded", "@absl//absl/strings:str_format", ], diff --git a/src/ir/auth_logic/souffle_emitter.h b/src/ir/auth_logic/souffle_emitter.h index 22232823e..5b7adc811 100644 --- a/src/ir/auth_logic/souffle_emitter.h +++ b/src/ir/auth_logic/souffle_emitter.h @@ -25,108 +25,102 @@ namespace raksha::ir::auth_logic { class SouffleEmitter { - public: - std::string EmitProgram(DLIRProgram program) { - SouffleEmitter emitter; - std::string body = emitter.EmitProgramBody(program); - std::string outputs = emitter.EmitOutputs(program); - std::string decls = emitter.EmitDeclarations(); - return absl::StrCat(std::move(body), - "\n", - std::move(outputs), - "\n", - std::move(decls)); - } - - private: - SouffleEmitter() + public: + std::string EmitProgram(DLIRProgram program) { + SouffleEmitter emitter; + std::string body = emitter.EmitProgramBody(program); + std::string outputs = emitter.EmitOutputs(program); + std::string decls = emitter.EmitDeclarations(); + return absl::StrCat(std::move(body), "\n", std::move(outputs), "\n", + std::move(decls)); + } + + private: + SouffleEmitter() : declarations_( - absl::flat_hash_set()) - {}; - - // This function produces a normalized version of predicates to - // be used when generating declarations of the predicates. It replaces - // argument names with generic numbered ones. It is applied - // to predicates before they are added to the set of declarations so that - // there are no duplicate delcarations (which would otherwise happen - // whenever a predicate is referenced more than once with different - // arguments). - // To prevent instances of negated and non-negated uses of the predicate - // from generating two declarations, the sign here is always positive. - Predicate PredToDeclaration(Predicate predicate) { - std::vector decl_args; - for( int i=0; i < predicate.args().size(); i++ ) { - decl_args.push_back("x" + std::to_string(i)); - } - return Predicate(predicate.name(), decl_args, kPositive); - } - - std::string EmitPredicate(Predicate predicate) { - // Whenever a new predicate is encountered, it is added to the set of - // declarations (which does not include duplicates because it is a set). - declarations_.insert(PredToDeclaration(predicate)); - return absl::StrCat( - predicate.sign() == kNegated ? "!" : "", - predicate.name(), - "(", absl::StrJoin(predicate.args(), ", "), ")"); + absl::flat_hash_set()){}; + + // This function produces a normalized version of predicates to + // be used when generating declarations of the predicates. It replaces + // argument names with generic numbered ones. It is applied + // to predicates before they are added to the set of declarations so that + // there are no duplicate delcarations (which would otherwise happen + // whenever a predicate is referenced more than once with different + // arguments). + // To prevent instances of negated and non-negated uses of the predicate + // from generating two declarations, the sign here is always positive. + Predicate PredToDeclaration(Predicate predicate) { + std::vector decl_args; + for (int i = 0; i < predicate.args().size(); i++) { + decl_args.push_back("x" + std::to_string(i)); } - - std::string EmitAssertionInner(Predicate predicate) { - return EmitPredicate(predicate) + "."; + return Predicate(predicate.name(), decl_args, kPositive); + } + + std::string EmitPredicate(Predicate predicate) { + // Whenever a new predicate is encountered, it is added to the set of + // declarations (which does not include duplicates because it is a set). + declarations_.insert(PredToDeclaration(predicate)); + return absl::StrCat(predicate.sign() == kNegated ? "!" : "", + predicate.name(), "(", + absl::StrJoin(predicate.args(), ", "), ")"); + } + + std::string EmitAssertionInner(Predicate predicate) { + return EmitPredicate(predicate) + "."; + } + + std::string EmitAssertionInner(DLIRCondAssertion cond_assertion) { + std::string lhs = EmitPredicate(cond_assertion.lhs()); + std::vector rhs_translated; + for (auto arg : cond_assertion.rhs()) { + rhs_translated.push_back(EmitPredicate(arg)); } - - std::string EmitAssertionInner(DLIRCondAssertion cond_assertion) { - std::string lhs = EmitPredicate(cond_assertion.lhs()); - std::vector rhs_translated; - for(auto arg: cond_assertion.rhs()) { - rhs_translated.push_back(EmitPredicate(arg)); - } - return absl::StrCat(std::move(lhs), " :- ", - absl::StrJoin(rhs_translated, ", "), "."); + return absl::StrCat(std::move(lhs), " :- ", + absl::StrJoin(rhs_translated, ", "), "."); + } + + std::string EmitAssertion(DLIRAssertion assertion) { + return std::visit([this](auto value) { return EmitAssertionInner(value); }, + assertion.GetValue()); + } + + std::string EmitProgramBody(DLIRProgram program) { + std::vector body_translated; + for (auto assertion : program.assertions()) { + body_translated.push_back(EmitAssertion(assertion)); } + return absl::StrJoin(body_translated, "\n"); + } - std::string EmitAssertion(DLIRAssertion assertion) { - return std::visit( - [this](auto value) { return EmitAssertionInner(value); }, - assertion.GetValue()); + std::string EmitOutputs(DLIRProgram program) { + std::string ret; + for (auto out : program.outputs()) { + absl::StrAppend(&ret, absl::StrCat(".output", out, "\n")); } + return ret; + } - std::string EmitProgramBody(DLIRProgram program) { - std::vector body_translated; - for(auto assertion : program.assertions()) { - body_translated.push_back(EmitAssertion(assertion)); - } - return absl::StrJoin(body_translated, "\n"); + std::string EmitDeclaration(Predicate predicate) { + std::vector args_with_types; + for (auto elm : predicate.args()) { + args_with_types.push_back(absl::StrCat(elm, ": symbol")); } - - std::string EmitOutputs(DLIRProgram program) { - std::string ret; - for (auto out : program.outputs()) { - absl::StrAppend(&ret, absl::StrCat(".output", out, "\n")); - } - return ret; - } - - std::string EmitDeclaration(Predicate predicate) { - std::vector args_with_types; - for(auto elm : predicate.args()) { - args_with_types.push_back(absl::StrCat(elm, ": symbol")); - } - return absl::StrCat(".decl ", predicate.name(), - "(", absl::StrJoin(args_with_types, ", "), ")"); - } - - std::string EmitDeclarations() { - std::vector ret; - for(auto decl : declarations_) { - ret.push_back(EmitDeclaration(decl)); - } - return absl::StrJoin(ret, "\n"); + return absl::StrCat(".decl ", predicate.name(), "(", + absl::StrJoin(args_with_types, ", "), ")"); + } + + std::string EmitDeclarations() { + std::vector ret; + for (auto decl : declarations_) { + ret.push_back(EmitDeclaration(decl)); } + return absl::StrJoin(ret, "\n"); + } - absl::flat_hash_set declarations_; + absl::flat_hash_set declarations_; }; } // namespace raksha::ir::auth_logic -#endif //SRC_IR_AUTH_LOGIC_SOUFFLE_EMITTER_H_ +#endif // SRC_IR_AUTH_LOGIC_SOUFFLE_EMITTER_H_