From 0b444c1955cbf390b6215f73e3c30284498799e7 Mon Sep 17 00:00:00 2001 From: Pedro Date: Fri, 7 Nov 2014 15:21:54 +0100 Subject: [PATCH] added destructors --- src/Makefile | 2 +- src/build_and_run_tests.sh | 22 +++++++++---------- src/lib/Expr.cpp | 9 +++++--- src/lib/Expr.h | 6 +++++- src/lib/ImportStatement.h | 1 + src/lib/List.h | 4 ++++ src/lib/SelectStatement.h | 3 +++ src/lib/Statement.h | 2 ++ src/lib/Table.h | 3 ++- src/lib/destructors.cpp | 43 ++++++++++++++++++++++++++++++++++++++ src/sql_analysis.cpp | 3 ++- 11 files changed, 80 insertions(+), 18 deletions(-) create mode 100644 src/lib/destructors.cpp diff --git a/src/Makefile b/src/Makefile index 79d861f..95ee6ff 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,7 +1,7 @@ # Makefile -LIB_FILES = parser/bison_parser.cpp parser/flex_lexer.cpp parser/SQLParser.cpp lib/Expr.cpp lib/sqlhelper.cpp +LIB_FILES = parser/bison_parser.cpp parser/flex_lexer.cpp parser/SQLParser.cpp lib/Expr.cpp lib/sqlhelper.cpp lib/destructors.cpp TESTS_MAIN = sql_tests.cpp TESTS_BIN = bin/tests diff --git a/src/build_and_run_tests.sh b/src/build_and_run_tests.sh index dcd0826..f4f3432 100644 --- a/src/build_and_run_tests.sh +++ b/src/build_and_run_tests.sh @@ -9,15 +9,15 @@ make grammar_test echo "\n\n" -./bin/grammar_test "SELECT a FROM foo WHERE a > 12 OR b > 3 AND c = 3 LIMIT 10" -./bin/grammar_test "SELECT col1, col2, 'test' FROM table, foo AS t WHERE age > 12 AND zipcode = 12345 GROUP BY col1;" -./bin/grammar_test "SELECT age FROM table AS t1, (SELECT * FROM table2) AS t2 ORDER BY age DESC" -./bin/grammar_test "SELECT * from table JOIN table2 ON a = b WHERE (b OR NOT a) AND a = 12.5" -./bin/grammar_test "(SELECT a FROM foo WHERE a > 12 OR b > 3 AND c LIKE 's%' LIMIT 10);" -./bin/grammar_test "(SELECT a FROM foo WHERE a > 12 OR b > 3 AND c NOT LIKE 's%' LIMIT 10);" -./bin/grammar_test "SELECT t1.a, t1.b, t2.c FROM table AS t1 JOIN (SELECT * FROM foo JOIN bar ON foo.id = bar.id) t2 ON t1.a = t2.b WHERE (t1.b OR NOT t1.a) AND t2.c = 12.5" +./bin/grammar_test "SELECT a FROM foo WHERE a > 12 OR b > 3 AND NOT c LIMIT 10" +# ./bin/grammar_test "SELECT col1, col2, 'test' FROM table, foo AS t WHERE age > 12 AND zipcode = 12345 GROUP BY col1;" +./bin/grammar_test "SELECT age FROM table AS t1, (SELECT * FROM table2) AS t2 ORDER BY age DESC LIMIT 10; SELECT * AS table;" +# ./bin/grammar_test "SELECT * from table JOIN table2 ON a = b WHERE (b OR NOT a) AND a = 12.5" +# ./bin/grammar_test "(SELECT a FROM foo WHERE a > 12 OR b > 3 AND c LIKE 's%' LIMIT 10);" +# ./bin/grammar_test "(SELECT a FROM foo WHERE a > 12 OR b > 3 AND c NOT LIKE 's%' LIMIT 10);" +# ./bin/grammar_test "SELECT t1.a, t1.b, t2.c FROM table AS t1 JOIN (SELECT * FROM foo JOIN bar ON foo.id = bar.id) t2 ON t1.a = t2.b WHERE (t1.b OR NOT t1.a) AND t2.c = 12.5" -./bin/grammar_test "IMPORT FROM TBL FILE 'students.tbl' INTO table" +# ./bin/grammar_test "IMPORT FROM TBL FILE 'students.tbl' INTO table" # Error: Where clause in between join statement # ./bin/grammar_test -f "SELECT * from table WHERE (b OR NOT a) AND a = 12.5 AS t1 JOIN table2 ON a = b" @@ -32,8 +32,8 @@ echo "\n\n" # ./bin/analysis "-- test # SELECT * FROM table WHERE a NOT LIKE '%s' -- inline comment # --my comment" -./bin/analysis " -IMPORT FROM TBL FILE 'students.tbl' INTO table; -SELECT * FROM table;" +# ./bin/analysis " +# IMPORT FROM TBL FILE 'students.tbl' INTO table; +# SELECT * FROM table;" echo "\n\n" \ No newline at end of file diff --git a/src/lib/Expr.cpp b/src/lib/Expr.cpp index 0e802e6..f415599 100644 --- a/src/lib/Expr.cpp +++ b/src/lib/Expr.cpp @@ -86,8 +86,11 @@ Expr* Expr::makeFunctionRef(char* func_name, Expr* expr) { return e; } -// Expr::~Expr() { - -// } +Expr::~Expr() { + delete expr; + delete expr2; + delete name; + delete table; +} } // namespace hsql \ No newline at end of file diff --git a/src/lib/Expr.h b/src/lib/Expr.h index 35ecd6a..42f5749 100644 --- a/src/lib/Expr.h +++ b/src/lib/Expr.h @@ -2,6 +2,7 @@ #define __EXPRESSION_H__ #include +#include namespace hsql { @@ -45,7 +46,10 @@ struct Expr { Expr(ExprType type) : type(type) {}; - // virtual ~Expr(); + + // Interesting side-effect: + // Making the destructor virtual causes segmentation faults + ~Expr(); ExprType type; diff --git a/src/lib/ImportStatement.h b/src/lib/ImportStatement.h index 2113434..a36c437 100644 --- a/src/lib/ImportStatement.h +++ b/src/lib/ImportStatement.h @@ -20,6 +20,7 @@ typedef enum { */ struct ImportStatement : Statement { ImportStatement() : Statement(kStmtImport) {}; + virtual ~ImportStatement(); // defined in destructors.cpp ImportFileType file_type; const char* file_path; diff --git a/src/lib/List.h b/src/lib/List.h index 3492ee2..46c450d 100644 --- a/src/lib/List.h +++ b/src/lib/List.h @@ -17,6 +17,10 @@ public: _vector.push_back(first_value); } + virtual ~List() { + for (_T e : _vector) delete e; + } + inline size_t size() { return _vector.size(); }; inline _T at(int i) { return _vector[i]; } diff --git a/src/lib/SelectStatement.h b/src/lib/SelectStatement.h index a44cb9c..b25048b 100644 --- a/src/lib/SelectStatement.h +++ b/src/lib/SelectStatement.h @@ -21,6 +21,8 @@ typedef enum { struct OrderDescription { OrderDescription(OrderType type, Expr* expr) : type(type), expr(expr) {} + virtual ~OrderDescription(); // defined in destructors.cpp + OrderType type; Expr* expr; }; @@ -43,6 +45,7 @@ struct LimitDescription { */ struct SelectStatement : Statement { SelectStatement() : Statement(kStmtSelect) {}; + virtual ~SelectStatement(); // defined in destructors.cpp TableRef* from_table; List* select_list; diff --git a/src/lib/Statement.h b/src/lib/Statement.h index 0487847..dc0e95d 100644 --- a/src/lib/Statement.h +++ b/src/lib/Statement.h @@ -27,6 +27,7 @@ typedef enum { struct Statement { Statement(StatementType type) : type(type) {}; + virtual ~Statement(); // defined in destructors.cpp StatementType type; }; @@ -36,6 +37,7 @@ class StatementList : public List { public: StatementList() : List(), isValid(true) {}; StatementList(Statement* stmt) : List(stmt), isValid(true) {}; + virtual ~StatementList(); // defined in destructors.cpp bool isValid; const char* parser_msg; diff --git a/src/lib/Table.h b/src/lib/Table.h index db8764a..62b389a 100644 --- a/src/lib/Table.h +++ b/src/lib/Table.h @@ -1,7 +1,7 @@ #ifndef __TABLEREF_H__ #define __TABLEREF_H__ - +#include namespace hsql { class SelectStatement; @@ -30,6 +30,7 @@ typedef struct TableRef TableRef; struct TableRef { TableRef(TableRefType type) : type(type) {} + virtual ~TableRef(); // defined in destructors.cpp TableRefType type; diff --git a/src/lib/destructors.cpp b/src/lib/destructors.cpp new file mode 100644 index 0000000..d7dfc9f --- /dev/null +++ b/src/lib/destructors.cpp @@ -0,0 +1,43 @@ + + +#include "sqllib.h" + +namespace hsql { + +Statement::~Statement() { + /* empty */ +} + +StatementList::~StatementList() { + delete parser_msg; +} + +TableRef::~TableRef() { + delete name; + delete alias; + delete select; + delete list; + delete left; + delete right; + delete join_condition; +} + +SelectStatement::~SelectStatement() { + delete from_table; + delete select_list; + delete where_clause; + delete group_by; + delete order; + delete limit; +} + +OrderDescription::~OrderDescription() { + delete expr; +} + +ImportStatement::~ImportStatement() { + delete file_path; + delete table_name; +} + +} // namespace hsql \ No newline at end of file diff --git a/src/sql_analysis.cpp b/src/sql_analysis.cpp index dcf9ffa..3f02f59 100644 --- a/src/sql_analysis.cpp +++ b/src/sql_analysis.cpp @@ -36,9 +36,10 @@ int main(int argc, char *argv[]) { default: fprintf(stderr, "\tStatement Type %u. No detailed print method available.\n", stmt->type); break; - } + // delete stmt; } + delete stmt_list; } return 0;