kwk created this revision.
Herald added a reviewer: jdoerfert.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.
kwk planned changes to this revision.

I found the above named method hard to read because it had

a) many nested blocks and
b) one return statement at the end with some logic involved.

I decided to refactor this function by employing an early exit strategy.
In order to capture the logic in the return statement and to not have it
repeated more than once I chose to implement a very small lamda function
that captures all the variables it needs.

This is a non-functional change (NFC).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D70774

Files:
  lldb/source/Symbol/CompileUnit.cpp

Index: lldb/source/Symbol/CompileUnit.cpp
===================================================================
--- lldb/source/Symbol/CompileUnit.cpp
+++ lldb/source/Symbol/CompileUnit.cpp
@@ -275,80 +275,90 @@
 
   const uint32_t prev_size = sc_list.GetSize();
 
+  auto returnLambda = [&sc_list, prev_size] {
+    return sc_list.GetSize() - prev_size;
+  };
+
   SymbolContext sc(GetModule());
   sc.comp_unit = this;
 
-  if (line != 0) {
-    LineTable *line_table = sc.comp_unit->GetLineTable();
-
-    if (line_table != nullptr) {
-      uint32_t found_line;
-      uint32_t line_idx;
-
-      if (num_file_indexes == 1) {
-        // We only have a single support file that matches, so use the line
-        // table function that searches for a line entries that match a single
-        // support file index
-        LineEntry line_entry;
-        line_idx = line_table->FindLineEntryIndexByFileIndex(
-            0, file_indexes.front(), line, exact, &line_entry);
-
-        // If "exact == true", then "found_line" will be the same as "line". If
-        // "exact == false", the "found_line" will be the closest line entry
-        // with a line number greater than "line" and we will use this for our
-        // subsequent line exact matches below.
-        found_line = line_entry.line;
-
-        while (line_idx != UINT32_MAX) {
-          // If they only asked for the line entry, then we're done, we can
-          // just copy that over. But if they wanted more than just the line
-          // number, fill it in.
-          if (resolve_scope == eSymbolContextLineEntry) {
-            sc.line_entry = line_entry;
-          } else {
-            line_entry.range.GetBaseAddress().CalculateSymbolContext(
-                &sc, resolve_scope);
-          }
-
-          sc_list.Append(sc);
-          line_idx = line_table->FindLineEntryIndexByFileIndex(
-              line_idx + 1, file_indexes.front(), found_line, true,
-              &line_entry);
-        }
-      } else {
-        // We found multiple support files that match "file_spec" so use the
-        // line table function that searches for a line entries that match a
-        // multiple support file indexes.
-        LineEntry line_entry;
-        line_idx = line_table->FindLineEntryIndexByFileIndex(
-            0, file_indexes, line, exact, &line_entry);
-
-        // If "exact == true", then "found_line" will be the same as "line". If
-        // "exact == false", the "found_line" will be the closest line entry
-        // with a line number greater than "line" and we will use this for our
-        // subsequent line exact matches below.
-        found_line = line_entry.line;
-
-        while (line_idx != UINT32_MAX) {
-          if (resolve_scope == eSymbolContextLineEntry) {
-            sc.line_entry = line_entry;
-          } else {
-            line_entry.range.GetBaseAddress().CalculateSymbolContext(
-                &sc, resolve_scope);
-          }
-
-          sc_list.Append(sc);
-          line_idx = line_table->FindLineEntryIndexByFileIndex(
-              line_idx + 1, file_indexes, found_line, true, &line_entry);
-        }
-      }
-    }
+  if (line == 0) {
+    return returnLambda();
   } else if (file_spec_matches_cu_file_spec && !check_inlines) {
     // only append the context if we aren't looking for inline call sites by
     // file and line and if the file spec matches that of the compile unit
     sc_list.Append(sc);
+    return returnLambda();
+  }
+
+  LineTable *line_table = sc.comp_unit->GetLineTable();
+
+  if (line_table == nullptr)
+    return returnLambda();
+
+  uint32_t found_line;
+  uint32_t line_idx;
+  LineEntry line_entry;
+
+  if (num_file_indexes == 1) {
+    // We only have a single support file that matches, so use the line
+    // table function that searches for a line entries that match a single
+    // support file index
+    line_idx = line_table->FindLineEntryIndexByFileIndex(
+        0, file_indexes.front(), line, exact, &line_entry);
+
+    // If "exact == true", then "found_line" will be the same as "line". If
+    // "exact == false", the "found_line" will be the closest line entry
+    // with a line number greater than "line" and we will use this for our
+    // subsequent line exact matches below.
+    found_line = line_entry.line;
+
+    while (line_idx != UINT32_MAX) {
+      // If they only asked for the line entry, then we're done, we can
+      // just copy that over. But if they wanted more than just the line
+      // number, fill it in.
+      if (resolve_scope == eSymbolContextLineEntry) {
+        sc.line_entry = line_entry;
+      } else {
+        line_entry.range.GetBaseAddress().CalculateSymbolContext(
+            &sc, resolve_scope);
+      }
+
+      sc_list.Append(sc);
+      line_idx = line_table->FindLineEntryIndexByFileIndex(
+          line_idx + 1, file_indexes.front(), found_line, true,
+          &line_entry);
+    }
+    return returnLambda();
+  }
+  
+  // We found multiple support files that match "file_spec" so use the
+  // line table function that searches for a line entries that match a
+  // multiple support file indexes.
+ 
+  line_idx = line_table->FindLineEntryIndexByFileIndex(
+      0, file_indexes, line, exact, &line_entry);
+
+  // If "exact == true", then "found_line" will be the same as "line". If
+  // "exact == false", the "found_line" will be the closest line entry
+  // with a line number greater than "line" and we will use this for our
+  // subsequent line exact matches below.
+  found_line = line_entry.line;
+
+  while (line_idx != UINT32_MAX) {
+    if (resolve_scope == eSymbolContextLineEntry) {
+      sc.line_entry = line_entry;
+    } else {
+      line_entry.range.GetBaseAddress().CalculateSymbolContext(
+          &sc, resolve_scope);
+    }
+
+    sc_list.Append(sc);
+    line_idx = line_table->FindLineEntryIndexByFileIndex(
+        line_idx + 1, file_indexes, found_line, true, &line_entry);
   }
-  return sc_list.GetSize() - prev_size;
+  
+  return returnLambda();
 }
 
 bool CompileUnit::GetIsOptimized() {
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to