Skip to content

Conversation

@smallp-o-p
Copy link
Contributor

Resolves #165790

  • Don't add -nobuiltininc when -ibuiltininc is passed with -nostdinc
  • Add tests

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels Oct 31, 2025
@smallp-o-p smallp-o-p changed the title Fix-ibuiltininc-nostdinc [clang] Fix-ibuiltininc-nostdinc Oct 31, 2025
@llvmbot
Copy link
Member

llvmbot commented Oct 31, 2025

@llvm/pr-subscribers-clang

Author: William Tran-Viet (smallp-o-p)

Changes

Resolves #165790

  • Don't add -nobuiltininc when -ibuiltininc is passed with -nostdinc
  • Add tests

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

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+1)
  • (modified) clang/lib/Driver/ToolChains/Clang.cpp (+3-1)
  • (added) clang/test/Driver/nostdinc-ibuiltininc.c (+10)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index e6e33e7a9a280..7e0e6ca5f564b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -505,6 +505,7 @@ Bug Fixes to AST Handling
 Miscellaneous Bug Fixes
 ^^^^^^^^^^^^^^^^^^^^^^^
 - Fixed missing diagnostics of ``diagnose_if`` on templates involved in initialization. (#GH160776)
+- Fix `-ibuiltininc` compiler flag being ignored if `-nostdinc` is specified. (#GH165790)
 
 Miscellaneous Clang Crashes Fixed
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 79edc561c551f..f9a894205771a 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -6172,7 +6172,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
   // Pass options for controlling the default header search paths.
   if (Args.hasArg(options::OPT_nostdinc)) {
     CmdArgs.push_back("-nostdsysteminc");
-    CmdArgs.push_back("-nobuiltininc");
+    if (!Args.hasArg(options::OPT_ibuiltininc)) {
+      CmdArgs.push_back("-nobuiltininc");
+    }
   } else {
     if (Args.hasArg(options::OPT_nostdlibinc))
       CmdArgs.push_back("-nostdsysteminc");
diff --git a/clang/test/Driver/nostdinc-ibuiltininc.c b/clang/test/Driver/nostdinc-ibuiltininc.c
new file mode 100644
index 0000000000000..d4bc885264c45
--- /dev/null
+++ b/clang/test/Driver/nostdinc-ibuiltininc.c
@@ -0,0 +1,10 @@
+// RUN: %clang -target x86_64-unknown-unknown -nostdinc -ibuiltininc -ffreestanding -fsyntax-only %s
+// RUN: %clang -target x86_64-unknown-unknown -ibuiltininc -nostdinc -ffreestanding -fsyntax-only %s
+
+#if !__has_include("stddef.h")
+#error "expected to be able to find compiler builtin headers!"
+#endif
+
+#if __has_include("stdlib.h")
+#error "expected to *not* be able to find standard C headers"
+#endif
\ No newline at end of file

@llvmbot
Copy link
Member

llvmbot commented Oct 31, 2025

@llvm/pr-subscribers-clang-driver

Author: William Tran-Viet (smallp-o-p)

Changes

Resolves #165790

  • Don't add -nobuiltininc when -ibuiltininc is passed with -nostdinc
  • Add tests

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

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+1)
  • (modified) clang/lib/Driver/ToolChains/Clang.cpp (+3-1)
  • (added) clang/test/Driver/nostdinc-ibuiltininc.c (+10)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index e6e33e7a9a280..7e0e6ca5f564b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -505,6 +505,7 @@ Bug Fixes to AST Handling
 Miscellaneous Bug Fixes
 ^^^^^^^^^^^^^^^^^^^^^^^
 - Fixed missing diagnostics of ``diagnose_if`` on templates involved in initialization. (#GH160776)
+- Fix `-ibuiltininc` compiler flag being ignored if `-nostdinc` is specified. (#GH165790)
 
 Miscellaneous Clang Crashes Fixed
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 79edc561c551f..f9a894205771a 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -6172,7 +6172,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
   // Pass options for controlling the default header search paths.
   if (Args.hasArg(options::OPT_nostdinc)) {
     CmdArgs.push_back("-nostdsysteminc");
-    CmdArgs.push_back("-nobuiltininc");
+    if (!Args.hasArg(options::OPT_ibuiltininc)) {
+      CmdArgs.push_back("-nobuiltininc");
+    }
   } else {
     if (Args.hasArg(options::OPT_nostdlibinc))
       CmdArgs.push_back("-nostdsysteminc");
diff --git a/clang/test/Driver/nostdinc-ibuiltininc.c b/clang/test/Driver/nostdinc-ibuiltininc.c
new file mode 100644
index 0000000000000..d4bc885264c45
--- /dev/null
+++ b/clang/test/Driver/nostdinc-ibuiltininc.c
@@ -0,0 +1,10 @@
+// RUN: %clang -target x86_64-unknown-unknown -nostdinc -ibuiltininc -ffreestanding -fsyntax-only %s
+// RUN: %clang -target x86_64-unknown-unknown -ibuiltininc -nostdinc -ffreestanding -fsyntax-only %s
+
+#if !__has_include("stddef.h")
+#error "expected to be able to find compiler builtin headers!"
+#endif
+
+#if __has_include("stdlib.h")
+#error "expected to *not* be able to find standard C headers"
+#endif
\ No newline at end of file

@smallp-o-p smallp-o-p changed the title [clang] Fix-ibuiltininc-nostdinc [clang] Fix behavior of -ibuiltininc when passed alongside -nostdinc Oct 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[clang] -ibuiltininc does not re-add builtin include paths if -nostdinc is specified

2 participants