mirror of
https://github.com/github/codeql.git
synced 2025-12-21 19:26:31 +01:00
Merge pull request #1860 from matt-gretton-dann/add-using-aliases
Add support for using aliases
This commit is contained in:
@@ -3,11 +3,17 @@ private import semmle.code.cpp.internal.ResolveClass
|
||||
|
||||
/**
|
||||
* A C/C++ typedef type. See 4.9.1.
|
||||
*
|
||||
* Represents either of the following typedef styles:
|
||||
*
|
||||
* * CTypedefType: typedef <type> <name>;
|
||||
* * UsingAliasTypedefType: using <name> = <type>;
|
||||
*/
|
||||
class TypedefType extends UserType {
|
||||
TypedefType() { usertypes(underlyingElement(this), _, 5) }
|
||||
|
||||
override string getCanonicalQLClass() { result = "TypedefType" }
|
||||
TypedefType() {
|
||||
usertypes(underlyingElement(this), _, 5) or
|
||||
usertypes(underlyingElement(this), _, 14)
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the base type of this typedef type.
|
||||
@@ -26,10 +32,6 @@ class TypedefType extends UserType {
|
||||
result = this.getBaseType().getPointerIndirectionLevel()
|
||||
}
|
||||
|
||||
override string explain() {
|
||||
result = "typedef {" + this.getBaseType().explain() + "} as \"" + this.getName() + "\""
|
||||
}
|
||||
|
||||
override predicate isDeeplyConst() { this.getBaseType().isDeeplyConst() } // Just an alias
|
||||
|
||||
override predicate isDeeplyConstBelow() { this.getBaseType().isDeeplyConstBelow() } // Just an alias
|
||||
@@ -45,6 +47,32 @@ class TypedefType extends UserType {
|
||||
override Type stripType() { result = getBaseType().stripType() }
|
||||
}
|
||||
|
||||
/**
|
||||
* A traditional C/C++ typedef type. See 4.9.1.
|
||||
*/
|
||||
class CTypedefType extends TypedefType {
|
||||
CTypedefType() { usertypes(underlyingElement(this), _, 5) }
|
||||
|
||||
override string getCanonicalQLClass() { result = "CTypedefType" }
|
||||
|
||||
override string explain() {
|
||||
result = "typedef {" + this.getBaseType().explain() + "} as \"" + this.getName() + "\""
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A using alias C++ typedef type.
|
||||
*/
|
||||
class UsingAliasTypedefType extends TypedefType {
|
||||
UsingAliasTypedefType() { usertypes(underlyingElement(this), _, 14) }
|
||||
|
||||
override string getCanonicalQLClass() { result = "UsingAliasTypedefType" }
|
||||
|
||||
override string explain() {
|
||||
result = "using {" + this.getBaseType().explain() + "} as \"" + this.getName() + "\""
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A C++ typedef type that is directly enclosed by a function.
|
||||
*/
|
||||
|
||||
@@ -675,7 +675,7 @@ decltypes(
|
||||
| 2 = class
|
||||
| 3 = union
|
||||
| 4 = enum
|
||||
| 5 = typedef
|
||||
| 5 = typedef // classic C: typedef typedef type name
|
||||
| 6 = template
|
||||
| 7 = template_parameter
|
||||
| 8 = template_template_parameter
|
||||
@@ -684,6 +684,7 @@ decltypes(
|
||||
// ... 11 objc_protocol deprecated
|
||||
// ... 12 objc_category deprecated
|
||||
| 13 = scoped_enum
|
||||
| 14 = using_alias // a using name = type style typedef
|
||||
;
|
||||
*/
|
||||
usertypes(
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -917,7 +917,7 @@ Varargs.c:
|
||||
# 8| body: [Block] { ... }
|
||||
# 9| 0: [DeclStmt] declaration
|
||||
# 9| 0: [VariableDeclarationEntry] definition of args
|
||||
# 9| Type = [TypedefType] va_list
|
||||
# 9| Type = [CTypedefType] va_list
|
||||
# 10| 1: [ExprStmt] ExprStmt
|
||||
# 10| 0: [BuiltInVarArgsStart] __builtin_va_start
|
||||
# 10| Type = [VoidType] void
|
||||
@@ -926,7 +926,7 @@ Varargs.c:
|
||||
# 10| Type = [PointerType] __va_list_tag *
|
||||
# 10| ValueCategory = prvalue
|
||||
# 10| expr: [VariableAccess] args
|
||||
# 10| Type = [TypedefType] va_list
|
||||
# 10| Type = [CTypedefType] va_list
|
||||
# 10| ValueCategory = lvalue
|
||||
# 10| 1: [VariableAccess] text
|
||||
# 10| Type = [PointerType] const char *
|
||||
@@ -939,7 +939,7 @@ Varargs.c:
|
||||
# 11| Type = [PointerType] __va_list_tag *
|
||||
# 11| ValueCategory = prvalue
|
||||
# 11| expr: [VariableAccess] args
|
||||
# 11| Type = [TypedefType] va_list
|
||||
# 11| Type = [CTypedefType] va_list
|
||||
# 11| ValueCategory = lvalue
|
||||
# 12| 3: [ReturnStmt] return ...
|
||||
macro_etc.c:
|
||||
|
||||
@@ -3672,7 +3672,7 @@ ir.cpp:
|
||||
# 560| [TopLevelFunction] int EnumSwitch(E)
|
||||
# 560| params:
|
||||
# 560| 0: [Parameter] e
|
||||
# 560| Type = [TypedefType] E
|
||||
# 560| Type = [CTypedefType] E
|
||||
# 560| body: [Block] { ... }
|
||||
# 561| 0: [SwitchStmt] switch (...) ...
|
||||
# 561| 0: [CStyleCast] (int)...
|
||||
@@ -3680,7 +3680,7 @@ ir.cpp:
|
||||
# 561| Type = [IntType] int
|
||||
# 561| ValueCategory = prvalue
|
||||
# 561| expr: [VariableAccess] e
|
||||
# 561| Type = [TypedefType] E
|
||||
# 561| Type = [CTypedefType] E
|
||||
# 561| ValueCategory = prvalue(load)
|
||||
# 561| 1: [Block] { ... }
|
||||
# 562| 0: [SwitchCase] case ...:
|
||||
@@ -6191,19 +6191,19 @@ ir.cpp:
|
||||
# 915| [Operator,TopLevelFunction] void* operator new(size_t, float)
|
||||
# 915| params:
|
||||
# 915| 0: [Parameter] p#0
|
||||
# 915| Type = [Size_t,TypedefType] size_t
|
||||
# 915| Type = [CTypedefType,Size_t] size_t
|
||||
# 915| 1: [Parameter] p#1
|
||||
# 915| Type = [FloatType] float
|
||||
# 916| [Operator,TopLevelFunction] void* operator new[](size_t, float)
|
||||
# 916| params:
|
||||
# 916| 0: [Parameter] p#0
|
||||
# 916| Type = [Size_t,TypedefType] size_t
|
||||
# 916| Type = [CTypedefType,Size_t] size_t
|
||||
# 916| 1: [Parameter] p#1
|
||||
# 916| Type = [FloatType] float
|
||||
# 917| [Operator,TopLevelFunction] void* operator new(size_t, std::align_val_t, float)
|
||||
# 917| params:
|
||||
# 917| 0: [Parameter] p#0
|
||||
# 917| Type = [Size_t,TypedefType] size_t
|
||||
# 917| Type = [CTypedefType,Size_t] size_t
|
||||
# 917| 1: [Parameter] p#1
|
||||
# 917| Type = [ScopedEnum] align_val_t
|
||||
# 917| 2: [Parameter] p#2
|
||||
@@ -6211,7 +6211,7 @@ ir.cpp:
|
||||
# 918| [Operator,TopLevelFunction] void* operator new[](size_t, std::align_val_t, float)
|
||||
# 918| params:
|
||||
# 918| 0: [Parameter] p#0
|
||||
# 918| Type = [Size_t,TypedefType] size_t
|
||||
# 918| Type = [CTypedefType,Size_t] size_t
|
||||
# 918| 1: [Parameter] p#1
|
||||
# 918| Type = [ScopedEnum] align_val_t
|
||||
# 918| 2: [Parameter] p#2
|
||||
@@ -6255,23 +6255,23 @@ ir.cpp:
|
||||
# 926| [MemberFunction] void* SizedDealloc::operator new(size_t)
|
||||
# 926| params:
|
||||
# 926| 0: [Parameter] p#0
|
||||
# 926| Type = [Size_t,TypedefType] size_t
|
||||
# 926| Type = [CTypedefType,Size_t] size_t
|
||||
# 927| [MemberFunction] void* SizedDealloc::operator new[](size_t)
|
||||
# 927| params:
|
||||
# 927| 0: [Parameter] p#0
|
||||
# 927| Type = [Size_t,TypedefType] size_t
|
||||
# 927| Type = [CTypedefType,Size_t] size_t
|
||||
# 928| [MemberFunction] void SizedDealloc::operator delete(void*, size_t)
|
||||
# 928| params:
|
||||
# 928| 0: [Parameter] p#0
|
||||
# 928| Type = [VoidPointerType] void *
|
||||
# 928| 1: [Parameter] p#1
|
||||
# 928| Type = [Size_t,TypedefType] size_t
|
||||
# 928| Type = [CTypedefType,Size_t] size_t
|
||||
# 929| [MemberFunction] void SizedDealloc::operator delete[](void*, size_t)
|
||||
# 929| params:
|
||||
# 929| 0: [Parameter] p#0
|
||||
# 929| Type = [VoidPointerType] void *
|
||||
# 929| 1: [Parameter] p#1
|
||||
# 929| Type = [Size_t,TypedefType] size_t
|
||||
# 929| Type = [CTypedefType,Size_t] size_t
|
||||
# 932| [CopyAssignmentOperator] Overaligned& Overaligned::operator=(Overaligned const&)
|
||||
# 932| params:
|
||||
#-----| 0: [Parameter] p#0
|
||||
@@ -7855,7 +7855,7 @@ ir.cpp:
|
||||
# 1118| Type = [IntType] int
|
||||
# 1119| 4: [DeclStmt] declaration
|
||||
# 1119| 0: [TypeDeclarationEntry] declaration of d
|
||||
# 1119| Type = [LocalTypedefType] d
|
||||
# 1119| Type = [CTypedefType,LocalTypedefType] d
|
||||
# 1120| 5: [ReturnStmt] return ...
|
||||
# 1117| [TopLevelFunction] int f(float)
|
||||
# 1117| params:
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
| typedefs.cpp:6:6:6:7 | f1 | typedefs.cpp:8:15:8:18 | TYPE | LocalTypedefType |
|
||||
| typedefs.cpp:6:6:6:7 | f1 | typedefs.cpp:8:15:8:18 | TYPE | CTypedefType, LocalTypedefType |
|
||||
| typedefs.cpp:6:6:6:7 | f1 | typedefs.cpp:9:9:9:9 | D | DirectAccessHolder, LocalClass, MetricClass, StructLikeClass |
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
| file://:0:0:0:0 | wchar_t | Wchar_t, WideCharType | |
|
||||
| file://:0:0:0:0 | wchar_t * | PointerType | TypedefType, Wchar_t |
|
||||
| ms.c:2:24:2:30 | wchar_t | TypedefType, Wchar_t | |
|
||||
| file://:0:0:0:0 | wchar_t * | PointerType | CTypedefType, Wchar_t |
|
||||
| ms.c:2:24:2:30 | wchar_t | CTypedefType, Wchar_t | |
|
||||
|
||||
10
cpp/ql/test/library-tests/using-aliases/using-alias.cpp
Normal file
10
cpp/ql/test/library-tests/using-aliases/using-alias.cpp
Normal file
@@ -0,0 +1,10 @@
|
||||
|
||||
typedef int type1;
|
||||
using using1 = float;
|
||||
|
||||
typedef using1 type2;
|
||||
using using2 = type1;
|
||||
|
||||
template<typename T> struct S { using X = T*; };
|
||||
|
||||
using Y = S<int>::X;
|
||||
@@ -0,0 +1,9 @@
|
||||
| file://:0:0:0:0 | X | NestedTypedefType | file://:0:0:0:0 | int * |
|
||||
| file://:0:0:0:0 | X | UsingAliasTypedefType | file://:0:0:0:0 | int * |
|
||||
| using-alias.cpp:2:13:2:17 | type1 | CTypedefType | file://:0:0:0:0 | int |
|
||||
| using-alias.cpp:3:7:3:12 | using1 | UsingAliasTypedefType | file://:0:0:0:0 | float |
|
||||
| using-alias.cpp:5:16:5:20 | type2 | CTypedefType | file://:0:0:0:0 | float |
|
||||
| using-alias.cpp:6:7:6:12 | using2 | UsingAliasTypedefType | file://:0:0:0:0 | int |
|
||||
| using-alias.cpp:8:39:8:39 | X | NestedTypedefType | file://:0:0:0:0 | T * |
|
||||
| using-alias.cpp:8:39:8:39 | X | UsingAliasTypedefType | file://:0:0:0:0 | T * |
|
||||
| using-alias.cpp:10:7:10:7 | Y | UsingAliasTypedefType | file://:0:0:0:0 | int * |
|
||||
4
cpp/ql/test/library-tests/using-aliases/using-alias.ql
Normal file
4
cpp/ql/test/library-tests/using-aliases/using-alias.ql
Normal file
@@ -0,0 +1,4 @@
|
||||
import cpp
|
||||
|
||||
from TypedefType t
|
||||
select t, t.getCanonicalQLClass(), t.getUnderlyingType()
|
||||
Reference in New Issue
Block a user