cleanup: Add and use macro MIN().
authorChristoph Mallon <christoph.mallon@gmx.de>
Tue, 25 Dec 2012 19:13:27 +0000 (20:13 +0100)
committerChristoph Mallon <christoph.mallon@gmx.de>
Thu, 27 Dec 2012 14:56:08 +0000 (15:56 +0100)
adt/util.h
ast.c
input.c
main.c
printer.c

index 403a14f..05c40f1 100644 (file)
@@ -39,6 +39,8 @@
 #define endof(x) ((x) + lengthof(x))
 
 #undef MAX
+#undef MIN
 #define MAX(a, b) ((a) > (b) ? (a) : (b))
+#define MIN(a, b) ((a) < (b) ? (a) : (b))
 
 #endif
diff --git a/ast.c b/ast.c
index ebcb03c..baa472d 100644 (file)
--- a/ast.c
+++ b/ast.c
@@ -1454,9 +1454,7 @@ expression_classification_t is_constant_initializer(const initializer_t *initial
                for (size_t i = 0; i < initializer->list.len; ++i) {
                        initializer_t *sub_initializer = initializer->list.initializers[i];
                        expression_classification_t const cur = is_constant_initializer(sub_initializer);
-                       if (all > cur) {
-                               all = cur;
-                       }
+                       all = MIN(all, cur);
                }
                return all;
        }
@@ -1499,7 +1497,7 @@ static expression_classification_t is_object_with_linker_constant_address(
        case EXPR_ARRAY_ACCESS: {
                expression_classification_t const ref = is_linker_constant(expression->array_access.array_ref);
                expression_classification_t const idx = is_constant_expression(expression->array_access.index);
-               return ref < idx ? ref : idx;
+               return MIN(ref, idx);
        }
 
        case EXPR_REFERENCE: {
@@ -1578,11 +1576,11 @@ expression_classification_t is_linker_constant(const expression_t *expression)
                if (is_type_pointer(ltype)) {
                        expression_classification_t const l = is_linker_constant(left);
                        expression_classification_t const r = is_constant_expression(right);
-                       return l < r ? l : r;
+                       return MIN(l, r);
                } else if (is_type_pointer(rtype)) {
                        expression_classification_t const l = is_constant_expression(left);
                        expression_classification_t const r = is_linker_constant(right);
-                       return l < r ? l : r;
+                       return MIN(l, r);
                } else if (!is_type_valid(ltype) || !is_type_valid(rtype)) {
                        return EXPR_CLASS_ERROR;
                } else {
@@ -1872,7 +1870,7 @@ check_type:
        case EXPR_BINARY_ISUNORDERED: {
                expression_classification_t const l = is_constant_expression(expression->binary.left);
                expression_classification_t const r = is_constant_expression(expression->binary.right);
-               return l < r ? l : r;
+               return MIN(l, r);
        }
 
        case EXPR_BINARY_LOGICAL_AND: {
@@ -1885,7 +1883,7 @@ check_type:
                        return EXPR_CLASS_INTEGER_CONSTANT;
                if (!fold_constant_to_bool(left))
                        return EXPR_CLASS_CONSTANT;
-               return rcls < EXPR_CLASS_CONSTANT ? rcls : EXPR_CLASS_CONSTANT;
+               return MIN(rcls, EXPR_CLASS_CONSTANT);
        }
 
        case EXPR_BINARY_LOGICAL_OR: {
@@ -1898,7 +1896,7 @@ check_type:
                        return EXPR_CLASS_INTEGER_CONSTANT;
                if (fold_constant_to_bool(left))
                        return EXPR_CLASS_CONSTANT;
-               return rcls < EXPR_CLASS_CONSTANT ? rcls : EXPR_CLASS_CONSTANT;
+               return MIN(rcls, EXPR_CLASS_CONSTANT);
        }
 
        case EXPR_COMPOUND_LITERAL:
@@ -1917,7 +1915,7 @@ check_type:
                    fcls == EXPR_CLASS_INTEGER_CONSTANT)
                        return EXPR_CLASS_INTEGER_CONSTANT;
                expression_classification_t const cls = fold_constant_to_bool(cond) ? tcls : fcls;
-               return cls < EXPR_CLASS_CONSTANT ? cls : EXPR_CLASS_CONSTANT;
+               return MIN(cls, EXPR_CLASS_CONSTANT);
        }
 
        case EXPR_ERROR:
diff --git a/input.c b/input.c
index b405f6b..693b180 100644 (file)
--- a/input.c
+++ b/input.c
@@ -4,6 +4,7 @@
  */
 #include "config.h"
 
+#include "adt/util.h"
 #include "diagnostic.h"
 #include "input.h"
 
@@ -54,8 +55,7 @@ static size_t read_block(input_t *input, unsigned char *const read_buf,
        } else {
                assert(input->kind == INPUT_STRING);
                size_t len = strlen(input->in.string);
-               if (len > n)
-                       len = n;
+               len = MIN(len, n);
                memcpy(read_buf, input->in.string, len);
                input->in.string += len;
                return len;
diff --git a/main.c b/main.c
index 876416a..a1e4e24 100644 (file)
--- a/main.c
+++ b/main.c
@@ -360,8 +360,7 @@ static bool run_external_preprocessor(compilation_unit_t *unit)
                static char dep_target[4096];
                if (outname != 0) {
                        size_t len = strlen(outname);
-                       if (len > sizeof(dep_target)-4) /* leave room for .d extension */
-                               len = sizeof(dep_target)-4;
+                       len = MIN(len, sizeof(dep_target) - 4); /* leave room for .d extension */
                        memcpy(dep_target, outname, len);
                        /* replace extension with .d if found */
                        char *dot = &dep_target[len-1];
index 869c692..414023c 100644 (file)
--- a/printer.c
+++ b/printer.c
@@ -4,6 +4,7 @@
  */
 #include "config.h"
 
+#include "adt/util.h"
 #include "printer.h"
 
 #include <stdio.h>
@@ -85,7 +86,7 @@ static void print_vformat_buffer(const char *format, va_list ap)
 {
        size_t size    = buffer_end - buffer_pos;
        size_t written = (size_t) vsnprintf(buffer_pos, size, format, ap);
-       buffer_pos    += written < size ? written : size;
+       buffer_pos    += MIN(written, size);
 }
 
 void print_to_buffer(char *buffer, size_t buffer_size)