Even after I thought that this check was missing, I am now suddenly getting the output of clang-analyzer-alpha.unix.PthreadLock check from the clang-tidy 4.0 tool. Here is a toned down use case of my code which I am trying to modernize by using clang-tidy tool.
I have enabled all the checks using -checks=* argument.
#include <boost/thread.hpp>
#include <boost/thread/once.hpp>
void foo2()
{
boost:mutex mymutex;
boost::mutex::scoped_lock lock(mymutex);
int* x = NULL; // This is intentional. This triggers the clang-tidy checks. If I remove this lines, I wont get the clang-tidy warnings/errors/recommendations.
}
int main() {
foo2();
return 0;
}
When clang-tidy is ran over this code,it produces following warnings.
path/boost/include/boost/thread/pthread/mutex.hpp:149:23: warning: This lock has already been acquired [clang-analyzer-alpha.unix.PthreadLock]
int res = posix::pthread_mutex_lock(&m);
^
path/Source.cpp:40:5: note: Calling 'foo2'
foo2();
^
path/Source.cpp:32:31: note: Calling constructor for 'unique_lock'
boost::mutex::scoped_lock lock(getMutex());
^
path/boost/include/boost/thread/lock_types.hpp:157:7: note: Calling 'unique_lock::lock'
lock();
^
path/boost/include/boost/thread/lock_types.hpp:369:7: note: Taking false branch
if (m == 0)
^
path/boost/include/boost/thread/lock_types.hpp:374:7: note: Taking false branch
if (owns_lock())
^
path/boost/include/boost/thread/lock_types.hpp:379:7: note: Calling 'mutex::lock'
m->lock();
^
path/boost/thread/pthread/mutex.hpp:149:23: note: This lock has already been acquired
int res = posix::pthread_mutex_lock(&m);
^
path/Source.cpp:34:10: warning: unused variable 'x' [clang-diagnostic-unused-variable]
int* x = NULL;
^
path/Source.cpp:34:14: warning: use nullptr [modernize-use-nullptr]
int* x = NULL;
^
nullptr
Suppressed 33125 warnings (33125 in non-user code).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
Why does clang-tidy think that this lock has already been taken? Two functions are totally apart and the lock inside foo2 has no relation to lock in the boost headers. Is this an incorrect warning ? If not then what should I do about it?
Related
Compiling this program for mips with gcc (i.e., mips-linux-gnu-gcc):
__asm__
(
"f_asm:\n"
"jr $ra\n"
"nop\n"
);
static void f(void) __asm__("f_asm");
int main(void)
{
f();
return 0;
}
Yields this warning:
a.c:8:13: warning: 'f' used but never defined
static void f(void) __asm__("f_asm");
^
I'd like to get rid of this warning without making the symbol global. However, I haven't been able to find a -Wno- option that makes it go away. Any ideas?
(Note that changing f_asm to f does not improve the situation).
Here's a short example that reproduces this “no viable conversion” with lemon for clang but valid for g++ difference in compiler behavior.
#include <iostream>
struct A {
int i;
};
#ifndef UNSCREW_CLANG
using cast_type = const A;
#else
using cast_type = A;
#endif
struct B {
operator cast_type () const {
return A{i};
}
int i;
};
int main () {
A a{0};
B b{1};
#ifndef CLANG_WORKAROUND
a = b;
#else
a = b.operator cast_type ();
#endif
std::cout << a.i << std::endl;
return EXIT_SUCCESS;
}
live at godbolt's
g++ (4.9, 5.2) compiles that silently; whereas clang++ (3.5, 3.7) compiles it
if
using cast_type = A;
or
using cast_type = const A;
// [...]
a = b.operator cast_type ();
are used,
but not with the defaulted
using cast_type = const A;
// [...]
a = b;
In that case clang++ (3.5) blames a = b:
testling.c++:25:9: error: no viable conversion from 'B' to 'A'
a = b;
^
testling.c++:3:8: note: candidate constructor (the implicit copy constructor)
not viable:
no known conversion from 'B' to 'const A &' for 1st argument
struct A {
^
testling.c++:3:8: note: candidate constructor (the implicit move constructor)
not viable:
no known conversion from 'B' to 'A &&' for 1st argument
struct A {
^
testling.c++:14:5: note: candidate function
operator cast_type () const {
^
testling.c++:3:8: note: passing argument to parameter here
struct A {
With reference to the 2011¹ standard: Is clang++ right about rejecting the defaulted code or is g++ right about accepting it?
Nota bene: This is not a question about whether that const qualifier on the cast_type makes sense. This is about which compiler works standard-compliant and only about that.
¹ 2014 should not make a difference here.
EDIT:
Please refrain from re-tagging this with the generic c++ tag.
I'd first like to know which behavior complies to the 2011 standard, and keep the committees' dedication not to break existing (< 2011) code out of ansatz for now.
So it looks like this is covered by this clang bug report rvalue overload hides the const lvalue one? which has the following example:
struct A{};
struct B{operator const A()const;};
void f(A const&);
#ifdef ERR
void f(A&&);
#endif
int main(){
B a;
f(a);
}
which fails with the same error as the OP's code. Richard Smith toward the end says:
Update: we're correct to choose 'f(A&&)', but we're wrong to reject
the initialization of the parameter. Further reduced:
struct A {};
struct B { operator const A(); } b;
A &&a = b;
Here, [dcl.init.ref]p5 bullet 2 bullet 1 bullet 2 does not apply,
because [over.match.ref]p1 finds no candidate conversion functions,
because "A" is not reference-compatible with "const A". So we fall
into [dcl.init.ref]p5 bullet 2 bullet 2, and copy-initialize a
temporary of type A from 'b', and bind the reference to that. I'm not
sure where in that process we go wrong.
but then comes back with another comment due to a defect report 1604:
DR1604 changed the rules so that
A &&a = b;
is now ill-formed. So we're now correct to reject the initialization.
But this is still a terrible answer; I've prodded CWG again. We should
probably discard f(A&&) during overload resolution.
So it seems like clang is technically doing the right thing based on the standard language today but it may change since there seems to be disagreement at least from the clang team that this is the correct outcome. So presumably this will result in a defect report and we will have to wait till it is resolved before we can come to a final conclusion.
Update
Looks like defect report 2077 was filed based on this issue.
Why am I getting a warning about initialization in one case, but not the other? The code is in a C++ source file, and I am using GCC 4.7 with -std=c++11.
struct sigaction old_handler, new_handler;
The above doesn't produce any warnings with -Wall and -Wextra.
struct sigaction old_handler={}, new_handler={};
struct sigaction old_handler={0}, new_handler={0};
The above produces warnings:
warning: missing initializer for member ‘sigaction::__sigaction_handler’ [-Wmissing-field-initializers]
warning: missing initializer for member ‘sigaction::sa_mask’ [-Wmissing-field-initializers]
warning: missing initializer for member ‘sigaction::sa_flags’ [-Wmissing-field-initializers]
warning: missing initializer for member ‘sigaction::sa_restorer’ [-Wmissing-field-initializers]
I've read through How should I properly initialize a C struct from C++?, Why is the compiler throwing this warning: "missing initializer"? Isn't the structure initialized?, and bug reports like Bug 36750. Summary: -Wmissing-field-initializers relaxation request. I don't understand why the uninitialized struct is not generating a warning, while the initialized struct is generating a warning.
Why is the uninitialized structs not generating a warning; and why is the initialized structs generating a warning?
Here is a simple example:
#include <iostream>
struct S {
int a;
int b;
};
int main() {
S s { 1 }; // b will be automatically set to 0
// and that's probably(?) not what you want
std::cout<<"s.a = "<<s.a<<", s.b = "<<s.b<<std::endl;
}
It gives the warning:
missing.cpp: In function ‘int main()’:
missing.cpp:9:11: warning: missing initializer for member 'S::b' [-Wmissing-field-initializers]
The program prints:
s.a = 1, s.b = 0
The warning is just a reminder from the compiler that S has two members but you only explicitly initialized one of them, the other will be set to zero. If that's what you want, you can safely ignore that warning.
In such a simple example, it looks silly and annoying; if your struct has many members, then this warning can be helpful (catching bugs: miscounting the number of fields or typos).
Why is the uninitialized structs not generating a warning?
I guess it would simply generate too much warnings. After all, it is legal and it is only a bug if you use the uninitialized members. For example:
int main() {
S s;
std::cout<<"s.a = "<<s.a<<", s.b = "<<s.b<<std::endl;
}
missing.cpp: In function ‘int main()’:
missing.cpp:10:43: warning: ‘s.S::b’ is used uninitialized in this function [-Wuninitialized]
missing.cpp:10:26: warning: ‘s.S::a’ is used uninitialized in this function [-Wuninitialized]
Even though it did not warn me about the uninitialized members of s, it did warn me about using the uninitialized fields. All is fine.
Why is the initialized structs generating a warning?
It warns you only if you explicitly but partially initialize the fields. It is a reminder that the struct has more fields than you enumerated. In my opinion, it is questionable how useful this warning is: It can indeed generate too much false alarms. Well, it is not on by default for a reason...
That's a defective warning. You did initialize all the members, but you just didn't have the initializers for each member separately appear in the code.
Just ignore that warning if you know what you are doing. I regularly get such warnings too, and I'm upset regularly. But there's nothing I can do about it but to ignore it.
Why is the uninitialized struct not giving a warning? I don't know, but most probably that is because you didn't try to initialize anything. So GCC has no reason to believe that you made a mistake in doing the initialization.
You're solving the symptom but not the problem. Per my copy of "Advanced Programming in the UNIX Environment, Second Edition" in section 10.15:
Note that we must use sigemptyset() to initialize the sa_mask member of the structure. We're not guaranteed that act.sa_mask = 0 does the same thing.
So, yes, you can silence the warning, and no this isn't how you initialize a struct sigaction.
The compiler warns that all members are not initialized when you initialize the struct. There is nothing to warn about declaring an uninitialized struct. You should get the same warnings when you (partially) initialize the uninitialized structs.
struct sigaction old_handler, new_handler;
old_handler = {};
new_handler = {};
So, that's the difference. Your code that doesn't produce the warning is not an initialization at all. Why gcc warns about zero initialized struct at all is beyond me.
The only way to prevent that warning (or error, if you or your organization is treating warnings as errors (-Werror option)) is to memset() it to an init value. For example:
#include <stdio.h>
#include <memory.h>
typedef struct {
int a;
int b;
char c[12];
} testtype_t;
int main(int argc, char* argv[]) {
testtype_t type1;
memset(&type1, 0, sizeof(testtype_t));
printf("%d, %s, %d\n", argc, argv[0], type1.a);
return 0;
}
It is not very clean, however, it seems like that for GCC maintainers, there is only one way to initialize a struct and code beauty is not on top of their list.
I've been working on a C++ project using Boost, and between compiles, I must have upgraded something in boost without meaning to or something, because now Boost dependencies won't compile:
In file included from /usr/include/boost/thread/pthread/mutex.hpp:14:0,
from /usr/include/boost/thread/mutex.hpp:16,
from /usr/include/boost/thread/pthread/thread_data.hpp:12,
from /usr/include/boost/thread/thread.hpp:17,
from /usr/include/boost/thread.hpp:13,
from /blah.h:4,
from bluh.h:3,
from bleh/main.cpp:4:
/usr/include/boost/thread/xtime.hpp:23:5: error: expected identifier before numeric constant
/usr/include/boost/thread/xtime.hpp:23:5: error: expected '}' before numeric constant
/usr/include/boost/thread/xtime.hpp:23:5: error: expected unqualified-id before numeric constant
/usr/include/boost/thread/xtime.hpp:46:14: error: expected type-specifier before 'system_time'
In file included from /usr/include/boost/thread/pthread/mutex.hpp:14:0,
from /usr/include/boost/thread/mutex.hpp:16,
from /usr/include/boost/thread/pthread/thread_data.hpp:12,
from /usr/include/boost/thread/thread.hpp:17,
from /usr/include/boost/thread.hpp:13,
from /blah,
from /bleh,(changed these names, obviously)
from /bluh /main.cpp:4:
/usr/include/boost/thread/xtime.hpp: In function 'int xtime_get(xtime*, int)':
/usr/include/boost/thread/xtime.hpp:73:40: error: 'get_system_time' was not declared in this scope
/usr/include/boost/thread/xtime.hpp:73:40: note: suggested alternative:
/usr/include/boost/thread/thread_time.hpp:19:24: note: 'boost::get_system_time'
/usr/include/boost/thread/xtime.hpp: At global scope:
/usr/include/boost/thread/xtime.hpp:88:1: error: expected declaration before '}' token
make[2]: *** [CMakeFiles/edge_based_tracker.dir/main.o] Error 1
make[1]: *** [CMakeFiles/edge_based_tracker.dir/all] Error 2
make: *** [all] Error 2
Any ideas? I tried changing TIME_UTC to TIME_UTC_ as this was recommended to me on another site, but that didn't seem to help.
EDIT: The Boost Version is Version: 1.48.0.2. I've attached xtime below:
// Copyright (C) 2001-2003
// William E. Kempf
// Copyright (C) 2007-8 Anthony Williams
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_XTIME_WEK070601_HPP
#define BOOST_XTIME_WEK070601_HPP
#include <boost/thread/detail/config.hpp>
#include <boost/cstdint.hpp>
#include <boost/thread/thread_time.hpp>
#include <boost/date_time/posix_time/conversion.hpp>
#include <boost/config/abi_prefix.hpp>
namespace boost {
enum xtime_clock_types
{
TIME_UTC=1 //LINE 23
// TIME_TAI,
// TIME_MONOTONIC,
// TIME_PROCESS,
// TIME_THREAD,
// TIME_LOCAL,
// TIME_SYNC,
// TIME_RESOLUTION
};
struct xtime
{
#if defined(BOOST_NO_INT64_T)
typedef int_fast32_t xtime_sec_t; //INT_FAST32_MIN <= sec <= INT_FAST32_MAX
#else
typedef int_fast64_t xtime_sec_t; //INT_FAST64_MIN <= sec <= INT_FAST64_MAX
#endif
typedef int_fast32_t xtime_nsec_t; //0 <= xtime.nsec < NANOSECONDS_PER_SECOND
xtime_sec_t sec;
xtime_nsec_t nsec;
operator system_time() const
{
return boost::posix_time::from_time_t(0)+
boost::posix_time::seconds(static_cast<long>(sec))+
#ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
boost::posix_time::nanoseconds(nsec);
#else
boost::posix_time::microseconds((nsec+500)/1000);
#endif
}
};
inline xtime get_xtime(boost::system_time const& abs_time)
{
xtime res;
boost::posix_time::time_duration const time_since_epoch=abs_time-boost::posix_time::from_time_t(0);
res.sec=static_cast<xtime::xtime_sec_t>(time_since_epoch.total_seconds());
res.nsec=static_cast<xtime::xtime_nsec_t>(time_since_epoch.fractional_seconds()*(1000000000/time_since_epoch.ticks_per_second()));
return res;
}
inline int xtime_get(struct xtime* xtp, int clock_type)
{
if (clock_type == TIME_UTC)
{
*xtp=get_xtime(get_system_time());
return clock_type;
}
return 0;
}
inline int xtime_cmp(const xtime& xt1, const xtime& xt2)
{
if (xt1.sec == xt2.sec)
return (int)(xt1.nsec - xt2.nsec);
else
return (xt1.sec > xt2.sec) ? 1 : -1;
}
} // namespace boost
#include <boost/config/abi_suffix.hpp>
#endif //BOOST_XTIME_WEK070601_HPP
EDIT: To make it clear, the code is failing on an import of boost/thread.hpp
For those running into the same issue and struggling to find the solution here.
Derived from noodlebox' link (https://svn.boost.org/trac/boost/ticket/6940):
You can edit /usr/include/boost/thread/xtime.hpp (or whereever your xtime.hpp lies)
and change all occurrences of TIME_UTC to TIME_UTC_ - Probably around lines 23 and 71.
i.e. sed -i 's/TIME_UTC/TIME_UTC_/g' /usr/include/boost/thread/xtime.hpp
If you're getting syntax errors when using Boost, you may need to compile your code with -std=c++11.
The TIME_UTC issue you might have read about is a side effect of using c++11. TIME_UTC is now defined as a macro in c++11, so you will need to either replace all instances of TIME_UTC with TIME_UTC_, or just use a newer (1.50.0+) version of Boost where this has been fixed already.
See: https://svn.boost.org/trac/boost/ticket/6940
Since you do not show your code, we can only guess. My guess is that you define TIME_UTC macro somewhere in your code. This macro messes-up xtime.hpp header.
I'm aware of this SO question and this SO question. The element
of novelty in this one is in its focus on Xcode, and in its use of
square brackets to dereference a pointer to void.
The following program compiles with no warning in Xcode 4.5.2, compiles
with a warning on GCC 4.2 and, even though I don't have Visual Studio
right now, I remember that it would consider this a compiler
error, and MSDN and Internet agree.
#include <stdio.h>
int main(int argc, const char * argv[])
{
int x = 24;
void *xPtr = &x;
int *xPtr2 = (int *)&xPtr[1];
printf("%p %p\n", xPtr, xPtr2);
}
If I change the third line of the body of main to:
int *xPtr2 = (int *)(xPtr + 1);
It compiles with no warnings on both GCC and Xcode.
I would like to know how can I turn this silence into warnings or errors, on
GDB and especially Xcode/LLVM, including the fact that function main is int but
does not explicitly return any value (By the way I think -Wall does
the trick on GDB).
that isnt wrong at all...
the compiler doesnt know how big the pointer is ... a void[] ~~ void*
thats why char* used as strings need to be \0-terminated
you cannot turn on a warning for that as it isnt possible to determine a 'size of memory pointer to by a pointer' at compile time
void *v = nil;
*v[1] = 0 //invalid
void *v = malloc(sizeof(int)*2);
*v[1] = 0 //valid
*note typed inline on SO -- sorry for any non-working code