TPTP 4.4.0 Platform Project
Public API Specification

org.eclipse.tptp.platform.analysis.codereview.cpp
Class RulesHelper

java.lang.Object
  extended byorg.eclipse.tptp.platform.analysis.codereview.cpp.RulesHelper

public class RulesHelper
extends java.lang.Object


Constructor Summary
RulesHelper()
           
 
Method Summary
static boolean functionTakesVarArgs(org.eclipse.cdt.core.dom.ast.IASTName functionName)
          Given a function's IASTName, returns true if the function takes variable arguments
static org.eclipse.cdt.core.dom.ast.IASTExpression[] getArgumentListForFunctionCall(org.eclipse.cdt.core.dom.ast.IASTFunctionCallExpression fCall)
          Given a function call, returns a string list of parameters being passed to that function
static org.eclipse.cdt.core.dom.ast.IBinding getBindingFromExpression(org.eclipse.cdt.core.dom.ast.IASTExpression expression)
          Gets the binding of the variable given in expression
static org.eclipse.cdt.core.dom.ast.IASTDeclarator getDeclaratorOfVariable(CodeReviewResource resource, org.eclipse.cdt.core.dom.ast.IASTNode expression, boolean searchOnlyCurrentFunction)
          Gets the declarator of the parameter in the given expression.
static org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier getDeclSpecifierOfParentDeclaration(org.eclipse.cdt.core.dom.ast.IASTDeclarator declarator)
          This function can be used to get the specifier of a variable given its declarator.
static org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition getDefinitionOfFucntionDeclared(CodeReviewResource resource, org.eclipse.cdt.core.dom.ast.IBinding classNameBinding, org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration declaration)
          Retrieves a function definition for the given declaration (if the decalaration is that of a function) and the definition can be found.
static java.lang.String getIncludeName(org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement statement)
          Get's the name or path being included in the file.
static java.util.HashMap getLocalVisibleDeclarationsMap(CodeReviewResource resource, org.eclipse.cdt.core.dom.ast.IASTNode childNode, java.lang.String type)
          This function isn't being used by any rules any more.
static org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier getParentClassSpecifier(org.eclipse.cdt.core.dom.ast.IASTNode childNode)
          Gets the parent function of the node passed, or null if not found.
static org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition getParentFunctionDefinition(org.eclipse.cdt.core.dom.ast.IASTNode childNode)
          Gets the parent function of the node passed, or null if not found.
static java.util.List getVariablesDeclaredInFile(CodeReviewResource resource, boolean onlyGlobalVars, boolean includeConst)
          Return a String list of variables declared in the file (or just the global variables).
static boolean nameIsTypeClass(CodeReviewResource resource, org.eclipse.cdt.core.dom.ast.IASTName name)
          Method to check whether a name is from a class.
static boolean nameIsTypeStruct(CodeReviewResource resource, org.eclipse.cdt.core.dom.ast.IASTName name)
          Method to check whether a name is from a structure.
static int stringIndexOf(java.lang.String str, java.lang.String searchFor, int start)
          Alternative to java.lang.String.indexOf(java.lang.String,int) which does a bitwise match and does not support Unicode supplementary characters.
static boolean variableIsGlobal(CodeReviewResource resource, org.eclipse.cdt.core.dom.ast.IASTName varName)
          Checks if the given IdExpression is a global variable.
static boolean variableIsPointer(CodeReviewResource resource, org.eclipse.cdt.core.dom.ast.IASTNode expression, boolean searchOnlyCurrentFunction, boolean assumeThisIfDecNotFound)
          This should only be used when the pointer operators cannot be verified directly.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RulesHelper

public RulesHelper()
Method Detail

getBindingFromExpression

public static org.eclipse.cdt.core.dom.ast.IBinding getBindingFromExpression(org.eclipse.cdt.core.dom.ast.IASTExpression expression)
Gets the binding of the variable given in expression

Parameters:
expression -
Returns:
IBinding, it may be null

getParentFunctionDefinition

public static org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition getParentFunctionDefinition(org.eclipse.cdt.core.dom.ast.IASTNode childNode)
Gets the parent function of the node passed, or null if not found.

