From a309b3cfe6173a923988f645aad0488d65589f0f Mon Sep 17 00:00:00 2001
From: Orgad Shaneh <orgad.shaneh@audiocodes.com>
Date: Sat, 18 Jan 2014 19:51:57 +0200
Subject: [PATCH] C++: Store token kind as lexer state

... when needed

Change-Id: I32a1649c87e1fa42da80eff5003b2f5714062064
Reviewed-by: Erik Verbruggen <erik.verbruggen@digia.com>
---
 src/libs/3rdparty/cplusplus/Lexer.cpp     | 24 +++++++++++------------
 src/libs/3rdparty/cplusplus/Lexer.h       |  6 ------
 src/plugins/cppeditor/cpphighlighter.cpp  |  6 +++---
 src/plugins/cpptools/cppcodeformatter.cpp |  8 ++++----
 4 files changed, 19 insertions(+), 25 deletions(-)

diff --git a/src/libs/3rdparty/cplusplus/Lexer.cpp b/src/libs/3rdparty/cplusplus/Lexer.cpp
index 40ae37daee3..a9f783b7054 100644
--- a/src/libs/3rdparty/cplusplus/Lexer.cpp
+++ b/src/libs/3rdparty/cplusplus/Lexer.cpp
@@ -32,7 +32,7 @@ using namespace CPlusPlus;
 Lexer::Lexer(TranslationUnit *unit)
     : _translationUnit(unit),
       _control(unit->control()),
-      _state(State_Default),
+      _state(T_EOF_SYMBOL),
       _flags(0),
       _currentLine(1)
 {
@@ -44,7 +44,7 @@ Lexer::Lexer(TranslationUnit *unit)
 Lexer::Lexer(const char *firstChar, const char *lastChar)
     : _translationUnit(0),
       _control(0),
-      _state(State_Default),
+      _state(T_EOF_SYMBOL),
       _flags(0),
       _currentLine(1)
 {
@@ -145,7 +145,9 @@ void Lexer::scan_helper(Token *tok)
     _tokenStart = _currentChar;
     tok->offset = _currentChar - _firstChar;
 
-    if (_state == State_MultiLineComment || _state == State_MultiLineDoxyComment) {
+    switch (_state) {
+    case T_COMMENT:
+    case T_DOXY_COMMENT: {
         const int originalState = _state;
 
         if (! _yychar) {
@@ -160,7 +162,7 @@ void Lexer::scan_helper(Token *tok)
                 yyinp();
                 if (_yychar == '/') {
                     yyinp();
-                    _state = State_Default;
+                    _state = T_EOF_SYMBOL;
                     break;
                 }
             }
@@ -169,12 +171,10 @@ void Lexer::scan_helper(Token *tok)
         if (! f._scanCommentTokens)
             goto _Lagain;
 
-        else if (originalState == State_MultiLineComment)
-            tok->f.kind = T_COMMENT;
-        else
-            tok->f.kind = T_DOXY_COMMENT;
+        tok->f.kind = originalState;
         return; // done
     }
+    }
 
     if (! _yychar) {
         tok->f.kind = T_EOF_SYMBOL;
@@ -374,7 +374,7 @@ void Lexer::scan_helper(Token *tok)
         } else if (_yychar == '*') {
             yyinp();
 
-            bool doxy = false;
+            Kind commentKind = T_COMMENT;
 
             if (_yychar == '*' || _yychar == '!') {
                 const char ch = _yychar;
@@ -388,7 +388,7 @@ void Lexer::scan_helper(Token *tok)
                     yyinp();
 
                 if (! _yychar || std::isspace(_yychar))
-                    doxy = true;
+                    commentKind = T_DOXY_COMMENT;
             }
 
             while (_yychar) {
@@ -405,12 +405,12 @@ void Lexer::scan_helper(Token *tok)
             if (_yychar)
                 yyinp();
             else
-                _state = doxy ? State_MultiLineDoxyComment : State_MultiLineComment;
+                _state = commentKind;
 
             if (! f._scanCommentTokens)
                 goto _Lagain;
 
-            tok->f.kind = doxy ? T_DOXY_COMMENT : T_COMMENT;
+            tok->f.kind = commentKind;
 
         } else if (_yychar == '=') {
             yyinp();
diff --git a/src/libs/3rdparty/cplusplus/Lexer.h b/src/libs/3rdparty/cplusplus/Lexer.h
index 9d4b7b601d2..69c17ff52d6 100644
--- a/src/libs/3rdparty/cplusplus/Lexer.h
+++ b/src/libs/3rdparty/cplusplus/Lexer.h
@@ -32,12 +32,6 @@ class CPLUSPLUS_EXPORT Lexer
     void operator =(const Lexer &other);
 
 public:
-    enum State {
-        State_Default,
-        State_MultiLineComment,
-        State_MultiLineDoxyComment
-    };
-
     Lexer(TranslationUnit *unit);
     Lexer(const char *firstChar, const char *lastChar);
     ~Lexer();
diff --git a/src/plugins/cppeditor/cpphighlighter.cpp b/src/plugins/cppeditor/cpphighlighter.cpp
index 06a3c18ce1e..fd3d39a182d 100644
--- a/src/plugins/cppeditor/cpphighlighter.cpp
+++ b/src/plugins/cppeditor/cpphighlighter.cpp
@@ -66,7 +66,7 @@ CppHighlighter::CppHighlighter(QTextDocument *document) :
 void CppHighlighter::highlightBlock(const QString &text)
 {
     const int previousState = previousBlockState();
-    int state = 0, initialBraceDepth = 0;
+    int state = T_EOF_SYMBOL, initialBraceDepth = 0;
     if (previousState != -1) {
         state = previousState & 0xff;
         initialBraceDepth = previousState >> 8;
@@ -96,9 +96,9 @@ void CppHighlighter::highlightBlock(const QString &text)
         setCurrentBlockState(previousState);
         BaseTextDocumentLayout::clearParentheses(currentBlock());
         if (text.length())  {// the empty line can still contain whitespace
-            if (initialState == Lexer::State_MultiLineComment)
+            if (initialState == T_COMMENT)
                 highlightLine(text, 0, text.length(), formatForCategory(CppCommentFormat));
-            else if (initialState == Lexer::State_MultiLineDoxyComment)
+            else if (initialState == T_DOXY_COMMENT)
                 highlightLine(text, 0, text.length(), formatForCategory(CppDoxygenCommentFormat));
             else
                 setFormat(0, text.length(), formatForCategory(CppVisualWhitespace));
diff --git a/src/plugins/cpptools/cppcodeformatter.cpp b/src/plugins/cpptools/cppcodeformatter.cpp
index c15e8c4aac6..db557959be5 100644
--- a/src/plugins/cpptools/cppcodeformatter.cpp
+++ b/src/plugins/cpptools/cppcodeformatter.cpp
@@ -504,7 +504,7 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block)
                 leave();
                 continue;
             } else if (m_tokenIndex == m_tokens.size() - 1
-                    && lexerState == Lexer::State_Default) {
+                    && lexerState == T_EOF_SYMBOL) {
                 leave();
             } else if (m_tokenIndex == 0 && m_currentToken.isComment()) {
                 // to allow enter/leave to update the indentDepth
@@ -571,8 +571,8 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block)
 
     if (topState != multiline_comment_start
             && topState != multiline_comment_cont
-            && (lexerState == Lexer::State_MultiLineComment
-                || lexerState == Lexer::State_MultiLineDoxyComment)) {
+            && (lexerState == T_COMMENT
+                || lexerState == T_DOXY_COMMENT)) {
         enter(multiline_comment_start);
     }
 
@@ -1612,7 +1612,7 @@ void QtStyleCodeFormatter::adjustIndent(const QList<CPlusPlus::Token> &tokens, i
         if ((topState.type == multiline_comment_cont
              || topState.type == multiline_comment_start)
                 && (kind == T_COMMENT || kind == T_DOXY_COMMENT)
-                && (lexerState == Lexer::State_Default
+                && (lexerState == T_EOF_SYMBOL
                     || tokens.size() != 1)) {
             if (*indentDepth >= m_tabSettings.m_indentSize)
                 *indentDepth -= m_tabSettings.m_indentSize;
-- 
GitLab