[PATCH] D99646: [clang-tidy] misc-avoid-std-io-outside-main: a new check

Marco Gartmann via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 7 02:36:10 PDT 2021


mgartmann updated this revision to Diff 335750.
mgartmann retitled this revision from "[clang-tidy] misc-std-stream-objects-outside-main: a new check" to "[clang-tidy] misc-avoid-std-io-outside-main: a new check".
mgartmann added a comment.

- Added two new matchers to flag uses of `stdio.h`/`csdtio` functions outside of main.
- Renamed the check to fit those new functionalities.

**Rationale:**

When analyzing the AST dump of a program using `stdio.h`'s `printf()` e.g., I found that those functions are not declared directly under `translationUnit`, but under `translationUnitDecl/linkageSpecDecl`. A `hasDeclContext(linkageSpecDecl())` matcher worked with both `stdio.h` and `cstdio` functions. 
In the check's test, I tried to include a own header file from the //Inputs// directory. However, the functions declared in this header file were then direct AST children of `translationUnitDecl` and not of `linkageSpecDecl`. 
Due to the fact that I was not able to reproduce the original AST in the tests, I decided to omit this part of the matcher.

Furthermore, when using `cstdio`'s `std::printf()` e.g., these functions are in fact //UsingShadowDecl//s. Despite them being declared in the `std` namespace, a call of `std::printf("...")` does not match with a `isInStdNamespace()` matcher.
Thus, this part of the matcher was omitted as well.

The resulting matcher matches any reference to a function which is called "printf", "vprintf", "puts", "putchar", "scanf", "getchar" or "gets".

@njames93: I would be interested to hear your opinion on this newest update. If I overlooked something, I would be more than happy to incorporate any further feedback.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D99646/new/

https://reviews.llvm.org/D99646

Files:
  clang-tools-extra/clang-tidy/misc/AvoidStdIoOutsideMainCheck.cpp
  clang-tools-extra/clang-tidy/misc/AvoidStdIoOutsideMainCheck.h
  clang-tools-extra/clang-tidy/misc/CMakeLists.txt
  clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp
  clang-tools-extra/docs/ReleaseNotes.rst
  clang-tools-extra/docs/clang-tidy/checks/list.rst
  clang-tools-extra/docs/clang-tidy/checks/misc-avoid-std-io-outside-main.rst
  clang-tools-extra/test/clang-tidy/checkers/misc-avoid-std-io-outside-main.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D99646.335750.patch
Type: text/x-patch
Size: 16447 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210407/a6216a9e/attachment-0001.bin>


More information about the cfe-commits mailing list