home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!stanford.edu!agate!ucbvax!lrw.com!leichter
- From: leichter@lrw.com (Jerry Leichter)
- Newsgroups: comp.os.vms
- Subject: RE: 'ASSUME' macro in VAXC?
- Message-ID: <9301111547.AA20670@uu3.psi.com>
- Date: 11 Jan 93 14:21:11 GMT
- Sender: daemon@ucbvax.BERKELEY.EDU
- Distribution: world
- Organization: The Internet
- Lines: 68
-
-
- Does anybody know any way of implementing compile-time checking in
- VAXC similar to the ASSUME macro defined in LIB.MLB (or is it
- STARLET?)?
-
- Here as example taken from one of my programs:
- #define MAXPROCBITS 10 /* Bits set aside to hold process number */
- #define MAXPROC 64 /* Maximum number of processes */
-
- #if (MAXPROC > (1 << MAXPROCBITS))
- #include ">>> MAXPROC set too large <<<"
- #endif
-
- The #include will fail - I know of no system which will accept that particular
- string as a valid file name - and typically C compilers will tell you what
- file they couldn't open.
-
- Many C compilers, and all ANSI C conformant compilers, support a #error
- directive, which provides a standard way to do the same thing. (Compilers
- that DON'T support #error will, of course, also produce and error message,
- but it may not include the rest of the line.)
-
- What I want to check for specifically is that the size of a certain
- struct is equal to 512 bytes and if not, print a warning message.
-
- Say: check sizeof(struct foo) "Error: foo is the wrong size"
-
- Unfortunately, there's no way to do this at compile time. The only compile-
- time checking you can do is in pre-processing directives, and because of the
- way the first Unix C compilers were designed - with a stand-alone pre-proces-
- sor - the pre-processors cannot be assumed to have access to information
- that may be machine-dependent, or even compilation-mode dependent. The sizes
- of different objects are, unfortunately, examples of this kind of information.
-
- The best you can do is use the (ANSI standard, widely available) assert()
- macro, defined in assert.h. This will result in a run-time error message,
- rather than a compile-time error message, but if the test involved can
- actually be resolved by the compiler (as would be the case if you were
- simply comparing the size of some object to a constant) the test and the code
- to print an error message should be removed as part of dead code elimination.
-
- Hint to compiler writers: If the compiler can prove that assert() WILL
- produce an error message, how about printing it at compile time, at least as
- an option?
-
- I don't really mind how kludgy the solution would have to be, as long
- as it works.
-
-
- The same system that includes the code I showed above has the following code
- as part of its startup sequence:
-
- #define NOTQUAD(x) (((int)(x) & 7) != 0)
- if (NOTQUAD(sizeof(INBLOCK))
- || NOTQUAD(sizeof(SYNC_QLM))
- || NOTQUAD(&Common->freeib)
- || NOTQUAD(&Common->buckets[0])
- || NOTQUAD(&Common->ib[0])
- || NOTQUAD(CS_R2A(Common->freeQ))
- )
- BUGCHECK("Common section data not aligned")
-
- Messy, but about the best you can do. At least this way, if there's a problem
- the code falls over very early on, in a known way, rather than at some random
- time during execution when some data object that has to be quadword aligned
- turns out not to be.
- -- Jerry
-
-