Bug 24522 - [4.0 Regression] htonl in optimized template function generates compiler warning
Summary: [4.0 Regression] htonl in optimized template function generates compiler warning
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.0.2
: P2 minor
Target Milestone: 4.1.0
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2005-10-25 16:24 UTC by Charles Killian
Modified: 2007-02-03 15:48 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.1.0 3.4.0 3.4.5
Known to fail: 4.0.3
Last reconfirmed: 2006-01-24 04:29:35


Attachments
The test.cc file (119 bytes, text/plain)
2005-10-25 16:26 UTC, Charles Killian
Details
preprocessor output (5.14 KB, text/plain)
2005-10-25 16:26 UTC, Charles Killian
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Charles Killian 2005-10-25 16:24:41 UTC
Overview Description: More detailed expansion of summary.

        When compiling template functions in g++ 4.0.2 (Debian 4.0.2-2) (Debian testing/unstable) with -O2 and -Wall, the compiler generates a warning about a statement with no effect.  This seems to be due to the inlining with __v; as a statement, but the warning does not occur in non-template functions or with other compiler versions tried (3.4).

    Steps to Reproduce: Minimized, easy-to-follow steps that will trigger the bug. Include any special setup steps.

        1) Create the following .cc file (as test.cc):
#include <netinet/in.h>

template<typename S>
void serialize(const uint32_t* pitem, const S& item) {
  uint32_t t2 = htonl(item);
}


        2) Compile "test.cc"
$ g++ -O2 -Wall -c -o test.o test.cc

    Actual Results: What the application did after performing the above steps.

        g++ generated a warning that the htonl statement has no effect.
$ g++ -O2 -Wall -c -o test.o test.cc
test.cc: In function 'void serialize(const uint32_t*, const S&)':
test.cc:5: warning: statement has no effect

    Expected Results: What the application should have done, were the bug not present.

        test.o should have been created without warning

    Build Date & Platform: Date and platform of the build that you first encountered the bug in.

>Release:       4.0.2 (Debian 4.0.2-2) (Debian testing/unstable)
configured with: ../src/configure -v --enable-languages=c,c++,java,f95,objc,ada,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib--without-included-gettext --enable-threads=posix --enable-nls --program-suffix=-4.0 --enable-__cxa_atexit --enable-libstdcxx-allocator=mt --enable-clocale=gnu--enable-libstdcxx-debug --enable-java-gc=boehm --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-4.0-1.4.2.0/jre --enable-mpfr --disable-werror --enable-checking=release i486-linux-gnu
>Environment:
System: Linux killian.ucsd.edu 2.6.12.20051019-ck1 #1 SMP Wed Oct 19 09:24:47 PDT 2005 i686 GNU/Linux
Architecture: i686

    Additional Builds and Platforms: Whether or not the bug takes place on other platforms (or browsers, if applicable).

        - Also Occurs On        
        Unknown

        - Doesn't Occur On        
        g++ version 3.4.4 20050721 (Red Hat 3.4.4-2)
        Or without -O2, -Wall, or outside of a template function.
Comment 1 Charles Killian 2005-10-25 16:26:03 UTC
Created attachment 10055 [details]
The test.cc file
Comment 2 Charles Killian 2005-10-25 16:26:23 UTC
Created attachment 10056 [details]
preprocessor output
Comment 3 Ian Lance Taylor 2005-10-25 16:36:49 UTC
I believe this winds up being a duplicate of PR c++/8057, which is fixed on mainline.  With the test case in the PR, I see the warning with 4.0, but I do not see the warning on mainline.

Note that this slightly modified test case:

#include <netinet/in.h>

template<typename S>
void serialize(const uint32_t* pitem, const S& item) {
  uint32_t t2 = htonl(item);
}

template void serialize<int> (const uint32_t*, const int&);

issues a warning on both 4.0 and mainline:

foo.cc: In function ‘void serialize(const uint32_t*, const S&) [with S = int]’:
foo.cc:8:   instantiated from here
foo.cc:5: warning: unused variable ‘t2’
Comment 4 Charles Killian 2005-10-25 17:09:49 UTC
(In reply to comment #3)
> I believe this winds up being a duplicate of PR c++/8057, which is fixed on
> mainline.  With the test case in the PR, I see the warning with 4.0, but I do
> not see the warning on mainline.
> 
> Note that this slightly modified test case:
> 
> #include <netinet/in.h>
> 
> template<typename S>
> void serialize(const uint32_t* pitem, const S& item) {
>   uint32_t t2 = htonl(item);
> }
> 
> template void serialize<int> (const uint32_t*, const int&);
> 
> issues a warning on both 4.0 and mainline:
> 
> foo.cc: In function ‘void serialize(const uint32_t*, const S&) [with S = int]’:
> foo.cc:8:   instantiated from here
> foo.cc:5: warning: unused variable ‘t2’
> 

The unused variable warning of course is behavior as expected for this small sample.  My original statement that the sample should compile without warning should seems not to be correct.  That could be corrected by using the variable, for example by printing it.  The motivating example appended the bytes of t2 to a std::string which was passed in by reference.
Comment 5 Andrew Pinski 2005-10-25 17:51:43 UTC
Here is a reduced testcase:
template <int >
void f(int i)
{
  int i1 = (__extension__ ({int i2 = i; i2;}));
}
Comment 6 Andrew Pinski 2005-10-25 17:59:02 UTC
(In reply to comment #3)
> I believe this winds up being a duplicate of PR c++/8057, which is fixed on
> mainline.  With the test case in the PR, I see the warning with 4.0, but I do
> not see the warning on mainline.

I don't think this is a duplicate of PR 8057 because my reduced testcase worked in 3.4.5.  I think this was introduced by one of the C++ patches between 4.0.1 and 4.0.2
>        Or without -O2, -Wall, or outside of a template function.
the without -O2, is because the headers change, which is why my simplified example shows the issue at every optimization level.
Comment 7 Gabriel Dos Reis 2007-02-03 15:48:55 UTC
Fixed in GCC-4.1.0 and higher