public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Keith Thompson <keithsthompson@gmail.com>
To: cygwin@cygwin.com
Cc: Keith Thompson <Keith.S.Thompson@gmail.com>
Subject: clang++: M_PI is visible in conforming mode
Date: Mon, 07 Jan 2019 01:19:00 -0000	[thread overview]
Message-ID: <CAAHpriOFzks092zT+chJw4Dd91rPsO0mR3wO1gue8btARmBHKw@mail.gmail.com> (raw)

The macro M_PI, which expands to a constant approximating the value
of pi, is defined by POSIX, but not by ISO C or ISO C++.  It is
not a reserved identifier, so it should be available for use as a
user-defined identifier.

The problem: Including <cmath> causes M_PI to be defined, even when
the C++ compiler is invoked in what should be a conforming mode.

This problem occurs with clang++.  It does not occur with g++.

EXPECTED: Program compiles without error and prints "3".
OBSERVED: Program is rejected at compile time.

(The program is rejected when g++ or clang++ is invoked without
options.  That's not a bug.)

Using <math.h> rather than <cmath> doesn't change the symptom.

A similar program in C does not exhibit the problem.

I *think* the problem is in the "math.h" header, which should arrange
for M_PI and similar macros not to be defined in conforming mode.
It's also possible that a fix might involve updates to clang++.
I haven't fully investigated the twisty maze of macro definitions
and nested #includes in math.h.

(I acknowledge that writing your own definition of M_PI is not a
good idea, and that the definition in this program is particularly
poor style.)

I'm using 64-bit Cygwin on Windows 10, with all the latest updates.

See also
    https://bugs.launchpad.net/ubuntu/+source/glibc/+bug/1810695
a report for a similar bug on Ubuntu (affecting both g++ and clang++).

Output illustrating the problem follows:

$ uname -a
CYGWIN_NT-10.0 eddie 2.11.2(0.329/5/3) 2018-11-08 14:34 x86_64 Cygwin

$ clang++ --version
clang version 5.0.1 (tags/RELEASE_501/final)
Target: x86_64-unknown-windows-cygnus
Thread model: posix
InstalledDir: /usr/bin

$ cat M_PI_bug.cpp
#include <iostream>
#include <cmath>
int main() {
    const int M_PI = 22/7;
    std::cout << M_PI << '\n';
}

// This is not reasonable code.
// The issue is that a conforming C++ compiler must accept it.

// Expected output: 3

$ g++ -std=c++17 -pedantic-errors M_PI_bug.cpp && ./M_PI_BUG
3

$ clang++ -std=c++17 -pedantic-errors M_PI_bug.cpp && ./M_PI_BUG
M_PI_bug.cpp:4:15: error: expected unqualified-id
    const int M_PI = 22/7;
              ^
/usr/include/math.h:617:15: note: expanded from macro 'M_PI'
#define M_PI            3.14159265358979323846
                        ^
1 error generated.

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

                 reply	other threads:[~2019-01-07  1:19 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAAHpriOFzks092zT+chJw4Dd91rPsO0mR3wO1gue8btARmBHKw@mail.gmail.com \
    --to=keithsthompson@gmail.com \
    --cc=Keith.S.Thompson@gmail.com \
    --cc=cygwin@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).