[llvm] r260939 - Generate functions in 2 steps in the C API echo test. NFC

Amaury Sechet via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 15 23:08:49 PST 2016


Author: deadalnix
Date: Tue Feb 16 01:08:49 2016
New Revision: 260939

URL: http://llvm.org/viewvc/llvm-project?rev=260939&view=rev
Log:
Generate functions in 2 steps in the C API echo test. NFC

Modified:
    llvm/trunk/tools/llvm-c-test/echo.cpp

Modified: llvm/trunk/tools/llvm-c-test/echo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-c-test/echo.cpp?rev=260939&r1=260938&r2=260939&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-c-test/echo.cpp (original)
+++ llvm/trunk/tools/llvm-c-test/echo.cpp Tue Feb 16 01:08:49 2016
@@ -585,27 +585,53 @@ struct FunCloner {
   }
 };
 
-static LLVMValueRef clone_function(LLVMValueRef Src, LLVMModuleRef M) {
+static void clone_function(LLVMValueRef Src, LLVMModuleRef M) {
   const char *Name = LLVMGetValueName(Src);
   LLVMValueRef Fun = LLVMGetNamedFunction(M, Name);
-  if (Fun != nullptr)
-    return Fun;
+  if (!Fun)
+    report_fatal_error("Function must have been declared already");
 
-  LLVMTypeRef FunTy = LLVMGetElementType(TypeCloner(M).Clone(Src));
-  Fun = LLVMAddFunction(M, Name, FunTy);
   FunCloner FC(Src, Fun);
   FC.CloneBBs(Src);
+}
+
+static void declare_function(LLVMValueRef Src, LLVMModuleRef M) {
+  const char *Name = LLVMGetValueName(Src);
+  LLVMValueRef Fun = LLVMGetNamedFunction(M, Name);
+  if (Fun != nullptr)
+    report_fatal_error("Function already cloned");
 
-  return Fun;
+  LLVMTypeRef FunTy = LLVMGetElementType(TypeCloner(M).Clone(Src));
+  LLVMAddFunction(M, Name, FunTy);
 }
 
 static void clone_functions(LLVMModuleRef Src, LLVMModuleRef Dst) {
   LLVMValueRef Begin = LLVMGetFirstFunction(Src);
   LLVMValueRef End = LLVMGetLastFunction(Src);
 
+  // First pass, we declare all function
   LLVMValueRef Cur = Begin;
   LLVMValueRef Next = nullptr;
   while (true) {
+    declare_function(Cur, Dst);
+    Next = LLVMGetNextFunction(Cur);
+    if (Next == nullptr) {
+      if (Cur != End)
+        report_fatal_error("Last function does not match End");
+      break;
+    }
+
+    LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
+    if (Prev != Cur)
+      report_fatal_error("Next.Previous function is not Current");
+
+    Cur = Next;
+  }
+
+  // Second pass, we define them
+  Cur = Begin;
+  Next = nullptr;
+  while (true) {
     clone_function(Cur, Dst);
     Next = LLVMGetNextFunction(Cur);
     if (Next == nullptr) {




More information about the llvm-commits mailing list