From 75993812d5c1f269b781c34987748f2a792a579d Mon Sep 17 00:00:00 2001 From: Zequan Wu Date: Wed, 17 May 2023 17:06:42 -0400 Subject: [PATCH] Use windows baskslash on anonymous tag locations if using MSVCFormatting and it's not absolute path. This fixes a nondeterminism on debug info when building on windows natively vs cross building to windows. [1] https://github.com/llvm/llvm-project/blob/llvmorg-17-init/clang/lib/Lex/HeaderSearch.cpp#L465 Differential Revision: https://reviews.llvm.org/D150817 --- clang/lib/AST/TypePrinter.cpp | 15 +++++++++++---- clang/test/CodeGen/Inputs/debug-info-slash.cpp | 2 ++ clang/test/CodeGen/Inputs/debug-info-slash.h | 6 ++++++ clang/test/CodeGen/debug-info-slash.test | 10 ++++++++++ 4 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 clang/test/CodeGen/Inputs/debug-info-slash.cpp create mode 100644 clang/test/CodeGen/Inputs/debug-info-slash.h create mode 100644 clang/test/CodeGen/debug-info-slash.test diff --git a/clang/lib/AST/TypePrinter.cpp b/clang/lib/AST/TypePrinter.cpp index 02e0793..42bbbde 100644 --- a/clang/lib/AST/TypePrinter.cpp +++ b/clang/lib/AST/TypePrinter.cpp @@ -1385,11 +1385,18 @@ void TypePrinter::printTag(TagDecl *D, raw_ostream &OS) { if (PLoc.isValid()) { OS << " at "; StringRef File = PLoc.getFilename(); + llvm::SmallString<1024> WrittenFile(File); if (auto *Callbacks = Policy.Callbacks) - OS << Callbacks->remapPath(File); - else - OS << File; - OS << ':' << PLoc.getLine() << ':' << PLoc.getColumn(); + WrittenFile = Callbacks->remapPath(File); + // Fix inconsistent path separator created by + // clang::DirectoryLookup::LookupFile when the file path is relative + // path. + llvm::sys::path::Style Style = + !llvm::sys::path::is_absolute(WrittenFile) && Policy.MSVCFormatting + ? llvm::sys::path::Style::windows_backslash + : llvm::sys::path::Style::native; + llvm::sys::path::native(WrittenFile, Style); + OS << WrittenFile << ':' << PLoc.getLine() << ':' << PLoc.getColumn(); } } diff --git a/clang/test/CodeGen/Inputs/debug-info-slash.cpp b/clang/test/CodeGen/Inputs/debug-info-slash.cpp new file mode 100644 index 0000000..563077e --- /dev/null +++ b/clang/test/CodeGen/Inputs/debug-info-slash.cpp @@ -0,0 +1,2 @@ +#include "Inputs/debug-info-slash.h" +int main() { a(); return 0; } diff --git a/clang/test/CodeGen/Inputs/debug-info-slash.h b/clang/test/CodeGen/Inputs/debug-info-slash.h new file mode 100644 index 0000000..9092f4a --- /dev/null +++ b/clang/test/CodeGen/Inputs/debug-info-slash.h @@ -0,0 +1,6 @@ +template +void f1() {} +void a() { + auto Lambda = [] {}; + f1(); +} diff --git a/clang/test/CodeGen/debug-info-slash.test b/clang/test/CodeGen/debug-info-slash.test new file mode 100644 index 0000000..0e42912 --- /dev/null +++ b/clang/test/CodeGen/debug-info-slash.test @@ -0,0 +1,10 @@ +RUN: rm -rf %t-dir +RUN: mkdir -p %t-dir/header/Inputs +RUN: cp %S/Inputs/debug-info-slash.cpp %t-dir/ +RUN: cp %S/Inputs/debug-info-slash.h %t-dir/header/Inputs +RUN: cd %t-dir +RUN: %clang -target x86_64-pc-win32 -emit-llvm -S -g %t-dir/debug-info-slash.cpp -Iheader -o - | FileCheck --check-prefix=WIN %s +RUN: %clang -target x86_64-linux-gnu -emit-llvm -S -g %t-dir/debug-info-slash.cpp -Iheader -o - | FileCheck --check-prefix=LINUX %s + +WIN: lambda at header\\Inputs\\debug-info-slash.h +LINUX: lambda at header/Inputs/debug-info-slash.h -- 2.7.4