https://github.com/Sirraide updated https://github.com/llvm/llvm-project/pull/143520
>From 15c0a79d6a0cd65d88fbe152275b224201e632a1 Mon Sep 17 00:00:00 2001 From: Sirraide <aeternalm...@gmail.com> Date: Tue, 10 Jun 2025 14:32:32 +0200 Subject: [PATCH 1/2] [Clang] [Diagnostics] Simplify filenames that contain '..' --- clang/include/clang/Frontend/TextDiagnostic.h | 1 + clang/lib/Frontend/TextDiagnostic.cpp | 32 ++++++++++++------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/clang/include/clang/Frontend/TextDiagnostic.h b/clang/include/clang/Frontend/TextDiagnostic.h index e2e88d4d648a2..9c77bc3e00e19 100644 --- a/clang/include/clang/Frontend/TextDiagnostic.h +++ b/clang/include/clang/Frontend/TextDiagnostic.h @@ -35,6 +35,7 @@ namespace clang { class TextDiagnostic : public DiagnosticRenderer { raw_ostream &OS; const Preprocessor *PP; + llvm::StringMap<SmallString<128>> SimplifiedFileNameCache; public: TextDiagnostic(raw_ostream &OS, const LangOptions &LangOpts, diff --git a/clang/lib/Frontend/TextDiagnostic.cpp b/clang/lib/Frontend/TextDiagnostic.cpp index b9e681b52e509..edbad42b39950 100644 --- a/clang/lib/Frontend/TextDiagnostic.cpp +++ b/clang/lib/Frontend/TextDiagnostic.cpp @@ -738,12 +738,20 @@ void TextDiagnostic::printDiagnosticMessage(raw_ostream &OS, } void TextDiagnostic::emitFilename(StringRef Filename, const SourceManager &SM) { -#ifdef _WIN32 - SmallString<4096> TmpFilename; -#endif - if (DiagOpts.AbsolutePath) { - auto File = SM.getFileManager().getOptionalFileRef(Filename); - if (File) { + auto File = SM.getFileManager().getOptionalFileRef(Filename); + + // Try to simplify paths that contain '..' in any case since paths to + // standard library headers especially tend to get quite long otherwise. + // Only do that for local filesystems though to avoid slowing down + // compilation too much. + auto AlwaysSimplify = [&] { + return File->getName().contains("..") && + llvm::sys::fs::is_local(File->getName()); + }; + + if (File && (DiagOpts.AbsolutePath || AlwaysSimplify())) { + SmallString<128> &CacheEntry = SimplifiedFileNameCache[Filename]; + if (CacheEntry.empty()) { // We want to print a simplified absolute path, i. e. without "dots". // // The hardest part here are the paths like "<part1>/<link>/../<part2>". @@ -759,15 +767,15 @@ void TextDiagnostic::emitFilename(StringRef Filename, const SourceManager &SM) { // on Windows we can just use llvm::sys::path::remove_dots(), because, // on that system, both aforementioned paths point to the same place. #ifdef _WIN32 - TmpFilename = File->getName(); - llvm::sys::fs::make_absolute(TmpFilename); - llvm::sys::path::native(TmpFilename); - llvm::sys::path::remove_dots(TmpFilename, /* remove_dot_dot */ true); - Filename = StringRef(TmpFilename.data(), TmpFilename.size()); + CacheEntry = File->getName(); + llvm::sys::fs::make_absolute(CacheEntry); + llvm::sys::path::native(CacheEntry); + llvm::sys::path::remove_dots(CacheEntry, /* remove_dot_dot */ true); #else - Filename = SM.getFileManager().getCanonicalName(*File); + CacheEntry = SM.getFileManager().getCanonicalName(*File); #endif } + Filename = CacheEntry; } OS << Filename; >From 97c0accf4fb3dc983bbb9f344ad66d0281b231fb Mon Sep 17 00:00:00 2001 From: Sirraide <aeternalm...@gmail.com> Date: Tue, 10 Jun 2025 14:47:43 +0200 Subject: [PATCH 2/2] Use whichever path ends up being shorter --- clang/lib/Frontend/TextDiagnostic.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/clang/lib/Frontend/TextDiagnostic.cpp b/clang/lib/Frontend/TextDiagnostic.cpp index edbad42b39950..b77c1797c51c5 100644 --- a/clang/lib/Frontend/TextDiagnostic.cpp +++ b/clang/lib/Frontend/TextDiagnostic.cpp @@ -774,6 +774,12 @@ void TextDiagnostic::emitFilename(StringRef Filename, const SourceManager &SM) { #else CacheEntry = SM.getFileManager().getCanonicalName(*File); #endif + + // In some cases, the resolved path may actually end up being longer (e.g. + // if it was originally a relative path), so just retain whichever one + // ends up being shorter. + if (!DiagOpts.AbsolutePath && CacheEntry.size() > Filename.size()) + CacheEntry = Filename; } Filename = CacheEntry; } _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits