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 <roberto.raggi@nokia.com>
This commit is contained in:
Leandro Melo
2012-06-20 15:22:02 +02:00
parent e99c139352
commit d6ccffc06c
32 changed files with 1140 additions and 442 deletions

View File

@@ -60,7 +60,7 @@ QByteArray FastPreprocessor::run(QString fileName, const QString &source)
return preprocessed;
}
void FastPreprocessor::sourceNeeded(QString &fileName, IncludeType, unsigned)
void FastPreprocessor::sourceNeeded(unsigned, QString &fileName, IncludeType)
{ mergeEnvironment(fileName); }
void FastPreprocessor::mergeEnvironment(const QString &fileName)

View File

@@ -59,18 +59,19 @@ public:
QByteArray run(QString fileName, const QString &source);
// CPlusPlus::Client
virtual void sourceNeeded(QString &fileName, IncludeType, unsigned);
virtual void sourceNeeded(unsigned, QString &fileName, IncludeType);
virtual void macroAdded(const Macro &) {}
virtual void passedMacroDefinitionCheck(unsigned, const Macro &) {}
virtual void passedMacroDefinitionCheck(unsigned, unsigned, const Macro &) {}
virtual void failedMacroDefinitionCheck(unsigned, const ByteArrayRef &) {}
virtual void startExpandingMacro(unsigned,
const Macro &,
const ByteArrayRef &,
const QVector<MacroArgumentReference> &) {}
virtual void notifyMacroReference(unsigned, unsigned, const Macro &) {}
virtual void startExpandingMacro(unsigned,
unsigned,
const Macro &,
const QVector<MacroArgumentReference> &) {}
virtual void stopExpandingMacro(unsigned, const Macro &) {}
virtual void startSkippingBlocks(unsigned) {}

View File

@@ -23,10 +23,10 @@ int ByteArrayRef::count(char ch) const
return num;
}
void Internal::PPToken::squeeze()
void Internal::PPToken::squeezeSource()
{
if (isValid()) {
m_src = m_src.mid(offset, length());
if (hasSource()) {
m_src = m_src.mid(offset, f.length);
m_src.squeeze();
offset = 0;
}

View File

@@ -96,6 +96,11 @@ public:
const QByteArray &source() const
{ return m_src; }
bool hasSource() const
{ return !m_src.isEmpty(); }
void squeezeSource();
const char *bufferStart() const
{ return m_src.constData(); }
@@ -105,11 +110,6 @@ public:
ByteArrayRef asByteArrayRef() const
{ return ByteArrayRef(&m_src, offset, length()); }
bool isValid() const
{ return !m_src.isEmpty(); }
void squeeze();
private:
QByteArray m_src;
};

View File

@@ -80,24 +80,23 @@ public:
virtual void macroAdded(const Macro &macro) = 0;
virtual void passedMacroDefinitionCheck(unsigned offset, const Macro &macro) = 0;
virtual void passedMacroDefinitionCheck(unsigned offset, unsigned line, const Macro &macro) = 0;
virtual void failedMacroDefinitionCheck(unsigned offset, const ByteArrayRef &name) = 0;
virtual void notifyMacroReference(unsigned offset, unsigned line, const Macro &macro) = 0;
virtual void startExpandingMacro(unsigned offset,
unsigned line,
const Macro &macro,
const ByteArrayRef &originalText,
const QVector<MacroArgumentReference> &actuals
= QVector<MacroArgumentReference>()) = 0;
virtual void stopExpandingMacro(unsigned offset,
const Macro &macro) = 0;
virtual void stopExpandingMacro(unsigned offset, const Macro &macro) = 0;
/// Start skipping from the given offset.
virtual void startSkippingBlocks(unsigned offset) = 0;
virtual void stopSkippingBlocks(unsigned offset) = 0;
virtual void sourceNeeded(QString &fileName, IncludeType mode,
unsigned line) = 0; // ### FIX the signature.
virtual void sourceNeeded(unsigned line, QString &fileName, IncludeType mode) = 0;
};
} // namespace CPlusPlus

View File

@@ -95,6 +95,7 @@ private:
public:
QString currentFile;
QByteArray currentFileUtf8;
unsigned currentLine;
bool hideNext;

File diff suppressed because it is too large Load Diff

View File

@@ -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; }
void trackExpansionCycles(PPToken *tk);
inline void startNewOutputLine() const
{
if (m_state.m_result && !m_state.m_result->isEmpty() && m_state.m_result->end()[-1] != '\n')
out('\n');
}
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();
void genLine(unsigned lineno, const QByteArray &fileName) const;
const QByteArray *currentOutputBuffer() const;
QByteArray *currentOutputBuffer();
inline void out(const QByteArray &text) const
{ if (m_state.m_result) m_state.m_result->append(text); }
void enforceSpacing(const PPToken &tk, bool forceSpacing = false);
static std::size_t computeDistance(const PPToken &tk, bool forceTillLine = false);
inline void out(char ch) const
{ if (m_state.m_result) m_state.m_result->append(ch); }
inline void out(const char *s) const
{ if (m_state.m_result) m_state.m_result->append(s); }
inline void out(const ByteArrayRef &ref) const
{ if (m_state.m_result) m_state.m_result->append(ref.start(), ref.length()); }
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;