Skip to content

Commit

Permalink
Define MIN() and MAX() correctly and exactly once (#1795)
Browse files Browse the repository at this point in the history
Instead of many .c and .h files defining, undefing and redefining MIN()
and MAX() have <grass/gis.h> (re)define each macro. Besides the obvious
code deduplication, this addresses a number of compiler warnings:

main.c:51: warning: "MIN" redefined
main.c:52: warning: "MAX" redefined
main.c:30: warning: "MIN" redefined
main.c:31: warning: "MAX" redefined
contour.h:10: warning: "MIN" redefined
contour.h:10: warning: "MIN" redefined
contour.h:10: warning: "MIN" redefined
contour.h:10: warning: "MIN" redefined
contour.h:10: warning: "MIN" redefined
frac.h:19: warning: "MAX" redefined
frac.h:19: warning: "MAX" redefined
frac.h:19: warning: "MAX" redefined
frac.h:19: warning: "MAX" redefined

This also addresses another, less apparent problem: macro definitions
that lack sufficient parenthesis around their arguments are a classic
error. For example, this definition evaluates incorrectly when b is a
ternary conditional itself:

 #define MAX(a,b) (a > b ? a : b)

Resolve the name clash in raster/r.stats.zonal/main.c with a prefix and
fixup indentation while at it.
  • Loading branch information
infrastation committed Aug 25, 2021
1 parent c863b71 commit 3089db7
Show file tree
Hide file tree
Showing 33 changed files with 90 additions and 219 deletions.
7 changes: 0 additions & 7 deletions display/d.linegraph/main.c
Expand Up @@ -41,13 +41,6 @@
#include <grass/glocale.h>
#include "linegraph.h"

#ifndef MAX
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#endif
#ifndef MIN
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#endif

/* the default order of precedence of colors to use for Y lines */
int default_y_colors[] = {
0,
Expand Down
9 changes: 0 additions & 9 deletions imagery/i.segment/region_growing.c
Expand Up @@ -16,15 +16,6 @@

#define EPSILON 1.0e-8

#ifdef MAX
#undef MAX
#endif
#define MAX(a,b) ( ((a) > (b)) ? (a) : (b) )
#ifdef MIN
#undef MIN
#endif
#define MIN(a,b) ( ((a) < (b)) ? (a) : (b) )

struct idlist
{
int *ids;
Expand Down
10 changes: 10 additions & 0 deletions include/grass/gis.h
Expand Up @@ -129,6 +129,16 @@ static const char *GRASS_copyright __attribute__ ((unused))
#define CONFIG_DIR ".grass8"
#endif

#ifdef MAX
#undef MAX
#endif
#define MAX(a, b) ((a) > (b) ? (a) : (b))

#ifdef MIN
#undef MIN
#endif
#define MIN(a, b) ((a) < (b) ? (a) : (b))

/* define PI and friends */
#undef M_PI
#define M_PI 3.14159265358979323846 /* pi */
Expand Down
5 changes: 0 additions & 5 deletions lib/btree2/kdtree.c
Expand Up @@ -20,11 +20,6 @@
#include <grass/glocale.h>
#include "kdtree.h"

#ifdef MAX
#undef MAX
#endif
#define MAX(a, b) ((a) > (b) ? (a) : (b))

#define KD_BTOL 7

#ifdef KD_DEBUG
Expand Down
5 changes: 1 addition & 4 deletions lib/cairodriver/line_width.c
Expand Up @@ -12,14 +12,11 @@
\author Glynn Clements
*/

#include <grass/gis.h>
#include "cairodriver.h"

#define MIN_WIDTH 1

#ifndef MAX
#define MAX(a,b) ((a)>(b)?(a):(b))
#endif

static double previous_width = -1;

/*!
Expand Down
7 changes: 1 addition & 6 deletions lib/cairodriver/raster.c
Expand Up @@ -15,15 +15,10 @@
#include <math.h>

#include "cairodriver.h"
#include <grass/gis.h>
#include <grass/glocale.h>

#define MAX_IMAGE_SIZE 32767
#ifndef MIN
#define MIN(a,b) ((a)<(b)?(a):(b))
#endif
#ifndef MAX
#define MAX(a,b) ((a)>(b)?(a):(b))
#endif

static int src_t, src_b, src_l, src_r, src_w, src_h;
static int dst_t, dst_b, dst_l, dst_r, dst_w, dst_h;
Expand Down
7 changes: 0 additions & 7 deletions lib/imagery/georef_tps.c
Expand Up @@ -42,13 +42,6 @@ struct MATRIX

#define MAXORDER 3 /* HIGHEST SUPPORTED ORDER OF TRANSFORMATION */

#ifndef MAX
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#endif
#ifndef MIN
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#endif

/***********************************************************************
FUNCTION PROTOTYPES FOR STATIC (INTERNAL) FUNCTIONS
Expand Down
5 changes: 0 additions & 5 deletions lib/raster/fpreclass.c
Expand Up @@ -246,11 +246,6 @@

/*--------------------------------------------------------------------------*/

#undef MIN
#undef MAX
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b))

#define NO_DEFAULT_RULE (! r->defaultDRuleSet)
#define NO_LEFT_INFINITE_RULE (! r->infiniteLeftSet)
#define NO_RIGHT_INFINITE_RULE (! r->infiniteRightSet)
Expand Down
5 changes: 0 additions & 5 deletions lib/raster/quant.c
Expand Up @@ -27,11 +27,6 @@ static int double_comp(const void *, const void *);
#define MAX_LOOKUP_TABLE_SIZE 2048
#define NO_DATA (Rast_set_c_null_value (&tmp, 1), (CELL) tmp)

#undef MIN
#undef MAX
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b))

