From 8901785edd624940494c3b6d6c14eb657489ed19 Mon Sep 17 00:00:00 2001 From: matyalatte Date: Wed, 9 Oct 2024 21:08:05 +0900 Subject: [PATCH 1/2] support unix convention of using '-' for stdin --- src/file_linter.cpp | 32 ++++++++++++++++---------------- src/options.cpp | 12 +++++++++++- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/file_linter.cpp b/src/file_linter.cpp index fd9644f..b5f5c7d 100644 --- a/src/file_linter.cpp +++ b/src/file_linter.cpp @@ -4059,21 +4059,21 @@ void FileLinter::ProcessFile() { std::vector null_lines = {}; std::vector lines = {}; - if (StrIsChar(m_filename, '-')) { - // TODO(matyalatte): support stdin code - /* - lines = codecs.StreamReaderWriter(sys.stdin, - codecs.getreader('utf8'), - codecs.getwriter('utf8'), - 'replace').read().split('\n'); - */ - m_cpplint_state->PrintError("Skipping input \"-\": stdin is unsupported yet.\n"); - return; - } else { - std::ifstream file(m_file, std::ios::binary); - if (!file) { - m_cpplint_state->PrintError( - "Skipping input '" + m_filename + "': Can't open for reading\n"); + { + std::istream* stream; + std::ifstream file; + if (StrIsChar(m_filename, '-')) { + // Read from stdin + stream = &std::cin; + } else { + // Read from a file + file = std::ifstream(m_file, std::ios::binary); + if (!file) { + m_cpplint_state->PrintError( + "Skipping input '" + m_filename + "': Can't open for reading\n"); + return; + } + stream = &file; } // insert a comment line at the beginning of file. @@ -4085,7 +4085,7 @@ void FileLinter::ProcessFile() { buffer.resize(120); // Note: We can't use getline cause it trims NUL bytes and a linefeed at EOF. while ((status & LINE_EOF) == 0) { - std::string line = GetLine(file, &buffer, &status); + std::string line = GetLine(*stream, &buffer, &status); if (!line.empty() && line.back() == '\r') { // line ends with \r. crlf_lines.push_back(linenum); diff --git a/src/options.cpp b/src/options.cpp index af37763..65af6bd 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -452,7 +452,8 @@ std::vector Options::ParseArguments(int argc, char** argv, std::vector filenames = {}; for (; argp < argv + argc; argp++) { fs::path p = argp[0]; - if (!fs::exists(p)) { + // Note: "-" is an alias for "read from stdin" + if (p != "-" && !fs::exists(p)) { // TODO(unknown): Maybe make this have an exit code of 2 after all is done cpplint_state->PrintError("Skipping input '" + p.string() + "': Path not found."); continue; @@ -504,6 +505,10 @@ void Options::ProcessIncludeOrderOption(const std::string& val) { static bool ShouldBeExcluded(const fs::path& filename, const std::vector& excludes) { + if (filename == "-") { + // "-" is an alias for "read from stdin" + return false; + } std::string file_str = filename.string(); for (const GlobPattern& exc : excludes) { // Check if file is the same as (or a child of) a glob pattern @@ -545,6 +550,11 @@ std::vector Options::ExpandDirectories(const std::vector& fi std::vector filtered = {}; std::set extensions = GetAllExtensions(); for (const fs::path& f : filenames) { + if (f == "-") { + // "-" is an alias for "read from stdin" + filtered.emplace_back(f); + continue; + } ExpandDirectoriesRec(f, filtered, extensions); } return filtered; From b1ed4951926d6fa497e955e8303e61106f8a9ab8 Mon Sep 17 00:00:00 2001 From: matyalatte Date: Wed, 9 Oct 2024 21:22:05 +0900 Subject: [PATCH 2/2] update changelog --- docs/CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 7a64397..467c37a 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## 0.3.0 (Unreleased) + +- Fixed a bug where `latch` and `numbers` were not considered as c++ headers, courtesy of @GermanAizek. (https://github.com/matyalatte/cpplint-cpp/pull/6) +- `--exclude` now supports glob patterns. (https://github.com/matyalatte/cpplint-cpp/pull/9) +- Fixed a compile error on Ubuntu20.04 with an old version of GCC. (https://github.com/matyalatte/cpplint-cpp/pull/12) +- Added support for unix convention of using `-` for stdin. (https://github.com/matyalatte/cpplint-cpp/pull/13) + ## 0.2.1 (2024-08-25) - Initial release for cpplint-cpp.