Skip to content

Commit

Permalink
Use windows baskslash on anonymous tag locations if using MSVCFormatt…
Browse files Browse the repository at this point in the history
…ing 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
  • Loading branch information
ZequanWu committed May 19, 2023
1 parent cb16b33 commit 7599381
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
15 changes: 11 additions & 4 deletions clang/lib/AST/TypePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
2 changes: 2 additions & 0 deletions clang/test/CodeGen/Inputs/debug-info-slash.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include "Inputs/debug-info-slash.h"
int main() { a(); return 0; }
6 changes: 6 additions & 0 deletions clang/test/CodeGen/Inputs/debug-info-slash.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
template <typename... T>
void f1() {}
void a() {
auto Lambda = [] {};
f1<decltype(Lambda)>();
}
10 changes: 10 additions & 0 deletions clang/test/CodeGen/debug-info-slash.test
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7599381

Please sign in to comment.