Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mattwarren/36e52b3f80a411ca5a6b7211c9f1a3a9 to your computer and use it in GitHub Desktop.
Save mattwarren/36e52b3f80a411ca5a6b7211c9f1a3a9 to your computer and use it in GitHub Desktop.
HELPER METHOD FRAME Macro - SystemNative::GetCommandLineArgs(..)

Done using how to expand VC++ macro references using Visual Studio? and Tutorials Point ONLINE C/C++ FORMATTER


Original code - 43 LOC (from here)

FCIMPL0(Object*, SystemNative::GetCommandLineArgs)
{
    FCALL_CONTRACT;

    PTRARRAYREF strArray = NULL;

    HELPER_METHOD_FRAME_BEGIN_RET_1(strArray);

    LPWSTR commandLine;

    if (g_pCachedCommandLine != NULL)
    {
        // Use the cached command line if available
        commandLine = g_pCachedCommandLine;
    }
    else
    {
        commandLine = WszGetCommandLine();
        if (commandLine==NULL)
            COMPlusThrowOM();
    }

    DWORD numArgs = 0;
    LPWSTR* argv = SegmentCommandLine(commandLine, &numArgs);
    if (!argv)
        COMPlusThrowOM();

    _ASSERTE(numArgs > 0);
    
    strArray = (PTRARRAYREF) AllocateObjectArray(numArgs, g_pStringClass);
    // Copy each argument into new Strings.
    for(unsigned int i=0; i<numArgs; i++) 
    {
        STRINGREF str = StringObject::NewString(argv[i]);
        STRINGREF * destData = ((STRINGREF*)(strArray->GetDataPtr())) + i;
        SetObjectReference((OBJECTREF*)destData, (OBJECTREF)str, strArray->GetAppDomain());
    }
    delete [] argv;

    HELPER_METHOD_FRAME_END();

    return OBJECTREFToObject(strArray); 
}
FCIMPLEND

Expanded Code - RELEASE - 81 LOC

