[PATCH:xscope 04/24] Convert some for loops to use C99-style inline variable declarations
Mark Kettenis
mark.kettenis at xs4all.nl
Sat Sep 1 03:33:24 PDT 2012
> From: Alan Coopersmith <alan.coopersmith at oracle.com>
> Date: Fri, 31 Aug 2012 22:17:46 -0700
Please don't do this. Some OpenBSD platforms are still stuck with GCC
2.95.3. GCC 2.95.3 is almost a C99 compiler, but doesn't support the
C99 (mis)feature of allowing variable declarations after statements.
That's always been a controversial feature, although using it to
declare loop variables inside a for statement like you're doing here
is fairly widely accepted.
> Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
> ---
> common.c | 3 +--
> decode11.c | 14 ++++++--------
> decode_glx.c | 3 +--
> decode_render.c | 8 +++-----
> fd.c | 7 ++-----
> print_render.c | 3 +--
> prtype.c | 49 ++++++++++++++++---------------------------------
> scope.c | 15 +++++----------
> table11.c | 3 +--
> 9 files changed, 36 insertions(+), 69 deletions(-)
>
> diff --git a/common.c b/common.c
> index c0e9fd6..3c59c0b 100644
> --- a/common.c
> +++ b/common.c
> @@ -196,7 +196,6 @@ SetUpConnectionSocket(int iport, void (*connectionFunc) (int))
> #ifdef USE_XTRANS
> char port[20];
> int partial;
> - int i;
> #else
> FD ConnectionSocket;
> struct sockaddr_in sin;
> @@ -230,7 +229,7 @@ SetUpConnectionSocket(int iport, void (*connectionFunc) (int))
> if (ListenTransFds == NULL)
> panic("Can't allocate memory for ListenTransFds");
>
> - for (i = 0; i < ListenTransCount; i++) {
> + for (int i = 0; i < ListenTransCount; i++) {
> int fd = _X11TransGetConnectionNumber(ListenTransConns[i]);
>
> ListenTransFds[i] = fd;
> diff --git a/decode11.c b/decode11.c
> index 89a4c48..560ca45 100644
> --- a/decode11.c
> +++ b/decode11.c
> @@ -166,14 +166,12 @@ DumpReplyQ(FD fd)
> {
> fprintf(stderr, "ReplyQ[%d] = { Head 0x%lx; Tail 0x%lx }\n", fd,
> (unsigned long) ReplyQ[fd].Head, (unsigned long) ReplyQ[fd].Tail);
> - {
> - struct QueueEntry *p;
> -
> - for (p = ReplyQ[fd].Head; p != NULL; p = p->Next)
> - fprintf(stderr,
> - "0x%lx = { Next 0x%lx; SequenceNumber %ld; Request %d }\n",
> - (unsigned long) p, (unsigned long) p->Next,
> - (unsigned long) p->SequenceNumber, p->Request);
> +
> + for (struct QueueEntry *p = ReplyQ[fd].Head; p != NULL; p = p->Next) {
> + fprintf(stderr,
> + "0x%lx = { Next 0x%lx; SequenceNumber %ld; Request %d }\n",
> + (unsigned long) p, (unsigned long) p->Next,
> + (unsigned long) p->SequenceNumber, p->Request);
> }
> }
>
> diff --git a/decode_glx.c b/decode_glx.c
> index b0a732c..fbf749e 100644
> --- a/decode_glx.c
> +++ b/decode_glx.c
> @@ -267,7 +267,6 @@ void
> InitializeGLX(const unsigned char *buf)
> {
> TYPE p;
> - int errcode;
>
> GLXRequest = (unsigned char) (buf[9]);
> GLXEvent = (unsigned char) (buf[10]);
> @@ -355,7 +354,7 @@ InitializeGLX(const unsigned char *buf)
>
> InitializeExtensionDecoder(GLXRequest, glx_decode_req, glx_decode_reply);
> InitializeExtensionEventDecoder(GLXEvent, glx_decode_event);
> - for (errcode = GLXError; errcode < (GLXError + GLXNError); errcode++) {
> + for (int errcode = GLXError; errcode < (GLXError + GLXNError); errcode++) {
> InitializeExtensionErrorDecoder(errcode, glx_decode_error);
> }
> }
> diff --git a/decode_render.c b/decode_render.c
> index 5f62d39..12149e9 100644
> --- a/decode_render.c
> +++ b/decode_render.c
> @@ -355,10 +355,9 @@ static int
> PrintRENDERTRANSFORM(const unsigned char *buf)
> {
> const unsigned char *next = buf;
> - int i, j;
>
> - for (i = 0; i < 3; i++) {
> - for (j = 0; j < 3; j++) {
> + for (int i = 0; i < 3; i++) {
> + for (int j = 0; j < 3; j++) {
> long f = ILong(next);
>
> next += 4;
> @@ -378,7 +377,6 @@ void
> InitializeRENDER(const unsigned char *buf)
> {
> TYPE p;
> - int errcode;
>
> RENDERRequest = (unsigned char) (buf[9]);
> RENDERError = (unsigned char) (buf[11]);
> @@ -545,7 +543,7 @@ InitializeRENDER(const unsigned char *buf)
>
> InitializeExtensionDecoder(RENDERRequest, render_decode_req,
> render_decode_reply);
> - for (errcode = RENDERError; errcode < (RENDERError + RENDERNError);
> + for (int errcode = RENDERError; errcode < (RENDERError + RENDERNError);
> errcode++) {
> InitializeExtensionErrorDecoder(errcode, render_decode_error);
> }
> diff --git a/fd.c b/fd.c
> index c026b0f..00a68a2 100644
> --- a/fd.c
> +++ b/fd.c
> @@ -88,8 +88,6 @@
> void
> InitializeFD(void)
> {
> - int i;
> -
> enterprocedure("InitializeFD");
> /* get the number of file descriptors the system will let us use */
> #ifdef _SC_OPEN_MAX
> @@ -114,7 +112,7 @@ InitializeFD(void)
> }
>
> /* be sure all fd's are closed and marked not busy */
> - for (i = 0; i < MaxFD; i++) {
> + for (int i = 0; i < MaxFD; i++) {
> /* 0, 1, 2 are special (stdin, stdout, stderr) */
> if (i > 2)
> close(i);
> @@ -411,7 +409,6 @@ MainLoop(void)
> while (true) {
> fd_set rfds, wfds, xfds;
> short nfds;
> - short fd;
>
> /* wait for something */
>
> @@ -472,7 +469,7 @@ MainLoop(void)
> }
>
> /* check each fd to see if it has input */
> - for (fd = 0; fd <= HighestFD; fd++) {
> + for (short fd = 0; fd <= HighestFD; fd++) {
> /*
> check all returned fd's; this prevents
> starvation of later clients by earlier clients
> diff --git a/print_render.c b/print_render.c
> index f8d14e3..03a3867 100644
> --- a/print_render.c
> +++ b/print_render.c
> @@ -386,12 +386,11 @@ PrintGlyphs(const unsigned char *buf, int n, const char *name)
> {
> const unsigned char *gids;
> const unsigned char *glyphs;
> - int i;
>
> fprintf(stdout, "%s%20s:\n", Leader, name);
> gids = buf;
> glyphs = gids + 4 * n;
> - for (i = 0; i < n; i++) {
> + for (int i = 0; i < n; i++) {
> PrintField(gids, 0, 4, CARD32, "glyphid");
> PrintField(glyphs, 0, 2, CARD16, "width");
> PrintField(glyphs, 2, 2, CARD16, "height");
> diff --git a/prtype.c b/prtype.c
> index 1fb917d..ef7889a 100644
> --- a/prtype.c
> +++ b/prtype.c
> @@ -126,8 +126,6 @@ static short CurrentLevel = 0;
> void
> SetIndentLevel(short which)
> {
> - short i;
> -
> if (which > MaxIndent)
> which = MaxIndent;
> if (which < 0)
> @@ -137,7 +135,7 @@ SetIndentLevel(short which)
>
> /* set the indent level to <which> */
> /* -> set the Print Leader to <which> tabs */
> - for (i = 0; i < which; i++)
> + for (short i = 0; i < which; i++)
> Leader[i] = '\t';
> Leader[which] = '\0';
> CurrentLevel = which;
> @@ -281,12 +279,9 @@ int
> PrintSTR(const unsigned char *buf)
> {
> /* STR have the length (1 byte) then a string of CHAR8 */
> - short n;
> + short n = IByte(buf++);
>
> - short i;
> -
> - n = IByte(buf++);
> - for (i = 0; i < n; i++)
> + for (short i = 0; i < n; i++)
> fprintf(stdout, "%s", printrep(buf[i]));
> return (n + 1);
> }
> @@ -761,7 +756,6 @@ PrintList(const unsigned char *buf,
> long number, short ListType, const char *name)
> {
> long n;
> - long i;
> long sum;
>
> if (number == 0)
> @@ -773,7 +767,7 @@ PrintList(const unsigned char *buf,
>
> ModifyIndentLevel(1);
> sum = 0;
> - for (i = 0; i < number; i++) {
> + for (long i = 0; i < number; i++) {
> switch (TD[ListType].Type) {
> case BUILTIN:
> n = (*TD[ListType].PrintProc) (buf);
> @@ -807,7 +801,6 @@ long
> PrintListSTR(const unsigned char *buf, long number, const char *name)
> {
> long n;
> - long i;
> long sum;
>
> if (number == 0)
> @@ -819,7 +812,7 @@ PrintListSTR(const unsigned char *buf, long number, const char *name)
>
> ModifyIndentLevel(1);
> sum = 0;
> - for (i = 0; i < number; i++) {
> + for (long i = 0; i < number; i++) {
> fprintf(stdout, "%s", Leader);
> n = PrintSTR(buf);
> buf = buf + n;
> @@ -842,7 +835,6 @@ int
> PrintBytes(const unsigned char *buf, long number, const char *name)
> {
> /* print a list of BYTE -- 8-bit character */
> - long i;
> short column;
>
> if (number == 0)
> @@ -850,7 +842,7 @@ PrintBytes(const unsigned char *buf, long number, const char *name)
>
> fprintf(stdout, "%s%20s: ", Leader, name);
> column = SizeofLeader() + 25;
> - for (i = 0; i < number; i++) {
> + for (long i = 0; i < number; i++) {
> if (column > 80) {
> if (Verbose < 2)
> break;
> @@ -877,13 +869,11 @@ PrintBytes(const unsigned char *buf, long number, const char *name)
> int
> PrintString8(const unsigned char *buf, int number, const char *name)
> {
> - short i;
> -
> if (number == 0)
> return (0);
>
> fprintf(stdout, "%s%20s: \"", Leader, name);
> - for (i = 0; i < number; i++)
> + for (short i = 0; i < number; i++)
> fprintf(stdout, "%s", printrep(buf[i]));
> fprintf(stdout, "\"\n");
>
> @@ -895,15 +885,12 @@ PrintString8(const unsigned char *buf, int number, const char *name)
> int
> PrintString16(const unsigned char *buf, int number, const char *name)
> {
> - long i;
> - unsigned short c;
> -
> if (number == 0)
> return (0);
>
> fprintf(stdout, "%s%20s: \"", Leader, name);
> - for (i = 0; i < number * 2; i += 2) {
> - c = IChar2B(&buf[i]);
> + for (long i = 0; i < number * 2; i += 2) {
> + unsigned short c = IChar2B(&buf[i]);
> fprintf(stdout, "%s", printrep(c));
> }
> fprintf(stdout, "\"\n");
> @@ -914,7 +901,6 @@ PrintString16(const unsigned char *buf, int number, const char *name)
> void
> PrintTString8(const unsigned char *buf, long number, const char *name)
> {
> - long i;
> int off;
>
> if (number == 0)
> @@ -924,7 +910,7 @@ PrintTString8(const unsigned char *buf, long number, const char *name)
> if (TranslateText)
> off = 0x20;
> fprintf(stdout, "%s%20s: \"", Leader, name);
> - for (i = 0; i < number; i++)
> + for (long i = 0; i < number; i++)
> fprintf(stdout, "%s", printrep(buf[i] + off));
> fprintf(stdout, "\"\n");
> }
> @@ -933,8 +919,6 @@ PrintTString8(const unsigned char *buf, long number, const char *name)
> void
> PrintTString16(const unsigned char *buf, long number, const char *name)
> {
> - long i;
> - unsigned short c;
> int off;
>
> if (number == 0)
> @@ -944,8 +928,8 @@ PrintTString16(const unsigned char *buf, long number, const char *name)
> if (TranslateText)
> off = 0x20;
> fprintf(stdout, "%s%20s: \"", Leader, name);
> - for (i = 0; i < number * 2; i += 2) {
> - c = IChar2B(&buf[i]);
> + for (long i = 0; i < number * 2; i += 2) {
> + unsigned short c = IChar2B(&buf[i]);
> fprintf(stdout, "%s", printrep(c + off));
> }
> fprintf(stdout, "\"\n");
> @@ -1066,12 +1050,11 @@ PrintTextList16(const unsigned char *buf, int length, const char *name)
> void
> DumpHexBuffer(const unsigned char *buf, long n)
> {
> - long i;
> - short column;
> - char h[6]; /* one hex or octal character */
> + short column = 27 + SizeofLeader();
> +
> + for (long i = 0; i < n; i++) {
> + char h[6]; /* one hex or octal character */
>
> - column = 27 + SizeofLeader();
> - for (i = 0; i < n; i++) {
> /* get the hex representations */
> sprintf(h, "%02x", (0xff & buf[i]));
>
> diff --git a/scope.c b/scope.c
> index 2f9befc..ab3b805 100644
> --- a/scope.c
> +++ b/scope.c
> @@ -225,9 +225,7 @@ CMDStringToInt(char *s, int *v)
> static CMDFuncPtr
> CMDStringToFunc(const char *name)
> {
> - int i;
> -
> - for (i = 0; i < NumCMDFuncs; i++) {
> + for (int i = 0; i < NumCMDFuncs; i++) {
> if (!strcmp(name, CMDFuncs[i].name) || !strcmp(name, CMDFuncs[i].alias)) {
> return &CMDFuncs[i];
> }
> @@ -272,15 +270,14 @@ CMDSplitIntoWords(char *line, char **argv)
> static CMDResult
> CMDHelp(int argc, char **argv)
> {
> - int i;
> CMDFuncPtr func;
>
> if (argc == 1) {
> - for (i = 0; i < NumCMDFuncs; i++)
> + for (int i = 0; i < NumCMDFuncs; i++)
> printf("%-10s%s\n", CMDFuncs[i].name, CMDFuncs[i].usage);
> }
> else {
> - for (i = 1; i < argc; i++) {
> + for (int i = 1; i < argc; i++) {
> func = CMDStringToFunc(argv[i]);
> if (!func) {
> printf("%-10s unknown command\n", argv[i]);
> @@ -389,13 +386,11 @@ static void
> setBreakPoint(void)
> {
> Boolean b = false;
> - BP *bp;
> - FD fd;
>
> if (SingleStep)
> b = true;
> else {
> - for (bp = breakPoints; bp; bp = bp->next) {
> + for (BP *bp = breakPoints; bp; bp = bp->next) {
> if (bp->enabled) {
> b = true;
> break;
> @@ -404,7 +399,7 @@ setBreakPoint(void)
> }
> if (b != BreakPoint) {
> BreakPoint = b;
> - for (fd = 0; fd < HighestFD; fd++) {
> + for (FD fd = 0; fd < HighestFD; fd++) {
> if (FDD[fd].Busy && FDD[fd].InputHandler == DataFromClient) {
> if (BreakPoint)
> SetBufLimit(fd);
> diff --git a/table11.c b/table11.c
> index 3d9d99c..aaf7a48 100644
> --- a/table11.c
> +++ b/table11.c
> @@ -144,14 +144,13 @@ void
> CreateValueRec(unsigned long key, int size, const unsigned long *def)
> {
> ValuePtr *bucket, value;
> - int i;
>
> bucket = &buckets[HASH(key)];
> value = malloc(sizeof(ValueRec) + (size * sizeof(unsigned long)));
> if (!value)
> return;
> value->values = (unsigned long *) (value + 1);
> - for (i = 0; i < size; i++)
> + for (int i = 0; i < size; i++)
> value->values[i] = ILong((const unsigned char *) (def + i));
> value->size = size;
> value->key = key;
> --
> 1.7.9.2
>
> _______________________________________________
> xorg-devel at lists.x.org: X.Org development
> Archives: http://lists.x.org/archives/xorg-devel
> Info: http://lists.x.org/mailman/listinfo/xorg-devel
>
More information about the xorg-devel
mailing list