This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Add a check for blocking types and functions.
AbandonedPublic

Authored by segoon on Dec 30 2020, 3:44 AM.

Details

Summary

The check searches for:

  • C++ synchronization primitives
  • C11 synchronization primitives
  • POSIX synchronization primitives
  • some POSIX blocking functions
  • some blocking Linux syscalls
  • some Boost.Thread synchronization primitives

The preemptive functions/types can be separated into the following categories:

  • explicit sleep(3)-like functions
  • sleeping/waiting synchronization primitives

There are AST matchers for both sync primitives creation and blocking methods calls - we want
the former for user-created mutexes and the latter for mutex usage passed from outside (e.g. via
library global variable or a function parameter).

A simplified version of the check is used in Yandex.Taxi for coroutine code.

The check doesn't include the following:

  • Io and filesystem operations. It will be included in a separate checker concurrency-async-fs as a user might have a different policy (e.g. no FS thread pool, so nothing can be improved).
  • Creation of new threads. Same here, will be implemented in concurrency-async-no-new-threads, it is OK for some projects and must be changed to async code in others.
  • Check for atomic::is_always_lock_free - will be implemented in future patches.

Diff Detail

Event Timeline

segoon created this revision.Dec 30 2020, 3:44 AM
segoon requested review of this revision.Dec 30 2020, 3:44 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 30 2020, 3:44 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
segoon updated this revision to Diff 314093.Dec 30 2020, 4:01 AM
segoon retitled this revision from Add a check for blocking types and functions. to [clang-tidy] Add a check for blocking types and functions..

fix the mess

Few stylistic nits, Also Theres lots of cases where single stmt if statements have braces, typically we elide those braces.
Is it worth flagging methods with Thread safety analysis attributes.
These are only used in clang, but if a project annotates their methods with these, it would be nice to autodetect these attributes, though I can see this being a big job and likely better for a follow up.

clang-tools-extra/clang-tidy/concurrency/AsyncBlockingCheck.cpp
17–22

These go against llvm naming convention of Variables being CamelCase.
Also don't use auto when the type is spelled in the initialization.

24

clang::StringRef is a code smell imo, use llvm::StringRef or move this code into the clang namespace and drop the qualifier.

25

This could take an ArrayRef instead of const vector ref.

34

This and all examples below don't belong on the heap, Just use an array.

36

Is it wise to fully qualify these?
::std::mutex

Also whats with the comments at the end of each line, they don't seem to add anything.

223

No need to case to StringRef, its implicit.

293–294
300–301
303–305

Diagnostics support "%0" style formatting.
That formatting handles NamedDecls.
Stmt has a SourceRange that does the same job as (getBeginLoc(), getEndLoc()]
This can be applied in other places.

309–310
321

From the code, Lockfree can't be null if Atomic binds, maybe make this an assert

clang-tools-extra/test/clang-tidy/checkers/concurrency-async-blocking.cpp
451

Whats this for?

Eugene.Zelenko edited reviewers, added: alexfh, aaron.ballman, hokein; removed: jfb.Dec 30 2020, 7:40 AM
Eugene.Zelenko set the repository for this revision to rG LLVM Github Monorepo.
Eugene.Zelenko added a project: Restricted Project.
Eugene.Zelenko added inline comments.
clang-tools-extra/clang-tidy/concurrency/AsyncBlockingCheck.cpp
2

Please make length same as end of comment.

clang-tools-extra/clang-tidy/concurrency/AsyncBlockingCheck.h
28

Please remove empty line between methods. See other checks as example. Same below.

clang-tools-extra/docs/clang-tidy/checks/concurrency-async-blocking.rst
7

Please distribute words more evenly up to 80 characters.

14

See other documentation for example of lists. Same below.

29

Will be good idea to highlight BasicLockable and other with single back-ticks.

31

Please use double beck-ticks for language constructs. Same below.

69

Please highlight std::atomic with double back-ticks.

70

Please highlight std::mutex with double back-ticks.

75

Please use double beck-ticks for language constructs. Same below.

segoon updated this revision to Diff 314136.Dec 30 2020, 11:36 AM
segoon marked 19 inline comments as done.

review fixes

segoon added inline comments.Dec 30 2020, 11:38 AM
clang-tools-extra/clang-tidy/concurrency/AsyncBlockingCheck.cpp
36

the comments signal clang-format not to join multiple items into a single line

clang-tools-extra/test/clang-tidy/checkers/concurrency-async-blocking.cpp
451

WIP tests for not yet ready atomic::is_always_lock_free check

clang-tools-extra/docs/clang-tidy/checks/concurrency-async-blocking.rst
8

Please fix double space.

Eugene.Zelenko added inline comments.Dec 30 2020, 4:41 PM
clang-tools-extra/clang-tidy/concurrency/AsyncBlockingCheck.cpp
320

Please don't use auto when type is not explicitly spelled in same statement or iterator.

segoon updated this revision to Diff 314631.Jan 5 2021, 8:41 AM
segoon edited the summary of this revision. (Show Details)
  • review fixes
  • drop of atomic::is_always_lock_free check
segoon edited the summary of this revision. (Show Details)Jan 5 2021, 10:15 AM

friendly ping, any comments on the patch?

Eugene.Zelenko, njames93, any comments on the patch?

Eugene.Zelenko, njames93, any comments on the patch?

Sorry, you need to wait for approval from one of maintainer mentioned as reviewers. I check only documentation and for minor code issues.

alexfh, hi! any comments on the patch?

segoon updated this revision to Diff 322685.Feb 10 2021, 7:39 AM
  • explicitly state in docs that the check must be used only for async code

alexfh, aaron.ballman, hi! Any comments on the patch?