cppcheck-verify: changed it into a Qt project

This commit is contained in:
Daniel Marjamäki 2010-09-13 21:00:24 +02:00
parent b0bb71ee20
commit d3dcb5ab1c
6 changed files with 270 additions and 90 deletions

View File

@ -1,15 +0,0 @@
FILES = ../lib/errorlogger.cpp \
../lib/filelister.cpp \
../lib/filelister_win32.cpp \
../lib/filelister_unix.cpp \
../lib/mathlib.cpp \
../lib/path.cpp \
../lib/preprocessor.cpp \
../lib/settings.cpp \
../lib/token.cpp \
../lib/tokenize.cpp
HDRS = $(FILES:%.cpp=%.h)
cppcheck-verify: main.cpp $(FILES) $(HDRS)
g++ -Wall -I../lib -o cppcheck-verify main.cpp $(FILES)

View File

@ -0,0 +1,31 @@
TARGET = cppcheck-verify
TEMPLATE = app
INCLUDEPATH += ../lib
SOURCES += main.cpp \
mainwindow.cpp \
../lib/tokenize.cpp \
../lib/token.cpp \
../lib/settings.cpp \
../lib/preprocessor.cpp \
../lib/path.cpp \
../lib/mathlib.cpp \
../lib/filelister_win32.cpp \
../lib/filelister_unix.cpp \
../lib/filelister.cpp \
../lib/errorlogger.cpp
HEADERS += mainwindow.h \
../lib/tokenize.h \
../lib/token.h \
../lib/settings.h \
../lib/preprocessor.h \
../lib/path.h \
../lib/mathlib.h \
../lib/filelister_win32.h \
../lib/filelister_unix.h \
../lib/filelister.h \
../lib/errorlogger.h
FORMS += mainwindow.ui

View File

@ -1,6 +1,6 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team.
* Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -16,80 +16,13 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "preprocessor.h"
#include "tokenize.h"
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
/**
* Check that array indexes are within bounds
* 1. Locate array access through: [ .. ]
* 2. Try to determine if index is within bounds.
* 3. If it fails to determine that the index is within bounds then write warning
* \param tokenizer The tokenizer
* \param errout output stream to write warnings to
*/
static void arrayIndex(const Tokenizer &tokenizer, std::ostream &errout)
int main(int argc, char *argv[])
{
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
{
// 1. Locate array access through: [ .. ]
if (tok->str() == "[")
{
// 2. try to determine if the array index is within bounds
// array declaration
if (Token::simpleMatch(tok, "[ ]"))
continue;
if (Token::Match(tok->tokAt(-2), "%type% %var% [ %num% ] ;|="))
continue;
// 3. If it fails to determine that the index is within bounds then write warning
errout << tokenizer.fileLine(tok)
<< " failed to determine if given array index is within bounds"
<< std::endl;
}
}
}
int main(int argc, const char *argv[])
{
if (argc != 2)
{
std::cerr << "syntax: cppcheck-verify file.cpp" << std::endl;
return 0;
}
const std::string fileName = argv[1];
Tokenizer tokenizer;
{
// Preprocess the file..
Preprocessor preprocessor;
std::ifstream fin(fileName.c_str());
std::string filedata;
std::list<std::string> configurations;
std::list<std::string> includePaths;
preprocessor.preprocess(fin,
filedata,
configurations,
fileName,
includePaths);
filedata = Preprocessor::getcode(filedata, "", fileName, NULL);
// Tokenize the preprocessed code..
std::istringstream istr(filedata);
tokenizer.tokenize(istr, fileName.c_str(), "");
}
// Check the tokens..
arrayIndex(tokenizer, std::cerr);
return 0;
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

140
verify/mainwindow.cpp Normal file
View File

@ -0,0 +1,140 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "preprocessor.h"
#include "tokenize.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <set>
static void arrayIndex(const Tokenizer &tokenizer, std::set<unsigned int> &errorlines);
static unsigned char readChar(std::istream &istr)
{
unsigned char ch = (unsigned char)istr.get();
// Handling of newlines..
if (ch == '\r')
{
ch = '\n';
if ((char)istr.peek() == '\n')
(void)istr.get();
}
return ch;
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
const std::string fileName("../lib/tokenize.cpp");
setWindowTitle(fileName.c_str());
Tokenizer tokenizer;
{
// Preprocess the file..
Preprocessor preprocessor;
std::ifstream fin(fileName.c_str());
std::string filedata;
std::list<std::string> configurations;
std::list<std::string> includePaths;
preprocessor.preprocess(fin,
filedata,
configurations,
fileName,
includePaths);
filedata = Preprocessor::getcode(filedata, "", fileName, NULL, NULL);
// Tokenize the preprocessed code..
std::istringstream istr(filedata);
tokenizer.tokenize(istr, fileName.c_str(), "");
}
// Check the tokens..
std::set<unsigned int> errorlines;
arrayIndex(tokenizer, errorlines);
// show report..
{
std::ostringstream report;
report << std::string(8,' ');
unsigned int lineno = 1;
std::ifstream fin(fileName.c_str());
for (unsigned char c = readChar(fin); fin.good(); c = readChar(fin))
{
report << c;
if (c == '\n')
{
++lineno;
if (errorlines.find(lineno) != errorlines.end())
report << std::string(6,'!') << " ";
else
report << std::string(8,' ');
}
}
ui->plainTextEdit->setPlainText(QString::fromStdString(report.str()));
}
}
MainWindow::~MainWindow()
{
delete ui;
}
/**
* Check that array indexes are within bounds
* 1. Locate array access through: [ .. ]
* 2. Try to determine if index is within bounds.
* 3. If it fails to determine that the index is within bounds then write warning
* \param tokenizer The tokenizer
* \param errout output stream to write warnings to
*/
static void arrayIndex(const Tokenizer &tokenizer, std::set<unsigned int> &errorlines)
{
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
{
// 1. Locate array access through: [ .. ]
if (tok->fileIndex() == 0 && tok->str() == "[")
{
// 2. try to determine if the array index is within bounds
// array declaration
if (Token::simpleMatch(tok, "[ ]"))
continue;
if (Token::Match(tok->tokAt(-2), "%type% %var% [ %num% ] ;|="))
continue;
// 3. If it fails to determine that the index is within bounds then write warning
errorlines.insert(tok->linenr());
}
}
}

41
verify/mainwindow.h Normal file
View File

@ -0,0 +1,41 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QMainWindow>
namespace Ui
{
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

50
verify/mainwindow.ui Normal file
View File

@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>600</width>
<height>400</height>
</rect>
</property>
<property name="windowTitle">
<string>Cppcheck-Verify</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPlainTextEdit" name="plainTextEdit">
<property name="plainText">
<string>ab</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>600</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>