[clang] [clang] Don't issue permission error during header search (PR #194015)

Steven Wu via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 24 10:53:42 PDT 2026


https://github.com/cachemeifyoucan created https://github.com/llvm/llvm-project/pull/194015

When searching for headers, a permission error is currently treated
differently from a no such file or directory error. The permission error
will trigger a `cannot open file` error and stop the search process.

Even more confusingly, if the search happens inside `__has_include`,
the cannot open file error is issued on the `__has_include` statement,
which should not try to open file.

Instead, just treat a permission_denied error as a regular file error
and resume the search process. This will also make __has_include check
to return false if the only file can be found has no permission to read.

rdar://175435878


>From a3941b89050c5d134a5eba1e63b0c65e0077c2fd Mon Sep 17 00:00:00 2001
From: Steven Wu <stevenwu at apple.com>
Date: Fri, 24 Apr 2026 10:53:29 -0700
Subject: [PATCH] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20initia?=
 =?UTF-8?q?l=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Created using spr 1.3.7
---
 clang/lib/Lex/HeaderSearch.cpp |  1 +
 clang/test/Lexer/search-perm.c | 11 +++++++++++
 2 files changed, 12 insertions(+)
 create mode 100644 clang/test/Lexer/search-perm.c

diff --git a/clang/lib/Lex/HeaderSearch.cpp b/clang/lib/Lex/HeaderSearch.cpp
index 5cc2c04a68077..9cedebba14901 100644
--- a/clang/lib/Lex/HeaderSearch.cpp
+++ b/clang/lib/Lex/HeaderSearch.cpp
@@ -460,6 +460,7 @@ OptionalFileEntryRef HeaderSearch::getFileAndSuggestModule(
     std::error_code EC = llvm::errorToErrorCode(File.takeError());
     if (EC != llvm::errc::no_such_file_or_directory &&
         EC != llvm::errc::invalid_argument &&
+        EC != llvm::errc::permission_denied &&
         EC != llvm::errc::is_a_directory && EC != llvm::errc::not_a_directory) {
       Diags.Report(IncludeLoc, diag::err_cannot_open_file)
           << FileName << EC.message();
diff --git a/clang/test/Lexer/search-perm.c b/clang/test/Lexer/search-perm.c
new file mode 100644
index 0000000000000..ff0e9f055d120
--- /dev/null
+++ b/clang/test/Lexer/search-perm.c
@@ -0,0 +1,11 @@
+// RUN: rm -rf %t && split-file %s %t
+// RUN: chmod -r %t/no-perm/header.h
+// RUN: %clang_cc1 -fsyntax-only -I %t/no-perm -I %t/include %t/tu.c
+
+//--- no-perm/header.h
+#error "no permission"
+//--- include/header.h
+//--- tu.c
+#if __has_include("header.h")
+#include "header.h"
+#endif



More information about the cfe-commits mailing list