typedef token_kind_t as int
[cparser] / preprocessor.c
index 3449063..af95d90 100644 (file)
@@ -91,7 +91,6 @@ static pp_conditional_t *conditional_stack;
 static token_t           pp_token;
 static bool              resolve_escape_sequences = false;
 static bool              ignore_unknown_chars     = true;
-static bool              in_pp_directive;
 static bool              skip_mode;
 static FILE             *out;
 static struct obstack    pp_obstack;
@@ -100,7 +99,7 @@ static const char       *printed_input_name = NULL;
 static source_position_t expansion_pos;
 static pp_definition_t  *current_expansion  = NULL;
 static strset_t          stringset;
-static preprocessor_token_kind_t last_token;
+static token_kind_t      last_token;
 
 static searchpath_entry_t *searchpath;
 
@@ -219,17 +218,18 @@ static inline void put_back(utf32 const pc)
        --input.position.colno;
 }
 
-#define MATCH_NEWLINE(code)                   \
-       case '\r':                                \
-               next_char();                          \
-               if (input.c == '\n') {                \
-       case '\n':                                \
-                       next_char();                      \
-               }                                     \
-               info.whitespace = 0;                  \
-               ++input.position.lineno;              \
-               input.position.colno = 1;             \
-               code
+#define NEWLINE \
+       '\r': \
+               next_char(); \
+               if (input.c == '\n') { \
+       case '\n': \
+                       next_char(); \
+               } \
+               info.whitespace = 0; \
+               ++input.position.lineno; \
+               input.position.colno = 1; \
+               goto newline; \
+               newline // Let it look like an ordinary case label.
 
 #define eat(c_type) (assert(input.c == c_type), next_char())
 
@@ -238,9 +238,8 @@ static void maybe_concat_lines(void)
        eat('\\');
 
        switch (input.c) {
-       MATCH_NEWLINE(
+       case NEWLINE:
                return;
-       )
 
        default:
                break;
@@ -452,171 +451,89 @@ static const char *identify_string(char *string)
        return result;
 }
 
-static string_t make_string(char *string, size_t len)
+static string_t sym_make_string(string_encoding_t const enc)
 {
-       const char *result = identify_string(string);
-       return (string_t) {result, len};
+       obstack_1grow(&symbol_obstack, '\0');
+       size_t      const len    = obstack_object_size(&symbol_obstack) - 1;
+       char       *const string = obstack_finish(&symbol_obstack);
+       char const *const result = identify_string(string);
+       return (string_t){ result, len, enc };
 }
 
-static void parse_string_literal(void)
+static void parse_string(utf32 const delimiter, token_kind_t const kind,
+                         string_encoding_t const enc,
+                         char const *const context)
 {
        const unsigned start_linenr = input.position.lineno;
 
-       eat('"');
+       eat(delimiter);
 
        while (true) {
                switch (input.c) {
                case '\\': {
-                       utf32 tc;
                        if (resolve_escape_sequences) {
-                               tc = parse_escape_sequence();
-                               obstack_1grow(&symbol_obstack, (char) tc);
+                               utf32 const tc = parse_escape_sequence();
+                               if (enc == STRING_ENCODING_CHAR) {
+                                       if (tc >= 0x100) {
+                                               warningf(WARN_OTHER, &pp_token.base.source_position, "escape sequence out of range");
+                                       }
+                                       obstack_1grow(&symbol_obstack, tc);
+                               } else {
+                                       obstack_grow_symbol(&symbol_obstack, tc);
+                               }
                        } else {
-                               obstack_1grow(&symbol_obstack, (char) input.c);
+                               obstack_1grow(&symbol_obstack, (char)input.c);
                                next_char();
-                               obstack_1grow(&symbol_obstack, (char) input.c);
+                               obstack_1grow(&symbol_obstack, (char)input.c);
                                next_char();
                        }
                        break;
                }
 
+               case NEWLINE:
+                       errorf(&pp_token.base.source_position, "newline while parsing %s", context);
+                       break;
+
                case EOF: {
                        source_position_t source_position;
                        source_position.input_name = pp_token.base.source_position.input_name;
                        source_position.lineno     = start_linenr;
-                       errorf(&source_position, "string has no end");
+                       errorf(&source_position, "EOF while parsing %s", context);
                        goto end_of_string;
                }
 
-               case '"':
-                       next_char();
-                       goto end_of_string;
-
                default:
-                       obstack_grow_symbol(&symbol_obstack, input.c);
-                       next_char();
-                       break;
+                       if (input.c == delimiter) {
+                               next_char();
+                               goto end_of_string;
+                       } else {
+                               obstack_grow_symbol(&symbol_obstack, input.c);
+                               next_char();
+                               break;
+                       }
                }
        }
 
 end_of_string:
-       /* add finishing 0 to the string */
-       obstack_1grow(&symbol_obstack, '\0');
-       const size_t size   = (size_t)obstack_object_size(&symbol_obstack);
-       char *const  string = obstack_finish(&symbol_obstack);
-
-       pp_token.kind          = TP_STRING_LITERAL;
-       pp_token.string.string = make_string(string, size);
+       pp_token.kind           = kind;
+       pp_token.literal.string = sym_make_string(enc);
 }
 
