Index: clang-tidy/bugprone/BugproneTidyModule.cpp =================================================================== --- clang-tidy/bugprone/BugproneTidyModule.cpp +++ clang-tidy/bugprone/BugproneTidyModule.cpp @@ -22,6 +22,7 @@ #include "InaccurateEraseCheck.h" #include "IncorrectRoundingsCheck.h" #include "IntegerDivisionCheck.h" +#include "IoFunctionsMisusedCheck.h" #include "LambdaFunctionNameCheck.h" #include "MacroParenthesesCheck.h" #include "MacroRepeatedSideEffectsCheck.h" @@ -79,6 +80,8 @@ "bugprone-incorrect-roundings"); CheckFactories.registerCheck( "bugprone-integer-division"); + CheckFactories.registerCheck( + "bugprone-io-functions-misused"); CheckFactories.registerCheck( "bugprone-lambda-function-name"); CheckFactories.registerCheck( Index: clang-tidy/bugprone/CMakeLists.txt =================================================================== --- clang-tidy/bugprone/CMakeLists.txt +++ clang-tidy/bugprone/CMakeLists.txt @@ -13,6 +13,7 @@ InaccurateEraseCheck.cpp IncorrectRoundingsCheck.cpp IntegerDivisionCheck.cpp + IoFunctionsMisusedCheck.cpp LambdaFunctionNameCheck.cpp MacroParenthesesCheck.cpp MacroRepeatedSideEffectsCheck.cpp Index: clang-tidy/bugprone/IoFunctionsMisusedCheck.h =================================================================== --- /dev/null +++ clang-tidy/bugprone/IoFunctionsMisusedCheck.h @@ -0,0 +1,38 @@ +//===--- IoFunctionsMisusedCheck.h - clang-tidy------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_IO_FUNCTIONS_MISUSED_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_IO_FUNCTIONS_MISUSED_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace bugprone { + +/// This check warns for cases when the return value of certain standard +/// iostream C functions (fgetwc(), getwc(), getwchar(), istream::get()) return +/// int value which is then incorrectly stored in char typed values which can +/// cause overflow at type conversion. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone-io-functions-misused.html +class IoFunctionsMisusedCheck : public ClangTidyCheck { +public: + IoFunctionsMisusedCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace bugprone +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_IO_FUNCTIONS_MISUSED_H Index: clang-tidy/bugprone/IoFunctionsMisusedCheck.cpp =================================================================== --- /dev/null +++ clang-tidy/bugprone/IoFunctionsMisusedCheck.cpp @@ -0,0 +1,60 @@ +//===--- IoFunctionsMisusedCheck.cpp - clang-tidy--------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "IoFunctionsMisusedCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace bugprone { + +void IoFunctionsMisusedCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + implicitCastExpr( + hasImplicitDestinationType(isAnyCharacter()), + anyOf( + has(callExpr(callee( + functionDecl(hasAnyName("::getwchar", "::getwc", "::fgetwc")) + .bind("FuncDecl")))), + has(callExpr(callee( + functionDecl(hasAnyName("::std::getwchar", "::std::getwc", + "::std::fgetwc")) + .bind("FuncDecl")))), + has(cxxMemberCallExpr( + on(hasType(namedDecl(hasAnyName("istream")))), + callee(cxxMethodDecl(hasName("get")).bind("DeclOfGet"))))), + hasCastKind(CK_IntegralCast)) + .bind("ImplicitCast"), + this); +} + +void IoFunctionsMisusedCheck::check(const MatchFinder::MatchResult &Result) { + const auto *MatchedImplicitCastToChar = + Result.Nodes.getNodeAs("ImplicitCast"); + const auto *FuncDecl = Result.Nodes.getNodeAs("FuncDecl"); + const auto *DeclGet = Result.Nodes.getNodeAs("DeclOfGet"); + + auto Diag = diag( + MatchedImplicitCastToChar->getLocStart(), + "consider to cast the return value of %0 from type integer to type char, " + "possible loss of precision if an error has occurred or the end " + "of file has been reached"); + + if (FuncDecl) + Diag << FuncDecl; + else + Diag << DeclGet; +} + +} // namespace bugprone +} // namespace tidy +} // namespace clang Index: docs/ReleaseNotes.rst =================================================================== --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -57,6 +57,12 @@ Improvements to clang-tidy -------------------------- +- New `bugprone-io-functions-misused + `_ check + + Check if the ``fgetwc()``, ``getwc()``, ``getwchar()``, ``istream::get()`` standard iostream C functions + return values incorrectly stored in char type value. + - The checks profiling info can now be stored as JSON files for futher post-processing and analysis. Index: docs/clang-tidy/checks/bugprone-io-functions-misused.rst =================================================================== --- /dev/null +++ docs/clang-tidy/checks/bugprone-io-functions-misused.rst @@ -0,0 +1,34 @@ +.. title:: clang-tidy - bugprone-io-functions-misused + +bugprone-io-functions-misused +============================= + +This check warns for cases when the return value of certain standard iostream C functions +(``fgetwc()``, ``getwc()``, ``getwchar()``, ``istream::get()``) return int value which is then incorrectly stored +in char typed values which can cause overflow at type conversion. +This check is valid in C and C++. + +Checking for implicit cast from integer to char. + +.. code-block:: c++ + + char c; + c = std::getwchar(); // Implicit cast from integer to char + +Check will warns for ``fgetwc()``, ``getwc()``, ``getwchar()`` functions from global or 'std' namespace +and will warn for get function from istream. + +.. code-block:: c++ + + namespace ownnamespace { ... } + std::istream is = ... + char c; + c = is.get(); // Warning about incorrectly stored int value + c = ownnamespace::get(); // Won't warn + +Check will also warns for incorrectly used functions in parameter passing. + +.. code-block:: c++ + + void f(char c){ ... } + f(std::fgetwc( ... )); // Warning about passed int value instead of char Index: docs/clang-tidy/checks/list.rst =================================================================== --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -30,6 +30,7 @@ bugprone-inaccurate-erase bugprone-incorrect-roundings bugprone-integer-division + bugprone-io-functions-misused bugprone-lambda-function-name bugprone-macro-parentheses bugprone-macro-repeated-side-effects Index: test/clang-tidy/bugprone-io-functions-misused.cpp =================================================================== --- /dev/null +++ test/clang-tidy/bugprone-io-functions-misused.cpp @@ -0,0 +1,74 @@ +// RUN: %check_clang_tidy %s bugprone-io-functions-misused %t + +typedef int wint_t; +typedef void *FILE; + +namespace std { +wint_t getwchar(); +wint_t getwc(FILE); +wint_t fgetwc(FILE); +class istream { +public: + wint_t get(); +}; +} // namespace std + +int char_io_getwchar() { + char c; + c = std::getwchar(); + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: consider to cast the return value of 'getwchar' from type integer to type char, possible loss of precision if an error has occurred or the end of file has been reached [bugprone-io-functions-misused] + return c; +} + +int char_io_getwc(FILE *fp) { + char c; + c = std::getwc(fp); + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: consider to cast the return value of 'getwc' from type integer to type char, possible loss of precision if an error has occurred or the end of file has been reached [bugprone-io-functions-misused] + return c; +} + +int char_io_fgetwc(FILE *fp) { + char c; + c = std::fgetwc(fp); + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: consider to cast the return value of 'fgetwc' from type integer to type char, possible loss of precision if an error has occurred or the end of file has been reached [bugprone-io-functions-misused] + return c; +} + +int char_io_get(std::istream &is) { + char c; + c = is.get(); + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: consider to cast the return value of 'get' from type integer to type char, possible loss of precision if an error has occurred or the end of file has been reached [bugprone-io-functions-misused] + return c; +} + +void char_type_in_argument(char c) {} + +void call_char_type_in_argument_with_getwchar() { + char_type_in_argument(std::getwchar()); + // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: consider to cast the return value of 'getwchar' from type integer to type char, possible loss of precision if an error has occurred or the end of file has been reached [bugprone-io-functions-misused] +} + +// Negatives +int int_io_getwchar() { + int x; + x = std::getwchar(); + return x; +} + +int int_io_getwc(FILE *fp) { + int x; + x = std::getwc(fp); + return x; +} + +int int_io_fgetwc(FILE *fp) { + int x; + x = std::fgetwc(fp); + return x; +} + +int int_io_get(std::istream &is) { + int x; + x = is.get(); + return x; +}