Skip to content

Commit

Permalink
chore: introduce IWYU to clean up imports
Browse files Browse the repository at this point in the history
  • Loading branch information
TendTo committed Apr 22, 2024
1 parent 3c00d0a commit 99a6487
Show file tree
Hide file tree
Showing 97 changed files with 436 additions and 261 deletions.
6 changes: 6 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ build:opt --cxxopt=-DNLOG
# Building python bindings
build:pydlinear --disk_cache=~/.cache/dlinear_bazel_pybind
build:pydlinear --//tools:enable_dynamic_build=True

# IWYU
build:iwyu --@bazel_iwyu//:iwyu_mappings=@dlinear//tools:iwyu_mappings
build:iwyu --aspects @bazel_iwyu//bazel/iwyu:iwyu.bzl%iwyu_aspect
build:iwyu --output_groups=report
build:iwyu --build_tag_filters=-no-iwyu
2 changes: 2 additions & 0 deletions CPPLINT.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ filter=-build/include_subdir
filter=-readability/todo
filter=-build/c++11
filter=-runtime/references
filter=-build/include_what_you_use
filter=-build/include_order
19 changes: 18 additions & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,16 @@ bazel_dep(name = "googletest", version = "1.14.0", dev_dependency = True)
bazel_dep(name = "com_github_nelhage_rules_boost", version = "0.0.1")
git_override(
module_name = "com_github_nelhage_rules_boost",
commit = "37043509947dc88627b45c283b974e05ad308bdc",
remote = "https://github.com/TendTo/rules_boost.git",
commit = "37043509947dc88627b45c283b974e05ad308bdc",
)

# Apache License 2.0 | Implementation of IWYU for Bazel
bazel_dep(name = "bazel_iwyu", version = "0.0.1")
git_override(
module_name = "bazel_iwyu",
remote = "https://github.com/TendTo/bazel_iwyu",
commit = "85cbd99369a3ed3f8c31234d2d887026f3e66bbc",
)

# Repository dependencies (see WORKSPACE for more details)
Expand Down Expand Up @@ -128,6 +136,15 @@ http_archive(
url = "https://ftp.gnu.org/gnu/gmp/gmp-6.3.0.tar.xz",
)

http_archive(
name = "com_github_storypku_bazel_iwyu",
sha256 = "aa78c331a2cb139f73f7d74eeb4d5ab29794af82023ef5d6d5194f76b7d37449",
strip_prefix = "bazel_iwyu-0.19.2",
urls = [
"https://github.com/storypku/bazel_iwyu/archive/0.19.2.tar.gz",
],
)

# Extensions

boost_modules = use_extension("@com_github_nelhage_rules_boost//:extensions.bzl", "boost_modules")
Expand Down
10 changes: 6 additions & 4 deletions benchmark/util/BenchArgParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#include "BenchArgParser.h"

#include <dirent.h>
#include <spdlog/fmt/fmt.h>
#include <fmt/fmt.h>

#include <filesystem>

