Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite41beea

Browse files
committed
Improve pgbench error reporting.
This would have been worth doing on general principle anyway, but therecent addition of an expression syntax to pgbench makes it an evenbetter idea than it would have been otherwise.Fabien Coelho
1 parent05cce2f commite41beea

File tree

3 files changed

+90
-37
lines changed

3 files changed

+90
-37
lines changed

‎contrib/pgbench/exprscan.l

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ static intyyline = 0, yycol = 0;
1717
static YY_BUFFER_STATE scanbufhandle;
1818
staticchar *scanbuf;
1919
staticintscanbuflen;
20+
21+
/* context information for error reporting*/
22+
staticchar *expr_source =NULL;
23+
staticint expr_lineno =0;
24+
staticchar *expr_full_line =NULL;
25+
staticchar *expr_command =NULL;
26+
staticint expr_col =0;
2027
%}
2128

2229
%option8bit
@@ -56,28 +63,37 @@ space[ \t\r\f]
5663

5764
.{
5865
yycol += yyleng;
59-
fprintf(stderr,"unexpected character\"%s\"\n", yytext);
66+
syntax_error(expr_source, expr_lineno, expr_full_line, expr_command,
67+
"unexpected character", yytext, expr_col + yycol);
68+
/* dead code, exit is called from syntax_error */
6069
return CHAR_ERROR;
6170
}
6271
%%
6372

6473
void
6574
yyerror(constchar *message)
6675
{
67-
/* yyline is always 1 as pgbench calls the parser for each line...
68-
* so the interesting location information is the column number */
69-
fprintf(stderr,"%s at column %d\n", message, yycol);
70-
/* go on to raise the error from pgbench with more information */
76+
syntax_error(expr_source, expr_lineno, expr_full_line, expr_command,
77+
message,NULL, expr_col + yycol);
7178
}
7279

7380
/*
7481
* Called before any actual parsing is done
7582
*/
7683
void
77-
expr_scanner_init(constchar *str)
84+
expr_scanner_init(constchar *str,constchar *source,
85+
constint lineno,constchar *line,
86+
constchar *cmd,constint ecol)
7887
{
7988
Sizeslen =strlen(str);
8089

90+
/* save context informations for error messages */
91+
expr_source = (char *) source;
92+
expr_lineno = (int) lineno;
93+
expr_full_line = (char *) line;
94+
expr_command = (char *) cmd;
95+
expr_col = (int) ecol;
96+
8197
/*
8298
* Might be left over after error
8399
*/
@@ -105,4 +121,9 @@ expr_scanner_finish(void)
105121
{
106122
yy_delete_buffer(scanbufhandle);
107123
pg_free(scanbuf);
124+
expr_source =NULL;
125+
expr_lineno =0;
126+
expr_full_line =NULL;
127+
expr_command =NULL;
128+
expr_col =0;
108129
}

‎contrib/pgbench/pgbench.c

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ typedef struct
287287
inttype;/* command type (SQL_COMMAND or META_COMMAND) */
288288
intargc;/* number of command words */
289289
char*argv[MAX_ARGS];/* command word list */
290+
intcols[MAX_ARGS];/* corresponding column starting from 1 */
290291
PgBenchExpr*expr;/* parsed expression */
291292
}Command;
292293

@@ -2185,6 +2186,32 @@ parseQuery(Command *cmd, const char *raw_sql)
21852186
return true;
21862187
}
21872188

2189+
void
2190+
syntax_error(constchar*source,constintlineno,
2191+
constchar*line,constchar*command,
2192+
constchar*msg,constchar*more,constintcolumn)
2193+
{
2194+
fprintf(stderr,"%s:%d: %s",source,lineno,msg);
2195+
if (more!=NULL)
2196+
fprintf(stderr," (%s)",more);
2197+
if (column!=-1)
2198+
fprintf(stderr," at column %d",column);
2199+
fprintf(stderr," in command \"%s\"\n",command);
2200+
if (line!=NULL)
2201+
{
2202+
fprintf(stderr,"%s\n",line);
2203+
if (column!=-1)
2204+
{
2205+
inti;
2206+
2207+
for (i=0;i<column-1;i++)
2208+
fprintf(stderr," ");
2209+
fprintf(stderr,"^ error found here\n");
2210+
}
2211+
}
2212+
exit(1);
2213+
}
2214+
21882215
/* Parse a command; return a Command struct, or NULL if it's a comment */
21892216
staticCommand*
21902217
process_commands(char*buf,constchar*source,constintlineno)
@@ -2229,6 +2256,7 @@ process_commands(char *buf, const char *source, const int lineno)
22292256

