C++: Don't strip comments from preprocessed source code.

Change-Id: I3a4817d36b9c724abca504c42914a73f97ab34c7
Reviewed-on: http://codereview.qt.nokia.com/3480
Reviewed-by: Roberto Raggi <roberto.raggi@nokia.com>
This commit is contained in:
Christian Kamm
2011-08-23 14:05:47 +02:00
parent 51ea52971f
commit 93e367ed0c
4 changed files with 27 additions and 3 deletions

View File

@@ -45,6 +45,7 @@ FastPreprocessor::FastPreprocessor(const Snapshot &snapshot)
QByteArray FastPreprocessor::run(QString fileName, const QString &source) QByteArray FastPreprocessor::run(QString fileName, const QString &source)
{ {
_preproc.setExpandMacros(false); _preproc.setExpandMacros(false);
_preproc.setKeepComments(true);
if (Document::Ptr doc = _snapshot.document(fileName)) { if (Document::Ptr doc = _snapshot.document(fileName)) {
_merged.insert(fileName); _merged.insert(fileName);

View File

@@ -461,7 +461,8 @@ Preprocessor::Preprocessor(Client *client, Environment *env)
_dot(_tokens.end()), _dot(_tokens.end()),
_result(0), _result(0),
_markGeneratedTokens(false), _markGeneratedTokens(false),
_expandMacros(true) _expandMacros(true),
_keepComments(false)
{ {
resetIfLevel (); resetIfLevel ();
} }
@@ -558,12 +559,24 @@ void Preprocessor::setExpandMacros(bool expandMacros)
_expandMacros = expandMacros; _expandMacros = expandMacros;
} }
bool Preprocessor::keepComments() const
{
return _keepComments;
}
void Preprocessor::setKeepComments(bool keepComments)
{
_keepComments = keepComments;
}
Preprocessor::State Preprocessor::createStateFromSource(const QByteArray &source) const Preprocessor::State Preprocessor::createStateFromSource(const QByteArray &source) const
{ {
State state; State state;
state.source = source; state.source = source;
Lexer lex(state.source.constBegin(), state.source.constEnd()); Lexer lex(state.source.constBegin(), state.source.constEnd());
lex.setScanKeywords(false); lex.setScanKeywords(false);
if (_keepComments)
lex.setScanCommentTokens(true);
Token tok; Token tok;
do { do {
lex(&tok); lex(&tok);
@@ -578,7 +591,11 @@ void Preprocessor::processNewline(bool force)
if (_dot != _tokens.constBegin()) { if (_dot != _tokens.constBegin()) {
TokenIterator prevTok = _dot - 1; TokenIterator prevTok = _dot - 1;
if (prevTok->isLiteral()) { // line changes due to multi-line tokens
if (prevTok->isLiteral()
|| (_keepComments
&& (prevTok->kind() == T_COMMENT
|| prevTok->kind() == T_DOXY_COMMENT))) {
const char *ptr = _source.constBegin() + prevTok->begin(); const char *ptr = _source.constBegin() + prevTok->begin();
const char *end = ptr + prevTok->length(); const char *end = ptr + prevTok->length();

View File

@@ -79,6 +79,9 @@ public:
bool expandMacros() const; bool expandMacros() const;
void setExpandMacros(bool expandMacros); void setExpandMacros(bool expandMacros);
bool keepComments() const;
void setKeepComments(bool keepComments);
private: private:
enum { MAX_LEVEL = 512 }; enum { MAX_LEVEL = 512 };
@@ -197,6 +200,7 @@ private:
QString _originalSource; QString _originalSource;
bool _expandMacros; bool _expandMacros;
bool _keepComments;
}; };
} // namespace CPlusPlus } // namespace CPlusPlus

View File

@@ -173,7 +173,9 @@ CppPreprocessor::CppPreprocessor(QPointer<CppModelManager> modelManager)
m_modelManager(modelManager), m_modelManager(modelManager),
preprocess(this, &env), preprocess(this, &env),
m_revision(0) m_revision(0)
{ } {
preprocess.setKeepComments(true);
}
#else #else