diff options
| author | Leandro Melo <[email protected]> | 2012-06-20 15:22:02 +0200 |
|---|---|---|
| committer | Leandro Melo <[email protected]> | 2012-06-25 15:49:27 +0200 |
| commit | d6ccffc06c0439f1d8248c374978f382b6bf5fe1 (patch) | |
| tree | 3fa332e4512c11942a99e1b3d69891a9884886cd /src/libs/cplusplus/pp-engine.h | |
| parent | e99c139352fe1f31fb2469c6a52ab41be610ef70 (diff) | |
C++: Core changes in preprocessing
Summary of most relevant items:
- Preprocessor output format change. No more gen true/false. Instead
a more intuitive and natural expansion (like from a real compiler) is
performed directly corresponding to the macro invocation. Notice that
information about the generated tokens is not lost, because it's now
embedded in the expansion section header (in terms of lines and columns
as explained in the code). In addition the location on where the macro
expansion happens is also documented for future use.
- Fix line control directives and associated token line numbers.
This was not detected in tests cases because some of them were
actually wrong: Within expansions the line information was being
considered as originally computed in the macro definition, while
the desired and expected for Creator's reporting mechanism (just
like regular compilers) is the line from the expanded version
of the tokens.
- Do not allow for eager expansion. This was previously being done
inside define directives. However, it's not allowed and might
lead to incorrect results, since the argument substitution should
only happen upon the macro invocation (and following nested ones).
At least GCC and clang are consistent with that. See test case
tst_Preprocessor:dont_eagerly_expand for a detailed explanation.
- Revive the 'expanded' token flag. This is used to mark every token
that originates from a macro expansion. Notice, however, that
expanded tokens are not necessarily generated tokens (although
every generated token is a expanded token). Expanded tokens that
are not generated are those which are still considered by our
code model features, since they are visible on the editor. The
translation unit is smart enough to calculate line/column position
for such tokens based on the information from the expansion section
header.
- How expansions are tracked has also changed. Now, we simply add
two surrounding marker tokens to each "top-level" expansion
sequence. There is an enumeration that control expansion states.
Also, no "previous" token is kept around.
- Preprocessor client methods suffered a change in signature so
they now receive the line number of the action in question as
a paramater. Previously such line could be retrieved by the client
implementation by accessing the environment line. However, this
is not reliable because we try to avoid synchronization of the
output/environment lines in order to avoid unnecessary output,
while expanding macros or handling preprocessor directives.
- Although macros are not expanded during define directives (as
mentioned above) the preprocessor client is now "notified"
when it sees a macro. This is to allow usage tracking.
- Other small stuff.
This is all in one patch because the fixes are a consequence
of the change in preprocessing control.
Change-Id: I8f4c6e6366f37756ec65d0a93b79f72a3ac4ed50
Reviewed-by: Roberto Raggi <[email protected]>
Diffstat (limited to 'src/libs/cplusplus/pp-engine.h')
| -rw-r--r-- | src/libs/cplusplus/pp-engine.h | 65 |
1 files changed, 38 insertions, 27 deletions
diff --git a/src/libs/cplusplus/pp-engine.h b/src/libs/cplusplus/pp-engine.h index c310a9e3e3a..478812918d3 100644 --- a/src/libs/cplusplus/pp-engine.h +++ b/src/libs/cplusplus/pp-engine.h @@ -60,6 +60,7 @@ #include <QVector> #include <QBitArray> #include <QByteArray> +#include <QPair> namespace CPlusPlus { @@ -92,10 +93,17 @@ private: void preprocess(const QString &filename, const QByteArray &source, QByteArray *result, bool noLines, bool markGeneratedTokens, bool inCondition, - unsigned offsetRef = 0, unsigned envLineRef = 1); + unsigned offsetRef = 0, unsigned lineRef = 1); enum { MAX_LEVEL = 512 }; + enum ExpansionStatus { + NotExpanding, + ReadyForExpansion, + Expanding, + JustFinishedExpansion + }; + struct State { State(); @@ -114,14 +122,17 @@ private: bool m_inPreprocessorDirective; QByteArray *m_result; - bool m_markGeneratedTokens; + bool m_markExpandedTokens; bool m_noLines; bool m_inCondition; - bool m_inDefine; unsigned m_offsetRef; - unsigned m_envLineRef; + unsigned m_lineRef; + + ExpansionStatus m_expansionStatus; + QByteArray m_expansionResult; + QVector<QPair<unsigned, unsigned> > m_expandedTokensInfo; }; void handleDefined(PPToken *tk); @@ -129,9 +140,11 @@ private: void lex(PPToken *tk); void skipPreprocesorDirective(PPToken *tk); bool handleIdentifier(PPToken *tk); - bool handleFunctionLikeMacro(PPToken *tk, const Macro *macro, QVector<PPToken> &body, - bool addWhitespaceMarker, - const QVector<QVector<PPToken> > &actuals); + bool handleFunctionLikeMacro(PPToken *tk, + const Macro *macro, + QVector<PPToken> &body, + const QVector<QVector<PPToken> > &actuals, + unsigned lineRef); bool skipping() const { return m_state.m_skipping[m_state.m_ifLevel]; } @@ -155,30 +168,28 @@ private: static bool isQtReservedWord(const ByteArrayRef &name); - inline bool atStartOfOutputLine() const - { return (m_state.m_result && !m_state.m_result->isEmpty()) ? m_state.m_result->end()[-1] == '\n' : true; } - - inline void startNewOutputLine() const - { - if (m_state.m_result && !m_state.m_result->isEmpty() && m_state.m_result->end()[-1] != '\n') - out('\n'); - } - - void genLine(unsigned lineno, const QByteArray &fileName) const; - - inline void out(const QByteArray &text) const - { if (m_state.m_result) m_state.m_result->append(text); } + void trackExpansionCycles(PPToken *tk); - inline void out(char ch) const - { if (m_state.m_result) m_state.m_result->append(ch); } + template <class T> + void writeOutput(const T &t); + void writeOutput(const ByteArrayRef &ref); + bool atStartOfOutputLine() const; + void maybeStartOutputLine(); + void generateOutputLineMarker(unsigned lineno); + void synchronizeOutputLines(const PPToken &tk, bool forceLine = false); + void removeTrailingOutputLines(); - inline void out(const char *s) const - { if (m_state.m_result) m_state.m_result->append(s); } + const QByteArray *currentOutputBuffer() const; + QByteArray *currentOutputBuffer(); - inline void out(const ByteArrayRef &ref) const - { if (m_state.m_result) m_state.m_result->append(ref.start(), ref.length()); } + void enforceSpacing(const PPToken &tk, bool forceSpacing = false); + static std::size_t computeDistance(const PPToken &tk, bool forceTillLine = false); - PPToken generateToken(enum Kind kind, const char *content, int len, unsigned lineno, bool addQuotes); + PPToken generateToken(enum Kind kind, + const char *content, int length, + unsigned lineno, + bool addQuotes, + bool addToControl = true); PPToken generateConcatenated(const PPToken &leftTk, const PPToken &rightTk); void startSkippingBlocks(const PPToken &tk) const; |
