NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
CodeAnalyzer Class Reference

This class analyzes the code handled by the editor by accessing it with friends rights. More...

#include <codeanalyzer.hpp>

Collaboration diagram for CodeAnalyzer:

Public Member Functions

 CodeAnalyzer (NumeReEditor *parent, Options *opts)
 
void run ()
 Starts the code analysis. Requires a pointer to the editor and to the options. More...
 

Static Public Member Functions

static char getVariableType (const std::string &sVarName)
 Returns the leading character, which indicates the type of this variable or a null character, if either the leading character is not known or if it does not fulfill the requirements. More...
 
static void changeVariableType (std::string &sVarName, char type)
 Change the variable's type to the passed type. More...
 

Private Member Functions

AnnotationCount analyseCommands ()
 Analyses commands in the code. More...
 
AnnotationCount analyseFunctions (bool isContinuedLine)
 Analyses calls to standard functions. More...
 
AnnotationCount analyseProcedures ()
 Analyses calls to procedures. More...
 
AnnotationCount analyseIdentifiers ()
 Analyses occurences of identifiers. More...
 
AnnotationCount analyseOperators ()
 Analyses occurences of operators. More...
 
AnnotationCount analyseNumbers ()
 Analyses occurences of numerical literals. More...
 
AnnotationCount analysePreDefs ()
 Analyses occurences of special predefined variables. More...
 
AnnotationCount addToAnnotation (const std::string &sMessage, int nStyle)
 Adds the passed sMessage with the style to the internal cache. More...
 
std::string highlightFoundOccurence (const std::string &sElement, int nPos, int nLength)
 Highlights the occurence in the editor. More...
 
bool containsAssignment (const std::string &sCurrentLine)
 Checks for assignments. More...
 
int calculateCyclomaticComplexity (int startline, int endline)
 Calculates the cyclomatic complexity between both lines. More...
 
int calculateLinesOfCode (int startline, int endline)
 Calculates the lines of code between both lines. More...
 
int countNumberOfComments (int startline, int endline)
 Counts the number of comment lines between both lines. More...
 

Private Attributes

NumeReEditorm_editor
 
Optionsm_options
 
SymDefManager m_symdefs
 
std::vector< std::string > m_MATOP_FUNCS
 
std::vector< std::string > m_DRAW_FUNCS
 
std::vector< std::string > m_STRING_FUNCS
 
std::vector< std::string > m_STRING_METHODS
 
std::string m_sNote
 
std::string m_sWarn
 
std::string m_sError
 
std::string m_sCurrentLine
 
std::string m_sStyles
 
std::string m_currentMode
 
int m_nCurPos
 
int m_nCurrentLine
 
bool m_hasProcedureDefinition
 
std::vector< std::pair< std::string, int > > m_vLocalVariables
 
const double MINCOMMENTDENSITY = 0.5
 
const double MAXCOMMENTDENSITY = 1.5
 
const int MAXCOMPLEXITYNOTIFY = 15
 
const int MAXCOMPLEXITYWARN = 20
 
const int MAXLINESOFCODE = 100
 

Detailed Description

This class analyzes the code handled by the editor by accessing it with friends rights.

Definition at line 58 of file codeanalyzer.hpp.

Constructor & Destructor Documentation

◆ CodeAnalyzer()

CodeAnalyzer::CodeAnalyzer ( NumeReEditor parent,
Options opts 
)

Constructor

Parameters
parentNumeReEditor*
optsOptions*

Definition at line 46 of file codeanalyzer.cpp.

References _guilang, Language::get(), Language::getList(), m_DRAW_FUNCS, m_MATOP_FUNCS, m_sError, m_sNote, m_STRING_FUNCS, m_STRING_METHODS, m_sWarn, and replaceDocStrings().

Here is the call graph for this function:

Member Function Documentation

◆ addToAnnotation()

AnnotationCount CodeAnalyzer::addToAnnotation ( const std::string &  sMessage,
int  nStyle 
)
private

Adds the passed sMessage with the style to the internal cache.

Parameters
sMessageconst string&
nStyleint
Returns
AnnotationCount

Definition at line 1662 of file codeanalyzer.cpp.

References ANNOTATION_ERROR, ANNOTATION_NOTE, ANNOTATION_WARN, NumeReEditor::countUmlauts(), Options::GetAnalyzerOption(), m_editor, m_options, m_sCurrentLine, m_sStyles, AnnotationCount::nErrors, AnnotationCount::nNotes, AnnotationCount::nWarnings, Options::USE_ERRORS, Options::USE_NOTES, and Options::USE_WARNINGS.

