logo       

[ tidy-Patches-1239422 ] Cleanup: explicit casts, native booleans (C++), mi: msg#00050

web.html-tidy.tracker

Subject: [ tidy-Patches-1239422 ] Cleanup: explicit casts, native booleans (C++), minor fix

Patches item #1239422, was opened at 2005-07-16 13:51
Message generated for change (Comment added) made by arnaud02
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=390965&aid=1239422&group_id=27659

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: None
Group: Current - all platforms
Status: Open
Resolution: None
Priority: 5
Submitted By: Eric Hartwell (ehartwell)
Assigned to: Nobody/Anonymous (nobody)
Summary: Cleanup: explicit casts, native booleans (C++), minor fix

Initial Comment:
Html Tidy compiles as C++ with only a few minor
changes - mostly using explicit casts for type
conversion. Why bother?
- Explicit casts make code easier to read
- C++ provides compiler checks (lint
- step towards object version
- required for .NET native mode.

These changes pass the regression tests using Visual
Studio .NET 2.0 beta 2, when compiled as C (with
USE_NATIVE_BOOL undefined) and as C++ (with
USE_NATIVE_BOOL defined).

1. Explicit casts for type conversion:

C++ is especially fussy about type conversions. In
general, it's a good idea to be perfectly explicit about
integer <=> boolean, char * <=> void *, and so on.
Explicit casts can only help the compiler, and they
make the code easier to read.

1a. Minor fixes:

In tidylib.c, the functions tidySetOptionCallback() and
tidySetReportFilter() are defined as Bool, but in the
production source they return yes or -EINVAL (which
evaluates to -22). I don't think it's safe to assume -22
will evaluate to "no" under all platforms, although there
is no difference in my regression tests.

2. Use native boolean where available:

The current Bool enum is an inspired workaround for C
compiler portability, but it adds extra overhead when
your compiler (or language, in the case of C++) has a
built-in boolean type. To minimize side effects, I've
added a new preprocessor definition,
USE_NATIVE_BOOL. When this is defined, the code
uses native booleans:

#ifdef USE_NATIVE_BOOL
typedef bool Bool;
#define no false
#define yes true
#else /* otherwise, it defaults to the original enum: */
typedef enum
{
no,
yes
} Bool;
#endif

To simplify the integer / flag / Bool conversion (usually
at the return of a subroutine), I added a new macro,
ToBool(x).

#ifdef USE_NATIVE_BOOL
#define ToBool(x) ((x) != 0)
#else /* otherwise, it defaults to the original enum: */
#define ToBool(x) (Bool)(x)
#endif

CHANGE LIST

Changed files: platform.h, tidy.c, clean.c, config.c,
fileio.c, lexer.c, parser.c, pprint.c, streamio.c, tags.c,
tidylib.c
Diff files are attached.

1. Explicit casts for type conversion:

clean.c 2220 prop->name = (tmbstr)MemAlloc( 8 +
tmbstrlen(enc) + 1 );
config.c 610 tmbstr p = (tmbstr)MemAlloc( len );
lexer.c 1479 TidyDoctypeModes dtmode =
(TidyDoctypeModes)cfg(doc, TidyDoctypeMode);
streamio.c 118 in->charbuf = (tchar *)MemAlloc(sizeof
(tchar) * in->bufsize);
streamio.c 498 in->charbuf = (tchar *)MemRealloc(in-
>charbuf, sizeof(tchar) * ++(in->bufsize));
tidylib.c 875 tidyBufAttach( &inbuf, (byte*)content,
tmbstrlen(content)+1 );
tidy.c 67 char *c1buf = (char *)malloc(l1+1);
tidy.c 68 char *c2buf = (char *)malloc(l2+1);
tidy.c 86 char *c1buf = (char *)malloc(l1+1);
tidy.c 87 char *c2buf = (char *)malloc(l2+1);
tidy.c 88 char *c3buf = (char *)malloc(l3+1);
tidy.c 278 name = (tmbstr)malloc(len+1);
tidy.c 314 escpName = (tmbstr)malloc(len+1);
tidy.c 757 val = (tmbstr)malloc(len+1);
tidy.c 770 tmbstr val = (tmbstr)malloc(1+strlen(d->vals));

tidylib.c 184 /* return -EINVAL; Function returns Bool,
not int*/
tidylib.c 185 return no;
tidylib.c 616 /* return -EINVAL; Function returns Bool,
not int*/
tidylib.c 617 return no;

tidy.c 343 /* C++ C2675: unary '++' : 'CmdOptCategory'
does not define this operator or a conversion to a type
acceptable to the predefined operator */
tidy.c 344 #ifdef __cplusplus
tidy.c 345 for( cat=CmdOptCatFIRST; cat!
=CmdOptCatLAST; cat = (CmdOptCategory)(((int)cat)
+1))
tidy.c 346 #else
tidy.c 347 for( cat=CmdOptCatFIRST; cat!
=CmdOptCatLAST; ++cat)
tidy.c 348 #endif

2. Use native boolean where available:

config.c 1011 SetOptionBool( doc, entry->id, ToBool
(flag) );
config.c 1011 SetOptionBool( doc, entry->id, ToBool
(flag) );
fileio.c 46 isEOF = ToBool(feof( fin->fp ));
lexer.c 195 return ToBool(map & white);
lexer.c 201 return ToBool(map & newline);
lexer.c 219 return ToBool(map & letter);
lexer.c 225 return ToBool(map & namechar);
lexer.c 572 return ToBool(map & lowercase);
lexer.c 579 return ToBool(map & uppercase);
parser.c 78 return ToBool(node->tag->model &
CM_NEW);
pprint.c 1001 Bool scriptlets = ToBool(cfg(doc,
TidyWrapScriptlets));
tags.c 507 deleteIt = ToBool( curr->model &
CM_EMPTY );
tags.c 511 deleteIt = ToBool( curr->model &
CM_INLINE );

----------------------------------------------------------------------

>Comment By: Arnaud Desitter (arnaud02)
Date: 2005-08-17 09:53

Message:
Logged In: YES
user_id=566665

Mostly applied so far.

----------------------------------------------------------------------

Comment By: Björn Höhrmann (hoehrmann)
Date: 2005-07-16 14:01

Message:
Logged In: YES
user_id=188003

Looks good to me (though I did not check the patches for
tidylib.c/tidy.c, they are not yet added to the bug).

----------------------------------------------------------------------

You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=390965&aid=1239422&group_id=27659


-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise