teemperor created this revision.
teemperor added a reviewer: xbolva00.
Herald added a subscriber: lldb-commits.

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.


Repository:
  rLLDB LLDB

https://reviews.llvm.org/D52561

Files:
  source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp
  source/Plugins/ExpressionParser/Clang/ClangUserExpression.h

Index: source/Plugins/ExpressionParser/Clang/ClangUserExpression.h
===================================================================
--- source/Plugins/ExpressionParser/Clang/ClangUserExpression.h
+++ source/Plugins/ExpressionParser/Clang/ClangUserExpression.h
@@ -177,8 +177,8 @@
                     lldb::addr_t struct_address,
                     DiagnosticManager &diagnostic_manager) override;
 
-  llvm::Optional<lldb::LanguageType> GetLanguageForExpr(
-      DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx);
+  void UpdateLanguageForExpr(DiagnosticManager &diagnostic_manager,
+                             ExecutionContext &exe_ctx);
   bool SetupPersistentState(DiagnosticManager &diagnostic_manager,
                                    ExecutionContext &exe_ctx);
   bool PrepareForParsing(DiagnosticManager &diagnostic_manager,
@@ -201,6 +201,9 @@
     lldb::TargetSP m_target_sp;
   };
 
+  /// The language type of the current expression.
+  lldb::LanguageType m_expr_lang = lldb::eLanguageTypeUnknown;
+
   /// The absolute character position in the transformed source code where the
   /// user code (as typed by the user) starts. If the variable is empty, then we
   /// were not able to calculate this position.
Index: source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp
===================================================================
--- source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp
+++ 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,30 +390,29 @@
                                             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
     // transformed code. We need this later for the code completion.
     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());
 
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
  • [Lldb-commits] [PATCH] ... Raphael Isemann via Phabricator via lldb-commits

Reply via email to