Referenced by analyseCommands(), analyseFunctions(), analyseIdentifiers(), analyseNumbers(), analyseOperators(), analysePreDefs(), analyseProcedures(), and run().

Here is the call graph for this function:

◆ analyseCommands()

AnnotationCount CodeAnalyzer::analyseCommands ( )
private

Analyses commands in the code.

Returns
AnnotationCount

This member function analyses syntax elements, which are highlighted as commands

Definition at line 356 of file codeanalyzer.cpp.

References _guilang, addToAnnotation(), Options::ALWAYS_SHOW_METRICS, ANNOTATION_ERROR, ANNOTATION_NOTE, ANNOTATION_WARN, NumeReEditor::BlockMatch(), calculateCyclomaticComplexity(), calculateLinesOfCode(), Options::COMMENT_DENSITY, Options::COMPLEXITY, containsAssignment(), countNumberOfComments(), SymDefManager::createSymbol(), FILE_MATLAB, FILE_NPRC, SearchController::FindAll(), findCommand(), SearchController::FindNamingProcedure(), findParameter(), Language::get(), Options::GetAnalyzerOption(), NumeReEditor::GetFileName(), getNextArgument(), Includer::getNextLine(), SyntaxError::getToken(), getVariableType(), Options::GLOBAL_VARIABLES, highlightFoundOccurence(), Options::INLINE_IF, Includer::is_open(), NumeReEditor::isBlockEnd(), NumeReEditor::isBlockStart(), SymDefManager::isSymbol(), Options::LINES_OF_CODE, m_currentMode, m_editor, NumeReEditor::m_fileType, m_hasProcedureDefinition, m_nCurPos, m_nCurrentLine, m_options, NumeReEditor::m_search, m_sError, m_sNote, m_sWarn, m_symdefs, m_vLocalVariables, MAXCOMMENTDENSITY, MAXCOMPLEXITYNOTIFY, MAXCOMPLEXITYWARN, MAXLINESOFCODE, MINCOMMENTDENSITY, Options::MISLEADING_TYPE, Match::nPos, Options::PROCEDURE_LENGTH, Options::PROGRESS_RUNTIME, Options::RESULT_ASSIGNMENT, Options::RESULT_SUPPRESSION, Match::sString, StripSpaces(), Options::SWITCH_FALLTHROUGH, SYMDEF_COMMAND, Options::THISFILE_NAMESPACE, toString(), and Options::UNUSED_VARIABLES.

Referenced by run().

Here is the call graph for this function:

◆ analyseFunctions()

AnnotationCount CodeAnalyzer::analyseFunctions ( bool  isContinuedLine)
private

Analyses calls to standard functions.

Parameters
isContinuedLinebool Has to be true, if the current line is continued from above
Returns
AnnotationCount

This member function analyses syntax elements, which are highlighted as functions

Definition at line 1188 of file codeanalyzer.cpp.

References _guilang, addToAnnotation(), ANNOTATION_ERROR, ANNOTATION_WARN, FILE_MATLAB, FILE_NPRC, FILE_NSCR, Language::get(), Options::GetAnalyzerOption(), highlightFoundOccurence(), NumeReEditor::isStyleType(), m_currentMode, m_DRAW_FUNCS, m_editor, NumeReEditor::m_fileType, m_MATOP_FUNCS, m_nCurPos, m_nCurrentLine, m_options, m_sError, m_STRING_FUNCS, m_STRING_METHODS, m_sWarn, Options::RESULT_ASSIGNMENT, StripSpaces(), NumeReEditor::STYLE_COMMAND, NumeReEditor::STYLE_DATAOBJECT, and NumeReEditor::STYLE_PROCEDURE.

Referenced by run().

Here is the call graph for this function:

◆ analyseIdentifiers()

AnnotationCount CodeAnalyzer::analyseIdentifiers ( )
private

Analyses occurences of identifiers.

Returns
AnnotationCount

This member function analyses syntax elements, which are highlighted as identifiers (aka variable names)

Definition at line 1345 of file codeanalyzer.cpp.

References _guilang, SettingsValue::active(), addToAnnotation(), ANNOTATION_NOTE, ANNOTATION_WARN, Options::ARGUMENT_UNDERSCORE, FILE_MATLAB, FILE_NPRC, Language::get(), Options::GetAnalyzerOption(), Settings::getSetting(), getVariableType(), Options::GLOBAL_VARIABLES, highlightFoundOccurence(), NumeReEditor::isStyleType(), SymDefManager::isSymbol(), m_editor, NumeReEditor::m_fileType, m_hasProcedureDefinition, m_nCurPos, m_nCurrentLine, m_options, m_sNote, m_sWarn, m_symdefs, m_vLocalVariables, SETTING_B_TABLEREFS, NumeReEditor::STYLE_DATAOBJECT, Options::TYPE_ORIENTATION, and Options::VARIABLE_LENGTH.

