Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Driver][BoundsSafety] Add -fexperimental-bounds-safety flag #70480

Closed
wants to merge 11 commits into from
Prev Previous commit
Next Next commit
Make it a driver error to use -fexperimental-bounds-safety with unsup…
…ported language
  • Loading branch information
rapidsna committed Nov 9, 2023
commit f125532235d5120027ec261204ae09d315d0fa14
5 changes: 5 additions & 0 deletions clang/include/clang/Basic/DiagnosticDriverKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions clang/include/clang/Basic/DiagnosticFrontendKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions clang/include/clang/Driver/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
15 changes: 13 additions & 2 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions clang/lib/Driver/Types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
12 changes: 0 additions & 12 deletions clang/test/Driver/bounds-safety-unused.c

This file was deleted.

30 changes: 22 additions & 8 deletions clang/test/Driver/fbounds-safety.c
Original file line number Diff line number Diff line change
@@ -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
// 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
2 changes: 1 addition & 1 deletion clang/test/Frontend/bounds-safety-lang-support.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading