diff --git a/CMakeLists.txt b/CMakeLists.txt index 78243b7bac..7b4062c891 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -131,6 +131,7 @@ add_library(libninja OBJECT src/clparser.cc src/dyndep.cc src/dyndep_parser.cc + src/dynout_parser.cc src/debug_flags.cc src/deps_log.cc src/disk_interface.cc @@ -259,6 +260,7 @@ if(BUILD_TESTING) src/deps_log_test.cc src/disk_interface_test.cc src/dyndep_parser_test.cc + src/dynout_parser_test.cc src/edit_distance_test.cc src/graph_test.cc src/json_test.cc diff --git a/configure.py b/configure.py index 2b16618c43..b76d8b61ad 100755 --- a/configure.py +++ b/configure.py @@ -538,6 +538,7 @@ def has_re2c() -> bool: 'disk_interface', 'dyndep', 'dyndep_parser', + 'dynout_parser', 'edit_distance', 'eval_env', 'graph', diff --git a/src/dynout_parser.cc b/src/dynout_parser.cc new file mode 100644 index 0000000000..4bc05183c0 --- /dev/null +++ b/src/dynout_parser.cc @@ -0,0 +1,41 @@ +/* Generated by re2c 1.3 */ +// Copyright 2011 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "dynout_parser.h" + +#include "metrics.h" +#include "string_piece.h" + +bool DynoutParser::Parse(const std::string& content, + std::vector& result, std::string* err) { + METRIC_RECORD("dynout parse"); + /// Split `content` into a series of individual text lines, without trailing + /// newlines, and remove empty lines + const char* input = content.c_str(); + const char* start = input; + const char* limit = input + content.size(); + while (start < limit) { + const char* end = start; + // Note: \r\n will be treated as a line end + an empty line, + // the latter will be ignored. This simplifies the logic in this + // loop to reduce its code and speed it up. + while (end < limit && (*end != '\n' && *end != '\r')) + ++end; + if (end > start) + result.push_back(StringPiece(start, end - start)); + start = end + 1; + } + return true; +} diff --git a/src/dynout_parser.h b/src/dynout_parser.h new file mode 100644 index 0000000000..b859a0b1e5 --- /dev/null +++ b/src/dynout_parser.h @@ -0,0 +1,32 @@ +// Copyright 2011 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef NINJA_DYNOUT_PARSER_H_ +#define NINJA_DYNOUT_PARSER_H_ + +#include +#include + +#include "string_piece.h" + +/// Parser for dynout file. +struct DynoutParser { + + /// Parse a dynout file, placing each listed output into `result`. + /// The resulting StringPieces point into `content`. + static bool Parse(const std::string& content, + std::vector& result, std::string* err); +}; + +#endif // NINJA_DYNOUT_PARSER_H_ diff --git a/src/dynout_parser_test.cc b/src/dynout_parser_test.cc new file mode 100644 index 0000000000..81d57b9525 --- /dev/null +++ b/src/dynout_parser_test.cc @@ -0,0 +1,64 @@ +// Copyright 2011 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "dynout_parser.h" + +#include "test.h" + +using namespace std; + +struct DynoutParserTest : public testing::Test { + + VirtualFileSystem fs_; +}; + +TEST_F(DynoutParserTest, Empty) { + std::string input = ""; + string err; + std::vector result; + EXPECT_TRUE(DynoutParser::Parse(input, result, &err)); + ASSERT_EQ(err, ""); + ASSERT_TRUE(result.empty()); +} + +TEST_F(DynoutParserTest, MultipleEntries) { + std::string input = "file1\nfile2\nfile3"; + string err; + std::vector result; + EXPECT_TRUE(DynoutParser::Parse(input, result, &err)); + ASSERT_EQ(err, ""); + ASSERT_EQ(result[0], "file1"); + ASSERT_EQ(result[1], "file2"); + ASSERT_EQ(result[2], "file3"); +} + +TEST_F(DynoutParserTest, EmptyLines) { + std::string input = "\nfile1\n\n\nfile2\n\n"; + string err; + std::vector result; + EXPECT_TRUE(DynoutParser::Parse(input, result, &err)); + ASSERT_EQ(err, ""); + ASSERT_EQ(result[0], "file1"); + ASSERT_EQ(result[1], "file2"); +} + +TEST_F(DynoutParserTest, CRLF) { + std::string input = "\r\nfile1\r\n\r\nfile2\r\n"; + string err; + std::vector result; + EXPECT_TRUE(DynoutParser::Parse(input, result, &err)); + ASSERT_EQ(err, ""); + ASSERT_EQ(result[0], "file1"); + ASSERT_EQ(result[1], "file2"); +}