Parameters:
childNode -
Returns:
IASTFunctionDefinition the parent function of the childNode passed

getParentClassSpecifier

public static org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier getParentClassSpecifier(org.eclipse.cdt.core.dom.ast.IASTNode childNode)
Gets the parent function of the node passed, or null if not found.

Parameters:
childNode -
Returns:
IASTFunctionDefinition the parent function of the childNode passed

getLocalVisibleDeclarationsMap

public static java.util.HashMap getLocalVisibleDeclarationsMap(CodeReviewResource resource,
                                                               org.eclipse.cdt.core.dom.ast.IASTNode childNode,
                                                               java.lang.String type)
This function isn't being used by any rules any more. Returns the variables declared within the scope of the childNode's parent function. This includes the function parameters, it puts the values into a hashmap using the variable name as the key and the type as the value. If the type you are interested in is available in IASTSimpleDeclSpecifier, eg IASTSimpleDeclSpecifier.t_char, you may want to use that along with getDeclaratorOfVariable(CodeReviewResource, IASTNode, boolean) and getDeclSpecifierOfParentDeclaration(IASTDeclarator)

Parameters:
resource -
childNode -
type - of variables to find. If this is null, it will include every variable, otherwise only variables of type are included
Returns:
Hashmap containing local declarations with name as the key and the type as the value.

variableIsPointer

public static boolean variableIsPointer(CodeReviewResource resource,
                                        org.eclipse.cdt.core.dom.ast.IASTNode expression,
                                        boolean searchOnlyCurrentFunction,
                                        boolean assumeThisIfDecNotFound)
This should only be used when the pointer operators cannot be verified directly. If you have access to the declaration, it's possible to get the pointer operators from the declarator. see getDeclaratorOfVariable(CodeReviewResource, IASTNode, boolean)

Identifies if the parameter in the given expression is a pointer.

RulesHelper.variableIsPointer(resource, variable, false, true)) - returns true if var IS pointer or Unknown
RulesHelper.variableIsPointer(resource, variable, false, false)) - returns true if and only if var IS pointer
!RulesHelper.variableIsPointer(resource, variable, false, true)) - returns true if and only if var IS NOT pointer
!RulesHelper.variableIsPointer(resource, variable, false, false)) - returns true if var IS NOT pointer or Unknown

This works for the following node types: IASTUnaryExpression, IASTArraySubscriptExpression, IASTIdExpression, IASTFieldReference, IASTName

Parameters:
resource -
expression - one of: IASTUnaryExpression, IASTIdExpression, IASTFieldReference, IASTName
searchOnlyCurrentFunction - if true, searches only in the context of the current function, if false tries to find declaration from the class it belongs to
assumeThisIfDecNotFound - returns this value if the declaration of the expression was not found, ie assumes this result if result is actually unkown
Returns:
boolean true if expression is a pointer, or the value given (AssumeThisIfDecNotFound) if the declaration was not found

getDeclSpecifierOfParentDeclaration

public static org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier getDeclSpecifierOfParentDeclaration(org.eclipse.cdt.core.dom.ast.IASTDeclarator declarator)
This function can be used to get the specifier of a variable given its declarator. The function will check if the variable being passed is null so that the result of getDeclaratorOfVariable(CodeReviewResource, IASTNode, boolean) can be used directly. Returns null if the specifier is not found. This method works for declarators of variables and functions

Parameters:
declarator - of variable or function
Returns:
the IASTDeclSpecifier of the parent declaration or null if not found
See Also:
getDeclaratorOfVariable(CodeReviewResource, IASTNode, boolean)

getDeclaratorOfVariable

public static org.eclipse.cdt.core.dom.ast.IASTDeclarator getDeclaratorOfVariable(CodeReviewResource resource,
                                                                                  org.eclipse.cdt.core.dom.ast.IASTNode expression,
                                                                                  boolean searchOnlyCurrentFunction)
Gets the declarator of the parameter in the given expression. If parameters are not declared locally, but are in a class function, the declaration will be found and checked if the class is declared in the same file. searchOnlyCurrentFunction can be used to restrict resolving binding only with declarations in the current function. This is rarely needed, so it is usually set to false. This works for the following node types: IASTArraySubscriptExpression, IASTIdExpression, IASTFieldReference, IASTName

