Commit 753ddb7c authored by Roberto Raggi's avatar Roberto Raggi

Some cleanup in the C++ preprocessor.

Introduced pp-scanner.cpp, renamed `pp' to `Preprocessor' and removed useless #includes.
parent d8336257
......@@ -136,7 +136,7 @@ QString TypeOfExpression::preprocessedExpression(const QString &expression,
processEnvironment(documents, thisDocument,
&env, &processed);
const QByteArray code = expression.toUtf8();
pp preproc(0, env);
Preprocessor preproc(0, env);
QByteArray preprocessedCode;
preproc("<expression>", code, &preprocessedCode);
return QString::fromUtf8(preprocessedCode);
......
......@@ -22,13 +22,13 @@ HEADERS += \
TypePrettyPrinter.h \
ResolveExpression.h \
LookupContext.h \
pp.h \
pp-cctype.h \
pp-engine.h \
pp-macro-expander.h \
pp-scanner.h \
pp-client.h \
pp-environment.h \
pp-internal.h \
pp-macro.h
SOURCES += \
......@@ -46,6 +46,7 @@ SOURCES += \
LookupContext.cpp \
pp-engine.cpp \
pp-environment.cpp \
pp-macro-expander.cpp
pp-macro-expander.cpp \
pp-scanner.cpp
RESOURCES += cplusplus.qrc
......@@ -50,8 +50,8 @@
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef PP_CCTYPE_H
#define PP_CCTYPE_H
#ifndef CPLUSPLUS_PP_CCTYPE_H
#define CPLUSPLUS_PP_CCTYPE_H
#include <CPlusPlusForwardDeclarations.h>
......@@ -73,4 +73,4 @@ inline bool CPLUSPLUS_EXPORT pp_isspace (int __ch)
} // namespace CPlusPlus
#endif // PP_CCTYPE_H
#endif // CPLUSPLUS_PP_CCTYPE_H
......@@ -31,8 +31,8 @@
**
***************************************************************************/
#ifndef PP_CLIENT_H
#define PP_CLIENT_H
#ifndef CPLUSPLUS_PP_CLIENT_H
#define CPLUSPLUS_PP_CLIENT_H
#include <CPlusPlusForwardDeclarations.h>
......@@ -79,4 +79,4 @@ public:
} // namespace CPlusPlus
#endif // PP_CLIENT_H
#endif // CPLUSPLUS_PP_CLIENT_H
......@@ -451,7 +451,7 @@ private:
} // end of anonymous namespace
pp::pp (Client *client, Environment &env)
Preprocessor::Preprocessor(Client *client, Environment &env)
: client(client),
env(env),
expand(env)
......@@ -459,7 +459,7 @@ pp::pp (Client *client, Environment &env)
resetIfLevel ();
}
void pp::pushState(const State &s)
void Preprocessor::pushState(const State &s)
{
_savedStates.append(state());
_source = s.source;
......@@ -467,7 +467,7 @@ void pp::pushState(const State &s)
_dot = s.dot;
}
pp::State pp::state() const
Preprocessor::State Preprocessor::state() const
{
State state;
state.source = _source;
......@@ -476,7 +476,7 @@ pp::State pp::state() const
return state;
}
void pp::popState()
void Preprocessor::popState()
{
const State &state = _savedStates.last();
_source = state.source;
......@@ -485,7 +485,7 @@ void pp::popState()
_savedStates.removeLast();
}
void pp::operator () (const QByteArray &filename,
void Preprocessor::operator () (const QByteArray &filename,
const QByteArray &source,
QByteArray *result)
{
......@@ -497,7 +497,7 @@ void pp::operator () (const QByteArray &filename,
env.currentFile = previousFile;
}
pp::State pp::createStateFromSource(const QByteArray &source) const
Preprocessor::State Preprocessor::createStateFromSource(const QByteArray &source) const
{
State state;
state.source = source;
......@@ -512,7 +512,7 @@ pp::State pp::createStateFromSource(const QByteArray &source) const
return state;
}
void pp::operator()(const QByteArray &source, QByteArray *result)
void Preprocessor::operator()(const QByteArray &source, QByteArray *result)
{
pushState(createStateFromSource(source));
......@@ -700,27 +700,27 @@ void pp::operator()(const QByteArray &source, QByteArray *result)
env.currentLine = previousCurrentLine;
}
const char *pp::startOfToken(const Token &token) const
const char *Preprocessor::startOfToken(const Token &token) const
{ return _source.constBegin() + token.begin(); }
const char *pp::endOfToken(const Token &token) const
const char *Preprocessor::endOfToken(const Token &token) const
{ return _source.constBegin() + token.end(); }
QByteArray pp::tokenSpell(const Token &token) const
QByteArray Preprocessor::tokenSpell(const Token &token) const
{
const QByteArray text = QByteArray::fromRawData(_source.constBegin() + token.offset,
token.length);
return text;
}
QByteArray pp::tokenText(const Token &token) const
QByteArray Preprocessor::tokenText(const Token &token) const
{
const QByteArray text(_source.constBegin() + token.offset,
token.length);
return text;
}
void pp::processDirective(TokenIterator firstToken, TokenIterator lastToken)
void Preprocessor::processDirective(TokenIterator firstToken, TokenIterator lastToken)
{
RangeLexer tk(firstToken, lastToken);
++tk; // skip T_POUND
......@@ -771,7 +771,7 @@ void pp::processDirective(TokenIterator firstToken, TokenIterator lastToken)
}
}
QVector<Token> pp::tokenize(const QByteArray &text) const
QVector<Token> Preprocessor::tokenize(const QByteArray &text) const
{
QVector<Token> tokens;
Lexer lex(text.constBegin(), text.constEnd());
......@@ -784,7 +784,7 @@ QVector<Token> pp::tokenize(const QByteArray &text) const
return tokens;
}
void pp::processInclude(bool skipCurentPath,
void Preprocessor::processInclude(bool skipCurentPath,
TokenIterator firstToken, TokenIterator lastToken,
bool acceptMacros)
{
......@@ -836,7 +836,7 @@ void pp::processInclude(bool skipCurentPath,
}
}
void pp::processDefine(TokenIterator firstToken, TokenIterator lastToken)
void Preprocessor::processDefine(TokenIterator firstToken, TokenIterator lastToken)
{
RangeLexer tk(firstToken, lastToken);
......@@ -921,7 +921,7 @@ void pp::processDefine(TokenIterator firstToken, TokenIterator lastToken)
client->macroAdded(macro);
}
void pp::processIf(TokenIterator firstToken, TokenIterator lastToken)
void Preprocessor::processIf(TokenIterator firstToken, TokenIterator lastToken)
{
RangeLexer tk(firstToken, lastToken);
......@@ -948,7 +948,7 @@ void pp::processIf(TokenIterator firstToken, TokenIterator lastToken)
}
}
void pp::processElse(TokenIterator firstToken, TokenIterator lastToken)
void Preprocessor::processElse(TokenIterator firstToken, TokenIterator lastToken)
{
RangeLexer tk(firstToken, lastToken);
......@@ -961,7 +961,7 @@ void pp::processElse(TokenIterator firstToken, TokenIterator lastToken)
}
}
void pp::processElif(TokenIterator firstToken, TokenIterator lastToken)
void Preprocessor::processElif(TokenIterator firstToken, TokenIterator lastToken)
{
RangeLexer tk(firstToken, lastToken);
++tk; // skip T_POUND
......@@ -980,7 +980,7 @@ void pp::processElif(TokenIterator firstToken, TokenIterator lastToken)
}
}
void pp::processEndif(TokenIterator, TokenIterator)
void Preprocessor::processEndif(TokenIterator, TokenIterator)
{
if (iflevel == 0 && !skipping()) {
// std::cerr << "*** WARNING #endif without #if" << std::endl;
......@@ -992,7 +992,7 @@ void pp::processEndif(TokenIterator, TokenIterator)
}
}
void pp::processIfdef(bool checkUndefined,
void Preprocessor::processIfdef(bool checkUndefined,
TokenIterator firstToken, TokenIterator lastToken)
{
RangeLexer tk(firstToken, lastToken);
......@@ -1013,7 +1013,7 @@ void pp::processIfdef(bool checkUndefined,
}
}
void pp::processUndef(TokenIterator firstToken, TokenIterator lastToken)
void Preprocessor::processUndef(TokenIterator firstToken, TokenIterator lastToken)
{
RangeLexer tk(firstToken, lastToken);
......@@ -1029,14 +1029,14 @@ void pp::processUndef(TokenIterator firstToken, TokenIterator lastToken)
}
}
void pp::resetIfLevel ()
void Preprocessor::resetIfLevel ()
{
iflevel = 0;
_skipping[iflevel] = false;
_true_test[iflevel] = false;
}
pp::PP_DIRECTIVE_TYPE pp::classifyDirective (const QByteArray &__directive) const
Preprocessor::PP_DIRECTIVE_TYPE Preprocessor::classifyDirective (const QByteArray &__directive) const
{
switch (__directive.size())
{
......@@ -1085,7 +1085,7 @@ pp::PP_DIRECTIVE_TYPE pp::classifyDirective (const QByteArray &__directive) cons
return PP_UNKNOWN_DIRECTIVE;
}
bool pp::testIfLevel()
bool Preprocessor::testIfLevel()
{
const bool result = !_skipping[iflevel++];
_skipping[iflevel] = _skipping[iflevel - 1];
......@@ -1093,10 +1093,10 @@ bool pp::testIfLevel()
return result;
}
int pp::skipping() const
int Preprocessor::skipping() const
{ return _skipping[iflevel]; }
Value pp::evalExpression(TokenIterator firstToken, TokenIterator lastToken,
Value Preprocessor::evalExpression(TokenIterator firstToken, TokenIterator lastToken,
const QByteArray &source) const
{
ExpressionEvaluator eval(&env);
......@@ -1104,7 +1104,7 @@ Value pp::evalExpression(TokenIterator firstToken, TokenIterator lastToken,
return result;
}
bool pp::isQtReservedWord (const QByteArray &macroId) const
bool Preprocessor::isQtReservedWord (const QByteArray &macroId) const
{
const int size = macroId.size();
if (size == 9 && macroId.at(0) == 'Q' && macroId == "Q_SIGNALS")
......
......@@ -50,8 +50,8 @@
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef PP_ENGINE_H
#define PP_ENGINE_H
#ifndef CPLUSPLUS_PP_ENGINE_H
#define CPLUSPLUS_PP_ENGINE_H
#include "pp-client.h"
......@@ -134,7 +134,7 @@ namespace CPlusPlus {
#undef PP_DEFINE_BIN_OP
};
class CPLUSPLUS_EXPORT pp
class CPLUSPLUS_EXPORT Preprocessor
{
Client *client;
Environment &env;
......@@ -182,7 +182,7 @@ namespace CPlusPlus {
State createStateFromSource(const QByteArray &source) const;
public:
pp(Client *client, Environment &env);
Preprocessor(Client *client, Environment &env);
void operator()(const QByteArray &filename,
const QByteArray &source,
......@@ -228,4 +228,4 @@ namespace CPlusPlus {
} // namespace CPlusPlus
#endif // PP_ENGINE_H
#endif // CPLUSPLUS_PP_ENGINE_H
......@@ -50,8 +50,8 @@
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef PP_ENVIRONMENT_H
#define PP_ENVIRONMENT_H
#ifndef CPLUSPLUS_PP_ENVIRONMENT_H
#define CPLUSPLUS_PP_ENVIRONMENT_H
#include "CPlusPlusForwardDeclarations.h"
......@@ -108,4 +108,4 @@ private:
} // namespace CPlusPlus
#endif // PP_ENVIRONMENT_H
#endif // CPLUSPLUS_PP_ENVIRONMENT_H
/***************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Qt Software Information (qt-info@nokia.com)
**
**
** Non-Open Source Usage
**
** Licensees may use this file in accordance with the Qt Beta Version
** License Agreement, Agreement version 2.2 provided with the Software or,
** alternatively, in accordance with the terms contained in a written
** agreement between you and Nokia.
**
** GNU General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the packaging
** of this file. Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
**
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt GPL Exception
** version 1.3, included in the file GPL_EXCEPTION.txt in this package.
**
***************************************************************************/
/*
Copyright 2005 Roberto Raggi <roberto@kdevelop.org>
Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
KDEVELOP TEAM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef PP_INTERNAL_H
#define PP_INTERNAL_H
#include <QByteArray>
namespace CPlusPlus {
namespace _PP_internal {
inline bool comment_p (const char *__first, const char *__last)
{
if (__first == __last)
return false;
if (*__first != '/')
return false;
if (++__first == __last)
return false;
return (*__first == '/' || *__first == '*');
}
} // _PP_internal
} // namespace CPlusPlus
#endif // PP_INTERNAL_H
......@@ -32,11 +32,26 @@
***************************************************************************/
#include "pp.h"
#include "pp-cctype.h"
#include "pp-macro-expander.h"
#include <QDateTime>
using namespace CPlusPlus;
inline static bool comment_p (const char *__first, const char *__last)
{
if (__first == __last)
return false;
if (*__first != '/')
return false;
if (++__first == __last)
return false;
return (*__first == '/' || *__first == '*');
}
MacroExpander::MacroExpander (Environment &env, pp_frame *frame)
: env(env), frame(frame),
lines(0), generated_lines(0)
......@@ -137,7 +152,7 @@ const char *MacroExpander::operator () (const char *__first, const char *__last,
__result->append(__first, next_pos - __first);
__first = next_pos;
}
else if (_PP_internal::comment_p (__first, __last))
else if (comment_p (__first, __last))
{
__first = skip_comment_or_divop (__first, __last);
int n = skip_comment_or_divop.lines;
......
/***************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Qt Software Information (qt-info@nokia.com)
**
**
** Non-Open Source Usage
**
** Licensees may use this file in accordance with the Qt Beta Version
** License Agreement, Agreement version 2.2 provided with the Software or,
** alternatively, in accordance with the terms contained in a written
** agreement between you and Nokia.
**
** GNU General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the packaging
** of this file. Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
**
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt GPL Exception
** version 1.3, included in the file GPL_EXCEPTION.txt in this package.
**
***************************************************************************/
/*
Copyright 2005 Roberto Raggi <roberto@kdevelop.org>
Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
KDEVELOP TEAM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "pp-scanner.h"
#include "pp-cctype.h"
using namespace CPlusPlus;
const char *pp_skip_blanks::operator () (const char *__first, const char *__last)
{
lines = 0;
for (; __first != __last; lines += (*__first != '\n' ? 0 : 1), ++__first) {
if (*__first == '\\') {
const char *__begin = __first;
++__begin;
if (__begin != __last && *__begin == '\n')
++__first;
else
break;
} else if (*__first == '\n' || !pp_isspace (*__first))
break;
}
return __first;
}
const char *pp_skip_whitespaces::operator () (const char *__first, const char *__last)
{
lines = 0;
for (; __first != __last; lines += (*__first != '\n' ? 0 : 1), ++__first) {
if (! pp_isspace (*__first))
break;
}
return __first;
}
const char *pp_skip_comment_or_divop::operator () (const char *__first, const char *__last)
{
enum {
MAYBE_BEGIN,
BEGIN,
MAYBE_END,
END,
IN_COMMENT,
IN_CXX_COMMENT
} state (MAYBE_BEGIN);
lines = 0;
for (; __first != __last; lines += (*__first != '\n' ? 0 : 1), ++__first) {
switch (state) {
default:
break;
case MAYBE_BEGIN:
if (*__first != '/')
return __first;
state = BEGIN;
break;
case BEGIN:
if (*__first == '*')
state = IN_COMMENT;
else if (*__first == '/')
state = IN_CXX_COMMENT;
else
return __first;
break;
case IN_COMMENT:
if (*__first == '*')
state = MAYBE_END;
break;
case IN_CXX_COMMENT:
if (*__first == '\n')
return __first;
break;
case MAYBE_END:
if (*__first == '/')
state = END;
else if (*__first != '*')
state = IN_COMMENT;
break;
case END:
return __first;
}
}
return __first;
}
const char *pp_skip_identifier::operator () (const char *__first, const char *__last)
{
lines = 0;
for (; __first != __last; lines += (*__first != '\n' ? 0 : 1), ++__first) {
if (! pp_isalnum (*__first) && *__first != '_')
break;
}
return __first;
}
const char *pp_skip_number::operator () (const char *__first, const char *__last)
{
lines = 0;
for (; __first != __last; lines += (*__first != '\n' ? 0 : 1), ++__first) {
if (! pp_isalnum (*__first) && *__first != '.')
break;
}
return __first;
}
const char *pp_skip_string_literal::operator () (const char *__first, const char *