[clang] 9621b17 - [clang] Don't include C++ Standard Library headers when -nostdinc is used

Louis Dionne via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 16 12:25:50 PST 2022


Author: Louis Dionne
Date: 2022-11-16T15:25:32-05:00
New Revision: 9621b1776a1b3d16735b3c5da31585ad64a018df

URL: https://github.com/llvm/llvm-project/commit/9621b1776a1b3d16735b3c5da31585ad64a018df
DIFF: https://github.com/llvm/llvm-project/commit/9621b1776a1b3d16735b3c5da31585ad64a018df.diff

LOG: [clang] Don't include C++ Standard Library headers when -nostdinc is used

This is a follow-up to 53c98d85a, which made the same change but only
for GNU. It seems that we should try to provide a consistent behavior
across all targets.

This fixes an issue where clang/test/Driver/nostdincxx.cpp would start
failing on non-GNU targets because that test was too loose in its checks.
It would only check that 'file not found' was part of the error message,
but didn't ensure that the file we had not found was <vector>.

Differential Revision: https://reviews.llvm.org/D138062

Added: 
    

Modified: 
    clang/lib/Driver/ToolChains/Darwin.cpp
    clang/lib/Driver/ToolChains/Fuchsia.cpp
    clang/lib/Driver/ToolChains/MinGW.cpp
    clang/lib/Driver/ToolChains/WebAssembly.cpp
    clang/test/Driver/darwin-header-search-libcxx.cpp
    clang/test/Driver/nostdincxx.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp
index 333d99842e234..c3a7627460ae4 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -2388,8 +2388,8 @@ void DarwinClang::AddClangCXXStdlibIncludeArgs(
   //        Also check whether this is used for setting library search paths.
   ToolChain::AddClangCXXStdlibIncludeArgs(DriverArgs, CC1Args);
 
-  if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
-      DriverArgs.hasArg(options::OPT_nostdincxx))
+  if (DriverArgs.hasArg(options::OPT_nostdinc, options::OPT_nostdlibinc,
+                        options::OPT_nostdincxx))
     return;
 
   llvm::SmallString<128> Sysroot = GetEffectiveSysroot(DriverArgs);

diff  --git a/clang/lib/Driver/ToolChains/Fuchsia.cpp b/clang/lib/Driver/ToolChains/Fuchsia.cpp
index 6f4fa2ce7c40a..9bdc3a7917791 100644
--- a/clang/lib/Driver/ToolChains/Fuchsia.cpp
+++ b/clang/lib/Driver/ToolChains/Fuchsia.cpp
@@ -377,8 +377,8 @@ void Fuchsia::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
 
 void Fuchsia::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
                                            ArgStringList &CC1Args) const {
-  if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
-      DriverArgs.hasArg(options::OPT_nostdincxx))
+  if (DriverArgs.hasArg(options::OPT_nostdinc, options::OPT_nostdlibinc,
+                        options::OPT_nostdincxx))
     return;
 
   const Driver &D = getDriver();

