Problem Description
The iostream header file defines multiple global objects like std:cin, std::cout, etc.
The pitfall of using these global objects outside of the main function is that it negatively affects the code's testability. They cannot be replaced with a mocked input stream during testing. Therefore, std::cin, std::cout etc. should only be used inside the main function. If other functions need an input or an output stream, one is encouraged to use the std::istream and std::ostream interfaces and to receive the stream object files as function parameters.
Thus, during testing, mocked stream objects can be handed to the function.
What this Check Does
The goal of this check is to find any uses of predefined standard stream objects (i.e., cout, wcout, cerr, wcerr, cin, wcin) and to check whether they are used inside the main function or not. If any uses are found outside the main function, they get flagged.
The use of clog and wclog does not get flagged by this check. The rationale for this is that code with logging functionality rarely needs to be tested.
Context
The idea for this check is adopted from the checks implemented in Cevelop IDE.
This contribution is part of a project which aims to port Cevelop's built-in checks to other IDEs.
We are happy to receive any suggestions for improvement.
Would there be a way to extract these names (cin, cout, ...) into a separate variable? In my opinion, this would make the AST matchers cleaner and easier to read.
E.g., I was not able to find an overload of hasAnyName() which takes a std:.vector as argument.
Looking forward to hearing from you.
Thanks for any feedback in advance.