#define NO_LEFT_INFINITE_RULE (! q->infiniteLeftSet)
#define NO_RIGHT_INFINITE_RULE (! q->infiniteRightSet)
#define NO_FINITE_RULE (q->nofRules <= 0)
Expand Down
7 changes: 1 addition & 6 deletions lib/raster3d/tilewrite.c
Expand Up @@ -3,6 +3,7 @@
#include <sys/types.h>
#include <unistd.h>

#include <grass/gis.h>
#include <grass/raster.h>
#include "raster3d_intern.h"

Expand Down Expand Up @@ -276,12 +277,6 @@ int Rast3d_flush_tile(RASTER3D_Map * map, int tileIndex)

/*---------------------------------------------------------------------------*/

#ifndef MIN
#define MIN(a,b) (a < b ? a : b)
#define MAX(a,b) (a > b ? a : b)
#endif


/*!
* \brief
*
Expand Down
7 changes: 1 addition & 6 deletions lib/vector/Vlib/buffer2.c
Expand Up @@ -18,18 +18,13 @@

#include <stdlib.h>
#include <math.h>
#include <grass/gis.h>
#include <grass/vector.h>
#include <grass/glocale.h>

#include "dgraph.h"

#define LENGTH(DX, DY) (sqrt((DX*DX)+(DY*DY)))
#ifndef MIN
#define MIN(X,Y) ((X<Y)?X:Y)
#endif
#ifndef MAX
#define MAX(X,Y) ((X>Y)?X:Y)
#endif
#define PI M_PI
#define RIGHT_SIDE 1
#define LEFT_SIDE -1
Expand Down
7 changes: 1 addition & 6 deletions lib/vector/Vlib/dgraph.c
Expand Up @@ -16,18 +16,13 @@
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <grass/gis.h>
#include <grass/vector.h>
#include <grass/glocale.h>
#include "dgraph.h"
#include "e_intersect.h"

#define LENGTH(DX, DY) (sqrt((DX*DX)+(DY*DY)))
#ifndef MIN
#define MIN(X,Y) ((X<Y)?X:Y)
#endif
#ifndef MAX
#define MAX(X,Y) ((X>Y)?X:Y)
#endif
#define PI M_PI

struct intersection_point
Expand Down
6 changes: 0 additions & 6 deletions lib/vector/Vlib/e_intersect.c
Expand Up @@ -21,12 +21,6 @@
#include "e_intersect.h"

#define SWAP(a,b) {double t = a; a = b; b = t;}
#ifndef MIN
#define MIN(X,Y) ((X<Y)?X:Y)
#endif
#ifndef MAX
#define MAX(X,Y) ((X>Y)?X:Y)
#endif
#define D ((ax2-ax1)*(by1-by2) - (ay2-ay1)*(bx1-bx2))
#define DA ((bx1-ax1)*(by1-by2) - (by1-ay1)*(bx1-bx2))
#define DB ((ax2-ax1)*(by1-ay1) - (ay2-ay1)*(bx1-ax1))
Expand Down
5 changes: 1 addition & 4 deletions lib/vector/dglib/graph.c
Expand Up @@ -30,6 +30,7 @@

#define DGL_V2 1

#include <grass/gis.h>
#include "type.h"
#include "tree.h"
#include "graph.h"
Expand Down Expand Up @@ -1687,10 +1688,6 @@ int dglWriteChunk(dglIOContext_s * pIO, dglWriteChunk_fn pfn, void *pv)
return 0;
}

#ifndef MIN
#define MIN(x,y) (((x)<(y))?x:y)
#endif

int dglReadChunk(dglIOContext_s * pIO, dglByte_t * pbChunk, int cbChunk)
{
int i, c;
Expand Down
3 changes: 1 addition & 2 deletions lib/vector/rtree/rect.c
Expand Up @@ -23,13 +23,12 @@

#include <float.h>
#include <math.h>
#include <grass/gis.h>

#define BIG_NUM (FLT_MAX/4.0)


#define Undefined(x, t) ((x)->boundary[0] > (x)->boundary[t->ndims_alloc])
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))

/*!
\brief Create a new rectangle for a given tree
Expand Down
6 changes: 1 addition & 5 deletions raster/r.buildvrt/proto.h
@@ -1,10 +1,6 @@
#include <grass/gis.h>
#include <grass/raster.h>

#undef MIN
#undef MAX
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b))

struct input
{
const char *name;
Expand Down
6 changes: 1 addition & 5 deletions raster/r.external/proto.h
@@ -1,10 +1,6 @@
#include <grass/gis.h>
#include <grass/raster.h>

#undef MIN
#undef MAX
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b))

struct band_info
{
RASTER_MAP_TYPE data_type;
Expand Down
8 changes: 0 additions & 8 deletions raster/r.geomorphon/local_proto.h
Expand Up @@ -27,14 +27,6 @@
#define DEGREE2RAD(a) ((a)/(180/PI))
#define RAD2DEGREE(a) ((a)*(180/PI))

#ifndef MAX
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#endif

#ifndef MIN
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#endif

/* Number of cardinal directions. */
#define NUM_DIRS 8

