diff options
| author | Ted Kremenek <kremenek@apple.com> | 2007-08-29 18:06:12 +0000 |
|---|---|---|
| committer | Ted Kremenek <kremenek@apple.com> | 2007-08-29 18:06:12 +0000 |
| commit | 5ccf0d832de28ff2ea2869f98fe8d62a126b6c92 (patch) | |
| tree | 2caf48ddd80b5d85730497a6f19e141232a62d60 | |
| parent | 8a241f9359d6d09c004fbd5f1d1cc59cbf3be336 (diff) | |
| download | bcm5719-llvm-5ccf0d832de28ff2ea2869f98fe8d62a126b6c92.tar.gz bcm5719-llvm-5ccf0d832de28ff2ea2869f98fe8d62a126b6c92.zip | |
Added checking (during parsing) of comparison of floating point values using == or !=.
This is the same functionality gcc provides via --Wfloat-equal.
llvm-svn: 41574
| -rw-r--r-- | clang/Sema/SemaExpr.cpp | 3 | ||||
| -rw-r--r-- | clang/include/clang/Basic/DiagnosticKinds.def | 7 | ||||
| -rw-r--r-- | clang/test/Sema/floating-point-compare.c | 9 |
3 files changed, 18 insertions, 1 deletions
diff --git a/clang/Sema/SemaExpr.cpp b/clang/Sema/SemaExpr.cpp index 9208db26a8e..b613cd90a2c 100644 --- a/clang/Sema/SemaExpr.cpp +++ b/clang/Sema/SemaExpr.cpp @@ -1157,6 +1157,9 @@ inline QualType Sema::CheckCompareOperands( // C99 6.5.8 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; } diff --git a/clang/include/clang/Basic/DiagnosticKinds.def b/clang/include/clang/Basic/DiagnosticKinds.def index f2437d6554b..8b1b1430e74 100644 --- a/clang/include/clang/Basic/DiagnosticKinds.def +++ b/clang/include/clang/Basic/DiagnosticKinds.def @@ -696,7 +696,7 @@ DIAG(err_typecheck_choose_expr_requires_constant, ERROR, 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_is_wide_literal, WARNING, 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") diff --git a/clang/test/Sema/floating-point-compare.c b/clang/test/Sema/floating-point-compare.c new file mode 100644 index 00000000000..008bedf9776 --- /dev/null +++ b/clang/test/Sema/floating-point-compare.c @@ -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 |

