Skip to content

Commit abc742f

Browse files
authored
C: Add HERB_EXCLUDE_PRETTYPRINT preprocessor flag (#1262)
This pull request adds an `HERB_EXCLUDE_PRETTYPRINT` preprocessor flag to compile out pretty print support from all language bindings, since only the C CLI's parse command uses it. The respective language bindings have their own pretty print implementation in the binding language. Additionally, we added `PRISM_EXCLUDE_PRETTYPRINT`, `PRISM_EXCLUDE_JSON`, `PRISM_EXCLUDE_PACK`, and `PRISM_EXCLUDE_SERIALIZATION` to all bindings, since we are not using these Prism features.
1 parent 93fe61f commit abc742f

File tree

13 files changed

+84
-27
lines changed

13 files changed

+84
-27
lines changed

ext/herb/extconf.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
$INCFLAGS << " -I#{prism_src_path}/util"
2323

2424
$CFLAGS << " -fvisibility=hidden"
25+
$CFLAGS << " -DHERB_EXCLUDE_PRETTYPRINT"
26+
$CFLAGS << " -DPRISM_EXCLUDE_PRETTYPRINT"
27+
$CFLAGS << " -DPRISM_EXCLUDE_JSON"
28+
$CFLAGS << " -DPRISM_EXCLUDE_PACK"
29+
$CFLAGS << " -DPRISM_EXCLUDE_SERIALIZATION"
2530

2631
herb_src_files = Dir.glob("#{$srcdir}/../../src/**/*.c").map { |file| file.delete_prefix("../../../../ext/herb/") }.sort
2732

java/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ PRISM_BUILD = $(PRISM_PATH)/build
3030

3131
JAVAC = $(JAVA_HOME)/bin/javac
3232
JAVA_CMD = $(JAVA_HOME)/bin/java
33-
CFLAGS = -std=c99 -Wall -Wextra -fPIC -O2
33+
CFLAGS = -std=c99 -Wall -Wextra -fPIC -O2 -DHERB_EXCLUDE_PRETTYPRINT -DPRISM_EXCLUDE_PRETTYPRINT -DPRISM_EXCLUDE_JSON -DPRISM_EXCLUDE_PACK -DPRISM_EXCLUDE_SERIALIZATION
3434
INCLUDES = -I. -I$(SRC_DIR)/include -I$(PRISM_INCLUDE) $(JNI_INCLUDES)
3535
LDFLAGS = -shared
3636
LIBS = $(PRISM_BUILD)/libprism.a

javascript/packages/node/binding.gyp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,12 @@
9090
],
9191
"defines": [
9292
"PRISM_EXPORT_SYMBOLS=static",
93-
"PRISM_STATIC=1"
93+
"PRISM_STATIC=1",
94+
"HERB_EXCLUDE_PRETTYPRINT",
95+
"PRISM_EXCLUDE_PRETTYPRINT",
96+
"PRISM_EXCLUDE_JSON",
97+
"PRISM_EXCLUDE_PACK",
98+
"PRISM_EXCLUDE_SERIALIZATION"
9499
],
95100
"cflags": [
96101
"-Wall",

rust/build.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ fn main() {
4141
.flag("-std=c99")
4242
.flag("-fPIC")
4343
.opt_level(2)
44+
.define("HERB_EXCLUDE_PRETTYPRINT", None)
45+
.define("PRISM_EXCLUDE_PRETTYPRINT", None)
46+
.define("PRISM_EXCLUDE_JSON", None)
47+
.define("PRISM_EXCLUDE_PACK", None)
48+
.define("PRISM_EXCLUDE_SERIALIZATION", None)
4449
.include(&vendor_prism_include)
4550
.files(&prism_sources)
4651
.warnings(false);
@@ -72,6 +77,11 @@ fn main() {
7277
.flag("-Wno-unused-parameter")
7378
.flag("-fPIC")
7479
.opt_level(2)
80+
.define("HERB_EXCLUDE_PRETTYPRINT", None)
81+
.define("PRISM_EXCLUDE_PRETTYPRINT", None)
82+
.define("PRISM_EXCLUDE_JSON", None)
83+
.define("PRISM_EXCLUDE_PACK", None)
84+
.define("PRISM_EXCLUDE_SERIALIZATION", None)
7585
.include(&include_dir)
7686
.include(&prism_include)
7787
.files(&c_sources);

src/include/pretty_print.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
#ifndef HERB_PRETTY_PRINT_H
22
#define HERB_PRETTY_PRINT_H
33

4-
#include "analyze/analyzed_ruby.h"
5-
#include "ast_nodes.h"
6-
#include "location.h"
7-
#include "util/hb_buffer.h"
4+
#ifdef HERB_EXCLUDE_PRETTYPRINT
5+
// Pretty print support excluded
6+
#else
87

9-
#include <stdbool.h>
8+
# include "analyze/analyzed_ruby.h"
9+
# include "ast_nodes.h"
10+
# include "location.h"
11+
# include "util/hb_buffer.h"
12+
13+
# include <stdbool.h>
1014

1115
void pretty_print_indent(hb_buffer_T* buffer, size_t indent);
1216
void pretty_print_newline(size_t indent, size_t relative_indent, hb_buffer_T* buffer);
@@ -101,3 +105,4 @@ void pretty_print_errors(
101105
);
102106

103107
#endif
108+
#endif

src/main.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
#include "include/ast_node.h"
44
#include "include/ast_nodes.h"
5-
#include "include/ast_pretty_print.h"
5+
6+
#ifndef HERB_EXCLUDE_PRETTYPRINT
7+
# include "include/ast_pretty_print.h"
8+
#endif
9+
610
#include "include/extract.h"
711
#include "include/herb.h"
812
#include "include/io.h"
@@ -84,8 +88,10 @@ int main(const int argc, char* argv[]) {
8488
if (argc > 3 && string_equals(argv[3], "--silent")) { silent = 1; }
8589

8690
if (!silent) {
91+
#ifndef HERB_EXCLUDE_PRETTYPRINT
8792
ast_pretty_print_node((AST_NODE_T*) root, 0, 0, &output);
8893
puts(output.value);
94+
#endif
8995

9096
print_time_diff(start, end, "parsing");
9197
}

src/pretty_print.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
#include "include/pretty_print.h"
2-
#include "include/ast_nodes.h"
3-
#include "include/ast_pretty_print.h"
4-
#include "include/errors.h"
5-
#include "include/token_struct.h"
6-
#include "include/util.h"
7-
#include "include/util/hb_buffer.h"
8-
#include "include/util/hb_string.h"
9-
10-
#include <stdbool.h>
11-
#include <stdio.h>
12-
#include <stdlib.h>
1+
#ifdef HERB_EXCLUDE_PRETTYPRINT
2+
// Pretty print support excluded
3+
#else
4+
5+
# include "include/pretty_print.h"
6+
# include "include/ast_nodes.h"
7+
# include "include/ast_pretty_print.h"
8+
# include "include/errors.h"
9+
# include "include/token_struct.h"
10+
# include "include/util.h"
11+
# include "include/util/hb_buffer.h"
12+
# include "include/util/hb_string.h"
13+
14+
# include <stdbool.h>
15+
# include <stdio.h>
16+
# include <stdlib.h>
1317

1418
void pretty_print_indent(hb_buffer_T* buffer, const size_t indent) {
1519
for (size_t i = 0; i < indent; i++) {
@@ -249,3 +253,5 @@ void pretty_print_string_property(
249253
if (!hb_string_is_empty(quoted)) { free(quoted.data); }
250254
}
251255
}
256+
257+
#endif

src/ruby_parser.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ void herb_parse_ruby_to_stdout(char* source) {
3838

3939
pm_visit_node(root, herb_prism_visit, data);
4040

41+
#ifndef PRISM_EXCLUDE_PRETTYPRINT
4142
pm_prettyprint(&buffer, &parser, root);
4243
printf("%s\n", buffer.value);
44+
#endif
4345

4446
pm_buffer_free(&buffer);
4547
pm_node_destroy(&parser, root);

templates/src/ast_pretty_print.c.erb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#ifdef HERB_EXCLUDE_PRETTYPRINT
2+
// Pretty print support excluded
3+
#else
4+
15
#include "include/analyze/helpers.h"
26
#include "include/ast_node.h"
37
#include "include/ast_nodes.h"
@@ -110,3 +114,5 @@ void ast_pretty_print_node(AST_NODE_T* node, const size_t indent, const size_t r
110114
<%- end -%>
111115
}
112116
}
117+
118+
#endif

templates/src/errors.c.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ void error_free(ERROR_T* error) {
161161
}
162162
}
163163

164+
#ifndef HERB_EXCLUDE_PRETTYPRINT
165+
164166
void error_pretty_print_array(
165167
const char* name, hb_array_T* array, const size_t indent, const size_t relative_indent, const bool last_property,
166168
hb_buffer_T* buffer
@@ -245,3 +247,5 @@ void error_pretty_print(ERROR_T* error, const size_t indent, const size_t relati
245247
<%- end -%>
246248
}
247249
}
250+
251+
#endif

0 commit comments

Comments
 (0)