Implement support for brackets in filter expressions
[lttng-tools.git] / src / lib / lttng-ctl / filter / filter-parser.y
index 3b77528a6a27061a8242e5055299334a23f15336..12605cd382b8af4d65c3d59b7076b3d982870af5 100644 (file)
 #include "filter-ast.h"
 #include "filter-parser.h"
 
-__attribute__((visibility("hidden")))
+#include <common/macros.h>
+
+#define WIDTH_u64_SCANF_IS_A_BROKEN_API        "20"
+#define WIDTH_o64_SCANF_IS_A_BROKEN_API        "22"
+#define WIDTH_x64_SCANF_IS_A_BROKEN_API        "17"
+#define WIDTH_lg_SCANF_IS_A_BROKEN_API "4096"  /* Hugely optimistic approximation */
+
+LTTNG_HIDDEN
 int yydebug;
-__attribute__((visibility("hidden")))
+LTTNG_HIDDEN
 int filter_parser_debug = 0;
 
-__attribute__((visibility("hidden")))
-int yyparse(struct filter_parser_ctx *parser_ctx);
-__attribute__((visibility("hidden")))
-int yylex(union YYSTYPE *yyval, struct filter_parser_ctx *parser_ctx);
-__attribute__((visibility("hidden")))
+LTTNG_HIDDEN
+int yyparse(struct filter_parser_ctx *parser_ctx, yyscan_t scanner);
+LTTNG_HIDDEN
+int yylex(union YYSTYPE *yyval, yyscan_t scanner);
+LTTNG_HIDDEN
 int yylex_init_extra(struct filter_parser_ctx *parser_ctx, yyscan_t * ptr_yy_globals);
-__attribute__((visibility("hidden")))
+LTTNG_HIDDEN
 int yylex_destroy(yyscan_t yyparser_ctx);
