[clang] [Driver][BoundsSafety] Add -fexperimental-bounds-safety flag (PR #70480)

Yeoul Na via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 9 15:12:32 PST 2023


https://github.com/rapidsna updated https://github.com/llvm/llvm-project/pull/70480

>From 99ec6e055dd32a86bf6d589a6895658dcbe1d7bd Mon Sep 17 00:00:00 2001
From: Yeoul Na <yeoul_na at apple.com>
Date: Fri, 27 Oct 2023 08:34:37 -0700
Subject: [PATCH 01/10] [Driver][BoundsSafety] Add -fbounds-safety-experimental
 flag

-fbounds-safety-experimental is an experimental flag for
-fbounds-safety, which is a bounds-safety extension for C.
-fbounds-safety will require substantial changes across the Clang
codebase. So we introduce this experimental flag is to gate our
incremental patches until we push the essential functionality of
the extension.

-fbounds-safety-experimental currently doesn't do anything but
reporting an error when the flag is used with an unsupported
source language (currently only supports C).
---
 .../clang/Basic/DiagnosticFrontendKinds.td    |  3 +++
 clang/include/clang/Basic/LangOptions.def     |  2 ++
 clang/include/clang/Driver/Options.td         |  8 +++++++
 clang/lib/Driver/ToolChains/Clang.cpp         |  3 +++
 clang/lib/Frontend/CompilerInvocation.cpp     | 23 +++++++++++++++++++
 clang/test/BoundsSafety/Driver/driver.c       |  9 ++++++++
 .../Frontend/only_c_is_supported.c            | 15 ++++++++++++
 7 files changed, 63 insertions(+)
 create mode 100644 clang/test/BoundsSafety/Driver/driver.c
 create mode 100644 clang/test/BoundsSafety/Frontend/only_c_is_supported.c

diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index 715e0c0dc8fa84e..edcbbe992377e12 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -330,6 +330,9 @@ def warn_alias_with_section : Warning<
   "as the %select{aliasee|resolver}2">,
   InGroup<IgnoredAttributes>;
 
+def error_bounds_safety_lang_not_supported : Error<
+  "bounds safety is only supported for C">;
+
 let CategoryName = "Instrumentation Issue" in {
 def warn_profile_data_out_of_date : Warning<
   "profile data may be out of date: of %0 function%s0, %1 %plural{1:has|:have}1"
diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def
index c0ea4ecb9806a5b..222812d876a65f8 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -470,6 +470,8 @@ VALUE_LANGOPT(FuchsiaAPILevel, 32, 0, "Fuchsia API level")
 // on large _BitInts.
 BENIGN_VALUE_LANGOPT(MaxBitIntWidth, 32, 128, "Maximum width of a _BitInt")
 
+LANGOPT(BoundsSafety, 1, 0, "Bounds safety extension for C")
+
 LANGOPT(IncrementalExtensions, 1, 0, " True if we want to process statements"
         "on the global scope, ignore EOF token and continue later on (thus "
         "avoid tearing the Lexer and etc. down). Controlled by "
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 7f3f5125d42e7a9..3eb98c8ee2950a1 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -1732,6 +1732,14 @@ def fswift_async_fp_EQ : Joined<["-"], "fswift-async-fp=">,
     NormalizedValues<["Auto", "Always", "Never"]>,
     MarshallingInfoEnum<CodeGenOpts<"SwiftAsyncFramePointer">, "Always">;
 
+defm bounds_safety : BoolFOption<
+  "bounds-safety-experimental",
+  LangOpts<"BoundsSafety">, DefaultFalse,
+  PosFlag<SetTrue, [], [ClangOption], "Enable">,
+  NegFlag<SetFalse, [], [ClangOption], "Disable">,
+  BothFlags<[], [ClangOption, CC1Option],
+          " experimental bounds safety extension for C">>;
+
 defm addrsig : BoolFOption<"addrsig",
   CodeGenOpts<"Addrsig">, DefaultFalse,
   PosFlag<SetTrue, [], [ClangOption, CC1Option], "Emit">,
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 43a92adbef64ba8..7482b852fb37958 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -6689,6 +6689,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
   Args.addOptOutFlag(CmdArgs, options::OPT_fassume_sane_operator_new,
                      options::OPT_fno_assume_sane_operator_new);
 
+  Args.addOptInFlag(CmdArgs, options::OPT_fbounds_safety,
+                    options::OPT_fno_bounds_safety);
+
   // -fblocks=0 is default.
   if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
                    TC.IsBlocksDefault()) ||
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index fd6c250efeda2a8..f785bd504d63a81 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -3618,6 +3618,23 @@ void CompilerInvocationBase::GenerateLangArgs(const LangOptions &Opts,
     GenerateArg(Consumer, OPT_frandomize_layout_seed_EQ, Opts.RandstructSeed);
 }
 
+static bool SupportsBoundsSafety(Language Lang) {
+  // Currently, bounds safety is only supported for C. However, it's also
+  // possible to pass assembly files and LLVM IR through Clang, and
+  // those should be trivially supported. This is especially important because
+  // some build systems, like xcbuild and somewhat clumsy Makefiles, will pass
+  // C_FLAGS to Clang while building assembly files.
+  switch (Lang) {
+  case Language::Unknown:
+  case Language::Asm:
+  case Language::LLVM_IR:
+  case Language::C:
+    return true;
+  default:
+    return false;
+  }
+}
+
 bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
                                        InputKind IK, const llvm::Triple &T,
                                        std::vector<std::string> &Includes,
@@ -3835,6 +3852,12 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
   Opts.Trigraphs =
       Args.hasFlag(OPT_ftrigraphs, OPT_fno_trigraphs, Opts.Trigraphs);
 
+  Opts.BoundsSafety = Args.hasFlag(OPT_fbounds_safety, OPT_fno_bounds_safety,
+                                   /*Default*/ Opts.BoundsSafety);
+
+  if (Opts.BoundsSafety && !SupportsBoundsSafety(IK.getLanguage()))
+    Diags.Report(diag::error_bounds_safety_lang_not_supported);
+
   Opts.Blocks = Args.hasArg(OPT_fblocks) || (Opts.OpenCL
     && Opts.OpenCLVersion == 200);
 
diff --git a/clang/test/BoundsSafety/Driver/driver.c b/clang/test/BoundsSafety/Driver/driver.c
new file mode 100644
index 000000000000000..91759838d8e1cd0
--- /dev/null
+++ b/clang/test/BoundsSafety/Driver/driver.c
@@ -0,0 +1,9 @@
+// RUN: %clang -c %s -### 2>&1 | not grep fbounds-safety-experimental
+
+// RUN: %clang -fbounds-safety-experimental -### %s 2>&1 | FileCheck -check-prefix T0 %s
+// T0: -fbounds-safety-experimental
+
+// RUN: %clang -fbounds-safety-experimental -fno-bounds-safety-experimental -c %s -### 2>&1 | not grep -e fbounds-safety-experimental
+
+// RUN: %clang -fno-bounds-safety-experimental -fbounds-safety-experimental -c %s -### 2>&1 | FileCheck -check-prefix T1 %s
+// T1: -fbounds-safety-experimental
\ No newline at end of file
diff --git a/clang/test/BoundsSafety/Frontend/only_c_is_supported.c b/clang/test/BoundsSafety/Frontend/only_c_is_supported.c
new file mode 100644
index 000000000000000..fdfc74e8da19878
--- /dev/null
+++ b/clang/test/BoundsSafety/Frontend/only_c_is_supported.c
@@ -0,0 +1,15 @@
+// RUN: not %clang -fbounds-safety-experimental -x c++ %s 2>&1 | FileCheck %s
+
+// RUN: not %clang -fbounds-safety-experimental -x objective-c %s 2>&1 | FileCheck %s
+
+// RUN: not %clang -fbounds-safety-experimental -x objective-c++ %s 2>&1 | FileCheck %s
+
+// RUN: not %clang -cc1 -fbounds-safety-experimental -x c++ %s 2>&1 | FileCheck %s
+
+// RUN: not %clang -cc1 -fbounds-safety-experimental -x objective-c %s 2>&1 | FileCheck %s
+
+// RUN: not %clang -cc1 -fbounds-safety-experimental -x objective-c++ %s 2>&1 | FileCheck %s
+
+// RUN: not %clang -cc1 -fbounds-safety-experimental -x objective-c++ %s 2>&1 | FileCheck %s
+
+// CHECK: error: bounds safety is only supported for C

>From 8049f6f230d43d703db76121c9d160adcf58b7af Mon Sep 17 00:00:00 2001
From: Yeoul Na <yeoul_na at apple.com>
Date: Fri, 27 Oct 2023 16:13:16 -0700
Subject: [PATCH 02/10] Report warning when -fbounds-safety is ignored for
 assembly

Also, LLVM IR and Unknown must be unreachable when parsing the language
arguments.
---
 .../clang/Basic/DiagnosticFrontendKinds.td    |  7 ++++-
 clang/include/clang/Basic/DiagnosticGroups.td |  3 +++
 clang/lib/Frontend/CompilerInvocation.cpp     | 24 ++++++++++-------
 clang/test/BoundsSafety/Driver/driver.c       | 14 +++++-----
 .../BoundsSafety/Frontend/ignored_for_asm.s   |  5 ++++
 .../BoundsSafety/Frontend/ignored_for_ir.ll   |  5 ++++
 .../Frontend/only_c_is_supported.c            | 26 +++++++++++++------
 7 files changed, 59 insertions(+), 25 deletions(-)
 create mode 100644 clang/test/BoundsSafety/Frontend/ignored_for_asm.s
 create mode 100644 clang/test/BoundsSafety/Frontend/ignored_for_ir.ll

diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index edcbbe992377e12..6d87c0dd2a9b1ba 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -330,8 +330,13 @@ def warn_alias_with_section : Warning<
   "as the %select{aliasee|resolver}2">,
   InGroup<IgnoredAttributes>;
 
-def error_bounds_safety_lang_not_supported : Error<
+let CategoryName = "Bounds Safety Issue" in {
+def err_bounds_safety_lang_not_supported : Error<
   "bounds safety is only supported for C">;
+def warn_bounds_safety_asm_ignored : Warning<
+  "'-fbounds-safety' is ignored for assembly">,
+  InGroup<IgnoredBoundsSafety>;
+} // end of bounds safety issue category
 
 let CategoryName = "Instrumentation Issue" in {
 def warn_profile_data_out_of_date : Warning<
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td
index 9a8f3f03b39d165..429450245fb013c 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -1412,6 +1412,9 @@ def FunctionMultiVersioning
 
 def NoDeref : DiagGroup<"noderef">;
 
+// Bounds safety specific warnings
+def IgnoredBoundsSafety : DiagGroup<"ignored-bounds-safety">;
+
 // A group for cross translation unit static analysis related warnings.
 def CrossTU : DiagGroup<"ctu">;
 
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index f785bd504d63a81..6143488a27ee630 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -3618,20 +3618,27 @@ void CompilerInvocationBase::GenerateLangArgs(const LangOptions &Opts,
     GenerateArg(Consumer, OPT_frandomize_layout_seed_EQ, Opts.RandstructSeed);
 }
 
-static bool SupportsBoundsSafety(Language Lang) {
+static void CheckBoundsSafetyLang(InputKind IK, DiagnosticsEngine &Diags) {
   // Currently, bounds safety is only supported for C. However, it's also
   // possible to pass assembly files and LLVM IR through Clang, and
   // those should be trivially supported. This is especially important because
   // some build systems, like xcbuild and somewhat clumsy Makefiles, will pass
   // C_FLAGS to Clang while building assembly files.
-  switch (Lang) {
-  case Language::Unknown:
+  switch (IK.getLanguage()) {
   case Language::Asm:
+    Diags.Report(diag::warn_bounds_safety_asm_ignored);
+    break;
+
+  case Language::Unknown:
   case Language::LLVM_IR:
+    llvm_unreachable("Unexpected file format");
+
   case Language::C:
-    return true;
+    break;
+
   default:
-    return false;
+    Diags.Report(diag::err_bounds_safety_lang_not_supported);
+    break;
   }
 }
 
@@ -3852,11 +3859,8 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
   Opts.Trigraphs =
       Args.hasFlag(OPT_ftrigraphs, OPT_fno_trigraphs, Opts.Trigraphs);
 
-  Opts.BoundsSafety = Args.hasFlag(OPT_fbounds_safety, OPT_fno_bounds_safety,
-                                   /*Default*/ Opts.BoundsSafety);
-
-  if (Opts.BoundsSafety && !SupportsBoundsSafety(IK.getLanguage()))
-    Diags.Report(diag::error_bounds_safety_lang_not_supported);
+  if (Opts.BoundsSafety)
+    CheckBoundsSafetyLang(IK, Diags);
 
   Opts.Blocks = Args.hasArg(OPT_fblocks) || (Opts.OpenCL
     && Opts.OpenCLVersion == 200);
diff --git a/clang/test/BoundsSafety/Driver/driver.c b/clang/test/BoundsSafety/Driver/driver.c
index 91759838d8e1cd0..e17f6a8694c4536 100644
--- a/clang/test/BoundsSafety/Driver/driver.c
+++ b/clang/test/BoundsSafety/Driver/driver.c
@@ -1,9 +1,11 @@
-// RUN: %clang -c %s -### 2>&1 | not grep fbounds-safety-experimental
+// RUN: %clang -c %s -### 2>&1 | FileCheck -check-prefix T0 %s
+// T0-NOT: -fbounds-safety-experimental
 
-// RUN: %clang -fbounds-safety-experimental -### %s 2>&1 | FileCheck -check-prefix T0 %s
-// T0: -fbounds-safety-experimental
+// RUN: %clang -fbounds-safety-experimental -### %s 2>&1 | FileCheck -check-prefix T1 %s
+// T1: -fbounds-safety-experimental
 
-// RUN: %clang -fbounds-safety-experimental -fno-bounds-safety-experimental -c %s -### 2>&1 | not grep -e fbounds-safety-experimental
+// RUN: %clang -fbounds-safety-experimental -fno-bounds-safety-experimental -c %s -### 2>&1 | FileCheck -check-prefix T2 %s
+// T2-NOT: -fbounds-safety-experimental
 
-// RUN: %clang -fno-bounds-safety-experimental -fbounds-safety-experimental -c %s -### 2>&1 | FileCheck -check-prefix T1 %s
-// T1: -fbounds-safety-experimental
\ No newline at end of file
+// RUN: %clang -fno-bounds-safety-experimental -fbounds-safety-experimental -c %s -### 2>&1 | FileCheck -check-prefix T3 %s
+// T3: -fbounds-safety-experimental
\ No newline at end of file
diff --git a/clang/test/BoundsSafety/Frontend/ignored_for_asm.s b/clang/test/BoundsSafety/Frontend/ignored_for_asm.s
new file mode 100644
index 000000000000000..44072730ba92fc4
--- /dev/null
+++ b/clang/test/BoundsSafety/Frontend/ignored_for_asm.s
@@ -0,0 +1,5 @@
+
+// RUN: %clang -fbounds-safety-experimental -fsyntax-only %s 2>&1 | FileCheck %s
+// RUN: %clang_cc1 -fbounds-safety-experimental -fsyntax-only %s 2>&1 | FileCheck %s
+
+// CHECK: warning: '-fbounds-safety' is ignored for assembly
diff --git a/clang/test/BoundsSafety/Frontend/ignored_for_ir.ll b/clang/test/BoundsSafety/Frontend/ignored_for_ir.ll
new file mode 100644
index 000000000000000..33219397301bbb6
--- /dev/null
+++ b/clang/test/BoundsSafety/Frontend/ignored_for_ir.ll
@@ -0,0 +1,5 @@
+; RUN: %clang -fbounds-safety-experimental -x ir -S %s -o /dev/null 2>&1 | FileCheck %s
+; RUN: %clang_cc1 -fbounds-safety-experimental -x ir -S %s -o /dev/null 2>&1 | FileCheck %s
+
+; CHECK-NOT: warning: '-fbounds-safety' is ignored for LLVM IR
+; CHECK-NOT: error: bounds safety is only supported for C
\ No newline at end of file
diff --git a/clang/test/BoundsSafety/Frontend/only_c_is_supported.c b/clang/test/BoundsSafety/Frontend/only_c_is_supported.c
index fdfc74e8da19878..d6c46cbab791888 100644
--- a/clang/test/BoundsSafety/Frontend/only_c_is_supported.c
+++ b/clang/test/BoundsSafety/Frontend/only_c_is_supported.c
@@ -1,15 +1,25 @@
-// RUN: not %clang -fbounds-safety-experimental -x c++ %s 2>&1 | FileCheck %s
+// RUN: not %clang -fbounds-safety-experimental -x c++ %s 2>&1 | FileCheck -check-prefix ERR %s
 
-// RUN: not %clang -fbounds-safety-experimental -x objective-c %s 2>&1 | FileCheck %s
+// RUN: not %clang -fbounds-safety-experimental -x objective-c %s 2>&1 | FileCheck -check-prefix ERR %s
 
-// RUN: not %clang -fbounds-safety-experimental -x objective-c++ %s 2>&1 | FileCheck %s
+// RUN: not %clang -fbounds-safety-experimental -x objective-c++ %s 2>&1 | FileCheck -check-prefix ERR %s
 
-// RUN: not %clang -cc1 -fbounds-safety-experimental -x c++ %s 2>&1 | FileCheck %s
+// RUN: not %clang -fbounds-safety-experimental -x cuda -nocudalib -nocudainc %s 2>&1 | FileCheck -check-prefix ERR %s
 
-// RUN: not %clang -cc1 -fbounds-safety-experimental -x objective-c %s 2>&1 | FileCheck %s
+// RUN: not %clang -fbounds-safety-experimental -x renderscript %s 2>&1 | FileCheck -check-prefix ERR %s
 
-// RUN: not %clang -cc1 -fbounds-safety-experimental -x objective-c++ %s 2>&1 | FileCheck %s
+// RUN: not %clang_cc1 -fbounds-safety-experimental -x c++ %s 2>&1 | FileCheck -check-prefix ERR %s
 
-// RUN: not %clang -cc1 -fbounds-safety-experimental -x objective-c++ %s 2>&1 | FileCheck %s
+// RUN: not %clang_cc1 -fbounds-safety-experimental -x objective-c %s 2>&1 | FileCheck -check-prefix ERR %s
 
-// CHECK: error: bounds safety is only supported for C
+// RUN: not %clang_cc1 -fbounds-safety-experimental -x objective-c++ %s 2>&1 | FileCheck -check-prefix ERR %s
+
+// RUN: not %clang_cc1 -fbounds-safety-experimental -x cuda %s 2>&1 | FileCheck -check-prefix ERR %s
+
+// RUN: not %clang_cc1 -fbounds-safety-experimental -x renderscript %s 2>&1 | FileCheck -check-prefix ERR %s
+
+// ERR: error: bounds safety is only supported for C
+
+// expected-no-diagnostics
+// RUN: %clang -fbounds-safety-experimental -fsyntax-only -Xclang -verify -c -x c %s
+// RUN: %clang_cc1 -fbounds-safety-experimental -fsyntax-only -verify -x c %s
\ No newline at end of file

>From 90410bec85e9384b3e7838e2f5f4f90908612441 Mon Sep 17 00:00:00 2001
From: Yeoul Na <yeoul_na at apple.com>
Date: Wed, 1 Nov 2023 17:25:29 -0700
Subject: [PATCH 03/10] Remove bounds-safety ignored warning, instead let it
 handled by driver by default

---
 .../clang/Basic/DiagnosticFrontendKinds.td    |  3 ---
 clang/lib/Frontend/CompilerInvocation.cpp     |  6 +----
 .../BoundsSafety/Frontend/ignored_for_asm.s   |  5 ----
 .../BoundsSafety/Frontend/ignored_for_ir.ll   |  6 ++---
 .../Frontend/only_c_is_supported.c            | 25 ++++++++-----------
 5 files changed, 15 insertions(+), 30 deletions(-)
 delete mode 100644 clang/test/BoundsSafety/Frontend/ignored_for_asm.s

diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index 6d87c0dd2a9b1ba..f913d36a8004cbc 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -333,9 +333,6 @@ def warn_alias_with_section : Warning<
 let CategoryName = "Bounds Safety Issue" in {
 def err_bounds_safety_lang_not_supported : Error<
   "bounds safety is only supported for C">;
-def warn_bounds_safety_asm_ignored : Warning<
-  "'-fbounds-safety' is ignored for assembly">,
-  InGroup<IgnoredBoundsSafety>;
 } // end of bounds safety issue category
 
 let CategoryName = "Instrumentation Issue" in {
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index 6143488a27ee630..253a00706027e92 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -3625,14 +3625,10 @@ static void CheckBoundsSafetyLang(InputKind IK, DiagnosticsEngine &Diags) {
   // some build systems, like xcbuild and somewhat clumsy Makefiles, will pass
   // C_FLAGS to Clang while building assembly files.
   switch (IK.getLanguage()) {
-  case Language::Asm:
-    Diags.Report(diag::warn_bounds_safety_asm_ignored);
-    break;
-
   case Language::Unknown:
   case Language::LLVM_IR:
     llvm_unreachable("Unexpected file format");
-
+  case Language::Asm:
   case Language::C:
     break;
 
diff --git a/clang/test/BoundsSafety/Frontend/ignored_for_asm.s b/clang/test/BoundsSafety/Frontend/ignored_for_asm.s
deleted file mode 100644
index 44072730ba92fc4..000000000000000
--- a/clang/test/BoundsSafety/Frontend/ignored_for_asm.s
+++ /dev/null
@@ -1,5 +0,0 @@
-
-// RUN: %clang -fbounds-safety-experimental -fsyntax-only %s 2>&1 | FileCheck %s
-// RUN: %clang_cc1 -fbounds-safety-experimental -fsyntax-only %s 2>&1 | FileCheck %s
-
-// CHECK: warning: '-fbounds-safety' is ignored for assembly
diff --git a/clang/test/BoundsSafety/Frontend/ignored_for_ir.ll b/clang/test/BoundsSafety/Frontend/ignored_for_ir.ll
index 33219397301bbb6..7f49a3802b9fffe 100644
--- a/clang/test/BoundsSafety/Frontend/ignored_for_ir.ll
+++ b/clang/test/BoundsSafety/Frontend/ignored_for_ir.ll
@@ -1,5 +1,5 @@
-; RUN: %clang -fbounds-safety-experimental -x ir -S %s -o /dev/null 2>&1 | FileCheck %s
-; RUN: %clang_cc1 -fbounds-safety-experimental -x ir -S %s -o /dev/null 2>&1 | FileCheck %s
+; RUN: %clang -fbounds-safety-experimental -c -x ir -S %s -o /dev/null 2>&1 | FileCheck %s
 
-; CHECK-NOT: warning: '-fbounds-safety' is ignored for LLVM IR
+; The option is silently ignored for LLVM IR. This conforms to the behavior of most other cflags.
+; CHECK-NOT: warning: warning: argument unused during compilation: '-fbounds-safety-experimental'
 ; CHECK-NOT: error: bounds safety is only supported for C
\ No newline at end of file
diff --git a/clang/test/BoundsSafety/Frontend/only_c_is_supported.c b/clang/test/BoundsSafety/Frontend/only_c_is_supported.c
index d6c46cbab791888..5d81263267ad2c8 100644
--- a/clang/test/BoundsSafety/Frontend/only_c_is_supported.c
+++ b/clang/test/BoundsSafety/Frontend/only_c_is_supported.c
@@ -1,25 +1,22 @@
 // RUN: not %clang -fbounds-safety-experimental -x c++ %s 2>&1 | FileCheck -check-prefix ERR %s
-
 // RUN: not %clang -fbounds-safety-experimental -x objective-c %s 2>&1 | FileCheck -check-prefix ERR %s
-
 // RUN: not %clang -fbounds-safety-experimental -x objective-c++ %s 2>&1 | FileCheck -check-prefix ERR %s
-
 // RUN: not %clang -fbounds-safety-experimental -x cuda -nocudalib -nocudainc %s 2>&1 | FileCheck -check-prefix ERR %s
-
 // RUN: not %clang -fbounds-safety-experimental -x renderscript %s 2>&1 | FileCheck -check-prefix ERR %s
 
-// RUN: not %clang_cc1 -fbounds-safety-experimental -x c++ %s 2>&1 | FileCheck -check-prefix ERR %s
-
-// RUN: not %clang_cc1 -fbounds-safety-experimental -x objective-c %s 2>&1 | FileCheck -check-prefix ERR %s
-
-// RUN: not %clang_cc1 -fbounds-safety-experimental -x objective-c++ %s 2>&1 | FileCheck -check-prefix ERR %s
+// ERR: error: bounds safety is only supported for C
 
-// RUN: not %clang_cc1 -fbounds-safety-experimental -x cuda %s 2>&1 | FileCheck -check-prefix ERR %s
+// This reports a warning to follow the default behavior of ClangAs.
+// RUN: %clang -fbounds-safety-experimental -x assembler -c %s -o /dev/null 2>&1 | FileCheck -check-prefix WARN %s
 
-// RUN: not %clang_cc1 -fbounds-safety-experimental -x renderscript %s 2>&1 | FileCheck -check-prefix ERR %s
+// '-x assembler-with-cpp' silently ignores unused options by default.
+// Reporting a warning for -fbounds-safety when is used because preprocessor directives using the feature flag are currently not supported.
+// specific warning instead of ignored?
 
-// ERR: error: bounds safety is only supported for C
+// WARN: warning: argument unused during compilation: '-fbounds-safety-experimental'
 
 // expected-no-diagnostics
-// RUN: %clang -fbounds-safety-experimental -fsyntax-only -Xclang -verify -c -x c %s
-// RUN: %clang_cc1 -fbounds-safety-experimental -fsyntax-only -verify -x c %s
\ No newline at end of file
+// RUN: %clang -fbounds-safety-experimental -Xclang -verify -c -x c %s -o /dev/null
+// RUN: %clang -fbounds-safety-experimental -Xclang -verify -c -x assembler-with-cpp %s -o /dev/null
+
+// '-x ir' test is done in a separate file because it doesn't recognize '//' as the comment prefix
\ No newline at end of file

>From 3155dc9078c371a169cdfad2f7e9e9c23bbda6b6 Mon Sep 17 00:00:00 2001
From: Yeoul Na <yeoul_na at apple.com>
Date: Thu, 2 Nov 2023 11:22:45 -0700
Subject: [PATCH 04/10] Flag name update: -fexperimental-bounds-safety

---
 clang/include/clang/Driver/Options.td          |  2 +-
 clang/lib/Frontend/CompilerInvocation.cpp      |  3 ++-
 clang/test/BoundsSafety/Driver/driver.c        | 14 +++++++-------
 .../BoundsSafety/Frontend/ignored_for_ir.ll    |  4 ++--
 .../Frontend/only_c_is_supported.c             | 18 +++++++++---------
 5 files changed, 21 insertions(+), 20 deletions(-)

diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 3eb98c8ee2950a1..70a47d3d8e079e5 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -1733,7 +1733,7 @@ def fswift_async_fp_EQ : Joined<["-"], "fswift-async-fp=">,
     MarshallingInfoEnum<CodeGenOpts<"SwiftAsyncFramePointer">, "Always">;
 
 defm bounds_safety : BoolFOption<
-  "bounds-safety-experimental",
+  "experimental-bounds-safety",
   LangOpts<"BoundsSafety">, DefaultFalse,
   PosFlag<SetTrue, [], [ClangOption], "Enable">,
   NegFlag<SetFalse, [], [ClangOption], "Disable">,
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index 253a00706027e92..775fb4434cd58a4 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -3623,11 +3623,12 @@ static void CheckBoundsSafetyLang(InputKind IK, DiagnosticsEngine &Diags) {
   // possible to pass assembly files and LLVM IR through Clang, and
   // those should be trivially supported. This is especially important because
   // some build systems, like xcbuild and somewhat clumsy Makefiles, will pass
-  // C_FLAGS to Clang while building assembly files.
+  // C_FLAGS to Clang while building assembly files. 
   switch (IK.getLanguage()) {
   case Language::Unknown:
   case Language::LLVM_IR:
     llvm_unreachable("Unexpected file format");
+  // 'argument unused' warning is reported for assembler in the driver.
   case Language::Asm:
   case Language::C:
     break;
diff --git a/clang/test/BoundsSafety/Driver/driver.c b/clang/test/BoundsSafety/Driver/driver.c
index e17f6a8694c4536..cc082d8cf368baf 100644
--- a/clang/test/BoundsSafety/Driver/driver.c
+++ b/clang/test/BoundsSafety/Driver/driver.c
@@ -1,11 +1,11 @@
 // RUN: %clang -c %s -### 2>&1 | FileCheck -check-prefix T0 %s
-// T0-NOT: -fbounds-safety-experimental
+// T0-NOT: -fexperimental-bounds-safety
 
-// RUN: %clang -fbounds-safety-experimental -### %s 2>&1 | FileCheck -check-prefix T1 %s
-// T1: -fbounds-safety-experimental
+// RUN: %clang -fexperimental-bounds-safety -### %s 2>&1 | FileCheck -check-prefix T1 %s
+// T1: -fexperimental-bounds-safety
 
-// RUN: %clang -fbounds-safety-experimental -fno-bounds-safety-experimental -c %s -### 2>&1 | FileCheck -check-prefix T2 %s
-// T2-NOT: -fbounds-safety-experimental
+// RUN: %clang -fexperimental-bounds-safety -fno-experimental-bounds-safety -c %s -### 2>&1 | FileCheck -check-prefix T2 %s
+// T2-NOT: -fexperimental-bounds-safety
 
-// RUN: %clang -fno-bounds-safety-experimental -fbounds-safety-experimental -c %s -### 2>&1 | FileCheck -check-prefix T3 %s
-// T3: -fbounds-safety-experimental
\ No newline at end of file
+// RUN: %clang -fno-experimental-bounds-safety -fexperimental-bounds-safety -c %s -### 2>&1 | FileCheck -check-prefix T3 %s
+// T3: -fexperimental-bounds-safety
\ No newline at end of file
diff --git a/clang/test/BoundsSafety/Frontend/ignored_for_ir.ll b/clang/test/BoundsSafety/Frontend/ignored_for_ir.ll
index 7f49a3802b9fffe..958a325d5149582 100644
--- a/clang/test/BoundsSafety/Frontend/ignored_for_ir.ll
+++ b/clang/test/BoundsSafety/Frontend/ignored_for_ir.ll
@@ -1,5 +1,5 @@
-; RUN: %clang -fbounds-safety-experimental -c -x ir -S %s -o /dev/null 2>&1 | FileCheck %s
+; RUN: %clang -fexperimental-bounds-safety -c -x ir -S %s -o /dev/null 2>&1 | FileCheck %s
 
 ; The option is silently ignored for LLVM IR. This conforms to the behavior of most other cflags.
-; CHECK-NOT: warning: warning: argument unused during compilation: '-fbounds-safety-experimental'
+; CHECK-NOT: warning: warning: argument unused during compilation: '-fexperimental-bounds-safety'
 ; CHECK-NOT: error: bounds safety is only supported for C
\ No newline at end of file
diff --git a/clang/test/BoundsSafety/Frontend/only_c_is_supported.c b/clang/test/BoundsSafety/Frontend/only_c_is_supported.c
index 5d81263267ad2c8..8f218c30a9e2824 100644
--- a/clang/test/BoundsSafety/Frontend/only_c_is_supported.c
+++ b/clang/test/BoundsSafety/Frontend/only_c_is_supported.c
@@ -1,22 +1,22 @@
-// RUN: not %clang -fbounds-safety-experimental -x c++ %s 2>&1 | FileCheck -check-prefix ERR %s
-// RUN: not %clang -fbounds-safety-experimental -x objective-c %s 2>&1 | FileCheck -check-prefix ERR %s
-// RUN: not %clang -fbounds-safety-experimental -x objective-c++ %s 2>&1 | FileCheck -check-prefix ERR %s
-// RUN: not %clang -fbounds-safety-experimental -x cuda -nocudalib -nocudainc %s 2>&1 | FileCheck -check-prefix ERR %s
-// RUN: not %clang -fbounds-safety-experimental -x renderscript %s 2>&1 | FileCheck -check-prefix ERR %s
+// RUN: not %clang -fexperimental-bounds-safety -x c++ %s 2>&1 | FileCheck -check-prefix ERR %s
+// RUN: not %clang -fexperimental-bounds-safety -x objective-c %s 2>&1 | FileCheck -check-prefix ERR %s
+// RUN: not %clang -fexperimental-bounds-safety -x objective-c++ %s 2>&1 | FileCheck -check-prefix ERR %s
+// RUN: not %clang -fexperimental-bounds-safety -x cuda -nocudalib -nocudainc %s 2>&1 | FileCheck -check-prefix ERR %s
+// RUN: not %clang -fexperimental-bounds-safety -x renderscript %s 2>&1 | FileCheck -check-prefix ERR %s
 
 // ERR: error: bounds safety is only supported for C
 
 // This reports a warning to follow the default behavior of ClangAs.
-// RUN: %clang -fbounds-safety-experimental -x assembler -c %s -o /dev/null 2>&1 | FileCheck -check-prefix WARN %s
+// RUN: %clang -fexperimental-bounds-safety -x assembler -c %s -o /dev/null 2>&1 | FileCheck -check-prefix WARN %s
 
 // '-x assembler-with-cpp' silently ignores unused options by default.
 // Reporting a warning for -fbounds-safety when is used because preprocessor directives using the feature flag are currently not supported.
 // specific warning instead of ignored?
 
-// WARN: warning: argument unused during compilation: '-fbounds-safety-experimental'
+// WARN: warning: argument unused during compilation: '-fexperimental-bounds-safety'
 
 // expected-no-diagnostics
-// RUN: %clang -fbounds-safety-experimental -Xclang -verify -c -x c %s -o /dev/null
-// RUN: %clang -fbounds-safety-experimental -Xclang -verify -c -x assembler-with-cpp %s -o /dev/null
+// RUN: %clang -fexperimental-bounds-safety -Xclang -verify -c -x c %s -o /dev/null
+// RUN: %clang -fexperimental-bounds-safety -Xclang -verify -c -x assembler-with-cpp %s -o /dev/null
 
 // '-x ir' test is done in a separate file because it doesn't recognize '//' as the comment prefix
\ No newline at end of file

>From 5e35c30414517b242a1e68d46ecc5cfae3b75130 Mon Sep 17 00:00:00 2001
From: Yeoul Na <yeoul_na at apple.com>
Date: Thu, 2 Nov 2023 11:38:51 -0700
Subject: [PATCH 05/10] Move tests to follow the existing test layout

---
 .../BoundsSafety/Frontend/ignored_for_ir.ll   |  5 -----
 .../Frontend/only_c_is_supported.c            | 22 -------------------
 clang/test/Driver/bounds-safety-unused.c      | 12 ++++++++++
 .../driver.c => Driver/fbounds-safety.c}      |  0
 .../Frontend/bounds-safety-lang-support.c     |  7 ++++++
 5 files changed, 19 insertions(+), 27 deletions(-)
 delete mode 100644 clang/test/BoundsSafety/Frontend/ignored_for_ir.ll
 delete mode 100644 clang/test/BoundsSafety/Frontend/only_c_is_supported.c
 create mode 100644 clang/test/Driver/bounds-safety-unused.c
 rename clang/test/{BoundsSafety/Driver/driver.c => Driver/fbounds-safety.c} (100%)
 create mode 100644 clang/test/Frontend/bounds-safety-lang-support.c

diff --git a/clang/test/BoundsSafety/Frontend/ignored_for_ir.ll b/clang/test/BoundsSafety/Frontend/ignored_for_ir.ll
deleted file mode 100644
index 958a325d5149582..000000000000000
--- a/clang/test/BoundsSafety/Frontend/ignored_for_ir.ll
+++ /dev/null
@@ -1,5 +0,0 @@
-; RUN: %clang -fexperimental-bounds-safety -c -x ir -S %s -o /dev/null 2>&1 | FileCheck %s
-
-; The option is silently ignored for LLVM IR. This conforms to the behavior of most other cflags.
-; CHECK-NOT: warning: warning: argument unused during compilation: '-fexperimental-bounds-safety'
-; CHECK-NOT: error: bounds safety is only supported for C
\ No newline at end of file
diff --git a/clang/test/BoundsSafety/Frontend/only_c_is_supported.c b/clang/test/BoundsSafety/Frontend/only_c_is_supported.c
deleted file mode 100644
index 8f218c30a9e2824..000000000000000
--- a/clang/test/BoundsSafety/Frontend/only_c_is_supported.c
+++ /dev/null
@@ -1,22 +0,0 @@
-// RUN: not %clang -fexperimental-bounds-safety -x c++ %s 2>&1 | FileCheck -check-prefix ERR %s
-// RUN: not %clang -fexperimental-bounds-safety -x objective-c %s 2>&1 | FileCheck -check-prefix ERR %s
-// RUN: not %clang -fexperimental-bounds-safety -x objective-c++ %s 2>&1 | FileCheck -check-prefix ERR %s
-// RUN: not %clang -fexperimental-bounds-safety -x cuda -nocudalib -nocudainc %s 2>&1 | FileCheck -check-prefix ERR %s
-// RUN: not %clang -fexperimental-bounds-safety -x renderscript %s 2>&1 | FileCheck -check-prefix ERR %s
-
-// ERR: error: bounds safety is only supported for C
-
-// This reports a warning to follow the default behavior of ClangAs.
-// RUN: %clang -fexperimental-bounds-safety -x assembler -c %s -o /dev/null 2>&1 | FileCheck -check-prefix WARN %s
-
-// '-x assembler-with-cpp' silently ignores unused options by default.
-// Reporting a warning for -fbounds-safety when is used because preprocessor directives using the feature flag are currently not supported.
-// specific warning instead of ignored?
-
-// WARN: warning: argument unused during compilation: '-fexperimental-bounds-safety'
-
-// expected-no-diagnostics
-// RUN: %clang -fexperimental-bounds-safety -Xclang -verify -c -x c %s -o /dev/null
-// RUN: %clang -fexperimental-bounds-safety -Xclang -verify -c -x assembler-with-cpp %s -o /dev/null
-
-// '-x ir' test is done in a separate file because it doesn't recognize '//' as the comment prefix
\ No newline at end of file
diff --git a/clang/test/Driver/bounds-safety-unused.c b/clang/test/Driver/bounds-safety-unused.c
new file mode 100644
index 000000000000000..b96083323b21f7a
--- /dev/null
+++ b/clang/test/Driver/bounds-safety-unused.c
@@ -0,0 +1,12 @@
+// This reports a warning to follow the default behavior of ClangAs.
+// RUN: %clang -fexperimental-bounds-safety -x assembler -c %s -o /dev/null 2>&1 | FileCheck -check-prefix WARN %s
+
+
+// WARN: warning: argument unused during compilation: '-fexperimental-bounds-safety'
+
+// expected-no-diagnostics
+// RUN: %clang -fexperimental-bounds-safety -Xclang -verify -c -x c %s -o /dev/null
+// Unlike '-x assembler', '-x assembler-with-cpp' silently ignores unused options by default.
+// XXX: Should report a targeted warning in future when assembler tries to use preprocessor directives to conditionalize behavior when bounds safety is enabled.
+// RUN: %clang -fexperimental-bounds-safety -Xclang -verify -c -x assembler-with-cpp %s -o /dev/null
+// RUN: %clang -### -x ir -fexperimental-bounds-safety %s -Xclang -verify -o /dev/null
diff --git a/clang/test/BoundsSafety/Driver/driver.c b/clang/test/Driver/fbounds-safety.c
similarity index 100%
rename from clang/test/BoundsSafety/Driver/driver.c
rename to clang/test/Driver/fbounds-safety.c
diff --git a/clang/test/Frontend/bounds-safety-lang-support.c b/clang/test/Frontend/bounds-safety-lang-support.c
new file mode 100644
index 000000000000000..097c8408789b53f
--- /dev/null
+++ b/clang/test/Frontend/bounds-safety-lang-support.c
@@ -0,0 +1,7 @@
+// RUN: not %clang_cc1 -fexperimental-bounds-safety -x c++ %s 2>&1 | FileCheck -check-prefix ERR %s
+// RUN: not %clang_cc1 -fexperimental-bounds-safety -x objective-c %s 2>&1 | FileCheck -check-prefix ERR %s
+// RUN: not %clang_cc1 -fexperimental-bounds-safety -x objective-c++ %s 2>&1 | FileCheck -check-prefix ERR %s
+// RUN: not %clang_cc1 -fexperimental-bounds-safety -x cuda -nocudalib -nocudainc %s 2>&1 | FileCheck -check-prefix ERR %s
+// RUN: not %clang_cc1 -fexperimental-bounds-safety -x renderscript %s 2>&1 | FileCheck -check-prefix ERR %s
+
+// ERR: error: bounds safety is only supported for C

>From 7bb2a9321ca093a66990fd6ad56142c23c476d1b Mon Sep 17 00:00:00 2001
From: Yeoul Na <yeoul_na at apple.com>
Date: Thu, 2 Nov 2023 16:08:34 -0700
Subject: [PATCH 06/10] clang-format

---
 clang/lib/Frontend/CompilerInvocation.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index 775fb4434cd58a4..17946ca8a2e2e89 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -3623,7 +3623,7 @@ static void CheckBoundsSafetyLang(InputKind IK, DiagnosticsEngine &Diags) {
   // possible to pass assembly files and LLVM IR through Clang, and
   // those should be trivially supported. This is especially important because
   // some build systems, like xcbuild and somewhat clumsy Makefiles, will pass
-  // C_FLAGS to Clang while building assembly files. 
+  // C_FLAGS to Clang while building assembly files.
   switch (IK.getLanguage()) {
   case Language::Unknown:
   case Language::LLVM_IR:

>From c739327a5b3c6b223e39bd30e2ccd40a5b69aba5 Mon Sep 17 00:00:00 2001
From: Yeoul Na <yeoul_na at apple.com>
Date: Tue, 7 Nov 2023 12:41:14 -0800
Subject: [PATCH 07/10] Remove unused diag group

---
 clang/include/clang/Basic/DiagnosticGroups.td | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td
index 429450245fb013c..9a8f3f03b39d165 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -1412,9 +1412,6 @@ def FunctionMultiVersioning
 
 def NoDeref : DiagGroup<"noderef">;
 
-// Bounds safety specific warnings
-def IgnoredBoundsSafety : DiagGroup<"ignored-bounds-safety">;
-
 // A group for cross translation unit static analysis related warnings.
 def CrossTU : DiagGroup<"ctu">;
 

>From b0774f14df25811209563f8f6619682d5a13f318 Mon Sep 17 00:00:00 2001
From: Yeoul Na <yeoul_na at apple.com>
Date: Tue, 7 Nov 2023 12:44:16 -0800
Subject: [PATCH 08/10] Remove superfluous comment about ignored options

---
 clang/lib/Frontend/CompilerInvocation.cpp | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index 17946ca8a2e2e89..005a1aff236c8ad 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -3619,11 +3619,8 @@ void CompilerInvocationBase::GenerateLangArgs(const LangOptions &Opts,
 }
 
 static void CheckBoundsSafetyLang(InputKind IK, DiagnosticsEngine &Diags) {
-  // Currently, bounds safety is only supported for C. However, it's also
-  // possible to pass assembly files and LLVM IR through Clang, and
-  // those should be trivially supported. This is especially important because
-  // some build systems, like xcbuild and somewhat clumsy Makefiles, will pass
-  // C_FLAGS to Clang while building assembly files.
+  // Currently, bounds safety is only supported for C and it's not supported
+  // in other languages, such as C++, Obj-C, and Obj-C++.
   switch (IK.getLanguage()) {
   case Language::Unknown:
   case Language::LLVM_IR:

>From f125532235d5120027ec261204ae09d315d0fa14 Mon Sep 17 00:00:00 2001
From: Yeoul Na <yeoul_na at apple.com>
Date: Thu, 9 Nov 2023 14:51:14 -0800
Subject: [PATCH 09/10] Make it a driver error to use
 -fexperimental-bounds-safety with unsupported language

---
 .../clang/Basic/DiagnosticDriverKinds.td      |  5 ++++
 .../clang/Basic/DiagnosticFrontendKinds.td    |  5 ----
 clang/include/clang/Driver/Types.h            |  6 ++++
 clang/lib/Driver/ToolChains/Clang.cpp         | 15 ++++++++--
 clang/lib/Driver/Types.cpp                    | 13 ++++++++
 clang/lib/Frontend/CompilerInvocation.cpp     |  2 +-
 clang/test/Driver/bounds-safety-unused.c      | 12 --------
 clang/test/Driver/fbounds-safety.c            | 30 ++++++++++++++-----
 .../Frontend/bounds-safety-lang-support.c     |  2 +-
 9 files changed, 61 insertions(+), 29 deletions(-)
 delete mode 100644 clang/test/Driver/bounds-safety-unused.c

diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index c0ccd64a2a7b82e..a3dd208c9b72722 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -255,6 +255,11 @@ def err_drv_cannot_read_config_file : Error<
 def err_drv_arg_requires_bitcode_input: Error<
   "option '%0' requires input to be LLVM bitcode">;
 
+let CategoryName = "Bounds Safety Issue" in {
+def err_drv_bounds_safety_lang_not_supported : Error<
+  "'-fexperimental-bounds-safety' is only supported for C">;
+} // end of bounds safety issue category
+
 def err_target_unsupported_arch
   : Error<"the target architecture '%0' is not supported by the target '%1'">;
 def err_cpu_unsupported_isa
diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index f913d36a8004cbc..715e0c0dc8fa84e 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -330,11 +330,6 @@ def warn_alias_with_section : Warning<
   "as the %select{aliasee|resolver}2">,
   InGroup<IgnoredAttributes>;
 
-let CategoryName = "Bounds Safety Issue" in {
-def err_bounds_safety_lang_not_supported : Error<
-  "bounds safety is only supported for C">;
-} // end of bounds safety issue category
-
 let CategoryName = "Instrumentation Issue" in {
 def warn_profile_data_out_of_date : Warning<
   "profile data may be out of date: of %0 function%s0, %1 %plural{1:has|:have}1"
diff --git a/clang/include/clang/Driver/Types.h b/clang/include/clang/Driver/Types.h
index 121b58a6b477d9b..fefd0845e8e713c 100644
--- a/clang/include/clang/Driver/Types.h
+++ b/clang/include/clang/Driver/Types.h
@@ -80,6 +80,12 @@ namespace types {
   /// isCXX - Is this a "C++" input (C++ and Obj-C++ sources and headers).
   bool isCXX(ID Id);
 
+  /// isC - Is this a C input (C sources and headers).
+  bool isC(ID Id);
+
+  /// isAsm - Is this an assembler input.
+  bool isAsm(ID Id);
+
   /// Is this LLVM IR.
   bool isLLVMIR(ID Id);
 
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 7482b852fb37958..4c48c00492e2872 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -6689,8 +6689,19 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
   Args.addOptOutFlag(CmdArgs, options::OPT_fassume_sane_operator_new,
                      options::OPT_fno_assume_sane_operator_new);
 
-  Args.addOptInFlag(CmdArgs, options::OPT_fbounds_safety,
-                    options::OPT_fno_bounds_safety);
+  // -fexperimental-bounds-safety is only supported for C. The option is
+  // silently ignored for Asm and LLVM inputs. Report an error for the rest of
+  // unsupported languages.
+  if (Args.hasFlag(options::OPT_fbounds_safety, options::OPT_fno_bounds_safety,
+                   false)) {
+    if (llvm::any_of(Inputs, [](const InputInfo &Input) {
+          auto InputType = Input.getType();
+          return !isC(InputType) && !isAsm(InputType) && !isLLVMIR(InputType);
+        })) {
+      D.Diag(diag::err_drv_bounds_safety_lang_not_supported);
+    }
+    CmdArgs.push_back("-fexperimental-bounds-safety");
+  }
 
   // -fblocks=0 is default.
   if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
diff --git a/clang/lib/Driver/Types.cpp b/clang/lib/Driver/Types.cpp
index 08df34ade7b6530..3481eae0c778cfa 100644
--- a/clang/lib/Driver/Types.cpp
+++ b/clang/lib/Driver/Types.cpp
@@ -227,6 +227,19 @@ bool types::isObjC(ID Id) {
 
 bool types::isOpenCL(ID Id) { return Id == TY_CL || Id == TY_CLCXX; }
 
+bool types::isC(ID Id) {
+  switch (Id) {
+  default:
+    return false;
+  
+  case TY_C: case TY_PP_C:
+  case TY_CHeader: case TY_PP_CHeader:
+    return true;
+  }
+}
+
+bool types::isAsm(ID Id) { return Id == TY_Asm || Id == TY_PP_Asm; }
+
 bool types::isCXX(ID Id) {
   switch (Id) {
   default:
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index 005a1aff236c8ad..8f74f408edc6021 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -3631,7 +3631,7 @@ static void CheckBoundsSafetyLang(InputKind IK, DiagnosticsEngine &Diags) {
     break;
 
   default:
-    Diags.Report(diag::err_bounds_safety_lang_not_supported);
+    Diags.Report(diag::err_drv_bounds_safety_lang_not_supported);
     break;
   }
 }
diff --git a/clang/test/Driver/bounds-safety-unused.c b/clang/test/Driver/bounds-safety-unused.c
deleted file mode 100644
index b96083323b21f7a..000000000000000
--- a/clang/test/Driver/bounds-safety-unused.c
+++ /dev/null
@@ -1,12 +0,0 @@
-// This reports a warning to follow the default behavior of ClangAs.
-// RUN: %clang -fexperimental-bounds-safety -x assembler -c %s -o /dev/null 2>&1 | FileCheck -check-prefix WARN %s
-
-
-// WARN: warning: argument unused during compilation: '-fexperimental-bounds-safety'
-
-// expected-no-diagnostics
-// RUN: %clang -fexperimental-bounds-safety -Xclang -verify -c -x c %s -o /dev/null
-// Unlike '-x assembler', '-x assembler-with-cpp' silently ignores unused options by default.
-// XXX: Should report a targeted warning in future when assembler tries to use preprocessor directives to conditionalize behavior when bounds safety is enabled.
-// RUN: %clang -fexperimental-bounds-safety -Xclang -verify -c -x assembler-with-cpp %s -o /dev/null
-// RUN: %clang -### -x ir -fexperimental-bounds-safety %s -Xclang -verify -o /dev/null
diff --git a/clang/test/Driver/fbounds-safety.c b/clang/test/Driver/fbounds-safety.c
index cc082d8cf368baf..f25aff4d7614668 100644
--- a/clang/test/Driver/fbounds-safety.c
+++ b/clang/test/Driver/fbounds-safety.c
@@ -1,11 +1,25 @@
-// RUN: %clang -c %s -### 2>&1 | FileCheck -check-prefix T0 %s
-// T0-NOT: -fexperimental-bounds-safety
+// RUN: %clang -c %s -### 2>&1 | FileCheck -check-prefix NOFLAG %s
+// RUN: %clang -fexperimental-bounds-safety -fno-experimental-bounds-safety -c %s -### 2>&1 | FileCheck -check-prefix NOFLAG %s
+// NOFLAG-NOT: -fexperimental-bounds-safety
 
-// RUN: %clang -fexperimental-bounds-safety -### %s 2>&1 | FileCheck -check-prefix T1 %s
-// T1: -fexperimental-bounds-safety
+// RUN: %clang -fexperimental-bounds-safety -### %s 2>&1 | FileCheck -check-prefix FLAG %s
+// RUN: %clang -fno-experimental-bounds-safety -fexperimental-bounds-safety -c %s -### 2>&1 | FileCheck -check-prefix FLAG %s
+// FLAG: -fexperimental-bounds-safety
 
-// RUN: %clang -fexperimental-bounds-safety -fno-experimental-bounds-safety -c %s -### 2>&1 | FileCheck -check-prefix T2 %s
-// T2-NOT: -fexperimental-bounds-safety
+// RUN: not %clang -fexperimental-bounds-safety -x c++ %s 2>&1 | FileCheck -check-prefix ERR %s
+// RUN: not %clang -fexperimental-bounds-safety -x objective-c %s 2>&1 | FileCheck -check-prefix ERR %s
+// RUN: not %clang -fexperimental-bounds-safety -x objective-c++ %s 2>&1 | FileCheck -check-prefix ERR %s
+// RUN: not %clang -fexperimental-bounds-safety -x cuda -nocudalib -nocudainc %s 2>&1 | FileCheck -check-prefix ERR %s
+// RUN: not %clang -fexperimental-bounds-safety -x renderscript %s 2>&1 | FileCheck -check-prefix ERR %s
+// ERR: error: '-fexperimental-bounds-safety' is only supported for C
 
-// RUN: %clang -fno-experimental-bounds-safety -fexperimental-bounds-safety -c %s -### 2>&1 | FileCheck -check-prefix T3 %s
-// T3: -fexperimental-bounds-safety
\ No newline at end of file
+// This reports a warning to follow the default behavior of ClangAs.
+// RUN: %clang -fexperimental-bounds-safety -x assembler -c %s -o /dev/null 2>&1 | FileCheck -check-prefix WARN %s
+// WARN: warning: argument unused during compilation: '-fexperimental-bounds-safety'
+
+// expected-no-diagnostics
+// RUN: %clang -fexperimental-bounds-safety -Xclang -verify -c -x c %s -o /dev/null
+// Unlike '-x assembler', '-x assembler-with-cpp' silently ignores unused options by default.
+// XXX: Should report a targeted warning in future when assembler tries to use preprocessor directives to conditionalize behavior when bounds safety is enabled.
+// RUN: %clang -fexperimental-bounds-safety -Xclang -verify -c -x assembler-with-cpp %s -o /dev/null
+// RUN: %clang -### -x ir -fexperimental-bounds-safety %s -Xclang -verify -o /dev/null
diff --git a/clang/test/Frontend/bounds-safety-lang-support.c b/clang/test/Frontend/bounds-safety-lang-support.c
index 097c8408789b53f..3ae6c6eaecd25be 100644
--- a/clang/test/Frontend/bounds-safety-lang-support.c
+++ b/clang/test/Frontend/bounds-safety-lang-support.c
@@ -4,4 +4,4 @@
 // RUN: not %clang_cc1 -fexperimental-bounds-safety -x cuda -nocudalib -nocudainc %s 2>&1 | FileCheck -check-prefix ERR %s
 // RUN: not %clang_cc1 -fexperimental-bounds-safety -x renderscript %s 2>&1 | FileCheck -check-prefix ERR %s
 
-// ERR: error: bounds safety is only supported for C
+// ERR: error: '-fexperimental-bounds-safety' is only supported for C

>From 05cd678c72341f7b0b152f1c4162709306e2873f Mon Sep 17 00:00:00 2001
From: Yeoul Na <yeoul_na at apple.com>
Date: Thu, 9 Nov 2023 15:10:30 -0800
Subject: [PATCH 10/10] clang format

---
 clang/lib/Driver/Types.cpp | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Driver/Types.cpp b/clang/lib/Driver/Types.cpp
index 3481eae0c778cfa..aaa3d57c2476e8f 100644
--- a/clang/lib/Driver/Types.cpp
+++ b/clang/lib/Driver/Types.cpp
@@ -231,9 +231,11 @@ bool types::isC(ID Id) {
   switch (Id) {
   default:
     return false;
-  
-  case TY_C: case TY_PP_C:
-  case TY_CHeader: case TY_PP_CHeader:
+
+  case TY_C:
+  case TY_PP_C:
+  case TY_CHeader:
+  case TY_PP_CHeader:
     return true;
   }
 }



More information about the cfe-commits mailing list