clang optimization bug? - optimization

I've been trying to track down what seems like a bug in clang, and I think I've got a reasonably minimal reproduction of it. Here's my program:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define x_Is_Digit(x) isdigit((unsigned char) (x))
void Odd_Behavior(char * version)
{
char * ptr, *tmp;
for (ptr = version; x_Is_Digit(*ptr); ptr++);
ptr++;
for (tmp = ptr; x_Is_Digit(*ptr); ptr++);
if (ptr == tmp)
printf("%08x == %08x! Really?\n", ptr, tmp);
}
int main()
{
char buffer[100];
strcpy(buffer, "3.8a");
Odd_Behavior(buffer);
return(0);
}
When I compile it with optimization, in the clang included with the Xcode download ("Apple clang 2.1"):
clang++ -Os optimizebug.cpp
And run it, it reports:
6b6f2be3 == 6b6f2be2! Really?
This strikes me as a tad odd, to say the least. If I remove the (unsigned char) cast in x_Is_Digit, it works properly.
Have I run into a bug in clang? Or am I doing something here that's causing some sort of undefined behavior? If I compile it with -O0, I don't get the problem.

Certainly looks like a bug to me. Clang mainline doesn't display this (at least on darwin/x86-64). Please file a bug at llvm.org/bugs with full details on how to reproduce this. Stack overflow isn't a great place to report compiler bugs :)

Definitively a bug. If the two pointers are equal at the if statement, they must also be equal in the printf statement.

Related

typedef an sc_fixed but got template error

Trying to define a simple sc_fixed type in Visual Studio 2017:
#include <systemc.h>
#include <sysc/datatypes/fx/sc_fixed.h> # just in case
....
typedef sc_fixed<16, 4> fixed_type;
....
This typedef line resulted an error:
E0864: sc_fixed is not a template
Had no idea why this error popped, even included sysc/datatypes/fx/sc_fixed.h. Why does it say "not a template"?
From INSTALL file shipped with SystemC
SystemC 2.3 includes a fixed-point package that is always built.
When compiling your applications with fixed-point types, you still have
to use compiler flag -DSC_INCLUDE_FX. Note that compile times increase
significantly when using this compiler flag.
Using your code as an example
#include <systemc.h>
#include <sysc/datatypes/fx/sc_fixed.h>
#include <iostream>
typedef sc_dt::sc_fixed<16, 4> fixed_type;
int sc_main(int, char**){
fixed_type ft(1.25);
std::cout << "ft = " << ft << '\n';
return 0;
}
Gives me the output
ft = 1.25
As a caveat it is best to check the docs for your particular version of SystemC as I believe this #define may have been removed in 2.3.3 (I am using 2.3.2) I am not certain.
I got the same type of error message in one of my C++ programs - E0864 - unique_ptr is not a template
I managed to get rid of this error in my program by adding this to my header file.
#include <memory>
My program managed to compile okay using this. I am using Visual Studio 2019 if that helps.

MPFR: get sin of mpf_class

I installed gmp and mpfr on my environment. Now I can successfully
#include <gmpxx.h>
#include <mpfr.h>
#include <mpf2mpfr.h>
Now, say that I initialize an mpf_class with some value:
mpf_class x = 0.73;
How can I use mpfr to get the sin of this number? I just need an mpf_class in, an mpf_class out. Something like:
mpf_class y = sin(x)
Which obviously doesn't work. I noticed that there is a mpfr_sin function, which I called like this:
mpfr_sin(x, y, MPFR_RNDN);
But that didn't work as well. So what am I supposed to do? Am I doing something wrong?
Thank you
mpf2mpfr.h is probably not what you want. It contains plenty of #define to replace mpf names with mpfr names in everything that follows. If you wanted a chance of having it work in your case, you would have to include mpf2mpfr.h before gmpxx.h. However, the file does not translate everything. The following lets it compile in C++03 (as long as you don't convert to mpq_class):
#include <mpfr.h>
#include <mpf2mpfr.h>
void mpfr_get_q (mpq_ptr, mpfr_srcptr);
#undef mpq_set_f
#define mpq_set_f(x,y) mpfr_get_q(x,y)
#include <gmpxx.h>
int main(){
mpf_class x=.73;
mpf_class y;
mpfr_sin(y.get_mpf_t(),x.get_mpf_t(),MPFR_RNDN);
}
but trying to print with operator<< will print a pointer instead of the number, for instance. Extra functions provided in C++11 would require more tweaking, it is easier to disable them: #define __GMPXX_USE_CXX11 0 before including gmpxx.h.
There are mostly two ways to solve this and both start with removing mpf2mpfr.h. The first one is to create a temporary mpfr_t:
mpf_class x=.73;
mpf_class y;
mpfr_t xx;
mpfr_t yy;
mpfr_init_set_f(xx, x.get_mpf_t(), MPFR_RNDN);
mpfr_init(yy);
mpfr_sin(yy, xx, MPFR_RNDN);
mpfr_get_f(y.get_mpf_t(), yy, MPFR_RNDN);
The second one is to drop mpf completely and use only mpfr. Its webpage lists 6 C++ wrappers for it, several of which are still maintained.

