Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions clang/include/clang/Basic/Builtins.td
Original file line number Diff line number Diff line change
Expand Up @@ -5253,3 +5253,9 @@ def CountedByRef : Builtin {
let Attributes = [NoThrow, CustomTypeChecking];
let Prototype = "int(...)";
}

def StaticAnalysisAssume : Builtin {
let Spellings = ["__builtin_static_analysis_assume"];
let Attributes = [NoThrow, Const, Pure, UnevaluatedArguments];
let Prototype = "void(bool)";
}
2 changes: 2 additions & 0 deletions clang/lib/CodeGen/CGBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3733,6 +3733,8 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,

return RValue::get(Result);
}
case Builtin::BI__builtin_static_analysis_assume:
return RValue::get(nullptr);
case Builtin::BI__builtin_prefetch: {
Value *Locality, *RW, *Address = EmitScalarExpr(E->getArg(0));
// FIXME: Technically these constants should of type 'int', yes?
Expand Down
22 changes: 22 additions & 0 deletions clang/test/AST/builtin-static-analysis-assume.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// RUN: %clang_cc1 -ast-dump -triple x86_64-linux-gnu %s \
// RUN: | FileCheck %s --strict-whitespace --check-prefixes=CHECK
//
// Tests with serialization:
// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-pch -o %t %s
// RUN: %clang_cc1 -triple x86_64-linux-gnu -include-pch %t -ast-dump-all /dev/null \
// RUN: | FileCheck %s --strict-whitespace

int fun() {
int x = 0;
__builtin_static_analysis_assume(++x >= 1);
return x;
}

// CHECK: |-CallExpr {{.*}} <line:11:5, col:46> 'void'
// CHECK: | |-ImplicitCastExpr {{.*}} <col:5> 'void (*)(_Bool)' <BuiltinFnToFnPtr>
// CHECK: | | `-DeclRefExpr {{.*}} <col:5> '<builtin fn type>' Function {{.*}} '__builtin_static_analysis_assume' 'void (_Bool)'
// CHECK: | `-ImplicitCastExpr {{.*}} <col:38, col:45> '_Bool' <IntegralToBoolean>
// CHECK: | `-BinaryOperator {{.*}} <col:38, col:45> 'int' '>='
// CHECK: | |-UnaryOperator {{.*}} <col:38, col:40> 'int' prefix '++'
// CHECK: | | `-DeclRefExpr {{.*}} <col:40> 'int' lvalue Var {{.*}} 'x' 'int'
// CHECK: | `-IntegerLiteral {{.*}} <col:45> 'int' 1
17 changes: 17 additions & 0 deletions clang/test/CodeGen/builtin-static-analysis-assume.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 6
// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -o - %s \
// RUN: | FileCheck --check-prefix CHECK %s

// CHECK-LABEL: define dso_local i32 @fun(
// CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
// CHECK-NEXT: [[ENTRY:.*:]]
// CHECK-NEXT: [[X:%.*]] = alloca i32, align 4
// CHECK-NEXT: store i32 0, ptr [[X]], align 4
// CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[X]], align 4
// CHECK-NEXT: ret i32 [[TMP0]]
//
int fun() {
int x = 0;
__builtin_static_analysis_assume(++x >= 1);
return x;
}
14 changes: 14 additions & 0 deletions clang/test/Sema/builtin-static-analysis-assume.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %clang_cc1 %s -fsyntax-only -verify -pedantic

void voidfn();

class Foo{};

int fun() {
int x = 0;
__builtin_static_analysis_assume(true);
__builtin_static_analysis_assume(x <= 0);
__builtin_static_analysis_assume(voidfn()); // expected-error{{cannot initialize a parameter of type 'bool' with an rvalue of type 'void}}
Comment on lines +3 to +11
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also have an example of intfn() too.

Suggested change
void voidfn();
class Foo{};
int fun() {
int x = 0;
__builtin_static_analysis_assume(true);
__builtin_static_analysis_assume(x <= 0);
__builtin_static_analysis_assume(voidfn()); // expected-error{{cannot initialize a parameter of type 'bool' with an rvalue of type 'void}}
void voidfn();
int intfn();
class Foo{};
int fun() {
int x = 0;
__builtin_static_analysis_assume(true);
__builtin_static_analysis_assume(x <= 0);
__builtin_static_analysis_assume(intfn());
__builtin_static_analysis_assume(voidfn()); // expected-error{{cannot initialize a parameter of type 'bool' with an rvalue of type 'void}}

__builtin_static_analysis_assume(Foo()); // expected-error{{no viable conversion from 'Foo' to 'bool'}}
return x;
}