Referenced by run().

Here is the call graph for this function:

◆ analyseNumbers()

AnnotationCount CodeAnalyzer::analyseNumbers ( )
private

Analyses occurences of numerical literals.

Returns
AnnotationCount

This member function analyses syntax elements, which are highlighted as numbers

Definition at line 1504 of file codeanalyzer.cpp.

References _guilang, addToAnnotation(), ANNOTATION_WARN, Language::get(), Options::GetAnalyzerOption(), highlightFoundOccurence(), NumeReEditor::isStyleType(), m_editor, m_nCurPos, m_nCurrentLine, m_options, m_sWarn, Options::MAGIC_NUMBERS, NumeReEditor::STYLE_NUMBER, and NumeReEditor::STYLE_OPERATOR.

Referenced by run().

Here is the call graph for this function:

◆ analyseOperators()

AnnotationCount CodeAnalyzer::analyseOperators ( )
private

Analyses occurences of operators.

Returns
AnnotationCount

This member function analyses syntax elements, which are highlighted as operators

Definition at line 1478 of file codeanalyzer.cpp.

References _guilang, addToAnnotation(), ANNOTATION_ERROR, Language::get(), highlightFoundOccurence(), m_editor, m_nCurPos, and m_sError.

Referenced by run().

Here is the call graph for this function:

◆ analysePreDefs()

AnnotationCount CodeAnalyzer::analysePreDefs ( )
private

Analyses occurences of special predefined variables.

Returns
AnnotationCount

Definition at line 1553 of file codeanalyzer.cpp.

References _guilang, addToAnnotation(), ANNOTATION_WARN, findVariableInExpression(), Language::get(), getAllArguments(), highlightFoundOccurence(), m_editor, m_nCurPos, m_nCurrentLine, and m_sWarn.

Referenced by run().

Here is the call graph for this function:

◆ analyseProcedures()

AnnotationCount CodeAnalyzer::analyseProcedures ( )
private

Analyses calls to procedures.

Returns
AnnotationCount

This member function analyses syntax elements, which are highlighted as procedure calls

Definition at line 1293 of file codeanalyzer.cpp.

References _guilang, addToAnnotation(), ANNOTATION_ERROR, SearchController::FindMarkedProcedure(), SearchController::FindProcedureDefinition(), Language::get(), highlightFoundOccurence(), NumeReEditor::isStyleType(), m_editor, m_nCurPos, NumeReEditor::m_search, m_sError, and NumeReEditor::STYLE_PROCEDURE.

Referenced by run().

Here is the call graph for this function:

◆ calculateCyclomaticComplexity()

int CodeAnalyzer::calculateCyclomaticComplexity ( int  startline,
int  endline 
)
private

Calculates the cyclomatic complexity between both lines.

Parameters
startlineint
endlineint
Returns
int

The cyclomatic complexity (also called McGabe metric) is calculated from the number of possible control flow paths, which the code may take in a scope.

Definition at line 1769 of file codeanalyzer.cpp.

References NumeReEditor::isStyleType(), m_editor, NumeReEditor::STYLE_COMMAND, NumeReEditor::STYLE_FUNCTION, and NumeReEditor::STYLE_OPERATOR.

Referenced by analyseCommands(), and run().

Here is the call graph for this function:

◆ calculateLinesOfCode()

int CodeAnalyzer::calculateLinesOfCode ( int  startline,
int  endline 
)
private

Calculates the lines of code between both lines.

Parameters
startlineint
endlineint
Returns
int

The lines of code a calculated from the number of all non-comment and non-whitespace lines (i.e. all lines, which contain code)

Definition at line 1844 of file codeanalyzer.cpp.

References NumeReEditor::isStyleType(), m_editor, NumeReEditor::STYLE_COMMENT_BLOCK, and NumeReEditor::STYLE_COMMENT_LINE.

Referenced by analyseCommands(), and run().

Here is the call graph for this function:

◆ changeVariableType()

void CodeAnalyzer::changeVariableType ( std::string &  sVarName,
char  type 
)
static

Change the variable's type to the passed type.

Parameters
sVarNamestd::string&
typechar
Returns
void

