home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / os / vms / 20723 < prev    next >
Encoding:
Text File  |  1993-01-11  |  3.2 KB  |  80 lines

  1. Path: sparky!uunet!stanford.edu!agate!ucbvax!lrw.com!leichter
  2. From: leichter@lrw.com (Jerry Leichter)
  3. Newsgroups: comp.os.vms
  4. Subject: RE: 'ASSUME' macro in VAXC?
  5. Message-ID: <9301111547.AA20670@uu3.psi.com>
  6. Date: 11 Jan 93 14:21:11 GMT
  7. Sender: daemon@ucbvax.BERKELEY.EDU
  8. Distribution: world
  9. Organization: The Internet
  10. Lines: 68
  11.  
  12.  
  13.     Does anybody know any way of implementing compile-time checking in
  14.     VAXC  similar to the ASSUME macro defined in LIB.MLB (or is it
  15.     STARLET?)?
  16.  
  17. Here as example taken from one of my programs:
  18. #define MAXPROCBITS    10    /* Bits set aside to hold process number */
  19. #define MAXPROC         64    /* Maximum number of processes         */
  20.  
  21. #if (MAXPROC > (1 << MAXPROCBITS))
  22. #include ">>> MAXPROC set too large <<<"
  23. #endif
  24.  
  25. The #include will fail - I know of no system which will accept that particular
  26. string as a valid file name - and typically C compilers will tell you what
  27. file they couldn't open.
  28.  
  29. Many C compilers, and all ANSI C conformant compilers, support a #error
  30. directive, which provides a standard way to do the same thing.  (Compilers
  31. that DON'T support #error will, of course, also produce and error message,
  32. but it may not include the rest of the line.)
  33.  
  34.     What I want to check for specifically is that the size of a certain
  35.     struct is equal to 512 bytes and if not, print a warning message.
  36.  
  37.     Say:  check sizeof(struct foo) "Error: foo is the wrong size"
  38.  
  39. Unfortunately, there's no way to do this at compile time.  The only compile-
  40. time checking you can do is in pre-processing directives, and because of the
  41. way the first Unix C compilers were designed - with a stand-alone pre-proces-
  42. sor - the pre-processors cannot be assumed to have access to information
  43. that may be machine-dependent, or even compilation-mode dependent.  The sizes
  44. of different objects are, unfortunately, examples of this kind of information.
  45.  
  46. The best you can do is use the (ANSI standard, widely available) assert()
  47. macro, defined in assert.h.  This will result in a run-time error message,
  48. rather than a compile-time error message, but if the test involved can
  49. actually be resolved by the compiler (as would be the case if you were
  50. simply comparing the size of some object to a constant) the test and the code
  51. to print an error message should be removed as part of dead code elimination.
  52.  
  53. Hint to compiler writers:  If the compiler can prove that assert() WILL
  54. produce an error message, how about printing it at compile time, at least as
  55. an option?
  56.  
  57.     I don't really mind how kludgy the solution would have to be, as long
  58.     as it works.
  59.  
  60.  
  61. The same system that includes the code I showed above has the following code
  62. as part of its startup sequence:
  63.  
  64. #define NOTQUAD(x) (((int)(x) & 7) != 0)
  65.         if (NOTQUAD(sizeof(INBLOCK))
  66.          || NOTQUAD(sizeof(SYNC_QLM))
  67.          || NOTQUAD(&Common->freeib)
  68.          || NOTQUAD(&Common->buckets[0])
  69.          || NOTQUAD(&Common->ib[0])
  70.          || NOTQUAD(CS_R2A(Common->freeQ))
  71.                    )
  72.                         BUGCHECK("Common section data not aligned")
  73.  
  74. Messy, but about the best you can do.  At least this way, if there's a problem
  75. the code falls over very early on, in a known way, rather than at some random
  76. time during execution when some data object that has to be quadword aligned
  77. turns out not to be.
  78.                             -- Jerry
  79.  
  80.