Skip to content

Commit 1eb804a

Browse files
authored
Merge pull request github#6496 from sashabu/sashabu/c_linkage
C++: Allow querying for "C" language linkage on routine types.
2 parents 48818eb + 3ba308a commit 1eb804a

File tree

7 files changed

+29
-0
lines changed

7 files changed

+29
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lgtm,codescanning
2+
* Added `RoutineType.hasCLinkage` predicate to check whether a function type has "C" language linkage.

cpp/ql/lib/semmle/code/cpp/Type.qll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,6 +1621,19 @@ class RoutineType extends Type, @routinetype {
16211621
*/
16221622
Type getReturnType() { routinetypes(underlyingElement(this), unresolveElement(result)) }
16231623

1624+
/**
1625+
* Holds if this function type has "C" language linkage.
1626+
*
1627+
* This includes any function declared in a C source file, or explicitly marked as having "C" linkage:
1628+
* ```
1629+
* extern "C" void f();
1630+
* extern "C" {
1631+
* void g();
1632+
* }
1633+
* ```
1634+
*/
1635+
predicate hasCLinkage() { this.hasSpecifier("c_linkage") }
1636+
16241637
override string explain() {
16251638
result =
16261639
"function returning {" + this.getReturnType().explain() + "} with arguments (" +

cpp/ql/test/library-tests/clang_ms/element.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
| file://:0:0:0:0 | auto |
6767
| file://:0:0:0:0 | auto |
6868
| file://:0:0:0:0 | bool |
69+
| file://:0:0:0:0 | c_linkage |
6970
| file://:0:0:0:0 | char |
7071
| file://:0:0:0:0 | char8_t |
7172
| file://:0:0:0:0 | char16_t |

cpp/ql/test/library-tests/conditions/elements.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
| file://:0:0:0:0 | auto |
4141
| file://:0:0:0:0 | auto |
4242
| file://:0:0:0:0 | bool |
43+
| file://:0:0:0:0 | c_linkage |
4344
| file://:0:0:0:0 | char |
4445
| file://:0:0:0:0 | char8_t |
4546
| file://:0:0:0:0 | char16_t |

cpp/ql/test/library-tests/specifiers2/specifiers2.expected

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,19 @@
1717
| Function | cpp20.cpp:7:19:7:40 | member_const_constexpr | member_const_constexpr | inline |
1818
| Function | cpp20.cpp:7:19:7:40 | member_const_constexpr | member_const_constexpr | is_constexpr |
1919
| Function | cpp20.cpp:7:19:7:40 | member_const_constexpr | member_const_constexpr | private |
20+
| Function | specifiers2.c:11:6:11:6 | f | f | c_linkage |
2021
| Function | specifiers2.c:11:6:11:6 | f | f | extern |
22+
| Function | specifiers2.c:12:13:12:13 | f | f | c_linkage |
2123
| Function | specifiers2.c:12:13:12:13 | f | f | extern |
24+
| Function | specifiers2.c:13:13:13:13 | f | f | c_linkage |
2225
| Function | specifiers2.c:13:13:13:13 | f | f | extern |
26+
| Function | specifiers2.c:15:13:15:13 | g | g | c_linkage |
2327
| Function | specifiers2.c:15:13:15:13 | g | g | extern |
28+
| Function | specifiers2.c:16:13:16:13 | g | g | c_linkage |
2429
| Function | specifiers2.c:16:13:16:13 | g | g | extern |
30+
| Function | specifiers2.c:21:6:21:12 | somefun | somefun | c_linkage |
2531
| Function | specifiers2.c:21:6:21:12 | somefun | somefun | extern |
32+
| Function | specifiers2.c:25:12:25:14 | add | add | c_linkage |
2633
| Function | specifiers2.c:25:12:25:14 | add | add | inline |
2734
| Function | specifiers2pp.cpp:8:7:8:7 | MyClass | MyClass | extern |
2835
| Function | specifiers2pp.cpp:8:7:8:7 | MyClass | MyClass | extern |
@@ -96,8 +103,11 @@
96103
| Function | specifiers2pp.cpp:35:7:35:7 | operator= | operator= | public |
97104
| Function | specifiers2pp.cpp:35:7:35:7 | operator= | operator= | public |
98105
| Function | specifiers2pp.cpp:40:12:40:18 | someFun | someFun | extern |
106+
| Function | specifiers2pp.cpp:41:16:41:23 | someFun2 | someFun2 | c_linkage |
99107
| Function | specifiers2pp.cpp:41:16:41:23 | someFun2 | someFun2 | extern |
108+
| Function | specifiers2pp.cpp:43:9:43:16 | someFun3 | someFun3 | c_linkage |
100109
| Function | specifiers2pp.cpp:43:9:43:16 | someFun3 | someFun3 | extern |
110+
| Function | specifiers2pp.cpp:44:16:44:23 | someFun4 | someFun4 | c_linkage |
101111
| Function | specifiers2pp.cpp:44:16:44:23 | someFun4 | someFun4 | static |
102112
| Function | specifiers2pp.cpp:62:7:62:7 | operator= | operator= | extern |
103113
| Function | specifiers2pp.cpp:62:7:62:7 | operator= | operator= | extern |

cpp/ql/test/library-tests/templates/instantiations_functions/elements.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
| file://:0:0:0:0 | auto |
7171
| file://:0:0:0:0 | auto |
7272
| file://:0:0:0:0 | bool |
73+
| file://:0:0:0:0 | c_linkage |
7374
| file://:0:0:0:0 | char |
7475
| file://:0:0:0:0 | char8_t |
7576
| file://:0:0:0:0 | char16_t |

cpp/ql/test/library-tests/unnamed/elements.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
| file://:0:0:0:0 | auto | Other |
3535
| file://:0:0:0:0 | auto | Other |
3636
| file://:0:0:0:0 | bool | Other |
37+
| file://:0:0:0:0 | c_linkage | Other |
3738
| file://:0:0:0:0 | char | Other |
3839
| file://:0:0:0:0 | char8_t | Other |
3940
| file://:0:0:0:0 | char16_t | Other |

0 commit comments

Comments
 (0)