[clang] [clang][ASTImporter] Support Importer of BuiltinBitCastExpr (PR #74813)

Qizhi Hu via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 7 23:30:05 PST 2023


https://github.com/jcsxky created https://github.com/llvm/llvm-project/pull/74813

This patch aims to fix the [crash](https://github.com/llvm/llvm-project/issues/74774)

>From 49ec0838ecef753d86562ce4c12b3d84000a859e Mon Sep 17 00:00:00 2001
From: huqizhi <huqizhi at feysh.com>
Date: Fri, 8 Dec 2023 15:26:01 +0800
Subject: [PATCH] [clang][ASTImporter] Support Importer of BuiltinBitCastExpr

---
 clang/lib/AST/ASTImporter.cpp           | 12 ++++++++++++
 clang/unittests/AST/ASTImporterTest.cpp | 20 ++++++++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index f1f335118f37a..79ca48bf9ec0f 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -7820,6 +7820,18 @@ ExpectedStmt ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
         *ToLParenLocOrErr, OCE->getBridgeKind(), E->getCastKind(),
         *ToBridgeKeywordLocOrErr, ToTypeInfoAsWritten, ToSubExpr);
   }
+  case Stmt::BuiltinBitCastExprClass: {
+    auto *BBC = cast<BuiltinBitCastExpr>(E);
+    ExpectedSLoc ToLParenLocOrErr = import(BBC->getBeginLoc());
+    if (!ToLParenLocOrErr)
+      return ToLParenLocOrErr.takeError();
+    ExpectedSLoc ToRParenLocOrErr = import(BBC->getEndLoc());
+    if (!ToRParenLocOrErr)
+      return ToRParenLocOrErr.takeError();
+    return new (Importer.getToContext()) BuiltinBitCastExpr(
+        ToType, E->getValueKind(), E->getCastKind(), ToSubExpr,
+        ToTypeInfoAsWritten, *ToLParenLocOrErr, *ToRParenLocOrErr);
+  }
   default:
     llvm_unreachable("Cast expression of unsupported type!");
     return make_error<ASTImportError>(ASTImportError::UnsupportedConstruct);
diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp
index 4dd7510bf8ddf..3d9c426e13895 100644
--- a/clang/unittests/AST/ASTImporterTest.cpp
+++ b/clang/unittests/AST/ASTImporterTest.cpp
@@ -10,6 +10,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "clang/AST/ASTFwd.h"
 #include "clang/AST/RecordLayout.h"
 #include "clang/ASTMatchers/ASTMatchers.h"
 #include "clang/Testing/CommandLineArgs.h"
@@ -19,6 +20,7 @@
 #include "gtest/gtest.h"
 
 #include "ASTImporterFixtures.h"
+#include "DeclMatcher.h"
 #include <optional>
 
 namespace clang {
@@ -9284,6 +9286,24 @@ TEST_P(ASTImporterOptionSpecificTestBase,
   // EXPECT_EQ(ToF1Imported->getPreviousDecl(), ToF1);
 }
 
+const internal::VariadicDynCastAllOfMatcher<Stmt, BuiltinBitCastExpr>
+    builtinBitCastExpr;
+
+TEST_P(ASTImporterOptionSpecificTestBase, ImportBuiltinBitCastExpr) {
+  const char *CodeFrom =
+      R"(
+      void foo(int T) { (void)__builtin_bit_cast(float, T); }
+      )";
+  Decl *FromTU = getTuDecl(CodeFrom, Lang_CXX20);
+  auto *FromFoo = FirstDeclMatcher<FunctionDecl>().match(
+      FromTU, functionDecl(hasName("foo")));
+  auto *ToFoo = Import(FromFoo, Lang_CXX20);
+  EXPECT_TRUE(ToFoo);
+  auto *ToBuiltinBitCastExpr =
+      FirstDeclMatcher<BuiltinBitCastExpr>().match(ToFoo, builtinBitCastExpr());
+  EXPECT_TRUE(ToBuiltinBitCastExpr);
+}
+
 INSTANTIATE_TEST_SUITE_P(ParameterizedTests, ASTImporterLookupTableTest,
                          DefaultTestValuesForRunOptions);
 



More information about the cfe-commits mailing list