CodeQL 文档

C 和 C++ 的 CodeQL 库

分析 C 或 C++ 代码时,可以使用 C 和 C++ 的 CodeQL 库中的大量类集合。

关于 C 和 C++ 的 CodeQL 库

有一个广泛的库用于分析从 C/C++ 项目中提取的 CodeQL 数据库。该库中的类以面向对象的格式呈现来自数据库的数据,并提供抽象和谓词来帮助您完成常见的分析任务。该库实现为一组 QL 模块,即扩展名为 .qll 的文件。模块 cpp.qll 导入所有核心 C/C++ 库模块,因此您可以通过以下方式包含整个库:以以下方式开始您的查询

import cpp

本主题的其余部分总结了可用的 CodeQL 类和相应的 C/C++ 结构。

常用库类

下面列出了最常用的标准库类。该列表按功能分类。每个库类都标注了它对应的 C/C++ 结构。

声明类

此表列出了 Declaration 类,代表 C/C++ 声明。

示例语法 CodeQL 类 备注
int var ; GlobalVariable  
namespace N {float var ;} NamespaceVariable  
int func ( void ) {float var ;} LocalVariable 另请参见 Initializer
class C {int var ;} MemberVariable  
int func ( const char param ); Function  
template < typename T >
void func ( T param);
TemplateFunction  
int func ( const char* format , ...)
{}
FormattingFunction  
func < int, float > (); FunctionTemplateInstantiation  
template < typename T >
func < int, T > () {}
FunctionTemplateSpecialization  
class C {
int func ( float param );};
MemberFunction  
class C {
int func ( float param ) const;};
ConstMemberFunction  
class C {virtual int func () {} }; VirtualFunction  
class C {C () {}}; Constructor  
C::operator float () const; ConversionOperator  
class C {~ C ( void ) {}}; Destructor  
class C {
C ( const D & d ) {}};
ConversionConstructor  
C & C :: operator= (const C & ); CopyAssignmentOperator  
C & C :: operator= ( C && ); MoveAssignmentOperator  
C :: C (const C & ); CopyConstructor  
C :: C ( C && ); MoveConstructor  
C :: C (void); NoArgConstructor 默认构造函数
enum en { val1 , val2} EnumConstant  
friend void func ( int );
friend class B ;
FriendDecl  
int func ( void ) {
enum en { val1 , val2};}
LocalEnum  
class C {
enum en { val1 , val2}}
NestedEnum  
enum class en : short { val1 , val2} ScopedEnum  
class C {
virtual void func ( int ) = 0;};
AbstractClass  
template < int , float > class C {}; ClassTemplateInstantiation  
template < > class C < Type > {}; 完全类模板特化  
template < typename T >
class C < T , 5 > {};
偏特化类模板  
int func ( void ) {class C {};} 局部类  
class C {class D {};}; 嵌套类  
class C {
Type var ;
Type func ( Parameter) {}};
 
struct S {
Type var ;
Type func ( Parameter) {}};
 
union U {
Type var1 ;
Type var2 ;};
 
template < typename T >
struct C : T {};
代理类 仅在*未实例化*的模板中出现
int func ( void ) {
struct S {};}
局部结构体  
class C {
struct S {};};
嵌套结构体  
int * func ( void ) {union U {};} 局部联合体  
class C {union U {};}; 嵌套联合体  
typedef int T ; typedef 类型  
int func ( void ) {
typedef int T ;}
局部 typedef 类型  
class C {
typedef int T ;};
嵌套 typedef 类型  
class V :public B{}; 类派生  
class V :virtual B{}; 虚类派生  
template < typename T >
class C {};
模板类  
int foo ( Type param1 , Type param2); 参数  
template <typename T > T t ; 模板变量 自 C++14 起

语句类

该表列出了表示 C/C++ 语句的 Stmt 子类。

示例语法 CodeQL 类 备注
__asm__ (" movb %bh, (%eax) "); AsmStmt 特定于给定 CPU 指令集
{ Stmt} BlockStmt  
catch ( Parameter ) BlockStmt CatchBlock  
catch ( ... ) BlockStmt CatchAnyBlock  
goto * labelptr ; ComputedGotoStmt GNU 扩展;与 LabelLiteral 结合使用
Type i , j ; DeclStmt  
if ( Expr ) Stmt else Stmt IfStmt  
switch ( Expr ) { SwitchCase} SwitchStmt  
do Stmt while ( Expr ) DoStmt  
for ( DeclStmt ; Expr ; Expr ) Stmt ForStmt  
for ( DeclStmt : Expr ) Stmt RangeBasedForStmt  
while ( Expr ) Stmt WhileStmt  
Expr ; ExprStmt  
__try {} __except ( Expr ) {} MicrosoftTryExceptStmt Windows 下的结构化异常处理 (SEH)
__try {} __finally {} MicrosoftTryFinallyStmt Windows 下的结构化异常处理 (SEH)
return Expr ; ReturnStmt  
case Expr : SwitchCase  
try { Stmt} CatchBlockCatchAnyBlock TryStmt  
void func (void) try { Stmt}
FunctionTryStmt  
; EmptyStmt  
break; BreakStmt  
continue; ContinueStmt  
goto LabelStmt ; GotoStmt  
slabel : LabelStmt  
float arr [ Expr ] [ Expr ]; VlaDeclStmt C99 可变长数组

表达式类

此表列出了表示 C/C++ 表达式的 Expr 的子类。

