aboutsummaryrefslogtreecommitdiff
path: root/clang-tools-extra/clang-tidy/cert/FloatLoopCounter.cpp
diff options
context:
space:
mode:
authorAaron Ballman <aaron@aaronballman.com>2016-02-19 14:03:20 +0000
committerAaron Ballman <aaron@aaronballman.com>2016-02-19 14:03:20 +0000
commit611d2e4ee6a5150b45659abafd76e5917d4382a7 (patch)
tree3872485262322b245a83bc1c270fba2f39c1b786 /clang-tools-extra/clang-tidy/cert/FloatLoopCounter.cpp
parent7ca8a826f407e4d408e503afa14df9b7f147ba11 (diff)
downloadllvm-611d2e4ee6a5150b45659abafd76e5917d4382a7.zip
llvm-611d2e4ee6a5150b45659abafd76e5917d4382a7.tar.gz
llvm-611d2e4ee6a5150b45659abafd76e5917d4382a7.tar.bz2
Add a new check, cert-flp30-c, that diagnoses loop induction expressions of floating-point type. This check corresponds to the CERT secure coding rule: https://www.securecoding.cert.org/confluence/display/c/FLP30-C.+Do+not+use+floating-point+variables+as+loop+counters
llvm-svn: 261324
Diffstat (limited to 'clang-tools-extra/clang-tidy/cert/FloatLoopCounter.cpp')
-rw-r--r--clang-tools-extra/clang-tidy/cert/FloatLoopCounter.cpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-tidy/cert/FloatLoopCounter.cpp b/clang-tools-extra/clang-tidy/cert/FloatLoopCounter.cpp
new file mode 100644
index 0000000..e92552e
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/cert/FloatLoopCounter.cpp
@@ -0,0 +1,35 @@
+//===--- FloatLoopCounter.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 "FloatLoopCounter.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace cert {
+
+void FloatLoopCounter::registerMatchers(MatchFinder *Finder) {
+ Finder->addMatcher(
+ forStmt(hasIncrement(expr(hasType(realFloatingPointType())))).bind("for"),
+ this);
+}
+
+void FloatLoopCounter::check(const MatchFinder::MatchResult &Result) {
+ const auto *FS = Result.Nodes.getNodeAs<ForStmt>("for");
+
+ diag(FS->getInc()->getExprLoc(), "loop induction expression should not have "
+ "floating-point type");
+}
+
+} // namespace cert
+} // namespace tidy
+} // namespace clang