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

159 lines
4.7 KiB
C++
Raw Normal View History

2009-10-23 12:58:39 +02:00
/**************************************************************************
**
** This file is part of Qt Creator
**
2011-01-11 16:28:15 +01:00
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
2009-10-23 12:58:39 +02:00
**
2011-04-13 08:42:33 +02:00
** Contact: Nokia Corporation (info@qt.nokia.com)
2009-10-23 12:58:39 +02:00
**
**
** GNU Lesser General Public License Usage
**
2011-04-13 08:42:33 +02:00
** 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.
2009-10-23 12:58:39 +02:00
**
2010-12-17 16:01:08 +01:00
** In addition, as a special exception, Nokia gives you certain additional
2011-04-13 08:42:33 +02:00
** rights. These rights are described in the Nokia Qt LGPL Exception
2010-12-17 16:01:08 +01:00
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
2011-04-13 08:42:33 +02:00
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
2010-12-17 16:01:08 +01:00
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
2009-10-23 12:58:39 +02:00
**
**************************************************************************/
2009-12-15 16:49:59 +01:00
#include "Preprocessor.h"
2010-08-13 17:37:33 +02:00
#include <CPlusPlus.h>
2009-10-23 12:58:39 +02:00
#include <string>
#include <cstdlib>
2009-12-15 16:38:33 +01:00
#include <sstream>
2009-10-23 12:58:39 +02:00
using namespace CPlusPlus;
2009-12-15 16:49:59 +01:00
enum { BLOCK_SIZE = 4 * 1024};
void parse(const char *fileName, const char *source, unsigned size);
int runWithSystemPreprocessor(int argc, char *argv[]);
int runWithNewPreprocessor(int argc, char *argv[]);
struct V: public ASTVisitor
{
V(TranslationUnit *unit)
: ASTVisitor(unit) {}
virtual bool visit(FunctionDeclaratorAST *ast)
{
if (ast->as_cpp_initializer) {
if (! (ast->symbol && ast->symbol->enclosingScope()))
; //translationUnit()->warning(ast->firstToken(), "resolved as function declaration");
else if (ast->symbol->enclosingScope()->isNamespace() || ast->symbol->enclosingScope()->isTemplate())
; //translationUnit()->warning(ast->firstToken(), "resolved as function declaration");
else if (ast->symbol->enclosingScope()->isBlock())
; //translationUnit()->warning(ast->firstToken(), "resolved as C++ initializer");
else
translationUnit()->warning(ast->firstToken(), "ambiguous function declarator or C++ intializer");
}
return true;
}
virtual bool visit(ExpressionOrDeclarationStatementAST *ast)
{
translationUnit()->warning(ast->firstToken(), "ambiguous expression or declaration statement");
return true;
}
};
2009-10-23 12:58:39 +02:00
int main(int argc, char *argv[])
2009-12-15 16:49:59 +01:00
{
if (getenv("CPLUSPLUS_WITH_NEW_PREPROCESSOR"))
return runWithNewPreprocessor(argc, argv);
return runWithSystemPreprocessor(argc, argv);
}
int runWithSystemPreprocessor(int argc, char *argv[])
2009-10-23 12:58:39 +02:00
{
std::string cmdline;
cmdline += "gcc -E -xc++ -U__BLOCKS__ -D__restrict= -D__restrict__= -D__extension__= -D__imag__= -D__real__= -D__complex__= -D_Complex= -D__signed=signed";
2009-10-23 12:58:39 +02:00
for (int i = 1; i < argc; ++i) {
cmdline += ' ';
cmdline += argv[i];
}
char block[BLOCK_SIZE];
2009-12-15 16:49:59 +01:00
std::string preprocessedCode;
2009-10-23 12:58:39 +02:00
if (FILE *fp = popen(cmdline.c_str(), "r")) {
while (size_t sz = fread(block, 1, BLOCK_SIZE, fp))
2009-12-15 16:49:59 +01:00
preprocessedCode.append(block, sz);
2009-10-23 12:58:39 +02:00
pclose(fp);
} else {
fprintf(stderr, "c++: No such file or directory\n");
return EXIT_FAILURE;
}
2009-12-15 16:49:59 +01:00
parse("<stdin>", preprocessedCode.c_str(), preprocessedCode.size());
return EXIT_SUCCESS;
}
int runWithNewPreprocessor(int argc, char *argv[])
{
if (argc == 1) {
fprintf(stderr, "c++: No such file or directory\n");
return EXIT_FAILURE;
}
char block[BLOCK_SIZE];
std::string source;
if (FILE *fp = fopen(argv[1], "r")) {
while (size_t sz = fread(block, 1, BLOCK_SIZE, fp))
source.append(block, sz);
fclose(fp);
} else {
fprintf(stderr, "c++: No such file or directory\n");
return EXIT_FAILURE;
}
std::ostringstream out;
Preprocessor pp(out);
pp(source.c_str(), source.size(), StringRef(argv[1]));
const std::string preprocessedCode = out.str();
parse(argv[1], preprocessedCode.c_str(), preprocessedCode.size());
return EXIT_SUCCESS;
}
void parse(const char *fileName, const char *source, unsigned size)
{
2009-10-23 12:58:39 +02:00
Control control;
2010-08-11 16:12:07 +02:00
TranslationUnit unit(&control, control.stringLiteral(fileName));
2009-12-15 16:49:59 +01:00
unit.setSource(source, size);
2009-10-23 12:58:39 +02:00
unit.parse();
#if 1
2010-08-13 14:00:27 +02:00
Namespace *globalNamespace = control.newNamespace(0);
Bind bind(&unit);
bind(unit.ast()->asTranslationUnit(), globalNamespace);
V v(&unit);
v.accept(unit.ast());
#endif
2009-10-23 12:58:39 +02:00
}