示例语法 CodeQL 类 备注
{ Expr}  
alignof ( Expr ) AlignofExprOperator  
alignof ( Type ) AlignofTypeOperator  
Expr [ Expr ] ArrayExpr  
__assume ( Expr ) AssumeExpr Microsoft 扩展
static_assert ( Expr , StringLiteral ) _Static_assert ( Expr , StringLiteral ) StaticAssert
C++11
C11
__noop; BuiltInNoOp Microsoft 扩展
Expr ( Expr) ExprCall  
func ( Expr)
instance . func ( Expr)
FunctionCall  
Expr , Expr CommaExpr  
if ( Type arg = Expr ) ConditionDeclExpr  
( Type ) Expr CStyleCast  
const_cast < Type > ( Expr ) ConstCast  
dynamic_cast < Type > ( Expr ) DynamicCast  
reinterpret_cast < Type > ( Expr ) ReinterpretCast  
static_cast < Type > ( Expr ) StaticCast  
template < typename... T >
auto sum ( T t )
{ return ( t + ... + 0 ); }
FoldExpr 仅在*未实例化*的模板中出现
int func ( format , ... ); FormattingFunctionCall  
[ = ] ( float b ) -> float
{ return captured * b ; }
LambdaExpression C++11
^ int ( int x , int y ) {
{ Stmt; return x + y ; }
BlockExpr Apple 扩展
void * labelptr = && label ; LabelLiteral GNU 扩展;与 ComputedGotoStmt 一起使用
“%3d %s\n” FormatLiteral  
0xdbceffca HexLiteral  
0167 OctalLiteral  
‘c’ CharLiteral  
“abcdefgh”, L”wide” StringLiteral  
new Type [ Expr ] NewArrayExpr  
new Type NewExpr  
delete [ ] Expr ; DeleteArrayExpr  
delete Expr ; DeleteExpr  
noexcept ( Expr ) NoExceptExpr  
Expr = Expr AssignExpr 另请参见 Initializer
Expr += Expr  
Expr /= Expr AssignDivExpr  
Expr *= Expr AssignMulExpr  
Expr %= Expr AssignRemExpr  
Expr -= Expr  
Expr &= Expr AssignAndExpr  
Expr <<= Expr AssignLShiftExpr  
Expr `` =`` Expr AssignOrExpr
Expr >>= Expr AssignRShiftExpr  
Expr ^= Expr AssignXorExpr  
Expr + Expr


C99
C99
Expr / Expr

C99
Expr >? Expr MaxExpr GNU extension
Expr <? Expr MinExpr GNU extension
Expr * Expr

C99
Expr % Expr RemExpr  
Expr - Expr



C99
C99
Expr & Expr BitwiseAndExpr  
Expr | Expr BitwiseOrExpr  
Expr ^ Expr BitwiseXorExpr  
Expr << Expr LShiftExpr  
Expr >> Expr RShiftExpr  
Expr && Expr LogicalAndExpr  
Expr || Expr LogicalOrExpr  
Expr == Expr EQExpr  
Expr != Expr NEExpr  
Expr >= Expr GEExpr  
Expr > Expr GTExpr  
Expr <= Expr LEExpr  
Expr < Expr LTExpr  
Expr ? Expr : Expr ConditionalExpr  
& Expr AddressOfExpr  
* Expr PointerDereferenceExpr  
Expr -- PostfixDecrExpr  
-- Expr PrefixDecrExpr  
Expr ++ PostfixIncrExpr  
++ Expr PrefixIncrExpr  
__imag ( Expr ) ImaginaryPartExpr GNU extension
__real ( Expr ) RealPartExpr GNU extension
- Expr UnaryMinusExpr  
+ Expr UnaryPlusExpr  
~ Expr

GNU extension
! Expr NotExpr  
int vect __attribute__
( ( vector_size ( 16 ) ) )
= { 3 , 8 , 32 , 33 };
VectorFillOperation GNU extension
sizeof ( Expr ) SizeofExprOperator  
sizeof ( Type ) SizeofTypeOperator  
template < typename... T >
int count ( T &&... t )
{ return sizeof... ( t ); }
SizeofPackOperator  
( { Stmt; Expr } ) StmtExpr GNU/Clang extension
this ThisExpr  
throw ( Expr ); ThrowExpr  
throw; ReThrowExpr  
typeid ( Expr )
typeid ( Type )
TypeidOperator  
__uuidof ( Expr ) UuidofOperator Microsoft 扩展

类型类

此表列出了表示 C/C++ 类型的 Type 的子类。

示例语法 CodeQL 类 备注
void VoidType  
_Boolbool BoolType  
char16_t Char16Type C11, C++11
char32_t Char32Type C11, C++11
char PlainCharType  
signed char SignedCharType  
unsigned char UnsignedCharType  
int IntType  
long long LongLongType  
long LongType  
short ShortType  
wchar_t WideCharType  
nullptr_t NullPointerType  
double DoubleType  
long double LongDoubleType  
float FloatType  
auto AutoType  
decltype ( Expr ) Decltype  
Type [ n ] ArrayType  
Type ( ^ blockptr ) ( Parameter) BlockType Apple 扩展
Type ( * funcptr ) ( Parameter) FunctionPointerType  
Type ( & funcref ) ( Parameter) FunctionReferenceType  
Type __attribute__ ( ( vector_size ( n ) ) ) GNUVectorType  
Type * PointerType  
Type & LValueReferenceType  
Type && RValueReferenceType  
Type ( Class *:: membptr ) ( Parameter) PointerToMemberType  
template < template < typename > class C > TemplateTemplateParameter  
template < typename T > TemplateParameter  

预处理器类

此表列出了表示 C/C++ 预处理指令的 Preprocessor 类。

示例语法 CodeQL 类 备注
#elif condition PreprocessorElif  
#if condition PreprocessorIf  
#ifdef macro PreprocessorIfdef  
#ifndef macro PreprocessorIfndef  
#else PreprocessorElse  
#endif PreprocessorEndif  
#line line_number file_name PreprocessorLine  
#pragma pragma_property PreprocessorPragma  
#undef macro PreprocessorUndef  
#warning message PreprocessorWarning  
#error message PreprocessorError  
#include file_name Include  
#import file_name Import Apple/NeXT 扩展
#include_next file_name IncludeNext Apple/NeXT 扩展
#define macro Macro  
  • ©GitHub, Inc.
  • 条款
  • 隐私