CodeQL library for C and C++¶
When analyzing C or C++ code, you can use the large collection of classes in the CodeQL library for C and C++.
About the CodeQL library for C and C++¶
There is an extensive library for analyzing CodeQL databases extracted from C/C++ projects. The classes in this library present the data from a database in an object-oriented form and provide abstractions and predicates to help you with common analysis tasks.
The library is implemented as a set of QL modules, that is, files with the extension .qll. The module cpp.qll imports all the core C/C++ library modules, so you can include the complete library by beginning your query with:
import cpp
The rest of this topic summarizes the available CodeQL classes and corresponding C/C++ constructs.
Commonly-used library classes¶
The most commonly used standard library classes are listed below. The listing is broken down by functionality. Each library class is annotated with a C/C++ construct it corresponds to.
Declaration classes¶
This table lists Declaration classes representing C/C++ declarations.
| Example syntax | CodeQL class | Remarks | 
|---|---|---|
| intvar; | GlobalVariable | |
| namespaceN{…floatvar;…} | NamespaceVariable | |
| intfunc( void ) {…floatvar;…} | LocalVariable | See also Initializer | 
| classC{…intvar;…} | MemberVariable | |
| intfunc(const charparam); | Function | |
| template < typenameT>voidfunc(Tparam); | TemplateFunction | |
| intfunc(const char*format, ...){…} | FormattingFunction | |
| func < int, float > (…); | FunctionTemplateInstantiation | |
| template < typenameT>func  < int,T> (…) {…} | FunctionTemplateSpecialization | |
| classC{…intfunc( floatparam);…}; | MemberFunction | |
| classC{…intfunc( floatparam) const;…}; | ConstMemberFunction | |
| classC{…virtual intfunc(…) {…} }; | VirtualFunction | |
| classC{… C(…) {…}…}; | Constructor | |
| C::operator float () const; | ConversionOperator | |
| classC{…~C( void ) {…}…}; | Destructor | |
| classC{…C  ( constD&d) {…}…}; | ConversionConstructor | |
| C &C:: operator= (constC& ); | CopyAssignmentOperator | |
| C &C:: operator= (C&& ); | MoveAssignmentOperator | |
| C ::C(constC& ); | CopyConstructor | |
| C ::C(C&& ); | MoveConstructor | |
| C ::C(void); | NoArgConstructor | Default constructor | 
| enumen{val1,val2 …} | EnumConstant | |
| friend voidfunc( int );friend classB; | FriendDecl | |
| intfunc( void ) {…enumen{val1,val2 …};…} | LocalEnum | |
| classC{…enumen{val1,val2 …}…} | NestedEnum | |
| enum classen: short {val1,val2 …} | ScopedEnum | |
| classC{…virtual voidfunc( int ) = 0;…}; | AbstractClass | |
| template < int , float > classC{…}; | ClassTemplateInstantiation | |
| template < > classC<Type> {…}; | FullClassTemplateSpecialization | |
| template < typenameT>classC<T,5> {…}; | PartialClassTemplateSpecialization | |
| intfunc( void ) {…classC{…};…} | LocalClass | |
| classC{…classD{…};…}; | NestedClass | |
| Class | ||
| template < typenameT>structC:T{…}; | ProxyClass | Appears only in uninstantiated templates | 
| intfunc( void ) {…structS{…};…} | LocalStruct | |
| classC{…structS{…};…}; | NestedStruct | |
| int *func( void ) {…unionU{…};…} | LocalUnion | |
| classC{…unionU{…};…}; | NestedUnion | |
| typedef intT; | TypedefType | |
| intfunc( void ) {…typedef intT;…} | LocalTypedefType | |
| classC{…typedef intT;…}; | NestedTypedefType | |
| classV:…publicB …{…}; | ClassDerivation | |
| classV:…virtualB …{…}; | VirtualClassDerivation | |
| template < typenameT>classC{…}; | TemplateClass | |
| intfoo(Type param1,Type param2 …); | Parameter | |
| template <typenameT>T t; | TemplateVariable | Since C++14 | 
Statement classes¶
This table lists subclasses of Stmt representing C/C++ statements.
| Example syntax | CodeQL class | Remarks | 
|---|---|---|
| __asm__ ("movb %bh, (%eax)"); | AsmStmt | Specific to a given CPU instruction set | 
| {Stmt…} | BlockStmt | |
| catch (Parameter)BlockStmt | CatchBlock | |
| catch ( ... )BlockStmt | CatchAnyBlock | |
| goto *labelptr; | ComputedGotoStmt | GNU extension; use with LabelLiteral | 
| Type i ,j; | DeclStmt | |
| if (Expr)StmtelseStmt | IfStmt | |
| switch (Expr) {SwitchCase…} | SwitchStmt | |
| doStmtwhile (Expr) | DoStmt | |
| for (DeclStmt;Expr;Expr)Stmt | ForStmt | |
| for (DeclStmt:Expr)Stmt | RangeBasedForStmt | |
| while (Expr)Stmt | WhileStmt | |
| Expr ; | ExprStmt | |
| __try {…} __except (Expr) {…} | MicrosoftTryExceptStmt | Structured exception handling (SEH) under Windows | 
| __try {…} __finally {…} | MicrosoftTryFinallyStmt | Structured exception handling (SEH) under Windows | 
| returnExpr; | ReturnStmt | |
| caseExpr: | SwitchCase | |
| try {Stmt…}CatchBlock… CatchAnyBlock | TryStmt | |
| FunctionTryStmt | ||
| ; | EmptyStmt | |
| break; | BreakStmt | |
| continue; | ContinueStmt | |
| gotoLabelStmt; | GotoStmt | |
| slabel : | LabelStmt | |
| floatarr[Expr] [Expr]; | VlaDeclStmt | C99 variable-length array | 
Expression classes¶
This table lists subclasses of Expr representing C/C++ expressions.
Type classes¶
This table lists subclasses of Type representing C/C++ types.
| Example syntax | CodeQL class | Remarks | 
|---|---|---|
| void | VoidType | |
| _Boolorbool | 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 extension | 
| 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 > classC> | TemplateTemplateParameter | |
| template < typenameT> | TemplateParameter | 
Preprocessor classes¶
This table lists Preprocessor classes representing C/C++ preprocessing directives.
| Example syntax | CodeQL class | Remarks | 
|---|---|---|
| #elifcondition | PreprocessorElif | |
| #ifcondition | PreprocessorIf | |
| #ifdefmacro | PreprocessorIfdef | |
| #ifndefmacro | PreprocessorIfndef | |
| #else | PreprocessorElse | |
| #endif | PreprocessorEndif | |
| #lineline_number file_name | PreprocessorLine | |
| #pragmapragma_property | PreprocessorPragma | |
| #undefmacro | PreprocessorUndef | |
| #warningmessage | PreprocessorWarning | |
| #errormessage | PreprocessorError | |
| #includefile_name | Include | |
| #importfile_name | Import | Apple/NeXT extension | 
| #include_nextfile_name | IncludeNext | Apple/NeXT extension | 
| #definemacro … | Macro | 



