Refactor ClangUserExpression::GetLanguageForExpr

Summary:
The `ClangUserExpression::GetLanguageForExpr` method is currently a big
source of sadness, as it's name implies that it's an accessor method, but it actually
is also initializing some variables that we need for parsing. This caused that we
currently call this getter just for it's side effects while ignoring it's return value,
which is confusing for the reader.

This patch renames it to `UpdateLanguageForExpr` and merges all calls to the
method into a single call in `ClangUserExpression::PrepareForParsing` (as calling
this method is anyway mandatory for parsing to succeed)

While looking at the code, I also found that we actually have two language
variables in this class hierarchy. The normal `Language` from the UserExpression
class and the `LanguageForExpr` that we implemented in this subclass. Both
don't seem to actually contain the same value, so we probably should look at this
next.

Reviewers: xbolva00

Reviewed By: xbolva00

Subscribers: lldb-commits

Differential Revision: https://reviews.llvm.org/D52561

llvm-svn: 343191
diff --git a/lldb/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp b/lldb/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp
index e097622..f42955d 100644
--- a/lldb/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp
+++ b/lldb/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp
@@ -376,9 +376,9 @@
   }
 }
 
-llvm::Optional<lldb::LanguageType> ClangUserExpression::GetLanguageForExpr(
+void ClangUserExpression::UpdateLanguageForExpr(
     DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx) {
-  lldb::LanguageType lang_type = lldb::LanguageType::eLanguageTypeUnknown;
+  m_expr_lang = lldb::LanguageType::eLanguageTypeUnknown;
 
   std::string prefix = m_expr_prefix;
 
@@ -390,17 +390,17 @@
                                             m_expr_text.c_str()));
 
     if (m_in_cplusplus_method)
-      lang_type = lldb::eLanguageTypeC_plus_plus;
+      m_expr_lang = lldb::eLanguageTypeC_plus_plus;
     else if (m_in_objectivec_method)
-      lang_type = lldb::eLanguageTypeObjC;
+      m_expr_lang = lldb::eLanguageTypeObjC;
     else
-      lang_type = lldb::eLanguageTypeC;
+      m_expr_lang = lldb::eLanguageTypeC;
 
-    if (!source_code->GetText(m_transformed_text, lang_type, m_in_static_method,
-                              exe_ctx)) {
+    if (!source_code->GetText(m_transformed_text, m_expr_lang,
+                              m_in_static_method, exe_ctx)) {
       diagnostic_manager.PutString(eDiagnosticSeverityError,
                                    "couldn't construct expression body");
-      return llvm::Optional<lldb::LanguageType>();
+      return;
     }
 
     // Find and store the start position of the original code inside the
@@ -408,12 +408,11 @@
     std::size_t original_start;
     std::size_t original_end;
     bool found_bounds = source_code->GetOriginalBodyBounds(
-        m_transformed_text, lang_type, original_start, original_end);
+        m_transformed_text, m_expr_lang, original_start, original_end);
     if (found_bounds) {
       m_user_expression_start_pos = original_start;
     }
   }
-  return lang_type;
 }
 
 bool ClangUserExpression::PrepareForParsing(
@@ -437,6 +436,8 @@
   ApplyObjcCastHack(m_expr_text);
 
   SetupDeclVendor(exe_ctx, m_target);
+
+  UpdateLanguageForExpr(diagnostic_manager, exe_ctx);
   return true;
 }
 
@@ -450,11 +451,6 @@
   if (!PrepareForParsing(diagnostic_manager, exe_ctx))
     return false;
 
-  lldb::LanguageType lang_type = lldb::LanguageType::eLanguageTypeUnknown;
-  if (auto new_lang = GetLanguageForExpr(diagnostic_manager, exe_ctx)) {
-    lang_type = new_lang.getValue();
-  }
-
   if (log)
     log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
 
@@ -514,7 +510,7 @@
         const std::string &fixed_expression =
             diagnostic_manager.GetFixedExpression();
         if (ExpressionSourceCode::GetOriginalBodyBounds(
-                fixed_expression, lang_type, fixed_start, fixed_end))
+                fixed_expression, m_expr_lang, fixed_start, fixed_end))
           m_fixed_text =
               fixed_expression.substr(fixed_start, fixed_end - fixed_start);
       }
@@ -655,8 +651,6 @@
   if (!PrepareForParsing(diagnostic_manager, exe_ctx))
     return false;
 
-  GetLanguageForExpr(diagnostic_manager, exe_ctx);
-
   if (log)
     log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());