⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 check1.cpp

📁 代码检查工具 只要简单一些switch case的配对 以及类里面memset的使用
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Check1.cpp: implementation of the CCheck class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "check.h"
#include "Check1.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

#define ICON_H		(0)
#define ICON_C		(1)
#define ICON_CPP	(2)
#define ICON_ERR	(3)
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CCheck::CCheck()
{
	m_buf	= 0;
	m_len	= 0;
	m_tree	= 0; 
	m_item	= 0;
	m_file	= 0;
	m_last_alert = 0;
	m_opt.return_as_break = true;
}

CCheck::~CCheck()
{
}

void CCheck::check_file(const char* file)
{
	//file = "E:\\wqzhang\\hlqclient\\MyShell\\DlgAuctionItemWareHouse.cpp";//@del
	//file = "DlgAuctionItemWareHouse.cpp";
	//file = "E:\\wqzhang\\hlqclient\\MyShell\\DbgFunc.h";//@del
	//file = "E:\\wqzhang\\hlqclient\\MyShell\\ChatFaceManager.h";//@del
	//file = "test.cpp";
	
	if (!this->read_file(file))
		return;
	if (m_buf == 0 || m_len == 0)
		return;
	char* old_buf = m_buf;
	if (m_tree == 0)
		return;
	m_item = 0; m_file = (char*)file;
	
	this->init_line_info();
	while (this->get_token("switch"))
	{
		this->check_switch();
		//this->check_char();
	}
	
	m_item = 0; m_file = 0;
	m_buf  = old_buf;
	this->clr_buf();
// 	if (!m_tree->ItemHasChildren(m_item))
// 		m_tree->DeleteItem(m_item);
}

void CCheck::check_char()
{
	//@todo...
}

void CCheck::check_switch()
{
	this->move_next(6);
	if (m_len <= 0)
		return;
	
	if (!this->get_token("{"))
		return;
	this->move_next();

	while (1)
	{
		const char* token = this->get_next_token();
		if (token == 0)
			return;

		if (strcmp(token, "case") == 0)
		{
			char* old_buf = m_buf;
			int   old_len = m_len;
			this->move_next(strlen("case"));
			while (1)
			{
				const char* next_token = this->get_next_token();
				if (next_token != 0 && strcmp(next_token, "case") == 0)
				{
					old_buf = m_buf;
					old_len = m_len;
					this->move_next(strlen(next_token));
				}
				else
				{
					m_buf = old_buf;
					m_len = old_len;
					break;
				}
			}
			this->check_case(m_buf);
		}
		else if (strcmp(token, "default") == 0)
		{
			this->check_default(m_buf);
		}
		else if (strcmp(token, "}") == 0)
		{
			this->move_next();
			return; //switch ends!
		}
		else
			this->move_next();
	}
}

void CCheck::check_default(const char* ptr)
{
	this->move_next(strlen("default"));
	if (m_len <= 0)
		return;

	if (!this->get_token(":"))
		return;

	while (1)
	{
		const char* next_token = this->get_next_token();
		if (next_token == 0)
			break;
		if (strcmp(next_token, "{") == 0)
		{
			this->check_stmt(ptr);
		}
		else if (strcmp(next_token, "break") == 0)
		{
			this->move_next();
			break;
		}
		else if (strcmp(next_token, "return") == 0)
		{
			this->move_next();
			break;
		}
		else if (strcmp(next_token, "}") == 0)
		{
			//this "}" token belongs to outside statement, don't move it.
			break;
		}
		this->move_next();
	}
}

void CCheck::check_case(const char* ptr)
{
	this->move_next(4);
	if (m_len <= 0)
		return;

	if (!this->get_token(":"))
		return;
	
	while (1)
	{
		const char* token = this->get_next_token();
		if (token == 0)
		{
			this->alert(ptr);
			return;
		}
		
		if (strcmp(token, "switch") == 0)
		{
			this->check_switch(); //nested switch is ok
		}
		else if (strcmp(token, "case") == 0)
		{
			//"break" not found, while "case" found!
			this->alert(ptr);
			return;
		}
		else if (strcmp(token, "break") == 0)
		{
			this->move_next();
			return;//this is correct
		}
		else if (strcmp(token, "return") == 0)
		{
			this->move_next();
			if (m_opt.return_as_break)
				return;//this is correct
		}
		else if (strcmp(token, ";") == 0)
		{
			//this->move_next(); //dont do this, move next in check_stmt()!
			this->check_stmt(ptr); //more statment go here...
		}
		else if (strcmp(token, "{") == 0)
		{
			bool is_break_in_stmt = false;
			this->check_stmt(ptr, is_break_in_stmt); //more statment go here...
			//this->move_next();//@del
			if (is_break_in_stmt)
				return;
		}
		else if (strcmp(token, "}") == 0)
		{
			//alert: "break" not found! while "}" found!
			this->alert(ptr);
			return;			
		}
		else if (strcmp(token, "default") == 0)
		{
			//alert: "break" not found! while "default" found!
			this->alert(ptr);
			return;
		}
	}
}

void CCheck::check_stmt	(const char* ptr)
{
	bool is_break_in_stmt = false;
	check_stmt(ptr, is_break_in_stmt);
}

void CCheck::check_stmt(const char* ptr, bool& is_break_in_stmt)
{
	is_break_in_stmt = false;
	bool has_bra = m_buf[0] == '{';

	while (1)
	{
		this->move_next();
		const char* token = this->get_next_token();
		if (token == 0)
		{
			this->alert(ptr);
			return;
		}
		
		if (strcmp(token, "switch") == 0)
		{
			this->check_switch(); //nested switch is ok
		}
		else if (strcmp(token, "case") == 0)
		{
			//alert: "break" not found, while "case" found!
			this->alert(ptr);
			return;
		}
		else if (strcmp(token, "default") == 0)
		{
			//alert: "break" not found, while "default" found!
			this->alert(ptr);
			return;
		}
		else if (strcmp(token, "break") == 0)
		{
			is_break_in_stmt = true;
			if (!has_bra)
				return;//ok, this is correct
		}
		else if (strcmp(token, "return") == 0)
		{
			if (m_opt.return_as_break && !has_bra)
				return;//ok, this is correct
		}
		else if (strcmp(token, ";") == 0)
		{
			continue;
		}
		else if (strcmp(token, "{") == 0)
		{
			this->check_stmt(ptr); //continuious statment is ok
		}
		else if (strcmp(token, "}") == 0)
		{
			if (has_bra)
				this->move_next(1);
			return;
		}
	}
}

bool CCheck::read_file(const char* file)
{
	//open file
	FILE* fp = fopen(file, "rb");
	if (!fp) 
		return false;
	
	//get file len
	fseek(fp, 0L, SEEK_END);
	m_len = ftell(fp);
	if (m_len == 0) 
	{
		fclose(fp);
		return true;
	}
	fseek(fp, 0L, SEEK_SET);
	
	//alloc buf
	m_buf = new char[m_len + 1];
	if (!m_buf) 
	{
		fclose(fp);
		return false;
	}
	memset(m_buf, 0, m_len + 1);
	
	//read file into buf
	int n = fread(m_buf, 1, m_len, fp);
	if (n != m_len) 
	{
		//printf("read file failed: %d < %d\n", n, len);
		fclose(fp);
		return false;
	}
	
	//close file
	fclose(fp);
	return true;
}

bool CCheck::is_var_char(const char c)
{
	return 
		(c >= '0' && c <= '9') ||
		(c >= 'a' && c <= 'z') ||
		(c >= 'A' && c <= 'Z') ||
		(c == '$' || c == '_');
}

bool CCheck::is_white_space()
{
	const char c = m_buf[0];
	return 
		c == ' ' || 
		c == '\t' ||
		c == '\r' ||
		c == '\n';
}

bool CCheck::is_comment()
{
	const char c0 = m_buf[0];
	const char c1 = m_buf[1];
	return 
		(c0 == '/' && c1 == '/') ||
		(c0 == '/' && c1 == '*');
}

bool CCheck::is_quote()
{
	const char c0 = m_buf[0];
	return 
		c0 == '\'' ||
		c0 == '\"';
}

void CCheck::skip_quotes()
{
	const char sng_quote = '\'';
	const char dbl_quote = '\"';
	const char slash = '\\';
	while (m_len > 0)
	{
		const char c = m_buf[0];
		if (c != sng_quote && c != dbl_quote)
			return;
		
		if (c == sng_quote)
		{
			this->move_next();
			while (m_len > 0)
			{
				if (m_buf[0] == sng_quote)
				{
					this->move_next();
					break;
				}
				if (m_buf[0] == slash && m_buf[1] == sng_quote)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -