Commits

Andrew Dunstan committed 1c38265

Provide Assert() for frontend code.

Per discussion on-hackers. psql is converted to use the new code.

Follows a suggestion from Heikki Linnakangas.

Comments (0)

Files changed (7)

src/bin/psql/command.c

 	char	   *cmd;
 	char	   *arg;
 
-	psql_assert(scan_state);
+	Assert(scan_state != NULL);
 
 	/* Parse off the command name */
 	cmd = psql_scan_slash_command(scan_state);
 	char	   *sys;
 	int			result;
 
-	psql_assert(fname);
+	Assert(fname != NULL);
 
 	/* Find an editor to use */
 	editorName = getenv("PSQL_EDITOR");
 {
 	size_t		vallen = 0;
 
-	psql_assert(param);
+	Assert(param != NULL);
 
 	if (value)
 		vallen = strlen(value);

src/bin/psql/common.c

 			}
 
 			OK = AcceptResult(results);
-			psql_assert(!OK);
+			Assert(!OK);
 			PQclear(results);
 			break;
 		}

src/bin/psql/common.h

 #include <setjmp.h>
 #include "libpq-fe.h"
 
-#ifdef USE_ASSERT_CHECKING
-#include <assert.h>
-#define psql_assert(p) assert(p)
-#else
-#define psql_assert(p)
-#endif
-
 #define atooid(x)  ((Oid) strtoul((x), NULL, 10))
 
 /*

src/bin/psql/psqlscan.l

 				const char *line, int line_len)
 {
 	/* Mustn't be scanning already */
-	psql_assert(state->scanbufhandle == NULL);
-	psql_assert(state->buffer_stack == NULL);
+	Assert(state->scanbufhandle == NULL);
+	Assert(state->buffer_stack == NULL);
 
 	/* Do we need to hack the character set encoding? */
 	state->encoding = pset.encoding;
 	int			lexresult;
 
 	/* Must be scanning already */
-	psql_assert(state->scanbufhandle);
+	Assert(state->scanbufhandle != NULL);
 
 	/* Set up static variables that will be used by yylex */
 	cur_state = state;
 	PQExpBufferData mybuf;
 
 	/* Must be scanning already */
-	psql_assert(state->scanbufhandle);
+	Assert(state->scanbufhandle != NULL);
 
 	/* Build a local buffer that we'll return the data of */
 	initPQExpBuffer(&mybuf);
 	char		local_quote;
 
 	/* Must be scanning already */
-	psql_assert(state->scanbufhandle);
+	Assert(state->scanbufhandle != NULL);
 
 	if (quote == NULL)
 		quote = &local_quote;
 	 * or LEXRES_EOL (the latter indicating end of string).  If we were inside
 	 * a quoted string, as indicated by YY_START, EOL is an error.
 	 */
-	psql_assert(lexresult == LEXRES_EOL || lexresult == LEXRES_OK);
+	Assert(lexresult == LEXRES_EOL || lexresult == LEXRES_OK);
 
 	switch (YY_START)
 	{
 psql_scan_slash_command_end(PsqlScanState state)
 {
 	/* Must be scanning already */
-	psql_assert(state->scanbufhandle);
+	Assert(state->scanbufhandle != NULL);
 
 	/* Set up static variables that will be used by yylex */
 	cur_state = state;

src/bin/psql/stringutils.c

 	char	   *src;
 	char	   *dst;
 
-	psql_assert(source);
-	psql_assert(quote);
+	Assert(source != NULL);
+	Assert(quote != '\0');
 
 	src = dst = source;
 
 	char	   *dst;
 	bool		need_quotes = false;
 
-	psql_assert(source);
-	psql_assert(quote);
+	Assert(source != NULL);
+	Assert(quote != '\0');
 
 	src = source;
 	dst = ret = pg_malloc(2 * strlen(src) + 3); /* excess */

src/bin/psql/tab-complete.c

 	const char *item;
 
 	/* need to have a list */
-	psql_assert(completion_charpp);
+	Assert(completion_charpp != NULL);
 
 	/* Initialization */
 	if (state == 0)
 static char *
 complete_from_const(const char *text, int state)
 {
-	psql_assert(completion_charp);
+	Assert(completion_charp != NULL);
 	if (state == 0)
 	{
 		if (completion_case_sensitive)
 		/* expect a NULL return for the empty string only */
 		if (!unquoted_text)
 		{
-			psql_assert(!*text);
+			Assert(*text == '\0');
 			unquoted_text = text;
 		}
 	}

src/include/postgres_fe.h

 
 #include "c.h"
 
+/*
+ * Assert() can be used in both frontend and backend code. In frontend code it
+ * just calls the standard assert, if it's available. If use of assertions is
+ * not configured, it does nothing.
+ */
+#ifdef USE_ASSERT_CHECKING
+#include <assert.h>
+#define Assert(p) assert(p)
+#else
+#define Assert(p)
+#endif
+
 #endif   /* POSTGRES_FE_H */