[clang] 6026822 - bugfix in InfiniteLoopCheck to not print warnings for unevaluated loops

2022-05-23 Thread usama hameed via cfe-commits

Author: usama hameed
Date: 2022-05-23T20:18:48-07:00
New Revision: 602682225ad6c9135e84bbca3b91d5738712c64f

URL: 
https://github.com/llvm/llvm-project/commit/602682225ad6c9135e84bbca3b91d5738712c64f
DIFF: 
https://github.com/llvm/llvm-project/commit/602682225ad6c9135e84bbca3b91d5738712c64f.diff

LOG: bugfix in InfiniteLoopCheck to not print warnings for unevaluated loops

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

Added: 


Modified: 
clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
clang-tools-extra/test/clang-tidy/checkers/bugprone-infinite-loop.cpp
clang/include/clang/Analysis/Analyses/ExprMutationAnalyzer.h
clang/lib/Analysis/ExprMutationAnalyzer.cpp

Removed: 




diff  --git a/clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
index 3359a7f8932ed..cb9bd7bb43f50 100644
--- a/clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
@@ -81,6 +81,22 @@ static bool isVarThatIsPossiblyChanged(const Decl *Func, 
const Stmt *LoopStmt,
   return false;
 }
 
+bool isUnevaluated(const Decl *Func, const Stmt *LoopStmt, const Stmt *Cond,
+   ASTContext *Context) {
+  if (const auto *Exp = dyn_cast(Cond)) {
+if (const auto *ForLoop = dyn_cast(LoopStmt)) {
+  return (ForLoop->getInc() && ExprMutationAnalyzer::isUnevaluated(
+   Exp, *ForLoop->getInc(), *Context)) ||
+ (ForLoop->getBody() && ExprMutationAnalyzer::isUnevaluated(
+Exp, *ForLoop->getBody(), *Context)) ||
+ (ForLoop->getCond() && ExprMutationAnalyzer::isUnevaluated(
+Exp, *ForLoop->getCond(), *Context));
+}
+return ExprMutationAnalyzer::isUnevaluated(Exp, *LoopStmt, *Context);
+  }
+  return true;
+}
+
 /// Return whether at least one variable of `Cond` changed in `LoopStmt`.
 static bool isAtLeastOneCondVarChanged(const Decl *Func, const Stmt *LoopStmt,
const Stmt *Cond, ASTContext *Context) {
@@ -177,6 +193,9 @@ void InfiniteLoopCheck::check(const 
MatchFinder::MatchResult &Result) {
 }
   }
 
+  if (isUnevaluated(Func, LoopStmt, Cond, Result.Context))
+return;
+
   if (isAtLeastOneCondVarChanged(Func, LoopStmt, Cond, Result.Context))
 return;
 

diff  --git 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone-infinite-loop.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone-infinite-loop.cpp
index 0074266ce4b87..a22f1bf06a534 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone-infinite-loop.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone-infinite-loop.cpp
@@ -650,3 +650,38 @@ void test_dependent_condition() {
   do {
   } while (1, (false) && val4 == 1);
 }
+
+void test_typeof() {
+  __typeof__({
+for (int i = 0; i < 10; ++i) {
+}
+0;
+  }) x;
+}
+
+void test_typeof_infinite() {
+  __typeof__({
+for (int i = 0; i < 10;) {
+}
+0;
+  }) x;
+}
+
+void test_typeof_while_infinite() {
+  __typeof__({
+int i = 0;
+while (i < 10) {
+}
+0;
+  }) x;
+}
+
+void test_typeof_dowhile_infinite() {
+  __typeof__({
+int i = 0;
+do {
+
+} while (i < 10);
+0;
+  }) x;
+}

diff  --git a/clang/include/clang/Analysis/Analyses/ExprMutationAnalyzer.h 
b/clang/include/clang/Analysis/Analyses/ExprMutationAnalyzer.h
index 9397c5df78abe..1b2b7ffcfb67a 100644
--- a/clang/include/clang/Analysis/Analyses/ExprMutationAnalyzer.h
+++ b/clang/include/clang/Analysis/Analyses/ExprMutationAnalyzer.h
@@ -38,6 +38,8 @@ class ExprMutationAnalyzer {
   }
   const Stmt *findPointeeMutation(const Expr *Exp);
   const Stmt *findPointeeMutation(const Decl *Dec);
+  static bool isUnevaluated(const Expr *Exp, const Stmt &Stm,
+ASTContext &Context);
 
 private:
   using MutationFinder = const Stmt *(ExprMutationAnalyzer::*)(const Expr *);

diff  --git a/clang/lib/Analysis/ExprMutationAnalyzer.cpp 
b/clang/lib/Analysis/ExprMutationAnalyzer.cpp
index e9ff5e5e87658..4d7decc1137c2 100644
--- a/clang/lib/Analysis/ExprMutationAnalyzer.cpp
+++ b/clang/lib/Analysis/ExprMutationAnalyzer.cpp
@@ -194,7 +194,8 @@ const Stmt *ExprMutationAnalyzer::tryEachDeclRef(const Decl 
*Dec,
   return nullptr;
 }
 
