DerWaschbar updated this revision to Diff 262383.
DerWaschbar marked 7 inline comments as done.
DerWaschbar added a comment.
- Removed unnecessary empty lines.
- Added double back-ticks to highlight language constructs.
- Indented code in documentation.
Repository:
rCTE Clang Tools Extra
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D79437/new/
https://reviews.llvm.org/D79437
Files:
clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
clang-tools-extra/clang-tidy/bugprone/FsetposArgumentCheck.cpp
clang-tools-extra/clang-tidy/bugprone/FsetposArgumentCheck.h
clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/docs/clang-tidy/checks/bugprone-fsetpos-argument-check.rst
clang-tools-extra/docs/clang-tidy/checks/cert-fio44-c.rst
clang-tools-extra/docs/clang-tidy/checks/list.rst
clang-tools-extra/test/clang-tidy/bugprone-fsetpos-argument-check.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;
+}
+
+
+//---------------------------------------------------------------------
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-err61-cpp>
cert-fio38-c (redirects to misc-non-copyable-objects) <cert-fio38-c>
+ cert-fio44-c
cert-flp30-c
cert-msc30-c (redirects to cert-msc50-cpp) <cert-msc30-c>
cert-msc32-c (redirects to cert-msc51-cpp) <cert-msc32-c>
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 <bugprone-fsetpos-argument-check.html>`_
+for more information.
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 <stdio.h>
+ #include <string.h>
+
+ 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()
+<https://wiki.sei.cmu.edu/confluence/pages/viewpage.action?pageId=87152071>`_.
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
+ <clang-tidy/checks/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
<clang-tidy/checks/fuchsia-default-arguments-calls>` check.
@@ -264,6 +270,11 @@
:doc:`bugprone-unhandled-self-assignment
<clang-tidy/checks/bugprone-unhandled-self-assignment>` was added.
+- New alias :doc:`cert-fio44-c
+ <clang-tidy/checks/cert-fio44-c>` to
+ :doc:`bugprone-fsetpos-argument-check
+ <clang-tidy/checks/bugprone-bugprone-fsetpos-argument-check>` was added.
+
- New alias :doc:`cppcoreguidelines-explicit-virtual-functions
<clang-tidy/checks/cppcoreguidelines-explicit-virtual-functions>` to
:doc:`modernize-use-override
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<FloatLoopCounter>("cert-flp30-c");
// FIO
+ CheckFactories.registerCheck<bugprone::FsetposArgumentCheck>("cert-fio44-c");
CheckFactories.registerCheck<misc::NonCopyableObjectsCheck>("cert-fio38-c");
// ERR
CheckFactories.registerCheck<StrToNumCheck>("cert-err34-c");
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 <iostream>
+
+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<CallExpr>("fsetpos");
+
+ diag(MatchedDecl->getBeginLoc(),
+ "file position indicator should be obtained only from 'fgetpos'");
+
+ const auto *WrongFunction = Result.Nodes.getNodeAs<CallExpr>("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/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/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<ForwardingReferenceOverloadCheck>(
"bugprone-forwarding-reference-overload");
+ CheckFactories.registerCheck<FsetposArgumentCheck>(
+ "bugprone-fsetpos-argument-check");
CheckFactories.registerCheck<InaccurateEraseCheck>(
"bugprone-inaccurate-erase");
CheckFactories.registerCheck<IncorrectRoundingsCheck>(
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits