Index: clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp =================================================================== --- clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp +++ clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp @@ -20,6 +20,7 @@ #include "FoldInitTypeCheck.h" #include "ForwardDeclarationNamespaceCheck.h" #include "ForwardingReferenceOverloadCheck.h" +#include "FsetposArgumentCheck.h" #include "InaccurateEraseCheck.h" #include "IncorrectRoundingsCheck.h" #include "IntegerDivisionCheck.h" @@ -81,6 +82,8 @@ "bugprone-forward-declaration-namespace"); CheckFactories.registerCheck( "bugprone-forwarding-reference-overload"); + CheckFactories.registerCheck( + "bugprone-fsetpos-argument-check"); CheckFactories.registerCheck( "bugprone-inaccurate-erase"); CheckFactories.registerCheck( Index: clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt =================================================================== --- clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt +++ clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt @@ -12,6 +12,7 @@ FoldInitTypeCheck.cpp ForwardDeclarationNamespaceCheck.cpp ForwardingReferenceOverloadCheck.cpp + FsetposArgumentCheck.cpp InaccurateEraseCheck.cpp IncorrectRoundingsCheck.cpp IntegerDivisionCheck.cpp Index: clang-tools-extra/clang-tidy/bugprone/FsetposArgumentCheck.h =================================================================== --- /dev/null +++ clang-tools-extra/clang-tidy/bugprone/FsetposArgumentCheck.h @@ -0,0 +1,35 @@ +//===--- FsetposArgumentCheck.h - clang-tidy ----------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_FSETPOSARGUMENTCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_FSETPOSARGUMENTCHECK_H + +#include "../ClangTidyCheck.h" + +namespace clang { +namespace tidy { +namespace bugprone { + +/// Finds call of 'fsetpos' functions, which do not have file position indicator +/// obtained from 'fgetpos'. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone-fsetpos-argument-check.html +class FsetposArgumentCheck : public ClangTidyCheck { +public: + FsetposArgumentCheck(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_FSETPOSARGUMENTCHECK_H Index: clang-tools-extra/clang-tidy/bugprone/FsetposArgumentCheck.cpp =================================================================== --- /dev/null +++ clang-tools-extra/clang-tidy/bugprone/FsetposArgumentCheck.cpp @@ -0,0 +1,60 @@ +//===--- FsetposArgumentCheck.cpp - clang-tidy ------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "FsetposArgumentCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace bugprone { + +void FsetposArgumentCheck::registerMatchers(MatchFinder *Finder) { + const auto Offset = declRefExpr(to(varDecl().bind("offset"))); + const auto WithOffset = hasArgument( + 1, + ignoringImpCasts(anyOf(unaryOperator(hasUnaryOperand(Offset)), Offset))); + + const auto IsOffset = declRefExpr(to(varDecl(equalsBoundNode("offset")))); + const auto HasOffset = hasAnyArgument(ignoringImpCasts( + anyOf(unaryOperator(hasUnaryOperand(IsOffset)), IsOffset))); + + const auto OffsetInWrongFunction = hasAncestor(functionDecl( + hasDescendant(callExpr(HasOffset, unless(callee(functionDecl(hasAnyName( + "::fgetpos", "::fsetpos"))))) + .bind("non-fpos")))); + const auto OffsetInFgetpos = hasAncestor(functionDecl(hasDescendant( + callExpr(HasOffset, callee(functionDecl(hasName("::fgetpos"))))))); + + Finder->addMatcher( + callExpr(WithOffset, callee(functionDecl(hasName("::fsetpos"))), + anyOf(OffsetInWrongFunction, unless(OffsetInFgetpos))) + .bind("fsetpos"), + this); +} + +void FsetposArgumentCheck::check(const MatchFinder::MatchResult &Result) { + const auto *MatchedDecl = Result.Nodes.getNodeAs("fsetpos"); + + diag(MatchedDecl->getBeginLoc(), + "file position indicator should be obtained only from 'fgetpos'"); + + const auto *WrongFunction = Result.Nodes.getNodeAs("non-fpos"); + if (WrongFunction) + diag(WrongFunction->getBeginLoc(), + "fpos_t object should be passed as an argument only to 'fsetpos' or " + "'fgetpos' functions.", + DiagnosticIDs::Note); +} + +} // namespace bugprone +} // namespace tidy +} // namespace clang Index: clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp =================================================================== --- clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp +++ clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp @@ -10,6 +10,7 @@ #include "../ClangTidyModule.h" #include "../ClangTidyModuleRegistry.h" #include "../bugprone/UnhandledSelfAssignmentCheck.h" +#include "../bugprone/FsetposArgumentCheck.h" #include "../google/UnnamedNamespaceInHeaderCheck.h" #include "../misc/NewDeleteOverloadsCheck.h" #include "../misc/NonCopyableObjects.h" @@ -75,6 +76,7 @@ // FLP CheckFactories.registerCheck("cert-flp30-c"); // FIO + CheckFactories.registerCheck("cert-fio44-c"); CheckFactories.registerCheck("cert-fio38-c"); // ERR CheckFactories.registerCheck("cert-err34-c"); Index: clang-tools-extra/docs/ReleaseNotes.rst =================================================================== --- clang-tools-extra/docs/ReleaseNotes.rst +++ clang-tools-extra/docs/ReleaseNotes.rst @@ -194,6 +194,12 @@ against self-assignment either by checking self-assignment explicitly or using the copy-and-swap or the copy-and-move method. +- New :doc:`bugprone-fsetpos-argument-check + ` check. + + Finds call of ``fsetpos`` functions, which does not have file position indicator + obtained from ``fgetpos``. + - New :doc:`fuchsia-default-arguments-calls ` check. @@ -264,6 +270,11 @@ :doc:`bugprone-unhandled-self-assignment ` was added. +- New alias :doc:`cert-fio44-c + ` to + :doc:`bugprone-fsetpos-argument-check + ` was added. + - New alias :doc:`cppcoreguidelines-explicit-virtual-functions ` to :doc:`modernize-use-override Index: clang-tools-extra/docs/clang-tidy/checks/bugprone-fsetpos-argument-check.rst =================================================================== --- /dev/null +++ clang-tools-extra/docs/clang-tidy/checks/bugprone-fsetpos-argument-check.rst @@ -0,0 +1,37 @@ +.. title:: clang-tidy - bugprone-fsetpos-argument-check + +bugprone-fsetpos-argument-check +======================== + +Finds call of ``fsetpos`` functions, which does not have file position indicator +obtained from ``fgetpos``. + +.. code-block:: c + + #include + #include + + int opener(FILE *file) { + int rc; + fpos_t offset; + + memset(&offset, 0, sizeof(offset)); + + if (file == NULL) { + return -1; + } + + /* Read in data from file */ + + rc = fsetpos(file, &offset); + if (rc != 0 ) { + return rc; + } + + return 0; + } + + +This check corresponds to the CERT C++ Coding Standard rule +`FIO44-C. Only use values for fsetpos() that are returned from fgetpos() +`_. Index: clang-tools-extra/docs/clang-tidy/checks/cert-fio44-c.rst =================================================================== --- /dev/null +++ clang-tools-extra/docs/clang-tidy/checks/cert-fio44-c.rst @@ -0,0 +1,10 @@ +.. title:: clang-tidy - cert-fio44-c +.. meta:: + :http-equiv=refresh: 5;URL=bugprone-fsetpos-argument-check.html + +cert-fio44-c +============ + +The cert-fio44-c check is an alias, please see +`bugprone-fsetpos-argument-check `_ +for more information. Index: clang-tools-extra/docs/clang-tidy/checks/list.rst =================================================================== --- clang-tools-extra/docs/clang-tidy/checks/list.rst +++ clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -47,6 +47,7 @@ bugprone-fold-init-type bugprone-forward-declaration-namespace bugprone-forwarding-reference-overload + bugprone-fsetpos-argument-check bugprone-inaccurate-erase bugprone-incorrect-roundings bugprone-integer-division @@ -95,6 +96,7 @@ cert-err60-cpp cert-err61-cpp (redirects to misc-throw-by-value-catch-by-reference) cert-fio38-c (redirects to misc-non-copyable-objects) + cert-fio44-c cert-flp30-c cert-msc30-c (redirects to cert-msc50-cpp) cert-msc32-c (redirects to cert-msc51-cpp) Index: clang-tools-extra/test/clang-tidy/bugprone-fsetpos-argument-check.cpp =================================================================== --- /dev/null +++ clang-tools-extra/test/clang-tidy/bugprone-fsetpos-argument-check.cpp @@ -0,0 +1,59 @@ +// RUN: %check_clang_tidy %s bugprone-fsetpos-argument-check %t + +typedef unsigned int size_t; +class FILE; +struct fpos_t {}; +int fsetpos(FILE *stream, const fpos_t *pos); +int fgetpos(FILE *stream, fpos_t *pos); +void *memset(void *ptr, int value, size_t num); + +//--------------------------------------------------------------------- + +int opener1(FILE *file) { + int rc; + fpos_t offset; + + if (file == nullptr ) { + return -1; + } + + rc = fgetpos(file, &offset); + if (rc != 0 ) { + return rc; + } + + /* Read in data from file */ + + rc = fsetpos(file, &offset); + if (rc != 0 ) { + return rc; + } + + return 0; +} + +//--------------------------------------------------------------------- + +int opener2(FILE *file) { + int rc; + fpos_t offset; + + memset(&offset, 0, sizeof(offset)); + + if (file == nullptr) { + return -1; + } + + /* Read in data from file */ + + rc = fsetpos(file, &offset); + // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: file position indicator should be obtained only from 'fgetpos' [bugprone-fsetpos-argument-check] + if (rc != 0 ) { + return rc; + } + + return 0; +} + + +//---------------------------------------------------------------------