Skip to content

[clang] Canonicalize absolute paths in dependency file #117458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/include/clang/Frontend/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class DependencyFileGenerator : public DependencyCollector {
private:
void outputDependencyFile(DiagnosticsEngine &Diags);

llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;
std::string OutputFile;
std::vector<std::string> Targets;
bool IncludeSystemHeaders;
Expand Down
22 changes: 18 additions & 4 deletions clang/lib/Frontend/DependencyFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
//
//===----------------------------------------------------------------------===//

#include "clang/Frontend/Utils.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Frontend/DependencyOutputOptions.h"
#include "clang/Frontend/FrontendDiagnostic.h"
#include "clang/Frontend/Utils.h"
#include "clang/Lex/DirectoryLookup.h"
#include "clang/Lex/ModuleMap.h"
#include "clang/Lex/PPCallbacks.h"
Expand All @@ -23,8 +23,10 @@
#include "llvm/ADT/StringSet.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/Support/raw_ostream.h"
#include <optional>
#include <system_error>

using namespace clang;

Expand Down Expand Up @@ -236,6 +238,7 @@ void DependencyFileGenerator::attachToPreprocessor(Preprocessor &PP) {
PP.SetSuppressIncludeNotFoundError(true);

DependencyCollector::attachToPreprocessor(PP);
FS = PP.getFileManager().getVirtualFileSystemPtr();
}

bool DependencyFileGenerator::sawDependency(StringRef Filename, bool FromModule,
Expand Down Expand Up @@ -312,11 +315,22 @@ void DependencyFileGenerator::finishedMainFile(DiagnosticsEngine &Diags) {
/// https://msdn.microsoft.com/en-us/library/dd9y37ha.aspx for NMake info,
/// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
/// for Windows file-naming info.
static void PrintFilename(raw_ostream &OS, StringRef Filename,
static void printFilename(raw_ostream &OS, llvm::vfs::FileSystem *FS,
StringRef Filename,
DependencyOutputFormat OutputFormat) {
// Convert filename to platform native path
llvm::SmallString<256> NativePath;
llvm::sys::path::native(Filename.str(), NativePath);
// Resolve absolute path. Make and Ninja canonicalize paths
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL the Make behavior :)

@madscientist

// without checking for symbolic links in the path, for performance concerns.
// If there is something like `/bin/../lib64` -> `/usr/lib64`
// (where `/bin` links to `/usr/bin`), Make will see them as `/lib64`.
if (FS != nullptr && llvm::sys::path::is_absolute(NativePath)) {
llvm::SmallString<256> NativePathTmp = NativePath;
std::error_code EC = FS->getRealPath(NativePathTmp, NativePath);
if (EC)
NativePath = NativePathTmp;
}

if (OutputFormat == DependencyOutputFormat::NMake) {
// Add quotes if needed. These are the characters listed as "special" to
Expand Down Expand Up @@ -400,7 +414,7 @@ void DependencyFileGenerator::outputDependencyFile(llvm::raw_ostream &OS) {
Columns = 2;
}
OS << ' ';
PrintFilename(OS, File, OutputFormat);
printFilename(OS, FS.get(), File, OutputFormat);
Columns += N + 1;
}
OS << '\n';
Expand All @@ -411,7 +425,7 @@ void DependencyFileGenerator::outputDependencyFile(llvm::raw_ostream &OS) {
for (auto I = Files.begin(), E = Files.end(); I != E; ++I) {
if (Index++ == InputFileIndex)
continue;
PrintFilename(OS, *I, OutputFormat);
printFilename(OS, FS.get(), *I, OutputFormat);
OS << ":\n";
}
}
Expand Down
2 changes: 1 addition & 1 deletion clang/test/Frontend/dependency-gen-symlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// CHECK: dependency-gen-symlink.c.o
// CHECK: dependency-gen-symlink.c
// CHECK: a/header.h
// CHECK: b/header.h
// CHECK-NOT: b/header.h
// CHECK-NOT: with-header-guard.h
#include "a/header.h"
#include "b/header.h"
Expand Down
2 changes: 1 addition & 1 deletion clang/test/Frontend/dependency-gen-windows-duplicates.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// RUN: %clang -MD -MF - %t.dir/test.c -fsyntax-only -I %t.dir/subdir | FileCheck %s
// CHECK: test.o:
// CHECK-NEXT: \test.c
// CHECK-NEXT: \SubDir\X.h
// CHECK-NEXT: \subdir\x.h
// File x.h must appear only once (case insensitive check).
// CHECK-NOT: {{\\|/}}{{x|X}}.{{h|H}}

Expand Down
2 changes: 1 addition & 1 deletion clang/test/VFS/external-names.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@

// RUN: %clang_cc1 -D REINCLUDE -I %t -ivfsoverlay %t.yaml -Eonly %s -MTfoo -dependency-file %t.dep
// RUN: cat %t.dep | FileCheck --check-prefix=CHECK-DEP %s
// CHECK-DEP-NOT: Inputs
// CHECK-DEP: Inputs{{..?}}external-names.h
Loading