Skip to content

C++11 lacks Errors when thread safety attribute is applied to static members #31414

Open
@llvmbot

Description

@llvmbot
Bugzilla Link 32066
Version trunk
OS Linux
Reporter LLVM Bugzilla Contributor
CC @AaronBallman,@DougGregor,@pogo59

Extended Description

When compiling at C++11, no error is issued when thread safety attributes applied to static members.

I discovered this while updating the following Lit test to be C++11 compatible.
test/SemaCXX/warn-thread-safety-parsing.cpp

Here is a reduced test case b.cpp.

#define LOCKABLE            __attribute__ ((lockable))
#define EXCLUSIVE_LOCKS_REQUIRED(...)   __attribute__ ((exclusive_locks_required(__VA_ARGS__)))
#define GUARDED_BY(x)       __attribute__ ((guarded_by(x)))

class LOCKABLE Mutex { };

class FooStream;

class Foo {
public:
  Mutex mu;

  static void foo() EXCLUSIVE_LOCKS_REQUIRED(mu);
  static int si GUARDED_BY(mu);
  friend FooStream& operator<<(FooStream& s, const Foo& f) EXCLUSIVE_LOCKS_REQUIRED(mu);
};

Compiling the above test at C++98 gives three errors

$ clang -c -Wthread-safety b.cpp -std=c++98
b.cpp:13:46: error: invalid use of member 'mu' in static member function
  static void foo() EXCLUSIVE_LOCKS_REQUIRED(mu);
                                             ^~
b.cpp:2:82: note: expanded from macro 'EXCLUSIVE_LOCKS_REQUIRED'
#define EXCLUSIVE_LOCKS_REQUIRED(...)   __attribute__ ((exclusive_locks_required(__VA_ARGS__)))
                                                                                 ^~~~~~~~~~~
b.cpp:14:28: error: invalid use of non-static data member 'mu'
  static int si GUARDED_BY(mu);
                           ^~
b.cpp:3:56: note: expanded from macro 'GUARDED_BY'
#define GUARDED_BY(x)       __attribute__ ((guarded_by(x)))
                                                       ^
b.cpp:15:85: error: invalid use of non-static data member 'mu'
  friend FooStream& operator<<(FooStream& s, const Foo& f) EXCLUSIVE_LOCKS_REQUIRED(mu);
                                                                                    ^~
b.cpp:2:82: note: expanded from macro 'EXCLUSIVE_LOCKS_REQUIRED'
#define EXCLUSIVE_LOCKS_REQUIRED(...)   __attribute__ ((exclusive_locks_required(__VA_ARGS__)))
                                                                                 ^~~~~~~~~~~
3 errors generated.

Compiling the same test at C++11 gives no error.

$ /home/chli/Source/uclsvn/build/bin/clang -c -Wthread-safety b.cpp -std=c++11

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillac++11clang:frontendLanguage frontend issues, e.g. anything involving "Sema"

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions