[cfe-dev] no const string consolidation in -O0?

Yingshen Yu yingshen.yu at gmail.com
Tue Mar 6 02:26:29 PST 2012


Hi,

Just spent about an hour to narrow down this.

For the program below. clang 3.1 returns "different" unless you optimize it
with -O2 or higher.  In gcc 4.2 and VC,  the code always return "same" no
matter in -O0 or -O2.

I guess this means clang doesn't do const string consolidation by
default. Is there any switch to make clang do this consolidation in -O0?

Or can Clang dev team can make this consolidate as default?
I hope this make sense. since it's kind of 'de facto' in other compilers,
some code was written with this assumption, they know all the keys are from
const strings so they do pointer compare instead string compare. There're
might be more code relies on this behavior.

#include <stdio.h>
#define LAYER_KEY L"Layer"
struct TEST {
  const wchar_t* key;
}gTESTINFO = {
    LAYER_KEY
};

int main() {
    const wchar_t* key = LAYER_KEY;

    if (gTESTINFO.key == key)
        printf("same\n");
    else
        printf("different\n");
    return 0;
}

Thanks!

-- 
Yingshen(Jonny) Yu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20120306/afcc31ac/attachment.html>


More information about the cfe-dev mailing list