Commit 1fe1c6d0 authored by Nikolai Kosjar's avatar Nikolai Kosjar

C++: Tests: Remove plain-cplusplus

Change-Id: Ifdf4cd5ea478c87d585c9404caf8f4be6873f692
Reviewed-by: default avatarChristian Stenger <christian.stenger@digia.com>
Reviewed-by: Orgad Shaneh's avatarOrgad Shaneh <orgads@gmail.com>
parent fb09f5cc
......@@ -136,7 +136,6 @@ tmp/
/tests/manual/qml-ast2dot/qml-ast2dot
/tests/manual/debugger/simple/libsimple_test_plugin.*dylib
/tests/manual/debugger/simple/simple_test_app
/tests/manual/plain-cplusplus/plain-c++
/tests/manual/preprocessor/pp
/tests/tools/cplusplus-ast2png/cplusplus-ast2png
/tests/auto/aggregation/tst_aggregation
......
......@@ -10,7 +10,6 @@ utils
unix {
# Uses popen
SUBDIRS += \
plain-cplusplus \
# Profile library paths issues
process \
ssh
......
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "Preprocessor.h"
#include "Lexer.h"
#include <list>
#include <iostream>
#include <cassert>
using namespace CPlusPlus;
std::ostream &operator << (std::ostream &out, const StringRef &s)
{
out.write(s.text(), s.size());
return out;
}
struct Preprocessor::TokenBuffer
{
std::list<Token> tokens;
const Macro *macro;
TokenBuffer *next;
template <typename _Iterator>
TokenBuffer(_Iterator firstToken, _Iterator lastToken, const Macro *macro, TokenBuffer *next)
: tokens(firstToken, lastToken), macro(macro), next(next) {}
};
Lexer *Preprocessor::switchLexer(Lexer *lex)
{
Lexer *previousLexer = _lexer;
_lexer = lex;
return previousLexer;
}
StringRef Preprocessor::switchSource(const StringRef &source)
{
StringRef previousSource = _source;
_source = source;
return previousSource;
}
const Preprocessor::Macro *Preprocessor::resolveMacro(const StringRef &name) const
{
std::map<StringRef, Macro>::const_iterator it = macros.find(name);
if (it != macros.end()) {
const Macro *m = &it->second;
for (TokenBuffer *r = _tokenBuffer; r; r = r->next) {
if (r->macro == m)
return 0;
}
return m;
}
return 0;
}
void Preprocessor::collectActualArguments(Token *tk, std::vector<std::vector<Token> > *actuals)
{
lex(tk);
assert(tk->is(T_LPAREN));
lex(tk);
std::vector<Token> tokens;
scanActualArgument(tk, &tokens);
actuals->push_back(tokens);
while (tk->is(T_COMMA)) {
lex(tk);
std::vector<Token> tokens;
scanActualArgument(tk, &tokens);
actuals->push_back(tokens);
}
assert(tk->is(T_RPAREN));
lex(tk);
}
void Preprocessor::scanActualArgument(Token *tk, std::vector<Token> *tokens)
{
int count = 0;
while (tk->isNot(T_EOF_SYMBOL)) {
if (tk->is(T_LPAREN)) {
++count;
} else if (tk->is(T_RPAREN)) {
if (! count)
break;
--count;
} else if (! count && tk->is(T_COMMA)) {
break;
}
tokens->push_back(*tk);
lex(tk);
}
}
void Preprocessor::lex(Token *tk)
{
_Lagain:
if (_tokenBuffer) {
if (_tokenBuffer->tokens.empty()) {
TokenBuffer *r = _tokenBuffer;
_tokenBuffer = _tokenBuffer->next;
delete r;
goto _Lagain;
}
*tk = _tokenBuffer->tokens.front();
_tokenBuffer->tokens.pop_front();
} else {
_lexer->scan(tk);
}
_Lclassify:
if (! inPreprocessorDirective) {
if (tk->newline() && tk->is(T_POUND)) {
handlePreprocessorDirective(tk);
goto _Lclassify;
} else if (tk->is(T_IDENTIFIER)) {
const StringRef id = asStringRef(*tk);
if (const Macro *macro = resolveMacro(id)) {
std::vector<Token> body = macro->body;
if (macro->isFunctionLike) {
std::vector<std::vector<Token> > actuals;
collectActualArguments(tk, &actuals);
std::vector<Token> expanded;
for (size_t i = 0; i < body.size(); ++i) {
const Token &token = body[i];
if (token.isNot(T_IDENTIFIER)) {
expanded.push_back(token);
} else {
const StringRef id = asStringRef(token);
size_t j = 0;
for (; j < macro->formals.size(); ++j) {
if (macro->formals[j] == id) {
expanded.insert(expanded.end(), actuals[j].begin(), actuals[j].end());
break;
}
}
if (j == macro->formals.size())
expanded.push_back(token);
}
}
const Token currentTokenBuffer[] = { *tk };
_tokenBuffer = new TokenBuffer(currentTokenBuffer, currentTokenBuffer + 1,
/*macro */ 0, _tokenBuffer);
body = expanded;
}
_tokenBuffer = new TokenBuffer(body.begin(), body.end(),
macro, _tokenBuffer);
goto _Lagain;
}
}
}
}
void Preprocessor::handlePreprocessorDirective(Token *tk)
{
inPreprocessorDirective = true;
lex(tk); // scan the directive
if (tk->newline() && ! tk->joined())
return; // nothing to do.
const StringRef ppDefine("define", 6);
if (tk->is(T_IDENTIFIER)) {
const StringRef directive = asStringRef(*tk);
if (directive == ppDefine)
handleDefineDirective(tk);
else
skipPreprocesorDirective(tk);
}
inPreprocessorDirective = false;
}
bool Preprocessor::isValidToken(const Token &tk) const
{
if (tk.isNot(T_EOF_SYMBOL) && (! tk.newline() || tk.joined()))
return true;
return false;
}
void Preprocessor::handleDefineDirective(Token *tk)
{
lex(tk);
if (tk->is(T_IDENTIFIER)) {
const StringRef macroName = asStringRef(*tk);
Macro macro;
lex(tk);
if (isValidToken(*tk) && tk->is(T_LPAREN) && ! tk->whitespace()) {
macro.isFunctionLike = true;
lex(tk); // skip `('
if (isValidToken(*tk) && tk->is(T_IDENTIFIER)) {
macro.formals.push_back(asStringRef(*tk));
lex(tk);
while (isValidToken(*tk) && tk->is(T_COMMA)) {
lex(tk);
if (isValidToken(*tk) && tk->is(T_IDENTIFIER)) {
macro.formals.push_back(asStringRef(*tk));
lex(tk);
}
}
}
if (isValidToken(*tk) && tk->is(T_RPAREN))
lex(tk); // skip `)'
}
while (isValidToken(*tk)) {
macro.body.push_back(*tk);
lex(tk);
}
macros.insert(std::make_pair(macroName, macro));
} else {
skipPreprocesorDirective(tk);
}
}
void Preprocessor::skipPreprocesorDirective(Token *tk)
{
do {
lex(tk);
} while (isValidToken(*tk));
}
StringRef Preprocessor::asStringRef(const Token &tk) const
{ return StringRef(_source.begin() + tk.bytesBegin(), tk.bytes()); }
Preprocessor::Preprocessor(std::ostream &out)
: out(out), _lexer(0), inPreprocessorDirective(false)
{ }
void Preprocessor::operator()(const char *source, unsigned size, const StringRef &currentFileName)
{
_currentFileName = currentFileName;
run(source, size);
}
void Preprocessor::run(const char *source, unsigned size)
{
_tokenBuffer = 0;
const StringRef previousSource = switchSource(StringRef(source, size));
Lexer thisLexer(source, source + size);
thisLexer.setScanKeywords(false);
Lexer *previousLexer = switchLexer(&thisLexer);
inPreprocessorDirective = false;
Token tk;
unsigned lineno = 0;
do {
lex(&tk);
if (lineno != tk.lineno) {
if (lineno > tk.lineno || tk.lineno - lineno > 3) {
out << std::endl << "#line " << tk.lineno << " \"" << _currentFileName << "\"" << std::endl;
} else {
for (unsigned i = lineno; i < tk.lineno; ++i)
out << std::endl;
}
lineno = tk.lineno;
} else {
if (tk.newline())
out << std::endl;
if (tk.whitespace())
out << ' ';
}
out << asStringRef(tk);
lineno = tk.lineno;
} while (tk.isNot(T_EOF_SYMBOL));
out << std::endl;
switchLexer(previousLexer);
switchSource(previousSource);
}
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef CPLUSPLUS_PREPROCESSOR_H
#define CPLUSPLUS_PREPROCESSOR_H
#include <CPlusPlusForwardDeclarations.h>
#include <iosfwd>
#include <vector>
#include <map>
#include <cstring>
namespace CPlusPlus {
class Lexer;
class Token;
class StringRef
{
const char *_text;
unsigned _size;
public:
typedef const char *iterator;
typedef const char *const_iterator;
StringRef()
: _text(0), _size(0) {}
StringRef(const char *text, unsigned size)
: _text(text), _size(size) {}
StringRef(const char *text)
: _text(text), _size(std::strlen(text)) {}
inline const char *text() const { return _text; }
inline unsigned size() const { return _size; }
inline const_iterator begin() const { return _text; }
inline const_iterator end() const { return _text + _size; }
bool operator == (const StringRef &other) const
{
if (_size == other._size)
return _text == other._text || ! std::strncmp(_text, other._text, _size);
return false;
}
bool operator != (const StringRef &other) const
{ return ! operator == (other); }
bool operator < (const StringRef &other) const
{ return std::lexicographical_compare(begin(), end(), other.begin(), other.end()); }
};
class CPLUSPLUS_EXPORT Preprocessor
{
public:
Preprocessor(std::ostream &out);
void operator()(const char *source, unsigned size, const StringRef &currentFileName);
private:
struct Macro
{
Macro(): isFunctionLike(false), isVariadic(false) {}
std::vector<StringRef> formals;
std::vector<Token> body;
bool isFunctionLike: 1;
bool isVariadic: 1;
};
void run(const char *source, unsigned size);
Lexer *switchLexer(Lexer *lex);
StringRef switchSource(const StringRef &source);
const Macro *resolveMacro(const StringRef &name) const;
StringRef asStringRef(const Token &tk) const;
void lex(Token *tk);
bool isValidToken(const Token &tk) const;
void handlePreprocessorDirective(Token *tk);
void handleDefineDirective(Token *tk);
void skipPreprocesorDirective(Token *tk);
void collectActualArguments(Token *tk, std::vector<std::vector<Token> > *actuals);
void scanActualArgument(Token *tk, std::vector<Token> *tokens);
private:
struct TokenBuffer;
std::ostream &out;
StringRef _currentFileName;
Lexer *_lexer;
StringRef _source;
TokenBuffer *_tokenBuffer;
bool inPreprocessorDirective: 1;
std::map<StringRef, Macro> macros;
};
} // end of namespace CPlusPlus
CPLUSPLUS_EXPORT std::ostream &operator << (std::ostream &out, const CPlusPlus::StringRef &s);
#endif // CPLUSPLUS_PREPROCESSOR_H
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "Preprocessor.h"
#include <CPlusPlus.h>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace CPlusPlus;
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;
}
};
int main(int argc, char *argv[])
{
if (getenv("CPLUSPLUS_WITH_NEW_PREPROCESSOR"))
return runWithNewPreprocessor(argc, argv);
return runWithSystemPreprocessor(argc, argv);
}
int runWithSystemPreprocessor(int argc, char *argv[])
{
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";
for (int i = 1; i < argc; ++i) {
cmdline += ' ';
cmdline += argv[i];
}
char block[BLOCK_SIZE];
std::string preprocessedCode;
if (FILE *fp = popen(cmdline.c_str(), "r")) {
while (size_t sz = fread(block, 1, BLOCK_SIZE, fp))
preprocessedCode.append(block, sz);
pclose(fp);
} else {
fprintf(stderr, "c++: No such file or directory\n");
return EXIT_FAILURE;
}
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();