Commit Graph

51 Commits

Author SHA1 Message Date
Piotr Pawel Stefaniak
dad19de0e6 indent(1): bug fix after r336333
The bug was that isalnum() is not exactly equivalent to previous code which
also allowed characters "$" and "_", so check for those explicitly.

Reported by:	tuexen@
2018-08-15 18:19:45 +00:00
Piotr Pawel Stefaniak
01c66110e1 indent(1): rewrite the integer/floating constant scanning part of lexi.c
Remove procedural code that did the scanning, which was faulty and didn't
support complex constants such as 0x1p-61. Replace it with a finite state
machine expressed as a transition table. The table was rewritten by hand
from lx's output, given parts of grammar expressed as regular expressions.

lx is Katherine Flavel's lexer generator, currently available at
https://github.com/katef/libfsm and the parts of grammar were taken from
http://quut.com/c/ANSI-C-grammar-l-2011.html and extended to support binary
integer constants which are a popular GCC extension.

Reported by:	bde
2018-07-16 05:46:50 +00:00
Piotr Pawel Stefaniak
7e53aaedd3 indent(1): group global option variables into an options structure
It's clearer now when a variable represents a toggable command line option.

Many options were stored in the parser's state structure, so fix also that.
2018-06-10 16:44:18 +00:00
Piotr Pawel Stefaniak
2364d1a74e indent(1): if an identifier can be either a typedef name or as a struct
member, it is taken as the latter
2018-06-03 19:05:20 +00:00
Piotr Pawel Stefaniak
9de29bfb5a indent(1): improve CHECK_SIZE_ macros
Rewrite the macros so that they take a parameter. Consumers use it to signal
how much room in the buffer they need; this lets them do that once when
required space is known instead of doing the check once every loop step.

Also take the parameter value into consideration when resizing the buffer;
the requested space may be larger than the constant 400 bytes that the
previous version used - now it's the sum of those two values.

On the consumer side, don't copy strings byte by byte - use memcpy().

Deduplicate code that copied base 2, base 8 and base 16 literals.

Don't advance the e_token pointer once the token has been copied into
s_token. This allows easy calculation of the token's length.
2018-06-03 18:19:41 +00:00
Piotr Pawel Stefaniak
1479f36d8e indent(1): remove troff output support
The troff output in indent was invented at Sun and the online documentation
for some post-SunOS operating system includes this:
The usual way to  get  a  troffed listing is with the command
                       indent -troff program.c | troff -mindent

The indent manual page in FreeBSD 1.0 already lacks that information and
troff -mindent complains about not being able to find the macro file.
It seems that the file did exist on SunOS and was supposed to be imported
into 4.3BSD together with the feature, but that has never happened.

Removal of troff output support simplifies a lot of indent's code.

vgrind(1) seems to be a promising replacement.
2018-06-03 17:55:50 +00:00
Piotr Pawel Stefaniak
e1baf57e4b indent(1): limit character classification functions' input to unsigned char 2018-06-03 17:03:55 +00:00
Piotr Pawel Stefaniak
95b813e501 indent(1): recognize more type names
Most are from C99.
2018-06-03 16:52:30 +00:00
Piotr Pawel Stefaniak
9522d0b0d2 indent(1): don't format function declarations as variables 2018-06-03 16:42:58 +00:00
Piotr Pawel Stefaniak
f9287a9d85 indent(1): disjoint parser state from lexi()
The function is sometimes used as a look-ahead, so ideally it should bear
no information about parser state.
2018-06-03 16:21:15 +00:00
Piotr Pawel Stefaniak
63c3f22696 indent(1): improve predictability of lexi()
lexi() reads the input stream and categorizes the next token. indent will
sometimes buffer up a sequence of tokens in order rearrange them. That is
needed for properly cuddling else or placing braces correctly according to
the chosen style (KNF vs Allman) when comments are around. The loop that
buffers tokens up uses lexi() to decide if it's time to stop buffering. Then
the temporary buffer is used to feed lexi() the same tokens again, this time
for normal processing.

The problem is that lexi() apart from recognizing the token, can change
a lot of information about the current state, for example ps.last_nl,
ps.keyword, buf_ptr. It also abandons leading whitespace, which is needed
mainly for comment-related considerations. So the call to lexi() while
tokens are buffered up and categorized can change the state before they're
read again for normal processing which may easily result in changing
interpretation of the current state and lead to incorrect output.

