[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
Thu Apr 1 07:16:32 PDT 2021


njames93 added a comment.

In D99646#2663733 <https://reviews.llvm.org/D99646#2663733>, @mgartmann wrote:

> Hi @njames93,
> I can see your point, I am going to add this functionality.
>
> However, I do not completely understand what you mean with //check for function refs to these names in the global or std namespace//.  
> Could you explain this a bit further?
>
> E.g., should all calls to these functions be flagged if they happen inside the `std` namespace or in the `global` namespace?
> And if they happen inside `my_namespace` e.g., they should not be flagged. Am I understanding this correctly?
> How should the check behave if the functions are called inside `main()`?

I mean any `DeclRefExpr` that reference those functions.
We don't actually want to only match on call expressions to those functions, we also want the other ways they can be called.

  auto Print = &puts;
  Print("This is using stdio");

I could imagine this is the kind of matcher expression you need.

  declRefExpr(
       hasDeclaration(functionDecl(
           anyOf(hasDeclContext(translationUnitDecl()), isInStdNamespace()),
           hasAnyName("printf", "vprintf", ...))),
       unless(forFunction(isMain())))

As for flagging the same rules should apply for references to `cin`, `cout` etc.

> Would it make more sense to put this functionality into a separat check?
>
> Thanks for your effort in advance.
> Looking forward to hearing from you soon.

You could include it in this check, then maybe rename this check to a more general name like `misc-avoid-stdio-outside-main`.



================
Comment at: clang-tools-extra/clang-tidy/misc/StdStreamObjectsOutsideMainCheck.cpp:22
+  Finder->addMatcher(
+      declRefExpr(to(namedDecl(hasAnyName("cin", "wcin", "cout", "wcout",
+                                          "cerr", "wcerr"),
----------------
should probably use varDecl here instead of namedDecl.


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