namespace dlinear::benchmark {

Expand Down Expand Up @@ -95,11 +97,11 @@ BenchConfig BenchArgParser::toConfig() const {
void BenchArgParser::validateOptions() {
if (parser_.is_used("file") && parser_.is_used("path"))
DLINEAR_INVALID_ARGUMENT("--path", "cannot be set if file is specified");
if (parser_.is_used("file") && !file_exists(parser_.get<std::string>("file")))
if (parser_.is_used("file") && !std::filesystem::is_regular_file(parser_.get<std::string>("file")))
DLINEAR_INVALID_ARGUMENT("--file", fmt::format("file {} does not exist", parser_.get<std::string>("file")));
if (!parser_.is_used("file") && !dir_exists(parser_.get<std::string>("path")))
if (!parser_.is_used("file") && !std::filesystem::is_directory(parser_.get<std::string>("path")))
DLINEAR_INVALID_ARGUMENT("--path", fmt::format("directory {} does not exist", parser_.get<std::string>("path")));
if (!file_exists(parser_.get<std::string>("config")))
if (!std::filesystem::is_regular_file(parser_.get<std::string>("config")))
DLINEAR_INVALID_ARGUMENT("--config", fmt::format("file {} does not exist", parser_.get<std::string>("config")));
if (parser_.get<int>("simplex-sat-phase") != 1 && parser_.get<int>("simplex-sat-phase") != 2)
DLINEAR_INVALID_ARGUMENT("--simplex-sat-phase", fmt::format("value must be either 1 or 2, received '{}'",
Expand Down
2 changes: 1 addition & 1 deletion dlinear/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("//tools:dlinear.bzl", "DLINEAR_NAME", "DLINEAR_VERSION", "dlinear_cc_binary", "dlinear_cc_library")
load("//tools:cpplint.bzl", "cpplint")
load("//tools:dlinear.bzl", "DLINEAR_NAME", "DLINEAR_VERSION", "dlinear_cc_binary", "dlinear_cc_library")

package(default_visibility = ["//visibility:public"])

Expand Down
1 change: 1 addition & 0 deletions dlinear/libs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ dlinear_cc_library(
name = "qsopt_ex",
srcs = ["libqsopt_ex.cpp"],
hdrs = ["libqsopt_ex.h"],
tags = ["no-iwyu"],
deps = [
"@gmp//:gmpxx",
] + select({
Expand Down
2 changes: 0 additions & 2 deletions dlinear/libs/libgmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

#include "libgmp.h"

#include <functional>

namespace std {

// https://en.cppreference.com/w/cpp/utility/hash/operator()
Expand Down
9 changes: 7 additions & 2 deletions dlinear/libs/libgmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@
*/
#pragma once

#include <gmpxx.h>
#include <gmp.h> // IWYU pragma: export
#include <gmpxx.h> // IWYU pragma: export

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring>
#include <string>
#include <string_view>

#include "dlinear/util/exception.h"
#include "dlinear/util/logging.h"
Expand Down Expand Up @@ -104,7 +109,7 @@ inline mpq_class &to_mpq_class(mpq_t &mpq) { return reinterpret_cast<mpq_class &
* @return True if the char is a digit or a plus/minus sign.
* @return False otherwise.
*/
inline bool is_digit_or_sign(char c) { return ::isdigit(c) || c == '+' || c == '-'; }
inline bool is_digit_or_sign(char c) { return std::isdigit(c) || c == '+' || c == '-'; }

/**
* Convert a string to a mpq_class.
Expand Down
2 changes: 1 addition & 1 deletion dlinear/libs/libqsopt_ex.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <gmpxx.h>

extern "C" {
#include <qsopt_ex/QSopt_ex.h>
#include <qsopt_ex/QSopt_ex.h> // IWYU pragma: export
}

#include <string>
Expand Down
2 changes: 1 addition & 1 deletion dlinear/libs/libsoplex.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "dlinear/util/logging.h"
// This import is unfortunately necessary to avoid a fmt clashes between spdlog and soplex.

#include <soplex.h> // NOLINT
#include <soplex.h> // IWYU pragma: export

OSTREAM_FORMATTER(soplex::VectorRational)
OSTREAM_FORMATTER(soplex::Rational)
Expand Down
12 changes: 1 addition & 11 deletions dlinear/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,14 @@
*
* Use the @verbatim-h @endverbatim flag to show the help tooltip.
*/
#include <csignal>
#include <iostream>

#include "dlinear/solver/SmtSolver.h"
#include "dlinear/solver/SmtSolverOutput.h"
#include "dlinear/util/ArgParser.h"
#include "dlinear/util/Config.h"

namespace {
void HandleSigInt(const int) {
// Properly exit so that we can see stat information produced by destructors
// even if a user press C-c.
std::exit(EXIT_FAILURE);
}
} // namespace

int main(int argc, const char* argv[]) {
// Handle C-c.
std::signal(SIGINT, HandleSigInt);
// Initialize the command line parser.
dlinear::ArgParser parser{};
// Parse the command line arguments.
Expand Down
10 changes: 9 additions & 1 deletion dlinear/parser/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@
*/
#include "Driver.h"

#include <sstream>
#include <iostream>
#include <sstream> // IWYU pragma: keep for std::stringstream
#include <vector>

#include "dlinear/libs/libgmp.h"
#include "dlinear/solver/SmtSolverOutput.h"
#include "dlinear/symbolic/PrefixPrinter.h"
#include "dlinear/util/Box.h"
#include "dlinear/util/Config.h"
#include "dlinear/util/ScopedVector.hpp"
#include "dlinear/util/exception.h"

namespace dlinear {

Expand Down
4 changes: 4 additions & 0 deletions dlinear/parser/Driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
*/
#pragma once

#include <iosfwd>
#include <string>

#include "dlinear/solver/Context.h"
#include "dlinear/solver/Logic.h"
#include "dlinear/symbolic/symbolic.h"
#include "dlinear/util/Stats.h"
#include "dlinear/util/Timer.h"

namespace dlinear {

Expand Down
1 change: 1 addition & 0 deletions dlinear/parser/mps/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ dlinear_cc_library(
"//tools:dynamic_build": ["DLINEAR_PYDLINEAR"],
"//conditions:default": [],
}),
tags = ["no-iwyu"],
deps = [
":mps_data",
"//dlinear/parser:driver",
Expand Down
40 changes: 29 additions & 11 deletions dlinear/parser/mps/BoundType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#include "BoundType.h"

#include <cstring>
#include <iostream>

#include "dlinear/util/exception.h"
Expand All @@ -18,16 +17,35 @@ BoundType ParseBoundType(const std::string& bound_type) { return ParseBoundType(

BoundType ParseBoundType(const char bound_type[]) {
while (*bound_type == ' ') ++bound_type;
if (!strncasecmp(bound_type, "LO", 2)) return BoundType::LO;
if (!strncasecmp(bound_type, "LI", 2)) return BoundType::LI;
if (!strncasecmp(bound_type, "UP", 2)) return BoundType::UP;
if (!strncasecmp(bound_type, "UI", 2)) return BoundType::UI;
if (!strncasecmp(bound_type, "FX", 2)) return BoundType::FX;
if (!strncasecmp(bound_type, "FR", 2)) return BoundType::FR;
if (!strncasecmp(bound_type, "MI", 2)) return BoundType::MI;
if (!strncasecmp(bound_type, "PL", 2)) return BoundType::PL;
if (!strncasecmp(bound_type, "BV", 2)) return BoundType::BV;
DLINEAR_UNREACHABLE();
if (bound_type[2] != '\0' && bound_type[2] != ' ') DLINEAR_RUNTIME_ERROR_FMT("Invalid bound type: '{}'", bound_type);
if ((bound_type[0] == 'l' || bound_type[0] == 'L') && (bound_type[1] == 'o' || bound_type[1] == 'O')) {
return BoundType::LO;
}
if ((bound_type[0] == 'l' || bound_type[0] == 'L') && (bound_type[1] == 'i' || bound_type[1] == 'I')) {
return BoundType::LI;
}
if ((bound_type[0] == 'u' || bound_type[0] == 'U') && (bound_type[1] == 'p' || bound_type[1] == 'P')) {
return BoundType::UP;
}
if ((bound_type[0] == 'u' || bound_type[0] == 'U') && (bound_type[1] == 'i' || bound_type[1] == 'I')) {
return BoundType::UI;
}
if ((bound_type[0] == 'f' || bound_type[0] == 'F') && (bound_type[1] == 'x' || bound_type[1] == 'X')) {
return BoundType::FX;
}
if ((bound_type[0] == 'f' || bound_type[0] == 'F') && (bound_type[1] == 'r' || bound_type[1] == 'R')) {
return BoundType::FR;
}
if ((bound_type[0] == 'm' || bound_type[0] == 'M') && (bound_type[1] == 'i' || bound_type[1] == 'I')) {
return BoundType::MI;
}
if ((bound_type[0] == 'p' || bound_type[0] == 'P') && (bound_type[1] == 'l' || bound_type[1] == 'L')) {
return BoundType::PL;
}
if ((bound_type[0] == 'b' || bound_type[0] == 'B') && (bound_type[1] == 'v' || bound_type[1] == 'V')) {
return BoundType::BV;
}
DLINEAR_RUNTIME_ERROR_FMT("Invalid bound type: '{}'", bound_type);
}

std::ostream& operator<<(std::ostream& os, const BoundType& bound) {
Expand Down
1 change: 1 addition & 0 deletions dlinear/parser/mps/BoundType.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
#pragma once

#include <iosfwd>
#include <string>

#include "dlinear/util/logging.h"
Expand Down
7 changes: 4 additions & 3 deletions dlinear/parser/mps/Sense.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,24 @@
*/
#include "dlinear/parser/mps/Sense.h"

#include <cstring>
#include <cctype>
#include <cstddef>
#include <iostream>

#include "dlinear/util/exception.h"

namespace dlinear::mps {

Sense ParseSense(const std::string &sense) {
size_t pos = sense.find_first_not_of(' ');
std::size_t pos = sense.find_first_not_of(' ');
return ParseSense(sense[pos]);
}
Sense ParseSense(const char sense[]) {
while (*sense == ' ') ++sense;
return ParseSense(*sense);
}
Sense ParseSense(char sense) {
sense = tolower(sense);
sense = std::tolower(sense);
switch (sense) {
case 'l':
return Sense::L;
Expand Down
1 change: 1 addition & 0 deletions dlinear/parser/mps/Sense.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
#pragma once

#include <iosfwd>
#include <string>

#include "dlinear/util/logging.h"
Expand Down
2 changes: 2 additions & 0 deletions dlinear/parser/mps/scanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*/
#pragma once

#include <iosfwd>

#ifndef __DLINEAR_MPS_SCANNER_H__
#define yyFlexLexer MpsFlexLexer
#include <FlexLexer.h>
Expand Down
1 change: 1 addition & 0 deletions dlinear/parser/smt2/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ dlinear_cc_library(
"//tools:dynamic_build": ["DLINEAR_PYDLINEAR"],
"//conditions:default": [],
}),
tags = ["no-iwyu"],
deps = [
":function_definition",
":sort",
Expand Down
2 changes: 1 addition & 1 deletion dlinear/parser/smt2/FunctionDefinition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Term FunctionDefinition::operator()(const std::vector<Term>& arguments) const {
}
Term t = body_;

for (size_t i = 0; i < parameters_.size(); ++i) {
for (std::size_t i = 0; i < parameters_.size(); ++i) {
const Variable& param_i{parameters_[i]};
const Term& arg_i{arguments[i]};
arg_i.Check(param_i.get_type());
Expand Down
4 changes: 3 additions & 1 deletion dlinear/parser/smt2/FunctionDefinition.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
#pragma once

#include <concepts>
#include <cstddef>
#include <vector>

#include "dlinear/parser/smt2/Sort.h"
#include "dlinear/parser/smt2/Term.h"
#include "dlinear/symbolic/literal.h"
#include "dlinear/util/exception.h"

namespace dlinear::smt2 {

Expand Down Expand Up @@ -66,7 +68,7 @@ class FunctionDefinition {
}
Term t = body_;

size_t i = 0;
std::size_t i = 0;
for (const Term& arg_i : {arguments...}) {
const Variable& param_i{parameters_[i]};
arg_i.Check(param_i.get_type());
Expand Down
2 changes: 1 addition & 1 deletion dlinear/parser/smt2/Sort.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <ostream>
#include <string>

#include "dlinear/symbolic/symbolic.h"
#include "dlinear/symbolic/literal.h"
#include "dlinear/util/logging.h"

namespace dlinear::smt2 {
Expand Down
3 changes: 0 additions & 3 deletions dlinear/parser/smt2/Term.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
*/
#include "Term.h"

#include <stdexcept>
#include <utility>

#include "dlinear/util/exception.h"
#include "dlinear/util/logging.h"

Expand Down
1 change: 1 addition & 0 deletions dlinear/parser/smt2/Term.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <variant>

#include "dlinear/parser/smt2/Sort.h"
#include "dlinear/symbolic/literal.h"
#include "dlinear/symbolic/symbolic.h"
#include "dlinear/util/logging.h"

Expand Down
4 changes: 2 additions & 2 deletions dlinear/parser/smt2/parser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,11 @@ command_define_fun: '(' TK_DEFINE_FUN SYMBOL enter_scope '(' name_sort_list ')'
}
|
'(' TK_DEFINE_FUN TK_MAX enter_scope '(' name_sort_list ')' sort term exit_scope ')' {
DLINEAR_WARN_FMT("Redefinition of function `max` is ignored");
DLINEAR_WARN("Redefinition of function `max` is ignored");
}
|
'(' TK_DEFINE_FUN TK_MIN enter_scope '(' name_sort_list ')' sort term exit_scope ')' {
DLINEAR_WARN_FMT("Redefinition of function `min` is ignored");
DLINEAR_WARN("Redefinition of function `min` is ignored");
}
;

Expand Down
Loading

0 comments on commit 99a6487

Please sign in to comment.