- 
                Notifications
    
You must be signed in to change notification settings  - Fork 15.1k
 
          [clang] Fix behavior of -ibuiltininc when passed alongside -nostdinc
          #165814
        
          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
          
     Open
      
      
            smallp-o-p
  wants to merge
  2
  commits into
  llvm:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
smallp-o-p:fix-ibuiltininc-nostdinc
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          Conversation
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
    | 
          
 @llvm/pr-subscribers-clang Author: William Tran-Viet (smallp-o-p) ChangesResolves #165790 
 Full diff: https://github.com/llvm/llvm-project/pull/165814.diff 3 Files Affected: 
 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
 | 
    
| 
          
 @llvm/pr-subscribers-clang-driver Author: William Tran-Viet (smallp-o-p) ChangesResolves #165790 
 Full diff: https://github.com/llvm/llvm-project/pull/165814.diff 3 Files Affected: 
 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
 | 
    
-ibuiltininc when passed alongside -nostdinc
      
  
    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 
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Resolves #165790
-nobuiltinincwhen-ibuiltinincis passed with-nostdinc