-__attribute__((visibility("hidden")))
+LTTNG_HIDDEN
 void yyrestart(FILE * in_str, yyscan_t parser_ctx);
 
 struct gc_string {
@@ -62,7 +69,7 @@ static const char *node_type_to_str[] = {
        [ NODE_UNARY_OP ] = "NODE_UNARY_OP",
 };
 
-__attribute__((visibility("hidden")))
+LTTNG_HIDDEN
 const char *node_type(struct filter_node *node)
 {
        if (node->type < NR_NODE_TYPES)
@@ -82,9 +89,13 @@ static struct gc_string *gc_string_alloc(struct filter_parser_ctx *parser_ctx,
        for (alloclen = 8; alloclen < sizeof(long) + sizeof(*gstr) + len;
             alloclen *= 2);
 
-       gstr = malloc(alloclen);
+       gstr = zmalloc(alloclen);
+       if (!gstr) {
+               goto end;
+       }
        cds_list_add(&gstr->gc, &parser_ctx->allocated_strings);
        gstr->alloclen = alloclen;
+end:
        return gstr;
 }
 
@@ -93,7 +104,7 @@ static struct gc_string *gc_string_alloc(struct filter_parser_ctx *parser_ctx,
  * gsrc will be garbage collected immediately, and gstr might be.
  * Should only be used to append characters to a string literal or constant.
  */
-__attribute__((visibility("hidden")))
+LTTNG_HIDDEN
 struct gc_string *gc_string_append(struct filter_parser_ctx *parser_ctx,
                                   struct gc_string *gstr,
                                   struct gc_string *gsrc)
@@ -123,7 +134,7 @@ struct gc_string *gc_string_append(struct filter_parser_ctx *parser_ctx,
        return gstr;
 }
 
-__attribute__((visibility("hidden")))
+LTTNG_HIDDEN
 void setstring(struct filter_parser_ctx *parser_ctx, YYSTYPE *lvalp, const char *src)
 {
        lvalp->gs = gc_string_alloc(parser_ctx, strlen(src) + 1);
@@ -136,7 +147,7 @@ static struct filter_node *make_node(struct filter_parser_ctx *scanner,
        struct filter_ast *ast = filter_parser_get_ast(scanner);
        struct filter_node *node;
 
-       node = malloc(sizeof(*node));
+       node = zmalloc(sizeof(*node));
        if (!node)
                return NULL;
        memset(node, 0, sizeof(*node));
@@ -173,7 +184,7 @@ static struct filter_node *make_op_node(struct filter_parser_ctx *scanner,
        struct filter_ast *ast = filter_parser_get_ast(scanner);
        struct filter_node *node;
 
-       node = malloc(sizeof(*node));
+       node = zmalloc(sizeof(*node));
        if (!node)
                return NULL;
        memset(node, 0, sizeof(*node));
@@ -185,13 +196,13 @@ static struct filter_node *make_op_node(struct filter_parser_ctx *scanner,
        return node;
 }
 
-__attribute__((visibility("hidden")))
-void yyerror(struct filter_parser_ctx *parser_ctx, const char *str)
+LTTNG_HIDDEN
+void yyerror(struct filter_parser_ctx *parser_ctx, yyscan_t scanner, const char *str)
 {
        fprintf(stderr, "error %s\n", str);
 }
  
-__attribute__((visibility("hidden")))
+LTTNG_HIDDEN
 int yywrap(void)
 {
        return 1;
@@ -199,7 +210,7 @@ int yywrap(void)
 
 #define parse_error(parser_ctx, str)                           \
 do {                                                           \
-       yyerror(parser_ctx, YY_("parse error: " str "\n"));     \
+       yyerror(parser_ctx, parser_ctx->scanner, YY_("parse error: " str "\n"));        \
        YYERROR;                                                \
 } while (0)
 
@@ -215,7 +226,7 @@ static struct filter_ast *filter_ast_alloc(void)
 {
        struct filter_ast *ast;
 
-       ast = malloc(sizeof(*ast));
+       ast = zmalloc(sizeof(*ast));
        if (!ast)
                return NULL;
        memset(ast, 0, sizeof(*ast));
@@ -230,15 +241,16 @@ static void filter_ast_free(struct filter_ast *ast)
 
        cds_list_for_each_entry_safe(node, tmp, &ast->allocated_nodes, gc)
                free(node);
+       free(ast);
 }
 
-__attribute__((visibility("hidden")))
+LTTNG_HIDDEN
 int filter_parser_ctx_append_ast(struct filter_parser_ctx *parser_ctx)
 {
-       return yyparse(parser_ctx);
+       return yyparse(parser_ctx, parser_ctx->scanner);
 }
 
-__attribute__((visibility("hidden")))
+LTTNG_HIDDEN
 struct filter_parser_ctx *filter_parser_ctx_alloc(FILE *input)
 {
        struct filter_parser_ctx *parser_ctx;
@@ -246,7 +258,7 @@ struct filter_parser_ctx *filter_parser_ctx_alloc(FILE *input)
 
        yydebug = filter_parser_debug;
 
-       parser_ctx = malloc(sizeof(*parser_ctx));
+       parser_ctx = zmalloc(sizeof(*parser_ctx));
        if (!parser_ctx)
                return NULL;
        memset(parser_ctx, 0, sizeof(*parser_ctx));
@@ -280,7 +292,7 @@ cleanup_parser_ctx:
        return NULL;
 }
 
-__attribute__((visibility("hidden")))
+LTTNG_HIDDEN
 void filter_parser_ctx_free(struct filter_parser_ctx *parser_ctx)
 {
        int ret;
@@ -298,7 +310,8 @@ void filter_parser_ctx_free(struct filter_parser_ctx *parser_ctx)
 %define api.pure
        /* %locations */
 %parse-param {struct filter_parser_ctx *parser_ctx}
-%lex-param {struct filter_parser_ctx *parser_ctx}
+%parse-param {yyscan_t scanner}
+%lex-param {yyscan_t scanner}
 %start translation_unit
 %token CHARACTER_CONSTANT_START SQUOTE STRING_LITERAL_START DQUOTE
 %token ESCSEQ CHAR_STRING_TOKEN
@@ -310,7 +323,7 @@ void filter_parser_ctx_free(struct filter_parser_ctx *parser_ctx)
 %token ASSIGN COLON SEMICOLON DOTDOTDOT DOT EQUAL COMMA
 %token XOR_BIN AND_BIN OR_BIN NOT_BIN
 
-%token <gs> IDENTIFIER
+%token <gs> IDENTIFIER GLOBAL_IDENTIFIER
 %token ERROR
 %union
 {
@@ -384,33 +397,50 @@ primary_expression
                        $$->u.expression.type = AST_EXP_IDENTIFIER;
                        $$->u.expression.u.identifier = yylval.gs->s;
                }
+       |       GLOBAL_IDENTIFIER
+               {
+                       $$ = make_node(parser_ctx, NODE_EXPRESSION);
+                       $$->u.expression.type = AST_EXP_GLOBAL_IDENTIFIER;
+                       $$->u.expression.u.identifier = yylval.gs->s;
+               }
+
        |       DECIMAL_CONSTANT
                {
                        $$ = make_node(parser_ctx, NODE_EXPRESSION);
                        $$->u.expression.type = AST_EXP_CONSTANT;
-                       sscanf(yylval.gs->s, "%" PRIu64,
-                              &$$->u.expression.u.constant);
+                       if (sscanf(yylval.gs->s, "%" WIDTH_u64_SCANF_IS_A_BROKEN_API SCNu64,
+                                       &$$->u.expression.u.constant) != 1) {
+                               parse_error(parser_ctx, "cannot scanf decimal constant");
+                       }
                }
        |       OCTAL_CONSTANT
                {
                        $$ = make_node(parser_ctx, NODE_EXPRESSION);
                        $$->u.expression.type = AST_EXP_CONSTANT;
-                       sscanf(yylval.gs->s, "0%" PRIo64,
-                              &$$->u.expression.u.constant);
+                       if (!strcmp(yylval.gs->s, "0")) {
+                               $$->u.expression.u.constant = 0;
+                       } else if (sscanf(yylval.gs->s, "0%" WIDTH_o64_SCANF_IS_A_BROKEN_API SCNo64,
+                                       &$$->u.expression.u.constant) != 1) {
+                               parse_error(parser_ctx, "cannot scanf octal constant");
+                       }
                }
        |       HEXADECIMAL_CONSTANT
                {
                        $$ = make_node(parser_ctx, NODE_EXPRESSION);
                        $$->u.expression.type = AST_EXP_CONSTANT;
-                       sscanf(yylval.gs->s, "0x%" PRIx64,
-                              &$$->u.expression.u.constant);
+                       if (sscanf(yylval.gs->s, "0x%" WIDTH_x64_SCANF_IS_A_BROKEN_API SCNx64,
+                                       &$$->u.expression.u.constant) != 1) {
+                               parse_error(parser_ctx, "cannot scanf hexadecimal constant");
+                       }
                }
        |       FLOAT_CONSTANT
                {
                        $$ = make_node(parser_ctx, NODE_EXPRESSION);
                        $$->u.expression.type = AST_EXP_FLOAT_CONSTANT;
-                       sscanf(yylval.gs->s, "%lg",
-                              &$$->u.expression.u.float_constant);
+                       if (sscanf(yylval.gs->s, "%" WIDTH_lg_SCANF_IS_A_BROKEN_API "lg",
+                                       &$$->u.expression.u.float_constant) != 1) {
+                               parse_error(parser_ctx, "cannot scanf float constant");
+                       }
                }
        |       STRING_LITERAL_START DQUOTE
                {
@@ -441,6 +471,12 @@ primary_expression
 postfix_expression
        : primary_expression
                {       $$ = $1;                                        }
+       | postfix_expression LSBRAC unary_expression RSBRAC
+               {
+                       $$ = $1;
+                       $$->u.expression.pre_op = AST_LINK_BRACKET;
+                       $$->u.expression.next = $3;
+               }
        | postfix_expression DOT IDENTIFIER
                {
                        $$ = make_node(parser_ctx, NODE_EXPRESSION);
This page took 0.026473 seconds and 4 git commands to generate.