-bool ExprMutationAnalyzer::isUnevaluated(const Expr *Exp) {
+bool ExprMutationAnalyzer::isUnevaluated(const Expr *Exp, const Stmt &Stm,
+ ASTContext &Context) {
   return selectFirst(
  NodeID::value,
  match(
@@ -225,6 +226,10 @@ bool ExprMutationAnalyzer::isUnevaluated(const Expr *Exp) {
  Stm, Context)) != nullptr;
 }
 
+bool ExprMutationAnalyzer::isUnevaluated(const Expr *Exp) {
+  return isUnevaluated(Exp, Stm, Context);
+}

[clang] 63ecb7d - bugfix in InfiniteLoopCheck to not print warnings for unevaluated loops

2022-05-23 Thread usama hameed via cfe-commits

Author: usama hameed
Date: 2022-05-23T20:18:49-07:00
New Revision: 63ecb7dcc80d17770461c8bf01bddeb2b795625b

URL: 
https://github.com/llvm/llvm-project/commit/63ecb7dcc80d17770461c8bf01bddeb2b795625b
DIFF: 
https://github.com/llvm/llvm-project/commit/63ecb7dcc80d17770461c8bf01bddeb2b795625b.diff

LOG: bugfix in InfiniteLoopCheck to not print warnings for unevaluated loops

Added a separate check for unevaluated statements. Updated InfiniteLoopCheck to 
use new check

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

Added: 


Modified: 
clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
clang/include/clang/Analysis/Analyses/ExprMutationAnalyzer.h
clang/lib/Analysis/ExprMutationAnalyzer.cpp

Removed: 




diff  --git a/clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
index cb9bd7bb43f50..8815de220882e 100644
--- a/clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
@@ -81,22 +81,6 @@ static bool isVarThatIsPossiblyChanged(const Decl *Func, 
const Stmt *LoopStmt,
   return false;
 }
 
-bool isUnevaluated(const Decl *Func, const Stmt *LoopStmt, const Stmt *Cond,
-   ASTContext *Context) {
-  if (const auto *Exp = dyn_cast(Cond)) {
-if (const auto *ForLoop = dyn_cast(LoopStmt)) {
-  return (ForLoop->getInc() && ExprMutationAnalyzer::isUnevaluated(
-   Exp, *ForLoop->getInc(), *Context)) ||
- (ForLoop->getBody() && ExprMutationAnalyzer::isUnevaluated(
-Exp, *ForLoop->getBody(), *Context)) ||
- (ForLoop->getCond() && ExprMutationAnalyzer::isUnevaluated(
-Exp, *ForLoop->getCond(), *Context));
-}
-return ExprMutationAnalyzer::isUnevaluated(Exp, *LoopStmt, *Context);
-  }
-  return true;
-}
-
 /// Return whether at least one variable of `Cond` changed in `LoopStmt`.
 static bool isAtLeastOneCondVarChanged(const Decl *Func, const Stmt *LoopStmt,
const Stmt *Cond, ASTContext *Context) {
@@ -193,7 +177,7 @@ void InfiniteLoopCheck::check(const 
MatchFinder::MatchResult &Result) {
 }
   }
 
-  if (isUnevaluated(Func, LoopStmt, Cond, Result.Context))
+  if (ExprMutationAnalyzer::isUnevaluated(LoopStmt, *LoopStmt, 
*Result.Context))
 return;
 
   if (isAtLeastOneCondVarChanged(Func, LoopStmt, Cond, Result.Context))

diff  --git a/clang/include/clang/Analysis/Analyses/ExprMutationAnalyzer.h 
b/clang/include/clang/Analysis/Analyses/ExprMutationAnalyzer.h
index 1b2b7ffcfb67a..a48b9735dfee5 100644
--- a/clang/include/clang/Analysis/Analyses/ExprMutationAnalyzer.h
+++ b/clang/include/clang/Analysis/Analyses/ExprMutationAnalyzer.h
@@ -38,6 +38,8 @@ class ExprMutationAnalyzer {
   }
   const Stmt *findPointeeMutation(const Expr *Exp);
   const Stmt *findPointeeMutation(const Decl *Dec);
+  static bool isUnevaluated(const Stmt *Smt, const Stmt &Stm,
+ASTContext &Context);
   static bool isUnevaluated(const Expr *Exp, const Stmt &Stm,
 ASTContext &Context);
 

diff  --git a/clang/lib/Analysis/ExprMutationAnalyzer.cpp 
b/clang/lib/Analysis/ExprMutationAnalyzer.cpp
index 4d7decc1137c2..135327a3cfe54 100644
--- a/clang/lib/Analysis/ExprMutationAnalyzer.cpp
+++ b/clang/lib/Analysis/ExprMutationAnalyzer.cpp
@@ -194,36 +194,44 @@ const Stmt *ExprMutationAnalyzer::tryEachDeclRef(const 
Decl *Dec,
   return nullptr;
 }
 
+auto isUnevaluatedMatcher(const Stmt *Exp) {
+  return anyOf(
+  // `Exp` is part of the underlying expression of
+  // decltype/typeof if it has an ancestor of
+  // typeLoc.
+  hasAncestor(typeLoc(unless(hasAncestor(unaryExprOrTypeTraitExpr(),
+  hasAncestor(expr(anyOf(
+  // `UnaryExprOrTypeTraitExpr` is unevaluated
+  // unless it's sizeof on VLA.
+  unaryExprOrTypeTraitExpr(
+  unless(sizeOfExpr(hasArgumentOfType(variableArrayType(),
+  // `CXXTypeidExpr` is unevaluated unless it's
+  // applied to an expression of glvalue of
+  // polymorphic class type.
+  cxxTypeidExpr(unless(isPotentiallyEvaluated())),
+  // The controlling expression of
+  // `GenericSelectionExpr` is unevaluated.
+  genericSelectionExpr(
+  hasControllingExpr(hasDescendant(equalsNode(Exp,
+  cxxNoexceptExpr();
+}
+
 bool ExprMutationAnalyzer::isUnevaluated(const Expr *Exp, const Stmt &Stm,
  ASTContext &Context) {
-  return selectFirst(
+  return selectFirst(NodeID::value,
+   
match(findAll(expr(canResolveToExpr(equalsNode(Exp)),
+  isUnevaluatedMatcher(Exp))
+

[clang] ca81abc - updated canResolveToExpr to accept both statements and expressions. Removed unnecessary code

2022-05-23 Thread usama hameed via cfe-commits

Author: usama hameed
Date: 2022-05-23T20:18:49-07:00
New Revision: ca81abcfd752e65c533825a5fadac19ce5a33578

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

LOG: updated canResolveToExpr to accept both statements and expressions. 
Removed unnecessary code

Added: 


Modified: 
clang/include/clang/Analysis/Analyses/ExprMutationAnalyzer.h
clang/lib/Analysis/ExprMutationAnalyzer.cpp

Removed: 




diff  --git a/clang/include/clang/Analysis/Analyses/ExprMutationAnalyzer.h 
b/clang/include/clang/Analysis/Analyses/ExprMutationAnalyzer.h
index a48b9735dfee..1ceef944fbc3 100644
--- a/clang/include/clang/Analysis/Analyses/ExprMutationAnalyzer.h
+++ b/clang/include/clang/Analysis/Analyses/ExprMutationAnalyzer.h
@@ -40,8 +40,6 @@ class ExprMutationAnalyzer {
   const Stmt *findPointeeMutation(const Decl *Dec);
   static bool isUnevaluated(const Stmt *Smt, const Stmt &Stm,
 ASTContext &Context);
-  static bool isUnevaluated(const Expr *Exp, const Stmt &Stm,
-ASTContext &Context);
 
 private:
   using MutationFinder = const Stmt *(ExprMutationAnalyzer::*)(const Expr *);

diff  --git a/clang/lib/Analysis/ExprMutationAnalyzer.cpp 
b/clang/lib/Analysis/ExprMutationAnalyzer.cpp
index 135327a3cfe5..e3bb902b1fe9 100644
--- a/clang/lib/Analysis/ExprMutationAnalyzer.cpp
+++ b/clang/lib/Analysis/ExprMutationAnalyzer.cpp
@@ -39,8 +39,13 @@ AST_MATCHER_P(Expr, maybeEvalCommaExpr, 
ast_matchers::internal::Matcher,
   return InnerMatcher.matches(*Result, Finder, Builder);
 }
 
-AST_MATCHER_P(Expr, canResolveToExpr, ast_matchers::internal::Matcher,
+AST_MATCHER_P(Stmt, canResolveToExpr, ast_matchers::internal::Matcher,
   InnerMatcher) {
+  auto *Exp = dyn_cast(&Node);
+  if (!Exp) {
+return stmt().matches(Node, Finder, Builder);
+  }
+
   auto DerivedToBase = [](const ast_matchers::internal::Matcher &Inner) {
 return implicitCastExpr(anyOf(hasCastKind(CK_DerivedToBase),
   hasCastKind(CK_UncheckedDerivedToBase)),
@@ -71,7 +76,7 @@ AST_MATCHER_P(Expr, canResolveToExpr, 
ast_matchers::internal::Matcher,
  IgnoreDerivedToBase(ConditionalOperator),
  IgnoreDerivedToBase(ElvisOperator;
 
-  return ComplexMatcher.matches(Node, Finder, Builder);
+  return ComplexMatcher.matches(*Exp, Finder, Builder);
 }
 
 // Similar to 'hasAnyArgument', but does not work because 'InitListExpr' does
@@ -194,44 +199,36 @@ const Stmt *ExprMutationAnalyzer::tryEachDeclRef(const 
Decl *Dec,
   return nullptr;
 }
 
-auto isUnevaluatedMatcher(const Stmt *Exp) {
-  return anyOf(
-  // `Exp` is part of the underlying expression of
-  // decltype/typeof if it has an ancestor of
-  // typeLoc.
-  hasAncestor(typeLoc(unless(hasAncestor(unaryExprOrTypeTraitExpr(),
-  hasAncestor(expr(anyOf(
-  // `UnaryExprOrTypeTraitExpr` is unevaluated
-  // unless it's sizeof on VLA.
-  unaryExprOrTypeTraitExpr(
-  unless(sizeOfExpr(hasArgumentOfType(variableArrayType(),
-  // `CXXTypeidExpr` is unevaluated unless it's
-  // applied to an expression of glvalue of
-  // polymorphic class type.
-  cxxTypeidExpr(unless(isPotentiallyEvaluated())),
-  // The controlling expression of
-  // `GenericSelectionExpr` is unevaluated.
-  genericSelectionExpr(
-  hasControllingExpr(hasDescendant(equalsNode(Exp,
-  cxxNoexceptExpr();
-}
-
-bool ExprMutationAnalyzer::isUnevaluated(const Expr *Exp, const Stmt &Stm,
- ASTContext &Context) {
-  return selectFirst(NodeID::value,
-   
match(findAll(expr(canResolveToExpr(equalsNode(Exp)),
-  isUnevaluatedMatcher(Exp))
- .bind(NodeID::value)),
- Stm, Context)) != nullptr;
-}
-
 bool ExprMutationAnalyzer::isUnevaluated(const Stmt *Exp, const Stmt &Stm,
  ASTContext &Context) {
   return selectFirst(
  NodeID::value,
- match(findAll(stmt(equalsNode(Exp), isUnevaluatedMatcher(Exp))
-   .bind(NodeID::value)),
-   Stm, Context)) != nullptr;
+ match(
+ findAll(
+ stmt(canResolveToExpr(equalsNode(Exp)),
+  anyOf(
+  // `Exp` is part of the underlying expression of
+  // decltype/typeof if it has an ancestor of
+  // typeLoc.
+  hasAncestor(typeLoc(unless(
+   

[clang] [compiler-rt] [llvm] Get the linker version and pass the it to compiler-rt tests on Darwin. (PR #86220)

2024-03-21 Thread Usama Hameed via cfe-commits

https://github.com/usama54321 created 
https://github.com/llvm/llvm-project/pull/86220

The HOST_LINK_VERSION is a hardcoded string in Darwin clang that detects the 
linker version at configure time. The driver uses this information to build the 
correct set of arguments for the linker. This patch detects the linker version 
again during compiler-rt configuration and passes it to the tests. This allows 
a clang built on a machine with a new linker to run compiler-rt tests on a 
machine with an old linker.

rdar://125198603

>From 3148a5e1030493d37da0b24079b1280d9f893331 Mon Sep 17 00:00:00 2001
From: usama 
Date: Thu, 21 Mar 2024 16:56:32 -0700
Subject: [PATCH] Get the linker version and pass the it to compiler-rt tests
 on Darwin.

The HOST_LINK_VERSION is a hardcoded string in Darwin clang that detects
the linker version at configure time. The driver uses this information
to build the correct set of arguments for the linker. This patch detects
the linker version again during compiler-rt configuration and passes it
to the tests. This allows a clang built on a machine with a new linker
to run compiler-rt tests on a machine with an old linker.

rdar://125198603
---
 clang/CMakeLists.txt   | 16 ++--
 cmake/Modules/GetDarwinLinkerVersion.cmake | 19 +++
 compiler-rt/CMakeLists.txt | 10 ++
 compiler-rt/test/lit.common.cfg.py |  4 
 compiler-rt/test/lit.common.configured.in  |  1 +
 5 files changed, 36 insertions(+), 14 deletions(-)
 create mode 100644 cmake/Modules/GetDarwinLinkerVersion.cmake

diff --git a/clang/CMakeLists.txt b/clang/CMakeLists.txt
index 761dab8c28c134..bcafbc37fbe1fb 100644
--- a/clang/CMakeLists.txt
+++ b/clang/CMakeLists.txt
@@ -15,6 +15,7 @@ endif()
 
 # Must go below project(..)
 include(GNUInstallDirs)
+include(GetDarwinLinkerVersion)
 
 if(CLANG_BUILT_STANDALONE)
   set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to")
@@ -346,20 +347,7 @@ endif ()
 # Determine HOST_LINK_VERSION on Darwin.
 set(HOST_LINK_VERSION)
 if (APPLE AND NOT CMAKE_LINKER MATCHES ".*lld.*")
-  set(LD_V_OUTPUT)
-  execute_process(
-COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
-RESULT_VARIABLE HAD_ERROR
-OUTPUT_VARIABLE LD_V_OUTPUT
-  )
-  if (HAD_ERROR)
-message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
-  endif()
-  if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
-string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION 
${LD_V_OUTPUT})
-  elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
-string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" HOST_LINK_VERSION 
${LD_V_OUTPUT})
-  endif()
+  get_darwin_linker_version()
   message(STATUS "Host linker version: ${HOST_LINK_VERSION}")
 endif()
 
diff --git a/cmake/Modules/GetDarwinLinkerVersion.cmake 
b/cmake/Modules/GetDarwinLinkerVersion.cmake
new file mode 100644
index 00..b6a10fdc173677
--- /dev/null
+++ b/cmake/Modules/GetDarwinLinkerVersion.cmake
@@ -0,0 +1,19 @@
+# Get the linker version on Darwin
+function(get_darwin_linker_version)
+  set(LINK_VERSION)
+  set(LD_V_OUTPUT)
+  execute_process(
+COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
+RESULT_VARIABLE HAD_ERROR
+OUTPUT_VARIABLE LD_V_OUTPUT
+)
+  if (HAD_ERROR)
+message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
+  endif()
+  if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
+string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" LINK_VERSION 
${LD_V_OUTPUT})
+  elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
+string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" LINK_VERSION 
${LD_V_OUTPUT})
+  endif()
+  set(HOST_LINK_VERSION ${LINK_VERSION} PARENT_SCOPE)
+endfunction()
diff --git a/compiler-rt/CMakeLists.txt b/compiler-rt/CMakeLists.txt
index d562a6206c00e7..9fb15403b933dc 100644
--- a/compiler-rt/CMakeLists.txt
+++ b/compiler-rt/CMakeLists.txt
@@ -36,6 +36,7 @@ include(SetPlatformToolchainTools)
 include(base-config-ix)
 include(CompilerRTUtils)
 include(CMakeDependentOption)
+include(GetDarwinLinkerVersion)
 
 option(COMPILER_RT_BUILD_BUILTINS "Build builtins" ON)
 mark_as_advanced(COMPILER_RT_BUILD_BUILTINS)
@@ -444,6 +445,15 @@ else()
   set(SANITIZER_USE_SYMBOLS FALSE)
 endif()
 
+# Get the linker version while configuring compiler-rt and explicitly pass it
+# in cflags during testing. This fixes the compiler/linker version mismatch
+# issue when running a clang built with a newer Xcode in an older Xcode
+set(HOST_LINK_VERSION)
+if (APPLE AND NOT CMAKE_LINKER MATCHES ".*lld.*")
+  get_darwin_linker_version()
+  message(STATUS "Host linker version: ${HOST_LINK_VERSION}")
+endif()
+
 # Build sanitizer runtimes with debug info.
 if(MSVC)
   # Use /Z7 instead of /Zi for the asan runtime. This avoids the LNK4099
diff --git a/compiler-rt/test/lit.common.cfg.py 
b/compiler-rt/test/lit.common.cfg.py
index bd9b926c1505b1..cc59a794e11352 100644
--- a/compiler-rt/test/lit.common.cfg.py
+++ b/co

[clang] [compiler-rt] [llvm] Get the linker version and pass the it to compiler-rt tests on Darwin. (PR #86220)

2024-03-22 Thread Usama Hameed via cfe-commits

https://github.com/usama54321 edited 
https://github.com/llvm/llvm-project/pull/86220
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] Get the linker version and pass the it to compiler-rt tests on Darwin. (PR #86220)

2024-03-22 Thread Usama Hameed via cfe-commits

https://github.com/usama54321 edited 
https://github.com/llvm/llvm-project/pull/86220
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] Get the linker version and pass the it to compiler-rt tests on Darwin. (PR #86220)

2024-03-22 Thread Usama Hameed via cfe-commits

https://github.com/usama54321 edited 
https://github.com/llvm/llvm-project/pull/86220
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] Get the linker version and pass the it to compiler-rt tests on Darwin. (PR #86220)

2024-03-22 Thread Usama Hameed via cfe-commits

https://github.com/usama54321 edited 
https://github.com/llvm/llvm-project/pull/86220
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] Get the linker version and pass the it to compiler-rt tests on Darwin. (PR #86220)

2024-03-22 Thread Usama Hameed via cfe-commits

https://github.com/usama54321 closed 
https://github.com/llvm/llvm-project/pull/86220
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] Get the linker version and pass the it to compiler-rt tests on Darwin. (PR #86220)

2024-03-22 Thread Usama Hameed via cfe-commits

https://github.com/usama54321 updated 
https://github.com/llvm/llvm-project/pull/86220

>From 0dab57b9f541a9fb6cbbe85f034117a19227c331 Mon Sep 17 00:00:00 2001
From: usama 
Date: Thu, 21 Mar 2024 16:56:32 -0700
Subject: [PATCH] Get the linker version and pass the it to compiler-rt tests
 on Darwin.

The HOST_LINK_VERSION is a hardcoded string in Darwin clang that detects
the linker version at configure time. The driver uses this information
to build the correct set of arguments for the linker. This patch detects
the linker version again during compiler-rt configuration and passes it
to the tests. This allows a clang built on a machine with a new linker
to run compiler-rt tests on a machine with an old linker.

rdar://125198603
---
 clang/CMakeLists.txt   | 16 ++--
 cmake/Modules/GetDarwinLinkerVersion.cmake | 19 +++
 compiler-rt/CMakeLists.txt | 10 ++
 compiler-rt/test/lit.common.cfg.py |  4 
 compiler-rt/test/lit.common.configured.in  |  1 +
 5 files changed, 36 insertions(+), 14 deletions(-)
 create mode 100644 cmake/Modules/GetDarwinLinkerVersion.cmake

diff --git a/clang/CMakeLists.txt b/clang/CMakeLists.txt
index 761dab8c28c134..ee783d52e4a4e2 100644
--- a/clang/CMakeLists.txt
+++ b/clang/CMakeLists.txt
@@ -15,6 +15,7 @@ endif()
 
 # Must go below project(..)
 include(GNUInstallDirs)
+include(GetDarwinLinkerVersion)
 
 if(CLANG_BUILT_STANDALONE)
   set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to")
@@ -346,20 +347,7 @@ endif ()
 # Determine HOST_LINK_VERSION on Darwin.
 set(HOST_LINK_VERSION)
 if (APPLE AND NOT CMAKE_LINKER MATCHES ".*lld.*")
-  set(LD_V_OUTPUT)
-  execute_process(
-COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
-RESULT_VARIABLE HAD_ERROR
-OUTPUT_VARIABLE LD_V_OUTPUT
-  )
-  if (HAD_ERROR)
-message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
-  endif()
-  if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
-string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION 
${LD_V_OUTPUT})
-  elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
-string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" HOST_LINK_VERSION 
${LD_V_OUTPUT})
-  endif()
+  get_darwin_linker_version(HOST_LINK_VERSION)
   message(STATUS "Host linker version: ${HOST_LINK_VERSION}")
 endif()
 
diff --git a/cmake/Modules/GetDarwinLinkerVersion.cmake 
b/cmake/Modules/GetDarwinLinkerVersion.cmake
new file mode 100644
index 00..c27e50128586db
--- /dev/null
+++ b/cmake/Modules/GetDarwinLinkerVersion.cmake
@@ -0,0 +1,19 @@
+# Get the linker version on Darwin
+function(get_darwin_linker_version variable)
+  set(LINK_VERSION)
+  set(LD_V_OUTPUT)
+  execute_process(
+COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
+RESULT_VARIABLE HAD_ERROR
+OUTPUT_VARIABLE LD_V_OUTPUT
+)
+  if (HAD_ERROR)
+message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
+  endif()
+  if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
+string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" LINK_VERSION 
${LD_V_OUTPUT})
+  elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
+string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" LINK_VERSION 
${LD_V_OUTPUT})
+  endif()
+  set(${variable} ${LINK_VERSION} PARENT_SCOPE)
+endfunction()
diff --git a/compiler-rt/CMakeLists.txt b/compiler-rt/CMakeLists.txt
index d562a6206c00e7..f4e92f14db8570 100644
--- a/compiler-rt/CMakeLists.txt
+++ b/compiler-rt/CMakeLists.txt
@@ -36,6 +36,7 @@ include(SetPlatformToolchainTools)
 include(base-config-ix)
 include(CompilerRTUtils)
 include(CMakeDependentOption)
+include(GetDarwinLinkerVersion)
 
 option(COMPILER_RT_BUILD_BUILTINS "Build builtins" ON)
 mark_as_advanced(COMPILER_RT_BUILD_BUILTINS)
@@ -444,6 +445,15 @@ else()
   set(SANITIZER_USE_SYMBOLS FALSE)
 endif()
 
+# Get the linker version while configuring compiler-rt and explicitly pass it
+# in cflags during testing. This fixes the compiler/linker version mismatch
+# issue when running a clang built with a newer Xcode in an older Xcode
+set(COMPILER_RT_DARWIN_LINKER_VERSION)
+if (APPLE AND NOT CMAKE_LINKER MATCHES ".*lld.*")
+  get_darwin_linker_version(COMPILER_RT_DARWIN_LINKER_VERSION)
+  message(STATUS "Host linker version: ${COMPILER_RT_DARWIN_LINKER_VERSION}")
+endif()
+
 # Build sanitizer runtimes with debug info.
 if(MSVC)
   # Use /Z7 instead of /Zi for the asan runtime. This avoids the LNK4099
diff --git a/compiler-rt/test/lit.common.cfg.py 
b/compiler-rt/test/lit.common.cfg.py
index bd9b926c1505b1..0ac20a9831d950 100644
--- a/compiler-rt/test/lit.common.cfg.py
+++ b/compiler-rt/test/lit.common.cfg.py
@@ -882,6 +882,10 @@ def is_windows_lto_supported():
 elif config.use_lld and (not config.has_lld):
 config.unsupported = True
 
+if config.host_os == "Darwin":
+if getattr(config, "darwin_linker_version", None):
+extra_cflags += ["-mlinker-version=" + config.darwin_linker_version]
+
 # Append any extra fla

[clang] [clang] [cmake] Add cmake module dir before using GetDarwinLinkerVersion (PR #86386)

2024-03-23 Thread Usama Hameed via cfe-commits

https://github.com/usama54321 approved this pull request.


https://github.com/llvm/llvm-project/pull/86386
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] [cmake] Add cmake module dir before using GetDarwinLinkerVersion (PR #86386)

2024-03-23 Thread Usama Hameed via cfe-commits

usama54321 wrote:

Thanks a lot for fixing this

https://github.com/llvm/llvm-project/pull/86386
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 5b6dbde - [CodeGen] bugfix: ApplyDebugLocation goes out of scope before intended

2023-01-24 Thread usama hameed via cfe-commits

Author: usama hameed
Date: 2023-01-24T15:31:07-08:00
New Revision: 5b6dbdecba0b033e6a64593062cf87cd6c8a66ee

URL: 
https://github.com/llvm/llvm-project/commit/5b6dbdecba0b033e6a64593062cf87cd6c8a66ee
DIFF: 
https://github.com/llvm/llvm-project/commit/5b6dbdecba0b033e6a64593062cf87cd6c8a66ee.diff

LOG: [CodeGen] bugfix: ApplyDebugLocation goes out of scope before intended

rdar://103570533

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

Added: 
clang/test/CodeGenObjC/objc-arc-ubsan-debugging.m

Modified: 
clang/lib/CodeGen/CodeGenFunction.cpp

Removed: 




diff  --git a/clang/lib/CodeGen/CodeGenFunction.cpp 
b/clang/lib/CodeGen/CodeGenFunction.cpp
index 55464e1b1636b..8cbe2a5407440 100644
--- a/clang/lib/CodeGen/CodeGenFunction.cpp
+++ b/clang/lib/CodeGen/CodeGenFunction.cpp
@@ -361,17 +361,18 @@ void CodeGenFunction::FinishFunction(SourceLocation 
EndLoc) {
   bool HasOnlyLifetimeMarkers =
   HasCleanups && EHStack.containsOnlyLifetimeMarkers(PrologueCleanupDepth);
   bool EmitRetDbgLoc = !HasCleanups || HasOnlyLifetimeMarkers;
+
+  std::optional OAL;
   if (HasCleanups) {
 // Make sure the line table doesn't jump back into the body for
 // the ret after it's been at EndLoc.
-std::optional AL;
 if (CGDebugInfo *DI = getDebugInfo()) {
   if (OnlySimpleReturnStmts)
 DI->EmitLocation(Builder, EndLoc);
   else
 // We may not have a valid end location. Try to apply it anyway, and
 // fall back to an artificial location if needed.
-AL = ApplyDebugLocation::CreateDefaultArtificial(*this, EndLoc);
+OAL = ApplyDebugLocation::CreateDefaultArtificial(*this, EndLoc);
 }
 
 PopCleanupBlocks(PrologueCleanupDepth);

diff  --git a/clang/test/CodeGenObjC/objc-arc-ubsan-debugging.m 
b/clang/test/CodeGenObjC/objc-arc-ubsan-debugging.m
new file mode 100644
index 0..531f0fdf9
--- /dev/null
+++ b/clang/test/CodeGenObjC/objc-arc-ubsan-debugging.m
@@ -0,0 +1,14 @@
+// RUN: %clang -x objective-c -target arm64-apple-macos12.0 -fobjc-arc 
-std=gnu99  -O0 -fsanitize=undefined -fsanitize=nullability -c %s -v -g
+
+@interface NSString
+@end
+
+struct A {
+NSString *a;
+};
+
+NSString* _Nonnull foo()
+{
+struct A a;
+return 0;
+}



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 92b4946 - [CodeGen] bugfix: add REQUIRES target triple in test

2023-01-24 Thread usama hameed via cfe-commits

Author: usama hameed
Date: 2023-01-24T16:28:01-08:00
New Revision: 92b4946aa2f6e061bf0ab9803fc6a0657d9ce969

URL: 
https://github.com/llvm/llvm-project/commit/92b4946aa2f6e061bf0ab9803fc6a0657d9ce969
DIFF: 
https://github.com/llvm/llvm-project/commit/92b4946aa2f6e061bf0ab9803fc6a0657d9ce969.diff

LOG: [CodeGen] bugfix: add REQUIRES target triple in test

Added: 


Modified: 
clang/test/CodeGenObjC/objc-arc-ubsan-debugging.m

Removed: 




diff  --git a/clang/test/CodeGenObjC/objc-arc-ubsan-debugging.m 
b/clang/test/CodeGenObjC/objc-arc-ubsan-debugging.m
index 531f0fdf9..04c8d69a7bc39 100644
--- a/clang/test/CodeGenObjC/objc-arc-ubsan-debugging.m
+++ b/clang/test/CodeGenObjC/objc-arc-ubsan-debugging.m
@@ -1,4 +1,5 @@
 // RUN: %clang -x objective-c -target arm64-apple-macos12.0 -fobjc-arc 
-std=gnu99  -O0 -fsanitize=undefined -fsanitize=nullability -c %s -v -g
+// REQUIRES: aarch64-registered-target
 
 @interface NSString
 @end



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 57690a8 - [Sanitizers] fix -fno-sanitize-link-runtime for darwin

2023-02-01 Thread usama hameed via cfe-commits

Author: usama hameed
Date: 2023-02-01T11:06:19-08:00
New Revision: 57690a8ece9e5d3b749c66a226a4c79f3d7588aa

URL: 
https://github.com/llvm/llvm-project/commit/57690a8ece9e5d3b749c66a226a4c79f3d7588aa
DIFF: 
https://github.com/llvm/llvm-project/commit/57690a8ece9e5d3b749c66a226a4c79f3d7588aa.diff

LOG: [Sanitizers] fix -fno-sanitize-link-runtime for darwin

rdar://99200922

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

Added: 


Modified: 
clang/lib/Driver/ToolChains/Darwin.cpp
clang/test/Driver/sanitizer-ld.c

Removed: 




diff  --git a/clang/lib/Driver/ToolChains/Darwin.cpp 
b/clang/lib/Driver/ToolChains/Darwin.cpp
index 00bd12faca572..c6343c3be24e9 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -1431,27 +1431,29 @@ void DarwinClang::AddLinkRuntimeLibArgs(const ArgList 
&Args,
 return;
   }
 
-  if (Sanitize.needsAsanRt())
-AddLinkSanitizerLibArgs(Args, CmdArgs, "asan");
-  if (Sanitize.needsLsanRt())
-AddLinkSanitizerLibArgs(Args, CmdArgs, "lsan");
-  if (Sanitize.needsUbsanRt()) {
-assert(Sanitize.needsSharedRt() && "Static sanitizer runtimes not 
supported");
-AddLinkSanitizerLibArgs(Args, CmdArgs,
-Sanitize.requiresMinimalRuntime() ? "ubsan_minimal"
-  : "ubsan");
-  }
-  if (Sanitize.needsTsanRt())
-AddLinkSanitizerLibArgs(Args, CmdArgs, "tsan");
-  if (Sanitize.needsFuzzer() && !Args.hasArg(options::OPT_dynamiclib)) {
-AddLinkSanitizerLibArgs(Args, CmdArgs, "fuzzer", /*shared=*/false);
-
-// Libfuzzer is written in C++ and requires libcxx.
-AddCXXStdlibLibArgs(Args, CmdArgs);
-  }
-  if (Sanitize.needsStatsRt()) {
-AddLinkRuntimeLib(Args, CmdArgs, "stats_client", RLO_AlwaysLink);
-AddLinkSanitizerLibArgs(Args, CmdArgs, "stats");
+  if (Sanitize.linkRuntimes()) {
+if (Sanitize.needsAsanRt())
+  AddLinkSanitizerLibArgs(Args, CmdArgs, "asan");
+if (Sanitize.needsLsanRt())
+  AddLinkSanitizerLibArgs(Args, CmdArgs, "lsan");
+if (Sanitize.needsUbsanRt()) {
+  assert(Sanitize.needsSharedRt() && "Static sanitizer runtimes not 
supported");
+  AddLinkSanitizerLibArgs(Args, CmdArgs,
+  Sanitize.requiresMinimalRuntime() ? 
"ubsan_minimal"
+: "ubsan");
+}
+if (Sanitize.needsTsanRt())
+  AddLinkSanitizerLibArgs(Args, CmdArgs, "tsan");
+if (Sanitize.needsFuzzer() && !Args.hasArg(options::OPT_dynamiclib)) {
+  AddLinkSanitizerLibArgs(Args, CmdArgs, "fuzzer", /*shared=*/false);
+
+// Libfuzzer is written in C++ and requires libcxx.
+AddCXXStdlibLibArgs(Args, CmdArgs);
+}
+if (Sanitize.needsStatsRt()) {
+  AddLinkRuntimeLib(Args, CmdArgs, "stats_client", RLO_AlwaysLink);
+  AddLinkSanitizerLibArgs(Args, CmdArgs, "stats");
+}
   }
 
   const XRayArgs &XRay = getXRayArgs();

diff  --git a/clang/test/Driver/sanitizer-ld.c 
b/clang/test/Driver/sanitizer-ld.c
index 6b85abfd81c55..3621d12eeb8c6 100644
--- a/clang/test/Driver/sanitizer-ld.c
+++ b/clang/test/Driver/sanitizer-ld.c
@@ -25,6 +25,14 @@
 //
 // CHECK-ASAN-NO-LINK-RUNTIME-LINUX-NOT: libclang_rt.asan-x86_64
 
+// RUN: %clang -fsanitize=address -fno-sanitize-link-runtime -### %s 2>&1 \
+// RUN: --target=arm64e-apple-macosx -fuse-ld=ld \
+// RUN: -resource-dir=%S/Inputs/resource_dir \
+// RUN: --sysroot=%S/Inputs/basic_linux_tree \
+// RUN:   | FileCheck --check-prefix=CHECK-ASAN-NO-LINK-RUNTIME-DARWIN %s
+//
+// CHECK-ASAN-NO-LINK-RUNTIME-DARWIN-NOT: libclang_rt.asan
+
 // RUN: %clang -fsanitize=address -### %s 2>&1 \
 // RUN: --target=x86_64-unknown-linux -fuse-ld=ld \
 // RUN: -resource-dir=%S/Inputs/resource_dir \
@@ -288,6 +296,14 @@
 //
 // CHECK-TSAN-NO-LINK-RUNTIME-LINUX-NOT: libclang_rt.tsan
 
+// RUN: %clang -fsanitize=thread -fno-sanitize-link-runtime -### %s 2>&1 \
+// RUN: --target=arm64e-apple-ios -fuse-ld=ld \
+// RUN: -resource-dir=%S/Inputs/resource_dir \
+// RUN: --sysroot=%S/Inputs/basic_linux_tree \
+// RUN:   | FileCheck --check-prefix=CHECK-TSAN-NO-LINK-RUNTIME-DARWIN %s
+//
+// CHECK-TSAN-NO-LINK-RUNTIME-DARWIN-NOT: libclang_rt.tsan
+
 // RUN: %clangxx -### %s 2>&1 \
 // RUN: --target=x86_64-unknown-linux -fuse-ld=ld -stdlib=platform 
-lstdc++ \
 // RUN: -fsanitize=memory \
@@ -353,6 +369,22 @@
 //
 // CHECK-UBSAN-NO-LINK-RUNTIME-LINUX-NOT: libclang_rt.undefined
 
+// RUN: %clang -fsanitize=undefined -fno-sanitize-link-runtime -### %s 2>&1 \
+// RUN: --target=x86_64-apple-darwin -fuse-ld=ld \
+// RUN: -resource-dir=%S/Inputs/resource_dir \
+// RUN: --sysroot=%S/Inputs/basic_linux_tree \
+// RUN:   | FileCheck --check-prefix=CHECK-UBSAN-NO-LINK-RUNTIME-DARWIN %s
+//
+// CHECK-UBSAN-NO-LINK-RUNTIME-DARWIN-NOT: libclang_rt.ubsan
+
+//

[clang] a44477b - [CompilerRT] Remove ubsan static runtime on Apple

2023-01-17 Thread usama hameed via cfe-commits

Author: usama hameed
Date: 2023-01-17T14:33:31-08:00
New Revision: a44477b1f4b5c462474c12144d5f914d04a674cb

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

LOG: [CompilerRT] Remove ubsan static runtime on Apple

This patch removes the static ubsan runtime on Apple devices. The motivation
is to reduce the toolchain size.

rdar://102061519

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

Added: 


Modified: 
clang/include/clang/Basic/DiagnosticDriverKinds.td
clang/lib/Driver/ToolChains/Darwin.cpp
clang/test/Driver/sanitizer-ld.c
compiler-rt/lib/ubsan/CMakeLists.txt
compiler-rt/test/ubsan/CMakeLists.txt

Removed: 




diff  --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td 
b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index 08be6966f686..6f046cdb7952 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -215,6 +215,8 @@ def err_drv_malformed_sanitizer_coverage_allowlist : Error<
   "malformed sanitizer coverage allowlist: '%0'">;
 def err_drv_malformed_sanitizer_coverage_ignorelist : Error<
   "malformed sanitizer coverage ignorelist: '%0'">;
+def err_drv_unsupported_static_ubsan_darwin : Error<
+  "static UndefinedBehaviorSanitizer runtime is not supported on darwin">;
 def err_drv_duplicate_config : Error<
   "no more than one option '--config' is allowed">;
 def err_drv_cannot_open_config_file : Error<

diff  --git a/clang/lib/Driver/ToolChains/Darwin.cpp 
b/clang/lib/Driver/ToolChains/Darwin.cpp
index a161355ccbc4..9f95c962ee9a 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -1425,15 +1425,22 @@ void DarwinClang::AddLinkRuntimeLibArgs(const ArgList 
&Args,
   }
 
   const SanitizerArgs &Sanitize = getSanitizerArgs(Args);
+
+  if (!Sanitize.needsSharedRt() && Sanitize.needsUbsanRt()) {
+getDriver().Diag(diag::err_drv_unsupported_static_ubsan_darwin);
+return;
+  }
+
   if (Sanitize.needsAsanRt())
 AddLinkSanitizerLibArgs(Args, CmdArgs, "asan");
   if (Sanitize.needsLsanRt())
 AddLinkSanitizerLibArgs(Args, CmdArgs, "lsan");
-  if (Sanitize.needsUbsanRt())
+  if (Sanitize.needsUbsanRt()) {
+assert(Sanitize.needsSharedRt() && "Static sanitizer runtimes not 
supported");
 AddLinkSanitizerLibArgs(Args, CmdArgs,
 Sanitize.requiresMinimalRuntime() ? "ubsan_minimal"
-  : "ubsan",
-Sanitize.needsSharedRt());
+  : "ubsan");
+  }
   if (Sanitize.needsTsanRt())
 AddLinkSanitizerLibArgs(Args, CmdArgs, "tsan");
   if (Sanitize.needsFuzzer() && !Args.hasArg(options::OPT_dynamiclib)) {

diff  --git a/clang/test/Driver/sanitizer-ld.c 
b/clang/test/Driver/sanitizer-ld.c
index e58a6e51547f..6b85abfd81c5 100644
--- a/clang/test/Driver/sanitizer-ld.c
+++ b/clang/test/Driver/sanitizer-ld.c
@@ -423,8 +423,7 @@
 // RUN: --target=x86_64-apple-darwin -fuse-ld=ld -static-libsan \
 // RUN: --sysroot=%S/Inputs/basic_linux_tree \
 // RUN:   | FileCheck --check-prefix=CHECK-UBSAN-STATIC-DARWIN %s
-// CHECK-UBSAN-STATIC-DARWIN: "{{.*}}ld{{(.exe)?}}"
-// CHECK-UBSAN-STATIC-DARWIN: "{{.*}}libclang_rt.ubsan_osx.a"
+// CHECK-UBSAN-STATIC-DARWIN: {{.*}}error: static UndefinedBehaviorSanitizer 
runtime is not supported on darwin
 
 // RUN: %clang -fsanitize=address,undefined -### %s 2>&1 \
 // RUN: --target=i386-unknown-linux -fuse-ld=ld \

diff  --git a/compiler-rt/lib/ubsan/CMakeLists.txt 
b/compiler-rt/lib/ubsan/CMakeLists.txt
index 2c7e0f596ff5..520a024fbede 100644
--- a/compiler-rt/lib/ubsan/CMakeLists.txt
+++ b/compiler-rt/lib/ubsan/CMakeLists.txt
@@ -114,19 +114,21 @@ if(APPLE)
   LINK_FLAGS ${WEAK_SYMBOL_LINK_FLAGS}
   PARENT_TARGET ubsan)
 
-add_compiler_rt_runtime(clang_rt.ubsan
-  STATIC
-  OS ${UBSAN_SUPPORTED_OS}
-  ARCHS ${UBSAN_SUPPORTED_ARCH}
-  OBJECT_LIBS RTUbsan
-  RTUbsan_standalone
-  RTSanitizerCommonNoHooks
-  RTSanitizerCommonLibcNoHooks
-  RTSanitizerCommonCoverage
-  RTSanitizerCommonSymbolizerNoHooks
-  RTInterception
-  LINK_FLAGS ${WEAK_SYMBOL_LINK_FLAGS}
-  PARENT_TARGET ubsan)
+if (NOT APPLE)
+  add_compiler_rt_runtime(clang_rt.ubsan
+STATIC
+OS ${UBSAN_SUPPORTED_OS}
+ARCHS ${UBSAN_SUPPORTED_ARCH}
+OBJECT_LIBS RTUbsan
+RTUbsan_standalone
+RTSanitizerCommonNoHooks
+RTSanitizerCommonLibcNoHooks
+RTSanitizerCommonCoverage
+RTSanitizerCommonSymbol

[clang] 9afc57d - [CodeGen][UBSan] Handle sugared QualTypes correctly in

2023-08-16 Thread usama hameed via cfe-commits

Author: usama hameed
Date: 2023-08-16T14:47:49-07:00
New Revision: 9afc57dcb2e5cd36ca1ddf0fee3efa958bfd4c2a

URL: 
https://github.com/llvm/llvm-project/commit/9afc57dcb2e5cd36ca1ddf0fee3efa958bfd4c2a
DIFF: 
https://github.com/llvm/llvm-project/commit/9afc57dcb2e5cd36ca1ddf0fee3efa958bfd4c2a.diff

LOG: [CodeGen][UBSan] Handle sugared QualTypes correctly in
getUBSanFunctionTypeHash.

getUBSanFunctionTypeHash checks if a Type is a FunctionNoPrototype
by calling isa(). This does not work correctly when
the Type is wrapped in a sugar type such as an AttributedType. This
patch fixes this by using isFunctionNoProtoType() function which removes
sugar and returns the expected result.

The added test is a sanity check that the compiler no longer crashes
during compilation. It also compares the hash with and without the
function attribute for both FunctionNoProtoType and FunctionProtoType.
The hash remains the same for FunctionNoProtoType even with the addition
of an attribute.

rdar://113144087

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

Added: 
clang/test/CodeGen/ubsan-function-attributed.c

Modified: 
clang/lib/CodeGen/CodeGenFunction.cpp

Removed: 




diff  --git a/clang/lib/CodeGen/CodeGenFunction.cpp 
b/clang/lib/CodeGen/CodeGenFunction.cpp
index 2000c4ce02c954..89138ddbdc0bc0 100644
--- a/clang/lib/CodeGen/CodeGenFunction.cpp
+++ b/clang/lib/CodeGen/CodeGenFunction.cpp
@@ -572,7 +572,7 @@ llvm::ConstantInt *
 CodeGenFunction::getUBSanFunctionTypeHash(QualType Ty) const {
   // Remove any (C++17) exception specifications, to allow calling e.g. a
   // noexcept function through a non-noexcept pointer.
-  if (!isa(Ty))
+  if (!Ty->isFunctionNoProtoType())
 Ty = getContext().getFunctionTypeWithExceptionSpec(Ty, EST_None);
   std::string Mangled;
   llvm::raw_string_ostream Out(Mangled);

diff  --git a/clang/test/CodeGen/ubsan-function-attributed.c 
b/clang/test/CodeGen/ubsan-function-attributed.c
new file mode 100644
index 00..c979f161fc9220
--- /dev/null
+++ b/clang/test/CodeGen/ubsan-function-attributed.c
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -S -triple x86_64 -std=c17 -fsanitize=function %s -o - | 
FileCheck %s --check-prefixes=CHECK
+
+// CHECK: .long248076293
+void __attribute__((ms_abi)) f(void) {}
+
+// CHECK: .long905068220
+void g(void) {}
+
+// CHECK: .long1717976574
+void __attribute__((ms_abi)) f_no_prototype() {}
+
+// CHECK: .long1717976574
+void g_no_prototype() {}



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] bb5f64a - Add Requires x86 target to test due to failure in clang-armv8-quick

2023-08-16 Thread usama hameed via cfe-commits

Author: usama hameed
Date: 2023-08-16T15:09:14-07:00
New Revision: bb5f64a6fc059fcf81b4b9ffe035f5063956ddd8

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

LOG: Add Requires x86 target to test due to failure in clang-armv8-quick
bot

Added: 


Modified: 
clang/test/CodeGen/ubsan-function-attributed.c

Removed: 




diff  --git a/clang/test/CodeGen/ubsan-function-attributed.c 
b/clang/test/CodeGen/ubsan-function-attributed.c
index c979f161fc9220..ae6700dbde2102 100644
--- a/clang/test/CodeGen/ubsan-function-attributed.c
+++ b/clang/test/CodeGen/ubsan-function-attributed.c
@@ -1,3 +1,4 @@
+// REQUIRES: x86-registered-target
 // RUN: %clang_cc1 -S -triple x86_64 -std=c17 -fsanitize=function %s -o - | 
FileCheck %s --check-prefixes=CHECK
 
 // CHECK: .long248076293



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 4e7d40e - [Sanitizers] Error out for -static-libsan on darwin

2023-03-01 Thread usama hameed via cfe-commits

Author: usama hameed
Date: 2023-03-02T00:07:03+05:00
New Revision: 4e7d40e0928cfe448ba947d2a67895fccaa3535f

URL: 
https://github.com/llvm/llvm-project/commit/4e7d40e0928cfe448ba947d2a67895fccaa3535f
DIFF: 
https://github.com/llvm/llvm-project/commit/4e7d40e0928cfe448ba947d2a67895fccaa3535f.diff

LOG: [Sanitizers] Error out for -static-libsan on darwin
since it is not supported

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

Added: 


Modified: 
clang/include/clang/Basic/DiagnosticDriverKinds.td
clang/include/clang/Driver/Options.td
clang/lib/Driver/ToolChains/Darwin.cpp
clang/test/Driver/sanitizer-ld.c

Removed: 




diff  --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td 
b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index 4c922650e100f..3b32227d21acd 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -220,8 +220,8 @@ def err_drv_malformed_sanitizer_coverage_ignorelist : Error<
   "malformed sanitizer coverage ignorelist: '%0'">;
 def err_drv_malformed_sanitizer_metadata_ignorelist : Error<
   "malformed sanitizer metadata ignorelist: '%0'">;
-def err_drv_unsupported_static_ubsan_darwin : Error<
-  "static UndefinedBehaviorSanitizer runtime is not supported on darwin">;
+def err_drv_unsupported_static_sanitizer_darwin : Error<
+  "static %0 runtime is not supported on darwin">;
 def err_drv_duplicate_config : Error<
   "no more than one option '--config' is allowed">;
 def err_drv_cannot_open_config_file : Error<

diff  --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 70882e110fa52..b914b1b8f12eb 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -1215,7 +1215,7 @@ defm xl_pragma_pack : BoolFOption<"xl-pragma-pack",
 def shared_libsan : Flag<["-"], "shared-libsan">,
   HelpText<"Dynamically link the sanitizer runtime">;
 def static_libsan : Flag<["-"], "static-libsan">,
-  HelpText<"Statically link the sanitizer runtime">;
+  HelpText<"Statically link the sanitizer runtime (Not supported for ASan, 
TSan or UBSan on darwin)">;
 def : Flag<["-"], "shared-libasan">, Alias;
 def fasm : Flag<["-"], "fasm">, Group;
 

diff  --git a/clang/lib/Driver/ToolChains/Darwin.cpp 
b/clang/lib/Driver/ToolChains/Darwin.cpp
index b273f8ba04fda..6cee9c74c7adb 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -1426,24 +1426,42 @@ void DarwinClang::AddLinkRuntimeLibArgs(const ArgList 
&Args,
 
   const SanitizerArgs &Sanitize = getSanitizerArgs(Args);
 
-  if (!Sanitize.needsSharedRt() && Sanitize.needsUbsanRt()) {
-getDriver().Diag(diag::err_drv_unsupported_static_ubsan_darwin);
-return;
+  if (!Sanitize.needsSharedRt()) {
+const char *sanitizer = nullptr;
+if (Sanitize.needsUbsanRt()) {
+  sanitizer = "UndefinedBehaviorSanitizer";
+} else if (Sanitize.needsAsanRt()) {
+  sanitizer = "AddressSanitizer";
+} else if (Sanitize.needsTsanRt()) {
+  sanitizer = "ThreadSanitizer";
+}
+if (sanitizer) {
+  getDriver().Diag(diag::err_drv_unsupported_static_sanitizer_darwin)
+  << sanitizer;
+  return;
+}
   }
 
   if (Sanitize.linkRuntimes()) {
-if (Sanitize.needsAsanRt())
+if (Sanitize.needsAsanRt()) {
+  assert(Sanitize.needsSharedRt() &&
+ "Static sanitizer runtimes not supported");
   AddLinkSanitizerLibArgs(Args, CmdArgs, "asan");
+}
 if (Sanitize.needsLsanRt())
   AddLinkSanitizerLibArgs(Args, CmdArgs, "lsan");
 if (Sanitize.needsUbsanRt()) {
-  assert(Sanitize.needsSharedRt() && "Static sanitizer runtimes not 
supported");
-  AddLinkSanitizerLibArgs(Args, CmdArgs,
-  Sanitize.requiresMinimalRuntime() ? 
"ubsan_minimal"
-: "ubsan");
+  assert(Sanitize.needsSharedRt() &&
+ "Static sanitizer runtimes not supported");
+  AddLinkSanitizerLibArgs(
+  Args, CmdArgs,
+  Sanitize.requiresMinimalRuntime() ? "ubsan_minimal" : "ubsan");
 }
-if (Sanitize.needsTsanRt())
+if (Sanitize.needsTsanRt()) {
+  assert(Sanitize.needsSharedRt() &&
+ "Static sanitizer runtimes not supported");
   AddLinkSanitizerLibArgs(Args, CmdArgs, "tsan");
+}
 if (Sanitize.needsFuzzer() && !Args.hasArg(options::OPT_dynamiclib)) {
   AddLinkSanitizerLibArgs(Args, CmdArgs, "fuzzer", /*shared=*/false);
 

diff  --git a/clang/test/Driver/sanitizer-ld.c 
b/clang/test/Driver/sanitizer-ld.c
index 0ba209d870c2a..910b0ed4ff0de 100644
--- a/clang/test/Driver/sanitizer-ld.c
+++ b/clang/test/Driver/sanitizer-ld.c
@@ -457,6 +457,18 @@
 // RUN:   | FileCheck --check-prefix=CHECK-UBSAN-STATIC-DARWIN %s
 // CHECK-UBSAN-STATIC-DARWIN: {{.*}}error: static Undefi

[clang] 6420daa - Revert "[Sanitizers] Error out for -static-libsan on darwin"

2023-03-02 Thread usama hameed via cfe-commits

Author: usama hameed
Date: 2023-03-02T19:48:38+05:00
New Revision: 6420daab19e8c95f2481090564508eea1996c4de

URL: 
https://github.com/llvm/llvm-project/commit/6420daab19e8c95f2481090564508eea1996c4de
DIFF: 
https://github.com/llvm/llvm-project/commit/6420daab19e8c95f2481090564508eea1996c4de.diff

LOG: Revert "[Sanitizers] Error out for -static-libsan on darwin"

This reverts commit 4e7d40e0928cfe448ba947d2a67895fccaa3535f.

Added: 


Modified: 
clang/include/clang/Basic/DiagnosticDriverKinds.td
clang/include/clang/Driver/Options.td
clang/lib/Driver/ToolChains/Darwin.cpp
clang/test/Driver/sanitizer-ld.c

Removed: 




diff  --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td 
b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index 3b32227d21acd..4c922650e100f 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -220,8 +220,8 @@ def err_drv_malformed_sanitizer_coverage_ignorelist : Error<
   "malformed sanitizer coverage ignorelist: '%0'">;
 def err_drv_malformed_sanitizer_metadata_ignorelist : Error<
   "malformed sanitizer metadata ignorelist: '%0'">;
-def err_drv_unsupported_static_sanitizer_darwin : Error<
-  "static %0 runtime is not supported on darwin">;
+def err_drv_unsupported_static_ubsan_darwin : Error<
+  "static UndefinedBehaviorSanitizer runtime is not supported on darwin">;
 def err_drv_duplicate_config : Error<
   "no more than one option '--config' is allowed">;
 def err_drv_cannot_open_config_file : Error<

diff  --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index b914b1b8f12eb..70882e110fa52 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -1215,7 +1215,7 @@ defm xl_pragma_pack : BoolFOption<"xl-pragma-pack",
 def shared_libsan : Flag<["-"], "shared-libsan">,
   HelpText<"Dynamically link the sanitizer runtime">;
 def static_libsan : Flag<["-"], "static-libsan">,
-  HelpText<"Statically link the sanitizer runtime (Not supported for ASan, 
TSan or UBSan on darwin)">;
+  HelpText<"Statically link the sanitizer runtime">;
 def : Flag<["-"], "shared-libasan">, Alias;
 def fasm : Flag<["-"], "fasm">, Group;
 

diff  --git a/clang/lib/Driver/ToolChains/Darwin.cpp 
b/clang/lib/Driver/ToolChains/Darwin.cpp
index 6cee9c74c7adb..b273f8ba04fda 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -1426,42 +1426,24 @@ void DarwinClang::AddLinkRuntimeLibArgs(const ArgList 
&Args,
 
   const SanitizerArgs &Sanitize = getSanitizerArgs(Args);
 
-  if (!Sanitize.needsSharedRt()) {
-const char *sanitizer = nullptr;
-if (Sanitize.needsUbsanRt()) {
-  sanitizer = "UndefinedBehaviorSanitizer";
-} else if (Sanitize.needsAsanRt()) {
-  sanitizer = "AddressSanitizer";
-} else if (Sanitize.needsTsanRt()) {
-  sanitizer = "ThreadSanitizer";
-}
-if (sanitizer) {
-  getDriver().Diag(diag::err_drv_unsupported_static_sanitizer_darwin)
-  << sanitizer;
-  return;
-}
+  if (!Sanitize.needsSharedRt() && Sanitize.needsUbsanRt()) {
+getDriver().Diag(diag::err_drv_unsupported_static_ubsan_darwin);
+return;
   }
 
   if (Sanitize.linkRuntimes()) {
-if (Sanitize.needsAsanRt()) {
-  assert(Sanitize.needsSharedRt() &&
- "Static sanitizer runtimes not supported");
+if (Sanitize.needsAsanRt())
   AddLinkSanitizerLibArgs(Args, CmdArgs, "asan");
-}
 if (Sanitize.needsLsanRt())
   AddLinkSanitizerLibArgs(Args, CmdArgs, "lsan");
 if (Sanitize.needsUbsanRt()) {
-  assert(Sanitize.needsSharedRt() &&
- "Static sanitizer runtimes not supported");
-  AddLinkSanitizerLibArgs(
-  Args, CmdArgs,
-  Sanitize.requiresMinimalRuntime() ? "ubsan_minimal" : "ubsan");
+  assert(Sanitize.needsSharedRt() && "Static sanitizer runtimes not 
supported");
+  AddLinkSanitizerLibArgs(Args, CmdArgs,
+  Sanitize.requiresMinimalRuntime() ? 
"ubsan_minimal"
+: "ubsan");
 }
-if (Sanitize.needsTsanRt()) {
-  assert(Sanitize.needsSharedRt() &&
- "Static sanitizer runtimes not supported");
+if (Sanitize.needsTsanRt())
   AddLinkSanitizerLibArgs(Args, CmdArgs, "tsan");
-}
 if (Sanitize.needsFuzzer() && !Args.hasArg(options::OPT_dynamiclib)) {
   AddLinkSanitizerLibArgs(Args, CmdArgs, "fuzzer", /*shared=*/false);
 

diff  --git a/clang/test/Driver/sanitizer-ld.c 
b/clang/test/Driver/sanitizer-ld.c
index 910b0ed4ff0de..0ba209d870c2a 100644
--- a/clang/test/Driver/sanitizer-ld.c
+++ b/clang/test/Driver/sanitizer-ld.c
@@ -457,18 +457,6 @@
 // RUN:   | FileCheck --check-prefix=CHECK-UBSAN-STATIC-DARWIN %s
 // CHECK-UBSAN-STATIC-DARWIN: {{.*}}error: static UndefinedBehavior

[clang] 665e32e - [Sanitizers] Error out for -static-libsan on darwin

2023-03-06 Thread usama hameed via cfe-commits

Author: usama hameed
Date: 2023-03-06T16:08:26+05:00
New Revision: 665e32ee0ffd2b70c9066bebaaa6140dc0ca93f8

URL: 
https://github.com/llvm/llvm-project/commit/665e32ee0ffd2b70c9066bebaaa6140dc0ca93f8
DIFF: 
https://github.com/llvm/llvm-project/commit/665e32ee0ffd2b70c9066bebaaa6140dc0ca93f8.diff

LOG: [Sanitizers] Error out for -static-libsan on darwin

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

Added: 
compiler-rt/test/asan/TestCases/replaceable_new_delete_shared.cpp
compiler-rt/test/asan/TestCases/replaceable_new_delete_static.cpp

Modified: 
clang/include/clang/Basic/DiagnosticDriverKinds.td
clang/include/clang/Driver/Options.td
clang/lib/Driver/ToolChains/Darwin.cpp
clang/test/Driver/sanitizer-ld.c

Removed: 
compiler-rt/test/asan/TestCases/replaceable_new_delete.cpp



diff  --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td 
b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index 4c922650e100f..3b32227d21acd 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -220,8 +220,8 @@ def err_drv_malformed_sanitizer_coverage_ignorelist : Error<
   "malformed sanitizer coverage ignorelist: '%0'">;
 def err_drv_malformed_sanitizer_metadata_ignorelist : Error<
   "malformed sanitizer metadata ignorelist: '%0'">;
-def err_drv_unsupported_static_ubsan_darwin : Error<
-  "static UndefinedBehaviorSanitizer runtime is not supported on darwin">;
+def err_drv_unsupported_static_sanitizer_darwin : Error<
+  "static %0 runtime is not supported on darwin">;
 def err_drv_duplicate_config : Error<
   "no more than one option '--config' is allowed">;
 def err_drv_cannot_open_config_file : Error<

diff  --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 70882e110fa52..b914b1b8f12eb 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -1215,7 +1215,7 @@ defm xl_pragma_pack : BoolFOption<"xl-pragma-pack",
 def shared_libsan : Flag<["-"], "shared-libsan">,
   HelpText<"Dynamically link the sanitizer runtime">;
 def static_libsan : Flag<["-"], "static-libsan">,
-  HelpText<"Statically link the sanitizer runtime">;
+  HelpText<"Statically link the sanitizer runtime (Not supported for ASan, 
TSan or UBSan on darwin)">;
 def : Flag<["-"], "shared-libasan">, Alias;
 def fasm : Flag<["-"], "fasm">, Group;
 

diff  --git a/clang/lib/Driver/ToolChains/Darwin.cpp 
b/clang/lib/Driver/ToolChains/Darwin.cpp
index b273f8ba04fda..6cee9c74c7adb 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -1426,24 +1426,42 @@ void DarwinClang::AddLinkRuntimeLibArgs(const ArgList 
&Args,
 
   const SanitizerArgs &Sanitize = getSanitizerArgs(Args);
 
-  if (!Sanitize.needsSharedRt() && Sanitize.needsUbsanRt()) {
-getDriver().Diag(diag::err_drv_unsupported_static_ubsan_darwin);
-return;
+  if (!Sanitize.needsSharedRt()) {
+const char *sanitizer = nullptr;
+if (Sanitize.needsUbsanRt()) {
+  sanitizer = "UndefinedBehaviorSanitizer";
+} else if (Sanitize.needsAsanRt()) {
+  sanitizer = "AddressSanitizer";
+} else if (Sanitize.needsTsanRt()) {
+  sanitizer = "ThreadSanitizer";
+}
+if (sanitizer) {
+  getDriver().Diag(diag::err_drv_unsupported_static_sanitizer_darwin)
+  << sanitizer;
+  return;
+}
   }
 
   if (Sanitize.linkRuntimes()) {
-if (Sanitize.needsAsanRt())
+if (Sanitize.needsAsanRt()) {
+  assert(Sanitize.needsSharedRt() &&
+ "Static sanitizer runtimes not supported");
   AddLinkSanitizerLibArgs(Args, CmdArgs, "asan");
+}
 if (Sanitize.needsLsanRt())
   AddLinkSanitizerLibArgs(Args, CmdArgs, "lsan");
 if (Sanitize.needsUbsanRt()) {
-  assert(Sanitize.needsSharedRt() && "Static sanitizer runtimes not 
supported");
-  AddLinkSanitizerLibArgs(Args, CmdArgs,
-  Sanitize.requiresMinimalRuntime() ? 
"ubsan_minimal"
-: "ubsan");
+  assert(Sanitize.needsSharedRt() &&
+ "Static sanitizer runtimes not supported");
+  AddLinkSanitizerLibArgs(
+  Args, CmdArgs,
+  Sanitize.requiresMinimalRuntime() ? "ubsan_minimal" : "ubsan");
 }
-if (Sanitize.needsTsanRt())
+if (Sanitize.needsTsanRt()) {
+  assert(Sanitize.needsSharedRt() &&
+ "Static sanitizer runtimes not supported");
   AddLinkSanitizerLibArgs(Args, CmdArgs, "tsan");
+}
 if (Sanitize.needsFuzzer() && !Args.hasArg(options::OPT_dynamiclib)) {
   AddLinkSanitizerLibArgs(Args, CmdArgs, "fuzzer", /*shared=*/false);
 

diff  --git a/clang/test/Driver/sanitizer-ld.c 
b/clang/test/Driver/sanitizer-ld.c
index 0ba209d870c2a..910b0ed4ff0de 100644
--- a/clang/test/Driver/sanitizer-ld.c
+++ b/clang/test/