To work around the problems:
1) copy the whitespace into the save_com buffer so that it will be read
again when processed
2) trick lexi() into modifying a temporary copy of the parser state instead
of the original.
2018-06-03 14:13:11 +00:00
Piotr Pawel Stefaniak
5bdd850968 indent(1): remove undocumented and rather useless option (-ps)
It's used to treat the "->" access operator as a binary operator and put
space characters around it.
2018-06-03 13:40:58 +00:00
Piotr Pawel Stefaniak
3bbaa755f3 indent(1): don't indent typedef declarations as object declarations 2018-06-01 09:41:15 +00:00
Pedro F. Giffuni
df57947f08 spdx: initial adoption of licensing ID tags.
The Software Package Data Exchange (SPDX) group provides a specification
to make it easier for automated tools to detect and summarize well known
opensource licenses. We are gradually adopting the specification, noting
that the tags are considered only advisory and do not, in any way,
superceed or replace the license texts.

Special thanks to Wind River for providing access to "The Duke of
Highlander" tool: an older (2014) run over FreeBSD tree was useful as a
starting point.

Initially, only tag files that use BSD 4-Clause "Original" license.

RelNotes:	yes
Differential Revision:	https://reviews.freebsd.org/D13133
2017-11-18 14:26:50 +00:00
Piotr Pawel Stefaniak
a3abcad0b7 indent(1): don't produce unneeded space character in function declarators. 2017-07-23 14:04:45 +00:00
Piotr Pawel Stefaniak
bd2969a00d indent(1): Support binary integer literals.
This was done by Romain Tartière for PR123553. I initially thought that it would break code like this:
#define b00101010 -1
if (0 b00101010)
...

by joining 0 and b00101010 together. However, the real problem with that patch was that once it saw a 0, it assumed that the number was base 2, 8 or 16, ignoring base 10 floating point numbers. I fixed that.

I didn't copy the diagnostic part of the original patch as it seems out of scope of implementing binary integer literals formatting.

PR:		123553
Submitted by:	romain (original version)
Approved by:	pfg (mentor)
2017-05-18 17:15:58 +00:00
Pedro F. Giffuni
cba6bdf9bc indent(1): Do not define opchar unless it will be used.
"opchar" is only used once depending on "undef" macro being defined.
Conditionalize it in the same way.

Submitted by:	Piotr Sephaniak
2016-12-02 01:52:32 +00:00
Pedro F. Giffuni
f171328eaa indent(1): Fix indent's confusion about custom FreeBSD macros.
Teach indent(1) about storage-class specifiers. Don't assume
"in_parameter_declaration" state if "in_decl" hasn't been set. Don't set
"in_decl" for storage-class specifiers.

That set of changes helps with recognizing the difference between file
scope declarations like this:

static LIST_HEAD(, alq) ald_active;
static int ald_shuttingdown = 0;
struct thread *ald_thread;

and old style function declarators like this:

static int
do_execve(td, args, mac_p)
	struct thread *td;
	struct image_args *args;
	struct mac *mac_p;
{

Unfortunately, at the same time this change makes indent(1) require
explicit int in declarations like "static a;", in order to understand that
it's part of a declaration. On the other hand, declarations like in the
first example are no longer indented as if ald_shuttingdown and ald_thread
were parameters of a function named LIST_HEAD.

Submitted by:	 Piotr Stefaniak
2016-12-02 01:25:51 +00:00
Pedro F. Giffuni
350fcdd5df indent(1): Properly handle the wide string literal and wide char constant L.
indent(1) treated the "L" in "L'a'" as if it were an identifier and forced
a space character after it, breaking valid code.

PR:		143090
MFC after:	2 weeks
2016-11-27 20:38:14 +00:00
Pedro F. Giffuni
8e7c1235ce indent(1): Fix off-by-one in control flow leading dead code.
Coverity correctly reported that it's impossible for /comparison/ to be 0
here, because the only way for the for loop to end is by /comparison/
being < 0.

Fortunately the consequences of this bug weren't severe; for duplicated
entries in the typedef names file it would unnecessarily duplicate strings
with strdup(), but pointers to those would replace existing ones. So this
was a memory leak at worst.

CID:	 1361477
Obtained from:	 Piotr Stephaniak
2016-08-23 02:07:08 +00:00
Pedro F. Giffuni
f3c23ec321 indent(1): Fix memory leaks pointed out by clang-analyzer.
Shift the responsibility of allocating memory for the string duplicate
from the caller (set_option, add_typedefs_from_file) to the callee
(add_typename) as it has more knowledge about when the duplication
actually needs to occur.

Taken from:	Piotr Stefaniak
2016-08-23 01:40:45 +00:00
Pedro F. Giffuni
a6bcfda487 indent(1): Use bsearch() for looking up type keywords.
Reference:
f3b8e6e57f

Submitted by:	Piotr Stefaniak
Differential Revision: https://reviews.freebsd.org/D6966
2016-08-04 15:27:09 +00:00
Pedro F. Giffuni
771aff0aa5 indent(1): add new -sac and -U options.
Add -sac (space after cast) and -nsac options.
These control whether space character is put after a cast operator or not.
Default is -nsac.

Add -U option for providing a file containing list of types.
This is needed for properly deciding which asterisks denote unary
operation and which denote binary.

These come from PostgreSQL.

Reference:
84b00e3d46
49c52cf383

Differential Revision: https://reviews.freebsd.org/D6966  (Partial)
Submitted by:	Piotr Stefaniak
2016-08-03 22:08:07 +00:00
Pedro F. Giffuni
b4939677e8 indent(1): accept offsetof(3) as a keyword.
Reference:
c470e5e2c9

Differential Revision: https://reviews.freebsd.org/D6966  (Partial)
Submitted by:	Piotr Stefaniak
2016-08-03 16:33:34 +00:00
Pedro F. Giffuni
70a3049ea5 indent(1): Use a dash in the license headers.
Use of the canonical dash avoids indent(1) from reformatting the
license headers.
2016-08-01 16:40:42 +00:00
Pedro F. Giffuni
e643b783e3 indent(1): Yet more style issues.
strchr(3) returns a pointer not a boolean.
Attempt to make the style somewhat more ocnsistent with what indent
had before recent changes.

Pointed out by:	bde
2016-07-30 01:04:18 +00:00
Pedro F. Giffuni
c917a54b26 indent(1): Use NULL instead of zero for pointers. 2016-07-29 19:36:10 +00:00
Pedro F. Giffuni
9f827409fb indent(1): Support "f" and "F" floating constant suffixes.
Actually this just brings back r303487 with the correct commit log.

Differential Revision: https://reviews.freebsd.org/D6966  (Partial)
Obtained from:	Piotr Stefaniak
2016-07-29 18:00:10 +00:00
Pedro F. Giffuni
b58cc20a1a Revert r303487: Wrong commit log.
Will be brought back with the correct log.
2016-07-29 17:55:38 +00:00
Pedro F. Giffuni
e5302a297b indent(1): fix struct termination detection.
Small style cleanup while here.

Differential Revision: https://reviews.freebsd.org/D6966 (Partial)
Obtained from:	Piotr Stefaniak
2016-07-29 16:28:51 +00:00
Pedro F. Giffuni
b67565942b indent(1): Fix typo.
It's typedef, not typdef.

Obtained from:	OpenBSD (CVS rev. 1.20)
MFC after:	3 days
2016-06-06 16:10:52 +00:00
Andriy Gapon
c27b391b3f indent(1): don't treat bare '_t' as a type name when -ta is used
It seems that identifier "_t" is sometimes used as a variable name,
even in our tree.  Not that I endorse that, but still it's better
to require at least one character before _t suffix to consider
an identifier to be a type name.

Reported by:	Alex Vasylenko <lxv@omut.org>
MFC after:	1 week
2010-04-15 21:41:07 +00:00
Andriy Gapon
c58c7416c7 indent(1): add a new option, -ta, to treat all *_t identifiers as types
Submitted by:	Hans Petter Selasky
Discussed with:	bde
MFC after:	10 days
2010-03-31 17:05:30 +00:00
Diomidis Spinellis
855196e9fa Use the appropriate error function for displaying the error,
instead of printing it to stdout.

MFC after:	1 week
2005-11-20 13:48:15 +00:00
Bruce Evans
47fe027b66 Fixed misformatting of "struct foo *bar" in function parameter lists. It
was mangled to "struct foo * bar".  There should be an option to control
this, but no space is normal.  This finishes fixing the bugs in rev.1.4.

indent(1) still doesn't really understand types in parameter lists.  It
thinks keywords inside parentheses are for casts or sizeofs.  This works
accidentally for scalar types and this quick fix makes it work similarly
but not so accidentally for struct/union/enum types.
2004-02-09 15:27:02 +00:00
Bruce Evans
611e9fdbb4 Expanded the size of the keyword table from 100 to 1000 entries so that
the number of typedef-names is not so limited.  Same as in rev.1.4.

Added the "const" and "volatile" to the keyword table.  Rev.1.4 added
these but they were misclassified so they were not formatted as types.
indent still doesn't really understand them.  E.g., it mangles
"char * const *foo" and "char *const *foo".  This change mainly stops
it mangling "char const foo" to "char<declaration-indent>const foo".
2004-02-09 13:13:03 +00:00
Bruce Evans
43a3abfd1e Backed out rev.1.4 and 1.7 so they they can be implemented and committed
properly.  Of the 3 changes mentioned in the log message for rev.1.4,
the first (implementing -[n]fcb) was correct but didn't touch this
file, the second (no-space-after-sizeof) was not actually done (it is
the default and is controlled by the undcoumented -[n]bs options), and
the third (no-space-after 'struct foo *') was very buggy and was reduced
to wrong comments and other style bugs by backing out the main part
of it in rev.1.6.  Rev.1.4 had 2 changes which were not mentioned in
its commit log: expand specials[] so that more than -83 typedef-names
can be specified (this was the one working change in rev.1.4), and add
"const" and "volatile" to specials[] (this was buggy).
2004-02-09 12:52:15 +00:00
Philippe Charnier
d005495293 err() on allocation failure. WARNS=9 compliant
use #if 0, #ifndef lint, #endif /* not lint */, #endif ordering
when a message is provided, use errx() instead of err().
2003-06-15 09:28:17 +00:00
Philippe Charnier
dc51023cb3 Spelling 2002-10-16 13:58:39 +00:00
David E. O'Brien
e026a48c34 Consistently use FBSDID 2002-06-30 05:25:07 +00:00
Juli Mallett
90af6a724e Remove deprecated register qualifier. 2002-06-24 17:40:27 +00:00
David Malone
8c7e769852 1) Const enough things to avoid warnings.
2) Cast ifdef_level to a size_t before comparing it to a ratio of size_ts.
   Ifdef_level should always be positive.
