aboutsummaryrefslogtreecommitdiff
path: root/libcpp
diff options
context:
space:
mode:
Diffstat (limited to 'libcpp')
-rw-r--r--libcpp/include/cpplib.h6
-rw-r--r--libcpp/internal.h4
-rw-r--r--libcpp/lex.cc56
3 files changed, 50 insertions, 16 deletions
diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h
index be367b1..3f05d08 100644
--- a/libcpp/include/cpplib.h
+++ b/libcpp/include/cpplib.h
@@ -598,6 +598,9 @@ struct cpp_options
/* True if -finput-charset= option has been used explicitly. */
bool cpp_input_charset_explicit;
+ /* -Wtrailing-whitespace= value. */
+ unsigned char cpp_warn_trailing_whitespace;
+
/* Dependency generation. */
struct
{
@@ -722,7 +725,8 @@ enum cpp_warning_reason {
CPP_W_INVALID_UTF8,
CPP_W_UNICODE,
CPP_W_HEADER_GUARD,
- CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER
+ CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER,
+ CPP_W_TRAILING_WHITESPACE
};
/* Callback for header lookup for HEADER, which is the name of a
diff --git a/libcpp/internal.h b/libcpp/internal.h
index 6c2a36e..a658a8c 100644
--- a/libcpp/internal.h
+++ b/libcpp/internal.h
@@ -318,8 +318,8 @@ struct _cpp_line_note
/* Type of note. The 9 'from' trigraph characters represent those
trigraphs, '\\' an escaped newline, ' ' an escaped newline with
- intervening space, 0 represents a note that has already been handled,
- and anything else is invalid. */
+ intervening space, 'W' trailing whitespace, 0 represents a note that
+ has already been handled, and anything else is invalid. */
unsigned int type;
};
diff --git a/libcpp/lex.cc b/libcpp/lex.cc
index f3feadf..bb5cd39 100644
--- a/libcpp/lex.cc
+++ b/libcpp/lex.cc
@@ -928,7 +928,7 @@ _cpp_clean_line (cpp_reader *pfile)
if (p == buffer->next_line || p[-1] != '\\')
break;
- add_line_note (buffer, p - 1, p != d ? ' ': '\\');
+ add_line_note (buffer, p - 1, p != d ? ' ' : '\\');
d = p - 2;
buffer->next_line = p - 1;
}
@@ -943,6 +943,20 @@ _cpp_clean_line (cpp_reader *pfile)
}
}
}
+ done:
+ if (d > buffer->next_line
+ && CPP_OPTION (pfile, cpp_warn_trailing_whitespace))
+ switch (CPP_OPTION (pfile, cpp_warn_trailing_whitespace))
+ {
+ case 1:
+ if (ISBLANK (d[-1]))
+ add_line_note (buffer, d - 1, 'W');
+ break;
+ case 2:
+ if (IS_NVSPACE (d[-1]) && d[-1])
+ add_line_note (buffer, d - 1, 'W');
+ break;
+ }
}
else
{
@@ -955,7 +969,6 @@ _cpp_clean_line (cpp_reader *pfile)
s++;
}
- done:
*d = '\n';
/* A sentinel note that should never be processed. */
add_line_note (buffer, d + 1, '\n');
@@ -1013,13 +1026,23 @@ _cpp_process_line_notes (cpp_reader *pfile, int in_comment)
if (note->type == '\\' || note->type == ' ')
{
- if (note->type == ' ' && !in_comment)
- cpp_error_with_line (pfile, CPP_DL_WARNING, pfile->line_table->highest_line, col,
- "backslash and newline separated by space");
+ if (note->type == ' ')
+ {
+ if (!in_comment)
+ cpp_error_with_line (pfile, CPP_DL_WARNING,
+ pfile->line_table->highest_line, col,
+ "backslash and newline separated by "
+ "space");
+ else if (CPP_OPTION (pfile, cpp_warn_trailing_whitespace))
+ cpp_warning_with_line (pfile, CPP_W_TRAILING_WHITESPACE,
+ pfile->line_table->highest_line, col,
+ "trailing whitespace");
+ }
if (buffer->next_line > buffer->rlimit)
{
- cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->line_table->highest_line, col,
+ cpp_error_with_line (pfile, CPP_DL_PEDWARN,
+ pfile->line_table->highest_line, col,
"backslash-newline at end of file");
/* Prevent "no newline at end of file" warning. */
buffer->next_line = buffer->rlimit;
@@ -1040,15 +1063,16 @@ _cpp_process_line_notes (cpp_reader *pfile, int in_comment)
note->type,
(int) _cpp_trigraph_map[note->type]);
else
- {
- cpp_warning_with_line
- (pfile, CPP_W_TRIGRAPHS,
- pfile->line_table->highest_line, col,
- "trigraph %<??%c%> ignored, use %<-trigraphs%> to enable",
- note->type);
- }
+ cpp_warning_with_line (pfile, CPP_W_TRIGRAPHS,
+ pfile->line_table->highest_line, col,
+ "trigraph %<??%c%> ignored, use "
+ "%<-trigraphs%> to enable", note->type);
}
}
+ else if (note->type == 'W')
+ cpp_warning_with_line (pfile, CPP_W_TRAILING_WHITESPACE,
+ pfile->line_table->highest_line, col,
+ "trailing whitespace");
else if (note->type == 0)
/* Already processed in lex_raw_string. */;
else
@@ -2507,6 +2531,12 @@ lex_raw_string (cpp_reader *pfile, cpp_token *token, const uchar *base)
note++;
break;
+ case 'W':
+ /* Don't warn about trailing whitespace in raw string literals. */
+ note->type = 0;
+ note++;
+ break;
+
default:
gcc_checking_assert (_cpp_trigraph_map[note->type]);