Expand Down
3 changes: 0 additions & 3 deletions raster/r.grow.distance/main.c
Expand Up @@ -37,9 +37,6 @@ static DCELL *old_val_row, *new_val_row;
static double (*distance) (double dx, double dy);
static double xres, yres;

#undef MAX
#define MAX(a, b) ((a) > (b) ? (a) : (b))

static double distance_euclidean_squared(double dx, double dy)
{
return dx * dx + dy * dy;
Expand Down
5 changes: 0 additions & 5 deletions raster/r.in.gdal/main.c
Expand Up @@ -32,11 +32,6 @@
#include <gdal.h>
#include <cpl_conv.h>

#undef MIN
#undef MAX
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b))

void check_projection(struct Cell_head *cellhd, GDALDatasetH hDS,
char *outloc, int create_only, int override,
int check_only);
Expand Down
3 changes: 0 additions & 3 deletions raster/r.out.pov/main.c
Expand Up @@ -48,9 +48,6 @@ void processProfiles(int inputFile, FILE * outputF);
#define YMAX 65536 /* max length scan line */
#define XMAX 65536 /* max # of scan lines */

#define MIN(x,y) (((x) < (y)) ? (x) : (y))
#define MAX(x,y) (((x) > (y)) ? (x) : (y))

#define SW 0
#define NW 1
#define NE 2
Expand Down
10 changes: 0 additions & 10 deletions raster/r.proj/bordwalk.c
Expand Up @@ -33,16 +33,6 @@
#include <grass/glocale.h>
#include "r.proj.h"

#ifdef MIN
#undef MIN
#endif
#define MIN(a, b) (((a) < (b)) ? (a) : (b))

#ifdef MAX
#undef MAX
#endif
#define MAX(a, b) (((a) > (b)) ? (a) : (b))

static void debug(const char *name, const struct Cell_head *hd)
{
G_debug(3, "%s: xmin: %f; xmax: %f; ymin: %f; ymax: %f",
Expand Down

0 comments on commit 3089db7

Please sign in to comment.