From 541762427a28af6f81a185fdfb19812e83fa30b6 Mon Sep 17 00:00:00 2001
From: jief666 <github.com@jfa.knudsen.ovh>
Date: Fri, 16 Apr 2021 12:55:51 +0300
Subject: [PATCH] Real fix to XCode optimization bug.

---
 Include/Library/printf_lite.h                         | 4 ++--
 rEFIt_UEFI/Platform/Settings.cpp                      | 7 +------
 rEFIt_UEFI/Platform/Settings.h                        | 9 +++++++++
 rEFIt_UEFI/Platform/smbios.h                          | 4 +++-
 rEFIt_UEFI/PlatformEFI/cpp_util/globals_ctor.cpp      | 8 ++++----
 rEFIt_UEFI/PlatformEFI/cpp_util/operatorNewDelete.cpp | 6 +++---
 6 files changed, 22 insertions(+), 16 deletions(-)

diff --git a/Include/Library/printf_lite.h b/Include/Library/printf_lite.h
index 91f334f72..683d0ca3d 100755
--- a/Include/Library/printf_lite.h
+++ b/Include/Library/printf_lite.h
@@ -182,7 +182,7 @@ typedef union {
 #define PRINTF_VA_ARG va_arg
 #endif
 #ifndef PRINTF_VA_END
-#define PRINTF_VA_END VA_END
+#define PRINTF_VA_END va_end
 #endif
 
 
@@ -268,7 +268,7 @@ typedef union {
       void printf_with_callback_timestamp(const char* format, transmitBufCallBackType transmitBufCallBack, void* context, int* newline, int timestamp, ...);
       inline void printf_with_callback(const char* format, transmitBufCallBackType transmitBufCallBack, void* context, ...) {
         PRINTF_VA_LIST va;
-        va_start(va, context);
+        PRINTF_VA_START(va, context);
         vprintf_with_callback_timestamp(format, va, transmitBufCallBack, context, NULL, 0);
         PRINTF_VA_END(va);
       }
diff --git a/rEFIt_UEFI/Platform/Settings.cpp b/rEFIt_UEFI/Platform/Settings.cpp
index 47928109c..04bbd72bc 100755
--- a/rEFIt_UEFI/Platform/Settings.cpp
+++ b/rEFIt_UEFI/Platform/Settings.cpp
@@ -2,10 +2,6 @@
  Slice 2012
  */
 
-#if defined(__clang__)
-#pragma clang optimize off
-#endif
-
 #include <Platform.h>
 #include "Settings.h"
 #include "FixBiosDsdt.h"
@@ -5065,9 +5061,8 @@ EFI_STATUS GetUserSettings(const TagDict* CfgDict, SETTINGS_DATA& gSettings)
 //                }
                 if ( arbProp == NULL ) {
                   arbProp = new SETTINGS_DATA::DevicesClass::ArbitraryPropertyClass();
-DBG("new ArbitraryPropertyClass()=%lld\n", uintptr_t(arbProp));
-                  arbProp->Label = Label;
                   arbProp->Device = (UINT32)DeviceAddr;
+                  arbProp->Label = Label;
                   gSettings.Devices.ArbitraryArray.AddReference(arbProp, true);
                 }
                 arbProp->CustomPropertyArray.AddReference(newDevProp, true);
diff --git a/rEFIt_UEFI/Platform/Settings.h b/rEFIt_UEFI/Platform/Settings.h
index e0ec8c5f0..220431807 100644
--- a/rEFIt_UEFI/Platform/Settings.h
+++ b/rEFIt_UEFI/Platform/Settings.h
@@ -631,6 +631,8 @@ public:
   MISC_SLOT_TYPE    SlotType = MISC_SLOT_TYPE();
   XString8          SlotName = XString8();
   
+  SLOT_DEVICE() {}
+
 	#if __cplusplus > 201703L
 		bool operator == (const SLOT_DEVICE&) const = default;
 	#endif
@@ -1279,6 +1281,8 @@ public:
         XString8                     DevicePathAsString = XString8();
         XString8                     Label = XString8();
         
+        AddPropertyClass() {}
+
 	#if __cplusplus > 201703L
 		bool operator == (const AddPropertyClass&) const = default;
 	#endif
@@ -1304,6 +1308,8 @@ public:
         TAG_TYPE                     ValueType = kTagTypeNone; // only used in CreateMenuProps()
         INPUT_ITEM                   MenuItem = INPUT_ITEM();  // Will get the Disabled value
         
+        SimplePropertyClass() {}
+
 	#if __cplusplus > 201703L
 		bool operator == (const SimplePropertyClass&) const = default;
 	#endif
@@ -1330,6 +1336,8 @@ public:
             // XString8                     Label = XString8(); // Label is the same as DevicePathAsString, so it's not needed.
             XObjArray<SimplePropertyClass>  propertiesArray = XObjArray<SimplePropertyClass>();
 
+            PropertyClass() {}
+
             EFI_DEVICE_PATH_PROTOCOL* getDevicePath() const
             {
               EFI_DEVICE_PATH_PROTOCOL* DevicePath;
@@ -1376,6 +1384,7 @@ public:
           XString8                     Label = XString8();
           XObjArray<SimplePropertyClass>   CustomPropertyArray = XObjArray<SimplePropertyClass> ();
         
+          ArbitraryPropertyClass() {}
 	#if __cplusplus > 201703L
 		bool operator == (const ArbitraryPropertyClass&) const = default;
 	#endif
diff --git a/rEFIt_UEFI/Platform/smbios.h b/rEFIt_UEFI/Platform/smbios.h
index 1d16206f0..4e29fc37e 100644
--- a/rEFIt_UEFI/Platform/smbios.h
+++ b/rEFIt_UEFI/Platform/smbios.h
@@ -27,7 +27,9 @@ public:
   XString8 SerialNo = XString8();
   UINT8   Type = UINT8();
   bool  InUse = bool();
-  
+
+  RAM_SLOT_INFO() {}
+
   #if __cplusplus > 201703L
     bool operator == (const RAM_SLOT_INFO&) const = default;
   #endif
diff --git a/rEFIt_UEFI/PlatformEFI/cpp_util/globals_ctor.cpp b/rEFIt_UEFI/PlatformEFI/cpp_util/globals_ctor.cpp
index aa8e38004..8b1f49492 100755
--- a/rEFIt_UEFI/PlatformEFI/cpp_util/globals_ctor.cpp
+++ b/rEFIt_UEFI/PlatformEFI/cpp_util/globals_ctor.cpp
@@ -57,12 +57,12 @@ void construct_globals_objects(EFI_HANDLE ImageHandle)
 		{
 
 			ctor_ptr* currentCtor = (ctor_ptr*) (((UINTN) (LoadedImage->ImageBase)) + SectionHeader->PointerToRawData);
-			ctor_ptr* ctorend = (ctor_ptr*) (((UINTN) (LoadedImage->ImageBase)) + SectionHeader->PointerToRawData + SectionHeader->Misc.VirtualSize);
+			ctor_ptr* ctorEnd = (ctor_ptr*) (((UINTN) (LoadedImage->ImageBase)) + SectionHeader->PointerToRawData + SectionHeader->Misc.VirtualSize);
+      DBG("currentBegin %llX, ctorEnd %llX, %lld ctors to call\n", (UINTN)(currentCtor), (UINTN)(ctorEnd), (UINTN)(ctorEnd-currentCtor));
 			size_t i = 0;
-			while (currentCtor < ctorend)
+			while (currentCtor < ctorEnd)
 			{
-				DBG("&currentCtor[%zu] %llX %lld\n", i, (UINTN) (currentCtor), (UINTN) (currentCtor));
-				DBG("currentCtor[%zu] %llX %lld\n", i, (UINTN) (*currentCtor), (UINTN) (*currentCtor));
+				DBG("[%03zu] &ctor %08llX, will call %08llX\n", i, (UINTN)(currentCtor), (UINTN)(*currentCtor));
 				if (*currentCtor != NULL) (*currentCtor)();
 				currentCtor++;
 				i++;
diff --git a/rEFIt_UEFI/PlatformEFI/cpp_util/operatorNewDelete.cpp b/rEFIt_UEFI/PlatformEFI/cpp_util/operatorNewDelete.cpp
index 8bd61c034..99a580e81 100755
--- a/rEFIt_UEFI/PlatformEFI/cpp_util/operatorNewDelete.cpp
+++ b/rEFIt_UEFI/PlatformEFI/cpp_util/operatorNewDelete.cpp
@@ -9,7 +9,7 @@
 #if defined(_MSC_VER)
 void* operator new  (size_t count)
 #else
-void* operator new  (unsigned long count)
+void* operator new(unsigned long count)
 #endif
 {
 	void* ptr = AllocatePool(count);
@@ -21,9 +21,9 @@ void* operator new  (unsigned long count)
 }
 
 #if defined(_MSC_VER)
-void* operator new[]  (size_t count)
+void* operator new[](size_t count)
 #else
-void* operator new[]  (unsigned long count)
+void* operator new[](unsigned long count)
 #endif
 {
   void* ptr = AllocatePool(count);