Definition at line 1988 of file codeanalyzer.cpp.

References getVariableType(), and isNumericType().

Referenced by NumeReEditor::fixSymbolName().

Here is the call graph for this function:

◆ containsAssignment()

bool CodeAnalyzer::containsAssignment ( const std::string &  sCurrentLine)
private

Checks for assignments.

Parameters
sCurrentLineconst string&
Returns
bool

This member function checks, whether the current line contains an assignement, i.e. the calculation result is stored into a target variable

Definition at line 1716 of file codeanalyzer.cpp.

References isInQuotes().

Referenced by analyseCommands().

Here is the call graph for this function:

◆ countNumberOfComments()

int CodeAnalyzer::countNumberOfComments ( int  startline,
int  endline 
)
private

Counts the number of comment lines between both lines.

Parameters
startlineint
endlineint
Returns
int

The number of comment lines are calculated from every line comment and the number of lines contained in additional block comments.

Definition at line 1895 of file codeanalyzer.cpp.

References NumeReEditor::isStyleType(), m_editor, NumeReEditor::STYLE_COMMENT_BLOCK, and NumeReEditor::STYLE_COMMENT_LINE.

Referenced by analyseCommands(), and run().

Here is the call graph for this function:

◆ getVariableType()

char CodeAnalyzer::getVariableType ( const std::string &  sVarName)
static

Returns the leading character, which indicates the type of this variable or a null character, if either the leading character is not known or if it does not fulfill the requirements.

Parameters
sVarNameconst std::string&
Returns
char

Definition at line 1939 of file codeanalyzer.cpp.

Referenced by analyseCommands(), analyseIdentifiers(), and changeVariableType().

◆ highlightFoundOccurence()

string CodeAnalyzer::highlightFoundOccurence ( const std::string &  sElement,
int  nPos,
int  nLength 
)
private

Highlights the occurence in the editor.

Parameters
sElementconst string&
nPosint
nLengthint
Returns
string The passed sElement string with an additional white space

The found occurence is highlighted in the editor using an indicator

Definition at line 1750 of file codeanalyzer.cpp.

References HIGHLIGHT_ANNOTATION, and m_editor.

Referenced by analyseCommands(), analyseFunctions(), analyseIdentifiers(), analyseNumbers(), analyseOperators(), analysePreDefs(), analyseProcedures(), and run().

◆ run()

void CodeAnalyzer::run ( )

Starts the code analysis. Requires a pointer to the editor and to the options.

Returns
void

This member function is the wrapper for the static code analyzer It may handle NumeRe and MATLAB files. The results are written directly as annotations in the editor.

Definition at line 73 of file codeanalyzer.cpp.

References _guilang, addToAnnotation(), Options::ALWAYS_SHOW_METRICS, analyseCommands(), analyseFunctions(), analyseIdentifiers(), analyseNumbers(), analyseOperators(), analysePreDefs(), analyseProcedures(), ANNOTATION_ERROR, ANNOTATION_NOTE, ANNOTATION_WARN, calculateCyclomaticComplexity(), calculateLinesOfCode(), SymDefManager::clear(), Options::COMMENT_DENSITY, Options::COMPLEXITY, Options::CONSTANT_EXPRESSION, countNumberOfComments(), FILE_CPP, FILE_MATLAB, FILE_NPRC, FILE_NSCR, Language::get(), Options::GetAnalyzerOption(), NumeReEditor::getEditorSetting(), NumeReEditor::GetFilenameString(), HIGHLIGHT_ANNOTATION, highlightFoundOccurence(), NumeReEditor::isStyleType(), Options::LINES_OF_CODE, m_currentMode, m_editor, NumeReEditor::m_fileType, m_hasProcedureDefinition, m_nCurPos, m_nCurrentLine, m_options, m_sCurrentLine, m_sNote, m_sStyles, m_sWarn, m_symdefs, MAXCOMMENTDENSITY, MAXCOMPLEXITYNOTIFY, MAXCOMPLEXITYWARN, MAXLINESOFCODE, MINCOMMENTDENSITY, AnnotationCount::nErrors, AnnotationCount::nNotes, AnnotationCount::nWarnings, Options::RESULT_SUPPRESSION, NumeReEditor::SETTING_USEANALYZER, StripSpaces(), NumeReEditor::STYLE_COMMAND, NumeReEditor::STYLE_COMMENT_BLOCK, NumeReEditor::STYLE_COMMENT_LINE, NumeReEditor::STYLE_DATAOBJECT, NumeReEditor::STYLE_FUNCTION, NumeReEditor::STYLE_IDENTIFIER, NumeReEditor::STYLE_NUMBER, NumeReEditor::STYLE_OPERATOR, NumeReEditor::STYLE_PROCEDURE, and toString().