Parameters:
resource -
searchOnlyCurrentFunction - if true, searches only in the context of the current function, setting to true is only sometimes necessary
Returns:
IASTDeclarator
See Also:
variableIsPointer(CodeReviewResource, IASTNode, boolean, boolean)

nameIsTypeClass

public static boolean nameIsTypeClass(CodeReviewResource resource,
                                      org.eclipse.cdt.core.dom.ast.IASTName name)
Method to check whether a name is from a class. If you can access the specifier already, you probably do not need to use this method.

Parameters:
resource -
name - the IASTName from the variable to check
Returns:
true if the variable is of type struct

nameIsTypeStruct

public static boolean nameIsTypeStruct(CodeReviewResource resource,
                                       org.eclipse.cdt.core.dom.ast.IASTName name)
Method to check whether a name is from a structure. If you can access the specifier already, you probably do not need to use this method.

Parameters:
resource -
name - the IASTName from the variable to check
Returns:
true if the variable is of type struct

functionTakesVarArgs

public static boolean functionTakesVarArgs(org.eclipse.cdt.core.dom.ast.IASTName functionName)
Given a function's IASTName, returns true if the function takes variable arguments

Parameters:
functionName - IASTName of a function
Returns:
boolean true if the function takes variable arguments
See Also:
#getMemberFunctionName(IASTDeclaration)

getArgumentListForFunctionCall

public static org.eclipse.cdt.core.dom.ast.IASTExpression[] getArgumentListForFunctionCall(org.eclipse.cdt.core.dom.ast.IASTFunctionCallExpression fCall)
Given a function call, returns a string list of parameters being passed to that function

Parameters:
fCall -
Returns:

getDefinitionOfFucntionDeclared

public static org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition getDefinitionOfFucntionDeclared(CodeReviewResource resource,
                                                                                                  org.eclipse.cdt.core.dom.ast.IBinding classNameBinding,
                                                                                                  org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration declaration)
Retrieves a function definition for the given declaration (if the decalaration is that of a function) and the definition can be found. The name of the class containing the declaration must be provided. Returns null if the definition was not found

Parameters:
resource -
classNameBinding -
declaration -
Returns:
The function's IASTFunctionDefinition or null if it was not found

getIncludeName

public static java.lang.String getIncludeName(org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement statement)
Get's the name or path being included in the file. For example, for #include it will return stdio.h and for #include "path/header.h" it will return path/header.h

Parameters:
statement - IASTPreprocessorIncludeStatement
Returns:
String the item being included

variableIsGlobal

public static boolean variableIsGlobal(CodeReviewResource resource,
                                       org.eclipse.cdt.core.dom.ast.IASTName varName)
Checks if the given IdExpression is a global variable. If the declarator is not in the scope of the function or class that the function belongs to, then the variable is global. Often used along with #getVariablesDeclared(CodeReviewResource, boolean, boolean) in order to only report on variables that are in the current file, like this: if(globalVarsDeclaredInFile.contains(idExpName) && RulesHelper.variableIsGlobal(resource, idExp)){ //result }

Parameters:
resource -
varName - varName to verify if global
Returns:
boolean true if IdExpression is global

getVariablesDeclaredInFile

public static java.util.List getVariablesDeclaredInFile(CodeReviewResource resource,
                                                        boolean onlyGlobalVars,
                                                        boolean includeConst)
Return a String list of variables declared in the file (or just the global variables). This is used to report only on cases where variables are present in the current file, but does not take scope into account.

Parameters:
resource -
onlyGlobalVars - if true, includes only global variables, not all variables
includeConst - if false, will ignore constant fields
Returns:
list of variable names

stringIndexOf

public static int stringIndexOf(java.lang.String str,
                                java.lang.String searchFor,
                                int start)
Alternative to java.lang.String.indexOf(java.lang.String,int) which does a bitwise match and does not support Unicode supplementary characters.

Parameters:
str - String to search
searchFor - String to search for
Returns:
int index of found string

TPTP 4.4.0 Platform Project
Public API Specification