llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-driver

Author: Steven Wu (cachemeifyoucan)

<details>
<summary>Changes</summary>

Add a query to vfs::Status that returns whether any of the execute
permissions is set, so that clients checking whether a file found
through a VFS is executable don't have to test the permission bits by
hand. Update MSVC toolchain to use the new API.

Unlike sys::fs::can_execute, it does not check whether the current
process is allowed to execute the file.


---
Full diff: https://github.com/llvm/llvm-project/pull/244339.diff


4 Files Affected:

- (modified) clang/lib/Driver/ToolChains/MSVC.cpp (+1-3) 
- (modified) llvm/include/llvm/Support/VirtualFileSystem.h (+5) 
- (modified) llvm/lib/Support/VirtualFileSystem.cpp (+4) 
- (modified) llvm/unittests/Support/VirtualFileSystemTest.cpp (+27) 


``````````diff
diff --git a/clang/lib/Driver/ToolChains/MSVC.cpp 
b/clang/lib/Driver/ToolChains/MSVC.cpp
index 8f4c8f89ce58f..0bf7e8874edfd 100644
--- a/clang/lib/Driver/ToolChains/MSVC.cpp
+++ b/clang/lib/Driver/ToolChains/MSVC.cpp
@@ -44,9 +44,7 @@ using namespace llvm::opt;
 
 static bool canExecute(llvm::vfs::FileSystem &VFS, StringRef Path) {
   auto Status = VFS.status(Path);
-  if (!Status)
-    return false;
-  return (Status->getPermissions() & llvm::sys::fs::perms::all_exe) != 0;
+  return Status && Status->isExecutable();
 }
 
 // Try to find Exe from a Visual Studio distribution.  This first tries to find
diff --git a/llvm/include/llvm/Support/VirtualFileSystem.h 
b/llvm/include/llvm/Support/VirtualFileSystem.h
index c2a8496320440..1b5e0f3b19dba 100644
--- a/llvm/include/llvm/Support/VirtualFileSystem.h
+++ b/llvm/include/llvm/Support/VirtualFileSystem.h
@@ -109,6 +109,11 @@ class Status {
   LLVM_ABI bool isStatusKnown() const;
   LLVM_ABI bool exists() const;
   /// @}
+
+  /// Returns true if any of the owner, group, or others execute permissions is
+  /// set. Unlike llvm::sys::fs::can_execute, this does not check whether the
+  /// current process is allowed to execute the file.
+  LLVM_ABI bool isExecutable() const;
 };
 
 /// Represents an open file.
diff --git a/llvm/lib/Support/VirtualFileSystem.cpp 
b/llvm/lib/Support/VirtualFileSystem.cpp
index 38d1e876a7a23..50a657c9d0890 100644
--- a/llvm/lib/Support/VirtualFileSystem.cpp
+++ b/llvm/lib/Support/VirtualFileSystem.cpp
@@ -110,6 +110,10 @@ bool Status::exists() const {
   return isStatusKnown() && Type != file_type::file_not_found;
 }
 
+bool Status::isExecutable() const {
+  return (Perms & sys::fs::all_exe) != sys::fs::no_perms;
+}
+
 File::~File() = default;
 
 FileSystem::~FileSystem() = default;
diff --git a/llvm/unittests/Support/VirtualFileSystemTest.cpp 
b/llvm/unittests/Support/VirtualFileSystemTest.cpp
index 1db4894e95719..ac356d32c755c 100644
--- a/llvm/unittests/Support/VirtualFileSystemTest.cpp
+++ b/llvm/unittests/Support/VirtualFileSystemTest.cpp
@@ -273,6 +273,33 @@ TEST(VirtualFileSystemTest, StatusQueries) {
   EXPECT_FALSE(Status->equivalent(*Status2));
 }
 
+TEST(VirtualFileSystemTest, StatusIsExecutable) {
+  auto D = makeIntrusiveRefCnt<DummyFileSystem>();
+  auto IsExecutable = [&](StringRef Path) {
+    ErrorOr<vfs::Status> Status = D->status(Path);
+    EXPECT_FALSE(Status.getError());
+    return Status && Status->isExecutable();
+  };
+
+  D->addRegularFile("/all");
+  EXPECT_TRUE(IsExecutable("/all"));
+
+  D->addRegularFile("/owner", sys::fs::owner_exe);
+  EXPECT_TRUE(IsExecutable("/owner"));
+
+  D->addRegularFile("/group", sys::fs::group_exe);
+  EXPECT_TRUE(IsExecutable("/group"));
+
+  D->addRegularFile("/others", sys::fs::others_exe);
+  EXPECT_TRUE(IsExecutable("/others"));
+
+  D->addRegularFile("/none", sys::fs::all_read | sys::fs::all_write);
+  EXPECT_FALSE(IsExecutable("/none"));
+
+  D->addDirectory("/dir", sys::fs::all_read);
+  EXPECT_FALSE(IsExecutable("/dir"));
+}
+
 TEST(VirtualFileSystemTest, BaseOnlyOverlay) {
   auto D = makeIntrusiveRefCnt<DummyFileSystem>();
   ErrorOr<vfs::Status> Status((std::error_code()));

``````````

</details>


https://github.com/llvm/llvm-project/pull/244339
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to