/*!
 * Parses lines for function/method names.
 *
 * \param line line to be processed
 * \param lastline last line processed
 * \param functionStack stack of functions
 * \param functionName function name found
 *
 * \return 1 if function name is found
 */
int CColdFusionCounter::ParseFunctionName(const string &line, string &/*lastline*/,
		filemap &functionStack, string &functionName, unsigned int &functionCount)
{
    
	size_t idx;

	idx = line.find("cffunction");
	
	if (idx != string::npos)
	{
		idx=line.find("name");
        	if (idx != string::npos)
		{
            		string str;
            		str = line.substr(idx);
            		idx = str.find("\"");
            		str = str.substr(idx + 1);
            		idx = str.find("\"");
            		str = str.substr(0, idx);
       		
	    		lineElement element(++functionCount, str);
        		functionStack.push_back(element);
            
        	}
	}
	
	
    	if (functionStack.empty())
	{
        	// dealing with some code out of any subroutines, it a "main" code
        	return 2;
    	}
	idx=line.find("/cffunction");
	if (idx != string::npos)
    	{
        	functionName = functionStack.back().line;
		functionCount = functionStack.back().lineNumber;
        	functionStack.pop_back();
        	return 1;
	}
	return 0;
}
/*!
* Parses lines for function/method names.
*
* \param line line to be processed
* \param functionStack stack of functions
* \param functionName function name found
* \param functionCount function count found
*
* \return 1 if function name is found
*/
int CIdlCounter::ParseFunctionName(const string &line, string &/*lastline*/,
    filemap &functionStack, string &functionName, unsigned int &functionCount)
{
    string str;
    size_t idx;
    unsigned int fcnt;

    idx = CUtil::FindKeyword(line, "pro", 0, TO_END_OF_STRING, false);
    if (idx != string::npos)
    {
        if (idx + 4 < line.length())
        {
            str = line.substr(idx + 4);
            str += ",";  // append "," so that a pro/function with no inputs is counted 
            lineElement element(++functionCount, str);
            functionStack.push_back(element);
        }
    }
    else
    {
        idx = CUtil::FindKeyword(line, "function", 0, TO_END_OF_STRING, false);
        if (idx != string::npos)
        {
            if (idx + 9 < line.length())
            {
                str = line.substr(idx + 9);
                str += ","; // append "," so that a pro/function with no inputs is counted 
                lineElement element(++functionCount, str);
                functionStack.push_back(element);
            }
        }
    }

    if (functionStack.empty())
    {
        // dealing with some code out of any subroutines, it a "main" code
        return 2;
    }

    idx = CUtil::FindKeyword(line, "end", 0, TO_END_OF_STRING, false);
    if (idx != string::npos)
    {
        str = functionStack.back().line;
        fcnt = functionStack.back().lineNumber;
        functionStack.pop_back();
        idx = str.find(",");
        if (idx != string::npos)
        {
            functionName = CUtil::ClearRedundantSpaces(str.substr(0, idx));
            functionCount = fcnt;
            return 1;
        }
    }
    return 0;
}
/*!
* Parses lines for function/method names.
*
* \param line line to be processed
* \param lastline last line processed
* \param functionStack stack of functions
* \param functionName function name found
* \param functionCount function count found
*
* \return 1 if function name is found
*/
int CJavascriptCounter::ParseFunctionName(const string &line, string &lastline,
		filemap &functionStack, string &functionName, unsigned int &functionCount)
{
        string tline, str, front, back;
	size_t idx, tidx, cnt, cnt2;
	unsigned int fcnt;

	tline = CUtil::TrimString(line);
	idx = tline.find('{');
	if (idx != string::npos)
	{
		// check whether it is at first index, if yes then function name is at above line
		if (idx == 0)
		{
			idx = lastline.find("function");
			if (idx != string::npos)
			{
				front = lastline.substr(0, idx);
				back = lastline.substr(idx + 8);

				idx = front.find('=');
				if (idx != string::npos)
				{
					front = front.substr(0, idx);
					lineElement element(++functionCount, CUtil::TrimString(front));
					functionStack.push_back(element);
					lastline.erase();
				}
				else {
					idx = back.find('(');
					if (idx != string::npos)
					{
						back = back.substr(0, idx);
						lineElement element(++functionCount, CUtil::TrimString(back));
						functionStack.push_back(element);
						lastline.erase();
					}
				}
			}
		}
		else
		{
			str = tline.substr(0, idx);
			tidx = cnt = cnt2 = 0;
			if (str[0] != '(' && str[0] != ':' && (lastline.length() < 1 || lastline[lastline.length() - 1] != ':'))
			{
				while (tidx != string::npos)
				{
					tidx = str.find('(', tidx);
					if (tidx != string::npos)
					{
						cnt++;
						tidx++;
					}
				}
				if (cnt > 0)
				{
					tidx = 0;
					while (tidx != string::npos)
					{
						tidx = str.find(')', tidx);
						if (tidx != string::npos)
						{
							cnt2++;
							tidx++;
						}
					}
				}
			}
			// make sure parentheses are closed and no parent class listed
			if ((cnt > 0 && cnt == cnt2) || (lastline.length() > 0 && lastline[lastline.length() - 1] == ';'))
				lastline = str;
			else
				lastline += " " + str;
			idx = lastline.find("function");
			if (idx != string::npos)
			{
				front = lastline.substr(0, idx);
				back = lastline.substr(idx + 8);
				idx = front.find('=');
				if (idx != string::npos)
				{
					front = front.substr(0, idx);
					lineElement element(++functionCount, CUtil::TrimString(front));
					functionStack.push_back(element);
					lastline.erase();
				}
				else {
					idx = back.find('(');
					if (idx != string::npos)
					{
						back = back.substr(0, idx);
						lineElement element(++functionCount, CUtil::TrimString(back));
						functionStack.push_back(element);
						lastline.erase();
					}
				}
			}
		}
	}
	else if (tline.length() > 0 && tline[tline.length() - 1] != ';' &&
		lastline.length() > 0 && lastline[lastline.length() - 1] != ';')
	{
		// append until all parentheses are closed
		tidx = lastline.find('(');
		if (tidx != string::npos)
		{
			cnt = 1;
			while (tidx != string::npos)
			{
				tidx = lastline.find('(', tidx + 1);
				if (tidx != string::npos)
					cnt++;
			}
			tidx = lastline.find(')');
			while (tidx != string::npos)
			{
				cnt++;
				tidx = lastline.find(')', tidx + 1);
			}
			if (cnt % 2 != 0)
				lastline += " " + tline;
			else
				lastline = tline;
		}
		else
			lastline = tline;
	}
	else
		lastline = tline;

	if (functionStack.empty())
	{
		// dealing with some code out of any subroutines, it a "main" code
		return 2;
	}

	idx = CUtil::FindKeyword(line, "{");
	if (idx != string::npos)
	{
		lineElement element(functionCount, "{");
		functionStack.push_back(element);	
	}
	idx = CUtil::FindKeyword(line, "}");
	if (idx != string::npos)
        {
		functionStack.pop_back();
		str = functionStack.back().line;
		fcnt = functionStack.back().lineNumber;
		if (str != "{")
		{
			functionStack.pop_back();
			functionName = CUtil::ClearRedundantSpaces(str);
			functionCount = fcnt;
			return 1;
		}
	}	
	return 0;


}
/*!
 * Parses lines for function/method names.
 *
 * \param line line to be processed
 * \param lastline last line processed
 * \param functionStack stack of functions
 * \param functionName function name found
 *
 * \return 1 if function name is found
 */
