[llvm] [Support] Use absolute paths for include filenames (PR #136687)

via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 22 04:29:39 PDT 2025


https://github.com/tcwzxx created https://github.com/llvm/llvm-project/pull/136687

Since the LSP file URL requires an absolute path, ensure that all include files use absolute paths

>From 3741cb3136eda2a677c64e17869f370c207811a6 Mon Sep 17 00:00:00 2001
From: tcwzxx <tcwzxx at gmail.com>
Date: Tue, 22 Apr 2025 19:09:19 +0800
Subject: [PATCH] make include filename as absolute path

---
 llvm/lib/Support/SourceMgr.cpp | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Support/SourceMgr.cpp b/llvm/lib/Support/SourceMgr.cpp
index 3f97213d86c05..1a6389737c1fb 100644
--- a/llvm/lib/Support/SourceMgr.cpp
+++ b/llvm/lib/Support/SourceMgr.cpp
@@ -20,6 +20,7 @@
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/Twine.h"
 #include "llvm/Support/ErrorOr.h"
+#include "llvm/Support/FileSystem.h"
 #include "llvm/Support/Locale.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/Path.h"
@@ -52,15 +53,18 @@ unsigned SourceMgr::AddIncludeFile(const std::string &Filename,
 ErrorOr<std::unique_ptr<MemoryBuffer>>
 SourceMgr::OpenIncludeFile(const std::string &Filename,
                            std::string &IncludedFile) {
+  SmallString<128> Path{Filename};
+  sys::fs::make_absolute(Path);
   ErrorOr<std::unique_ptr<MemoryBuffer>> NewBufOrErr =
-      MemoryBuffer::getFile(Filename);
+      MemoryBuffer::getFile(Path);
 
-  SmallString<64> Buffer(Filename);
+  SmallString<64> Buffer(Path);
   // If the file didn't exist directly, see if it's in an include path.
   for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBufOrErr;
        ++i) {
     Buffer = IncludeDirectories[i];
     sys::path::append(Buffer, Filename);
+    sys::fs::make_absolute(Buffer);
     NewBufOrErr = MemoryBuffer::getFile(Buffer);
   }
 



More information about the llvm-commits mailing list