Skip to content

Commit 04ff3b7

Browse files
committed
remove Global singleton
1 parent b8b1d69 commit 04ff3b7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+511
-367
lines changed

cmake/common.py

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def compare_files(expected_file, actual_file):
4545

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

5051
return True

cmake/redirect.py

+5
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,9 @@
4141
if stderr:
4242
stderr.close()
4343

44+
if status.returncode != 0 and args.err_file:
45+
with open(args.err_file, "r") as f:
46+
os.sys.stderr.write(f.read())
47+
48+
4449
os.sys.exit(status.returncode)

src/Global.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ void MainConfig::processArgs(int argc, char** argv, const std::string& header, c
182182
while ((c = getopt_long(argc, argv, shortNames.c_str(), longNames.get(), nullptr)) != -1) {
183183
// case for the unknown option
184184
if (c == '?') {
185-
std::cerr << Global::config().help();
185+
std::cerr << help();
186186
throw std::runtime_error("Error: Unknown command line option.");
187187
}
188188
// obtain an iterator to the option in the table referenced by the current short name
@@ -217,11 +217,11 @@ void MainConfig::processArgs(int argc, char** argv, const std::string& header, c
217217
}
218218

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

src/Global.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,21 @@ class MainConfig {
104104
* used to isolate all globals. */
105105
class Global {
106106
public:
107+
Global() = default;
107108
/* Deleted copy constructor. */
108109
Global(const Global&) = delete;
109110
/* Deleted assignment operator. */
110111
Global& operator=(const Global&) = delete;
111112
/* Obtain the global configuration. */
112-
static MainConfig& config() {
113-
static MainConfig _config;
113+
MainConfig& config() {
114+
return _config;
115+
}
116+
117+
const MainConfig& config() const {
114118
return _config;
115119
}
116120

117121
private:
118-
/* Private empty constructor, there is only one global instance. */
119-
Global() = default;
122+
MainConfig _config;
120123
};
121124
} // namespace souffle

src/TranslationUnitBase.h

+10-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#pragma once
1818

19+
#include "Global.h"
1920
#include "reports/DebugReport.h"
2021
#include "reports/ErrorReport.h"
2122
#include "souffle/utility/DynamicCasting.h"
@@ -72,8 +73,8 @@ struct TranslationUnitBase {
7273
virtual void run(Impl const&) = 0;
7374
};
7475

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

@@ -109,6 +110,11 @@ struct TranslationUnitBase {
109110
analyses.clear();
110111
}
111112

113+
/** @brief Get the global configuration */
114+
Global& global() const {
115+
return glb;
116+
}
117+
112118
/** @brief Get the RAM Program of the translation unit */
113119
Program& getProgram() const {
114120
return *program;
@@ -133,6 +139,8 @@ struct TranslationUnitBase {
133139
// Using `std::string` appears to suppress the issue (bug?).
134140
mutable std::map<std::string, Own<Analysis>> analyses;
135141

142+
Global& glb;
143+
136144
/* RAM program */
137145
Own<Program> program;
138146

src/ast/Program.h

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#pragma once
1818

19+
#include "Global.h"
1920
#include "ast/Clause.h"
2021
#include "ast/Component.h"
2122
#include "ast/ComponentInit.h"

src/ast/TranslationUnit.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "ast/TranslationUnit.h"
1010
#include "Global.h"
11+
#include "ast/Program.h"
1112
#include "ast/analysis/PrecedenceGraph.h"
1213
#include "ast/analysis/SCCGraph.h"
1314
#include "reports/DebugReport.h"
@@ -17,7 +18,7 @@ namespace souffle::ast {
1718

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

2223
std::string name = analysis.getName();
2324
if (as<analysis::PrecedenceGraphAnalysis>(analysis) || as<analysis::SCCGraphAnalysis>(analysis)) {

src/ast/TranslationUnit.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@
1616

1717
#pragma once
1818

19+
#include "Program.h"
1920
#include "TranslationUnitBase.h"
2021

2122
namespace souffle::ast {
2223

23-
class Program;
24-
2524
/**
2625
* @class TranslationUnit
2726
* @brief Translation unit class for the translation pipeline

src/ast/analysis/JoinSize.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ analysis::StratumJoinSizeEstimates JoinSizeAnalysis::computeRuleVersionStatement
120120
return statements;
121121
}
122122

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

136-
auto& config = Global::config();
137-
if (!config.has("emit-statistics")) {
137+
if (!emitStatistics) {
138138
return joinSizeStatements;
139139
}
140140

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

246246
void JoinSizeAnalysis::print(std::ostream& os) const {

src/ast/analysis/JoinSize.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class JoinSizeAnalysis : public Analysis {
6868
PolymorphicObjectsAnalysis* polyAnalysis = nullptr;
6969

7070
// for each stratum compute the EstimateJoinSize nodes to emit
71-
std::vector<StratumJoinSizeEstimates> computeJoinSizeStatements();
71+
std::vector<StratumJoinSizeEstimates> computeJoinSizeStatements(bool emitStatistics);
7272
StratumJoinSizeEstimates computeRuleVersionStatements(const RelationSet& sccRelations,
7373
const ast::Clause& clause, std::size_t version,
7474
ast2ram::TranslationMode mode = ast2ram::TranslationMode::DEFAULT);

src/ast/analysis/ProfileUse.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "ast/analysis/ProfileUse.h"
1919
#include "Global.h"
20+
#include "ast/Program.h"
2021
#include "ast/QualifiedName.h"
2122
#include "souffle/profile/ProgramRun.h"
2223
#include "souffle/profile/Reader.h"
@@ -29,10 +30,10 @@ namespace souffle::ast::analysis {
2930
/**
3031
* Run analysis, i.e., retrieve profile information
3132
*/
32-
void ProfileUseAnalysis::run(const TranslationUnit&) {
33+
void ProfileUseAnalysis::run(const TranslationUnit& TU) {
3334
std::string filename;
34-
if (Global::config().has("auto-schedule")) {
35-
filename = Global::config().get("auto-schedule");
35+
if (TU.global().config().has("auto-schedule")) {
36+
filename = TU.global().config().get("auto-schedule");
3637
}
3738
reader = mk<profile::Reader>(filename, programRun);
3839
reader->processFile();

src/ast/analysis/SCCGraph.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ namespace souffle::ast::analysis {
3636
void SCCGraphAnalysis::run(const TranslationUnit& translationUnit) {
3737
precedenceGraph = &translationUnit.getAnalysis<PrecedenceGraphAnalysis>();
3838
ioType = &translationUnit.getAnalysis<IOTypeAnalysis>();
39+
programName = translationUnit.global().config().get("name");
3940
sccToRelation.clear();
4041
relationToScc.clear();
4142
predecessors.clear();
@@ -115,7 +116,7 @@ void SCCGraphAnalysis::scR(const Relation* w, std::map<const Relation*, std::siz
115116
}
116117

117118
void SCCGraphAnalysis::printRaw(std::stringstream& ss) const {
118-
const std::string& name = Global::config().get("name");
119+
const std::string& name = programName;
119120
/* Print SCC graph */
120121
ss << "digraph {" << std::endl;
121122
/* Print nodes of SCC graph */

src/ast/analysis/SCCGraph.h

+2
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ class SCCGraphAnalysis : public Analysis {
228228

229229
IOTypeAnalysis* ioType = nullptr;
230230

231+
std::string programName;
232+
231233
/** Print the SCC graph to a string. */
232234
void printRaw(std::stringstream& ss) const;
233235
};

src/ast/analysis/typesystem/Type.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,8 @@ bool TypeAnalysis::isSymbol(const Argument* argument) const {
510510
void TypeAnalysis::run(const TranslationUnit& translationUnit) {
511511
// Check if debugging information is being generated
512512
std::ostream* debugStream = nullptr;
513-
if (Global::config().has("debug-report") || Global::config().has("show", "type-analysis")) {
513+
if (translationUnit.global().config().has("debug-report") ||
514+
translationUnit.global().config().has("show", "type-analysis")) {
514515
debugStream = &analysisLogs;
515516
}
516517

src/ast/tests/ast_print_test.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@
4545
namespace souffle::ast::test {
4646

4747
inline Own<TranslationUnit> makeATU(std::string program = ".decl A,B,C(x:number)") {
48+
Global glb;
4849
ErrorReport e;
49-
DebugReport d;
50-
return ParserDriver::parseTranslationUnit(program, e, d);
50+
DebugReport d(glb);
51+
return ParserDriver::parseTranslationUnit(glb, program, e, d);
5152
}
5253

5354
inline Own<TranslationUnit> makePrintedATU(Own<TranslationUnit>& tu) {
@@ -84,11 +85,12 @@ TEST(AstPrint, NumberConstant) {
8485
}
8586

8687
TEST(AstPrint, StringConstant) {
88+
Global glb;
8789
ErrorReport e;
88-
DebugReport d;
90+
DebugReport d(glb);
8991
auto testArgument = mk<StringConstant>("test string");
9092

91-
auto tu1 = ParserDriver::parseTranslationUnit(".decl A,B,C(x:number)", e, d);
93+
auto tu1 = ParserDriver::parseTranslationUnit(glb, ".decl A,B,C(x:number)", e, d);
9294
tu1->getProgram().addClause(makeClauseA(std::move(testArgument)));
9395
auto tu2 = makePrintedATU(tu1);
9496
EXPECT_EQ(tu1->getProgram(), tu2->getProgram());

src/ast/tests/ast_program_test.cpp

+18-15
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@
4343
namespace souffle::ast::test {
4444

4545
inline Own<TranslationUnit> makeATU(std::string program) {
46+
Global glb;
4647
ErrorReport e;
47-
DebugReport d;
48-
return ParserDriver::parseTranslationUnit(program, e, d);
48+
DebugReport d(glb);
49+
return ParserDriver::parseTranslationUnit(glb, program, e, d);
4950
}
5051

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

5859
TEST(Program, Parse) {
60+
Global glb;
5961
ErrorReport e;
60-
DebugReport d;
62+
DebugReport d(glb);
6163
// check the empty program
62-
Own<TranslationUnit> empty = ParserDriver::parseTranslationUnit("", e, d);
64+
Own<TranslationUnit> empty = ParserDriver::parseTranslationUnit(glb, "", e, d);
6365

6466
EXPECT_TRUE(empty->getProgram().getTypes().empty());
6567
EXPECT_TRUE(empty->getProgram().getRelations().empty());
6668

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

90-
#define TESTASTCLONEANDEQUAL(SUBTYPE, DL) \
91-
TEST(Ast, CloneAndEqual##SUBTYPE) { \
92-
ErrorReport e; \
93-
DebugReport d; \
94-
Own<TranslationUnit> tu = ParserDriver::parseTranslationUnit(DL, e, d); \
95-
Program& program = tu->getProgram(); \
96-
EXPECT_EQ(program, program); \
97-
Own<Program> cl(clone(program)); \
98-
EXPECT_NE(cl.get(), &program); \
99-
EXPECT_EQ(*cl, program); \
92+
#define TESTASTCLONEANDEQUAL(SUBTYPE, DL) \
93+
TEST(Ast, CloneAndEqual##SUBTYPE) { \
94+
Global glb; \
95+
ErrorReport e; \
96+
DebugReport d(glb); \
97+
Own<TranslationUnit> tu = ParserDriver::parseTranslationUnit(glb, DL, e, d); \
98+
Program& program = tu->getProgram(); \
99+
EXPECT_EQ(program, program); \
100+
Own<Program> cl(clone(program)); \
101+
EXPECT_NE(cl.get(), &program); \
102+
EXPECT_EQ(*cl, program); \
100103
}
101104

102105
TESTASTCLONEANDEQUAL(Program,

0 commit comments

Comments
 (0)