[cfe-commits] r41574 - in /cfe/trunk: Sema/SemaExpr.cpp include/clang/Basic/DiagnosticKinds.def test/Sema/floating-point-compare.c
Ted Kremenek
kremenek at apple.com
Wed Aug 29 11:06:12 PDT 2007
Author: kremenek
Date: Wed Aug 29 13:06:12 2007
New Revision: 41574
URL: http://llvm.org/viewvc/llvm-project?rev=41574&view=rev
Log:
Added checking (during parsing) of comparison of floating point values using == or !=.
This is the same functionality gcc provides via --Wfloat-equal.
Added:
cfe/trunk/test/Sema/floating-point-compare.c
Modified:
cfe/trunk/Sema/SemaExpr.cpp
cfe/trunk/include/clang/Basic/DiagnosticKinds.def
Modified: cfe/trunk/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaExpr.cpp?rev=41574&r1=41573&r2=41574&view=diff
==============================================================================
--- cfe/trunk/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/Sema/SemaExpr.cpp Wed Aug 29 13:06:12 2007
@@ -1157,6 +1157,9 @@
if (lType->isRealType() && rType->isRealType())
return Context.IntTy;
} else {
+ if (lType->isRealType() && rType->isRealType())
+ Diag(loc, diag::warn_floatingpoint_eq);
+
if (lType->isArithmeticType() && rType->isArithmeticType())
return Context.IntTy;
}
Modified: cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=41574&r1=41573&r2=41574&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Wed Aug 29 13:06:12 2007
@@ -696,7 +696,7 @@
DIAG(warn_unused_expr, WARNING,
"expression result unused")
-// Extra checking for finding simple bugs.
+// CHECK: printf format string errors
DIAG(warn_printf_not_string_constant, WARNING,
"format string is not a string literal (potentially insecure)")
DIAG(warn_printf_write_back, WARNING,
@@ -716,11 +716,16 @@
DIAG(warn_printf_format_string_contains_null_char, WARNING,
"format string contains '\\0' within the string body")
+// CHECK: returning address/reference of stack memory
DIAG(warn_ret_stack_addr, WARNING,
"address of stack memory associated with local variable '%0' returned")
DIAG(warn_ret_stack_ref, WARNING,
"reference to stack memory associated with local variable '%0' returned")
+// CHECK: floating point values should not use "==" or "!="
+DIAG(warn_floatingpoint_eq, WARNING,
+ "comparing floating point with == or != is unsafe")
+
// CFString checking
DIAG(err_cfstring_literal_not_string_constant, ERROR,
"CFString literal is not a string constant")
Added: cfe/trunk/test/Sema/floating-point-compare.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/floating-point-compare.c?rev=41574&view=auto
==============================================================================
--- cfe/trunk/test/Sema/floating-point-compare.c (added)
+++ cfe/trunk/test/Sema/floating-point-compare.c Wed Aug 29 13:06:12 2007
@@ -0,0 +1,9 @@
+// RUN: clang -parse-ast-check %s
+
+int foo(float x, float y) {
+ return x == y; // expected-warning {{comparing floating point with ==}}
+}
+
+int bar(float x, float y) {
+ return x != y; // expected-warning {{comparing floating point with ==}}
+}
\ No newline at end of file
More information about the cfe-commits
mailing list