From a2d721a90e955ee4fad4584c150e4aaca42e94d7 Mon Sep 17 00:00:00 2001 From: brettw Date: Thu, 15 Oct 2015 22:34:24 -0700 Subject: Handle Mozilla license blocks in the GN header checker. The include iterator doesn't handle real C preprocessor. Mozilla license blocks use C-style comments with *'s along the left, and are long enough that the header checker gives up. This patch doesn't fix the general comment issue, different types of comments can still confuse us, but Mozilla license blocks in particular are now handled. BUG=543850 Review URL: https://codereview.chromium.org/1411573002 Cr-Commit-Position: refs/heads/master@{#354454} --- tools/gn/c_include_iterator.cc | 2 ++ tools/gn/c_include_iterator_unittest.cc | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) (limited to 'tools') diff --git a/tools/gn/c_include_iterator.cc b/tools/gn/c_include_iterator.cc index 9e719c7..120295e 100644 --- a/tools/gn/c_include_iterator.cc +++ b/tools/gn/c_include_iterator.cc @@ -48,6 +48,8 @@ base::StringPiece TrimLeadingWhitespace(const base::StringPiece& str) { bool ShouldCountTowardNonIncludeLines(const base::StringPiece& line) { if (StartsWith(line, "//")) return false; // Don't count comments. + if (StartsWith(line, "/*") || StartsWith(line, " *")) + return false; // C-style comment blocks with stars along the left side. if (StartsWith(line, "#")) return false; // Don't count preprocessor. if (base::ContainsOnlyChars(line, base::kWhitespaceASCII)) diff --git a/tools/gn/c_include_iterator_unittest.cc b/tools/gn/c_include_iterator_unittest.cc index 11fa991..1add29b5 100644 --- a/tools/gn/c_include_iterator_unittest.cc +++ b/tools/gn/c_include_iterator_unittest.cc @@ -132,3 +132,26 @@ TEST(CIncludeIterator, TolerateNonIncludes) { } EXPECT_FALSE(iter.GetNextIncludeString(&contents, &range)); } + +// Tests that comments of the form +// /* +// * +// */ +// are not counted toward the non-include line count. +TEST(CIncludeIterator, CStyleComments) { + std::string buffer("/*"); + for (size_t i = 0; i < 1000; i++) + buffer.append(" *\n"); + buffer.append(" */\n\n"); + buffer.append("#include \"foo/bar.h\"\n"); + + InputFile file(SourceFile("//foo.cc")); + file.SetContents(buffer); + + base::StringPiece contents; + LocationRange range; + + CIncludeIterator iter(&file); + EXPECT_TRUE(iter.GetNextIncludeString(&contents, &range)); + EXPECT_EQ("foo/bar.h", contents); +} -- cgit v1.1