3) Complete prototype for chfont.
4) Cast some ptrdiff_ts to ints before using as a field width.
5) Avoid shadowing a local variable p with another local variable p.
2002-03-30 17:10:20 +00:00
Jens Schweikhardt
7916863d82 Make this compile cleanly when warnings are enabled:
- ANSIfy function declarations
 - braces around initializers structs within structs
 - add parens in complicated expressions
 - disambiguate dangling elses
 - no more implicit int
 - make functions static where possible
 - use prototypes
 - don't use varargs hack for diag()

Requested by: joerg
MFC after:	2 weeks
2001-10-28 18:35:32 +00:00
Jens Schweikhardt
f828df9d17 Properly handle backslash newline within an identifier or keyword.
PR:		bin/6015
Submitted by:	myself (schweikh)
Patch by:	Alexey V.Neyman <alex.neyman@auriga.ru>
Tested by:	indenting my chess problem solver and running its test suite
MFC after:	3 weeks
2001-10-19 19:10:36 +00:00
David E. O'Brien
0a8bb16255 Fix damage I did in rev 1.4 that broke formatting this example:
void foo(struct bar *bar) { return 0; }
2001-04-20 07:41:23 +00:00
David E. O'Brien
175f26d6ed Don't abuse the SCCS `@(#)' for RCS.
Requested by:	bde
2000-12-25 01:16:12 +00:00
David E. O'Brien
9e50dd7726 Add or fix FreeBSD IDs. 2000-12-09 09:52:52 +00:00
David E. O'Brien
a5e1cac0f5 "Implement -[n]fcb (formatting of block comments) and attempt to implement
no-space=after-sizeof (not optional) and no-space-after 'struct foo *'
(not optional).  Without these, indent unKNFizes even more perfectly KNF code."

Submitted by:	bde
2000-12-09 09:45:09 +00:00
Robert Nordier
c70772eab9 Support integer constant unsigned-suffix and long-long-suffix. 1998-05-19 20:41:20 +00:00
Rodney W. Grimes
7799f52a32 Remove trailing whitespace. 1995-05-30 06:41:30 +00:00