2 Copyright (C) 1997-2019 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 /* config.h must precede flex's inclusion of <stdio.h>
20 in order for its _GNU_SOURCE definition to take effect. */
27 /* When output is inhibited, exit with status:
29 1 if indentation is wrong, or if text follows #else/#endif
30 2 if #if/#endif mismatch
35 # error This scanner must be made using flex, not lex.
40 #include <sys/types.h>
43 #define _(msgid) gettext (msgid)
44 #define N_(msgid) msgid
51 /* The official name of this program (e.g., no `g' prefix). */
52 #define PROGRAM_NAME "cppi"
54 #define AUTHORS "Jim Meyering"
56 #define obstack_chunk_alloc malloc
57 #define obstack_chunk_free free
60 # define PUT2(x,y) do { putchar (x); putchar (y); } while (0)
62 # define PUT2(x,y) /* empty */
66 #define YY_DECL static int cpp_i (const char *in_file)
69 #define isblank(c) ((c) == ' ' || (c) == '\t')
71 #define MY_ECHO do { if (!inhibit_output) ECHO; } while (0)
73 #define OPENER_STACK_HEIGHT \
74 (obstack_object_size (&opener_stack) / sizeof (struct KL_pair))
76 /* An entry on the stack of opener (keyword/line_number) specs. */
79 /* The type of cpp directive opening a block: one of
80 EIC_IF, EIC_IFDEF, EIC_IFNDEF. */
81 enum Eic_type opener_type;
83 /* The line number of the directive. */
84 unsigned int line_number;
89 EXIT_NOT_PROPERLY_INDENTED = 1,
90 EXIT_STRING_TOO_LONG = 1,
95 /* A stack of cpp-opener-directive-spec/line-number pairs.
96 This lets us report the line numbers of any unmatched #if,
97 #ifdef, or #ifndef directives. */
98 static struct obstack opener_stack;
100 /* Current nesting level. */
103 /* Initial indentation nesting depth. May be negative.
104 Use a value of -1 to cause code (usually header files) to be indented
105 one level less than otherwise. This is useful if you don't want file-
106 enclosing ifdefs (e.g., #ifdef FOO_H/#define FOO_H/ ... #endif) to
107 cause every enclosed cpp directive to have at least one space between
108 the `#' and the cpp keyword. */
109 static int initial_i_depth;
111 /* Nonzero means don't generate diagnostics about indentation, and print
112 to stdout only the names of files that aren't properly indented. */
113 static int list_files_only;
115 /* Set to non-zero to enable ANSI-conformance check
116 (text after #else or #endif is non-ANSI). */
117 static int ansi_check = 0;
119 /* Set to non-zero to inhibit non-error output. */
120 static int inhibit_output = 0;
122 /* The maximum length of a double-quoted string. Set to zero
123 to indicate there is no limit. */
124 static unsigned long int max_string_length = 0;
126 static char *default_file_list[] = {(char *) "-", NULL};
128 static struct option const long_options[] =
130 {"ansi", no_argument, NULL, 'a'},
131 {"check", no_argument, NULL, 'c'},
132 {"list-files-only", no_argument, NULL, 'l'},
133 {"max-string-length", required_argument, NULL, 'm'},
134 {GETOPT_HELP_OPTION_DECL},
135 {GETOPT_VERSION_OPTION_DECL},
139 static void gobble_define (unsigned int *);
140 static void gobble_c_comment (const char *, unsigned int *);
141 static void gobble_line (unsigned int *);
143 /* Include gperf-generated hash function. */
144 #include "cpp-cond.c"
146 #if __GNUC__ && ! __STRICT_ANSI__
147 /* this use of a statement-expression works only with gcc. */
148 # define INPUT_AND_ECHO() \
150 int _c_ = input (); \
151 if (_c_ != EOF && !inhibit_output) \
152 fputc (_c_, yyout); \
156 /* Although the above is a macro, this function definition must
157 follow the declaration of inhibit_output, above. */
158 static int input (void);
160 INPUT_AND_ECHO (void)
163 if (_c_ != EOF && !inhibit_output)
169 /* PREFIX is the cpp line from beginning of line to end of the keyword
170 following the `#'. */
173 lookup_cpp_keyword (const char *prefix, size_t len, const char **kw)
175 /* Find the beginning of the keyword. */
177 for (p = prefix; ; ++p, --len)
188 struct KW const *ent = cpp_cond_lookup (p, len);
190 return (ent ? ent->code : EIC_OTHER);
197 static inline struct KL_pair
198 kth_entry (unsigned int k)
200 struct KL_pair *s = (struct KL_pair *) obstack_base (&opener_stack);
201 assert (k < OPENER_STACK_HEIGHT);
208 int pair_size = sizeof (struct KL_pair);
209 assert (OPENER_STACK_HEIGHT > 0);
210 obstack_blank_fast (&opener_stack, -pair_size);
214 push (enum Eic_type opener_type, unsigned int line_number)
217 pair.opener_type = opener_type;
218 pair.line_number = line_number;
220 obstack_grow (&opener_stack, &pair, sizeof (struct KL_pair));
224 emit_or_check (enum Eic_type type, const char *text, const char *other)
231 int depth = (i_depth < 0 ? 0 : i_depth);
233 && (n = strspn (text + 1, " ")) == depth
234 && !isblank (text[1 + n]))
235 /* This allows pragmas to have exactly one space before the `#'.
236 E.g., ` #pragma alloca' or ` # pragma alloca' . */
239 && type == EIC_PRAGMA
240 && (n = strspn (text + 2, " ")) == depth - 1
241 && text[n + 2] == 'p'))
249 const char *dir = (type == EIC_OTHER ? other : directive[type]);
250 int space_first = (type == EIC_PRAGMA && text[0] == ' ');
256 for (i = 0; i < i_depth - 1; i++)
262 for (i = 0; i < i_depth; i++)
275 emit_indented_cpp (char const *in_file, unsigned int line_number,
276 char const *text, size_t len, int *fail)
279 enum Eic_type t = lookup_cpp_keyword (text, len, &keyword);
288 /* Maintain a stack of (keyword, line number) pairs to better
289 report any `unterminated #if...' errors. Put a new pair
291 push (t, line_number);
293 *fail = emit_or_check (t, yytext, keyword);
299 if (i_depth <= initial_i_depth)
301 if (!list_files_only)
303 error (0, 0, _("%s: line %d: found #%s without matching #if"),
304 in_file, line_number, directive[t]);
307 emit_or_check (t, yytext, keyword);
313 *fail = emit_or_check (t, yytext, keyword);
319 if (i_depth <= initial_i_depth)
321 if (!list_files_only)
323 error (0, 0, _("%s: line %d: found #%s without matching #if"),
324 in_file, line_number, directive[t]);
326 i_depth = initial_i_depth + 1;
331 /* We've just found an #endif. Pop off and discard the
332 keyword,line-number pair that's on the top of the stack.
333 That pair identifies the matching #if, #ifdef, or #ifndef. */
339 int tf = emit_or_check (t, yytext, keyword);
348 *fail = emit_or_check (t, yytext, keyword);
356 if (*fail == EXIT_NOT_PROPERLY_INDENTED)
358 if (!list_files_only)
359 error (0, 0, _("%s: line %d: not properly indented"),
360 in_file, line_number);
374 /* This section contains dcls and code that is local to the
377 /* Current line number -- for diagnostics and errors. */
378 unsigned int lineno = 1;
382 obstack_init (&opener_stack);
385 i_depth = initial_i_depth;
392 gobble_c_comment (in_file, &lineno);
398 gobble_line (&lineno);
402 /* We need this rule so that the double quote in the character literal,
403 '"' (also written as '\"'), is not interpreted as a string opener. */
408 /* This rule is included to make flex's scanner more efficient
409 by avoiding backup states. */
412 "'\\" { /* Likewise. */ MY_ECHO;}
413 "'"/"\"" { /* Likewise. */ MY_ECHO; }
417 int start_lineno = lineno;
419 /* Count consecutive backslashes. We'll need this number when
420 a string of them immediately precedes a double quote. */
421 size_t n_backslashes = 0;
422 size_t string_length = 0;
430 c = INPUT_AND_ECHO ();
434 error (0, 0, _("%s: line %d: EOF in string"),
435 in_file, start_lineno);
436 fail = EXIT_LEX_ERROR;
440 /* If the number of preceding backslashes is even, then this is
441 an unescaped double quote, and it marks the end of the string. */
442 if (c == '"' && n_backslashes % 2 == 0)
445 n_backslashes = (c == '\\' ? n_backslashes + 1 : 0);
447 /* Some compilers (irix4's cc) impose a limit on the length of a
448 double quoted string. It's probably a limit on the length of
449 the actual value of the string, rather than on the number of
450 bytes between `"'s in the source, but I'm not sure it's worth
451 the trouble of computing the former. */
460 if (0 < max_string_length && max_string_length < string_length)
462 error (0, 0, _("%s: line %d: string (%lu) longer than maximum of %lu"),
463 in_file, start_lineno, (unsigned long) string_length,
464 (unsigned long) max_string_length);
466 lex_fail = EXIT_STRING_TOO_LONG;
473 ^[ \t]*#[ \t]*[a-zA-Z0-9_]+ {
477 t = emit_indented_cpp (in_file, lineno, yytext, yyleng, &fail);
479 if (t == EIC_IF || t == EIC_IFNDEF || t == EIC_ELIF || t == EIC_DEFINE)
484 /* Make sure there's exactly one space after this directive. */
486 if (c != ' ' || ((c = input ()) == ' ' || c == '\t' || c == EOF))
488 if (!list_files_only)
490 error (0, 0, _("%s: line %d: not properly formatted;\n\
491 there must be exactly one SPACE character after each\n\
492 #if, #elif, and #define directive"),
501 /* Squeeze multiple spaces and tabs after an #if or #elif
502 directive to a single space. */
504 while ((c = input ()) == ' ' || c == '\t')
512 gobble_define (&lineno);
515 else if (ansi_check && (t == EIC_ELSE || t == EIC_ENDIF))
517 /* If requested, make sure there's nothing after an #else or #endif. */
518 int found_non_ansi = 0;
519 int cpp_directive_lineno = lineno;
521 while ((c = INPUT_AND_ECHO ()) != EOF)
527 c = INPUT_AND_ECHO ();
532 gobble_c_comment (in_file, &lineno);
537 gobble_line (&lineno);
541 /* else, fall through to next if-stmt */
550 /* We've found a token after an #else or #endif.
551 Continue reading to end of line. */
557 if (!list_files_only)
559 _("%s: line %d: text following `#%s' violates ANSI standard"),
560 in_file, cpp_directive_lineno,
561 (t == EIC_ELSE ? "else" : "endif"));
570 ^[ \t]*#[ \t]* { MY_ECHO; }
573 \n { MY_ECHO; ++lineno; }
577 if (i_depth != initial_i_depth)
579 if (!list_files_only)
581 /* Iterate the opener stack from bottom to top, giving a
582 diagnostic per unterminated #if* directive.
583 Torture this code with a command like this:
584 $ yes '#if FOO' |head -600 |cppi -c */
586 for (i = 0; i < OPENER_STACK_HEIGHT; i++)
588 struct KL_pair x = kth_entry (i);
589 error (0, 0, _("%s: line %d: unterminated #%s"),
590 in_file, x.line_number, directive[x.opener_type]);
594 lex_fail = EXIT_LEX_ERROR;
603 cpp_indent (const char *in_file)
608 if (STREQ (in_file, "-"))
611 in_file = "standard input";
615 if ((in = fopen (in_file, "r")) == NULL)
617 error (EXIT_FILE_ERROR, errno, "%s", in_file);
622 fail = cpp_i (in_file);
624 obstack_free (&opener_stack, NULL);
626 if (in && fclose (in) == EOF)
627 error (EXIT_FILE_ERROR, errno, "%s", in_file);
632 static void usage (int status) ATTRIBUTE_NORETURN;
638 fprintf (stderr, "Try `%s --help' for more information.\n",
645 or: %s -c [OPTION] [FILE]...\n\
646 "), program_name, program_name);
649 Indent the C preprocessor directives in FILE to reflect their nesting\n\
650 and ensure that there is exactly one space character between each #if,\n\
651 #elif, #define directive and the following token, and write the result\n\
654 to standard output. The number of spaces between the `#' and the following\n\
655 directive must correspond to the level of nesting of that directive.\n\
656 With no FILE, or when FILE is -, read standard input.\n\
660 -a, --ansi when checking, fail if text follows #else or #endif\n\
661 -c, --check set exit code, but don't produce any output\n\
662 -l, --list-files-only don't generate diagnostics about indentation;\n\
663 print to stdout only the names of files that\n\
664 are not properly indented\n\
667 -m, --max-string-length=LENGTH\n\
668 fail if there is a double-quoted string longer\n\
669 than LENGTH; if LENGTH is 0 (the default),\n\
670 then there is no limit\n\
672 fputs (HELP_OPTION_DESCRIPTION, stdout);
673 fputs (VERSION_OPTION_DESCRIPTION, stdout);
675 With the -c option, don't write to stdout. Instead, check the\n\
676 indentation of the specified files giving diagnostics for preprocessor\n\
677 lines that aren't properly indented or are otherwise invalid.\n\
681 Note that --ansi without --check does not correct the problem of\n\
682 non-ANSI text following #else and #endif directives.\n\
686 The exit code will be one of these:\n\
687 0 all directives properly indented\n\
688 1 some cpp directive(s) improperly indented, or\n\
689 text follows #else/#endif (enabled with --check --ansi), or\n\
690 a double-quoted string is longer than the specified maximum\n\
691 2 #if/#endif mismatch, EOF in comment or string\n\
692 3 file (e.g. open/read/write) error\n\
696 A pragma directive may have its `#' indented.\n\
698 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
700 exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
703 /* Read the body of a #define directive (echoing or not, as appropriate).
704 That is, read up to and including the first unescaped newline (or EOF).
705 This is necessary because otherwise, we could mistakenly interpret the
706 stringification of a macro argument as a cpp directive. */
709 gobble_define (unsigned int *line_number)
712 int backslash_count = 0;
713 while ((c = INPUT_AND_ECHO ()) != EOF)
718 if (backslash_count % 2 == 0)
729 /* We've read the C comment opener. Read up to and including
730 the closing delimiter. */
733 gobble_c_comment (const char *in_file, unsigned int *line_number)
735 int start_lineno = *line_number;
739 while ((c = INPUT_AND_ECHO ()) != '*' && c != EOF)
747 while ((c = INPUT_AND_ECHO ()) == '*')
750 break; /* found the end */
757 error (EXIT_LEX_ERROR, 0, _("%s: line %d: EOF in comment"),
758 in_file, start_lineno);
764 /* Read up to and including any newline. */
767 gobble_line (unsigned int *line_number)
770 while ((c = INPUT_AND_ECHO ()) != EOF)
781 main (int argc, char **argv)
789 set_program_name (argv[0]);
790 setlocale (LC_ALL, "");
791 bindtextdomain (PACKAGE, LOCALEDIR);
792 textdomain (PACKAGE);
794 atexit (close_stdout);
796 while ((c = getopt_long (argc, argv, "aclm:", long_options, NULL)) != -1)
817 if (xstrtoul (optarg, NULL, 0, &max_string_length, NULL)
820 error (0, 0, _("invalid maximum string length %s"), optarg);
825 case_GETOPT_HELP_CHAR;
827 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
830 usage (EXIT_LEX_ERROR);
838 if (!inhibit_output && argc - optind > 2)
840 error (0, 0, _("too many arguments"));
841 usage (EXIT_FAILURE);
844 file_list = (optind == argc ? default_file_list : argv + optind);
847 for (i = 0; file_list[i]; i++)
851 err = cpp_indent (file_list[i]);
855 if (err && list_files_only)