[PATCH] D99646: [clang-tidy] misc-std-stream-objects-outside-main: a new check

Nathan James via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 31 08:58:06 PDT 2021


njames93 added a comment.

Is it not wise to also check the c standard library.
So check for function refs to these names in the global or std namespace.
`printf`, `vprintf`, `puts`, `putchar`, `scanf`, `scanf`, `getchar` and `gets`
It may be a bit of a pain checking for usages of `stdin` and  `stdout` due to them being defined as macros.

If you make this change the name of the check would likely need updating as they aren't stream objects.

Going on a tangent here, but on POSIX platforms you can redirect `stdin`, `stdout`, and `stderr` using `dup` and `dup2` (Windows is `_dup` and `_dup2` respectively), gtest uses this functionality.



================
Comment at: clang-tools-extra/clang-tidy/misc/StdStreamObjectsOutsideMainCheck.cpp:42
+
+bool StdStreamObjectsOutsideMainCheck::isInsideMainFunction(
+    const MatchFinder::MatchResult &Result, const DynTypedNode &Node) {
----------------
This all seems unnecessary, you can add `unless(forFunction(isMain()))` to your declRefExpr matcher instead.


================
Comment at: clang-tools-extra/test/clang-tidy/checkers/misc-std-stream-objects-outside-main.cpp:1
+// RUN: %check_clang_tidy -std=c++14-or-later %s misc-std-stream-objects-outside-main %t
+
----------------
Why won't this work in c++11 mode?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D99646



More information about the cfe-commits mailing list