Skip to content

Commit

Permalink
run formatters and linters
Browse files Browse the repository at this point in the history
  • Loading branch information
rkaminsk committed Sep 28, 2023
1 parent 7c61308 commit dee275e
Show file tree
Hide file tree
Showing 23 changed files with 1,234 additions and 1,751 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ repos:
rev: v1.3.5
hooks:
- id: clang-format
exclude: _clingodl.c
exclude: _clingolpx.c
args: ["-i"]
- id: clang-tidy
exclude: _clingodl.c
exclude: _clingolpx.c
args: ["-fix"]
10 changes: 5 additions & 5 deletions app/lib/clingo-lpx-app/app.hh
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,27 @@
#ifndef CLINGOLPX_APP_HH
#define CLINGOLPX_APP_HH

#include <clingo.hh>
#include <clingo-lpx.h>
#include <clingo.hh>
#include <optional>

namespace ClingoLPX {

//! Helper class to rewrite logic programs to use with the clingo DL theory.
class Rewriter {
public:
public:
Rewriter(clingolpx_theory_t *theory, clingo_program_builder_t *builder);
//! Rewrite the given files.
void rewrite(Clingo::Control &control, Clingo::StringSpan files);
//! Rewrite the given program.
void rewrite(Clingo::Control &control, char const *str);

private:
private:
//! C callback to add a statement using the builder.
static bool add_(clingo_ast_t *stm, void *data);
static auto add_(clingo_ast_t *stm, void *data) -> bool;

//! C callback to rewrite a statement and add it via the builder.
static bool rewrite_(clingo_ast_t *stm, void *data);
static auto rewrite_(clingo_ast_t *stm, void *data) -> bool;

clingolpx_theory_t *theory_; //!< A theory handle to rewrite statements.
clingo_program_builder_t *builder_; //!< The builder to add rewritten statements to.
Expand Down
17 changes: 8 additions & 9 deletions app/lib/src/app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,31 @@
#include <clingo-lpx-app/app.hh>

#include <clingo.hh>
#include <cmath>
#include <iostream>
#include <limits>
#include <cmath>

namespace ClingoLPX {

Rewriter::Rewriter(clingolpx_theory_t *theory, clingo_program_builder_t *builder)
: theory_{theory}
, builder_{builder} {
}
: theory_{theory}, builder_{builder} {}

void Rewriter::rewrite(Clingo::Control &control, Clingo::StringSpan files) {
Clingo::Detail::handle_error(clingo_ast_parse_files(files.begin(), files.size(), rewrite_, this, control.to_c(), nullptr, nullptr, 0));
Clingo::Detail::handle_error(
clingo_ast_parse_files(files.begin(), files.size(), rewrite_, this, control.to_c(), nullptr, nullptr, 0));
}

void Rewriter::rewrite(Clingo::Control &control, char const *str) {
Clingo::Detail::handle_error(clingo_ast_parse_string(str, rewrite_, this, control.to_c(), nullptr, nullptr, 0));
}

bool Rewriter::add_(clingo_ast_t *stm, void *data) {
auto *self = static_cast<Rewriter*>(data);
auto Rewriter::add_(clingo_ast_t *stm, void *data) -> bool {
auto *self = static_cast<Rewriter *>(data);
return clingo_program_builder_add(self->builder_, stm);
}

bool Rewriter::rewrite_(clingo_ast_t *stm, void *data) {
auto *self = static_cast<Rewriter*>(data);
auto Rewriter::rewrite_(clingo_ast_t *stm, void *data) -> bool {
auto *self = static_cast<Rewriter *>(data);
return clingolpx_rewrite_ast(self->theory_, stm, add_, self);
}

Expand Down
62 changes: 24 additions & 38 deletions app/src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@

// }}}

#include <clingo.hh>
#include <clingo-lpx.h>
#include <clingo-lpx-app/app.hh>
#include <optional>
#include <sstream>
#include <clingo-lpx.h>
#include <clingo.hh>
#include <fstream>
#include <limits>
#include <optional>
#include <sstream>
#ifdef CLINGOLPX_PROFILE
#include <gperftools/profiler.h>
#endif
Expand All @@ -40,13 +40,9 @@ namespace ClingoLPX {
namespace {

class Profiler {
public:
Profiler(char const *path) {
ProfilerStart(path);
}
~Profiler() {
ProfilerStop();
}
public:
Profiler(char const *path) { ProfilerStart(path); }
~Profiler() { ProfilerStop(); }
};

} // namespace
Expand All @@ -57,25 +53,17 @@ using Clingo::Detail::handle_error;

//! Application class to run clingo-lpx.
class App : public Clingo::Application, private Clingo::SolveEventHandler {
public:
App() {
handle_error(clingolpx_create(&theory_));
}
public:
App() { handle_error(clingolpx_create(&theory_)); }
App(App const &) = default;
App(App &&) = default;
App &operator=(App const &) = default;
App &operator=(App &&) = default;
~App() override {
clingolpx_destroy(theory_);
}
auto operator=(App const &) -> App & = default;
auto operator=(App &&) -> App & = default;
~App() override { clingolpx_destroy(theory_); }
//! Set program name to clingo-lpx.
[[nodiscard]] char const *program_name() const noexcept override {
return "clingo-lpx";
}
[[nodiscard]] auto program_name() const noexcept -> char const * override { return "clingo-lpx"; }
//! Set the version.
[[nodiscard]] char const *version() const noexcept override {
return CLINGOLPX_VERSION;
}
[[nodiscard]] auto version() const noexcept -> char const * override { return CLINGOLPX_VERSION; }
void print_model(Clingo::Model const &model, std::function<void()> default_printer) noexcept override {
static_cast<void>(default_printer);
try {
Expand All @@ -102,23 +90,23 @@ class App : public Clingo::Application, private Clingo::SolveEventHandler {
auto args = sym.arguments();
std::cout << args.front() << "=" << args.back().string();
comma = true;
}
else if (sym.match("__lpx_objective", 2) && sym.arguments().front().type() == Clingo::SymbolType::String && sym.arguments().back().type() == Clingo::SymbolType::Number) {
} else if (sym.match("__lpx_objective", 2) &&
sym.arguments().front().type() == Clingo::SymbolType::String &&
sym.arguments().back().type() == Clingo::SymbolType::Number) {
auto args = sym.arguments();
objective = std::make_pair(args.front(), args.back() == Clingo::Number(1));

}
}
if (objective.has_value()) {
std::cout << "\nOptimization: " << objective->first.string() << " [" << (objective->second ? "bounded" : "unbounded") << "]";
std::cout << "\nOptimization: " << objective->first.string() << " ["
<< (objective->second ? "bounded" : "unbounded") << "]";
}
std::cout << std::endl;
}
catch(...) {
} catch (...) {
}
}
//! Pass models to the theory.
bool on_model(Clingo::Model &model) override {
auto on_model(Clingo::Model &model) -> bool override {
handle_error(clingolpx_on_model(theory_, model.to_c()));
return true;
}
Expand Down Expand Up @@ -146,18 +134,16 @@ class App : public Clingo::Application, private Clingo::SolveEventHandler {
handle_error(clingolpx_register_options(theory_, options.to_c()));
}
//! Validate options of the theory.
void validate_options() override {
handle_error(clingolpx_validate_options(theory_));
}
void validate_options() override { handle_error(clingolpx_validate_options(theory_)); }

private:
private:
clingolpx_theory_t *theory_{nullptr}; //!< The underlying DL theory.
};

} // namespace ClingoLPX

//! Run the clingo-lpx application.
int main(int argc, char *argv[]) { // NOLINT(bugprone-exception-escape)
auto main(int argc, char *argv[]) -> int { // NOLINT(bugprone-exception-escape)
ClingoLPX::App app;
return Clingo::clingo_main(app, {argv + 1, static_cast<size_t>(argc - 1)});
}
2 changes: 1 addition & 1 deletion cmake/shared_mutex.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <shared_mutex>

int main() {
auto main() -> int {
std::shared_mutex mut;
std::shared_lock<std::shared_mutex> lock{mut};
}
73 changes: 42 additions & 31 deletions libclingo-lpx/clingo-lpx.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#ifndef CLINGOLPX_H
#define CLINGOLPX_H

#include <clingo.h>

//! Major version number.
#define CLINGOLPX_VERSION_MAJOR 1
//! Minor version number.
Expand All @@ -39,31 +41,31 @@ extern "C" {
#endif

#if defined _WIN32 || defined __CYGWIN__
# define CLINGOLPX_WIN
#define CLINGOLPX_WIN
#endif
#ifdef CLINGOLPX_NO_VISIBILITY
# define CLINGOLPX_VISIBILITY_DEFAULT
# define CLINGOLPX_VISIBILITY_PRIVATE
#define CLINGOLPX_VISIBILITY_DEFAULT
#define CLINGOLPX_VISIBILITY_PRIVATE
#else
#ifdef CLINGOLPX_WIN
#ifdef CLINGOLPX_BUILD_LIBRARY
#define CLINGOLPX_VISIBILITY_DEFAULT __declspec(dllexport)
#else
#define CLINGOLPX_VISIBILITY_DEFAULT __declspec(dllimport)
#endif
#define CLINGOLPX_VISIBILITY_PRIVATE
#else
# ifdef CLINGOLPX_WIN
# ifdef CLINGOLPX_BUILD_LIBRARY
# define CLINGOLPX_VISIBILITY_DEFAULT __declspec (dllexport)
# else
# define CLINGOLPX_VISIBILITY_DEFAULT __declspec (dllimport)
# endif
# define CLINGOLPX_VISIBILITY_PRIVATE
# else
# if __GNUC__ >= 4
# define CLINGOLPX_VISIBILITY_DEFAULT __attribute__ ((visibility ("default")))
# define CLINGOLPX_VISIBILITY_PRIVATE __attribute__ ((visibility ("hidden")))
# else
# define CLINGOLPX_VISIBILITY_DEFAULT
# define CLINGOLPX_VISIBILITY_PRIVATE
# endif
# endif
#if __GNUC__ >= 4
#define CLINGOLPX_VISIBILITY_DEFAULT __attribute__((visibility("default")))
#define CLINGOLPX_VISIBILITY_PRIVATE __attribute__((visibility("hidden")))
#else
#define CLINGOLPX_VISIBILITY_DEFAULT
#define CLINGOLPX_VISIBILITY_PRIVATE
#endif
#endif
#endif

#include <clingo.h>
// NOLINTBEGIN(modernize-use-using,modernize-use-trailing-return-type)

enum clingolpx_value_type {
clingolpx_value_type_int = 0,
Expand Down Expand Up @@ -93,13 +95,14 @@ CLINGOLPX_VISIBILITY_DEFAULT void clingolpx_version(int *major, int *minor, int
CLINGOLPX_VISIBILITY_DEFAULT bool clingolpx_create(clingolpx_theory_t **theory);

//! registers the theory with the control
CLINGOLPX_VISIBILITY_DEFAULT bool clingolpx_register(clingolpx_theory_t *theory, clingo_control_t* control);
CLINGOLPX_VISIBILITY_DEFAULT bool clingolpx_register(clingolpx_theory_t *theory, clingo_control_t *control);

//! Rewrite asts before adding them via the given callback.
CLINGOLPX_VISIBILITY_DEFAULT bool clingolpx_rewrite_ast(clingolpx_theory_t *theory, clingo_ast_t *ast, clingolpx_ast_callback_t add, void *data);
CLINGOLPX_VISIBILITY_DEFAULT bool clingolpx_rewrite_ast(clingolpx_theory_t *theory, clingo_ast_t *ast,
clingolpx_ast_callback_t add, void *data);

//! prepare the theory between grounding and solving
CLINGOLPX_VISIBILITY_DEFAULT bool clingolpx_prepare(clingolpx_theory_t *theory, clingo_control_t* control);
CLINGOLPX_VISIBILITY_DEFAULT bool clingolpx_prepare(clingolpx_theory_t *theory, clingo_control_t *control);

//! destroys the theory, currently no way to unregister a theory
CLINGOLPX_VISIBILITY_DEFAULT bool clingolpx_destroy(clingolpx_theory_t *theory);
Expand All @@ -109,43 +112,51 @@ CLINGOLPX_VISIBILITY_DEFAULT bool clingolpx_destroy(clingolpx_theory_t *theory);
CLINGOLPX_VISIBILITY_DEFAULT bool clingolpx_configure(clingolpx_theory_t *theory, char const *key, char const *value);

//! add options for your theory
CLINGOLPX_VISIBILITY_DEFAULT bool clingolpx_register_options(clingolpx_theory_t *theory, clingo_options_t* options);
CLINGOLPX_VISIBILITY_DEFAULT bool clingolpx_register_options(clingolpx_theory_t *theory, clingo_options_t *options);

//! validate options for your theory
CLINGOLPX_VISIBILITY_DEFAULT bool clingolpx_validate_options(clingolpx_theory_t *theory);

//! callback on every model
CLINGOLPX_VISIBILITY_DEFAULT bool clingolpx_on_model(clingolpx_theory_t *theory, clingo_model_t* model);
CLINGOLPX_VISIBILITY_DEFAULT bool clingolpx_on_model(clingolpx_theory_t *theory, clingo_model_t *model);

//! obtain a symbol index which can be used to get the value of a symbol
//! returns true if the symbol exists
//! does not throw
CLINGOLPX_VISIBILITY_DEFAULT bool clingolpx_lookup_symbol(clingolpx_theory_t *theory, clingo_symbol_t symbol, size_t *index);
CLINGOLPX_VISIBILITY_DEFAULT bool clingolpx_lookup_symbol(clingolpx_theory_t *theory, clingo_symbol_t symbol,
size_t *index);

//! obtain the symbol at the given index
//! does not throw
CLINGOLPX_VISIBILITY_DEFAULT clingo_symbol_t clingolpx_get_symbol(clingolpx_theory_t *theory, size_t index);

//! initialize index so that it can be used with clingolpx_assignment_next
//! does not throw
CLINGOLPX_VISIBILITY_DEFAULT void clingolpx_assignment_begin(clingolpx_theory_t *theory, uint32_t thread_id, size_t *index);
CLINGOLPX_VISIBILITY_DEFAULT void clingolpx_assignment_begin(clingolpx_theory_t *theory, uint32_t thread_id,
size_t *index);

//! move to the next index that has a value
//! returns true if the updated index is valid
//! does not throw
CLINGOLPX_VISIBILITY_DEFAULT bool clingolpx_assignment_next(clingolpx_theory_t *theory, uint32_t thread_id, size_t *index);
CLINGOLPX_VISIBILITY_DEFAULT bool clingolpx_assignment_next(clingolpx_theory_t *theory, uint32_t thread_id,
size_t *index);

//! check if the symbol at the given index has a value
//! does not throw
CLINGOLPX_VISIBILITY_DEFAULT bool clingolpx_assignment_has_value(clingolpx_theory_t *theory, uint32_t thread_id, size_t index);
CLINGOLPX_VISIBILITY_DEFAULT bool clingolpx_assignment_has_value(clingolpx_theory_t *theory, uint32_t thread_id,
size_t index);

//! get the symbol and it's value at the given index
//! does not throw
CLINGOLPX_VISIBILITY_DEFAULT void clingolpx_assignment_get_value(clingolpx_theory_t *theory, uint32_t thread_id, size_t index, clingolpx_value_t *value);
CLINGOLPX_VISIBILITY_DEFAULT void clingolpx_assignment_get_value(clingolpx_theory_t *theory, uint32_t thread_id,
size_t index, clingolpx_value_t *value);

//! callback on statistic updates
/// please add a subkey with the name of your theory
CLINGOLPX_VISIBILITY_DEFAULT bool clingolpx_on_statistics(clingolpx_theory_t *theory, clingo_statistics_t* step, clingo_statistics_t* accu);
CLINGOLPX_VISIBILITY_DEFAULT bool clingolpx_on_statistics(clingolpx_theory_t *theory, clingo_statistics_t *step,
clingo_statistics_t *accu);

// NOLINTEND(modernize-use-using,modernize-use-trailing-return-type)

#ifdef __cplusplus
}
Expand Down
Loading

0 comments on commit dee275e

Please sign in to comment.