int CBatchCounter::ParseFunctionName(const string &line, string &lastline,
		filemap &functionStack, string &functionName, unsigned int &functionCount)
{
	/*
	 * functionStack is used to record block names encountered, it will be poped when its closing "}" is encountered
	 * cyclomatic_stack is used to record the previous cyc_count when start of block is encountered, it will be poped
	 * 		when end of block is encountered and poped value will be added to the current cyc_cnt
	 */

	string tline, str;
    //size_t i, idx, tidx, cnt;
    size_t idx; // warning fix

    tline = CUtil::TrimString(line, 0);
	// Look for label
	idx = tline.find(':');
	if(idx == 0) {
		// check if there is any function in the stack that has noot been closed
		if(!functionStack.empty()) {
			// pop start of the block from functionStack
			functionName = functionStack.back().line;
			functionCount = functionStack.back().lineNumber;
			functionStack.pop_back();
			// push current function into stack
			lineElement element(++functionCount, CUtil::ClearRedundantSpaces(tline));
			functionStack.push_back(element);
			lastline.erase();

			return 1;
		}

		str = CUtil::ClearRedundantSpaces(tline);
		lineElement element(++functionCount, str);
		functionStack.push_back(element);
		lastline.erase();
	}

	// Look for end of function
	idx = CUtil::FindKeyword(tline, "goto");
	if(idx != string::npos) {
		str = CUtil::TrimString(tline.substr(idx+4));
		idx = CUtil::FindKeyword(str, ":eof");
	}
	if(idx == 0 && !functionStack.empty()) {
		functionName = functionStack.back().line;
		functionCount = functionStack.back().lineNumber;
		functionStack.pop_back();
		lastline.erase();
		return 1;
	}

	// update lastline
	lastline = tline;

	// code that belong to "main" block
	if(functionStack.empty()) {
		return 2;
	}

	return 0;

}
/*!
* Parses lines for function/method names.
*
* \param line line to be processed
* \param lastline last line processed
* \param functionStack stack of functions
* \param functionName function name found
* \param functionCount function count found
*
* \return 1 if function name is found
*/
int CScalaCounter::ParseFunctionName(const string &line, string &lastline,
	filemap &functionStack, string &functionName, unsigned int &functionCount)
{
	string tline, str;
	size_t idx, tidx, cnt, cnt2;
	unsigned int fcnt, cyclomatic_cnt = 0;
	string exclude = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_$";

	tline = CUtil::TrimString(line);
	idx = tline.find('{');
	if (idx != string::npos)
	{
		// check whether it is at first index, if yes then function name is at above line
		if (idx == 0)
		{
			lineElement element(++functionCount, lastline);
			functionStack.push_back(element);
			lastline.erase();
		}
		else
		{
			str = tline.substr(0, idx);
			tidx = cnt = cnt2 = 0;
			if (str[0] != '(' && str[0] != ':' && (lastline.length() < 1 || lastline[lastline.length() - 1] != ':'))
			{
				while (tidx != string::npos)
				{
					tidx = str.find('(', tidx);
					if (tidx != string::npos)
					{
						cnt++;
						tidx++;
					}
				}
				if (cnt > 0)
				{
					tidx = 0;
					while (tidx != string::npos)
					{
						tidx = str.find(')', tidx);
						if (tidx != string::npos)
						{
							cnt2++;
							tidx++;
						}
					}
				}
			}
			// make sure parentheses are closed and no parent class listed
			if ((cnt > 0 && cnt == cnt2) || (lastline.length() > 0 && lastline[lastline.length() - 1] == ';'))
				lastline = str;
			else
				lastline += " " + str;
			lineElement element(++functionCount, CUtil::TrimString(lastline));
			functionStack.push_back(element);
			lastline.erase();
		}
	}
	else if (tline.length() > 0 && tline[tline.length() - 1] != ';' &&
		lastline.length() > 0 && lastline[lastline.length() - 1] != ';')
	{
		// append until all parentheses are closed
		tidx = lastline.find('(');
		if (tidx != string::npos)
		{
			cnt = 1;
			while (tidx != string::npos)
			{
				tidx = lastline.find('(', tidx + 1);
				if (tidx != string::npos)
					cnt++;
			}
			tidx = lastline.find(')');
			while (tidx != string::npos)
			{
				cnt++;
				tidx = lastline.find(')', tidx + 1);
			}
			if (cnt % 2 != 0)
				lastline += " " + tline;
			else
				lastline = tline;
		}
		else
			lastline = tline;
	}
	else
		lastline = tline;

	idx = line.find('}');
	if (idx != string::npos && !functionStack.empty())
	{
		str = functionStack.back().line;
		fcnt = functionStack.back().lineNumber;
		functionStack.pop_back();
		idx = str.find('(');

		if (idx != string::npos)
		{
			// search for cyclomatic complexity keywords and other possible keywords
			CUtil::CountTally(str, cmplx_cyclomatic_list, cyclomatic_cnt, 1, exclude, "", "", 0, casesensitive);
			if (cyclomatic_cnt <= 0 && CUtil::FindKeyword(str, "match") == string::npos &&
				CUtil::FindKeyword(str, "try") == string::npos && CUtil::FindKeyword(str, "finally") == string::npos &&
				CUtil::FindKeyword(str, "return") == string::npos && str.find('=') == string::npos)
			{
				string myFunctionName = CUtil::ClearRedundantSpaces(str.substr(0, idx));

			#ifdef	_DEBUG
				if ( myFunctionName.size() == 0 )
				{
					// PrintCyclomaticComplexity will now skip any empty Function Names.
					// Done this way as is easier and less Risk of adding Defects from complex Cyclomatic Complexity code
					// TODO: Go through the CC code and the ParseFunctionName code and try to SAFELY improve.
				//	cout << endl << "CScalaCounter::ParseFunctionName() Empty function name allowed" << endl;
				}
			#endif
				// May have the actual arg list so look for first ( and do not copy
				size_t myIdx = myFunctionName.find( '(' );
				if ( myIdx != string::npos )
					myFunctionName = myFunctionName.substr( 0, myIdx );
				functionName = myFunctionName;
				functionCount = fcnt;
				lastline.erase();
				return 1;
			}
		}
		lastline.erase();
	}
	return 0;
}
/*!
* Parses lines for function/method names.
*
* \param line line to be processed
* \param lastline last line processed
* \param functionStack stack of functions
* \param functionName function name found
* \param functionCount function count found
*
* \return 1 if function name is found
*/
int CAdaCounter::ParseFunctionName(const string &line, string &/*lastline*/,
	filemap &functionStack, string &functionName, unsigned int &functionCount)
{
	string str;
	size_t idx;
	//unsigned int fcnt; waring fix

	idx = CUtil::FindKeyword(line, "procedure");
	if (idx != string::npos)
	{
		if (idx + 10 < line.length())
		{
			str = line.substr(idx + 10);
			idx = str.find("is");
			if (idx != string::npos)
			{
				str = CUtil::ClearRedundantSpaces(str.substr(0, idx));
			}
			lineElement element(++functionCount, str);
			functionStack.push_back(element);
		}
	}
	else
	{
		idx = CUtil::FindKeyword(line, "function");
		if (idx != string::npos)
		{
			if (idx + 9 < line.length())
			{
				str = line.substr(idx + 9);
				idx = str.find("(");
				if (idx != string::npos)
				{
					str = CUtil::ClearRedundantSpaces(str.substr(0, idx));
				}
				lineElement element(++functionCount, str);
				functionStack.push_back(element);
			}
		}
	}

	idx = CUtil::FindKeyword(line, "with");
	if (idx == string::npos)
	{
		idx = CUtil::FindKeyword(line, "use");
		if (idx == string::npos)
		{
			if (functionStack.empty())
			{
				// dealing with some code out of any subroutines, it a "main" code
				return 2;
			}
		}
	}

	idx = CUtil::FindKeyword(line, "end");
	if (idx != string::npos)
	{
            	idx = line.find(functionStack.back().line);
            	if (idx != string::npos)
		{
			functionName = functionStack.back().line;
			functionCount = functionStack.back().lineNumber;
			functionStack.pop_back();
			return 1;
		}
	}
	
	return 0;
}
Example #7
0
/*!
* Parses lines for function/method names.
*
* \param line line to be processed
* \param lastline last line processed
* \param functionStack stack of functions
* \param functionName function name found
* \param functionCount function count found
*
* \return 1 if function name is found
*/
int CCJavaCsCounter::ParseFunctionName(const string &line, string &lastline,
	filemap &functionStack, string &functionName, unsigned int &functionCount)
{
	string tline, str;
	size_t idx, tidx, cnt, cnt2;
	unsigned int fcnt, cyclomatic_cnt = 0;
	string exclude = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_$";

	tline = CUtil::TrimString(line);
	idx = tline.find('{');
	if (idx != string::npos)
	{
		// check whether it is at first index, if yes then function name is at above line
		if (idx == 0)
		{
			lineElement element(++functionCount, lastline);
			functionStack.push_back(element);
			lastline.erase();
		}
		else
		{
			str = tline.substr(0, idx);
			tidx = cnt = cnt2 = 0;
			if (str[0] != '(' && str[0] != ':' && (lastline.length() < 1 || lastline[lastline.length() - 1] != ':'))
			{
				while (tidx != string::npos)
				{
					tidx = str.find('(', tidx);
					if (tidx != string::npos)
					{
						cnt++;
						tidx++;
					}
				}
				if (cnt > 0)
				{
					tidx = 0;
					while (tidx != string::npos)
					{
						tidx = str.find(')', tidx);
						if (tidx != string::npos)
						{
							cnt2++;
							tidx++;
						}
					}
				}
			}
			// make sure parentheses are closed and no parent class listed
			if ((cnt > 0 && cnt == cnt2) || (lastline.length() > 0 && lastline[lastline.length() - 1] == ';'))
				lastline = str;
			else
				lastline += " " + str;
			lineElement element(++functionCount, CUtil::TrimString(lastline));
			functionStack.push_back(element);
			lastline.erase();
		}
	}
	else if (tline.length() > 0 && tline[tline.length() - 1] != ';' &&
		lastline.length() > 0 && lastline[lastline.length() - 1] != ';')
	{
		// append until all parentheses are closed
		tidx = lastline.find('(');
		if (tidx != string::npos)
		{
			cnt = 1;
			while (tidx != string::npos)
			{
				tidx = lastline.find('(', tidx + 1);
				if (tidx != string::npos)
					cnt++;
			}
			tidx = lastline.find(')');
			while (tidx != string::npos)
			{
				cnt++;
				tidx = lastline.find(')', tidx + 1);
			}
			if (cnt % 2 != 0)
				lastline += " " + tline;
			else
				lastline = tline;
		}
		else
			lastline = tline;
	}
	else
		lastline = tline;

	idx = line.find('}');
	if (idx != string::npos && !functionStack.empty())
	{
		str = functionStack.back().line;
		fcnt = functionStack.back().lineNumber;
		functionStack.pop_back();
		idx = str.find('(');

		if (idx != string::npos)
		{
			// search for cyclomatic complexity keywords and other possible keywords
			CUtil::CountTally(str, cmplx_cyclomatic_list, cyclomatic_cnt, 1, exclude, "", "", 0, casesensitive);
			if (cyclomatic_cnt <= 0 && CUtil::FindKeyword(str, "switch") == string::npos &&
				CUtil::FindKeyword(str, "try") == string::npos && CUtil::FindKeyword(str, "finally") == string::npos &&
				CUtil::FindKeyword(str, "return") == string::npos && str.find('=') == string::npos)
			{
				functionName = CUtil::ClearRedundantSpaces(str.substr(0, idx));
				functionCount = fcnt;
				lastline.erase();
				return 1;
			}
		}
		lastline.erase();
	}
	return 0;
}
/*!
* Parses lines for function/method names.
*
* \param line line to be processed
* \param lastline last line processed
* \param functionStack stack of functions
* \param functionName function name found
* \param functionCount function count found
*
* \return 1 if function name is found
*/
int CFortranCounter::ParseFunctionName(const string &line, string &/*lastline*/,
		filemap &functionStack, string &functionName, unsigned int &functionCount)
{
        string str;
	size_t idx;
	unsigned int fcnt;

	idx = CUtil::FindKeyword(line, "PROGRAM");
	if (idx != string::npos)
	{
		if (idx + 8 < line.length())
		{
			str = line.substr(idx + 8);
			lineElement element(++functionCount, str);
			functionStack.push_back(element);
		}
	}
	else
	{
		idx = CUtil::FindKeyword(line, "SUBROUTINE");
		if (idx != string::npos)
		{
			if (idx + 11 < line.length())
			{
				str = line.substr(idx + 11);
				lineElement element(++functionCount, str);
				functionStack.push_back(element);
			}
		}
	}

	if (functionStack.empty())
	{
		// dealing with some code out of any subroutines, it a "main" code
		return 2;
	}

	string temp = CUtil::ClearRedundantSpaces(line);
	idx = CUtil::FindKeyword(temp, "END");   
	if (idx != string::npos && temp == "END")
	{
		str = functionStack.back().line;
		fcnt = functionStack.back().lineNumber;
		functionStack.pop_back();
		idx = str.find("(");
		if (idx != string::npos)
		{
			functionName = CUtil::ClearRedundantSpaces(str.substr(0, idx));
			functionCount = fcnt;
			return 1;
		}
		else {
			functionName = CUtil::ClearRedundantSpaces(str);
			functionCount = fcnt;
			return 1;
		}
	}	
	return 0;


}
/*!
* Parses lines for function/method names.
*
* \param line line to be processed
* \param lastline last line processed
* \param functionStack stack of functions
* \param functionName function name found
* \param functionCount function count found
*
* \return 1 if function name is found
*/
int CPerlCounter::ParseFunctionName(const string &line, string &lastline,
	filemap &functionStack, string &functionName, unsigned int &functionCount)
{
	string tline, str;
	size_t i, idx, tidx, cnt;
	unsigned int fcnt;

	tline = CUtil::TrimString(line);
	idx = tline.find('{');
	if (idx != string::npos)
	{
		// check whether it is at first index, if yes then function name is at above line
		if (idx == 0)
		{
			lineElement element(++functionCount, lastline);
			functionStack.push_back(element);
			lastline.erase();
		}
		else
		{
			str = tline.substr(0, idx);
			if (str.find('(') != string::npos && str[0] != '(')
				lastline = str;
			else
				lastline += " " + str;
			lineElement element(++functionCount, CUtil::TrimString(lastline));
			functionStack.push_back(element);
			lastline.erase();
		}
	}
	else if (tline.length() > 0 && tline[tline.length() - 1] != ';' &&
		lastline.length() > 0 && lastline[lastline.length() - 1] != ';')
	{
		// append until all parenthesis are closed
		tidx = lastline.find('(');
		if (tidx != string::npos)
		{
			cnt = 1;
			while (tidx != string::npos)
			{
				tidx = lastline.find('(', tidx + 1);
				if (tidx != string::npos)
					cnt++;
			}
			tidx = lastline.find(')');
			while (tidx != string::npos)
			{
				cnt++;
				tidx = lastline.find(')', tidx + 1);
			}
			if (cnt % 2 != 0)
				lastline += " " + tline;
			else
				lastline = tline;
		}
		else
			lastline = tline;
	}
	else
		lastline = tline;

	idx = line.find('}');
	if (idx != string::npos && !functionStack.empty())
	{
		str = functionStack.back().line;
		fcnt = functionStack.back().lineNumber;
		functionStack.pop_back();
		idx = CUtil::FindKeyword(str, "sub");
		if (idx != string::npos && idx + 4 < str.length())
		{
			functionName = CUtil::ClearRedundantSpaces(str.substr(idx + 4));
			functionCount = fcnt;
			lastline.erase();
			return 1;
		}
		lastline.erase();
	}

	// check stack for any "sub"
	idx = string::npos;
	if (!functionStack.empty())
	{
		for (i = 0; i < functionStack.size(); i++)
		{
			idx = CUtil::FindKeyword(functionStack[i].line, "sub");
			if (idx != string::npos)
				break;
		}
	}
	if (idx == string::npos)
	{
		// dealing with some code out of any subroutines, it a "main" code
		return 2;
	}
	return 0;
}