Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove Global singleton #2360

Merged
merged 1 commit into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmake/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def compare_files(expected_file, actual_file):

if actual_lines != expected_lines:
os.sys.stdout.writelines(difflib.unified_diff(open(expected_file).readlines(), open(actual_file).readlines(), fromfile=expected_file, tofile=actual_file))
os.sys.stdout.write("\n")
os.sys.exit("Found output difference, expected file:'{}', actual file:'{}".format(expected_file, actual_file))

return True
Expand Down
5 changes: 5 additions & 0 deletions cmake/redirect.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@
if stderr:
stderr.close()

if status.returncode != 0 and args.err_file:
with open(args.err_file, "r") as f:
os.sys.stderr.write(f.read())


os.sys.exit(status.returncode)
6 changes: 3 additions & 3 deletions src/Global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ void MainConfig::processArgs(int argc, char** argv, const std::string& header, c
while ((c = getopt_long(argc, argv, shortNames.c_str(), longNames.get(), nullptr)) != -1) {
// case for the unknown option
if (c == '?') {
std::cerr << Global::config().help();
std::cerr << help();
throw std::runtime_error("Error: Unknown command line option.");
}
// obtain an iterator to the option in the table referenced by the current short name
Expand Down Expand Up @@ -217,11 +217,11 @@ void MainConfig::processArgs(int argc, char** argv, const std::string& header, c
}

// obtain the name of the datalog file, and store it in the option with the empty key
if (argc > 1 && !Global::config().has("help") && !Global::config().has("version")) {
if (argc > 1 && !has("help") && !has("version")) {
std::string filename = "";
// ensure that the optind is less than the total number of arguments
if (argc > 1 && optind >= argc) {
std::cerr << Global::config().help();
std::cerr << help();
throw std::runtime_error("Error: Missing source file path.");
}

Expand Down
11 changes: 7 additions & 4 deletions src/Global.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,21 @@ class MainConfig {
* used to isolate all globals. */
class Global {
public:
Global() = default;
/* Deleted copy constructor. */
Global(const Global&) = delete;
/* Deleted assignment operator. */
Global& operator=(const Global&) = delete;
/* Obtain the global configuration. */
static MainConfig& config() {
static MainConfig _config;
MainConfig& config() {
return _config;
}

const MainConfig& config() const {
return _config;
}

private:
/* Private empty constructor, there is only one global instance. */
Global() = default;
MainConfig _config;
};
} // namespace souffle
12 changes: 10 additions & 2 deletions src/TranslationUnitBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#pragma once

#include "Global.h"
#include "reports/DebugReport.h"
#include "reports/ErrorReport.h"
#include "souffle/utility/DynamicCasting.h"
Expand Down Expand Up @@ -72,8 +73,8 @@ struct TranslationUnitBase {
virtual void run(Impl const&) = 0;
};

TranslationUnitBase(Own<Program> prog, ErrorReport& e, DebugReport& d)
: program(std::move(prog)), errorReport(e), debugReport(d) {
TranslationUnitBase(Global& g, Own<Program> prog, ErrorReport& e, DebugReport& d)
: glb(g), program(std::move(prog)), errorReport(e), debugReport(d) {
assert(program != nullptr && "program is a null-pointer");
}

Expand Down Expand Up @@ -109,6 +110,11 @@ struct TranslationUnitBase {
analyses.clear();
}

/** @brief Get the global configuration */
Global& global() const {
return glb;
}

/** @brief Get the RAM Program of the translation unit */
Program& getProgram() const {
return *program;
Expand All @@ -133,6 +139,8 @@ struct TranslationUnitBase {
// Using `std::string` appears to suppress the issue (bug?).
mutable std::map<std::string, Own<Analysis>> analyses;

Global& glb;

/* RAM program */
Own<Program> program;

Expand Down
1 change: 1 addition & 0 deletions src/ast/Program.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#pragma once

#include "Global.h"
#include "ast/Clause.h"
#include "ast/Component.h"
#include "ast/ComponentInit.h"
Expand Down
3 changes: 2 additions & 1 deletion src/ast/TranslationUnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "ast/TranslationUnit.h"
#include "Global.h"
#include "ast/Program.h"
#include "ast/analysis/PrecedenceGraph.h"
#include "ast/analysis/SCCGraph.h"
#include "reports/DebugReport.h"
Expand All @@ -17,7 +18,7 @@ namespace souffle::ast {

/** get analysis: analysis is generated on the fly if not present */
void TranslationUnit::logAnalysis(Analysis& analysis) const {
if (!Global::config().has("debug-report")) return;
if (!global().config().has("debug-report")) return;

std::string name = analysis.getName();
if (as<analysis::PrecedenceGraphAnalysis>(analysis) || as<analysis::SCCGraphAnalysis>(analysis)) {
Expand Down
3 changes: 1 addition & 2 deletions src/ast/TranslationUnit.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@

#pragma once

#include "Program.h"
#include "TranslationUnitBase.h"

namespace souffle::ast {

class Program;

/**
* @class TranslationUnit
* @brief Translation unit class for the translation pipeline
Expand Down
8 changes: 4 additions & 4 deletions src/ast/analysis/JoinSize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ analysis::StratumJoinSizeEstimates JoinSizeAnalysis::computeRuleVersionStatement
return statements;
}

std::vector<analysis::StratumJoinSizeEstimates> JoinSizeAnalysis::computeJoinSizeStatements() {
std::vector<analysis::StratumJoinSizeEstimates> JoinSizeAnalysis::computeJoinSizeStatements(
const bool emitStatistics) {
auto* prog = program;
auto getSccAtoms = [prog](const ast::Clause* clause, const ast::RelationSet& scc) {
const auto& sccAtoms = filter(ast::getBodyLiterals<ast::Atom>(*clause),
Expand All @@ -133,8 +134,7 @@ std::vector<analysis::StratumJoinSizeEstimates> JoinSizeAnalysis::computeJoinSiz
std::vector<analysis::StratumJoinSizeEstimates> joinSizeStatements;
joinSizeStatements.resize(sccOrdering.size());

auto& config = Global::config();
if (!config.has("emit-statistics")) {
if (!emitStatistics) {
return joinSizeStatements;
}

Expand Down Expand Up @@ -240,7 +240,7 @@ void JoinSizeAnalysis::run(const TranslationUnit& translationUnit) {
topsortSCCGraphAnalysis = &translationUnit.getAnalysis<TopologicallySortedSCCGraphAnalysis>();
recursiveClauses = &translationUnit.getAnalysis<RecursiveClausesAnalysis>();
polyAnalysis = &translationUnit.getAnalysis<ast::analysis::PolymorphicObjectsAnalysis>();
joinSizeStatements = computeJoinSizeStatements();
joinSizeStatements = computeJoinSizeStatements(translationUnit.global().config().has("emit-statistics"));
}

void JoinSizeAnalysis::print(std::ostream& os) const {
Expand Down
2 changes: 1 addition & 1 deletion src/ast/analysis/JoinSize.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class JoinSizeAnalysis : public Analysis {
PolymorphicObjectsAnalysis* polyAnalysis = nullptr;

// for each stratum compute the EstimateJoinSize nodes to emit
std::vector<StratumJoinSizeEstimates> computeJoinSizeStatements();
std::vector<StratumJoinSizeEstimates> computeJoinSizeStatements(bool emitStatistics);
StratumJoinSizeEstimates computeRuleVersionStatements(const RelationSet& sccRelations,
const ast::Clause& clause, std::size_t version,
ast2ram::TranslationMode mode = ast2ram::TranslationMode::DEFAULT);
Expand Down
7 changes: 4 additions & 3 deletions src/ast/analysis/ProfileUse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "ast/analysis/ProfileUse.h"
#include "Global.h"
#include "ast/Program.h"
#include "ast/QualifiedName.h"
#include "souffle/profile/ProgramRun.h"
#include "souffle/profile/Reader.h"
Expand All @@ -29,10 +30,10 @@ namespace souffle::ast::analysis {
/**
* Run analysis, i.e., retrieve profile information
*/
void ProfileUseAnalysis::run(const TranslationUnit&) {
void ProfileUseAnalysis::run(const TranslationUnit& TU) {
std::string filename;
if (Global::config().has("auto-schedule")) {
filename = Global::config().get("auto-schedule");
if (TU.global().config().has("auto-schedule")) {
filename = TU.global().config().get("auto-schedule");
}
reader = mk<profile::Reader>(filename, programRun);
reader->processFile();
Expand Down
3 changes: 2 additions & 1 deletion src/ast/analysis/SCCGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ namespace souffle::ast::analysis {
void SCCGraphAnalysis::run(const TranslationUnit& translationUnit) {
precedenceGraph = &translationUnit.getAnalysis<PrecedenceGraphAnalysis>();
ioType = &translationUnit.getAnalysis<IOTypeAnalysis>();
programName = translationUnit.global().config().get("name");
sccToRelation.clear();
relationToScc.clear();
predecessors.clear();
Expand Down Expand Up @@ -115,7 +116,7 @@ void SCCGraphAnalysis::scR(const Relation* w, std::map<const Relation*, std::siz
}

void SCCGraphAnalysis::printRaw(std::stringstream& ss) const {
const std::string& name = Global::config().get("name");
const std::string& name = programName;
/* Print SCC graph */
ss << "digraph {" << std::endl;
/* Print nodes of SCC graph */
Expand Down
2 changes: 2 additions & 0 deletions src/ast/analysis/SCCGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ class SCCGraphAnalysis : public Analysis {

IOTypeAnalysis* ioType = nullptr;

std::string programName;

/** Print the SCC graph to a string. */
void printRaw(std::stringstream& ss) const;
};
Expand Down
3 changes: 2 additions & 1 deletion src/ast/analysis/typesystem/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,8 @@ bool TypeAnalysis::isSymbol(const Argument* argument) const {
void TypeAnalysis::run(const TranslationUnit& translationUnit) {
// Check if debugging information is being generated
std::ostream* debugStream = nullptr;
if (Global::config().has("debug-report") || Global::config().has("show", "type-analysis")) {
if (translationUnit.global().config().has("debug-report") ||
translationUnit.global().config().has("show", "type-analysis")) {
debugStream = &analysisLogs;
}

Expand Down
10 changes: 6 additions & 4 deletions src/ast/tests/ast_print_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@
namespace souffle::ast::test {

inline Own<TranslationUnit> makeATU(std::string program = ".decl A,B,C(x:number)") {
Global glb;
ErrorReport e;
DebugReport d;
return ParserDriver::parseTranslationUnit(program, e, d);
DebugReport d(glb);
return ParserDriver::parseTranslationUnit(glb, program, e, d);
}

inline Own<TranslationUnit> makePrintedATU(Own<TranslationUnit>& tu) {
Expand Down Expand Up @@ -84,11 +85,12 @@ TEST(AstPrint, NumberConstant) {
}

TEST(AstPrint, StringConstant) {
Global glb;
ErrorReport e;
DebugReport d;
DebugReport d(glb);
auto testArgument = mk<StringConstant>("test string");

auto tu1 = ParserDriver::parseTranslationUnit(".decl A,B,C(x:number)", e, d);
auto tu1 = ParserDriver::parseTranslationUnit(glb, ".decl A,B,C(x:number)", e, d);
tu1->getProgram().addClause(makeClauseA(std::move(testArgument)));
auto tu2 = makePrintedATU(tu1);
EXPECT_EQ(tu1->getProgram(), tu2->getProgram());
Expand Down
33 changes: 18 additions & 15 deletions src/ast/tests/ast_program_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@
namespace souffle::ast::test {

inline Own<TranslationUnit> makeATU(std::string program) {
Global glb;
ErrorReport e;
DebugReport d;
return ParserDriver::parseTranslationUnit(program, e, d);
DebugReport d(glb);
return ParserDriver::parseTranslationUnit(glb, program, e, d);
}

inline Own<Clause> makeClause(std::string name, Own<Argument> headArgument) {
Expand All @@ -56,16 +57,17 @@ inline Own<Clause> makeClause(std::string name, Own<Argument> headArgument) {
}

TEST(Program, Parse) {
Global glb;
ErrorReport e;
DebugReport d;
DebugReport d(glb);
// check the empty program
Own<TranslationUnit> empty = ParserDriver::parseTranslationUnit("", e, d);
Own<TranslationUnit> empty = ParserDriver::parseTranslationUnit(glb, "", e, d);

EXPECT_TRUE(empty->getProgram().getTypes().empty());
EXPECT_TRUE(empty->getProgram().getRelations().empty());

// check something simple
Own<TranslationUnit> tu = ParserDriver::parseTranslationUnit(
Own<TranslationUnit> tu = ParserDriver::parseTranslationUnit(glb,
R"(
.type Node <: symbol
.decl e ( a : Node , b : Node )
Expand All @@ -87,16 +89,17 @@ TEST(Program, Parse) {
EXPECT_FALSE(prog.getRelation("n"));
}

#define TESTASTCLONEANDEQUAL(SUBTYPE, DL) \
TEST(Ast, CloneAndEqual##SUBTYPE) { \
ErrorReport e; \
DebugReport d; \
Own<TranslationUnit> tu = ParserDriver::parseTranslationUnit(DL, e, d); \
Program& program = tu->getProgram(); \
EXPECT_EQ(program, program); \
Own<Program> cl(clone(program)); \
EXPECT_NE(cl.get(), &program); \
EXPECT_EQ(*cl, program); \
#define TESTASTCLONEANDEQUAL(SUBTYPE, DL) \
TEST(Ast, CloneAndEqual##SUBTYPE) { \
Global glb; \
ErrorReport e; \
DebugReport d(glb); \
Own<TranslationUnit> tu = ParserDriver::parseTranslationUnit(glb, DL, e, d); \
Program& program = tu->getProgram(); \
EXPECT_EQ(program, program); \
Own<Program> cl(clone(program)); \
EXPECT_NE(cl.get(), &program); \
EXPECT_EQ(*cl, program); \
}

TESTASTCLONEANDEQUAL(Program,
Expand Down
Loading