diff  --git a/clang/lib/Driver/ToolChains/MinGW.cpp b/clang/lib/Driver/ToolChains/MinGW.cpp
index 7ba46cba1da4b..4c392d96befde 100644
--- a/clang/lib/Driver/ToolChains/MinGW.cpp
+++ b/clang/lib/Driver/ToolChains/MinGW.cpp
@@ -644,8 +644,8 @@ void toolchains::MinGW::addClangTargetOptions(
 
 void toolchains::MinGW::AddClangCXXStdlibIncludeArgs(
     const ArgList &DriverArgs, ArgStringList &CC1Args) const {
-  if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
-      DriverArgs.hasArg(options::OPT_nostdincxx))
+  if (DriverArgs.hasArg(options::OPT_nostdinc, options::OPT_nostdlibinc,
+                        options::OPT_nostdincxx))
     return;
 
   StringRef Slash = llvm::sys::path::get_separator();

diff  --git a/clang/lib/Driver/ToolChains/WebAssembly.cpp b/clang/lib/Driver/ToolChains/WebAssembly.cpp
index b051bff875127..a1c4cd9ef9c79 100644
--- a/clang/lib/Driver/ToolChains/WebAssembly.cpp
+++ b/clang/lib/Driver/ToolChains/WebAssembly.cpp
@@ -424,8 +424,8 @@ void WebAssembly::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
 void WebAssembly::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
                                                ArgStringList &CC1Args) const {
 
-  if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
-      DriverArgs.hasArg(options::OPT_nostdincxx))
+  if (DriverArgs.hasArg(options::OPT_nostdlibinc, options::OPT_nostdinc,
+                        options::OPT_nostdincxx))
     return;
 
   switch (GetCXXStdlibType(DriverArgs)) {

diff  --git a/clang/test/Driver/darwin-header-search-libcxx.cpp b/clang/test/Driver/darwin-header-search-libcxx.cpp
index f1b6f39d84838..32474495bbb5b 100644
--- a/clang/test/Driver/darwin-header-search-libcxx.cpp
+++ b/clang/test/Driver/darwin-header-search-libcxx.cpp
@@ -92,39 +92,22 @@
 // CHECK-LIBCXX-SYSROOT_AND_TOOLCHAIN-1: "-internal-isystem" "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
 // CHECK-LIBCXX-SYSROOT_AND_TOOLCHAIN-1-NOT: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"
 
-// Make sure that using -nostdinc does not drop any C++ library include path.
-// This behavior is strange, but it is compatible with the legacy CC1 behavior.
-//
-// RUN: %clang -### %s -fsyntax-only 2>&1 \
-// RUN:     --target=x86_64-apple-darwin16 \
-// RUN:     -ccc-install-dir %S/Inputs/basic_darwin_toolchain/usr/bin \
-// RUN:     -resource-dir=%S/Inputs/resource_dir \
-// RUN:     -isysroot %S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
-// RUN:     -stdlib=platform \
-// RUN:     -nostdinc \
-// RUN:   | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
-// RUN:               -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain \
-// RUN:               --check-prefix=CHECK-LIBCXX-NOSTDINC-1 %s
-// CHECK-LIBCXX-NOSTDINC-1: "-cc1"
-// CHECK-LIBCXX-NOSTDINC-1-NOT: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"
-// CHECK-LIBCXX-NOSTDINC-1: "-internal-isystem" "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
+// Make sure that using -nostdinc, -nostdinc++ or -nostdlib will drop both the toolchain
+// C++ include path and the sysroot one.
 //
 // RUN: %clang -### %s -fsyntax-only 2>&1 \
 // RUN:     --target=x86_64-apple-darwin16 \
 // RUN:     -ccc-install-dir %S/Inputs/basic_darwin_toolchain/usr/bin \
 // RUN:     -resource-dir=%S/Inputs/resource_dir \
-// RUN:     -isysroot %S/Inputs/basic_darwin_sdk_no_libcxx \
+// RUN:     -isysroot %S/Inputs/basic_darwin_sdk_usr \
 // RUN:     -stdlib=platform \
 // RUN:     -nostdinc \
-// RUN:   | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_no_libcxx \
+// RUN:   | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr \
 // RUN:               -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain \
-// RUN:               --check-prefix=CHECK-LIBCXX-NOSTDINC-2 %s
-// CHECK-LIBCXX-NOSTDINC-2: "-cc1"
-// CHECK-LIBCXX-NOSTDINC-2: "-internal-isystem" "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
-// CHECK-LIBCXX-NOSTDINC-2-NOT: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"
-
-// Make sure that using -nostdinc++ or -nostdlib will drop both the toolchain
-// C++ include path and the sysroot one.
+// RUN:               --check-prefix=CHECK-LIBCXX-NOSTDLIBINC %s
+// CHECK-LIBCXX-NOSTDINC: "-cc1"
+// CHECK-LIBCXX-NOSTDINC-NOT: "-internal-isystem" "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
+// CHECK-LIBCXX-NOSTDINC-NOT: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"
 //
 // RUN: %clang -### %s -fsyntax-only 2>&1 \
 // RUN:     --target=x86_64-apple-darwin16 \

diff  --git a/clang/test/Driver/nostdincxx.cpp b/clang/test/Driver/nostdincxx.cpp
index 0d0204fb7366e..e27bee8dc6133 100644
--- a/clang/test/Driver/nostdincxx.cpp
+++ b/clang/test/Driver/nostdincxx.cpp
@@ -2,8 +2,8 @@
 // RUN: not %clangxx -nostdinc++ %s 2>&1 | FileCheck %s
 // RUN: not %clangxx -nostdlibinc %s 2>&1 | FileCheck %s
 // RUN: not %clangxx --target=x86_64-unknown-unknown-gnu -fsyntax-only -nostdinc -nostdinc++ %s 2>&1 | FileCheck /dev/null --implicit-check-not=-Wunused-command-line-argument
-// CHECK: file not found
-#include <vector> 
+// CHECK: 'vector' file not found
+#include <vector>
 
 // MSVC, PS4, PS5 have C++ headers in the same directory as C headers.
 // UNSUPPORTED: ms-sdk, ps4, ps5


        


More information about the cfe-commits mailing list