How to use strlcpy in g++'s -std=c++0x mode?

I would like to use strlcpy (to call an external api), which is missing from string.h, when I use g++'s -std=c++0x parameter.
% g++ -std=c++0x foo.cpp
foo.cpp: In function 'int main(int, char**)':
foo.cpp:5:11: error: 'strlcpy' was not declared in this scope
% g++ foo.cpp
% cat foo.cpp
#include <string.h>
int main(int argc, char* argv[])
{
const char src[] = "foo";
char dest[1024] = { 0 };
strlcpy(dest, src, sizeof(dest));
return 0;
}
Is it possible to use strlcpy and the std=c++0x flag, or do I have to drop the later?
Additionally I was not able to find the strlcpy manpage in cygwin, even though they seem to have the function. Any pointers?
I use gcc 4.7.2 on cygwin.
Quoting wikipedia: Criticism on strlcpy
The more popular strlcat and strlcpy have been criticised on the basis
that they encourage use of C strings and thus create more problems
than they solve. Consequently they have not been included in the
GNU C library (used by software on Linux), although they are
implemented in OpenBSD, FreeBSD, NetBSD, Solaris, Mac OS X, QNX, and
even internally in the Linux kernel.

Problem wit MDAC when trying to compile in VS2008 using x64 bit target platform

I am trying to compile an 32 bit application. I am aware of problems with it but that is why its being compiled on 64 bit version.
I am hanging at this problem.
Application uses lots of sql stuff.
In sqltypes.h file: (provided by MDAC)
#ifdef _WIN64
typedef INT64 SQLLEN;
typedef UINT64 SQLULEN;
typedef UINT64 SQLSETPOSIROW;
#else
#define SQLLEN SQLINTEGER
#define SQLULEN SQLUINTEGER
#define SQLSETPOSIROW SQLUSMALLINT
#endif
//For Backward compatibility
#ifdef WIN32
typedef SQLULEN SQLROWCOUNT;
typedef SQLULEN SQLROWSETSIZE;
typedef SQLULEN SQLTRANSID;
typedef SQLLEN SQLROWOFFSET;
#endif
For some reason when its compiled on 32 bit platform it works great
But when I try building it on 64 it goes berserk.
Error 61 error C2146: syntax error : missing ';' before identifier 'SQLLEN' ..\external\microsoft sdk\include\sqltypes.h 50
It does not recognize INT64, UINT64.
Is there something I need to enable so it will work under 64 build process?
Missing some #include or #define?
Any help would be great
Thanks
It turned out the problem was that somehow BaseTsd.h was not included (can't believe this is possible)
but as short fix I just included BaseTsd.h manually... i will comment on this answer if i find better solution
Use
#include <windows.h>
It contains BaseTsd.h and other relevant Windows-specific definitions.

MinGW and "declaration does not declare anything"

I'm working on converting a Linux project of mine to compile on Windows using MinGW. It compiles and runs just fine on Linux, but when I attempt to compile it with MinGW it bombs out with the following error message:
camera.h:11: error: declaration does not declare anything
camera.h:12: error: declaration does not declare anything
I'm kind of baffled why this is happening, because
I'm using the same version of g++ (4.4) on both Linux and Windows (via MinGW).
The contents of camera.h is absurdly simple.
Here's the code. It's choking on lines 11 and 12 where float near; and float far; are defined.
#include "Vector.h"
#ifndef _CAMERA_H_
#define _CAMERA_H_
class Camera{
public:
Vector eye;
Vector lookAt;
float fov;
float near;
float far;
};
#endif
Thanks for your help.
EDIT: Thanks both Dirk and mingos, that was exactly the problem!
Edit If you happen to include windef.h (either directly or indirectly), you will find
#define FAR
#define far
#define NEAR
#define near
there. I think, that this is the culprit.
Try
#undef near
#undef far
before your class definition.
Try giving them different names, like
float my_near;
float my_far;
I recall Borland using "near" and "far" as keywords (my 1992 Turbo C had these, back in MS-DOS era). Dunno if this is the case with gcc, but you can always try that.
In <windef.h>, you'll find on the following lines:
#define NEAR
#define near
Simple answer: you can't #undef them because they're a part of the Windows headers (_WINDEF_H will still be defined even if you #undef those definitions, so it won't be re-included if you try to #include <windef.h> again, not to mention the fact that if you #undef _WINDEF_H before using #include <windef.h> after your class definition, you'll end up with duplicate definitions for things like RECT, LONG, PROC and more), so the only other solution is to change your variable names.