Skip to content

[lldb][ClangExpressionParser] Log and assert on failure to create TargetInfo #101697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 2, 2024

Conversation

Michael137
Copy link
Member

@Michael137 Michael137 commented Aug 2, 2024

CreateTargetInfo can return a nullptr in a couple cases. So we should log that and let the user know something is wrong (hence the lldbassert).

I didn't actually run into this. Just stumbled upon it from reading the code.

@llvmbot
Copy link
Member

llvmbot commented Aug 2, 2024

@llvm/pr-subscribers-lldb

Author: Michael Buch (Michael137)

Changes

CreateTargetInfo can return a nullptr in a couple cases. So we should log that and let the user know something is wrong (hence the lldbassert).

I didn't actually run into this. Just stumbled upon it from reading the code.


Full diff: https://github.com/llvm/llvm-project/pull/101697.diff

1 Files Affected:

  • (modified) lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp (+17-11)
diff --git a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
index 303e88feea20b..9d9403ebb9051 100644
--- a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
+++ b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
@@ -465,18 +465,24 @@ ClangExpressionParser::ClangExpressionParser(
   // A value of 0 means no limit for both LLDB and Clang.
   m_compiler->getDiagnostics().setErrorLimit(target_sp->GetExprErrorLimit());
 
-  auto target_info = TargetInfo::CreateTargetInfo(
-      m_compiler->getDiagnostics(), m_compiler->getInvocation().TargetOpts);
-  if (log) {
-    LLDB_LOGF(log, "Target datalayout string: '%s'",
-              target_info->getDataLayoutString());
-    LLDB_LOGF(log, "Target ABI: '%s'", target_info->getABI().str().c_str());
-    LLDB_LOGF(log, "Target vector alignment: %d",
-              target_info->getMaxVectorAlign());
-  }
-  m_compiler->setTarget(target_info);
+  if (auto *target_info = TargetInfo::CreateTargetInfo(
+          m_compiler->getDiagnostics(),
+          m_compiler->getInvocation().TargetOpts)) {
+    if (log) {
+      LLDB_LOGF(log, "Target datalayout string: '%s'",
+                target_info->getDataLayoutString());
+      LLDB_LOGF(log, "Target ABI: '%s'", target_info->getABI().str().c_str());
+      LLDB_LOGF(log, "Target vector alignment: %d",
+                target_info->getMaxVectorAlign());
+    }
+    m_compiler->setTarget(target_info);
+  } else {
+    if (log)
+      LLDB_LOGF(log, "Failed to create TargetInfo for '%s'",
+                m_compiler->getTargetOpts().Triple.c_str());
 
-  assert(m_compiler->hasTarget());
+    lldbassert(false && "Failed to create TargetInfo.");
+  }
 
   // 4. Set language options.
   lldb::LanguageType language = expr.Language().AsLanguageType();


assert(m_compiler->hasTarget());
lldbassert(false && "Failed to create TargetInfo.");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on that there used to be an assert here, should this return?
Same comment as in the previous review — ideally this would return an llvm::Error here if it weren't a constructor.

Copy link
Member Author

@Michael137 Michael137 Aug 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit awkward because, as you also noted, this all happens in the constructor. And stopping the construction half-way probably won't buy us much. I think this is one of those unusual circumstances where we want to assert at runtime. I guess we could have a separate Init method for the ClangExpressionParser. Which returns Expected. Then update all the users to check the return of that.

But that's a larger refactor that we can do at some later point.

What used to happen in release builds (tbh I'm not sure if this ever really gets exercised in practice), is that target_info was a nullptr. And we just happily continued with the construction of the object.

@Michael137 Michael137 merged commit 5c1d768 into llvm:main Aug 2, 2024
8 checks passed
adrian-prantl pushed a commit to adrian-prantl/llvm-project that referenced this pull request Oct 11, 2024
…getInfo (llvm#101697)

`CreateTargetInfo` can return a `nullptr` in a couple cases. So we
should log that and let the user know something is wrong (hence the
`lldbassert`).

I didn't actually run into this. Just stumbled upon it from reading the
code.

(cherry picked from commit 5c1d768)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants