Print this page
5506 cstyle: warn about #if enumerating ISA defines

@@ -20,13 +20,11 @@
 # CDDL HEADER END
 #
 #
 # Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 # Use is subject to license terms.
-#
-# @(#)cstyle 1.58 98/09/09 (from shannon)
-#ident  "%Z%%M% %I%     %E% SMI"
+# Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
 #
 # cstyle - check for some common stylistic errors.
 #
 #       cstyle is a sort of "lint" for C coding style.
 #       It attempts to check for the style used in the

@@ -482,10 +480,28 @@
         # declaration.
         if (/\S.*\/\*/ && !/\S.*\/\*.*\*\// && !/\(\/\*/) {
                 err("unterminated single line comment");
         }
 
+        # check that #if and #elif don't enumerate ISA defines when there
+        # are more concise ways of checking.  E.g., don't do:
+        #     #if defined(__amd64) || defined(__i386)
+        # when there is:
+        #     #ifdef __x86
+        if ((/^(#if|#elif)\sdefined\((.*)\)\s\|\|\sdefined\((.*)\)/) ||
+            (/^(#if|#elif)\s!defined\((.*)\)\s&&\s!defined\((.*)\)/)) {
+                my $directive = $1;
+                my $first = $2;
+                my $second = $3;
+                ($first, $second) = ($second, $first) if ($first gt $second);
+
+                if (($first eq "__amd64") && ($second eq "__i386")) {
+                        err("$directive checking for $first or $second " .
+                            "instead of __x86");
+                }
+        }
+
         if (/^(#else|#endif|#include)(.*)$/) {
                 $prev = $line;
                 if ($picky) {
                         my $directive = $1;
                         my $clause = $2;