-/**
- * Parse a wide string literal and set lexer_token.
- */
-static void parse_wide_string_literal(void)
+static void parse_string_literal(string_encoding_t const enc)
 {
-       parse_string_literal();
-       if (pp_token.kind == TP_STRING_LITERAL)
-               pp_token.kind = TP_WIDE_STRING_LITERAL;
+       parse_string('"', TP_STRING_LITERAL, enc, "string literal");
 }
 
-static void parse_wide_character_constant(void)
+static void parse_character_constant(string_encoding_t const enc)
 {
-       eat('\'');
-
-       while (true) {
-               switch (input.c) {
-               case '\\': {
-                       const utf32 tc = parse_escape_sequence();
-                       obstack_grow_symbol(&symbol_obstack, tc);
-                       break;
-               }
-
-               MATCH_NEWLINE(
-                       parse_error("newline while parsing character constant");
-                       break;
-               )
-
-               case '\'':
-                       next_char();
-                       goto end_of_wide_char_constant;
-
-               case EOF:
-                       parse_error("EOF while parsing character constant");
-                       goto end_of_wide_char_constant;
-
-               default:
-                       obstack_grow_symbol(&symbol_obstack, input.c);
-                       next_char();
-                       break;
-               }
-       }
-
-end_of_wide_char_constant:
-       obstack_1grow(&symbol_obstack, '\0');
-       size_t  size = (size_t) obstack_object_size(&symbol_obstack)-1;
-       char   *string = obstack_finish(&symbol_obstack);
-       pp_token.kind          = TP_WIDE_CHARACTER_CONSTANT;
-       pp_token.string.string = make_string(string, size);
-
-       if (size == 0) {
+       parse_string('\'', TP_CHARACTER_CONSTANT, enc, "character constant");
+       if (pp_token.literal.string.size == 0) {
                parse_error("empty character constant");
        }
 }
 
-static void parse_character_constant(void)
-{
-       const unsigned start_linenr = input.position.lineno;
-
-       eat('\'');
-
-       int tc;
-       while (true) {
-               switch (input.c) {
-               case '\\':
-                       tc = parse_escape_sequence();
-                       obstack_1grow(&symbol_obstack, (char) tc);
-                       break;
-
-               MATCH_NEWLINE(
-                       parse_error("newline while parsing character constant");
-                       break;
-               )
-
-               case EOF: {
-                       source_position_t source_position;
-                       source_position.input_name = pp_token.base.source_position.input_name;
-                       source_position.lineno     = start_linenr;
-                       errorf(&source_position, "EOF while parsing character constant");
-                       goto end_of_char_constant;
-               }
-
-               case '\'':
-                       next_char();
-                       goto end_of_char_constant;
-
-               default:
-                       obstack_1grow(&symbol_obstack, (char) input.c);
-                       next_char();
-                       break;
-
-               }
-       }
-
-end_of_char_constant:;
-       obstack_1grow(&symbol_obstack, '\0');
-       const size_t size   = (size_t)obstack_object_size(&symbol_obstack);
-       char *const  string = obstack_finish(&symbol_obstack);
-
-       pp_token.kind          = TP_CHARACTER_CONSTANT;
-       pp_token.string.string = make_string(string, size);
-
-       if (size == 0) {
-               parse_error("empty character constant");
-       }
-}
-
-#define SYMBOL_CHARS_WITHOUT_E_P \
-       case 'a': \
+#define SYMBOL_CASES_WITHOUT_E_P \
+            'a': \
        case 'b': \
        case 'c': \
        case 'd': \
@@ -664,17 +581,17 @@ end_of_char_constant:;
        case 'X': \
        case 'Y': \
        case 'Z': \
-       case '_':
+       case '_'
 
-#define SYMBOL_CHARS \
-       SYMBOL_CHARS_WITHOUT_E_P \
+#define SYMBOL_CASES \
+            SYMBOL_CASES_WITHOUT_E_P: \
        case 'e': \
        case 'p': \
        case 'E': \
-       case 'P':
+       case 'P'
 
-#define DIGITS \
-       case '0':  \
+#define DIGIT_CASES \
+            '0':  \
        case '1':  \
        case '2':  \
        case '3':  \
@@ -683,7 +600,7 @@ end_of_char_constant:;
        case '6':  \
        case '7':  \
        case '8':  \
-       case '9':
+       case '9'
 
 /**
  * returns next final token from a preprocessor macro expansion
@@ -764,16 +681,15 @@ static void skip_multiline_comment(void)
                case '*':
                        next_char();
                        if (input.c == '/') {
+                               if (input.position.lineno != input.output_line)
+                                       info.whitespace = input.position.colno;
                                next_char();
-                               info.whitespace += input.position.colno-1;
                                return;
                        }
                        break;
 
-               MATCH_NEWLINE(
-                       info.at_line_begin |= !in_pp_directive;
+               case NEWLINE:
                        break;
-               )
 
                case EOF: {
                        source_position_t source_position;
@@ -799,10 +715,9 @@ static void skip_whitespace(void)
                        next_char();
                        continue;
 
-               MATCH_NEWLINE(
+               case NEWLINE:
                        info.at_line_begin = true;
                        return;
-               )
 
                case '/':
                        next_char();
@@ -825,10 +740,10 @@ static void skip_whitespace(void)
        }
 }
 
-static void eat_pp(preprocessor_token_kind_t const type)
+static inline void eat_pp(token_kind_t const kind)
 {
-       (void) type;
-       assert(pp_token.kind == type);
+       assert(pp_token.kind == kind);
+       (void) kind;
        next_preprocessing_token();
 }
 
@@ -839,8 +754,8 @@ static void parse_symbol(void)
 
        while (true) {
                switch (input.c) {
-               DIGITS
-               SYMBOL_CHARS
+               case DIGIT_CASES:
+               case SYMBOL_CASES:
                        obstack_1grow(&symbol_obstack, (char) input.c);
                        next_char();
                        break;
@@ -857,11 +772,11 @@ end_symbol:
        /* might be a wide string or character constant ( L"string"/L'c' ) */
        if (input.c == '"' && string[0] == 'L' && string[1] == '\0') {
                obstack_free(&symbol_obstack, string);
-               parse_wide_string_literal();
+               parse_string_literal(STRING_ENCODING_WIDE);
                return;
        } else if (input.c == '\'' && string[0] == 'L' && string[1] == '\0') {
                obstack_free(&symbol_obstack, string);
-               parse_wide_character_constant();
+               parse_character_constant(STRING_ENCODING_WIDE);
                return;
        }
 
@@ -885,8 +800,8 @@ static void parse_number(void)
        while (true) {
                switch (input.c) {
                case '.':
-               DIGITS
-               SYMBOL_CHARS_WITHOUT_E_P
+               case DIGIT_CASES:
+               case SYMBOL_CASES_WITHOUT_E_P:
                        obstack_1grow(&symbol_obstack, (char) input.c);
                        next_char();
                        break;
@@ -909,12 +824,8 @@ static void parse_number(void)
        }
 
 end_number:
-       obstack_1grow(&symbol_obstack, '\0');
-       size_t  size   = obstack_object_size(&symbol_obstack);
-       char   *string = obstack_finish(&symbol_obstack);
-
-       pp_token.kind          = TP_NUMBER;
-       pp_token.number.number = make_string(string, size);
+       pp_token.kind           = TP_NUMBER;
+       pp_token.literal.string = sym_make_string(STRING_ENCODING_CHAR);
 }
 
 
@@ -962,26 +873,25 @@ restart:
                next_char();
                goto restart;
 
-       MATCH_NEWLINE(
+       case NEWLINE:
                info.at_line_begin = true;
                info.had_whitespace = true;
                goto restart;
-       )
 
-       SYMBOL_CHARS
+       case SYMBOL_CASES:
                parse_symbol();
                return;
 
-       DIGITS
+       case DIGIT_CASES:
                parse_number();
                return;
 
        case '"':
-               parse_string_literal();
+               parse_string_literal(STRING_ENCODING_CHAR);
                return;
 
        case '\'':
-               parse_character_constant();
+               parse_character_constant(STRING_ENCODING_CHAR);
                return;
 
        case '.':
@@ -1116,7 +1026,6 @@ restart:
        case '~':
        case ';':
        case ',':
-       case '\\':
                pp_token.kind = input.c;
                next_char();
                return;
@@ -1188,9 +1097,11 @@ static void print_line_directive(const source_position_t *pos, const char *add)
        input.output_line  = pos->lineno-1;
 }
 
-static void emit_newlines(void)
+static bool emit_newlines(void)
 {
        unsigned delta = pp_token.base.source_position.lineno - input.output_line;
+       if (delta == 0)
+               return false;
 
        if (delta >= 9) {
                fputc('\n', out);
@@ -1202,6 +1113,11 @@ static void emit_newlines(void)
                }
        }
        input.output_line = pp_token.base.source_position.lineno;
+
+       for (unsigned i = 0; i < info.whitespace; ++i)
+               fputc(' ', out);
+
+       return true;
 }
 
 static void emit_pp_token(void)
@@ -1209,36 +1125,29 @@ static void emit_pp_token(void)
        if (skip_mode)
                return;
 
-       if (info.at_line_begin) {
-               emit_newlines();
-
-               for (unsigned i = 0; i < info.whitespace; ++i)
-                       fputc(' ', out);
-
-       } else if (info.had_whitespace ||
-                          tokens_would_paste(last_token, pp_token.kind)) {
+       if (!emit_newlines() &&
+           (info.had_whitespace || tokens_would_paste(last_token, pp_token.kind)))
                fputc(' ', out);
-       }
 
        switch (pp_token.kind) {
        case TP_IDENTIFIER:
                fputs(pp_token.base.symbol->string, out);
                break;
        case TP_NUMBER:
-               fputs(pp_token.number.number.begin, out);
+               fputs(pp_token.literal.string.begin, out);
                break;
-       case TP_WIDE_STRING_LITERAL:
-               fputc('L', out);
+
        case TP_STRING_LITERAL:
+               fputs(get_string_encoding_prefix(pp_token.literal.string.encoding), out);
                fputc('"', out);
-               fputs(pp_token.string.string.begin, out);
+               fputs(pp_token.literal.string.begin, out);
                fputc('"', out);
                break;
-       case TP_WIDE_CHARACTER_CONSTANT:
-               fputc('L', out);
+
        case TP_CHARACTER_CONSTANT:
+               fputs(get_string_encoding_prefix(pp_token.literal.string.encoding), out);
                fputc('\'', out);
-               fputs(pp_token.string.string.begin, out);
+               fputs(pp_token.literal.string.begin, out);
                fputc('\'', out);
                break;
        default:
@@ -1282,7 +1191,7 @@ static bool pp_tokens_equal(const token_t *token1, const token_t *token2)
        case TP_NUMBER:
        case TP_CHARACTER_CONSTANT:
        case TP_STRING_LITERAL:
-               return strings_equal(&token1->string.string, &token2->string.string);
+               return strings_equal(&token1->literal.string, &token2->literal.string);
 
        default:
                return true;
@@ -1432,58 +1341,49 @@ static void parse_undef_directive(void)
        eat_pp_directive();
 }
 
-static const char *parse_headername(void)
+/** behind an #include we can have the special headername lexems.
+ * They're only allowed behind an #include so they're not recognized
+ * by the normal next_preprocessing_token. We handle them as a special
+ * exception here */
+static void parse_headername(void)
 {
-       /* behind an #include we can have the special headername lexems.
-        * They're only allowed behind an #include so they're not recognized
-        * by the normal next_preprocessing_token. We handle them as a special
-        * exception here */
+       const source_position_t start_position = input.position;
+       string_t                string         = { NULL, 0, STRING_ENCODING_CHAR };
+       assert(obstack_object_size(&symbol_obstack) == 0);
+
        if (info.at_line_begin) {
                parse_error("expected headername after #include");
-               return NULL;
+               goto finish_error;
        }
 
-       assert(obstack_object_size(&symbol_obstack) == 0);
-
        /* check wether we have a "... or <... headername */
        switch (input.c) {
-       case '<':
+       {
+               utf32 delimiter;
+       case '<': delimiter = '>'; goto parse_name;
+       case '"': delimiter = '"'; goto parse_name;
+parse_name:
                next_char();
                while (true) {
                        switch (input.c) {
+                       case NEWLINE:
                        case EOF:
-                               /* fallthrough */
-                       MATCH_NEWLINE(
-                               parse_error("header name without closing '>'");
-                               return NULL;
-                       )
-                       case '>':
-                               next_char();
-                               goto finished_headername;
-                       }
-                       obstack_1grow(&symbol_obstack, (char) input.c);
-                       next_char();
-               }
-               /* we should never be here */
+                               errorf(&pp_token.base.source_position, "header name without closing '%c'", (char)delimiter);
+                               goto finish_error;
 
-       case '"':
-               next_char();
-               while (true) {
-                       switch (input.c) {
-                       case EOF:
-                               /* fallthrough */
-                       MATCH_NEWLINE(
-                               parse_error("header name without closing '>'");
-                               return NULL;
-                       )
-                       case '"':
-                               next_char();
-                               goto finished_headername;
+                       default:
+                               if (input.c == delimiter) {
+                                       next_char();
+                                       goto finished_headername;
+                               } else {
+                                       obstack_1grow(&symbol_obstack, (char)input.c);
+                                       next_char();
+                               }
+                               break;
                        }
-                       obstack_1grow(&symbol_obstack, (char) input.c);
-                       next_char();
                }
                /* we should never be here */
+       }
 
        default:
                /* TODO: do normal pp_token parsing and concatenate results */
@@ -1491,22 +1391,29 @@ static const char *parse_headername(void)
        }
 
 finished_headername:
-       obstack_1grow(&symbol_obstack, '\0');
-       char *headername = obstack_finish(&symbol_obstack);
+       string = sym_make_string(STRING_ENCODING_CHAR);
 
-       /* TODO: iterate search-path to find the file */
-
-       skip_whitespace();
-
-       return identify_string(headername);
+finish_error:
+       pp_token.base.source_position = start_position;
+       pp_token.kind                 = TP_HEADERNAME;
+       pp_token.literal.string       = string;
 }
 
 static bool do_include(bool system_include, const char *headername)
 {
+       size_t headername_len = strlen(headername);
        if (!system_include) {
-               /* for "bla" includes first try current dir
-                * TODO: this isn't correct, should be the directory of the source file
-                */
+               /* put dirname of current input on obstack */
+               const char *filename   = input.position.input_name;
+               const char *last_slash = strrchr(filename, '/');
+               if (last_slash != NULL) {
+                       size_t len = last_slash - filename;
+                       obstack_grow(&symbol_obstack, filename, len + 1);
+                       obstack_grow0(&symbol_obstack, headername, headername_len);
+                       char *complete_path = obstack_finish(&symbol_obstack);
+                       headername = identify_string(complete_path);
+               }
+
                FILE *file = fopen(headername, "r");
                if (file != NULL) {
                        switch_input(file, headername);
@@ -1514,47 +1421,64 @@ static bool do_include(bool system_include, const char *headername)
                }
        }
 
-       size_t headername_len = strlen(headername);
-       assert(obstack_object_size(&pp_obstack) == 0);
+       assert(obstack_object_size(&symbol_obstack) == 0);
        /* check searchpath */
        for (searchpath_entry_t *entry = searchpath; entry != NULL;
             entry = entry->next) {
            const char *path = entry->path;
            size_t      len  = strlen(path);
-               obstack_grow(&pp_obstack, path, len);
+               obstack_grow(&symbol_obstack, path, len);
                if (path[len-1] != '/')
-                       obstack_1grow(&pp_obstack, '/');
-               obstack_grow(&pp_obstack, headername, headername_len+1);
+                       obstack_1grow(&symbol_obstack, '/');
+               obstack_grow(&symbol_obstack, headername, headername_len+1);
 
-               char *complete_path = obstack_finish(&pp_obstack);
+               char *complete_path = obstack_finish(&symbol_obstack);
                FILE *file          = fopen(complete_path, "r");
                if (file != NULL) {
                        const char *filename = identify_string(complete_path);
                        switch_input(file, filename);
                        return true;
+               } else {
+                       obstack_free(&symbol_obstack, complete_path);
                }
-               obstack_free(&pp_obstack, complete_path);
        }
 
        return false;
 }
 
+/* read till next newline character, only for parse_include_directive(),
+ * use eat_pp_directive() in all other cases */
+static void skip_till_newline(void)
+{
+       /* skip till newline */
+       while (true) {
+               switch (input.c) {
+               case NEWLINE:
+               case EOF:
+                       return;
+               }
+               next_char();
+       }
+}
+
 static bool parse_include_directive(void)
 {
        /* don't eat the TP_include here!
         * we need an alternative parsing for the next token */
        skip_whitespace();
        bool system_include = input.c == '<';
-       const char *headername = parse_headername();
-       if (headername == NULL) {
+       parse_headername();
+       string_t headername = pp_token.literal.string;
+       if (headername.begin == NULL) {
                eat_pp_directive();
                return false;
        }
 
+       skip_whitespace();
        if (!info.at_line_begin) {
                warningf(WARN_OTHER, &pp_token.base.source_position,
                         "extra tokens at end of #include directive");
-               eat_pp_directive();
+               skip_till_newline();
        }
 
        if (n_inputs > INCLUDE_LIMIT) {
@@ -1564,19 +1488,12 @@ static bool parse_include_directive(void)
                return false;
        }
 
-       /* we have to reenable space counting and macro expansion here,
-        * because it is still disabled in directive parsing,
-        * but we will trigger a preprocessing token reading of the new file
-        * now and need expansions/space counting */
-       in_pp_directive = false;
-
        /* switch inputs */
        emit_newlines();
        push_input();
-       bool res = do_include(system_include, headername);
+       bool res = do_include(system_include, pp_token.literal.string.begin);
        if (!res) {
-               errorf(&pp_token.base.source_position,
-                      "failed including '%s': %s", headername, strerror(errno));
+               errorf(&pp_token.base.source_position, "failed including '%S': %s", &pp_token.literal, strerror(errno));
                pop_restore_input();
                return false;
        }
@@ -1640,7 +1557,7 @@ static void parse_ifdef_ifndef_directive(void)
                condition = true;
        } else {
                /* evaluate wether we are in true or false case */
-               condition = !pp_token.base.symbol->pp_definition == is_ifndef;
+               condition = (bool)!pp_token.base.symbol->pp_definition == is_ifndef;
 
                next_preprocessing_token();
 
@@ -1681,7 +1598,7 @@ static void parse_else_directive(void)
        if (conditional->in_else) {
                errorf(&pp_token.base.source_position,
                       "#else after #else (condition started %P)",
-                      conditional->source_position);
+                      &conditional->source_position);
                skip_mode = true;
                return;
        }
@@ -1718,9 +1635,13 @@ static void parse_endif_directive(void)
 
 static void parse_preprocessing_directive(void)
 {
-       in_pp_directive = true;
        eat_pp('#');
 
+       if (info.at_line_begin) {
+               /* empty directive */
+               return;
+       }
+
        if (skip_mode) {
                switch (pp_token.kind) {
                case TP_ifdef:
@@ -1770,7 +1691,6 @@ static void parse_preprocessing_directive(void)
                }
        }
 
-       in_pp_directive = false;
        assert(info.at_line_begin);
 }
 
@@ -1833,6 +1753,7 @@ int pptest_main(int argc, char **argv)
 
        /* simplistic commandline parser */
        const char *filename = NULL;
+       const char *output = NULL;
        for (int i = 1; i < argc; ++i) {
                const char *opt = argv[i];
                if (streq(opt, "-I")) {
@@ -1840,6 +1761,9 @@ int pptest_main(int argc, char **argv)
                        continue;
                } else if (streq(opt, "-E")) {
                        /* ignore */
+               } else if (streq(opt, "-o")) {
+                       output = argv[++i];
+                       continue;
                } else if (opt[0] == '-') {
                        fprintf(stderr, "Unknown option '%s'\n", opt);
                } else {
@@ -1853,7 +1777,15 @@ int pptest_main(int argc, char **argv)
                return 1;
        }
 
-       out = stdout;
+       if (output == NULL) {
+               out = stdout;
+       } else {
+               out = fopen(output, "w");
+               if (out == NULL) {
+                       fprintf(stderr, "Couldn't open output '%s'\n", output);
+                       return 1;
+               }
+       }
 
        /* just here for gcc compatibility */
        fprintf(out, "# 1 \"%s\"\n", filename);
@@ -1873,7 +1805,7 @@ int pptest_main(int argc, char **argv)
                        continue;
                } else if (pp_token.kind == TP_EOF) {
                        goto end_of_main_loop;
-               } else if (pp_token.kind == TP_IDENTIFIER && !in_pp_directive) {
+               } else if (pp_token.kind == TP_IDENTIFIER) {
                        symbol_t        *const symbol        = pp_token.base.symbol;
                        pp_definition_t *const pp_definition = symbol->pp_definition;
                        if (pp_definition != NULL && !pp_definition->is_expanding) {
@@ -1922,6 +1854,8 @@ end_of_main_loop:
        fputc('\n', out);
        check_unclosed_conditionals();
        close_input();
+       if (out != stdout)
+               fclose(out);
 
        obstack_free(&input_obstack, NULL);
        obstack_free(&pp_obstack, NULL);