Referenced by NumeReEditor::AnalyseCode(), NumeReEditor::AsynchEvaluations(), NumeReEditor::FindAndOpenProcedure(), and NumeReEditor::ToggleSettings().

Here is the call graph for this function:

Member Data Documentation

◆ m_currentMode

std::string CodeAnalyzer::m_currentMode
private

Definition at line 77 of file codeanalyzer.hpp.

Referenced by analyseCommands(), analyseFunctions(), and run().

◆ m_DRAW_FUNCS

std::vector<std::string> CodeAnalyzer::m_DRAW_FUNCS
private

Definition at line 66 of file codeanalyzer.hpp.

Referenced by analyseFunctions(), and CodeAnalyzer().

◆ m_editor

◆ m_hasProcedureDefinition

bool CodeAnalyzer::m_hasProcedureDefinition
private

Definition at line 82 of file codeanalyzer.hpp.

Referenced by analyseCommands(), analyseIdentifiers(), and run().

◆ m_MATOP_FUNCS

std::vector<std::string> CodeAnalyzer::m_MATOP_FUNCS
private

Definition at line 65 of file codeanalyzer.hpp.

Referenced by analyseFunctions(), and CodeAnalyzer().

◆ m_nCurPos

int CodeAnalyzer::m_nCurPos
private

◆ m_nCurrentLine

int CodeAnalyzer::m_nCurrentLine
private

◆ m_options

Options* CodeAnalyzer::m_options
private

◆ m_sCurrentLine

std::string CodeAnalyzer::m_sCurrentLine
private

Definition at line 74 of file codeanalyzer.hpp.

Referenced by addToAnnotation(), and run().

◆ m_sError

std::string CodeAnalyzer::m_sError
private

◆ m_sNote

std::string CodeAnalyzer::m_sNote
private

Definition at line 70 of file codeanalyzer.hpp.

Referenced by analyseCommands(), analyseIdentifiers(), CodeAnalyzer(), and run().

◆ m_sStyles

std::string CodeAnalyzer::m_sStyles
private

Definition at line 75 of file codeanalyzer.hpp.

Referenced by addToAnnotation(), and run().

◆ m_STRING_FUNCS

std::vector<std::string> CodeAnalyzer::m_STRING_FUNCS
private

Definition at line 67 of file codeanalyzer.hpp.

Referenced by analyseFunctions(), and CodeAnalyzer().

◆ m_STRING_METHODS

std::vector<std::string> CodeAnalyzer::m_STRING_METHODS
private

Definition at line 68 of file codeanalyzer.hpp.

Referenced by analyseFunctions(), and CodeAnalyzer().

◆ m_sWarn

std::string CodeAnalyzer::m_sWarn
private

◆ m_symdefs

SymDefManager CodeAnalyzer::m_symdefs
private

Definition at line 63 of file codeanalyzer.hpp.

Referenced by analyseCommands(), analyseIdentifiers(), and run().

◆ m_vLocalVariables

std::vector<std::pair<std::string,int> > CodeAnalyzer::m_vLocalVariables
private

Definition at line 83 of file codeanalyzer.hpp.

Referenced by analyseCommands(), and analyseIdentifiers().

◆ MAXCOMMENTDENSITY

const double CodeAnalyzer::MAXCOMMENTDENSITY = 1.5
private

Definition at line 86 of file codeanalyzer.hpp.

Referenced by analyseCommands(), and run().

◆ MAXCOMPLEXITYNOTIFY

const int CodeAnalyzer::MAXCOMPLEXITYNOTIFY = 15
private

Definition at line 87 of file codeanalyzer.hpp.

Referenced by analyseCommands(), and run().

◆ MAXCOMPLEXITYWARN

const int CodeAnalyzer::MAXCOMPLEXITYWARN = 20
private

Definition at line 88 of file codeanalyzer.hpp.

Referenced by analyseCommands(), and run().

◆ MAXLINESOFCODE

const int CodeAnalyzer::MAXLINESOFCODE = 100
private

Definition at line 89 of file codeanalyzer.hpp.

Referenced by analyseCommands(), and run().

◆ MINCOMMENTDENSITY

const double CodeAnalyzer::MINCOMMENTDENSITY = 0.5
private

Definition at line 85 of file codeanalyzer.hpp.

Referenced by analyseCommands(), and run().


The documentation for this class was generated from the following files: