Implement support for brackets in filter expressions
[lttng-tools.git] / src / lib / lttng-ctl / filter / filter-grammar-test.c
CommitLineData
953192ba
MD
1/*
2 * filter-grammar-test.c
3 *
4 * LTTng filter grammar test
5 *
6 * Copyright 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * This library is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License, version 2.1 only,
10 * as published by the Free Software Foundation.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include <stdio.h>
23#include <unistd.h>
24#include <string.h>
25#include <stdlib.h>
26#include <assert.h>
27#include <errno.h>
28#include <inttypes.h>
953192ba 29#include "filter-ast.h"
95b9bd90 30#include "filter-parser.h"
953192ba
MD
31#include "filter-bytecode.h"
32
33int main(int argc, char **argv)
34{
35 struct filter_parser_ctx *ctx;
36 int ret;
14eabff9
MD
37 int print_xml = 0, generate_ir = 0, generate_bytecode = 0,
38 print_bytecode = 0;
953192ba
MD
39 int i;
40
41 for (i = 1; i < argc; i++) {
42 if (strcmp(argv[i], "-p") == 0)
43 print_xml = 1;
44 else if (strcmp(argv[i], "-i") == 0)
45 generate_ir = 1;
46 else if (strcmp(argv[i], "-b") == 0)
47 generate_bytecode = 1;
48 else if (strcmp(argv[i], "-d") == 0)
49 filter_parser_debug = 1;
14eabff9
MD
50 else if (strcmp(argv[i], "-B") == 0)
51 print_bytecode = 1;
953192ba
MD
52 }
53
54 ctx = filter_parser_ctx_alloc(stdin);
55 if (!ctx) {
56 fprintf(stderr, "Error allocating parser\n");
57 goto alloc_error;
58 }
59 ret = filter_parser_ctx_append_ast(ctx);
60 if (ret) {
61 fprintf(stderr, "Parse error\n");
62 goto parse_error;
63 }
64 ret = filter_visitor_set_parent(ctx);
65 if (ret) {
66 fprintf(stderr, "Set parent error\n");
67 goto parse_error;
68 }
69 if (print_xml) {
70 ret = filter_visitor_print_xml(ctx, stdout, 0);
71 if (ret) {
72 fflush(stdout);
73 fprintf(stderr, "XML print error\n");
74 goto parse_error;
75 }
76 }
77 if (generate_ir) {
78 printf("Generating IR... ");
79 fflush(stdout);
80 ret = filter_visitor_ir_generate(ctx);
81 if (ret) {
82 fprintf(stderr, "Generate IR error\n");
83 goto parse_error;
84 }
85 printf("done\n");
86
87 printf("Validating IR... ");
88 fflush(stdout);
89 ret = filter_visitor_ir_check_binary_op_nesting(ctx);
90 if (ret) {
91 goto parse_error;
92 }
93 printf("done\n");
94 }
95 if (generate_bytecode) {
96 printf("Generating bytecode... ");
97 fflush(stdout);
98 ret = filter_visitor_bytecode_generate(ctx);
99 if (ret) {
100 fprintf(stderr, "Generate bytecode error\n");
101 goto parse_error;
102 }
103 printf("done\n");
104 printf("Size of bytecode generated: %u bytes.\n",
105 bytecode_get_len(&ctx->bytecode->b));
106 }
953192ba 107
14eabff9
MD
108 if (print_bytecode) {
109 unsigned int bytecode_len, len, i;
110
111 len = bytecode_get_len(&ctx->bytecode->b);
112 bytecode_len = ctx->bytecode->b.reloc_table_offset;
113 printf("Bytecode:\n");
114 for (i = 0; i < bytecode_len; i++) {
115 printf("0x%X ",
116 ((uint8_t *) ctx->bytecode->b.data)[i]);
117 }
118 printf("\n");
119 printf("Reloc table:\n");
120 for (i = bytecode_len; i < len;) {
121 printf("{ 0x%X, ",
122 *(uint16_t *) &ctx->bytecode->b.data[i]);
123 i += sizeof(uint16_t);
124 printf("%s } ", &((char *) ctx->bytecode->b.data)[i]);
125 i += strlen(&((char *) ctx->bytecode->b.data)[i]) + 1;
126 }
127 printf("\n");
128 }
129
953192ba
MD
130 filter_bytecode_free(ctx);
131 filter_ir_free(ctx);
132 filter_parser_ctx_free(ctx);
133 return 0;
134
135parse_error:
136 filter_bytecode_free(ctx);
137 filter_ir_free(ctx);
138 filter_parser_ctx_free(ctx);
139alloc_error:
140 exit(EXIT_FAILURE);
141}
This page took 0.040991 seconds and 4 git commands to generate.