Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2abf91b

Browse files
committed
C++: class and test for clang's __builtin_addressof
1 parent 93103e1 commit 2abf91b

6 files changed

Lines changed: 36 additions & 7 deletions

File tree

cpp/ql/src/semmle/code/cpp/exprs/BuiltInOperations.qll

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,20 @@ class BuiltInOperationBuiltInShuffleVector extends BuiltInOperation, @builtinshu
202202
override string toString() { result = "__builtin_shufflevector" }
203203
}
204204

205+
/**
206+
* A clang `__builtin_addressof` expression (can be used to implement C++'s std::addressof).
207+
*/
208+
class BuiltInOperationBuiltInAddressOf extends UnaryOperation, BuiltInOperation, @builtinaddressof {
209+
/** Gets the function or variable whose address is taken. */
210+
Declaration getAddressable() {
211+
result = this.getOperand().(Access).getTarget()
212+
// this handles the case where we are taking the address of a reference variable
213+
or result = this.getOperand().(ReferenceDereferenceExpr).getChild(0).(Access).getTarget()
214+
}
215+
216+
override string getOperator() { result = "__builtin_addressof" }
217+
}
218+
205219
/**
206220
* The `__is_trivially_constructible` type trait.
207221
*/
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// semmle-extractor-options: --edg --clang
2+
3+
int x = 0;
4+
5+
int* f(void) {
6+
int* x_addr = __builtin_addressof(x);
7+
return x_addr;
8+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| addressof.c:6:17:6:38 | __builtin_addressof ... | addressof.c:6:37:6:37 | x |
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import cpp
2+
3+
from BuiltInOperationBuiltInAddressOf op
4+
select op, op.getOperand()

cpp/ql/test/library-tests/clang_builtin_macros/values.expected

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
| | fp_offset | <no initialiser value> |
2-
| | gp_offset | <no initialiser value> |
3-
| | overflow_arg_area | <no initialiser value> |
4-
| | reg_save_area | <no initialiser value> |
1+
| | fp_offset | <no initialiser expr> |
2+
| | gp_offset | <no initialiser expr> |
3+
| | overflow_arg_area | <no initialiser expr> |
4+
| | reg_save_area | <no initialiser expr> |
5+
| addressof.c | x | 0 |
6+
| addressof.c | x_addr | __builtin_addressof ... |
57
| extended.cpp | has_nullptr_e | 1 |
68
| extended.cpp | has_nullptr_f | 1 |
79
| gcc492.c | has_include | 1 |

cpp/ql/test/library-tests/clang_builtin_macros/values.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import cpp
22

33
string varInit(Variable v) {
4-
if exists(v.getInitializer().getExpr().getValue())
5-
then result = v.getInitializer().getExpr().getValue().toString()
6-
else result = "<no initialiser value>"
4+
if exists(v.getInitializer().getExpr())
5+
then result = v.getInitializer().getExpr().toString()
6+
else result = "<no initialiser expr>"
77
}
88

99
from Variable v

0 commit comments

Comments
 (0)