Files
qt-creator/tests/manual/cplusplus/main.cpp

169 lines
4.3 KiB
C++
Raw Normal View History

/**************************************************************************
2008-12-03 11:37:35 +01:00
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
2008-12-03 11:37:35 +01:00
**
** Contact: Nokia Corporation (qt-info@nokia.com)
2008-12-03 11:37:35 +01:00
**
** Commercial Usage
2008-12-03 11:37:35 +01:00
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
2008-12-03 11:37:35 +01:00
**
** GNU Lesser General Public License Usage
2008-12-03 11:37:35 +01:00
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
2008-12-03 11:37:35 +01:00
**
** If you are unsure which license is appropriate for your use, please
2009-08-14 09:30:56 +02:00
** contact the sales department at http://qt.nokia.com/contact.
2008-12-03 11:37:35 +01:00
**
**************************************************************************/
2008-12-09 15:25:01 +01:00
#include <AST.h>
2009-01-02 16:10:28 +01:00
#include <ASTVisitor.h>
2009-11-13 12:13:49 +01:00
#include <ASTMatcher.h>
2008-12-09 15:25:01 +01:00
#include <Control.h>
#include <Scope.h>
#include <Semantic.h>
#include <TranslationUnit.h>
#include <Literals.h>
#include <Symbols.h>
#include <Names.h>
#include <CoreTypes.h>
#include <CppDocument.h>
2008-12-09 15:25:01 +01:00
2009-01-06 15:15:13 +01:00
#include <QFile>
#include <QList>
#include <QCoreApplication>
#include <QStringList>
#include <QFileInfo>
#include <QTime>
2009-01-02 16:10:28 +01:00
#include <QtDebug>
#include <cstdio>
#include <cstdlib>
2009-01-06 15:15:13 +01:00
#include <iostream>
2009-10-21 14:56:42 +02:00
using namespace CPlusPlus;
2009-01-02 16:10:28 +01:00
class PatternBuilder
{
public:
PatternBuilder()
: pool(new MemoryPool()) {}
~PatternBuilder()
{ delete pool; }
UnaryExpressionAST *CreateUnaryExpression(ExpressionAST *expr = 0)
{
UnaryExpressionAST *ast = new (pool) UnaryExpressionAST;
ast->expression = expr;
return ast;
}
BinaryExpressionAST *CreateBinaryExpression(ExpressionAST *left = 0, ExpressionAST *right = 0)
{
BinaryExpressionAST *ast = new (pool) BinaryExpressionAST;
ast->left_expression = left;
ast->right_expression = right;
return ast;
}
NumericLiteralAST *NumericLiteral()
{
NumericLiteralAST *ast = new (pool) NumericLiteralAST;
return ast;
}
SimpleNameAST *SimpleName()
{
SimpleNameAST *ast = new (pool) SimpleNameAST;
return ast;
}
IfStatementAST *IfStatement(ExpressionAST *cond = 0, StatementAST *iftrue = 0, StatementAST *iffalse = 0)
{
IfStatementAST *ast = new (pool) IfStatementAST;
ast->condition = cond;
ast->statement = iftrue;
ast->else_statement = iffalse;
return ast;
}
CompoundStatementAST *CompoundStatement()
{
CompoundStatementAST *ast = new (pool) CompoundStatementAST;
return ast;
}
private:
MemoryPool *pool;
};
class ForEachNode: protected ASTVisitor
2009-01-02 16:10:28 +01:00
{
Document::Ptr doc;
AST *pattern;
2009-01-06 15:15:13 +01:00
public:
ForEachNode(Document::Ptr doc)
: ASTVisitor(doc->control()), doc(doc),
matcher(doc->translationUnit()) {}
2009-01-06 15:15:13 +01:00
void operator()() { accept(doc->translationUnit()->ast()); }
2009-01-06 15:15:13 +01:00
protected:
using ASTVisitor::visit;
2009-10-21 14:56:42 +02:00
virtual bool preVisit(AST *ast)
{
PatternBuilder ir;
//IfStatementAST *pattern = ir.IfStatement(ir.SimpleName());
CompoundStatementAST *pattern = ir.CompoundStatement();
if (ast->match(ast, pattern, &matcher))
translationUnit()->warning(ast->firstToken(), "matched");
return true;
2009-01-06 15:15:13 +01:00
}
ASTMatcher matcher;
};
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QStringList files = app.arguments();
files.removeFirst();
foreach (const QString &fileName, files) {
QFile file(fileName);
if (! file.open(QFile::ReadOnly))
continue;
const QByteArray source = file.readAll();
file.close();
Document::Ptr doc = Document::create(fileName);
doc->control()->setDiagnosticClient(0);
doc->setSource(source);
doc->parse();
ForEachNode forEachNode(doc);
forEachNode();
}
return EXIT_SUCCESS;
}