Object * SystemNative::GetCommandLineArgs() {
  LPVOID __me;
  __me = ((LPVOID)(SystemNative::GetCommandLineArgs));
  FCallTransitionState ___FCallTransitionState;
  if (1) {
    PermitHelperMethodFrameState ___PermitHelperMethodFrameState;; {; {}; {}; {};

      PTRARRAYREF strArray = 0;

      static_assert(sizeof(strArray) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");
      static_assert(sizeof(strArray) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");
      int alwaysZero = 0;
      do {
        if (1) {
          PermitHelperMethodFrameState ___PermitHelperMethodFrameState;;
          PermitHelperMethodFrameState::CheckHelperMethodFramePermitted();
          CompletedFCallTransitionState ___CompletedFCallTransitionState;;
          FrameWithCookie < HelperMethodFrame_1OBJ > __helperframe(__me, Frame::FRAME_ATTR_NONE, (OBJECTREF * ) & strArray); {;
            LazyMachStateCaptureState(__helperframe.MachineState());
          };; {;;
            __helperframe.Push();
            Thread * CURRENT_THREAD = __helperframe.GetThread();
            const bool CURRENT_THREAD_AVAILABLE = true;
            (void) CURRENT_THREAD_AVAILABLE;;;; {
              Exception * __pUnCException = 0;
              Frame * __pUnCEntryFrame = ( & __helperframe);
              bool __fExceptionCatched = false;;
              if (true) try {; {;;;;

                  LPWSTR commandLine;

                  commandLine = GetCommandLineW();
                  if (commandLine == 0)
                    RealCOMPlusThrowOM();

                  DWORD numArgs = 0;
                  LPWSTR * argv = SegmentCommandLine(commandLine, & numArgs);
                  if (!argv)
                    RealCOMPlusThrowOM();

                  ((void) 0);

                  strArray = (PTRARRAYREF) AllocateObjectArray(numArgs, g_pStringClass);
                  // Copy each argument into new Strings.
                  for (unsigned int i = 0; i < numArgs; i++) {
                    STRINGREF str = StringObject::NewString(argv[i]);
                    STRINGREF * destData = ((STRINGREF * )(strArray - > GetDataPtr())) + i;
                    SetObjectReferenceUnchecked((OBJECTREF * ) destData, (OBJECTREF) str);
                  }
                  delete[] argv;

                  ;
                };
              } catch (Exception * __pException) {;
                do {} while (0);
                __pUnCException = __pException;
                UnwindAndContinueRethrowHelperInsideCatch(__pUnCEntryFrame, __pUnCException);
                __fExceptionCatched = true;;
              }
              if (__fExceptionCatched) {;
                UnwindAndContinueRethrowHelperAfterCatch(__pUnCEntryFrame, __pUnCException);
              }
            };;;;
            __helperframe.Pop();
          };;
          alwaysZero = HelperMethodFrameRestoreState(__helperframe.MachineState());
        } else {
          jmp_buf ___jmpbuf;
          _setjmp(___jmpbuf);
          __assume(0);
        }
      } while (alwaysZero);;;

      return ((PTR_Object)(strArray));
    }
  } else {
    jmp_buf ___jmpbuf;
    _setjmp(___jmpbuf);
    __assume(0);
  };;
}

Expanded Code - DEBUG - 114 LOC

Object * SystemNative::GetCommandLineArgs() {
  LPVOID __me;
  __me = ((LPVOID)(SystemNative::GetCommandLineArgs));
  DWORD __lastError = ::GetLastError();
  static void * __cache = 0;
  FCallAssert(__cache, (LPVOID) __me); {
    Thread * _pThread = GetThread();
    Thread::ObjectRefFlush(_pThread);
  }
  FCallCheck __fCallCheck("c:\\dev\\github\\coreclr\\src\\classlibnative\\bcltype\\system.cpp", 248);
  FCallTransitionState ___FCallTransitionState;
  if (1) {
    PermitHelperMethodFrameState ___PermitHelperMethodFrameState;;;::SetLastError(__lastError); {; {}; {}; {};

      PTRARRAYREF strArray = 0;

      static_assert(sizeof(strArray) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");
      static_assert(sizeof(strArray) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");
      int alwaysZero = 0;
      do {
        static BOOL __haveCheckedRestoreState = 0;
        if (1) {
          PermitHelperMethodFrameState ___PermitHelperMethodFrameState;;
          PermitHelperMethodFrameState::CheckHelperMethodFramePermitted();
          CompletedFCallTransitionState ___CompletedFCallTransitionState;;
          FrameWithCookie < HelperMethodFrame_1OBJ > __helperframe(__me, Frame::FRAME_ATTR_NONE, (OBJECTREF * ) & strArray); {;
            LazyMachStateCaptureState(__helperframe.MachineState());
          };
          __helperframe.SetAddrOfHaveCheckedRestoreState( & __haveCheckedRestoreState); {;
            FCallGCCanTrigger::Enter();
            __helperframe.Push();
            Thread * CURRENT_THREAD = __helperframe.GetThread();
            const bool CURRENT_THREAD_AVAILABLE = true;
            (void) CURRENT_THREAD_AVAILABLE;;
            do {
              if (CLRTestHookManager::Enabled()) CLRTestHookManager::TestHook() - > AppDomainCanBeUnloaded((CURRENT_THREAD_AVAILABLE ? CURRENT_THREAD : GetThread()) - > GetDomain() - > GetId().m_dwId, !1);
            } while (0);; {
              Exception * __pUnCException = 0;
              Frame * __pUnCEntryFrame = ( & __helperframe);
              bool __fExceptionCatched = false;;
              if (true) try {; {;;;;

                  LPWSTR commandLine;

                  commandLine = GetCommandLineW();
                  if (commandLine == 0)
                    if ((g_ExceptionFile = "c:\\dev\\github\\coreclr\\src\\classlibnative\\bcltype\\system.cpp", g_ExceptionLine = 260, 1) && 0) {} else RealCOMPlusThrowOM();

                  DWORD numArgs = 0;
                  LPWSTR * argv = SegmentCommandLine(commandLine, & numArgs);
                  if (!argv)
                    if ((g_ExceptionFile = "c:\\dev\\github\\coreclr\\src\\classlibnative\\bcltype\\system.cpp", g_ExceptionLine = 265, 1) && 0) {} else RealCOMPlusThrowOM();

                  do {
                    if (!(numArgs > 0)) {
                      DbgAssertDialog("c:\\dev\\github\\coreclr\\src\\classlibnative\\bcltype\\system.cpp", 267, "numArgs > 0");
                    }
                  } while (0);

                  strArray = (PTRARRAYREF) AllocateObjectArray(numArgs, g_pStringClass);
                  // Copy each argument into new Strings.
                  for (unsigned int i = 0; i < numArgs; i++) {
                    STRINGREF str = StringObject::NewString(argv[i]);
                    STRINGREF * destData = ((STRINGREF * )(strArray - > GetDataPtr())) + i;
                    SetObjectReferenceUnchecked((OBJECTREF * ) destData, (OBJECTREF) str);
                  }
                  delete[] argv;

                  ;
                };
              } catch (Exception * __pException) {;
                do {
                  CHECK _check;
                  if (_check.EnterAssert()) {
                    DebugOnlyCodeHolder __debugOnlyCodeHolder;
                    __debugOnlyCodeHolder.Enter();;
                    if (_check.Fail(0 != __pException)) {
                      _check.Setup("", "0 != __pException", "c:\\dev\\github\\coreclr\\src\\classlibnative\\bcltype\\system.cpp", 279);
                      _check.Trigger("Consistency check failed");
                    }
                    __debugOnlyCodeHolder.Leave();;
                    _check.LeaveAssert();
                  }
                } while (0);
                __pUnCException = __pException;
                UnwindAndContinueRethrowHelperInsideCatch(__pUnCEntryFrame, __pUnCException);
                __fExceptionCatched = true;;
              }
              if (__fExceptionCatched) {;
                UnwindAndContinueRethrowHelperAfterCatch(__pUnCEntryFrame, __pUnCException);
              }
            };;;
            do {
              if (CLRTestHookManager::Enabled()) CLRTestHookManager::TestHook() - > AppDomainCanBeUnloaded((CURRENT_THREAD_AVAILABLE ? CURRENT_THREAD : GetThread()) - > GetDomain() - > GetId().m_dwId, !0);
            } while (0);
            __helperframe.Pop();
          };
          FCallGCCanTrigger::Leave(__FUNCTION__, "c:\\dev\\github\\coreclr\\src\\classlibnative\\bcltype\\system.cpp", 279);
          alwaysZero = HelperMethodFrameRestoreState( & __helperframe, __helperframe.MachineState());
        } else {
          jmp_buf ___jmpbuf;
          _setjmp(___jmpbuf);
          __assume(0);
        }
      } while (alwaysZero);;;

      return ((strArray).operator - > ());
    }
  } else {
    jmp_buf ___jmpbuf;
    _setjmp(___jmpbuf);
    __assume(0);
  };;
}

Diff - Release v. Debug

@@ -1,47 +1,64 @@
 Object * SystemNative::GetCommandLineArgs() {
   LPVOID __me;
   __me = ((LPVOID)(SystemNative::GetCommandLineArgs));
+  DWORD __lastError = ::GetLastError();
+  static void * __cache = 0;
+  FCallAssert(__cache, (LPVOID) __me); {
+    Thread * _pThread = GetThread();
+    Thread::ObjectRefFlush(_pThread);
+  }
+  FCallCheck __fCallCheck("c:\\dev\\github\\coreclr\\src\\classlibnative\\bcltype\\system.cpp", 248);
   FCallTransitionState ___FCallTransitionState;
   if (1) {
-    PermitHelperMethodFrameState ___PermitHelperMethodFrameState;; {; {}; {}; {};
+    PermitHelperMethodFrameState ___PermitHelperMethodFrameState;;;::SetLastError(__lastError); {; {}; {}; {};
 
       PTRARRAYREF strArray = 0;
 
       static_assert(sizeof(strArray) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");
       static_assert(sizeof(strArray) == sizeof(OBJECTREF), "GC protecting structs of multiple OBJECTREFs requires a PROTECT variant of the HELPER METHOD FRAME macro");
       int alwaysZero = 0;
       do {
+        static BOOL __haveCheckedRestoreState = 0;
         if (1) {
           PermitHelperMethodFrameState ___PermitHelperMethodFrameState;;
           PermitHelperMethodFrameState::CheckHelperMethodFramePermitted();
           CompletedFCallTransitionState ___CompletedFCallTransitionState;;
           FrameWithCookie < HelperMethodFrame_1OBJ > __helperframe(__me, Frame::FRAME_ATTR_NONE, (OBJECTREF * ) & strArray); {;
             LazyMachStateCaptureState(__helperframe.MachineState());
-          };; {;;
+          };
+          __helperframe.SetAddrOfHaveCheckedRestoreState( & __haveCheckedRestoreState); {;
+            FCallGCCanTrigger::Enter();
             __helperframe.Push();
             Thread * CURRENT_THREAD = __helperframe.GetThread();
             const bool CURRENT_THREAD_AVAILABLE = true;
-            (void) CURRENT_THREAD_AVAILABLE;;;; {
+            (void) CURRENT_THREAD_AVAILABLE;;
+            do {
+              if (CLRTestHookManager::Enabled()) CLRTestHookManager::TestHook() - > AppDomainCanBeUnloaded((CURRENT_THREAD_AVAILABLE ? CURRENT_THREAD : GetThread()) - > GetDomain() - > GetId().m_dwId, !1);
+            } while (0);; {
               Exception * __pUnCException = 0;
               Frame * __pUnCEntryFrame = ( & __helperframe);
               bool __fExceptionCatched = false;;
               if (true) try {; {;;;;
 
                   LPWSTR commandLine;
 
                   commandLine = GetCommandLineW();
                   if (commandLine == 0)
-                    RealCOMPlusThrowOM();
+                    if ((g_ExceptionFile = "c:\\dev\\github\\coreclr\\src\\classlibnative\\bcltype\\system.cpp", g_ExceptionLine = 260, 1) && 0) {} else RealCOMPlusThrowOM();
 
                   DWORD numArgs = 0;
                   LPWSTR * argv = SegmentCommandLine(commandLine, & numArgs);
                   if (!argv)
-                    RealCOMPlusThrowOM();
-
-                  ((void) 0);
+                    if ((g_ExceptionFile = "c:\\dev\\github\\coreclr\\src\\classlibnative\\bcltype\\system.cpp", g_ExceptionLine = 265, 1) && 0) {} else RealCOMPlusThrowOM();
+
+                  do {
+                    if (!(numArgs > 0)) {
+                      DbgAssertDialog("c:\\dev\\github\\coreclr\\src\\classlibnative\\bcltype\\system.cpp", 267, "numArgs > 0");
+                    }
+                  } while (0);
 
                   strArray = (PTRARRAYREF) AllocateObjectArray(numArgs, g_pStringClass);
                   // Copy each argument into new Strings.
                   for (unsigned int i = 0; i < numArgs; i++) {
                     STRINGREF str = StringObject::NewString(argv[i]);
                     STRINGREF * destData = ((STRINGREF * )(strArray - > GetDataPtr())) + i;
@@ -49,33 +66,49 @@
                   }
                   delete[] argv;
 
                   ;
                 };
               } catch (Exception * __pException) {;
-                do {} while (0);
+                do {
+                  CHECK _check;
+                  if (_check.EnterAssert()) {
+                    DebugOnlyCodeHolder __debugOnlyCodeHolder;
+                    __debugOnlyCodeHolder.Enter();;
+                    if (_check.Fail(0 != __pException)) {
+                      _check.Setup("", "0 != __pException", "c:\\dev\\github\\coreclr\\src\\classlibnative\\bcltype\\system.cpp", 279);
+                      _check.Trigger("Consistency check failed");
+                    }
+                    __debugOnlyCodeHolder.Leave();;
+                    _check.LeaveAssert();
+                  }
+                } while (0);
                 __pUnCException = __pException;
                 UnwindAndContinueRethrowHelperInsideCatch(__pUnCEntryFrame, __pUnCException);
                 __fExceptionCatched = true;;
               }
               if (__fExceptionCatched) {;
                 UnwindAndContinueRethrowHelperAfterCatch(__pUnCEntryFrame, __pUnCException);
               }
-            };;;;
+            };;;
+            do {
+              if (CLRTestHookManager::Enabled()) CLRTestHookManager::TestHook() - > AppDomainCanBeUnloaded((CURRENT_THREAD_AVAILABLE ? CURRENT_THREAD : GetThread()) - > GetDomain() - > GetId().m_dwId, !0);
+            } while (0);
             __helperframe.Pop();
-          };;
-          alwaysZero = HelperMethodFrameRestoreState(__helperframe.MachineState());
+          };
+          FCallGCCanTrigger::Leave(__FUNCTION__, "c:\\dev\\github\\coreclr\\src\\classlibnative\\bcltype\\system.cpp", 279);
+          alwaysZero = HelperMethodFrameRestoreState( & __helperframe, __helperframe.MachineState());
         } else {
           jmp_buf ___jmpbuf;
           _setjmp(___jmpbuf);
           __assume(0);
         }
       } while (alwaysZero);;;
 
-      return ((PTR_Object)(strArray));
+      return ((strArray).operator - > ());
     }
   } else {
     jmp_buf ___jmpbuf;
     _setjmp(___jmpbuf);
     __assume(0);
   };;
 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment