diff options
author | Christian Kamm <christian.d.kamm@nokia.com> | 2011-08-30 14:29:40 +0200 |
---|---|---|
committer | Christian Kamm <christian.d.kamm@nokia.com> | 2011-08-31 07:41:32 +0200 |
commit | 9f1c294dd4e902b365dd29b004a5711a4475e900 (patch) | |
tree | f447cd197afa27f568839520a5c82e84aa2d52a7 /src/libs/cplusplus | |
parent | 6e877ec8ab08b5ed1c4d6891364d690dbd2b9bf4 (diff) | |
download | qt-creator-9f1c294dd4e902b365dd29b004a5711a4475e900.tar.gz |
C++ preprocessor: Fix multiline tokens in ifdef'ed out blocks.
Change-Id: If6f9819565f891e861f9e111423d99caa7c0f7aa
Reviewed-on: http://codereview.qt.nokia.com/3884
Reviewed-by: Roberto Raggi <roberto.raggi@nokia.com>
Reviewed-by: Leandro T. C. Melo <leandro.melo@nokia.com>
Diffstat (limited to 'src/libs/cplusplus')
-rw-r--r-- | src/libs/cplusplus/pp-engine.cpp | 52 | ||||
-rw-r--r-- | src/libs/cplusplus/pp-engine.h | 3 |
2 files changed, 43 insertions, 12 deletions
diff --git a/src/libs/cplusplus/pp-engine.cpp b/src/libs/cplusplus/pp-engine.cpp index 7d14260b6a..897cb2b1c9 100644 --- a/src/libs/cplusplus/pp-engine.cpp +++ b/src/libs/cplusplus/pp-engine.cpp @@ -591,11 +591,9 @@ void Preprocessor::processNewline(bool force) if (_dot != _tokens.constBegin()) { TokenIterator prevTok = _dot - 1; - // line changes due to multi-line tokens - if (prevTok->isLiteral() - || (_keepComments - && (prevTok->kind() == T_COMMENT - || prevTok->kind() == T_DOXY_COMMENT))) { + // Line changes due to multi-line tokens that we assume got printed + // to the preprocessed source. See interaction with skipToNextLine. + if (maybeMultilineToken(prevTok)) { const char *ptr = _source.constBegin() + prevTok->begin(); const char *end = ptr + prevTok->length(); @@ -711,6 +709,41 @@ bool Preprocessor::maybeAfterComment() const return false; } +bool Preprocessor::maybeMultilineToken(Preprocessor::TokenIterator tok) +{ + return tok->isLiteral() + || (_keepComments + && (tok->kind() == T_COMMENT + || tok->kind() == T_DOXY_COMMENT)); +} + +void Preprocessor::skipToNextLine() +{ + do { + if (maybeMultilineToken(_dot)) { + const char *ptr = _source.constBegin() + _dot->begin(); + const char *end = ptr + _dot->length(); + + int newlines = 0; + for (; ptr != end; ++ptr) { + if (*ptr == '\n') + ++newlines; + } + if (newlines) { + // This function does not output tokens it skips. We need to offset + // the currentLine so it gets correctly adjusted by processNewline. + env->currentLine -= newlines; + ++_dot; + return; + } + } + + ++_dot; + + } while (_dot->isNot(T_EOF_SYMBOL) + && (_dot->f.joined || !_dot->f.newline)); +} + void Preprocessor::preprocess(const QString &fileName, const QByteArray &source, QByteArray *result) { @@ -741,9 +774,7 @@ void Preprocessor::preprocess(const QString &fileName, const QByteArray &source, // handle the preprocessor directive TokenIterator start = _dot; - do { - ++_dot; - } while (_dot->isNot(T_EOF_SYMBOL) && (_dot->f.joined || ! _dot->f.newline)); + skipToNextLine(); const bool skippingBlocks = _skipping[iflevel]; @@ -752,10 +783,7 @@ void Preprocessor::preprocess(const QString &fileName, const QByteArray &source, } else if (skipping()) { // skip the current line - - do { - ++_dot; - } while (_dot->isNot(T_EOF_SYMBOL) && (_dot->f.joined || ! _dot->f.newline)); + skipToNextLine(); } else { diff --git a/src/libs/cplusplus/pp-engine.h b/src/libs/cplusplus/pp-engine.h index 0b7db439a2..9bac45a492 100644 --- a/src/libs/cplusplus/pp-engine.h +++ b/src/libs/cplusplus/pp-engine.h @@ -180,6 +180,9 @@ private: QString string(const char *first, int len) const; bool maybeAfterComment() const; + bool maybeMultilineToken(TokenIterator tok); + void skipToNextLine(); + private: Client *client; Environment *env; |