dolik-rce / pegof

PEG grammar optimizer and formatter
Other
9 stars 2 forks source link

Compiler warnings on gcc9 #3

Closed dolik-rce closed 2 years ago

dolik-rce commented 2 years ago

Here is all my changes so far that also remove compiler warnings pointed by gcc9:

diff --git a/CMakeLists.txt b/CMakeLists.txt
index a1ba459..4ccced3 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -22,3 +22,5 @@ add_custom_command(
 add_custom_target(test tests/run.sh DEPENDS pegof WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})

 target_include_directories(pegof BEFORE PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
+
+target_compile_features(pegof PUBLIC cxx_std_17)
diff --git a/ast.cc b/ast.cc
index 0d3ce59..9f9f5d1 100644
--- a/ast.cc
+++ b/ast.cc
@@ -54,7 +54,7 @@ const char* AstNode::get_type_name() const {
     case AST_GROUP:             return "GROUP";
     case AST_CAPTURE:           return "CAPTURE";
     default:
-        Io::debug("ERROR: unexpected AST node type!\n");
+        Io::debug("%s\n", "ERROR: unexpected AST node type!");
         exit(2);
     }
 }
@@ -138,14 +138,14 @@ void AstNode::replace_child(AstNode* removed, AstNode* added, bool do_delete) {
 }

 void AstNode::debug() {
-    Io::print("### Original AST:\n");
+    Io::print("%s\n", "### Original AST:");
     print_ast();
-    Io::print("### Original formatted:\n");
+    Io::print("%s\n", "### Original formatted:");
     format();
     optimize();
-    Io::print("### Optimized AST:\n");
+    Io::print("%s\n", "### Optimized AST:");
     print_ast();
-    Io::print("### Optimized formatted:\n");
+    Io::print("%s\n", "### Optimized formatted:");
     format();
 }

diff --git a/ast_format.cc b/ast_format.cc
index 9b78fc6..890c3da 100644
--- a/ast_format.cc
+++ b/ast_format.cc
@@ -72,7 +72,7 @@ void AstNode::format() const {
         format_terminal();
         break;
     default:
-        Io::debug("ERROR: unexpected AST node type!\n");
+        Io::debug("%s\n", "ERROR: unexpected AST node type!");
         exit(2);
     }
 }
@@ -125,7 +125,7 @@ void AstNode::format_source() const {
     int is_directive = parent->type == AST_DIRECTIVE;

     if (has_newlines || is_c_directive) {
-        Io::print(" {\n");
+        Io::print("%s\n", " {");
         reindent(text, is_directive ? 4 : 8);
         Io::print("%s}", is_directive ? "" : "    ");
     } else {
@@ -134,20 +134,20 @@ void AstNode::format_source() const {
 }

 void AstNode::format_error() const {
-    Io::print(" ~");
+    Io::print("%s", " ~");
     format_source();
 }

 void AstNode::format_directive() const {
     AstNode* content = children[1];

-    Io::print("%%");
+    Io::print("%s", "%");
     children[0]->format();
     if (content->type == AST_STRING) {
-        Io::print(" ");
+        Io::print("%s", " ");
     }
     content->format();
-    Io::print(parent->children.back() == this ? "\n" : "\n\n");
+    Io::print("%s", parent->children.back() == this ? "\n" : "\n\n");
 }

 void AstNode::format_alternation() const {
@@ -164,7 +164,7 @@ void AstNode::format_alternation() const {
 void AstNode::format_sequence() const {
     for (size_t i = 0; i < children.size(); i++) {
         if (i > 0) {
-            Io::print(" ");
+            Io::print("%s", " ");
         }
         children[i]->format();
     }
@@ -186,7 +186,7 @@ void AstNode::format_ruleref() const {
     children[0]->format();
     if (children.size() == 2) {
         // has variable
-        Io::print(":");
+        Io::print("%s", ":");
         children[1]->format();
     }
 }
@@ -196,7 +196,7 @@ void AstNode::format_rule() const {
     bool hasAlternation = body->type == AST_ALTERNATION && body->children.size() > 1;
     Io::print("%s%s <- ", children[0]->text.c_str(), hasAlternation ? "\n   " : "");
     body->format();
-    Io::print(parent->children.back() == this ? "\n" : "\n\n");
+    Io::print("%s", parent->children.back() == this ? "\n" : "\n\n");
 }

 void AstNode::format_code() const {
diff --git a/ast_opt.cc b/ast_opt.cc
index a78484a..41762e9 100644
--- a/ast_opt.cc
+++ b/ast_opt.cc
@@ -99,7 +99,7 @@ int AstNode::optimize_repeats() {
     }

     if (first_op == '*' && second_op == '*') {
-        Io::debug("  Removing unnecessary repeated node (X* X* -> X*)\n");
+        Io::debug("%s\n", "  Removing unnecessary repeated node (X* X* -> X*)");
         second->parent->remove_child(second);
     } else if (first_op == '*' || second_op == '*') {
         Io::debug("  Removing unnecessary repeated node (X%c X%c -> X+)\n", first_op, second_op);
@@ -145,7 +145,7 @@ int AstNode::optimize_children() {
 }

 int AstNode::optimize_strip_comment() {
-    Io::debug("  Removing comment\n");
+    Io::debug("%s\n", "  Removing comment");
     parent->remove_child(this);
     return 1;
 }
@@ -313,6 +313,6 @@ int AstNode::optimize() {
     case AST_BACKREF:
         return optimize_children();
     }
-    Io::debug("ERROR: unexpected AST node type!\n");
+    Io::debug("%s\n", "ERROR: unexpected AST node type!");
     exit(2);
 }
diff --git a/io.cc b/io.cc
index 55aa4e6..97afb36 100644
--- a/io.cc
+++ b/io.cc
@@ -12,7 +12,7 @@ FILE *Io::output = stdin;
 void Io::open(const std::string& input_path, const std::string& output_path) {
     std::stringstream stream;
     if (input_path.empty()) {
-        Io::debug("Reading grammar from stdin\n");
+        Io::debug("%s\n", "Reading grammar from stdin");
         stream << std::cin.rdbuf();
     } else {
         Io::debug("Reading grammar from %s\n", input_path.c_str());
diff --git a/parser.peg b/parser.peg
index 2103c92..44ed942 100644
--- a/parser.peg
+++ b/parser.peg
@@ -90,6 +90,10 @@ sequence
     }

 primary
+    <- <s:source> pb:primary_base { if(s) {$$ = s; append_child($$, pb);} else $$ = pb; }
+    / pb:primary_base { $$ = pb; }
+
+primary_base
     <- c:suffix_comment { $$ = c; }
     / (pr:prefix_op? _* l:literal _* po:postfix_op? (_* <s:source> / _* <e:error>)?) {
         if (!(pr || po || s || e )) {

Originally posted by @mingodad in https://github.com/dolik-rce/pegof/issues/1#issuecomment-1103686246