fix permissive C++ code (MSVC /permissive-) (dotnet/coreclr#8337)
authorPhil Christensen <philc@microsoft.com>
Thu, 1 Dec 2016 11:53:23 +0000 (03:53 -0800)
committerJan Vorlicek <janvorli@microsoft.com>
Thu, 1 Dec 2016 11:53:23 +0000 (12:53 +0100)
commit78d9afe8606a242caa0df9927763c7608c592782
treea02394b3d44fc59c7d3f601067a5bf412f0371c3
parent6426b43f93e17325306f1ef45b11531acf333650
fix permissive C++ code (MSVC /permissive-) (dotnet/coreclr#8337)

* fix permissive C++ code (MSVC /permissive-)

These were found by the C++ compiler group when doing "Real world code"
build tests using /permissive-.  We are sharing these with you to help you clean up
your code before the new version of the compiler comes out.  For more information on /permissive-
see https://blogs.msdn.microsoft.com/vcblog/2016/11/16/permissive-switch/.

----------------------------
Under /permissive-, skipping the initialization of a variable is not allowed.
As an extension the compiler allowed this when there was no destructor for the type.

    void func(bool b)
    {
        if(b) goto END;

        int value = 0; //error C2362: initialization of 'value' is skipped by 'goto END'
     int array[10]; //Okay, not initialized.
        //... value used here

    END:
        return;
    }

Fix 1) Limit the scope of value:

    {
      int value = 0;
      //... value used here
    }
    END:

Fix 2) Initialize/declare value before the 'goto'

    int value = 0;
    if(b) goto END;
    //... value used here
    END:

Fix 3) Don't initialize value in the variable declaration.

    int value;
    value = 0
    //... value used here
    END:

-------------------
Alternative token representations.
The following are reserved as alternative representations for operators:
  and and_eq bitand bitor compl not not_eq or or_eq xor xor_eq

    //Can't use reserved names for variables:
    static int and = 0; // Change name (possibly to 'and_')

    void func()
    {
        _asm {
            xor     edx,edx // xor is reserved, change to uppercase XOR
            or      eax,eax // or is reserved, change to uppercase OR
        }
    }

* Apply formatting patch.

* fixes from code review.

I addressed @janvorli requests from the pull request code review.

Commit migrated from https://github.com/dotnet/coreclr/commit/20275aa647c5733bc5b1929cba3fd1094c67fb1d
18 files changed:
src/coreclr/src/ToolBox/SOS/Strike/strike.cpp
src/coreclr/src/ToolBox/SOS/Strike/util.cpp
src/coreclr/src/debug/ee/controller.cpp
src/coreclr/src/dlls/mscorpe/ceefilegenwriter.cpp
src/coreclr/src/jit/codegenlegacy.cpp
src/coreclr/src/md/ceefilegen/cceegen.cpp
src/coreclr/src/md/winmd/adapter.cpp
src/coreclr/src/md/winmd/winmdimport.cpp
src/coreclr/src/utilcode/regutil.cpp
src/coreclr/src/utilcode/securitywrapper.cpp
src/coreclr/src/vm/ceemain.cpp
src/coreclr/src/vm/clrprivbinderwinrt.cpp
src/coreclr/src/vm/encee.cpp
src/coreclr/src/vm/excep.cpp
src/coreclr/src/vm/i386/cgenx86.cpp
src/coreclr/src/vm/mlinfo.cpp
src/coreclr/src/vm/util.cpp
src/coreclr/src/vm/win32threadpool.cpp