From c2cfbb0a7538cdbddbe11457a9a3a75d59dd21ff Mon Sep 17 00:00:00 2001 From: gitmodimo Date: Mon, 30 Dec 2024 14:11:25 +0100 Subject: [PATCH] fix: Rewrite more paths with absolute_paths_in_stderr (#1539) --- src/ccache/core/common.cpp | 21 +++++++++++++++++---- unittest/test_core_common.cpp | 28 ++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/ccache/core/common.cpp b/src/ccache/core/common.cpp index f9593733..df81f8d4 100644 --- a/src/ccache/core/common.cpp +++ b/src/ccache/core/common.cpp @@ -93,7 +93,17 @@ make_relative_path(const Context& ctx, const fs::path& path) std::string rewrite_stderr_to_absolute_paths(std::string_view text) { - const std::string_view in_file_included_from = "In file included from "; + // Line prefixes from gcc compiler plus extra space at the end + // https://gcc.gnu.org/git?p=gcc.git;a=blob;f=gcc/diagnostic-format-text.cc;h=856d25e8482cd0bff39bd8076e6e529e184362cc;hb=HEAD#l676 + const std::string_view in_file_included_from_msgs[] = { + " from ", + "In file included from ", + " included from ", + "In module imported at ", // longer message first to match in full + "In module ", + "of module ", + "imported at ", + }; std::string result; using util::Tokenizer; @@ -101,9 +111,12 @@ rewrite_stderr_to_absolute_paths(std::string_view text) "\n", Tokenizer::Mode::include_empty, Tokenizer::IncludeDelimiter::yes)) { - if (util::starts_with(line, in_file_included_from)) { - result += in_file_included_from; - line = line.substr(in_file_included_from.length()); + for (auto& in_file_included_from : in_file_included_from_msgs) { + if (util::starts_with(line, in_file_included_from)) { + result += in_file_included_from; + line = line.substr(in_file_included_from.length()); + break; + } } while (!line.empty() && line[0] == 0x1b) { auto csi_seq = find_first_ansi_csi_seq(line); diff --git a/unittest/test_core_common.cpp b/unittest/test_core_common.cpp index ec628f73..43c2a084 100644 --- a/unittest/test_core_common.cpp +++ b/unittest/test_core_common.cpp @@ -68,7 +68,19 @@ TEST_CASE("core::rewrite_stderr_to_absolute_paths") "\x1b[01m\x1b[Kexisting:\x1b[m\x1b[K: foo\n" "\x1b[01m\x1b[Kexisting:47:11:\x1b[m\x1b[K: foo\n" "In file included from \x1b[01m\x1b[Kexisting:\x1b[m\x1b[K: foo\n" - "In file included from \x1b[01m\x1b[Kexisting:47:11:\x1b[m\x1b[K: foo\n"; + "In file included from \x1b[01m\x1b[Kexisting:47:11:\x1b[m\x1b[K: foo\n" + " from \x1b[01m\x1b[Kexisting:\x1b[m\x1b[K: foo\n" + " from \x1b[01m\x1b[Kexisting:47:11:\x1b[m\x1b[K: foo\n" + " included from \x1b[01m\x1b[Kexisting:\x1b[m\x1b[K: foo\n" + " included from \x1b[01m\x1b[Kexisting:47:11:\x1b[m\x1b[K: foo\n" + "In module \x1b[01m\x1b[Kexisting:\x1b[m\x1b[K: foo\n" + "In module \x1b[01m\x1b[Kexisting:47:11:\x1b[m\x1b[K: foo\n" + "of module \x1b[01m\x1b[Kexisting:\x1b[m\x1b[K: foo\n" + "of module \x1b[01m\x1b[Kexisting:47:11:\x1b[m\x1b[K: foo\n" + "In module imported at \x1b[01m\x1b[Kexisting:\x1b[m\x1b[K: foo\n" + "In module imported at \x1b[01m\x1b[Kexisting:47:11:\x1b[m\x1b[K: foo\n" + "imported at \x1b[01m\x1b[Kexisting:\x1b[m\x1b[K: foo\n" + "imported at \x1b[01m\x1b[Kexisting:47:11:\x1b[m\x1b[K: foo\n"; std::string expected = FMT( "a:1:2\n" "a(3):\n" @@ -85,7 +97,19 @@ TEST_CASE("core::rewrite_stderr_to_absolute_paths") "\x1b[01m\x1b[K{0}:\x1b[m\x1b[K: foo\n" "\x1b[01m\x1b[K{0}:47:11:\x1b[m\x1b[K: foo\n" "In file included from \x1b[01m\x1b[K{0}:\x1b[m\x1b[K: foo\n" - "In file included from \x1b[01m\x1b[K{0}:47:11:\x1b[m\x1b[K: foo\n", + "In file included from \x1b[01m\x1b[K{0}:47:11:\x1b[m\x1b[K: foo\n" + " from \x1b[01m\x1b[K{0}:\x1b[m\x1b[K: foo\n" + " from \x1b[01m\x1b[K{0}:47:11:\x1b[m\x1b[K: foo\n" + " included from \x1b[01m\x1b[K{0}:\x1b[m\x1b[K: foo\n" + " included from \x1b[01m\x1b[K{0}:47:11:\x1b[m\x1b[K: foo\n" + "In module \x1b[01m\x1b[K{0}:\x1b[m\x1b[K: foo\n" + "In module \x1b[01m\x1b[K{0}:47:11:\x1b[m\x1b[K: foo\n" + "of module \x1b[01m\x1b[K{0}:\x1b[m\x1b[K: foo\n" + "of module \x1b[01m\x1b[K{0}:47:11:\x1b[m\x1b[K: foo\n" + "In module imported at \x1b[01m\x1b[K{0}:\x1b[m\x1b[K: foo\n" + "In module imported at \x1b[01m\x1b[K{0}:47:11:\x1b[m\x1b[K: foo\n" + "imported at \x1b[01m\x1b[K{0}:\x1b[m\x1b[K: foo\n" + "imported at \x1b[01m\x1b[K{0}:47:11:\x1b[m\x1b[K: foo\n", *fs::canonical("existing")); CHECK(core::rewrite_stderr_to_absolute_paths(input) == expected); } -- 2.47.2