[PATCH] D24091: [Driver] Warn on -nodefaultlibs and -fsanitize

Chris Bieneman via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 31 15:12:53 PDT 2016


beanz updated this revision to Diff 69910.
beanz added a comment.

Updates based on bruno's feedback.


https://reviews.llvm.org/D24091

Files:
  include/clang/Basic/DiagnosticDriverKinds.td
  include/clang/Driver/SanitizerArgs.h
  lib/Driver/Driver.cpp
  lib/Driver/ToolChains.cpp
  lib/Driver/Tools.cpp
  test/Driver/nodefaultlib.c

Index: test/Driver/nodefaultlib.c
===================================================================
--- test/Driver/nodefaultlib.c
+++ test/Driver/nodefaultlib.c
@@ -8,3 +8,12 @@
 // RUN: %clang -target i686-pc-linux-gnu -stdlib=libc++ -nodefaultlibs -lstdc++ -### %s 2>&1 | FileCheck -check-prefix=TEST2 %s
 // TEST2-NOT: "-lc++"
 // TEST2: "-lstdc++"
+
+// RUN: %clang -target i686-pc-linux-gnu -nodefaultlibs -fsanitize=address -### %s 2>&1 | FileCheck -check-prefix=TEST3 %s
+// RUN: %clang -target i686-unknown-freebsd -nodefaultlibs -fsanitize=address -### %s 2>&1 | FileCheck -check-prefix=TEST3 %s
+// TEST3: warning: Passing -nodefaultlibs and -fsanitize has resulted in linking sanitizer runtimes.
+// TEST3: libclang_rt.asan-i686.a
+
+// RUN: %clang -target i686-apple-darwin -nodefaultlibs -fsanitize=address -### %s 2>&1 | FileCheck -check-prefix=TEST4 %s
+// TEST4: warning: Passing -nodefaultlibs and -fsanitize has resulted in linking sanitizer runtimes.
+// TEST4: libclang_rt.asan_osx_dynamic.dylib
Index: lib/Driver/Tools.cpp
===================================================================
--- lib/Driver/Tools.cpp
+++ lib/Driver/Tools.cpp
@@ -8768,6 +8768,9 @@
     AddGoldPlugin(ToolChain, Args, CmdArgs, D.getLTOMode() == LTOK_Thin);
 
   bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
+
+  if (NeedsSanitizerDeps && Args.hasArg(options::OPT_nodefaultlibs))
+    D.Diag(clang::diag::warn_drv_sanitizers_and_nodefaultlibs);
   AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
 
   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
@@ -9604,6 +9607,10 @@
     CmdArgs.push_back("--no-demangle");
 
   bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
+  
+  if (NeedsSanitizerDeps && Args.hasArg(options::OPT_nodefaultlibs))
+    D.Diag(clang::diag::warn_drv_sanitizers_and_nodefaultlibs);
+
   bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs);
   AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
   // The profile runtime also needs access to system libraries.
Index: lib/Driver/ToolChains.cpp
===================================================================
--- lib/Driver/ToolChains.cpp
+++ lib/Driver/ToolChains.cpp
@@ -321,6 +321,8 @@
     CmdArgs.push_back("-rpath");
     CmdArgs.push_back(Args.MakeArgString(Dir));
   }
+  if (Args.hasArg(options::OPT_nodefaultlibs))
+    getDriver().Diag(clang::diag::warn_drv_sanitizers_and_nodefaultlibs);
 }
 
 StringRef Darwin::getPlatformFamily() const {
Index: lib/Driver/Driver.cpp
===================================================================
--- lib/Driver/Driver.cpp
+++ lib/Driver/Driver.cpp
@@ -1546,9 +1546,8 @@
   Arg *FinalPhaseArg;
   phases::ID FinalPhase = getFinalPhase(Args, &FinalPhaseArg);
 
-  if (FinalPhase == phases::Link && Args.hasArg(options::OPT_emit_llvm)) {
+  if (FinalPhase == phases::Link && Args.hasArg(options::OPT_emit_llvm))
     Diag(clang::diag::err_drv_emit_llvm_link);
-  }
 
   // Reject -Z* at the top level, these options should never have been exposed
   // by gcc.
Index: include/clang/Driver/SanitizerArgs.h
===================================================================
--- include/clang/Driver/SanitizerArgs.h
+++ include/clang/Driver/SanitizerArgs.h
@@ -63,6 +63,8 @@
     return Sanitizers.hasOneOf(SanitizerKind::Efficiency);
   }
 
+  bool hasSanitizers() const { return !Sanitizers.empty(); }
+
   bool requiresPIE() const;
   bool needsUnwindTables() const;
   bool linkCXXRuntimes() const { return LinkCXXRuntimes; }
Index: include/clang/Basic/DiagnosticDriverKinds.td
===================================================================
--- include/clang/Basic/DiagnosticDriverKinds.td
+++ include/clang/Basic/DiagnosticDriverKinds.td
@@ -271,4 +271,8 @@
   InGroup<InvalidOrNonExistentDirectory>;
 
 def err_drv_unsupported_linker : Error<"unsupported value '%0' for -linker option">;
+
+def warn_drv_sanitizers_and_nodefaultlibs : Warning<
+  "Passing -nodefaultlibs and -fsanitize has resulted in linking sanitizer runtimes.">,
+  InGroup<DiagGroup<"unexpected-driver-behavior">>;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D24091.69910.patch
Type: text/x-patch
Size: 4117 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160831/3cbcdb46/attachment-0001.bin>


More information about the cfe-commits mailing list