22302257
while (tok!=NULL)
22312258
{
2259+
my_commands->cols[j]=tok-buf+1;
22322260
my_commands->argv[j++]=pg_strdup(tok);
22332261
my_commands->argc++;
22342262
if (max_args >=0&&my_commands->argc >=max_args)
@@ -2246,9 +2274,10 @@ process_commands(char *buf, const char *source, const int lineno)
22462274

22472275
if (my_commands->argc<4)
22482276
{
2249-
fprintf(stderr,"%s: missing argument\n",my_commands->argv[0]);
2250-
exit(1);
2277+
syntax_error(source,lineno,my_commands->line,my_commands->argv[0],
2278+
"missing arguments",NULL,-1);
22512279
}
2280+
22522281
/* argc >= 4 */
22532282

22542283
if (my_commands->argc==4||/* uniform without/with "uniform" keyword */
@@ -2263,41 +2292,38 @@ process_commands(char *buf, const char *source, const int lineno)
22632292
{
22642293
if (my_commands->argc<6)
22652294
{
2266-
fprintf(stderr,"%s(%s): missing threshold argument\n",my_commands->argv[0],my_commands->argv[4]);
2267-
exit(1);
2295+
syntax_error(source,lineno,my_commands->line,my_commands->argv[0],
2296+
"missing threshold argument",my_commands->argv[4],-1);
22682297
}
22692298
elseif (my_commands->argc>6)
22702299
{
2271-
fprintf(stderr,"%s(%s): too many arguments (extra:",
2272-
my_commands->argv[0],my_commands->argv[4]);
2273-
for (j=6;j<my_commands->argc;j++)
2274-
fprintf(stderr," %s",my_commands->argv[j]);
2275-
fprintf(stderr,")\n");
2276-
exit(1);
2300+
syntax_error(source,lineno,my_commands->line,my_commands->argv[0],
2301+
"too many arguments",my_commands->argv[4],
2302+
my_commands->cols[6]);
22772303
}
22782304
}
22792305
else/* cannot parse, unexpected arguments */
22802306
{
2281-
fprintf(stderr,"%s: unexpected arguments (bad:",my_commands->argv[0]);
2282-
for (j=4;j<my_commands->argc;j++)
2283-
fprintf(stderr," %s",my_commands->argv[j]);
2284-
fprintf(stderr,")\n");
2285-
exit(1);
2307+
syntax_error(source,lineno,my_commands->line,my_commands->argv[0],
2308+
"unexpected argument",my_commands->argv[4],
2309+
my_commands->cols[4]);
22862310
}
22872311
}
22882312
elseif (pg_strcasecmp(my_commands->argv[0],"set")==0)
22892313
{
22902314
if (my_commands->argc<3)
22912315
{
2292-
fprintf(stderr,"%s: missing argument\n",my_commands->argv[0]);
2293-
exit(1);
2316+
syntax_error(source,lineno,my_commands->line,my_commands->argv[0],
2317+
"missing argument",NULL,-1);
22942318
}
22952319

2296-
expr_scanner_init(my_commands->argv[2]);
2320+
expr_scanner_init(my_commands->argv[2],source,lineno,
2321+
my_commands->line,my_commands->argv[0],
2322+
my_commands->cols[2]-1);
22972323

22982324
if (expr_yyparse()!=0)
22992325
{
2300-
fprintf(stderr,"%s: parse error\n",my_commands->argv[0]);
2326+
/* dead code: exit done from syntax_error called by yyerror */
23012327
exit(1);
23022328
}
23032329

@@ -2309,8 +2335,8 @@ process_commands(char *buf, const char *source, const int lineno)
23092335
{
23102336
if (my_commands->argc<2)
23112337
{
2312-
fprintf(stderr,"%s: missing argument\n",my_commands->argv[0]);
2313-
exit(1);
2338+
syntax_error(source,lineno,my_commands->line,my_commands->argv[0],
2339+
"missing argument",NULL,-1);
23142340
}
23152341

23162342
/*
@@ -2339,12 +2365,13 @@ process_commands(char *buf, const char *source, const int lineno)
23392365
pg_strcasecmp(my_commands->argv[2],"ms")!=0&&
23402366
pg_strcasecmp(my_commands->argv[2],"s")!=0)
23412367
{
2342-
fprintf(stderr,"%s: unknown time unit '%s' - must be us, ms or s\n",
2343-
my_commands->argv[0],my_commands->argv[2]);
2344-
exit(1);
2368+
syntax_error(source,lineno,my_commands->line,my_commands->argv[0],
2369+
"unknown time unit, must be us, ms or s",
2370+
my_commands->argv[2],my_commands->cols[2]);
23452371
}
23462372
}
23472373

2374+
/* this should be an error?! */
23482375
for (j=3;j<my_commands->argc;j++)
23492376
fprintf(stderr,"%s: extra argument \"%s\" ignored\n",
23502377
my_commands->argv[0],my_commands->argv[j]);
@@ -2353,22 +2380,22 @@ process_commands(char *buf, const char *source, const int lineno)
23532380
{
23542381
if (my_commands->argc<3)
23552382
{
2356-
fprintf(stderr,"%s: missing argument\n",my_commands->argv[0]);
2357-
exit(1);
2383+
syntax_error(source,lineno,my_commands->line,my_commands->argv[0],
2384+
"missing argument",NULL,-1);
23582385
}
23592386
}
23602387
elseif (pg_strcasecmp(my_commands->argv[0],"shell")==0)
23612388
{
23622389
if (my_commands->argc<1)
23632390
{
2364-
fprintf(stderr,"%s: missing command\n",my_commands->argv[0]);
2365-
exit(1);
2391+
syntax_error(source,lineno,my_commands->line,my_commands->argv[0],
2392+
"missing command",NULL,-1);
23662393
}
23672394
}
23682395
else
23692396
{
2370-
fprintf(stderr,"Invalid command %s\n",my_commands->argv[0]);
2371-
exit(1);
2397+
syntax_error(source,lineno,my_commands->line,my_commands->argv[0],
2398+
"invalid command",NULL,-1);
23722399
}
23732400
}
23742401
else

‎contrib/pgbench/pgbench.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ extern PgBenchExpr *expr_parse_result;
4747
externintexpr_yyparse(void);
4848
externintexpr_yylex(void);
4949
externvoidexpr_yyerror(constchar*str);
50-
externvoidexpr_scanner_init(constchar*str);
50+
externvoidexpr_scanner_init(constchar*str,constchar*source,
51+
constintlineno,constchar*line,
52+
constchar*cmd,constintecol);
53+
externvoidsyntax_error(constchar*source,constintlineno,constchar*line,
54+
constchar*cmd,constchar*msg,constchar*more,
55+
constintcol);
5156
externvoidexpr_scanner_finish(void);
5257

5358
externint64strtoint64(constchar*str);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp