We weren't handling properly c-style comments that started at

the beginning of the line but didn't go to the end.  I thought
this wouldn't happen in google code, but it does in macros:
#define FOO \
/* doing something cool now */ \
...

Rather than just check for the special case with backslash
after the comment, I "correctly" deal with all cases where a
c-style comment-line doesn't take the entire line.  I put
"correctly" in quotes because I always call such lines
'contentful' lines, when they could also be forward-declare
lines.  I'm ok with that.

While testing, discovered an off-by-one error, which I now fix
and test for as well.

R=wan
DELTA=60  (56 added, 0 deleted, 4 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=1609
This commit is contained in:
csilvers+iwyu 2011-04-26 23:26:53 +00:00
parent 525d9821e0
commit ea88085897
2 changed files with 60 additions and 4 deletions

View File

@ -96,7 +96,7 @@ _COMMENT_RE = re.compile('\s*//.*')
# These are the types of lines a file can have. These are matched
# using re.match(), so don't need a leading ^.
_C_COMMENT_START_RE = re.compile(r'\s*/\*')
_C_COMMENT_END_RE = re.compile(r'.*\*/\s*$')
_C_COMMENT_END_RE = re.compile(r'.*\*/\s*(.*)$')
_COMMENT_LINE_RE = re.compile(r'\s*//')
_BLANK_LINE_RE = re.compile(r'\s*$')
_IF_RE = re.compile(r'\s*#\s*if') # compiles #if/ifdef/ifndef
@ -598,9 +598,20 @@ def _CalculateLineTypesAndKeys(file_lines, iwyu_record):
if line_info.line is None:
line_info.type = None
elif _C_COMMENT_START_RE.match(line_info.line):
line_info.type = _COMMENT_LINE_RE
if not _C_COMMENT_END_RE.match(line_info.line): # not a 1-line comment
# Note: _C_COMMENT_START_RE only matches a comment at the start
# of a line. Comments in the middle of a line are ignored.
# This can cause problems with multi-line comments that start
# in the middle of the line, but that's hopefully quite rare.
# TODO(csilvers): check for that case.
m = _C_COMMENT_END_RE.match(line_info.line)
if not m: # comment continues onto future lines
line_info.type = _COMMENT_LINE_RE
in_c_style_comment = True
elif not m.group(1): # comment extends across entire line (only)
line_info.type = _COMMENT_LINE_RE
else: # comment takes only part of line, treat as content
# TODO(csilvers): this mis-diagnoses lines like '/*comment*/class Foo;'
line_info.type = None
elif in_c_style_comment and _C_COMMENT_END_RE.match(line_info.line):
line_info.type = _COMMENT_LINE_RE
in_c_style_comment = False
@ -1459,7 +1470,7 @@ def _FirstReorderSpanWith(file_lines, good_reorder_spans, kind, filename,
# of the _FORWARD_DECLARE span, whichever is smaller.
line_number = 0
seen_header_guard = False
while line_number < len(file_lines) - 1:
while line_number < len(file_lines):
if file_lines[line_number].deleted:
line_number += 1
elif file_lines[line_number].type == _HEADER_GUARD_RE:

View File

@ -541,6 +541,29 @@ The full include-list for top_of_file_comments.c:
self.RegisterFileContents({'top_of_file_comments.c': infile})
self.ProcessAndTest(iwyu_output)
def testNotFullLineCComments(self):
"""Tests that we treat lines with c comments then code as code-lines."""
infile = """\
// Copyright 2010
///+#include <stdio.h>
///+
/* code here */ x = 4;
int main() { return 0; }
"""
iwyu_output = """\
not_full_line_c_comments.c should add these lines:
#include <stdio.h>
not_full_line_c_comments.c should remove these lines:
The full include-list for not_full_line_c_comments.c:
#include <stdio.h>
---
"""
self.RegisterFileContents({'not_full_line_c_comments.c': infile})
self.ProcessAndTest(iwyu_output)
def testUnusualHFileNames(self):
"""Tests we treat .pb.h files as header files."""
infile = """\
@ -1595,6 +1618,28 @@ class Foo;
self.RegisterFileContents({'empty_file': infile})
self.ProcessAndTest(iwyu_output)
def testAddIncludeToOnlyOneContentfulLineFile(self):
"""Prevent regression when the only contentful line was the last."""
infile = """\
// Copyright 2010
///+
///+#include <stdio.h>
int main() { return 0; }
"""
iwyu_output = """\
only_one_contentful_line.c should add these lines:
#include <stdio.h>
only_one_contentful_line.c should remove these lines:
The full include-list for only_one_contentful_line.c:
#include <stdio.h>
---
"""
self.RegisterFileContents({'only_one_contentful_line.c': infile})
self.ProcessAndTest(iwyu_output)
def testCommentsAtEndOfFile(self):
"""Tests we don't crash if a file ends with #includs and then a comment."""
infile = """\